blob: dc7c5c1231df2ed6c3216c91cfd0eb3fc032d1ca [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;
1713
dea31012005-04-17 16:05:31 -05001714 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001715
dea31012005-04-17 16:05:31 -05001716 if (mp) {
1717 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1718 kfree(mp);
1719 }
James Smart7054a602007-04-25 09:52:34 -04001720
James Smart04c68492009-05-22 14:52:52 -04001721 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1722 (phba->sli_rev == LPFC_SLI_REV4))
1723 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1724
James Smart7054a602007-04-25 09:52:34 -04001725 /*
1726 * If a REG_LOGIN succeeded after node is destroyed or node
1727 * is in re-discovery driver need to cleanup the RPI.
1728 */
James Smart2e0fef82007-06-17 19:56:36 -05001729 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001730 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1731 !pmb->u.mb.mbxStatus) {
1732 rpi = pmb->u.mb.un.varWords[0];
1733 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1734 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001735 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001736 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1737 if (rc != MBX_NOT_FINISHED)
1738 return;
1739 }
1740
James Smart04c68492009-05-22 14:52:52 -04001741 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1742 lpfc_sli4_mbox_cmd_free(phba, pmb);
1743 else
1744 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001745}
1746
James Smarte59058c2008-08-24 21:49:00 -04001747/**
James Smart3621a712009-04-06 18:47:14 -04001748 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001749 * @phba: Pointer to HBA context object.
1750 *
1751 * This function is called with no lock held. This function processes all
1752 * the completed mailbox commands and gives it to upper layers. The interrupt
1753 * service routine processes mailbox completion interrupt and adds completed
1754 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1755 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1756 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1757 * function returns the mailbox commands to the upper layer by calling the
1758 * completion handler function of each mailbox.
1759 **/
dea31012005-04-17 16:05:31 -05001760int
James Smart2e0fef82007-06-17 19:56:36 -05001761lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001762{
James Smart92d7f7b2007-06-17 19:56:38 -05001763 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001764 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001765 int rc;
1766 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001767
1768 phba->sli.slistat.mbox_event++;
1769
James Smart92d7f7b2007-06-17 19:56:38 -05001770 /* Get all completed mailboxe buffers into the cmplq */
1771 spin_lock_irq(&phba->hbalock);
1772 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1773 spin_unlock_irq(&phba->hbalock);
1774
dea31012005-04-17 16:05:31 -05001775 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001776 do {
1777 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1778 if (pmb == NULL)
1779 break;
1780
James Smart04c68492009-05-22 14:52:52 -04001781 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001782
James Smart858c9f62007-06-17 19:56:39 -05001783 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1784 if (pmb->vport) {
1785 lpfc_debugfs_disc_trc(pmb->vport,
1786 LPFC_DISC_TRC_MBOX_VPORT,
1787 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1788 (uint32_t)pmbox->mbxCommand,
1789 pmbox->un.varWords[0],
1790 pmbox->un.varWords[1]);
1791 }
1792 else {
1793 lpfc_debugfs_disc_trc(phba->pport,
1794 LPFC_DISC_TRC_MBOX,
1795 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1796 (uint32_t)pmbox->mbxCommand,
1797 pmbox->un.varWords[0],
1798 pmbox->un.varWords[1]);
1799 }
1800 }
1801
dea31012005-04-17 16:05:31 -05001802 /*
1803 * It is a fatal error if unknown mbox command completion.
1804 */
1805 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1806 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001807 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001808 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001809 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001810 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001811 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001812 pmbox->mbxCommand,
1813 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001814 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001815 phba->work_hs = HS_FFER3;
1816 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001817 continue;
dea31012005-04-17 16:05:31 -05001818 }
1819
dea31012005-04-17 16:05:31 -05001820 if (pmbox->mbxStatus) {
1821 phba->sli.slistat.mbox_stat_err++;
1822 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1823 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001824 lpfc_printf_log(phba, KERN_INFO,
1825 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001826 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001827 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001828 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001829 pmb->vport ? pmb->vport->vpi :0,
1830 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001831 lpfc_sli4_mbox_opcode_get(phba,
1832 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001833 pmbox->mbxStatus,
1834 pmbox->un.varWords[0],
1835 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001836 pmbox->mbxStatus = 0;
1837 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001838 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001839 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001840 continue;
dea31012005-04-17 16:05:31 -05001841 }
1842 }
1843
1844 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001845 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001846 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001847 "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 -05001848 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001849 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001850 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001851 pmb->mbox_cmpl,
1852 *((uint32_t *) pmbox),
1853 pmbox->un.varWords[0],
1854 pmbox->un.varWords[1],
1855 pmbox->un.varWords[2],
1856 pmbox->un.varWords[3],
1857 pmbox->un.varWords[4],
1858 pmbox->un.varWords[5],
1859 pmbox->un.varWords[6],
1860 pmbox->un.varWords[7]);
1861
James Smart92d7f7b2007-06-17 19:56:38 -05001862 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001863 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001864 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001865 return 0;
dea31012005-04-17 16:05:31 -05001866}
James Smart92d7f7b2007-06-17 19:56:38 -05001867
James Smarte59058c2008-08-24 21:49:00 -04001868/**
James Smart3621a712009-04-06 18:47:14 -04001869 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001870 * @phba: Pointer to HBA context object.
1871 * @pring: Pointer to driver SLI ring object.
1872 * @tag: buffer tag.
1873 *
1874 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1875 * is set in the tag the buffer is posted for a particular exchange,
1876 * the function will return the buffer without replacing the buffer.
1877 * If the buffer is for unsolicited ELS or CT traffic, this function
1878 * returns the buffer and also posts another buffer to the firmware.
1879 **/
James Smart76bb24e2007-10-27 13:38:00 -04001880static struct lpfc_dmabuf *
1881lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001882 struct lpfc_sli_ring *pring,
1883 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001884{
James Smart9f1e1b52008-12-04 22:39:40 -05001885 struct hbq_dmabuf *hbq_entry;
1886
James Smart76bb24e2007-10-27 13:38:00 -04001887 if (tag & QUE_BUFTAG_BIT)
1888 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001889 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1890 if (!hbq_entry)
1891 return NULL;
1892 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001893}
James Smart57127f12007-10-27 13:37:05 -04001894
James Smart3772a992009-05-22 14:50:54 -04001895/**
1896 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1897 * @phba: Pointer to HBA context object.
1898 * @pring: Pointer to driver SLI ring object.
1899 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1900 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1901 * @fch_type: the type for the first frame of the sequence.
1902 *
1903 * This function is called with no lock held. This function uses the r_ctl and
1904 * type of the received sequence to find the correct callback function to call
1905 * to process the sequence.
1906 **/
1907static int
1908lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1909 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1910 uint32_t fch_type)
1911{
1912 int i;
1913
1914 /* unSolicited Responses */
1915 if (pring->prt[0].profile) {
1916 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1917 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1918 saveq);
1919 return 1;
1920 }
1921 /* We must search, based on rctl / type
1922 for the right routine */
1923 for (i = 0; i < pring->num_mask; i++) {
1924 if ((pring->prt[i].rctl == fch_r_ctl) &&
1925 (pring->prt[i].type == fch_type)) {
1926 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1927 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1928 (phba, pring, saveq);
1929 return 1;
1930 }
1931 }
1932 return 0;
1933}
James Smarte59058c2008-08-24 21:49:00 -04001934
1935/**
James Smart3621a712009-04-06 18:47:14 -04001936 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001937 * @phba: Pointer to HBA context object.
1938 * @pring: Pointer to driver SLI ring object.
1939 * @saveq: Pointer to the unsolicited iocb.
1940 *
1941 * This function is called with no lock held by the ring event handler
1942 * when there is an unsolicited iocb posted to the response ring by the
1943 * firmware. This function gets the buffer associated with the iocbs
1944 * and calls the event handler for the ring. This function handles both
1945 * qring buffers and hbq buffers.
1946 * When the function returns 1 the caller can free the iocb object otherwise
1947 * upper layer functions will free the iocb objects.
1948 **/
dea31012005-04-17 16:05:31 -05001949static int
1950lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1951 struct lpfc_iocbq *saveq)
1952{
1953 IOCB_t * irsp;
1954 WORD5 * w5p;
1955 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001956 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001957 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001958 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001959
1960 match = 0;
1961 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001962
1963 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1964 if (pring->lpfc_sli_rcv_async_status)
1965 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1966 else
1967 lpfc_printf_log(phba,
1968 KERN_WARNING,
1969 LOG_SLI,
1970 "0316 Ring %d handler: unexpected "
1971 "ASYNC_STATUS iocb received evt_code "
1972 "0x%x\n",
1973 pring->ringno,
1974 irsp->un.asyncstat.evt_code);
1975 return 1;
1976 }
1977
James Smart3163f722008-02-08 18:50:25 -05001978 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1979 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1980 if (irsp->ulpBdeCount > 0) {
1981 dmzbuf = lpfc_sli_get_buff(phba, pring,
1982 irsp->un.ulpWord[3]);
1983 lpfc_in_buf_free(phba, dmzbuf);
1984 }
1985
1986 if (irsp->ulpBdeCount > 1) {
1987 dmzbuf = lpfc_sli_get_buff(phba, pring,
1988 irsp->unsli3.sli3Words[3]);
1989 lpfc_in_buf_free(phba, dmzbuf);
1990 }
1991
1992 if (irsp->ulpBdeCount > 2) {
1993 dmzbuf = lpfc_sli_get_buff(phba, pring,
1994 irsp->unsli3.sli3Words[7]);
1995 lpfc_in_buf_free(phba, dmzbuf);
1996 }
1997
1998 return 1;
1999 }
2000
James Smart92d7f7b2007-06-17 19:56:38 -05002001 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002002 if (irsp->ulpBdeCount != 0) {
2003 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002004 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002005 if (!saveq->context2)
2006 lpfc_printf_log(phba,
2007 KERN_ERR,
2008 LOG_SLI,
2009 "0341 Ring %d Cannot find buffer for "
2010 "an unsolicited iocb. tag 0x%x\n",
2011 pring->ringno,
2012 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002013 }
2014 if (irsp->ulpBdeCount == 2) {
2015 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002016 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002017 if (!saveq->context3)
2018 lpfc_printf_log(phba,
2019 KERN_ERR,
2020 LOG_SLI,
2021 "0342 Ring %d Cannot find buffer for an"
2022 " unsolicited iocb. tag 0x%x\n",
2023 pring->ringno,
2024 irsp->unsli3.sli3Words[7]);
2025 }
2026 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002027 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002028 if (irsp->ulpBdeCount != 0) {
2029 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2030 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002031 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002032 lpfc_printf_log(phba,
2033 KERN_ERR,
2034 LOG_SLI,
2035 "0343 Ring %d Cannot find "
2036 "buffer for an unsolicited iocb"
2037 ". tag 0x%x\n", pring->ringno,
2038 irsp->un.ulpWord[3]);
2039 }
2040 if (irsp->ulpBdeCount == 2) {
2041 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2042 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002043 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002044 lpfc_printf_log(phba,
2045 KERN_ERR,
2046 LOG_SLI,
2047 "0344 Ring %d Cannot find "
2048 "buffer for an unsolicited "
2049 "iocb. tag 0x%x\n",
2050 pring->ringno,
2051 irsp->unsli3.sli3Words[7]);
2052 }
2053 }
James Smart92d7f7b2007-06-17 19:56:38 -05002054 }
James Smart9c2face2008-01-11 01:53:18 -05002055 if (irsp->ulpBdeCount != 0 &&
2056 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2057 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2058 int found = 0;
2059
2060 /* search continue save q for same XRI */
2061 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2062 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2063 list_add_tail(&saveq->list, &iocbq->list);
2064 found = 1;
2065 break;
2066 }
2067 }
2068 if (!found)
2069 list_add_tail(&saveq->clist,
2070 &pring->iocb_continue_saveq);
2071 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2072 list_del_init(&iocbq->clist);
2073 saveq = iocbq;
2074 irsp = &(saveq->iocb);
2075 } else
2076 return 0;
2077 }
2078 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2079 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2080 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002081 Rctl = FC_RCTL_ELS_REQ;
2082 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002083 } else {
2084 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2085 Rctl = w5p->hcsw.Rctl;
2086 Type = w5p->hcsw.Type;
2087
2088 /* Firmware Workaround */
2089 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2090 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2091 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002092 Rctl = FC_RCTL_ELS_REQ;
2093 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002094 w5p->hcsw.Rctl = Rctl;
2095 w5p->hcsw.Type = Type;
2096 }
2097 }
James Smart92d7f7b2007-06-17 19:56:38 -05002098
James Smart3772a992009-05-22 14:50:54 -04002099 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002100 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002101 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002102 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002103 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002104
James Smart92d7f7b2007-06-17 19:56:38 -05002105 return 1;
dea31012005-04-17 16:05:31 -05002106}
2107
James Smarte59058c2008-08-24 21:49:00 -04002108/**
James Smart3621a712009-04-06 18:47:14 -04002109 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002110 * @phba: Pointer to HBA context object.
2111 * @pring: Pointer to driver SLI ring object.
2112 * @prspiocb: Pointer to response iocb object.
2113 *
2114 * This function looks up the iocb_lookup table to get the command iocb
2115 * corresponding to the given response iocb using the iotag of the
2116 * response iocb. This function is called with the hbalock held.
2117 * This function returns the command iocb object if it finds the command
2118 * iocb else returns NULL.
2119 **/
dea31012005-04-17 16:05:31 -05002120static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002121lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2122 struct lpfc_sli_ring *pring,
2123 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002124{
dea31012005-04-17 16:05:31 -05002125 struct lpfc_iocbq *cmd_iocb = NULL;
2126 uint16_t iotag;
2127
James Bottomley604a3e32005-10-29 10:28:33 -05002128 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002129
James Bottomley604a3e32005-10-29 10:28:33 -05002130 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2131 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002132 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002133 pring->txcmplq_cnt--;
2134 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002135 }
2136
dea31012005-04-17 16:05:31 -05002137 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002138 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002139 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002140 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002141 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002142 return NULL;
2143}
2144
James Smarte59058c2008-08-24 21:49:00 -04002145/**
James Smart3772a992009-05-22 14:50:54 -04002146 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2147 * @phba: Pointer to HBA context object.
2148 * @pring: Pointer to driver SLI ring object.
2149 * @iotag: IOCB tag.
2150 *
2151 * This function looks up the iocb_lookup table to get the command iocb
2152 * corresponding to the given iotag. This function is called with the
2153 * hbalock held.
2154 * This function returns the command iocb object if it finds the command
2155 * iocb else returns NULL.
2156 **/
2157static struct lpfc_iocbq *
2158lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2159 struct lpfc_sli_ring *pring, uint16_t iotag)
2160{
2161 struct lpfc_iocbq *cmd_iocb;
2162
2163 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2164 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2165 list_del_init(&cmd_iocb->list);
2166 pring->txcmplq_cnt--;
2167 return cmd_iocb;
2168 }
2169
2170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2171 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2172 iotag, phba->sli.last_iotag);
2173 return NULL;
2174}
2175
2176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002178 * @phba: Pointer to HBA context object.
2179 * @pring: Pointer to driver SLI ring object.
2180 * @saveq: Pointer to the response iocb to be processed.
2181 *
2182 * This function is called by the ring event handler for non-fcp
2183 * rings when there is a new response iocb in the response ring.
2184 * The caller is not required to hold any locks. This function
2185 * gets the command iocb associated with the response iocb and
2186 * calls the completion handler for the command iocb. If there
2187 * is no completion handler, the function will free the resources
2188 * associated with command iocb. If the response iocb is for
2189 * an already aborted command iocb, the status of the completion
2190 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2191 * This function always returns 1.
2192 **/
dea31012005-04-17 16:05:31 -05002193static int
James Smart2e0fef82007-06-17 19:56:36 -05002194lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002195 struct lpfc_iocbq *saveq)
2196{
James Smart2e0fef82007-06-17 19:56:36 -05002197 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002198 int rc = 1;
2199 unsigned long iflag;
2200
2201 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002202 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002203 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002204 spin_unlock_irqrestore(&phba->hbalock, iflag);
2205
dea31012005-04-17 16:05:31 -05002206 if (cmdiocbp) {
2207 if (cmdiocbp->iocb_cmpl) {
2208 /*
James Smartea2151b2008-09-07 11:52:10 -04002209 * If an ELS command failed send an event to mgmt
2210 * application.
2211 */
2212 if (saveq->iocb.ulpStatus &&
2213 (pring->ringno == LPFC_ELS_RING) &&
2214 (cmdiocbp->iocb.ulpCommand ==
2215 CMD_ELS_REQUEST64_CR))
2216 lpfc_send_els_failure_event(phba,
2217 cmdiocbp, saveq);
2218
2219 /*
dea31012005-04-17 16:05:31 -05002220 * Post all ELS completions to the worker thread.
2221 * All other are passed to the completion callback.
2222 */
2223 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002224 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2225 (cmdiocbp->iocb_flag &
2226 LPFC_DRIVER_ABORTED)) {
2227 spin_lock_irqsave(&phba->hbalock,
2228 iflag);
James Smart07951072007-04-25 09:51:38 -04002229 cmdiocbp->iocb_flag &=
2230 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002231 spin_unlock_irqrestore(&phba->hbalock,
2232 iflag);
James Smart07951072007-04-25 09:51:38 -04002233 saveq->iocb.ulpStatus =
2234 IOSTAT_LOCAL_REJECT;
2235 saveq->iocb.un.ulpWord[4] =
2236 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002237
2238 /* Firmware could still be in progress
2239 * of DMAing payload, so don't free data
2240 * buffer till after a hbeat.
2241 */
James Smart341af102010-01-26 23:07:37 -05002242 spin_lock_irqsave(&phba->hbalock,
2243 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002244 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002245 spin_unlock_irqrestore(&phba->hbalock,
2246 iflag);
2247 }
2248 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2249 (saveq->iocb_flag & LPFC_EXCHANGE_BUSY)) {
2250 /* Set cmdiocb flag for the exchange
2251 * busy so sgl (xri) will not be
2252 * released until the abort xri is
2253 * received from hba, clear the
2254 * LPFC_DRIVER_ABORTED bit in case
2255 * it was driver initiated abort.
2256 */
2257 spin_lock_irqsave(&phba->hbalock,
2258 iflag);
2259 cmdiocbp->iocb_flag &=
2260 ~LPFC_DRIVER_ABORTED;
2261 cmdiocbp->iocb_flag |=
2262 LPFC_EXCHANGE_BUSY;
2263 spin_unlock_irqrestore(&phba->hbalock,
2264 iflag);
2265 cmdiocbp->iocb.ulpStatus =
2266 IOSTAT_LOCAL_REJECT;
2267 cmdiocbp->iocb.un.ulpWord[4] =
2268 IOERR_ABORT_REQUESTED;
2269 /*
2270 * For SLI4, irsiocb contains NO_XRI
2271 * in sli_xritag, it shall not affect
2272 * releasing sgl (xri) process.
2273 */
2274 saveq->iocb.ulpStatus =
2275 IOSTAT_LOCAL_REJECT;
2276 saveq->iocb.un.ulpWord[4] =
2277 IOERR_SLI_ABORTED;
2278 spin_lock_irqsave(&phba->hbalock,
2279 iflag);
2280 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2281 spin_unlock_irqrestore(&phba->hbalock,
2282 iflag);
James Smart07951072007-04-25 09:51:38 -04002283 }
dea31012005-04-17 16:05:31 -05002284 }
James Smart2e0fef82007-06-17 19:56:36 -05002285 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002286 } else
2287 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002288 } else {
2289 /*
2290 * Unknown initiating command based on the response iotag.
2291 * This could be the case on the ELS ring because of
2292 * lpfc_els_abort().
2293 */
2294 if (pring->ringno != LPFC_ELS_RING) {
2295 /*
2296 * Ring <ringno> handler: unexpected completion IoTag
2297 * <IoTag>
2298 */
James Smarta257bf92009-04-06 18:48:10 -04002299 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002300 "0322 Ring %d handler: "
2301 "unexpected completion IoTag x%x "
2302 "Data: x%x x%x x%x x%x\n",
2303 pring->ringno,
2304 saveq->iocb.ulpIoTag,
2305 saveq->iocb.ulpStatus,
2306 saveq->iocb.un.ulpWord[4],
2307 saveq->iocb.ulpCommand,
2308 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002309 }
2310 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002311
dea31012005-04-17 16:05:31 -05002312 return rc;
2313}
2314
James Smarte59058c2008-08-24 21:49:00 -04002315/**
James Smart3621a712009-04-06 18:47:14 -04002316 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002317 * @phba: Pointer to HBA context object.
2318 * @pring: Pointer to driver SLI ring object.
2319 *
2320 * This function is called from the iocb ring event handlers when
2321 * put pointer is ahead of the get pointer for a ring. This function signal
2322 * an error attention condition to the worker thread and the worker
2323 * thread will transition the HBA to offline state.
2324 **/
James Smart2e0fef82007-06-17 19:56:36 -05002325static void
2326lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002327{
James Smart34b02dc2008-08-24 21:49:55 -04002328 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002329 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002330 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002331 * rsp ring <portRspMax>
2332 */
2333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002334 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002335 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002336 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002337 pring->numRiocb);
2338
James Smart2e0fef82007-06-17 19:56:36 -05002339 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002340
2341 /*
2342 * All error attention handlers are posted to
2343 * worker thread
2344 */
2345 phba->work_ha |= HA_ERATT;
2346 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002347
James Smart5e9d9b82008-06-14 22:52:53 -04002348 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002349
2350 return;
2351}
2352
James Smarte59058c2008-08-24 21:49:00 -04002353/**
James Smart3621a712009-04-06 18:47:14 -04002354 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002355 * @ptr: Pointer to address of HBA context object.
2356 *
2357 * This function is invoked by the Error Attention polling timer when the
2358 * timer times out. It will check the SLI Error Attention register for
2359 * possible attention events. If so, it will post an Error Attention event
2360 * and wake up worker thread to process it. Otherwise, it will set up the
2361 * Error Attention polling timer for the next poll.
2362 **/
2363void lpfc_poll_eratt(unsigned long ptr)
2364{
2365 struct lpfc_hba *phba;
2366 uint32_t eratt = 0;
2367
2368 phba = (struct lpfc_hba *)ptr;
2369
2370 /* Check chip HA register for error event */
2371 eratt = lpfc_sli_check_eratt(phba);
2372
2373 if (eratt)
2374 /* Tell the worker thread there is work to do */
2375 lpfc_worker_wake_up(phba);
2376 else
2377 /* Restart the timer for next eratt poll */
2378 mod_timer(&phba->eratt_poll, jiffies +
2379 HZ * LPFC_ERATT_POLL_INTERVAL);
2380 return;
2381}
2382
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002383
James Smarte59058c2008-08-24 21:49:00 -04002384/**
James Smart3621a712009-04-06 18:47:14 -04002385 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002386 * @phba: Pointer to HBA context object.
2387 * @pring: Pointer to driver SLI ring object.
2388 * @mask: Host attention register mask for this ring.
2389 *
2390 * This function is called from the interrupt context when there is a ring
2391 * event for the fcp ring. The caller does not hold any lock.
2392 * The function processes each response iocb in the response ring until it
2393 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2394 * LE bit set. The function will call the completion handler of the command iocb
2395 * if the response iocb indicates a completion for a command iocb or it is
2396 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2397 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002398 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002399 * to check it explicitly.
2400 */
2401int
James Smart2e0fef82007-06-17 19:56:36 -05002402lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2403 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002404{
James Smart34b02dc2008-08-24 21:49:55 -04002405 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002406 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002407 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002408 struct lpfc_iocbq *cmdiocbq = NULL;
2409 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002410 uint32_t status;
2411 uint32_t portRspPut, portRspMax;
2412 int rc = 1;
2413 lpfc_iocb_type type;
2414 unsigned long iflag;
2415 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002416
James Smart2e0fef82007-06-17 19:56:36 -05002417 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002418 pring->stats.iocb_event++;
2419
dea31012005-04-17 16:05:31 -05002420 /*
2421 * The next available response entry should never exceed the maximum
2422 * entries. If it does, treat it as an adapter hardware error.
2423 */
2424 portRspMax = pring->numRiocb;
2425 portRspPut = le32_to_cpu(pgp->rspPutInx);
2426 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002427 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002428 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002429 return 1;
2430 }
James Smart45ed1192009-10-02 15:17:02 -04002431 if (phba->fcp_ring_in_use) {
2432 spin_unlock_irqrestore(&phba->hbalock, iflag);
2433 return 1;
2434 } else
2435 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002436
2437 rmb();
2438 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002439 /*
2440 * Fetch an entry off the ring and copy it into a local data
2441 * structure. The copy involves a byte-swap since the
2442 * network byte order and pci byte orders are different.
2443 */
James Smarted957682007-06-17 19:56:37 -05002444 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002445 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002446
2447 if (++pring->rspidx >= portRspMax)
2448 pring->rspidx = 0;
2449
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002450 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2451 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002452 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002453 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002454 irsp = &rspiocbq.iocb;
2455
dea31012005-04-17 16:05:31 -05002456 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2457 pring->stats.iocb_rsp++;
2458 rsp_cmpl++;
2459
2460 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002461 /*
2462 * If resource errors reported from HBA, reduce
2463 * queuedepths of the SCSI device.
2464 */
2465 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2466 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2467 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002468 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002469 spin_lock_irqsave(&phba->hbalock, iflag);
2470 }
2471
dea31012005-04-17 16:05:31 -05002472 /* Rsp ring <ringno> error: IOCB */
2473 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002474 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002475 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002476 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002477 irsp->un.ulpWord[0],
2478 irsp->un.ulpWord[1],
2479 irsp->un.ulpWord[2],
2480 irsp->un.ulpWord[3],
2481 irsp->un.ulpWord[4],
2482 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002483 *(uint32_t *)&irsp->un1,
2484 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002485 }
2486
2487 switch (type) {
2488 case LPFC_ABORT_IOCB:
2489 case LPFC_SOL_IOCB:
2490 /*
2491 * Idle exchange closed via ABTS from port. No iocb
2492 * resources need to be recovered.
2493 */
2494 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002495 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002496 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002497 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002498 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002499 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002500 break;
2501 }
2502
James Bottomley604a3e32005-10-29 10:28:33 -05002503 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2504 &rspiocbq);
dea31012005-04-17 16:05:31 -05002505 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
James Smart2e0fef82007-06-17 19:56:36 -05002506 spin_unlock_irqrestore(&phba->hbalock,
2507 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002508 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2509 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002510 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002511 iflag);
2512 }
dea31012005-04-17 16:05:31 -05002513 break;
James Smarta4bc3372006-12-02 13:34:16 -05002514 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002515 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002516 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002517 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002518 break;
dea31012005-04-17 16:05:31 -05002519 default:
2520 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2521 char adaptermsg[LPFC_MAX_ADPTMSG];
2522 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2523 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2524 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002525 dev_warn(&((phba->pcidev)->dev),
2526 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002527 phba->brd_no, adaptermsg);
2528 } else {
2529 /* Unknown IOCB command */
2530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002531 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002532 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002533 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002534 irsp->ulpStatus,
2535 irsp->ulpIoTag,
2536 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002537 }
2538 break;
2539 }
2540
2541 /*
2542 * The response IOCB has been processed. Update the ring
2543 * pointer in SLIM. If the port response put pointer has not
2544 * been updated, sync the pgp->rspPutInx and fetch the new port
2545 * response put pointer.
2546 */
James Smarted957682007-06-17 19:56:37 -05002547 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002548
2549 if (pring->rspidx == portRspPut)
2550 portRspPut = le32_to_cpu(pgp->rspPutInx);
2551 }
2552
2553 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2554 pring->stats.iocb_rsp_full++;
2555 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2556 writel(status, phba->CAregaddr);
2557 readl(phba->CAregaddr);
2558 }
2559 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2560 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2561 pring->stats.iocb_cmd_empty++;
2562
2563 /* Force update of the local copy of cmdGetInx */
2564 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2565 lpfc_sli_resume_iocb(phba, pring);
2566
2567 if ((pring->lpfc_sli_cmd_available))
2568 (pring->lpfc_sli_cmd_available) (phba, pring);
2569
2570 }
2571
James Smart45ed1192009-10-02 15:17:02 -04002572 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002573 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002574 return rc;
2575}
2576
James Smarte59058c2008-08-24 21:49:00 -04002577/**
James Smart3772a992009-05-22 14:50:54 -04002578 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2579 * @phba: Pointer to HBA context object.
2580 * @pring: Pointer to driver SLI ring object.
2581 * @rspiocbp: Pointer to driver response IOCB object.
2582 *
2583 * This function is called from the worker thread when there is a slow-path
2584 * response IOCB to process. This function chains all the response iocbs until
2585 * seeing the iocb with the LE bit set. The function will call
2586 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2587 * completion of a command iocb. The function will call the
2588 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2589 * The function frees the resources or calls the completion handler if this
2590 * iocb is an abort completion. The function returns NULL when the response
2591 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2592 * this function shall chain the iocb on to the iocb_continueq and return the
2593 * response iocb passed in.
2594 **/
2595static struct lpfc_iocbq *
2596lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2597 struct lpfc_iocbq *rspiocbp)
2598{
2599 struct lpfc_iocbq *saveq;
2600 struct lpfc_iocbq *cmdiocbp;
2601 struct lpfc_iocbq *next_iocb;
2602 IOCB_t *irsp = NULL;
2603 uint32_t free_saveq;
2604 uint8_t iocb_cmd_type;
2605 lpfc_iocb_type type;
2606 unsigned long iflag;
2607 int rc;
2608
2609 spin_lock_irqsave(&phba->hbalock, iflag);
2610 /* First add the response iocb to the countinueq list */
2611 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2612 pring->iocb_continueq_cnt++;
2613
2614 /* Now, determine whetehr the list is completed for processing */
2615 irsp = &rspiocbp->iocb;
2616 if (irsp->ulpLe) {
2617 /*
2618 * By default, the driver expects to free all resources
2619 * associated with this iocb completion.
2620 */
2621 free_saveq = 1;
2622 saveq = list_get_first(&pring->iocb_continueq,
2623 struct lpfc_iocbq, list);
2624 irsp = &(saveq->iocb);
2625 list_del_init(&pring->iocb_continueq);
2626 pring->iocb_continueq_cnt = 0;
2627
2628 pring->stats.iocb_rsp++;
2629
2630 /*
2631 * If resource errors reported from HBA, reduce
2632 * queuedepths of the SCSI device.
2633 */
2634 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2635 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2636 spin_unlock_irqrestore(&phba->hbalock, iflag);
2637 phba->lpfc_rampdown_queue_depth(phba);
2638 spin_lock_irqsave(&phba->hbalock, iflag);
2639 }
2640
2641 if (irsp->ulpStatus) {
2642 /* Rsp ring <ringno> error: IOCB */
2643 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2644 "0328 Rsp Ring %d error: "
2645 "IOCB Data: "
2646 "x%x x%x x%x x%x "
2647 "x%x x%x x%x x%x "
2648 "x%x x%x x%x x%x "
2649 "x%x x%x x%x x%x\n",
2650 pring->ringno,
2651 irsp->un.ulpWord[0],
2652 irsp->un.ulpWord[1],
2653 irsp->un.ulpWord[2],
2654 irsp->un.ulpWord[3],
2655 irsp->un.ulpWord[4],
2656 irsp->un.ulpWord[5],
2657 *(((uint32_t *) irsp) + 6),
2658 *(((uint32_t *) irsp) + 7),
2659 *(((uint32_t *) irsp) + 8),
2660 *(((uint32_t *) irsp) + 9),
2661 *(((uint32_t *) irsp) + 10),
2662 *(((uint32_t *) irsp) + 11),
2663 *(((uint32_t *) irsp) + 12),
2664 *(((uint32_t *) irsp) + 13),
2665 *(((uint32_t *) irsp) + 14),
2666 *(((uint32_t *) irsp) + 15));
2667 }
2668
2669 /*
2670 * Fetch the IOCB command type and call the correct completion
2671 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2672 * get freed back to the lpfc_iocb_list by the discovery
2673 * kernel thread.
2674 */
2675 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2676 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2677 switch (type) {
2678 case LPFC_SOL_IOCB:
2679 spin_unlock_irqrestore(&phba->hbalock, iflag);
2680 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2681 spin_lock_irqsave(&phba->hbalock, iflag);
2682 break;
2683
2684 case LPFC_UNSOL_IOCB:
2685 spin_unlock_irqrestore(&phba->hbalock, iflag);
2686 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2687 spin_lock_irqsave(&phba->hbalock, iflag);
2688 if (!rc)
2689 free_saveq = 0;
2690 break;
2691
2692 case LPFC_ABORT_IOCB:
2693 cmdiocbp = NULL;
2694 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2695 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2696 saveq);
2697 if (cmdiocbp) {
2698 /* Call the specified completion routine */
2699 if (cmdiocbp->iocb_cmpl) {
2700 spin_unlock_irqrestore(&phba->hbalock,
2701 iflag);
2702 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2703 saveq);
2704 spin_lock_irqsave(&phba->hbalock,
2705 iflag);
2706 } else
2707 __lpfc_sli_release_iocbq(phba,
2708 cmdiocbp);
2709 }
2710 break;
2711
2712 case LPFC_UNKNOWN_IOCB:
2713 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2714 char adaptermsg[LPFC_MAX_ADPTMSG];
2715 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2716 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2717 MAX_MSG_DATA);
2718 dev_warn(&((phba->pcidev)->dev),
2719 "lpfc%d: %s\n",
2720 phba->brd_no, adaptermsg);
2721 } else {
2722 /* Unknown IOCB command */
2723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2724 "0335 Unknown IOCB "
2725 "command Data: x%x "
2726 "x%x x%x x%x\n",
2727 irsp->ulpCommand,
2728 irsp->ulpStatus,
2729 irsp->ulpIoTag,
2730 irsp->ulpContext);
2731 }
2732 break;
2733 }
2734
2735 if (free_saveq) {
2736 list_for_each_entry_safe(rspiocbp, next_iocb,
2737 &saveq->list, list) {
2738 list_del(&rspiocbp->list);
2739 __lpfc_sli_release_iocbq(phba, rspiocbp);
2740 }
2741 __lpfc_sli_release_iocbq(phba, saveq);
2742 }
2743 rspiocbp = NULL;
2744 }
2745 spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 return rspiocbp;
2747}
2748
2749/**
2750 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002751 * @phba: Pointer to HBA context object.
2752 * @pring: Pointer to driver SLI ring object.
2753 * @mask: Host attention register mask for this ring.
2754 *
James Smart3772a992009-05-22 14:50:54 -04002755 * This routine wraps the actual slow_ring event process routine from the
2756 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002757 **/
James Smart3772a992009-05-22 14:50:54 -04002758void
James Smart2e0fef82007-06-17 19:56:36 -05002759lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2760 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002761{
James Smart3772a992009-05-22 14:50:54 -04002762 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2763}
2764
2765/**
2766 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2767 * @phba: Pointer to HBA context object.
2768 * @pring: Pointer to driver SLI ring object.
2769 * @mask: Host attention register mask for this ring.
2770 *
2771 * This function is called from the worker thread when there is a ring event
2772 * for non-fcp rings. The caller does not hold any lock. The function will
2773 * remove each response iocb in the response ring and calls the handle
2774 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2775 **/
2776static void
2777lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2778 struct lpfc_sli_ring *pring, uint32_t mask)
2779{
James Smart34b02dc2008-08-24 21:49:55 -04002780 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002781 IOCB_t *entry;
2782 IOCB_t *irsp = NULL;
2783 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002784 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002785 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002786 uint32_t status;
dea31012005-04-17 16:05:31 -05002787
James Smart34b02dc2008-08-24 21:49:55 -04002788 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002789 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002790 pring->stats.iocb_event++;
2791
dea31012005-04-17 16:05:31 -05002792 /*
2793 * The next available response entry should never exceed the maximum
2794 * entries. If it does, treat it as an adapter hardware error.
2795 */
2796 portRspMax = pring->numRiocb;
2797 portRspPut = le32_to_cpu(pgp->rspPutInx);
2798 if (portRspPut >= portRspMax) {
2799 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002800 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002801 * rsp ring <portRspMax>
2802 */
James Smarted957682007-06-17 19:56:37 -05002803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002804 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002805 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002806 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002807
James Smart2e0fef82007-06-17 19:56:36 -05002808 phba->link_state = LPFC_HBA_ERROR;
2809 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002810
2811 phba->work_hs = HS_FFER3;
2812 lpfc_handle_eratt(phba);
2813
James Smart3772a992009-05-22 14:50:54 -04002814 return;
dea31012005-04-17 16:05:31 -05002815 }
2816
2817 rmb();
dea31012005-04-17 16:05:31 -05002818 while (pring->rspidx != portRspPut) {
2819 /*
2820 * Build a completion list and call the appropriate handler.
2821 * The process is to get the next available response iocb, get
2822 * a free iocb from the list, copy the response data into the
2823 * free iocb, insert to the continuation list, and update the
2824 * next response index to slim. This process makes response
2825 * iocb's in the ring available to DMA as fast as possible but
2826 * pays a penalty for a copy operation. Since the iocb is
2827 * only 32 bytes, this penalty is considered small relative to
2828 * the PCI reads for register values and a slim write. When
2829 * the ulpLe field is set, the entire Command has been
2830 * received.
2831 */
James Smarted957682007-06-17 19:56:37 -05002832 entry = lpfc_resp_iocb(phba, pring);
2833
James Smart858c9f62007-06-17 19:56:39 -05002834 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002835 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002836 if (rspiocbp == NULL) {
2837 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002838 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002839 break;
2840 }
2841
James Smarted957682007-06-17 19:56:37 -05002842 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2843 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002844 irsp = &rspiocbp->iocb;
2845
2846 if (++pring->rspidx >= portRspMax)
2847 pring->rspidx = 0;
2848
James Smarta58cbd52007-08-02 11:09:43 -04002849 if (pring->ringno == LPFC_ELS_RING) {
2850 lpfc_debugfs_slow_ring_trc(phba,
2851 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2852 *(((uint32_t *) irsp) + 4),
2853 *(((uint32_t *) irsp) + 6),
2854 *(((uint32_t *) irsp) + 7));
2855 }
2856
James Smarted957682007-06-17 19:56:37 -05002857 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002858
James Smart3772a992009-05-22 14:50:54 -04002859 spin_unlock_irqrestore(&phba->hbalock, iflag);
2860 /* Handle the response IOCB */
2861 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2862 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002863
2864 /*
2865 * If the port response put pointer has not been updated, sync
2866 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2867 * response put pointer.
2868 */
2869 if (pring->rspidx == portRspPut) {
2870 portRspPut = le32_to_cpu(pgp->rspPutInx);
2871 }
2872 } /* while (pring->rspidx != portRspPut) */
2873
James Smart92d7f7b2007-06-17 19:56:38 -05002874 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002875 /* At least one response entry has been freed */
2876 pring->stats.iocb_rsp_full++;
2877 /* SET RxRE_RSP in Chip Att register */
2878 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2879 writel(status, phba->CAregaddr);
2880 readl(phba->CAregaddr); /* flush */
2881 }
2882 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2883 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2884 pring->stats.iocb_cmd_empty++;
2885
2886 /* Force update of the local copy of cmdGetInx */
2887 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2888 lpfc_sli_resume_iocb(phba, pring);
2889
2890 if ((pring->lpfc_sli_cmd_available))
2891 (pring->lpfc_sli_cmd_available) (phba, pring);
2892
2893 }
2894
James Smart2e0fef82007-06-17 19:56:36 -05002895 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002896 return;
dea31012005-04-17 16:05:31 -05002897}
2898
James Smarte59058c2008-08-24 21:49:00 -04002899/**
James Smart4f774512009-05-22 14:52:35 -04002900 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2901 * @phba: Pointer to HBA context object.
2902 * @pring: Pointer to driver SLI ring object.
2903 * @mask: Host attention register mask for this ring.
2904 *
2905 * This function is called from the worker thread when there is a pending
2906 * ELS response iocb on the driver internal slow-path response iocb worker
2907 * queue. The caller does not hold any lock. The function will remove each
2908 * response iocb from the response worker queue and calls the handle
2909 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2910 **/
2911static void
2912lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2913 struct lpfc_sli_ring *pring, uint32_t mask)
2914{
2915 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002916 struct hbq_dmabuf *dmabuf;
2917 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002918 unsigned long iflag;
2919
James Smart45ed1192009-10-02 15:17:02 -04002920 spin_lock_irqsave(&phba->hbalock, iflag);
2921 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2922 spin_unlock_irqrestore(&phba->hbalock, iflag);
2923 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002924 /* Get the response iocb from the head of work queue */
2925 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002926 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002927 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002928 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002929
2930 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2931 case CQE_CODE_COMPL_WQE:
2932 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2933 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04002934 /* Translate ELS WCQE to response IOCBQ */
2935 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2936 irspiocbq);
2937 if (irspiocbq)
2938 lpfc_sli_sp_handle_rspiocb(phba, pring,
2939 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04002940 break;
2941 case CQE_CODE_RECEIVE:
2942 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2943 cq_event);
2944 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2945 break;
2946 default:
2947 break;
2948 }
James Smart4f774512009-05-22 14:52:35 -04002949 }
2950}
2951
2952/**
James Smart3621a712009-04-06 18:47:14 -04002953 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002954 * @phba: Pointer to HBA context object.
2955 * @pring: Pointer to driver SLI ring object.
2956 *
2957 * This function aborts all iocbs in the given ring and frees all the iocb
2958 * objects in txq. This function issues an abort iocb for all the iocb commands
2959 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2960 * the return of this function. The caller is not required to hold any locks.
2961 **/
James Smart2e0fef82007-06-17 19:56:36 -05002962void
dea31012005-04-17 16:05:31 -05002963lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2964{
James Smart2534ba72007-04-25 09:52:20 -04002965 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002966 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002967
James Smart92d7f7b2007-06-17 19:56:38 -05002968 if (pring->ringno == LPFC_ELS_RING) {
2969 lpfc_fabric_abort_hba(phba);
2970 }
2971
dea31012005-04-17 16:05:31 -05002972 /* Error everything on txq and txcmplq
2973 * First do the txq.
2974 */
James Smart2e0fef82007-06-17 19:56:36 -05002975 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002976 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002977 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002978
2979 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002980 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2981 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2982
James Smart2e0fef82007-06-17 19:56:36 -05002983 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002984
James Smarta257bf92009-04-06 18:48:10 -04002985 /* Cancel all the IOCBs from the completions list */
2986 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2987 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05002988}
2989
James Smarte59058c2008-08-24 21:49:00 -04002990/**
James Smart3621a712009-04-06 18:47:14 -04002991 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04002992 * @phba: Pointer to HBA context object.
2993 *
2994 * This function flushes all iocbs in the fcp ring and frees all the iocb
2995 * objects in txq and txcmplq. This function will not issue abort iocbs
2996 * for all the iocb commands in txcmplq, they will just be returned with
2997 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2998 * slot has been permanently disabled.
2999 **/
3000void
3001lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3002{
3003 LIST_HEAD(txq);
3004 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003005 struct lpfc_sli *psli = &phba->sli;
3006 struct lpfc_sli_ring *pring;
3007
3008 /* Currently, only one fcp ring */
3009 pring = &psli->ring[psli->fcp_ring];
3010
3011 spin_lock_irq(&phba->hbalock);
3012 /* Retrieve everything on txq */
3013 list_splice_init(&pring->txq, &txq);
3014 pring->txq_cnt = 0;
3015
3016 /* Retrieve everything on the txcmplq */
3017 list_splice_init(&pring->txcmplq, &txcmplq);
3018 pring->txcmplq_cnt = 0;
3019 spin_unlock_irq(&phba->hbalock);
3020
3021 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003022 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3023 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003024
3025 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003026 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3027 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003028}
3029
3030/**
James Smart3772a992009-05-22 14:50:54 -04003031 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003032 * @phba: Pointer to HBA context object.
3033 * @mask: Bit mask to be checked.
3034 *
3035 * This function reads the host status register and compares
3036 * with the provided bit mask to check if HBA completed
3037 * the restart. This function will wait in a loop for the
3038 * HBA to complete restart. If the HBA does not restart within
3039 * 15 iterations, the function will reset the HBA again. The
3040 * function returns 1 when HBA fail to restart otherwise returns
3041 * zero.
3042 **/
James Smart3772a992009-05-22 14:50:54 -04003043static int
3044lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003045{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003046 uint32_t status;
3047 int i = 0;
3048 int retval = 0;
dea31012005-04-17 16:05:31 -05003049
Jamie Wellnitz41415862006-02-28 19:25:27 -05003050 /* Read the HBA Host Status Register */
3051 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003052
Jamie Wellnitz41415862006-02-28 19:25:27 -05003053 /*
3054 * Check status register every 100ms for 5 retries, then every
3055 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3056 * every 2.5 sec for 4.
3057 * Break our of the loop if errors occurred during init.
3058 */
3059 while (((status & mask) != mask) &&
3060 !(status & HS_FFERM) &&
3061 i++ < 20) {
dea31012005-04-17 16:05:31 -05003062
Jamie Wellnitz41415862006-02-28 19:25:27 -05003063 if (i <= 5)
3064 msleep(10);
3065 else if (i <= 10)
3066 msleep(500);
3067 else
3068 msleep(2500);
dea31012005-04-17 16:05:31 -05003069
Jamie Wellnitz41415862006-02-28 19:25:27 -05003070 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003071 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003072 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003073 lpfc_sli_brdrestart(phba);
3074 }
3075 /* Read the HBA Host Status Register */
3076 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003077 }
dea31012005-04-17 16:05:31 -05003078
Jamie Wellnitz41415862006-02-28 19:25:27 -05003079 /* Check to see if any errors occurred during init */
3080 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003081 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003082 retval = 1;
3083 }
dea31012005-04-17 16:05:31 -05003084
Jamie Wellnitz41415862006-02-28 19:25:27 -05003085 return retval;
dea31012005-04-17 16:05:31 -05003086}
3087
James Smartda0436e2009-05-22 14:51:39 -04003088/**
3089 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3090 * @phba: Pointer to HBA context object.
3091 * @mask: Bit mask to be checked.
3092 *
3093 * This function checks the host status register to check if HBA is
3094 * ready. This function will wait in a loop for the HBA to be ready
3095 * If the HBA is not ready , the function will will reset the HBA PCI
3096 * function again. The function returns 1 when HBA fail to be ready
3097 * otherwise returns zero.
3098 **/
3099static int
3100lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3101{
3102 uint32_t status;
3103 int retval = 0;
3104
3105 /* Read the HBA Host Status Register */
3106 status = lpfc_sli4_post_status_check(phba);
3107
3108 if (status) {
3109 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3110 lpfc_sli_brdrestart(phba);
3111 status = lpfc_sli4_post_status_check(phba);
3112 }
3113
3114 /* Check to see if any errors occurred during init */
3115 if (status) {
3116 phba->link_state = LPFC_HBA_ERROR;
3117 retval = 1;
3118 } else
3119 phba->sli4_hba.intr_enable = 0;
3120
3121 return retval;
3122}
3123
3124/**
3125 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3126 * @phba: Pointer to HBA context object.
3127 * @mask: Bit mask to be checked.
3128 *
3129 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3130 * from the API jump table function pointer from the lpfc_hba struct.
3131 **/
3132int
3133lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3134{
3135 return phba->lpfc_sli_brdready(phba, mask);
3136}
3137
James Smart92908312006-03-07 15:04:13 -05003138#define BARRIER_TEST_PATTERN (0xdeadbeef)
3139
James Smarte59058c2008-08-24 21:49:00 -04003140/**
James Smart3621a712009-04-06 18:47:14 -04003141 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003142 * @phba: Pointer to HBA context object.
3143 *
3144 * This function is called before resetting an HBA. This
3145 * function requests HBA to quiesce DMAs before a reset.
3146 **/
James Smart2e0fef82007-06-17 19:56:36 -05003147void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003148{
James Smart65a29c12006-07-06 15:50:50 -04003149 uint32_t __iomem *resp_buf;
3150 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003151 volatile uint32_t mbox;
3152 uint32_t hc_copy;
3153 int i;
3154 uint8_t hdrtype;
3155
3156 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3157 if (hdrtype != 0x80 ||
3158 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3159 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3160 return;
3161
3162 /*
3163 * Tell the other part of the chip to suspend temporarily all
3164 * its DMA activity.
3165 */
James Smart65a29c12006-07-06 15:50:50 -04003166 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003167
3168 /* Disable the error attention */
3169 hc_copy = readl(phba->HCregaddr);
3170 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3171 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003172 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003173
3174 if (readl(phba->HAregaddr) & HA_ERATT) {
3175 /* Clear Chip error bit */
3176 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003177 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003178 }
3179
3180 mbox = 0;
3181 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3182 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3183
3184 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003185 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003186 writel(mbox, mbox_buf);
3187
3188 for (i = 0;
3189 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3190 mdelay(1);
3191
3192 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003193 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003194 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003195 goto restore_hc;
3196 else
3197 goto clear_errat;
3198 }
3199
3200 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3201 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3202 mdelay(1);
3203
3204clear_errat:
3205
3206 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3207 mdelay(1);
3208
3209 if (readl(phba->HAregaddr) & HA_ERATT) {
3210 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003211 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003212 }
3213
3214restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003215 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003216 writel(hc_copy, phba->HCregaddr);
3217 readl(phba->HCregaddr); /* flush */
3218}
3219
James Smarte59058c2008-08-24 21:49:00 -04003220/**
James Smart3621a712009-04-06 18:47:14 -04003221 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003222 * @phba: Pointer to HBA context object.
3223 *
3224 * This function issues a kill_board mailbox command and waits for
3225 * the error attention interrupt. This function is called for stopping
3226 * the firmware processing. The caller is not required to hold any
3227 * locks. This function calls lpfc_hba_down_post function to free
3228 * any pending commands after the kill. The function will return 1 when it
3229 * fails to kill the board else will return 0.
3230 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003231int
James Smart2e0fef82007-06-17 19:56:36 -05003232lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003233{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003234 struct lpfc_sli *psli;
3235 LPFC_MBOXQ_t *pmb;
3236 uint32_t status;
3237 uint32_t ha_copy;
3238 int retval;
3239 int i = 0;
3240
3241 psli = &phba->sli;
3242
3243 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003245 "0329 Kill HBA Data: x%x x%x\n",
3246 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003247
James Smart98c9ea52007-10-27 13:37:33 -04003248 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3249 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003250 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003251
3252 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003253 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003254 status = readl(phba->HCregaddr);
3255 status &= ~HC_ERINT_ENA;
3256 writel(status, phba->HCregaddr);
3257 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003258 phba->link_flag |= LS_IGNORE_ERATT;
3259 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003260
3261 lpfc_kill_board(phba, pmb);
3262 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3263 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3264
3265 if (retval != MBX_SUCCESS) {
3266 if (retval != MBX_BUSY)
3267 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003268 spin_lock_irq(&phba->hbalock);
3269 phba->link_flag &= ~LS_IGNORE_ERATT;
3270 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003271 return 1;
3272 }
3273
James Smartf4b4c682009-05-22 14:53:12 -04003274 spin_lock_irq(&phba->hbalock);
3275 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3276 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003277
Jamie Wellnitz41415862006-02-28 19:25:27 -05003278 mempool_free(pmb, phba->mbox_mem_pool);
3279
3280 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3281 * attention every 100ms for 3 seconds. If we don't get ERATT after
3282 * 3 seconds we still set HBA_ERROR state because the status of the
3283 * board is now undefined.
3284 */
3285 ha_copy = readl(phba->HAregaddr);
3286
3287 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3288 mdelay(100);
3289 ha_copy = readl(phba->HAregaddr);
3290 }
3291
3292 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003293 if (ha_copy & HA_ERATT) {
3294 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003295 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003296 }
James Smart2e0fef82007-06-17 19:56:36 -05003297 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003298 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003299 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003300 phba->link_flag &= ~LS_IGNORE_ERATT;
3301 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003302
Jamie Wellnitz41415862006-02-28 19:25:27 -05003303 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003304 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003305
James Smart2e0fef82007-06-17 19:56:36 -05003306 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003307}
3308
James Smarte59058c2008-08-24 21:49:00 -04003309/**
James Smart3772a992009-05-22 14:50:54 -04003310 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003311 * @phba: Pointer to HBA context object.
3312 *
3313 * This function resets the HBA by writing HC_INITFF to the control
3314 * register. After the HBA resets, this function resets all the iocb ring
3315 * indices. This function disables PCI layer parity checking during
3316 * the reset.
3317 * This function returns 0 always.
3318 * The caller is not required to hold any locks.
3319 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003320int
James Smart2e0fef82007-06-17 19:56:36 -05003321lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003322{
3323 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003324 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003325 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003326 int i;
dea31012005-04-17 16:05:31 -05003327
Jamie Wellnitz41415862006-02-28 19:25:27 -05003328 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003329
Jamie Wellnitz41415862006-02-28 19:25:27 -05003330 /* Reset HBA */
3331 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003332 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003333 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003334
3335 /* perform board reset */
3336 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003337 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003338 phba->pport->fc_myDID = 0;
3339 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003340
Jamie Wellnitz41415862006-02-28 19:25:27 -05003341 /* Turn off parity checking and serr during the physical reset */
3342 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3343 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3344 (cfg_value &
3345 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3346
James Smart3772a992009-05-22 14:50:54 -04003347 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3348
Jamie Wellnitz41415862006-02-28 19:25:27 -05003349 /* Now toggle INITFF bit in the Host Control Register */
3350 writel(HC_INITFF, phba->HCregaddr);
3351 mdelay(1);
3352 readl(phba->HCregaddr); /* flush */
3353 writel(0, phba->HCregaddr);
3354 readl(phba->HCregaddr); /* flush */
3355
3356 /* Restore PCI cmd register */
3357 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003358
3359 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003360 for (i = 0; i < psli->num_rings; i++) {
3361 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003362 pring->flag = 0;
3363 pring->rspidx = 0;
3364 pring->next_cmdidx = 0;
3365 pring->local_getidx = 0;
3366 pring->cmdidx = 0;
3367 pring->missbufcnt = 0;
3368 }
dea31012005-04-17 16:05:31 -05003369
James Smart2e0fef82007-06-17 19:56:36 -05003370 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003371 return 0;
3372}
3373
James Smarte59058c2008-08-24 21:49:00 -04003374/**
James Smartda0436e2009-05-22 14:51:39 -04003375 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3376 * @phba: Pointer to HBA context object.
3377 *
3378 * This function resets a SLI4 HBA. This function disables PCI layer parity
3379 * checking during resets the device. The caller is not required to hold
3380 * any locks.
3381 *
3382 * This function returns 0 always.
3383 **/
3384int
3385lpfc_sli4_brdreset(struct lpfc_hba *phba)
3386{
3387 struct lpfc_sli *psli = &phba->sli;
3388 uint16_t cfg_value;
3389 uint8_t qindx;
3390
3391 /* Reset HBA */
3392 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3393 "0295 Reset HBA Data: x%x x%x\n",
3394 phba->pport->port_state, psli->sli_flag);
3395
3396 /* perform board reset */
3397 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003398 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003399 phba->pport->fc_myDID = 0;
3400 phba->pport->fc_prevDID = 0;
3401
3402 /* Turn off parity checking and serr during the physical reset */
3403 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3404 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3405 (cfg_value &
3406 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3407
3408 spin_lock_irq(&phba->hbalock);
3409 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3410 phba->fcf.fcf_flag = 0;
3411 /* Clean up the child queue list for the CQs */
3412 list_del_init(&phba->sli4_hba.mbx_wq->list);
3413 list_del_init(&phba->sli4_hba.els_wq->list);
3414 list_del_init(&phba->sli4_hba.hdr_rq->list);
3415 list_del_init(&phba->sli4_hba.dat_rq->list);
3416 list_del_init(&phba->sli4_hba.mbx_cq->list);
3417 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003418 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3419 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3420 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3421 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3422 spin_unlock_irq(&phba->hbalock);
3423
3424 /* Now physically reset the device */
3425 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3426 "0389 Performing PCI function reset!\n");
3427 /* Perform FCoE PCI function reset */
3428 lpfc_pci_function_reset(phba);
3429
3430 return 0;
3431}
3432
3433/**
3434 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003435 * @phba: Pointer to HBA context object.
3436 *
3437 * This function is called in the SLI initialization code path to
3438 * restart the HBA. The caller is not required to hold any lock.
3439 * This function writes MBX_RESTART mailbox command to the SLIM and
3440 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3441 * function to free any pending commands. The function enables
3442 * POST only during the first initialization. The function returns zero.
3443 * The function does not guarantee completion of MBX_RESTART mailbox
3444 * command before the return of this function.
3445 **/
James Smartda0436e2009-05-22 14:51:39 -04003446static int
3447lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003448{
3449 MAILBOX_t *mb;
3450 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003451 volatile uint32_t word0;
3452 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003453 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003454
James Smart2e0fef82007-06-17 19:56:36 -05003455 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003456
James Smart0d878412009-10-02 15:16:56 -04003457 /* Take PCIe device Advanced Error Reporting (AER) state */
3458 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3459
Jamie Wellnitz41415862006-02-28 19:25:27 -05003460 psli = &phba->sli;
3461
3462 /* Restart HBA */
3463 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003464 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003465 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003466
3467 word0 = 0;
3468 mb = (MAILBOX_t *) &word0;
3469 mb->mbxCommand = MBX_RESTART;
3470 mb->mbxHc = 1;
3471
James Smart92908312006-03-07 15:04:13 -05003472 lpfc_reset_barrier(phba);
3473
Jamie Wellnitz41415862006-02-28 19:25:27 -05003474 to_slim = phba->MBslimaddr;
3475 writel(*(uint32_t *) mb, to_slim);
3476 readl(to_slim); /* flush */
3477
3478 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003479 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003480 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003481 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003482 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003483 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003484 writel(*(uint32_t *) mb, to_slim);
3485 readl(to_slim); /* flush */
3486
3487 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003488 phba->pport->stopped = 0;
3489 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003490 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003491 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003492
James Smart64ba8812006-08-02 15:24:34 -04003493 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3494 psli->stats_start = get_seconds();
3495
James Smarteaf15d52008-12-04 22:39:29 -05003496 /* Give the INITFF and Post time to settle. */
3497 mdelay(100);
dea31012005-04-17 16:05:31 -05003498
James Smart0d878412009-10-02 15:16:56 -04003499 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3500 if (hba_aer_enabled)
3501 pci_disable_pcie_error_reporting(phba->pcidev);
3502
Jamie Wellnitz41415862006-02-28 19:25:27 -05003503 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003504
3505 return 0;
3506}
3507
James Smarte59058c2008-08-24 21:49:00 -04003508/**
James Smartda0436e2009-05-22 14:51:39 -04003509 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3510 * @phba: Pointer to HBA context object.
3511 *
3512 * This function is called in the SLI initialization code path to restart
3513 * a SLI4 HBA. The caller is not required to hold any lock.
3514 * At the end of the function, it calls lpfc_hba_down_post function to
3515 * free any pending commands.
3516 **/
3517static int
3518lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3519{
3520 struct lpfc_sli *psli = &phba->sli;
3521
3522
3523 /* Restart HBA */
3524 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3525 "0296 Restart HBA Data: x%x x%x\n",
3526 phba->pport->port_state, psli->sli_flag);
3527
3528 lpfc_sli4_brdreset(phba);
3529
3530 spin_lock_irq(&phba->hbalock);
3531 phba->pport->stopped = 0;
3532 phba->link_state = LPFC_INIT_START;
3533 phba->hba_flag = 0;
3534 spin_unlock_irq(&phba->hbalock);
3535
3536 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3537 psli->stats_start = get_seconds();
3538
3539 lpfc_hba_down_post(phba);
3540
3541 return 0;
3542}
3543
3544/**
3545 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3546 * @phba: Pointer to HBA context object.
3547 *
3548 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3549 * API jump table function pointer from the lpfc_hba struct.
3550**/
3551int
3552lpfc_sli_brdrestart(struct lpfc_hba *phba)
3553{
3554 return phba->lpfc_sli_brdrestart(phba);
3555}
3556
3557/**
James Smart3621a712009-04-06 18:47:14 -04003558 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003559 * @phba: Pointer to HBA context object.
3560 *
3561 * This function is called after a HBA restart to wait for successful
3562 * restart of the HBA. Successful restart of the HBA is indicated by
3563 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3564 * iteration, the function will restart the HBA again. The function returns
3565 * zero if HBA successfully restarted else returns negative error code.
3566 **/
dea31012005-04-17 16:05:31 -05003567static int
3568lpfc_sli_chipset_init(struct lpfc_hba *phba)
3569{
3570 uint32_t status, i = 0;
3571
3572 /* Read the HBA Host Status Register */
3573 status = readl(phba->HSregaddr);
3574
3575 /* Check status register to see what current state is */
3576 i = 0;
3577 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3578
3579 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3580 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3581 * 4.
3582 */
3583 if (i++ >= 20) {
3584 /* Adapter failed to init, timeout, status reg
3585 <status> */
James Smarted957682007-06-17 19:56:37 -05003586 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003587 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003588 "timeout, status reg x%x, "
3589 "FW Data: A8 x%x AC x%x\n", status,
3590 readl(phba->MBslimaddr + 0xa8),
3591 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003592 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003593 return -ETIMEDOUT;
3594 }
3595
3596 /* Check to see if any errors occurred during init */
3597 if (status & HS_FFERM) {
3598 /* ERROR: During chipset initialization */
3599 /* Adapter failed to init, chipset, status reg
3600 <status> */
James Smarted957682007-06-17 19:56:37 -05003601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003602 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003603 "chipset, status reg x%x, "
3604 "FW Data: A8 x%x AC x%x\n", status,
3605 readl(phba->MBslimaddr + 0xa8),
3606 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003607 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003608 return -EIO;
3609 }
3610
3611 if (i <= 5) {
3612 msleep(10);
3613 } else if (i <= 10) {
3614 msleep(500);
3615 } else {
3616 msleep(2500);
3617 }
3618
3619 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003620 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003621 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003622 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003623 }
3624 /* Read the HBA Host Status Register */
3625 status = readl(phba->HSregaddr);
3626 }
3627
3628 /* Check to see if any errors occurred during init */
3629 if (status & HS_FFERM) {
3630 /* ERROR: During chipset initialization */
3631 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003633 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003634 "status reg x%x, "
3635 "FW Data: A8 x%x AC x%x\n", status,
3636 readl(phba->MBslimaddr + 0xa8),
3637 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003638 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003639 return -EIO;
3640 }
3641
3642 /* Clear all interrupt enable conditions */
3643 writel(0, phba->HCregaddr);
3644 readl(phba->HCregaddr); /* flush */
3645
3646 /* setup host attn register */
3647 writel(0xffffffff, phba->HAregaddr);
3648 readl(phba->HAregaddr); /* flush */
3649 return 0;
3650}
3651
James Smarte59058c2008-08-24 21:49:00 -04003652/**
James Smart3621a712009-04-06 18:47:14 -04003653 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003654 *
3655 * This function calculates and returns the number of HBQs required to be
3656 * configured.
3657 **/
James Smart78b2d852007-08-02 11:10:21 -04003658int
James Smarted957682007-06-17 19:56:37 -05003659lpfc_sli_hbq_count(void)
3660{
James Smart92d7f7b2007-06-17 19:56:38 -05003661 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003662}
3663
James Smarte59058c2008-08-24 21:49:00 -04003664/**
James Smart3621a712009-04-06 18:47:14 -04003665 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003666 *
3667 * This function adds the number of hbq entries in every HBQ to get
3668 * the total number of hbq entries required for the HBA and returns
3669 * the total count.
3670 **/
James Smarted957682007-06-17 19:56:37 -05003671static int
3672lpfc_sli_hbq_entry_count(void)
3673{
3674 int hbq_count = lpfc_sli_hbq_count();
3675 int count = 0;
3676 int i;
3677
3678 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003679 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003680 return count;
3681}
3682
James Smarte59058c2008-08-24 21:49:00 -04003683/**
James Smart3621a712009-04-06 18:47:14 -04003684 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003685 *
3686 * This function calculates amount of memory required for all hbq entries
3687 * to be configured and returns the total memory required.
3688 **/
dea31012005-04-17 16:05:31 -05003689int
James Smarted957682007-06-17 19:56:37 -05003690lpfc_sli_hbq_size(void)
3691{
3692 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3693}
3694
James Smarte59058c2008-08-24 21:49:00 -04003695/**
James Smart3621a712009-04-06 18:47:14 -04003696 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003697 * @phba: Pointer to HBA context object.
3698 *
3699 * This function is called during the SLI initialization to configure
3700 * all the HBQs and post buffers to the HBQ. The caller is not
3701 * required to hold any locks. This function will return zero if successful
3702 * else it will return negative error code.
3703 **/
James Smarted957682007-06-17 19:56:37 -05003704static int
3705lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3706{
3707 int hbq_count = lpfc_sli_hbq_count();
3708 LPFC_MBOXQ_t *pmb;
3709 MAILBOX_t *pmbox;
3710 uint32_t hbqno;
3711 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003712
James Smart92d7f7b2007-06-17 19:56:38 -05003713 /* Get a Mailbox buffer to setup mailbox
3714 * commands for HBA initialization
3715 */
James Smarted957682007-06-17 19:56:37 -05003716 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3717
3718 if (!pmb)
3719 return -ENOMEM;
3720
James Smart04c68492009-05-22 14:52:52 -04003721 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003722
3723 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3724 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003725 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003726
3727 hbq_entry_index = 0;
3728 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3729 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3730 phba->hbqs[hbqno].hbqPutIdx = 0;
3731 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3732 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003733 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003734 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3735 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003736 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3737
3738 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3739 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3740 mbxStatus <status>, ring <num> */
3741
3742 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003743 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003744 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003745 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003746 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003747 pmbox->mbxStatus, hbqno);
3748
3749 phba->link_state = LPFC_HBA_ERROR;
3750 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003751 return ENXIO;
3752 }
3753 }
3754 phba->hbq_count = hbq_count;
3755
James Smarted957682007-06-17 19:56:37 -05003756 mempool_free(pmb, phba->mbox_mem_pool);
3757
James Smart92d7f7b2007-06-17 19:56:38 -05003758 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003759 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3760 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003761 return 0;
3762}
3763
James Smarte59058c2008-08-24 21:49:00 -04003764/**
James Smart4f774512009-05-22 14:52:35 -04003765 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3766 * @phba: Pointer to HBA context object.
3767 *
3768 * This function is called during the SLI initialization to configure
3769 * all the HBQs and post buffers to the HBQ. The caller is not
3770 * required to hold any locks. This function will return zero if successful
3771 * else it will return negative error code.
3772 **/
3773static int
3774lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3775{
3776 phba->hbq_in_use = 1;
3777 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3778 phba->hbq_count = 1;
3779 /* Initially populate or replenish the HBQs */
3780 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3781 return 0;
3782}
3783
3784/**
James Smart3621a712009-04-06 18:47:14 -04003785 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003786 * @phba: Pointer to HBA context object.
3787 * @sli_mode: sli mode - 2/3
3788 *
3789 * This function is called by the sli intialization code path
3790 * to issue config_port mailbox command. This function restarts the
3791 * HBA firmware and issues a config_port mailbox command to configure
3792 * the SLI interface in the sli mode specified by sli_mode
3793 * variable. The caller is not required to hold any locks.
3794 * The function returns 0 if successful, else returns negative error
3795 * code.
3796 **/
James Smart93996272008-08-24 21:50:30 -04003797int
3798lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003799{
3800 LPFC_MBOXQ_t *pmb;
3801 uint32_t resetcount = 0, rc = 0, done = 0;
3802
3803 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3804 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003805 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003806 return -ENOMEM;
3807 }
3808
James Smarted957682007-06-17 19:56:37 -05003809 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003810 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003811 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003812 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003813 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003814 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003815 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003816 rc = lpfc_sli_chipset_init(phba);
3817 if (rc)
3818 break;
3819
James Smart2e0fef82007-06-17 19:56:36 -05003820 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003821 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003822 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003823 resetcount++;
3824
James Smarted957682007-06-17 19:56:37 -05003825 /* Call pre CONFIG_PORT mailbox command initialization. A
3826 * value of 0 means the call was successful. Any other
3827 * nonzero value is a failure, but if ERESTART is returned,
3828 * the driver may reset the HBA and try again.
3829 */
dea31012005-04-17 16:05:31 -05003830 rc = lpfc_config_port_prep(phba);
3831 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003832 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003833 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003834 } else if (rc)
dea31012005-04-17 16:05:31 -05003835 break;
James Smart2e0fef82007-06-17 19:56:36 -05003836 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003837 lpfc_config_port(phba, pmb);
3838 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003839 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3840 LPFC_SLI3_HBQ_ENABLED |
3841 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003842 LPFC_SLI3_INB_ENABLED |
3843 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003844 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003845 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003846 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003847 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003848 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003849 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003850 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003851 spin_unlock_irq(&phba->hbalock);
3852 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003853 } else {
3854 /* Allow asynchronous mailbox command to go through */
3855 spin_lock_irq(&phba->hbalock);
3856 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3857 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003858 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003859 }
dea31012005-04-17 16:05:31 -05003860 }
James Smarted957682007-06-17 19:56:37 -05003861 if (!done) {
3862 rc = -EINVAL;
3863 goto do_prep_failed;
3864 }
James Smart04c68492009-05-22 14:52:52 -04003865 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3866 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003867 rc = -ENXIO;
3868 goto do_prep_failed;
3869 }
James Smart04c68492009-05-22 14:52:52 -04003870 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003871 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003872 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3873 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3874 phba->max_vpi : phba->max_vports;
3875
James Smart34b02dc2008-08-24 21:49:55 -04003876 } else
3877 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003878 if (pmb->u.mb.un.varCfgPort.gdss)
3879 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3880 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003881 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003882 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003883 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003884 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003885 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003886 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003887 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3888 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3889 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3890 phba->inb_last_counter =
3891 phba->mbox->us.s3_inb_pgp.counter;
3892 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003893 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003894 phba->port_gp = phba->mbox->us.s3_pgp.port;
3895 phba->inb_ha_copy = NULL;
3896 phba->inb_counter = NULL;
3897 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003898
3899 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003900 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003901 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3902 else
3903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3904 "0443 Adapter did not grant "
3905 "BlockGuard\n");
3906 }
James Smart34b02dc2008-08-24 21:49:55 -04003907 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003908 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003909 phba->port_gp = phba->mbox->us.s2.port;
3910 phba->inb_ha_copy = NULL;
3911 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003912 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003913 }
James Smart92d7f7b2007-06-17 19:56:38 -05003914do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003915 mempool_free(pmb, phba->mbox_mem_pool);
3916 return rc;
3917}
3918
James Smarte59058c2008-08-24 21:49:00 -04003919
3920/**
James Smart3621a712009-04-06 18:47:14 -04003921 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003922 * @phba: Pointer to HBA context object.
3923 *
3924 * This function is the main SLI intialization function. This function
3925 * is called by the HBA intialization code, HBA reset code and HBA
3926 * error attention handler code. Caller is not required to hold any
3927 * locks. This function issues config_port mailbox command to configure
3928 * the SLI, setup iocb rings and HBQ rings. In the end the function
3929 * calls the config_port_post function to issue init_link mailbox
3930 * command and to start the discovery. The function will return zero
3931 * if successful, else it will return negative error code.
3932 **/
James Smarted957682007-06-17 19:56:37 -05003933int
3934lpfc_sli_hba_setup(struct lpfc_hba *phba)
3935{
3936 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003937 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003938
3939 switch (lpfc_sli_mode) {
3940 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003941 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003943 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003944 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003945 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003946 break;
3947 }
James Smarted957682007-06-17 19:56:37 -05003948 mode = 2;
3949 break;
3950 case 0:
3951 case 3:
3952 break;
3953 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003955 "1819 Unrecognized lpfc_sli_mode "
3956 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003957
3958 break;
3959 }
3960
James Smart93996272008-08-24 21:50:30 -04003961 rc = lpfc_sli_config_port(phba, mode);
3962
James Smarted957682007-06-17 19:56:37 -05003963 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003965 "1820 Unable to select SLI-3. "
3966 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003967 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003968 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003969 if (rc)
dea31012005-04-17 16:05:31 -05003970 goto lpfc_sli_hba_setup_error;
3971
James Smart0d878412009-10-02 15:16:56 -04003972 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
3973 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
3974 rc = pci_enable_pcie_error_reporting(phba->pcidev);
3975 if (!rc) {
3976 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3977 "2709 This device supports "
3978 "Advanced Error Reporting (AER)\n");
3979 spin_lock_irq(&phba->hbalock);
3980 phba->hba_flag |= HBA_AER_ENABLED;
3981 spin_unlock_irq(&phba->hbalock);
3982 } else {
3983 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3984 "2708 This device does not support "
3985 "Advanced Error Reporting (AER)\n");
3986 phba->cfg_aer_support = 0;
3987 }
3988 }
3989
James Smarted957682007-06-17 19:56:37 -05003990 if (phba->sli_rev == 3) {
3991 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3992 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003993 } else {
3994 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3995 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003996 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003997 }
3998
3999 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004000 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4001 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004002 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004003
4004 if (rc)
4005 goto lpfc_sli_hba_setup_error;
4006
James Smart93996272008-08-24 21:50:30 -04004007 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004008 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4009 rc = lpfc_sli_hbq_setup(phba);
4010 if (rc)
4011 goto lpfc_sli_hba_setup_error;
4012 }
James Smart04c68492009-05-22 14:52:52 -04004013 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004014 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004015 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004016
4017 rc = lpfc_config_port_post(phba);
4018 if (rc)
4019 goto lpfc_sli_hba_setup_error;
4020
James Smarted957682007-06-17 19:56:37 -05004021 return rc;
4022
James Smart92d7f7b2007-06-17 19:56:38 -05004023lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004024 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05004025 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004026 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004027 return rc;
4028}
4029
James Smartda0436e2009-05-22 14:51:39 -04004030/**
4031 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4032 * @phba: Pointer to HBA context object.
4033 * @mboxq: mailbox pointer.
4034 * This function issue a dump mailbox command to read config region
4035 * 23 and parse the records in the region and populate driver
4036 * data structure.
4037 **/
4038static int
4039lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4040 LPFC_MBOXQ_t *mboxq)
4041{
4042 struct lpfc_dmabuf *mp;
4043 struct lpfc_mqe *mqe;
4044 uint32_t data_length;
4045 int rc;
4046
4047 /* Program the default value of vlan_id and fc_map */
4048 phba->valid_vlan = 0;
4049 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4050 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4051 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4052
4053 mqe = &mboxq->u.mqe;
4054 if (lpfc_dump_fcoe_param(phba, mboxq))
4055 return -ENOMEM;
4056
4057 mp = (struct lpfc_dmabuf *) mboxq->context1;
4058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4059
4060 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4061 "(%d):2571 Mailbox cmd x%x Status x%x "
4062 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4063 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4064 "CQ: x%x x%x x%x x%x\n",
4065 mboxq->vport ? mboxq->vport->vpi : 0,
4066 bf_get(lpfc_mqe_command, mqe),
4067 bf_get(lpfc_mqe_status, mqe),
4068 mqe->un.mb_words[0], mqe->un.mb_words[1],
4069 mqe->un.mb_words[2], mqe->un.mb_words[3],
4070 mqe->un.mb_words[4], mqe->un.mb_words[5],
4071 mqe->un.mb_words[6], mqe->un.mb_words[7],
4072 mqe->un.mb_words[8], mqe->un.mb_words[9],
4073 mqe->un.mb_words[10], mqe->un.mb_words[11],
4074 mqe->un.mb_words[12], mqe->un.mb_words[13],
4075 mqe->un.mb_words[14], mqe->un.mb_words[15],
4076 mqe->un.mb_words[16], mqe->un.mb_words[50],
4077 mboxq->mcqe.word0,
4078 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4079 mboxq->mcqe.trailer);
4080
4081 if (rc) {
4082 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4083 kfree(mp);
4084 return -EIO;
4085 }
4086 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004087 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004088 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4089 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004090 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004091 }
James Smartda0436e2009-05-22 14:51:39 -04004092
4093 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4094 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4095 kfree(mp);
4096 return 0;
4097}
4098
4099/**
4100 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4101 * @phba: pointer to lpfc hba data structure.
4102 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4103 * @vpd: pointer to the memory to hold resulting port vpd data.
4104 * @vpd_size: On input, the number of bytes allocated to @vpd.
4105 * On output, the number of data bytes in @vpd.
4106 *
4107 * This routine executes a READ_REV SLI4 mailbox command. In
4108 * addition, this routine gets the port vpd data.
4109 *
4110 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004111 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004112 * ENOMEM - could not allocated memory.
4113 **/
4114static int
4115lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4116 uint8_t *vpd, uint32_t *vpd_size)
4117{
4118 int rc = 0;
4119 uint32_t dma_size;
4120 struct lpfc_dmabuf *dmabuf;
4121 struct lpfc_mqe *mqe;
4122
4123 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4124 if (!dmabuf)
4125 return -ENOMEM;
4126
4127 /*
4128 * Get a DMA buffer for the vpd data resulting from the READ_REV
4129 * mailbox command.
4130 */
4131 dma_size = *vpd_size;
4132 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4133 dma_size,
4134 &dmabuf->phys,
4135 GFP_KERNEL);
4136 if (!dmabuf->virt) {
4137 kfree(dmabuf);
4138 return -ENOMEM;
4139 }
4140 memset(dmabuf->virt, 0, dma_size);
4141
4142 /*
4143 * The SLI4 implementation of READ_REV conflicts at word1,
4144 * bits 31:16 and SLI4 adds vpd functionality not present
4145 * in SLI3. This code corrects the conflicts.
4146 */
4147 lpfc_read_rev(phba, mboxq);
4148 mqe = &mboxq->u.mqe;
4149 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4150 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4151 mqe->un.read_rev.word1 &= 0x0000FFFF;
4152 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4153 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4154
4155 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4156 if (rc) {
4157 dma_free_coherent(&phba->pcidev->dev, dma_size,
4158 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004159 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004160 return -EIO;
4161 }
4162
James Smartda0436e2009-05-22 14:51:39 -04004163 /*
4164 * The available vpd length cannot be bigger than the
4165 * DMA buffer passed to the port. Catch the less than
4166 * case and update the caller's size.
4167 */
4168 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4169 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4170
4171 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4172 dma_free_coherent(&phba->pcidev->dev, dma_size,
4173 dmabuf->virt, dmabuf->phys);
4174 kfree(dmabuf);
4175 return 0;
4176}
4177
4178/**
4179 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4180 * @phba: pointer to lpfc hba data structure.
4181 *
4182 * This routine is called to explicitly arm the SLI4 device's completion and
4183 * event queues
4184 **/
4185static void
4186lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4187{
4188 uint8_t fcp_eqidx;
4189
4190 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4191 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004192 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4193 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4194 LPFC_QUEUE_REARM);
4195 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4196 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4197 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4198 LPFC_QUEUE_REARM);
4199}
4200
4201/**
4202 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4203 * @phba: Pointer to HBA context object.
4204 *
4205 * This function is the main SLI4 device intialization PCI function. This
4206 * function is called by the HBA intialization code, HBA reset code and
4207 * HBA error attention handler code. Caller is not required to hold any
4208 * locks.
4209 **/
4210int
4211lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4212{
4213 int rc;
4214 LPFC_MBOXQ_t *mboxq;
4215 struct lpfc_mqe *mqe;
4216 uint8_t *vpd;
4217 uint32_t vpd_size;
4218 uint32_t ftr_rsp = 0;
4219 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4220 struct lpfc_vport *vport = phba->pport;
4221 struct lpfc_dmabuf *mp;
4222
4223 /* Perform a PCI function reset to start from clean */
4224 rc = lpfc_pci_function_reset(phba);
4225 if (unlikely(rc))
4226 return -ENODEV;
4227
4228 /* Check the HBA Host Status Register for readyness */
4229 rc = lpfc_sli4_post_status_check(phba);
4230 if (unlikely(rc))
4231 return -ENODEV;
4232 else {
4233 spin_lock_irq(&phba->hbalock);
4234 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4235 spin_unlock_irq(&phba->hbalock);
4236 }
4237
4238 /*
4239 * Allocate a single mailbox container for initializing the
4240 * port.
4241 */
4242 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4243 if (!mboxq)
4244 return -ENOMEM;
4245
4246 /*
4247 * Continue initialization with default values even if driver failed
4248 * to read FCoE param config regions
4249 */
4250 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4251 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004252 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004253
4254 /* Issue READ_REV to collect vpd and FW information. */
4255 vpd_size = PAGE_SIZE;
4256 vpd = kzalloc(vpd_size, GFP_KERNEL);
4257 if (!vpd) {
4258 rc = -ENOMEM;
4259 goto out_free_mbox;
4260 }
4261
4262 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4263 if (unlikely(rc))
4264 goto out_free_vpd;
4265
4266 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004267 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4268 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4269 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004270
4271 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4272 LPFC_DCBX_CEE_MODE)
4273 phba->hba_flag |= HBA_FIP_SUPPORT;
4274 else
4275 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4276
James Smartf1126682009-06-10 17:22:44 -04004277 if (phba->sli_rev != LPFC_SLI_REV4 ||
4278 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004279 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4280 "0376 READ_REV Error. SLI Level %d "
4281 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004282 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004283 rc = -EIO;
4284 goto out_free_vpd;
4285 }
James Smartda0436e2009-05-22 14:51:39 -04004286 /*
4287 * Evaluate the read rev and vpd data. Populate the driver
4288 * state with the results. If this routine fails, the failure
4289 * is not fatal as the driver will use generic values.
4290 */
4291 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4292 if (unlikely(!rc)) {
4293 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4294 "0377 Error %d parsing vpd. "
4295 "Using defaults.\n", rc);
4296 rc = 0;
4297 }
4298
James Smartf1126682009-06-10 17:22:44 -04004299 /* Save information as VPD data */
4300 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4301 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4302 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4303 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4304 &mqe->un.read_rev);
4305 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4306 &mqe->un.read_rev);
4307 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4308 &mqe->un.read_rev);
4309 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4310 &mqe->un.read_rev);
4311 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4312 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4313 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4314 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4315 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4316 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4317 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4318 "(%d):0380 READ_REV Status x%x "
4319 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4320 mboxq->vport ? mboxq->vport->vpi : 0,
4321 bf_get(lpfc_mqe_status, mqe),
4322 phba->vpd.rev.opFwName,
4323 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4324 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004325
4326 /*
4327 * Discover the port's supported feature set and match it against the
4328 * hosts requests.
4329 */
4330 lpfc_request_features(phba, mboxq);
4331 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4332 if (unlikely(rc)) {
4333 rc = -EIO;
4334 goto out_free_vpd;
4335 }
4336
4337 /*
4338 * The port must support FCP initiator mode as this is the
4339 * only mode running in the host.
4340 */
4341 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4342 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4343 "0378 No support for fcpi mode.\n");
4344 ftr_rsp++;
4345 }
4346
4347 /*
4348 * If the port cannot support the host's requested features
4349 * then turn off the global config parameters to disable the
4350 * feature in the driver. This is not a fatal error.
4351 */
4352 if ((phba->cfg_enable_bg) &&
4353 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4354 ftr_rsp++;
4355
4356 if (phba->max_vpi && phba->cfg_enable_npiv &&
4357 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4358 ftr_rsp++;
4359
4360 if (ftr_rsp) {
4361 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4362 "0379 Feature Mismatch Data: x%08x %08x "
4363 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4364 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4365 phba->cfg_enable_npiv, phba->max_vpi);
4366 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4367 phba->cfg_enable_bg = 0;
4368 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4369 phba->cfg_enable_npiv = 0;
4370 }
4371
4372 /* These SLI3 features are assumed in SLI4 */
4373 spin_lock_irq(&phba->hbalock);
4374 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4375 spin_unlock_irq(&phba->hbalock);
4376
4377 /* Read the port's service parameters. */
4378 lpfc_read_sparam(phba, mboxq, vport->vpi);
4379 mboxq->vport = vport;
4380 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4381 mp = (struct lpfc_dmabuf *) mboxq->context1;
4382 if (rc == MBX_SUCCESS) {
4383 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4384 rc = 0;
4385 }
4386
4387 /*
4388 * This memory was allocated by the lpfc_read_sparam routine. Release
4389 * it to the mbuf pool.
4390 */
4391 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4392 kfree(mp);
4393 mboxq->context1 = NULL;
4394 if (unlikely(rc)) {
4395 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4396 "0382 READ_SPARAM command failed "
4397 "status %d, mbxStatus x%x\n",
4398 rc, bf_get(lpfc_mqe_status, mqe));
4399 phba->link_state = LPFC_HBA_ERROR;
4400 rc = -EIO;
4401 goto out_free_vpd;
4402 }
4403
4404 if (phba->cfg_soft_wwnn)
4405 u64_to_wwn(phba->cfg_soft_wwnn,
4406 vport->fc_sparam.nodeName.u.wwn);
4407 if (phba->cfg_soft_wwpn)
4408 u64_to_wwn(phba->cfg_soft_wwpn,
4409 vport->fc_sparam.portName.u.wwn);
4410 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4411 sizeof(struct lpfc_name));
4412 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4413 sizeof(struct lpfc_name));
4414
4415 /* Update the fc_host data structures with new wwn. */
4416 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4417 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4418
4419 /* Register SGL pool to the device using non-embedded mailbox command */
4420 rc = lpfc_sli4_post_sgl_list(phba);
4421 if (unlikely(rc)) {
4422 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004423 "0582 Error %d during sgl post operation\n",
4424 rc);
James Smartda0436e2009-05-22 14:51:39 -04004425 rc = -ENODEV;
4426 goto out_free_vpd;
4427 }
4428
4429 /* Register SCSI SGL pool to the device */
4430 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4431 if (unlikely(rc)) {
4432 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004433 "0383 Error %d during scsi sgl post "
4434 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004435 /* Some Scsi buffers were moved to the abort scsi list */
4436 /* A pci function reset will repost them */
4437 rc = -ENODEV;
4438 goto out_free_vpd;
4439 }
4440
4441 /* Post the rpi header region to the device. */
4442 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4443 if (unlikely(rc)) {
4444 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4445 "0393 Error %d during rpi post operation\n",
4446 rc);
4447 rc = -ENODEV;
4448 goto out_free_vpd;
4449 }
James Smartda0436e2009-05-22 14:51:39 -04004450
4451 /* Set up all the queues to the device */
4452 rc = lpfc_sli4_queue_setup(phba);
4453 if (unlikely(rc)) {
4454 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4455 "0381 Error %d during queue setup.\n ", rc);
4456 goto out_stop_timers;
4457 }
4458
4459 /* Arm the CQs and then EQs on device */
4460 lpfc_sli4_arm_cqeq_intr(phba);
4461
4462 /* Indicate device interrupt mode */
4463 phba->sli4_hba.intr_enable = 1;
4464
4465 /* Allow asynchronous mailbox command to go through */
4466 spin_lock_irq(&phba->hbalock);
4467 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4468 spin_unlock_irq(&phba->hbalock);
4469
4470 /* Post receive buffers to the device */
4471 lpfc_sli4_rb_setup(phba);
4472
4473 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004474 mod_timer(&vport->els_tmofunc,
4475 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004476
4477 /* Start heart beat timer */
4478 mod_timer(&phba->hb_tmofunc,
4479 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4480 phba->hb_outstanding = 0;
4481 phba->last_completion_time = jiffies;
4482
4483 /* Start error attention (ERATT) polling timer */
4484 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4485
4486 /*
4487 * The port is ready, set the host's link state to LINK_DOWN
4488 * in preparation for link interrupts.
4489 */
4490 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4491 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4492 lpfc_set_loopback_flag(phba);
4493 /* Change driver state to LPFC_LINK_DOWN right before init link */
4494 spin_lock_irq(&phba->hbalock);
4495 phba->link_state = LPFC_LINK_DOWN;
4496 spin_unlock_irq(&phba->hbalock);
4497 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4498 if (unlikely(rc != MBX_NOT_FINISHED)) {
4499 kfree(vpd);
4500 return 0;
4501 } else
4502 rc = -EIO;
4503
4504 /* Unset all the queues set up in this routine when error out */
4505 if (rc)
4506 lpfc_sli4_queue_unset(phba);
4507
4508out_stop_timers:
4509 if (rc)
4510 lpfc_stop_hba_timers(phba);
4511out_free_vpd:
4512 kfree(vpd);
4513out_free_mbox:
4514 mempool_free(mboxq, phba->mbox_mem_pool);
4515 return rc;
4516}
James Smarte59058c2008-08-24 21:49:00 -04004517
4518/**
James Smart3621a712009-04-06 18:47:14 -04004519 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004520 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004521 *
James Smarte59058c2008-08-24 21:49:00 -04004522 * This is the callback function for mailbox timer. The mailbox
4523 * timer is armed when a new mailbox command is issued and the timer
4524 * is deleted when the mailbox complete. The function is called by
4525 * the kernel timer code when a mailbox does not complete within
4526 * expected time. This function wakes up the worker thread to
4527 * process the mailbox timeout and returns. All the processing is
4528 * done by the worker thread function lpfc_mbox_timeout_handler.
4529 **/
dea31012005-04-17 16:05:31 -05004530void
4531lpfc_mbox_timeout(unsigned long ptr)
4532{
James Smart92d7f7b2007-06-17 19:56:38 -05004533 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004534 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004535 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004536
James Smart2e0fef82007-06-17 19:56:36 -05004537 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004538 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004539 if (!tmo_posted)
4540 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4541 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4542
James Smart5e9d9b82008-06-14 22:52:53 -04004543 if (!tmo_posted)
4544 lpfc_worker_wake_up(phba);
4545 return;
dea31012005-04-17 16:05:31 -05004546}
4547
James Smarte59058c2008-08-24 21:49:00 -04004548
4549/**
James Smart3621a712009-04-06 18:47:14 -04004550 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004551 * @phba: Pointer to HBA context object.
4552 *
4553 * This function is called from worker thread when a mailbox command times out.
4554 * The caller is not required to hold any locks. This function will reset the
4555 * HBA and recover all the pending commands.
4556 **/
dea31012005-04-17 16:05:31 -05004557void
4558lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4559{
James Smart2e0fef82007-06-17 19:56:36 -05004560 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004561 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004562 struct lpfc_sli *psli = &phba->sli;
4563 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004564
James Smarta257bf92009-04-06 18:48:10 -04004565 /* Check the pmbox pointer first. There is a race condition
4566 * between the mbox timeout handler getting executed in the
4567 * worklist and the mailbox actually completing. When this
4568 * race condition occurs, the mbox_active will be NULL.
4569 */
4570 spin_lock_irq(&phba->hbalock);
4571 if (pmbox == NULL) {
4572 lpfc_printf_log(phba, KERN_WARNING,
4573 LOG_MBOX | LOG_SLI,
4574 "0353 Active Mailbox cleared - mailbox timeout "
4575 "exiting\n");
4576 spin_unlock_irq(&phba->hbalock);
4577 return;
4578 }
4579
dea31012005-04-17 16:05:31 -05004580 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004581 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004582 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004583 mb->mbxCommand,
4584 phba->pport->port_state,
4585 phba->sli.sli_flag,
4586 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004587 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004588
James Smart1dcb58e2007-04-25 09:51:30 -04004589 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4590 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4591 * it to fail all oustanding SCSI IO.
4592 */
James Smart2e0fef82007-06-17 19:56:36 -05004593 spin_lock_irq(&phba->pport->work_port_lock);
4594 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4595 spin_unlock_irq(&phba->pport->work_port_lock);
4596 spin_lock_irq(&phba->hbalock);
4597 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004598 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004599 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004600
4601 pring = &psli->ring[psli->fcp_ring];
4602 lpfc_sli_abort_iocb_ring(phba, pring);
4603
4604 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004605 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004606
4607 /* Reset the HBA device */
4608 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004609}
4610
James Smarte59058c2008-08-24 21:49:00 -04004611/**
James Smart3772a992009-05-22 14:50:54 -04004612 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004613 * @phba: Pointer to HBA context object.
4614 * @pmbox: Pointer to mailbox object.
4615 * @flag: Flag indicating how the mailbox need to be processed.
4616 *
4617 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004618 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4619 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004620 * The mailbox command can be submitted in polling mode, in which case
4621 * this function will wait in a polling loop for the completion of the
4622 * mailbox.
4623 * If the mailbox is submitted in no_wait mode (not polling) the
4624 * function will submit the command and returns immediately without waiting
4625 * for the mailbox completion. The no_wait is supported only when HBA
4626 * is in SLI2/SLI3 mode - interrupts are enabled.
4627 * The SLI interface allows only one mailbox pending at a time. If the
4628 * mailbox is issued in polling mode and there is already a mailbox
4629 * pending, then the function will return an error. If the mailbox is issued
4630 * in NO_WAIT mode and there is a mailbox pending already, the function
4631 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4632 * The sli layer owns the mailbox object until the completion of mailbox
4633 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4634 * return codes the caller owns the mailbox command after the return of
4635 * the function.
4636 **/
James Smart3772a992009-05-22 14:50:54 -04004637static int
4638lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4639 uint32_t flag)
dea31012005-04-17 16:05:31 -05004640{
dea31012005-04-17 16:05:31 -05004641 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004642 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004643 uint32_t status, evtctr;
4644 uint32_t ha_copy;
4645 int i;
James Smart09372822008-01-11 01:52:54 -05004646 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004647 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004648 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004649 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004650 int processing_queue = 0;
4651
4652 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4653 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004654 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004655 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004656 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4657 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4658 return MBX_SUCCESS;
4659 }
James Smart58da1ff2008-04-07 10:15:56 -04004660 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004661 pmbox = lpfc_mbox_get(phba);
4662 if (!pmbox) {
4663 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4664 return MBX_SUCCESS;
4665 }
4666 }
dea31012005-04-17 16:05:31 -05004667
James Smarted957682007-06-17 19:56:37 -05004668 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004669 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004670 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004671 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004672 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004673 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004674 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004675 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004676 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004677 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004678 }
4679 }
4680
Linas Vepstas8d63f372007-02-14 14:28:36 -06004681 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004682 if (unlikely(pci_channel_offline(phba->pcidev))) {
4683 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4684 goto out_not_finished;
4685 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004686
James Smarta257bf92009-04-06 18:48:10 -04004687 /* If HBA has a deferred error attention, fail the iocb. */
4688 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4690 goto out_not_finished;
4691 }
4692
dea31012005-04-17 16:05:31 -05004693 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004694
James Smart3772a992009-05-22 14:50:54 -04004695 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004696 status = MBX_SUCCESS;
4697
James Smart2e0fef82007-06-17 19:56:36 -05004698 if (phba->link_state == LPFC_HBA_ERROR) {
4699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004700
4701 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004702 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4703 "(%d):0311 Mailbox command x%x cannot "
4704 "issue Data: x%x x%x\n",
4705 pmbox->vport ? pmbox->vport->vpi : 0,
4706 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004707 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004708 }
4709
James Smart92908312006-03-07 15:04:13 -05004710 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4711 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004712 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004713 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4714 "(%d):2528 Mailbox command x%x cannot "
4715 "issue Data: x%x x%x\n",
4716 pmbox->vport ? pmbox->vport->vpi : 0,
4717 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004718 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004719 }
4720
dea31012005-04-17 16:05:31 -05004721 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4722 /* Polling for a mbox command when another one is already active
4723 * is not allowed in SLI. Also, the driver must have established
4724 * SLI2 mode to queue and process multiple mbox commands.
4725 */
4726
4727 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004728 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004729
4730 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004731 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4732 "(%d):2529 Mailbox command x%x "
4733 "cannot issue Data: x%x x%x\n",
4734 pmbox->vport ? pmbox->vport->vpi : 0,
4735 pmbox->u.mb.mbxCommand,
4736 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004737 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004738 }
4739
James Smart3772a992009-05-22 14:50:54 -04004740 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004742 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004743 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4744 "(%d):2530 Mailbox command x%x "
4745 "cannot issue Data: x%x x%x\n",
4746 pmbox->vport ? pmbox->vport->vpi : 0,
4747 pmbox->u.mb.mbxCommand,
4748 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004749 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004750 }
4751
dea31012005-04-17 16:05:31 -05004752 /* Another mailbox command is still being processed, queue this
4753 * command to be processed later.
4754 */
4755 lpfc_mbox_put(phba, pmbox);
4756
4757 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004758 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004759 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004760 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004761 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4762 mb->mbxCommand, phba->pport->port_state,
4763 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004764
4765 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004766 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004767
James Smart858c9f62007-06-17 19:56:39 -05004768 if (pmbox->vport) {
4769 lpfc_debugfs_disc_trc(pmbox->vport,
4770 LPFC_DISC_TRC_MBOX_VPORT,
4771 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4772 (uint32_t)mb->mbxCommand,
4773 mb->un.varWords[0], mb->un.varWords[1]);
4774 }
4775 else {
4776 lpfc_debugfs_disc_trc(phba->pport,
4777 LPFC_DISC_TRC_MBOX,
4778 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4779 (uint32_t)mb->mbxCommand,
4780 mb->un.varWords[0], mb->un.varWords[1]);
4781 }
4782
James Smart2e0fef82007-06-17 19:56:36 -05004783 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004784 }
4785
dea31012005-04-17 16:05:31 -05004786 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4787
4788 /* If we are not polling, we MUST be in SLI2 mode */
4789 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004790 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004791 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004792 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004793 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004794 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004795 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4796 "(%d):2531 Mailbox command x%x "
4797 "cannot issue Data: x%x x%x\n",
4798 pmbox->vport ? pmbox->vport->vpi : 0,
4799 pmbox->u.mb.mbxCommand,
4800 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004801 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004802 }
4803 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004804 mod_timer(&psli->mbox_tmo, (jiffies +
4805 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004806 }
4807
4808 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004809 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004810 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004811 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004812 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004813 mb->mbxCommand, phba->pport->port_state,
4814 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004815
James Smart858c9f62007-06-17 19:56:39 -05004816 if (mb->mbxCommand != MBX_HEARTBEAT) {
4817 if (pmbox->vport) {
4818 lpfc_debugfs_disc_trc(pmbox->vport,
4819 LPFC_DISC_TRC_MBOX_VPORT,
4820 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4821 (uint32_t)mb->mbxCommand,
4822 mb->un.varWords[0], mb->un.varWords[1]);
4823 }
4824 else {
4825 lpfc_debugfs_disc_trc(phba->pport,
4826 LPFC_DISC_TRC_MBOX,
4827 "MBOX Send: cmd:x%x mb:x%x x%x",
4828 (uint32_t)mb->mbxCommand,
4829 mb->un.varWords[0], mb->un.varWords[1]);
4830 }
4831 }
4832
dea31012005-04-17 16:05:31 -05004833 psli->slistat.mbox_cmd++;
4834 evtctr = psli->slistat.mbox_event;
4835
4836 /* next set own bit for the adapter and copy over command word */
4837 mb->mbxOwner = OWN_CHIP;
4838
James Smart3772a992009-05-22 14:50:54 -04004839 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004840 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004841 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004842 } else {
James Smart92908312006-03-07 15:04:13 -05004843 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004844 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004845 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004846 }
4847
4848 /* First copy mbox command data to HBA SLIM, skip past first
4849 word */
4850 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4851 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4852 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4853
4854 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004855 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004856 to_slim = phba->MBslimaddr;
4857 writel(ldata, to_slim);
4858 readl(to_slim); /* flush */
4859
4860 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4861 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004862 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004863 }
4864 }
4865
4866 wmb();
dea31012005-04-17 16:05:31 -05004867
4868 switch (flag) {
4869 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004870 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004871 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004872 /* Interrupt board to do it */
4873 writel(CA_MBATT, phba->CAregaddr);
4874 readl(phba->CAregaddr); /* flush */
4875 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004876 break;
4877
4878 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004879 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004880 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004881 /* Interrupt board to do it */
4882 writel(CA_MBATT, phba->CAregaddr);
4883 readl(phba->CAregaddr); /* flush */
4884
James Smart3772a992009-05-22 14:50:54 -04004885 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004886 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004887 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004888 word0 = le32_to_cpu(word0);
4889 } else {
4890 /* First read mbox status word */
4891 word0 = readl(phba->MBslimaddr);
4892 }
4893
4894 /* Read the HBA Host Attention Register */
4895 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004896 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4897 mb->mbxCommand) *
4898 1000) + jiffies;
4899 i = 0;
dea31012005-04-17 16:05:31 -05004900 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004901 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4902 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004903 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004904 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004905 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004906 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004907 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004908 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004909 }
4910
4911 /* Check if we took a mbox interrupt while we were
4912 polling */
4913 if (((word0 & OWN_CHIP) != OWN_CHIP)
4914 && (evtctr != psli->slistat.mbox_event))
4915 break;
4916
James Smart09372822008-01-11 01:52:54 -05004917 if (i++ > 10) {
4918 spin_unlock_irqrestore(&phba->hbalock,
4919 drvr_flag);
4920 msleep(1);
4921 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4922 }
dea31012005-04-17 16:05:31 -05004923
James Smart3772a992009-05-22 14:50:54 -04004924 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004925 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004926 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004927 word0 = le32_to_cpu(word0);
4928 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4929 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004930 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004931 /* Check real SLIM for any errors */
4932 slimword0 = readl(phba->MBslimaddr);
4933 slimmb = (MAILBOX_t *) & slimword0;
4934 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4935 && slimmb->mbxStatus) {
4936 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004937 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004938 word0 = slimword0;
4939 }
4940 }
4941 } else {
4942 /* First copy command data */
4943 word0 = readl(phba->MBslimaddr);
4944 }
4945 /* Read the HBA Host Attention Register */
4946 ha_copy = readl(phba->HAregaddr);
4947 }
4948
James Smart3772a992009-05-22 14:50:54 -04004949 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004950 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04004951 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004952 } else {
4953 /* First copy command data */
4954 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4955 MAILBOX_CMD_SIZE);
4956 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4957 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05004958 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05004959 phba->MBslimaddr + DMP_RSP_OFFSET,
4960 mb->un.varDmp.word_cnt);
4961 }
4962 }
4963
4964 writel(HA_MBATT, phba->HAregaddr);
4965 readl(phba->HAregaddr); /* flush */
4966
4967 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4968 status = mb->mbxStatus;
4969 }
4970
James Smart2e0fef82007-06-17 19:56:36 -05004971 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4972 return status;
James Smart58da1ff2008-04-07 10:15:56 -04004973
4974out_not_finished:
4975 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04004976 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04004977 lpfc_mbox_cmpl_put(phba, pmbox);
4978 }
4979 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004980}
4981
James Smarte59058c2008-08-24 21:49:00 -04004982/**
James Smartf1126682009-06-10 17:22:44 -04004983 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
4984 * @phba: Pointer to HBA context object.
4985 *
4986 * The function blocks the posting of SLI4 asynchronous mailbox commands from
4987 * the driver internal pending mailbox queue. It will then try to wait out the
4988 * possible outstanding mailbox command before return.
4989 *
4990 * Returns:
4991 * 0 - the outstanding mailbox command completed; otherwise, the wait for
4992 * the outstanding mailbox command timed out.
4993 **/
4994static int
4995lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
4996{
4997 struct lpfc_sli *psli = &phba->sli;
4998 uint8_t actcmd = MBX_HEARTBEAT;
4999 int rc = 0;
5000 unsigned long timeout;
5001
5002 /* Mark the asynchronous mailbox command posting as blocked */
5003 spin_lock_irq(&phba->hbalock);
5004 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5005 if (phba->sli.mbox_active)
5006 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5007 spin_unlock_irq(&phba->hbalock);
5008 /* Determine how long we might wait for the active mailbox
5009 * command to be gracefully completed by firmware.
5010 */
5011 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5012 jiffies;
5013 /* Wait for the outstnading mailbox command to complete */
5014 while (phba->sli.mbox_active) {
5015 /* Check active mailbox complete status every 2ms */
5016 msleep(2);
5017 if (time_after(jiffies, timeout)) {
5018 /* Timeout, marked the outstanding cmd not complete */
5019 rc = 1;
5020 break;
5021 }
5022 }
5023
5024 /* Can not cleanly block async mailbox command, fails it */
5025 if (rc) {
5026 spin_lock_irq(&phba->hbalock);
5027 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5028 spin_unlock_irq(&phba->hbalock);
5029 }
5030 return rc;
5031}
5032
5033/**
5034 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5035 * @phba: Pointer to HBA context object.
5036 *
5037 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5038 * commands from the driver internal pending mailbox queue. It makes sure
5039 * that there is no outstanding mailbox command before resuming posting
5040 * asynchronous mailbox commands. If, for any reason, there is outstanding
5041 * mailbox command, it will try to wait it out before resuming asynchronous
5042 * mailbox command posting.
5043 **/
5044static void
5045lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5046{
5047 struct lpfc_sli *psli = &phba->sli;
5048
5049 spin_lock_irq(&phba->hbalock);
5050 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5051 /* Asynchronous mailbox posting is not blocked, do nothing */
5052 spin_unlock_irq(&phba->hbalock);
5053 return;
5054 }
5055
5056 /* Outstanding synchronous mailbox command is guaranteed to be done,
5057 * successful or timeout, after timing-out the outstanding mailbox
5058 * command shall always be removed, so just unblock posting async
5059 * mailbox command and resume
5060 */
5061 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5062 spin_unlock_irq(&phba->hbalock);
5063
5064 /* wake up worker thread to post asynchronlous mailbox command */
5065 lpfc_worker_wake_up(phba);
5066}
5067
5068/**
James Smartda0436e2009-05-22 14:51:39 -04005069 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5070 * @phba: Pointer to HBA context object.
5071 * @mboxq: Pointer to mailbox object.
5072 *
5073 * The function posts a mailbox to the port. The mailbox is expected
5074 * to be comletely filled in and ready for the port to operate on it.
5075 * This routine executes a synchronous completion operation on the
5076 * mailbox by polling for its completion.
5077 *
5078 * The caller must not be holding any locks when calling this routine.
5079 *
5080 * Returns:
5081 * MBX_SUCCESS - mailbox posted successfully
5082 * Any of the MBX error values.
5083 **/
5084static int
5085lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5086{
5087 int rc = MBX_SUCCESS;
5088 unsigned long iflag;
5089 uint32_t db_ready;
5090 uint32_t mcqe_status;
5091 uint32_t mbx_cmnd;
5092 unsigned long timeout;
5093 struct lpfc_sli *psli = &phba->sli;
5094 struct lpfc_mqe *mb = &mboxq->u.mqe;
5095 struct lpfc_bmbx_create *mbox_rgn;
5096 struct dma_address *dma_address;
5097 struct lpfc_register bmbx_reg;
5098
5099 /*
5100 * Only one mailbox can be active to the bootstrap mailbox region
5101 * at a time and there is no queueing provided.
5102 */
5103 spin_lock_irqsave(&phba->hbalock, iflag);
5104 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5105 spin_unlock_irqrestore(&phba->hbalock, iflag);
5106 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5107 "(%d):2532 Mailbox command x%x (x%x) "
5108 "cannot issue Data: x%x x%x\n",
5109 mboxq->vport ? mboxq->vport->vpi : 0,
5110 mboxq->u.mb.mbxCommand,
5111 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5112 psli->sli_flag, MBX_POLL);
5113 return MBXERR_ERROR;
5114 }
5115 /* The server grabs the token and owns it until release */
5116 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5117 phba->sli.mbox_active = mboxq;
5118 spin_unlock_irqrestore(&phba->hbalock, iflag);
5119
5120 /*
5121 * Initialize the bootstrap memory region to avoid stale data areas
5122 * in the mailbox post. Then copy the caller's mailbox contents to
5123 * the bmbx mailbox region.
5124 */
5125 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5126 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5127 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5128 sizeof(struct lpfc_mqe));
5129
5130 /* Post the high mailbox dma address to the port and wait for ready. */
5131 dma_address = &phba->sli4_hba.bmbx.dma_address;
5132 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5133
5134 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5135 * 1000) + jiffies;
5136 do {
5137 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5138 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5139 if (!db_ready)
5140 msleep(2);
5141
5142 if (time_after(jiffies, timeout)) {
5143 rc = MBXERR_ERROR;
5144 goto exit;
5145 }
5146 } while (!db_ready);
5147
5148 /* Post the low mailbox dma address to the port. */
5149 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5150 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5151 * 1000) + jiffies;
5152 do {
5153 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5154 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5155 if (!db_ready)
5156 msleep(2);
5157
5158 if (time_after(jiffies, timeout)) {
5159 rc = MBXERR_ERROR;
5160 goto exit;
5161 }
5162 } while (!db_ready);
5163
5164 /*
5165 * Read the CQ to ensure the mailbox has completed.
5166 * If so, update the mailbox status so that the upper layers
5167 * can complete the request normally.
5168 */
5169 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5170 sizeof(struct lpfc_mqe));
5171 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5172 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5173 sizeof(struct lpfc_mcqe));
5174 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5175
5176 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5177 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5178 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5179 rc = MBXERR_ERROR;
5180 }
5181
5182 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5183 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5184 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5185 " x%x x%x CQ: x%x x%x x%x x%x\n",
5186 mboxq->vport ? mboxq->vport->vpi : 0,
5187 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5188 bf_get(lpfc_mqe_status, mb),
5189 mb->un.mb_words[0], mb->un.mb_words[1],
5190 mb->un.mb_words[2], mb->un.mb_words[3],
5191 mb->un.mb_words[4], mb->un.mb_words[5],
5192 mb->un.mb_words[6], mb->un.mb_words[7],
5193 mb->un.mb_words[8], mb->un.mb_words[9],
5194 mb->un.mb_words[10], mb->un.mb_words[11],
5195 mb->un.mb_words[12], mboxq->mcqe.word0,
5196 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5197 mboxq->mcqe.trailer);
5198exit:
5199 /* We are holding the token, no needed for lock when release */
5200 spin_lock_irqsave(&phba->hbalock, iflag);
5201 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5202 phba->sli.mbox_active = NULL;
5203 spin_unlock_irqrestore(&phba->hbalock, iflag);
5204 return rc;
5205}
5206
5207/**
5208 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5209 * @phba: Pointer to HBA context object.
5210 * @pmbox: Pointer to mailbox object.
5211 * @flag: Flag indicating how the mailbox need to be processed.
5212 *
5213 * This function is called by discovery code and HBA management code to submit
5214 * a mailbox command to firmware with SLI-4 interface spec.
5215 *
5216 * Return codes the caller owns the mailbox command after the return of the
5217 * function.
5218 **/
5219static int
5220lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5221 uint32_t flag)
5222{
5223 struct lpfc_sli *psli = &phba->sli;
5224 unsigned long iflags;
5225 int rc;
5226
James Smart8fa38512009-07-19 10:01:03 -04005227 rc = lpfc_mbox_dev_check(phba);
5228 if (unlikely(rc)) {
5229 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5230 "(%d):2544 Mailbox command x%x (x%x) "
5231 "cannot issue Data: x%x x%x\n",
5232 mboxq->vport ? mboxq->vport->vpi : 0,
5233 mboxq->u.mb.mbxCommand,
5234 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5235 psli->sli_flag, flag);
5236 goto out_not_finished;
5237 }
5238
James Smartda0436e2009-05-22 14:51:39 -04005239 /* Detect polling mode and jump to a handler */
5240 if (!phba->sli4_hba.intr_enable) {
5241 if (flag == MBX_POLL)
5242 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5243 else
5244 rc = -EIO;
5245 if (rc != MBX_SUCCESS)
5246 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5247 "(%d):2541 Mailbox command x%x "
5248 "(x%x) cannot issue Data: x%x x%x\n",
5249 mboxq->vport ? mboxq->vport->vpi : 0,
5250 mboxq->u.mb.mbxCommand,
5251 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5252 psli->sli_flag, flag);
5253 return rc;
5254 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005255 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5256 "(%d):2542 Try to issue mailbox command "
5257 "x%x (x%x) synchronously ahead of async"
5258 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005259 mboxq->vport ? mboxq->vport->vpi : 0,
5260 mboxq->u.mb.mbxCommand,
5261 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5262 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005263 /* Try to block the asynchronous mailbox posting */
5264 rc = lpfc_sli4_async_mbox_block(phba);
5265 if (!rc) {
5266 /* Successfully blocked, now issue sync mbox cmd */
5267 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5268 if (rc != MBX_SUCCESS)
5269 lpfc_printf_log(phba, KERN_ERR,
5270 LOG_MBOX | LOG_SLI,
5271 "(%d):2597 Mailbox command "
5272 "x%x (x%x) cannot issue "
5273 "Data: x%x x%x\n",
5274 mboxq->vport ?
5275 mboxq->vport->vpi : 0,
5276 mboxq->u.mb.mbxCommand,
5277 lpfc_sli4_mbox_opcode_get(phba,
5278 mboxq),
5279 psli->sli_flag, flag);
5280 /* Unblock the async mailbox posting afterward */
5281 lpfc_sli4_async_mbox_unblock(phba);
5282 }
5283 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005284 }
5285
5286 /* Now, interrupt mode asynchrous mailbox command */
5287 rc = lpfc_mbox_cmd_check(phba, mboxq);
5288 if (rc) {
5289 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5290 "(%d):2543 Mailbox command x%x (x%x) "
5291 "cannot issue Data: x%x x%x\n",
5292 mboxq->vport ? mboxq->vport->vpi : 0,
5293 mboxq->u.mb.mbxCommand,
5294 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5295 psli->sli_flag, flag);
5296 goto out_not_finished;
5297 }
James Smartda0436e2009-05-22 14:51:39 -04005298
5299 /* Put the mailbox command to the driver internal FIFO */
5300 psli->slistat.mbox_busy++;
5301 spin_lock_irqsave(&phba->hbalock, iflags);
5302 lpfc_mbox_put(phba, mboxq);
5303 spin_unlock_irqrestore(&phba->hbalock, iflags);
5304 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5305 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5306 "x%x (x%x) x%x x%x x%x\n",
5307 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5308 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5309 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5310 phba->pport->port_state,
5311 psli->sli_flag, MBX_NOWAIT);
5312 /* Wake up worker thread to transport mailbox command from head */
5313 lpfc_worker_wake_up(phba);
5314
5315 return MBX_BUSY;
5316
5317out_not_finished:
5318 return MBX_NOT_FINISHED;
5319}
5320
5321/**
5322 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5323 * @phba: Pointer to HBA context object.
5324 *
5325 * This function is called by worker thread to send a mailbox command to
5326 * SLI4 HBA firmware.
5327 *
5328 **/
5329int
5330lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5331{
5332 struct lpfc_sli *psli = &phba->sli;
5333 LPFC_MBOXQ_t *mboxq;
5334 int rc = MBX_SUCCESS;
5335 unsigned long iflags;
5336 struct lpfc_mqe *mqe;
5337 uint32_t mbx_cmnd;
5338
5339 /* Check interrupt mode before post async mailbox command */
5340 if (unlikely(!phba->sli4_hba.intr_enable))
5341 return MBX_NOT_FINISHED;
5342
5343 /* Check for mailbox command service token */
5344 spin_lock_irqsave(&phba->hbalock, iflags);
5345 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5346 spin_unlock_irqrestore(&phba->hbalock, iflags);
5347 return MBX_NOT_FINISHED;
5348 }
5349 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5350 spin_unlock_irqrestore(&phba->hbalock, iflags);
5351 return MBX_NOT_FINISHED;
5352 }
5353 if (unlikely(phba->sli.mbox_active)) {
5354 spin_unlock_irqrestore(&phba->hbalock, iflags);
5355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5356 "0384 There is pending active mailbox cmd\n");
5357 return MBX_NOT_FINISHED;
5358 }
5359 /* Take the mailbox command service token */
5360 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5361
5362 /* Get the next mailbox command from head of queue */
5363 mboxq = lpfc_mbox_get(phba);
5364
5365 /* If no more mailbox command waiting for post, we're done */
5366 if (!mboxq) {
5367 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5368 spin_unlock_irqrestore(&phba->hbalock, iflags);
5369 return MBX_SUCCESS;
5370 }
5371 phba->sli.mbox_active = mboxq;
5372 spin_unlock_irqrestore(&phba->hbalock, iflags);
5373
5374 /* Check device readiness for posting mailbox command */
5375 rc = lpfc_mbox_dev_check(phba);
5376 if (unlikely(rc))
5377 /* Driver clean routine will clean up pending mailbox */
5378 goto out_not_finished;
5379
5380 /* Prepare the mbox command to be posted */
5381 mqe = &mboxq->u.mqe;
5382 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5383
5384 /* Start timer for the mbox_tmo and log some mailbox post messages */
5385 mod_timer(&psli->mbox_tmo, (jiffies +
5386 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5387
5388 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5389 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5390 "x%x x%x\n",
5391 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5392 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5393 phba->pport->port_state, psli->sli_flag);
5394
5395 if (mbx_cmnd != MBX_HEARTBEAT) {
5396 if (mboxq->vport) {
5397 lpfc_debugfs_disc_trc(mboxq->vport,
5398 LPFC_DISC_TRC_MBOX_VPORT,
5399 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5400 mbx_cmnd, mqe->un.mb_words[0],
5401 mqe->un.mb_words[1]);
5402 } else {
5403 lpfc_debugfs_disc_trc(phba->pport,
5404 LPFC_DISC_TRC_MBOX,
5405 "MBOX Send: cmd:x%x mb:x%x x%x",
5406 mbx_cmnd, mqe->un.mb_words[0],
5407 mqe->un.mb_words[1]);
5408 }
5409 }
5410 psli->slistat.mbox_cmd++;
5411
5412 /* Post the mailbox command to the port */
5413 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5414 if (rc != MBX_SUCCESS) {
5415 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5416 "(%d):2533 Mailbox command x%x (x%x) "
5417 "cannot issue Data: x%x x%x\n",
5418 mboxq->vport ? mboxq->vport->vpi : 0,
5419 mboxq->u.mb.mbxCommand,
5420 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5421 psli->sli_flag, MBX_NOWAIT);
5422 goto out_not_finished;
5423 }
5424
5425 return rc;
5426
5427out_not_finished:
5428 spin_lock_irqsave(&phba->hbalock, iflags);
5429 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5430 __lpfc_mbox_cmpl_put(phba, mboxq);
5431 /* Release the token */
5432 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5433 phba->sli.mbox_active = NULL;
5434 spin_unlock_irqrestore(&phba->hbalock, iflags);
5435
5436 return MBX_NOT_FINISHED;
5437}
5438
5439/**
5440 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5441 * @phba: Pointer to HBA context object.
5442 * @pmbox: Pointer to mailbox object.
5443 * @flag: Flag indicating how the mailbox need to be processed.
5444 *
5445 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5446 * the API jump table function pointer from the lpfc_hba struct.
5447 *
5448 * Return codes the caller owns the mailbox command after the return of the
5449 * function.
5450 **/
5451int
5452lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5453{
5454 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5455}
5456
5457/**
5458 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5459 * @phba: The hba struct for which this call is being executed.
5460 * @dev_grp: The HBA PCI-Device group number.
5461 *
5462 * This routine sets up the mbox interface API function jump table in @phba
5463 * struct.
5464 * Returns: 0 - success, -ENODEV - failure.
5465 **/
5466int
5467lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5468{
5469
5470 switch (dev_grp) {
5471 case LPFC_PCI_DEV_LP:
5472 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5473 phba->lpfc_sli_handle_slow_ring_event =
5474 lpfc_sli_handle_slow_ring_event_s3;
5475 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5476 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5477 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5478 break;
5479 case LPFC_PCI_DEV_OC:
5480 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5481 phba->lpfc_sli_handle_slow_ring_event =
5482 lpfc_sli_handle_slow_ring_event_s4;
5483 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5484 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5485 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5486 break;
5487 default:
5488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5489 "1420 Invalid HBA PCI-device group: 0x%x\n",
5490 dev_grp);
5491 return -ENODEV;
5492 break;
5493 }
5494 return 0;
5495}
5496
5497/**
James Smart3621a712009-04-06 18:47:14 -04005498 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005499 * @phba: Pointer to HBA context object.
5500 * @pring: Pointer to driver SLI ring object.
5501 * @piocb: Pointer to address of newly added command iocb.
5502 *
5503 * This function is called with hbalock held to add a command
5504 * iocb to the txq when SLI layer cannot submit the command iocb
5505 * to the ring.
5506 **/
James Smart858c9f62007-06-17 19:56:39 -05005507static void
James Smart92d7f7b2007-06-17 19:56:38 -05005508__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005509 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005510{
5511 /* Insert the caller's iocb in the txq tail for later processing. */
5512 list_add_tail(&piocb->list, &pring->txq);
5513 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005514}
5515
James Smarte59058c2008-08-24 21:49:00 -04005516/**
James Smart3621a712009-04-06 18:47:14 -04005517 * lpfc_sli_next_iocb - Get the next iocb in 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 before a new
5523 * iocb is submitted to the firmware. This function checks
5524 * txq to flush the iocbs in txq to Firmware before
5525 * submitting new iocbs to the Firmware.
5526 * If there are iocbs in the txq which need to be submitted
5527 * to firmware, lpfc_sli_next_iocb returns the first element
5528 * of the txq after dequeuing it from txq.
5529 * If there is no iocb in the txq then the function will return
5530 * *piocb and *piocb is set to NULL. Caller needs to check
5531 * *piocb to find if there are more commands in the txq.
5532 **/
dea31012005-04-17 16:05:31 -05005533static struct lpfc_iocbq *
5534lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005535 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005536{
5537 struct lpfc_iocbq * nextiocb;
5538
5539 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5540 if (!nextiocb) {
5541 nextiocb = *piocb;
5542 *piocb = NULL;
5543 }
5544
5545 return nextiocb;
5546}
5547
James Smarte59058c2008-08-24 21:49:00 -04005548/**
James Smart3772a992009-05-22 14:50:54 -04005549 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005550 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005551 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005552 * @piocb: Pointer to command iocb.
5553 * @flag: Flag indicating if this command can be put into txq.
5554 *
James Smart3772a992009-05-22 14:50:54 -04005555 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5556 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5557 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5558 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5559 * this function allows only iocbs for posting buffers. This function finds
5560 * next available slot in the command ring and posts the command to the
5561 * available slot and writes the port attention register to request HBA start
5562 * processing new iocb. If there is no slot available in the ring and
5563 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5564 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005565 *
James Smart3772a992009-05-22 14:50:54 -04005566 * This function is called with hbalock held. The function will return success
5567 * after it successfully submit the iocb to firmware or after adding to the
5568 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005569 **/
James Smart98c9ea52007-10-27 13:37:33 -04005570static int
James Smart3772a992009-05-22 14:50:54 -04005571__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005572 struct lpfc_iocbq *piocb, uint32_t flag)
5573{
5574 struct lpfc_iocbq *nextiocb;
5575 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005576 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005577
James Smart92d7f7b2007-06-17 19:56:38 -05005578 if (piocb->iocb_cmpl && (!piocb->vport) &&
5579 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5580 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5581 lpfc_printf_log(phba, KERN_ERR,
5582 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005583 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005584 piocb->iocb.ulpCommand);
5585 dump_stack();
5586 return IOCB_ERROR;
5587 }
5588
5589
Linas Vepstas8d63f372007-02-14 14:28:36 -06005590 /* If the PCI channel is in offline state, do not post iocbs. */
5591 if (unlikely(pci_channel_offline(phba->pcidev)))
5592 return IOCB_ERROR;
5593
James Smarta257bf92009-04-06 18:48:10 -04005594 /* If HBA has a deferred error attention, fail the iocb. */
5595 if (unlikely(phba->hba_flag & DEFER_ERATT))
5596 return IOCB_ERROR;
5597
dea31012005-04-17 16:05:31 -05005598 /*
5599 * We should never get an IOCB if we are in a < LINK_DOWN state
5600 */
James Smart2e0fef82007-06-17 19:56:36 -05005601 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005602 return IOCB_ERROR;
5603
5604 /*
5605 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005606 * outstanding event.
dea31012005-04-17 16:05:31 -05005607 */
James Smart0b727fe2007-10-27 13:37:25 -04005608 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005609 goto iocb_busy;
5610
James Smart2e0fef82007-06-17 19:56:36 -05005611 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005612 /*
James Smart2680eea2007-04-25 09:52:55 -04005613 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005614 * can be issued if the link is not up.
5615 */
5616 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005617 case CMD_GEN_REQUEST64_CR:
5618 case CMD_GEN_REQUEST64_CX:
5619 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5620 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005621 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005622 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5623 MENLO_TRANSPORT_TYPE))
5624
5625 goto iocb_busy;
5626 break;
dea31012005-04-17 16:05:31 -05005627 case CMD_QUE_RING_BUF_CN:
5628 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005629 /*
5630 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5631 * completion, iocb_cmpl MUST be 0.
5632 */
5633 if (piocb->iocb_cmpl)
5634 piocb->iocb_cmpl = NULL;
5635 /*FALLTHROUGH*/
5636 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005637 case CMD_CLOSE_XRI_CN:
5638 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005639 break;
5640 default:
5641 goto iocb_busy;
5642 }
5643
5644 /*
5645 * For FCP commands, we must be in a state where we can process link
5646 * attention events.
5647 */
5648 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005649 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005650 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005651 }
dea31012005-04-17 16:05:31 -05005652
dea31012005-04-17 16:05:31 -05005653 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5654 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5655 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5656
5657 if (iocb)
5658 lpfc_sli_update_ring(phba, pring);
5659 else
5660 lpfc_sli_update_full_ring(phba, pring);
5661
5662 if (!piocb)
5663 return IOCB_SUCCESS;
5664
5665 goto out_busy;
5666
5667 iocb_busy:
5668 pring->stats.iocb_cmd_delay++;
5669
5670 out_busy:
5671
5672 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005673 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005674 return IOCB_SUCCESS;
5675 }
5676
5677 return IOCB_BUSY;
5678}
5679
James Smart3772a992009-05-22 14:50:54 -04005680/**
James Smart4f774512009-05-22 14:52:35 -04005681 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5682 * @phba: Pointer to HBA context object.
5683 * @piocb: Pointer to command iocb.
5684 * @sglq: Pointer to the scatter gather queue object.
5685 *
5686 * This routine converts the bpl or bde that is in the IOCB
5687 * to a sgl list for the sli4 hardware. The physical address
5688 * of the bpl/bde is converted back to a virtual address.
5689 * If the IOCB contains a BPL then the list of BDE's is
5690 * converted to sli4_sge's. If the IOCB contains a single
5691 * BDE then it is converted to a single sli_sge.
5692 * The IOCB is still in cpu endianess so the contents of
5693 * the bpl can be used without byte swapping.
5694 *
5695 * Returns valid XRI = Success, NO_XRI = Failure.
5696**/
5697static uint16_t
5698lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5699 struct lpfc_sglq *sglq)
5700{
5701 uint16_t xritag = NO_XRI;
5702 struct ulp_bde64 *bpl = NULL;
5703 struct ulp_bde64 bde;
5704 struct sli4_sge *sgl = NULL;
5705 IOCB_t *icmd;
5706 int numBdes = 0;
5707 int i = 0;
5708
5709 if (!piocbq || !sglq)
5710 return xritag;
5711
5712 sgl = (struct sli4_sge *)sglq->sgl;
5713 icmd = &piocbq->iocb;
5714 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5715 numBdes = icmd->un.genreq64.bdl.bdeSize /
5716 sizeof(struct ulp_bde64);
5717 /* The addrHigh and addrLow fields within the IOCB
5718 * have not been byteswapped yet so there is no
5719 * need to swap them back.
5720 */
5721 bpl = (struct ulp_bde64 *)
5722 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5723
5724 if (!bpl)
5725 return xritag;
5726
5727 for (i = 0; i < numBdes; i++) {
5728 /* Should already be byte swapped. */
5729 sgl->addr_hi = bpl->addrHigh;
5730 sgl->addr_lo = bpl->addrLow;
5731 /* swap the size field back to the cpu so we
5732 * can assign it to the sgl.
5733 */
5734 bde.tus.w = le32_to_cpu(bpl->tus.w);
5735 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5736 if ((i+1) == numBdes)
5737 bf_set(lpfc_sli4_sge_last, sgl, 1);
5738 else
5739 bf_set(lpfc_sli4_sge_last, sgl, 0);
5740 sgl->word2 = cpu_to_le32(sgl->word2);
5741 sgl->word3 = cpu_to_le32(sgl->word3);
5742 bpl++;
5743 sgl++;
5744 }
5745 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5746 /* The addrHigh and addrLow fields of the BDE have not
5747 * been byteswapped yet so they need to be swapped
5748 * before putting them in the sgl.
5749 */
5750 sgl->addr_hi =
5751 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5752 sgl->addr_lo =
5753 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5754 bf_set(lpfc_sli4_sge_len, sgl,
5755 icmd->un.genreq64.bdl.bdeSize);
5756 bf_set(lpfc_sli4_sge_last, sgl, 1);
5757 sgl->word2 = cpu_to_le32(sgl->word2);
5758 sgl->word3 = cpu_to_le32(sgl->word3);
5759 }
5760 return sglq->sli4_xritag;
5761}
5762
5763/**
5764 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5765 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005766 *
5767 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005768 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5769 * held.
James Smart4f774512009-05-22 14:52:35 -04005770 *
5771 * Return: index into SLI4 fast-path FCP queue index.
5772 **/
5773static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005774lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005775{
James Smart8fa38512009-07-19 10:01:03 -04005776 ++phba->fcp_qidx;
5777 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5778 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005779
James Smart8fa38512009-07-19 10:01:03 -04005780 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005781}
5782
5783/**
5784 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5785 * @phba: Pointer to HBA context object.
5786 * @piocb: Pointer to command iocb.
5787 * @wqe: Pointer to the work queue entry.
5788 *
5789 * This routine converts the iocb command to its Work Queue Entry
5790 * equivalent. The wqe pointer should not have any fields set when
5791 * this routine is called because it will memcpy over them.
5792 * This routine does not set the CQ_ID or the WQEC bits in the
5793 * wqe.
5794 *
5795 * Returns: 0 = Success, IOCB_ERROR = Failure.
5796 **/
5797static int
5798lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5799 union lpfc_wqe *wqe)
5800{
James Smart5ffc2662009-11-18 15:39:44 -05005801 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005802 uint8_t ct = 0;
5803 uint32_t fip;
5804 uint32_t abort_tag;
5805 uint8_t command_type = ELS_COMMAND_NON_FIP;
5806 uint8_t cmnd;
5807 uint16_t xritag;
5808 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005809 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005810 int numBdes, i;
5811 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005812
James Smart45ed1192009-10-02 15:17:02 -04005813 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005814 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005815 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005816 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005817 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005818 command_type = ELS_COMMAND_FIP;
5819 else
5820 command_type = ELS_COMMAND_NON_FIP;
5821
James Smart4f774512009-05-22 14:52:35 -04005822 /* Some of the fields are in the right position already */
5823 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5824 abort_tag = (uint32_t) iocbq->iotag;
5825 xritag = iocbq->sli4_xritag;
5826 wqe->words[7] = 0; /* The ct field has moved so reset */
5827 /* words0-2 bpl convert bde */
5828 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005829 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5830 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005831 bpl = (struct ulp_bde64 *)
5832 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5833 if (!bpl)
5834 return IOCB_ERROR;
5835
5836 /* Should already be byte swapped. */
5837 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5838 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5839 /* swap the size field back to the cpu so we
5840 * can assign it to the sgl.
5841 */
5842 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005843 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5844 total_len = 0;
5845 for (i = 0; i < numBdes; i++) {
5846 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5847 total_len += bde.tus.f.bdeSize;
5848 }
James Smart4f774512009-05-22 14:52:35 -04005849 } else
James Smart5ffc2662009-11-18 15:39:44 -05005850 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005851
5852 iocbq->iocb.ulpIoTag = iocbq->iotag;
5853 cmnd = iocbq->iocb.ulpCommand;
5854
5855 switch (iocbq->iocb.ulpCommand) {
5856 case CMD_ELS_REQUEST64_CR:
5857 if (!iocbq->iocb.ulpLe) {
5858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5859 "2007 Only Limited Edition cmd Format"
5860 " supported 0x%x\n",
5861 iocbq->iocb.ulpCommand);
5862 return IOCB_ERROR;
5863 }
James Smart5ffc2662009-11-18 15:39:44 -05005864 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005865 /* Els_reguest64 has a TMO */
5866 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5867 iocbq->iocb.ulpTimeout);
5868 /* Need a VF for word 4 set the vf bit*/
5869 bf_set(els_req64_vf, &wqe->els_req, 0);
5870 /* And a VFID for word 12 */
5871 bf_set(els_req64_vfid, &wqe->els_req, 0);
5872 /*
5873 * Set ct field to 3, indicates that the context_tag field
5874 * contains the FCFI and remote N_Port_ID is
5875 * in word 5.
5876 */
5877
5878 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5879 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5880 iocbq->iocb.ulpContext);
5881
James Smart4f774512009-05-22 14:52:35 -04005882 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5883 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5884 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005885
5886 if (command_type == ELS_COMMAND_FIP) {
5887 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5888 >> LPFC_FIP_ELS_ID_SHIFT);
5889 }
5890 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5891
James Smart4f774512009-05-22 14:52:35 -04005892 break;
James Smart5ffc2662009-11-18 15:39:44 -05005893 case CMD_XMIT_SEQUENCE64_CX:
5894 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5895 iocbq->iocb.un.ulpWord[3]);
5896 wqe->generic.word3 = 0;
5897 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05005898 /* The entire sequence is transmitted for this IOCB */
5899 xmit_len = total_len;
5900 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005901 case CMD_XMIT_SEQUENCE64_CR:
5902 /* word3 iocb=io_tag32 wqe=payload_offset */
5903 /* payload offset used for multilpe outstanding
5904 * sequences on the same exchange
5905 */
5906 wqe->words[3] = 0;
5907 /* word4 relative_offset memcpy */
5908 /* word5 r_ctl/df_ctl memcpy */
5909 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005910 wqe->xmit_sequence.xmit_len = xmit_len;
5911 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005912 break;
5913 case CMD_XMIT_BCAST64_CN:
5914 /* word3 iocb=iotag32 wqe=payload_len */
5915 wqe->words[3] = 0; /* no definition for this in wqe */
5916 /* word4 iocb=rsvd wqe=rsvd */
5917 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5918 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5919 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5920 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5921 break;
5922 case CMD_FCP_IWRITE64_CR:
5923 command_type = FCP_COMMAND_DATA_OUT;
5924 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5925 * confusing.
5926 * word3 is payload_len: byte offset to the sgl entry for the
5927 * fcp_command.
5928 * word4 is total xfer len, same as the IOCB->ulpParameter.
5929 * word5 is initial xfer len 0 = wait for xfer-ready
5930 */
5931
5932 /* Always wait for xfer-ready before sending data */
5933 wqe->fcp_iwrite.initial_xfer_len = 0;
5934 /* word 4 (xfer length) should have been set on the memcpy */
5935
5936 /* allow write to fall through to read */
5937 case CMD_FCP_IREAD64_CR:
5938 /* FCP_CMD is always the 1st sgl entry */
5939 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05005940 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04005941
5942 /* word 4 (xfer length) should have been set on the memcpy */
5943
5944 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5945 iocbq->iocb.ulpFCP2Rcvy);
5946 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5947 /* The XC bit and the XS bit are similar. The driver never
5948 * tracked whether or not the exchange was previouslly open.
5949 * XC = Exchange create, 0 is create. 1 is already open.
5950 * XS = link cmd: 1 do not close the exchange after command.
5951 * XS = 0 close exchange when command completes.
5952 * The only time we would not set the XC bit is when the XS bit
5953 * is set and we are sending our 2nd or greater command on
5954 * this exchange.
5955 */
James Smart4f774512009-05-22 14:52:35 -04005956 /* Always open the exchange */
5957 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5958
5959 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5960 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04005961 break;
5962 case CMD_FCP_ICMND64_CR:
5963 /* Always open the exchange */
5964 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5965
5966 wqe->words[4] = 0;
5967 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5968 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04005969 break;
5970 case CMD_GEN_REQUEST64_CR:
5971 /* word3 command length is described as byte offset to the
5972 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5973 * sgl[0] = cmnd
5974 * sgl[1] = rsp.
5975 *
5976 */
James Smart5ffc2662009-11-18 15:39:44 -05005977 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005978 /* Word4 parameter copied in the memcpy */
5979 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
5980 /* word6 context tag copied in memcpy */
5981 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
5982 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5983 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5984 "2015 Invalid CT %x command 0x%x\n",
5985 ct, iocbq->iocb.ulpCommand);
5986 return IOCB_ERROR;
5987 }
5988 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
5989 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
5990 iocbq->iocb.ulpTimeout);
5991
5992 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5993 command_type = OTHER_COMMAND;
5994 break;
5995 case CMD_XMIT_ELS_RSP64_CX:
5996 /* words0-2 BDE memcpy */
5997 /* word3 iocb=iotag32 wqe=rsvd */
5998 wqe->words[3] = 0;
5999 /* word4 iocb=did wge=rsvd. */
6000 wqe->words[4] = 0;
6001 /* word5 iocb=rsvd wge=did */
6002 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6003 iocbq->iocb.un.elsreq64.remoteID);
6004
6005 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6006 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6007
6008 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6009 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6010 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6011 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6012 iocbq->vport->vpi + phba->vpi_base);
6013 command_type = OTHER_COMMAND;
6014 break;
6015 case CMD_CLOSE_XRI_CN:
6016 case CMD_ABORT_XRI_CN:
6017 case CMD_ABORT_XRI_CX:
6018 /* words 0-2 memcpy should be 0 rserved */
6019 /* port will send abts */
6020 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6021 /*
6022 * The link is down so the fw does not need to send abts
6023 * on the wire.
6024 */
6025 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6026 else
6027 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6028 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006029 wqe->words[5] = 0;
6030 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6031 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6032 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006033 /*
6034 * The abort handler will send us CMD_ABORT_XRI_CN or
6035 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6036 */
6037 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6038 cmnd = CMD_ABORT_XRI_CX;
6039 command_type = OTHER_COMMAND;
6040 xritag = 0;
6041 break;
James Smart6669f9b2009-10-02 15:16:45 -04006042 case CMD_XMIT_BLS_RSP64_CX:
6043 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6044 * we re-construct this WQE here based on information in
6045 * iocbq from scratch.
6046 */
6047 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006048 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006049 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006050 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6051 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6052 LPFC_ABTS_UNSOL_INT) {
6053 /* ABTS sent by initiator to CT exchange, the
6054 * RX_ID field will be filled with the newly
6055 * allocated responder XRI.
6056 */
6057 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6058 iocbq->sli4_xritag);
6059 } else {
6060 /* ABTS sent by responder to CT exchange, the
6061 * RX_ID field will be filled with the responder
6062 * RX_ID from ABTS.
6063 */
6064 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6065 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6066 }
James Smart6669f9b2009-10-02 15:16:45 -04006067 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6068 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6069 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6070 iocbq->iocb.ulpContext);
6071 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6072 command_type = OTHER_COMMAND;
6073 break;
James Smart4f774512009-05-22 14:52:35 -04006074 case CMD_XRI_ABORTED_CX:
6075 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6076 /* words0-2 are all 0's no bde */
6077 /* word3 and word4 are rsvrd */
6078 wqe->words[3] = 0;
6079 wqe->words[4] = 0;
6080 /* word5 iocb=rsvd wge=did */
6081 /* There is no remote port id in the IOCB? */
6082 /* Let this fall through and fail */
6083 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6084 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6085 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6086 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6087 default:
6088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6089 "2014 Invalid command 0x%x\n",
6090 iocbq->iocb.ulpCommand);
6091 return IOCB_ERROR;
6092 break;
6093
6094 }
6095 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6096 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6097 wqe->generic.abort_tag = abort_tag;
6098 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6099 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6100 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6101 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6102
6103 return 0;
6104}
6105
6106/**
6107 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6108 * @phba: Pointer to HBA context object.
6109 * @ring_number: SLI ring number to issue iocb on.
6110 * @piocb: Pointer to command iocb.
6111 * @flag: Flag indicating if this command can be put into txq.
6112 *
6113 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6114 * an iocb command to an HBA with SLI-4 interface spec.
6115 *
6116 * This function is called with hbalock held. The function will return success
6117 * after it successfully submit the iocb to firmware or after adding to the
6118 * txq.
6119 **/
6120static int
6121__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6122 struct lpfc_iocbq *piocb, uint32_t flag)
6123{
6124 struct lpfc_sglq *sglq;
6125 uint16_t xritag;
6126 union lpfc_wqe wqe;
6127 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006128
6129 if (piocb->sli4_xritag == NO_XRI) {
6130 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006131 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006132 sglq = NULL;
6133 else {
6134 sglq = __lpfc_sli_get_sglq(phba);
6135 if (!sglq)
6136 return IOCB_ERROR;
6137 piocb->sli4_xritag = sglq->sli4_xritag;
6138 }
6139 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6140 sglq = NULL; /* These IO's already have an XRI and
6141 * a mapped sgl.
6142 */
6143 } else {
6144 /* This is a continuation of a commandi,(CX) so this
6145 * sglq is on the active list
6146 */
6147 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6148 if (!sglq)
6149 return IOCB_ERROR;
6150 }
6151
6152 if (sglq) {
6153 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6154 if (xritag != sglq->sli4_xritag)
6155 return IOCB_ERROR;
6156 }
6157
6158 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6159 return IOCB_ERROR;
6160
James Smart341af102010-01-26 23:07:37 -05006161 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6162 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006163 /*
6164 * For FCP command IOCB, get a new WQ index to distribute
6165 * WQE across the WQsr. On the other hand, for abort IOCB,
6166 * it carries the same WQ index to the original command
6167 * IOCB.
6168 */
James Smart341af102010-01-26 23:07:37 -05006169 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006170 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6171 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6172 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006173 return IOCB_ERROR;
6174 } else {
6175 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6176 return IOCB_ERROR;
6177 }
6178 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6179
6180 return 0;
6181}
6182
6183/**
James Smart3772a992009-05-22 14:50:54 -04006184 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6185 *
6186 * This routine wraps the actual lockless version for issusing IOCB function
6187 * pointer from the lpfc_hba struct.
6188 *
6189 * Return codes:
6190 * IOCB_ERROR - Error
6191 * IOCB_SUCCESS - Success
6192 * IOCB_BUSY - Busy
6193 **/
6194static inline int
6195__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6196 struct lpfc_iocbq *piocb, uint32_t flag)
6197{
6198 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6199}
6200
6201/**
6202 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6203 * @phba: The hba struct for which this call is being executed.
6204 * @dev_grp: The HBA PCI-Device group number.
6205 *
6206 * This routine sets up the SLI interface API function jump table in @phba
6207 * struct.
6208 * Returns: 0 - success, -ENODEV - failure.
6209 **/
6210int
6211lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6212{
6213
6214 switch (dev_grp) {
6215 case LPFC_PCI_DEV_LP:
6216 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6217 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6218 break;
James Smart4f774512009-05-22 14:52:35 -04006219 case LPFC_PCI_DEV_OC:
6220 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6221 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6222 break;
James Smart3772a992009-05-22 14:50:54 -04006223 default:
6224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6225 "1419 Invalid HBA PCI-device group: 0x%x\n",
6226 dev_grp);
6227 return -ENODEV;
6228 break;
6229 }
6230 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6231 return 0;
6232}
James Smart92d7f7b2007-06-17 19:56:38 -05006233
James Smarte59058c2008-08-24 21:49:00 -04006234/**
James Smart3621a712009-04-06 18:47:14 -04006235 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006236 * @phba: Pointer to HBA context object.
6237 * @pring: Pointer to driver SLI ring object.
6238 * @piocb: Pointer to command iocb.
6239 * @flag: Flag indicating if this command can be put into txq.
6240 *
6241 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6242 * function. This function gets the hbalock and calls
6243 * __lpfc_sli_issue_iocb function and will return the error returned
6244 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6245 * functions which do not hold hbalock.
6246 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006247int
James Smart3772a992009-05-22 14:50:54 -04006248lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006249 struct lpfc_iocbq *piocb, uint32_t flag)
6250{
6251 unsigned long iflags;
6252 int rc;
6253
6254 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006255 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006256 spin_unlock_irqrestore(&phba->hbalock, iflags);
6257
6258 return rc;
6259}
6260
James Smarte59058c2008-08-24 21:49:00 -04006261/**
James Smart3621a712009-04-06 18:47:14 -04006262 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006263 * @phba: Pointer to HBA context object.
6264 *
6265 * This function is called while driver attaches with the
6266 * HBA to setup the extra ring. The extra ring is used
6267 * only when driver needs to support target mode functionality
6268 * or IP over FC functionalities.
6269 *
6270 * This function is called with no lock held.
6271 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006272static int
6273lpfc_extra_ring_setup( struct lpfc_hba *phba)
6274{
6275 struct lpfc_sli *psli;
6276 struct lpfc_sli_ring *pring;
6277
6278 psli = &phba->sli;
6279
6280 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006281
6282 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006283 pring = &psli->ring[psli->fcp_ring];
6284 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6285 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6286 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6287 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6288
James Smarta4bc3372006-12-02 13:34:16 -05006289 /* and give them to the extra ring */
6290 pring = &psli->ring[psli->extra_ring];
6291
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006292 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6293 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6294 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6295 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6296
6297 /* Setup default profile for this ring */
6298 pring->iotag_max = 4096;
6299 pring->num_mask = 1;
6300 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006301 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6302 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006303 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6304 return 0;
6305}
6306
James Smarte59058c2008-08-24 21:49:00 -04006307/**
James Smart3621a712009-04-06 18:47:14 -04006308 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006309 * @phba: Pointer to HBA context object.
6310 * @pring: Pointer to driver SLI ring object.
6311 * @iocbq: Pointer to iocb object.
6312 *
6313 * This function is called by the slow ring event handler
6314 * function when there is an ASYNC event iocb in the ring.
6315 * This function is called with no lock held.
6316 * Currently this function handles only temperature related
6317 * ASYNC events. The function decodes the temperature sensor
6318 * event message and posts events for the management applications.
6319 **/
James Smart98c9ea52007-10-27 13:37:33 -04006320static void
James Smart57127f12007-10-27 13:37:05 -04006321lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6322 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6323{
6324 IOCB_t *icmd;
6325 uint16_t evt_code;
6326 uint16_t temp;
6327 struct temp_event temp_event_data;
6328 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006329 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006330
6331 icmd = &iocbq->iocb;
6332 evt_code = icmd->un.asyncstat.evt_code;
6333 temp = icmd->ulpContext;
6334
6335 if ((evt_code != ASYNC_TEMP_WARN) &&
6336 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006337 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006338 lpfc_printf_log(phba,
6339 KERN_ERR,
6340 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006341 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006342 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006343 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6344 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6345 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6346 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006347 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006348 icmd->un.asyncstat.evt_code,
6349 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6350 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6351 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6352 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6353
James Smart57127f12007-10-27 13:37:05 -04006354 return;
6355 }
6356 temp_event_data.data = (uint32_t)temp;
6357 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6358 if (evt_code == ASYNC_TEMP_WARN) {
6359 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6360 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006361 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006362 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006363 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006364 "corrective action. temperature : %d Celsius\n",
6365 temp);
6366 }
6367 if (evt_code == ASYNC_TEMP_SAFE) {
6368 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6369 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006370 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006371 LOG_TEMP,
6372 "0340 Adapter temperature is OK now. "
6373 "temperature : %d Celsius\n",
6374 temp);
6375 }
6376
6377 /* Send temperature change event to applications */
6378 shost = lpfc_shost_from_vport(phba->pport);
6379 fc_host_post_vendor_event(shost, fc_get_event_number(),
6380 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006381 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006382
6383}
6384
6385
James Smarte59058c2008-08-24 21:49:00 -04006386/**
James Smart3621a712009-04-06 18:47:14 -04006387 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006388 * @phba: Pointer to HBA context object.
6389 *
6390 * lpfc_sli_setup sets up rings of the SLI interface with
6391 * number of iocbs per ring and iotags. This function is
6392 * called while driver attach to the HBA and before the
6393 * interrupts are enabled. So there is no need for locking.
6394 *
6395 * This function always returns 0.
6396 **/
dea31012005-04-17 16:05:31 -05006397int
6398lpfc_sli_setup(struct lpfc_hba *phba)
6399{
James Smarted957682007-06-17 19:56:37 -05006400 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006401 struct lpfc_sli *psli = &phba->sli;
6402 struct lpfc_sli_ring *pring;
6403
6404 psli->num_rings = MAX_CONFIGURED_RINGS;
6405 psli->sli_flag = 0;
6406 psli->fcp_ring = LPFC_FCP_RING;
6407 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006408 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006409
James Bottomley604a3e32005-10-29 10:28:33 -05006410 psli->iocbq_lookup = NULL;
6411 psli->iocbq_lookup_len = 0;
6412 psli->last_iotag = 0;
6413
dea31012005-04-17 16:05:31 -05006414 for (i = 0; i < psli->num_rings; i++) {
6415 pring = &psli->ring[i];
6416 switch (i) {
6417 case LPFC_FCP_RING: /* ring 0 - FCP */
6418 /* numCiocb and numRiocb are used in config_port */
6419 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6420 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6421 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6422 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6423 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6424 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006425 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006426 SLI3_IOCB_CMD_SIZE :
6427 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006428 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006429 SLI3_IOCB_RSP_SIZE :
6430 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006431 pring->iotag_ctr = 0;
6432 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006433 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006434 pring->fast_iotag = pring->iotag_max;
6435 pring->num_mask = 0;
6436 break;
James Smarta4bc3372006-12-02 13:34:16 -05006437 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006438 /* numCiocb and numRiocb are used in config_port */
6439 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6440 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006441 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006442 SLI3_IOCB_CMD_SIZE :
6443 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006444 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006445 SLI3_IOCB_RSP_SIZE :
6446 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006447 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006448 pring->num_mask = 0;
6449 break;
6450 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6451 /* numCiocb and numRiocb are used in config_port */
6452 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6453 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006454 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006455 SLI3_IOCB_CMD_SIZE :
6456 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006457 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006458 SLI3_IOCB_RSP_SIZE :
6459 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006460 pring->fast_iotag = 0;
6461 pring->iotag_ctr = 0;
6462 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006463 pring->lpfc_sli_rcv_async_status =
6464 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006465 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006466 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006467 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6468 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006469 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006470 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006471 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006472 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6473 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006474 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006475 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006476 pring->prt[2].profile = 0; /* Mask 2 */
6477 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006478 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006479 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006480 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006481 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006482 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006483 pring->prt[3].profile = 0; /* Mask 3 */
6484 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006485 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006486 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006487 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006488 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006489 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006490 /* abort unsolicited sequence */
6491 pring->prt[4].profile = 0; /* Mask 4 */
6492 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6493 pring->prt[4].type = FC_TYPE_BLS;
6494 pring->prt[4].lpfc_sli_rcv_unsol_event =
6495 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006496 break;
6497 }
James Smarted957682007-06-17 19:56:37 -05006498 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006499 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006500 }
James Smarted957682007-06-17 19:56:37 -05006501 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006502 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006503 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6504 "SLI2 SLIM Data: x%x x%lx\n",
6505 phba->brd_no, totiocbsize,
6506 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006507 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006508 if (phba->cfg_multi_ring_support == 2)
6509 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006510
6511 return 0;
6512}
6513
James Smarte59058c2008-08-24 21:49:00 -04006514/**
James Smart3621a712009-04-06 18:47:14 -04006515 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006516 * @phba: Pointer to HBA context object.
6517 *
6518 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6519 * ring. This function also initializes ring indices of each ring.
6520 * This function is called during the initialization of the SLI
6521 * interface of an HBA.
6522 * This function is called with no lock held and always returns
6523 * 1.
6524 **/
dea31012005-04-17 16:05:31 -05006525int
James Smart2e0fef82007-06-17 19:56:36 -05006526lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006527{
6528 struct lpfc_sli *psli;
6529 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006530 int i;
dea31012005-04-17 16:05:31 -05006531
6532 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006533 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006534 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006535 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006536 /* Initialize list headers for txq and txcmplq as double linked lists */
6537 for (i = 0; i < psli->num_rings; i++) {
6538 pring = &psli->ring[i];
6539 pring->ringno = i;
6540 pring->next_cmdidx = 0;
6541 pring->local_getidx = 0;
6542 pring->cmdidx = 0;
6543 INIT_LIST_HEAD(&pring->txq);
6544 INIT_LIST_HEAD(&pring->txcmplq);
6545 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006546 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006547 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006548 }
James Smart2e0fef82007-06-17 19:56:36 -05006549 spin_unlock_irq(&phba->hbalock);
6550 return 1;
dea31012005-04-17 16:05:31 -05006551}
6552
James Smarte59058c2008-08-24 21:49:00 -04006553/**
James Smart04c68492009-05-22 14:52:52 -04006554 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6555 * @phba: Pointer to HBA context object.
6556 *
6557 * This routine flushes the mailbox command subsystem. It will unconditionally
6558 * flush all the mailbox commands in the three possible stages in the mailbox
6559 * command sub-system: pending mailbox command queue; the outstanding mailbox
6560 * command; and completed mailbox command queue. It is caller's responsibility
6561 * to make sure that the driver is in the proper state to flush the mailbox
6562 * command sub-system. Namely, the posting of mailbox commands into the
6563 * pending mailbox command queue from the various clients must be stopped;
6564 * either the HBA is in a state that it will never works on the outstanding
6565 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6566 * mailbox command has been completed.
6567 **/
6568static void
6569lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6570{
6571 LIST_HEAD(completions);
6572 struct lpfc_sli *psli = &phba->sli;
6573 LPFC_MBOXQ_t *pmb;
6574 unsigned long iflag;
6575
6576 /* Flush all the mailbox commands in the mbox system */
6577 spin_lock_irqsave(&phba->hbalock, iflag);
6578 /* The pending mailbox command queue */
6579 list_splice_init(&phba->sli.mboxq, &completions);
6580 /* The outstanding active mailbox command */
6581 if (psli->mbox_active) {
6582 list_add_tail(&psli->mbox_active->list, &completions);
6583 psli->mbox_active = NULL;
6584 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6585 }
6586 /* The completed mailbox command queue */
6587 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6588 spin_unlock_irqrestore(&phba->hbalock, iflag);
6589
6590 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6591 while (!list_empty(&completions)) {
6592 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6593 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6594 if (pmb->mbox_cmpl)
6595 pmb->mbox_cmpl(phba, pmb);
6596 }
6597}
6598
6599/**
James Smart3621a712009-04-06 18:47:14 -04006600 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006601 * @vport: Pointer to virtual port object.
6602 *
6603 * lpfc_sli_host_down is called to clean up the resources
6604 * associated with a vport before destroying virtual
6605 * port data structures.
6606 * This function does following operations:
6607 * - Free discovery resources associated with this virtual
6608 * port.
6609 * - Free iocbs associated with this virtual port in
6610 * the txq.
6611 * - Send abort for all iocb commands associated with this
6612 * vport in txcmplq.
6613 *
6614 * This function is called with no lock held and always returns 1.
6615 **/
dea31012005-04-17 16:05:31 -05006616int
James Smart92d7f7b2007-06-17 19:56:38 -05006617lpfc_sli_host_down(struct lpfc_vport *vport)
6618{
James Smart858c9f62007-06-17 19:56:39 -05006619 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006620 struct lpfc_hba *phba = vport->phba;
6621 struct lpfc_sli *psli = &phba->sli;
6622 struct lpfc_sli_ring *pring;
6623 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006624 int i;
6625 unsigned long flags = 0;
6626 uint16_t prev_pring_flag;
6627
6628 lpfc_cleanup_discovery_resources(vport);
6629
6630 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006631 for (i = 0; i < psli->num_rings; i++) {
6632 pring = &psli->ring[i];
6633 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006634 /* Only slow rings */
6635 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006636 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006637 /* Set the lpfc data pending flag */
6638 set_bit(LPFC_DATA_READY, &phba->data_flags);
6639 }
James Smart92d7f7b2007-06-17 19:56:38 -05006640 /*
6641 * Error everything on the txq since these iocbs have not been
6642 * given to the FW yet.
6643 */
James Smart92d7f7b2007-06-17 19:56:38 -05006644 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6645 if (iocb->vport != vport)
6646 continue;
James Smart858c9f62007-06-17 19:56:39 -05006647 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006648 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006649 }
6650
6651 /* Next issue ABTS for everything on the txcmplq */
6652 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6653 list) {
6654 if (iocb->vport != vport)
6655 continue;
6656 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6657 }
6658
6659 pring->flag = prev_pring_flag;
6660 }
6661
6662 spin_unlock_irqrestore(&phba->hbalock, flags);
6663
James Smarta257bf92009-04-06 18:48:10 -04006664 /* Cancel all the IOCBs from the completions list */
6665 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6666 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006667 return 1;
6668}
6669
James Smarte59058c2008-08-24 21:49:00 -04006670/**
James Smart3621a712009-04-06 18:47:14 -04006671 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006672 * @phba: Pointer to HBA context object.
6673 *
6674 * This function cleans up all iocb, buffers, mailbox commands
6675 * while shutting down the HBA. This function is called with no
6676 * lock held and always returns 1.
6677 * This function does the following to cleanup driver resources:
6678 * - Free discovery resources for each virtual port
6679 * - Cleanup any pending fabric iocbs
6680 * - Iterate through the iocb txq and free each entry
6681 * in the list.
6682 * - Free up any buffer posted to the HBA
6683 * - Free mailbox commands in the mailbox queue.
6684 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006685int
James Smart2e0fef82007-06-17 19:56:36 -05006686lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006687{
James Smart2534ba72007-04-25 09:52:20 -04006688 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006689 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006690 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006691 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006692 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006693 int i;
6694
6695 /* Shutdown the mailbox command sub-system */
6696 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006697
dea31012005-04-17 16:05:31 -05006698 lpfc_hba_down_prep(phba);
6699
James Smart92d7f7b2007-06-17 19:56:38 -05006700 lpfc_fabric_abort_hba(phba);
6701
James Smart2e0fef82007-06-17 19:56:36 -05006702 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006703 for (i = 0; i < psli->num_rings; i++) {
6704 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006705 /* Only slow rings */
6706 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006707 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006708 /* Set the lpfc data pending flag */
6709 set_bit(LPFC_DATA_READY, &phba->data_flags);
6710 }
dea31012005-04-17 16:05:31 -05006711
6712 /*
6713 * Error everything on the txq since these iocbs have not been
6714 * given to the FW yet.
6715 */
James Smart2534ba72007-04-25 09:52:20 -04006716 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006717 pring->txq_cnt = 0;
6718
dea31012005-04-17 16:05:31 -05006719 }
James Smart2e0fef82007-06-17 19:56:36 -05006720 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006721
James Smarta257bf92009-04-06 18:48:10 -04006722 /* Cancel all the IOCBs from the completions list */
6723 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6724 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006725
James Smart0ff10d42008-01-11 01:52:36 -05006726 spin_lock_irqsave(&phba->hbalock, flags);
6727 list_splice_init(&phba->elsbuf, &completions);
6728 phba->elsbuf_cnt = 0;
6729 phba->elsbuf_prev_cnt = 0;
6730 spin_unlock_irqrestore(&phba->hbalock, flags);
6731
6732 while (!list_empty(&completions)) {
6733 list_remove_head(&completions, buf_ptr,
6734 struct lpfc_dmabuf, list);
6735 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6736 kfree(buf_ptr);
6737 }
6738
dea31012005-04-17 16:05:31 -05006739 /* Return any active mbox cmds */
6740 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006741
James Smartda0436e2009-05-22 14:51:39 -04006742 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006743 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006744 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006745
James Smartda0436e2009-05-22 14:51:39 -04006746 return 1;
6747}
James Smart92d7f7b2007-06-17 19:56:38 -05006748
James Smartda0436e2009-05-22 14:51:39 -04006749/**
6750 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6751 * @phba: Pointer to HBA context object.
6752 *
6753 * This function cleans up all queues, iocb, buffers, mailbox commands while
6754 * shutting down the SLI4 HBA FCoE function. This function is called with no
6755 * lock held and always returns 1.
6756 *
6757 * This function does the following to cleanup driver FCoE function resources:
6758 * - Free discovery resources for each virtual port
6759 * - Cleanup any pending fabric iocbs
6760 * - Iterate through the iocb txq and free each entry in the list.
6761 * - Free up any buffer posted to the HBA.
6762 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6763 * - Free mailbox commands in the mailbox queue.
6764 **/
6765int
6766lpfc_sli4_hba_down(struct lpfc_hba *phba)
6767{
6768 /* Stop the SLI4 device port */
6769 lpfc_stop_port(phba);
6770
6771 /* Tear down the queues in the HBA */
6772 lpfc_sli4_queue_unset(phba);
6773
6774 /* unregister default FCFI from the HBA */
6775 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6776
dea31012005-04-17 16:05:31 -05006777 return 1;
6778}
6779
James Smarte59058c2008-08-24 21:49:00 -04006780/**
James Smart3621a712009-04-06 18:47:14 -04006781 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006782 * @srcp: Source memory pointer.
6783 * @destp: Destination memory pointer.
6784 * @cnt: Number of words required to be copied.
6785 *
6786 * This function is used for copying data between driver memory
6787 * and the SLI memory. This function also changes the endianness
6788 * of each word if native endianness is different from SLI
6789 * endianness. This function can be called with or without
6790 * lock.
6791 **/
dea31012005-04-17 16:05:31 -05006792void
6793lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6794{
6795 uint32_t *src = srcp;
6796 uint32_t *dest = destp;
6797 uint32_t ldata;
6798 int i;
6799
6800 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6801 ldata = *src;
6802 ldata = le32_to_cpu(ldata);
6803 *dest = ldata;
6804 src++;
6805 dest++;
6806 }
6807}
6808
James Smarte59058c2008-08-24 21:49:00 -04006809
6810/**
James Smarta0c87cb2009-07-19 10:01:10 -04006811 * lpfc_sli_bemem_bcopy - SLI memory copy function
6812 * @srcp: Source memory pointer.
6813 * @destp: Destination memory pointer.
6814 * @cnt: Number of words required to be copied.
6815 *
6816 * This function is used for copying data between a data structure
6817 * with big endian representation to local endianness.
6818 * This function can be called with or without lock.
6819 **/
6820void
6821lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6822{
6823 uint32_t *src = srcp;
6824 uint32_t *dest = destp;
6825 uint32_t ldata;
6826 int i;
6827
6828 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6829 ldata = *src;
6830 ldata = be32_to_cpu(ldata);
6831 *dest = ldata;
6832 src++;
6833 dest++;
6834 }
6835}
6836
6837/**
James Smart3621a712009-04-06 18:47:14 -04006838 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006839 * @phba: Pointer to HBA context object.
6840 * @pring: Pointer to driver SLI ring object.
6841 * @mp: Pointer to driver buffer object.
6842 *
6843 * This function is called with no lock held.
6844 * It always return zero after adding the buffer to the postbufq
6845 * buffer list.
6846 **/
dea31012005-04-17 16:05:31 -05006847int
James Smart2e0fef82007-06-17 19:56:36 -05006848lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6849 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006850{
6851 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6852 later */
James Smart2e0fef82007-06-17 19:56:36 -05006853 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006854 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006855 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006856 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006857 return 0;
6858}
6859
James Smarte59058c2008-08-24 21:49:00 -04006860/**
James Smart3621a712009-04-06 18:47:14 -04006861 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006862 * @phba: Pointer to HBA context object.
6863 *
6864 * When HBQ is enabled, buffers are searched based on tags. This function
6865 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6866 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6867 * does not conflict with tags of buffer posted for unsolicited events.
6868 * The function returns the allocated tag. The function is called with
6869 * no locks held.
6870 **/
James Smart76bb24e2007-10-27 13:38:00 -04006871uint32_t
6872lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6873{
6874 spin_lock_irq(&phba->hbalock);
6875 phba->buffer_tag_count++;
6876 /*
6877 * Always set the QUE_BUFTAG_BIT to distiguish between
6878 * a tag assigned by HBQ.
6879 */
6880 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6881 spin_unlock_irq(&phba->hbalock);
6882 return phba->buffer_tag_count;
6883}
6884
James Smarte59058c2008-08-24 21:49:00 -04006885/**
James Smart3621a712009-04-06 18:47:14 -04006886 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006887 * @phba: Pointer to HBA context object.
6888 * @pring: Pointer to driver SLI ring object.
6889 * @tag: Buffer tag.
6890 *
6891 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6892 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6893 * iocb is posted to the response ring with the tag of the buffer.
6894 * This function searches the pring->postbufq list using the tag
6895 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6896 * iocb. If the buffer is found then lpfc_dmabuf object of the
6897 * buffer is returned to the caller else NULL is returned.
6898 * This function is called with no lock held.
6899 **/
James Smart76bb24e2007-10-27 13:38:00 -04006900struct lpfc_dmabuf *
6901lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6902 uint32_t tag)
6903{
6904 struct lpfc_dmabuf *mp, *next_mp;
6905 struct list_head *slp = &pring->postbufq;
6906
6907 /* Search postbufq, from the begining, looking for a match on tag */
6908 spin_lock_irq(&phba->hbalock);
6909 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6910 if (mp->buffer_tag == tag) {
6911 list_del_init(&mp->list);
6912 pring->postbufq_cnt--;
6913 spin_unlock_irq(&phba->hbalock);
6914 return mp;
6915 }
6916 }
6917
6918 spin_unlock_irq(&phba->hbalock);
6919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006920 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006921 "ring %d Data x%lx x%p x%p x%x\n",
6922 pring->ringno, (unsigned long) tag,
6923 slp->next, slp->prev, pring->postbufq_cnt);
6924
6925 return NULL;
6926}
dea31012005-04-17 16:05:31 -05006927
James Smarte59058c2008-08-24 21:49:00 -04006928/**
James Smart3621a712009-04-06 18:47:14 -04006929 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006930 * @phba: Pointer to HBA context object.
6931 * @pring: Pointer to driver SLI ring object.
6932 * @phys: DMA address of the buffer.
6933 *
6934 * This function searches the buffer list using the dma_address
6935 * of unsolicited event to find the driver's lpfc_dmabuf object
6936 * corresponding to the dma_address. The function returns the
6937 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6938 * This function is called by the ct and els unsolicited event
6939 * handlers to get the buffer associated with the unsolicited
6940 * event.
6941 *
6942 * This function is called with no lock held.
6943 **/
dea31012005-04-17 16:05:31 -05006944struct lpfc_dmabuf *
6945lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6946 dma_addr_t phys)
6947{
6948 struct lpfc_dmabuf *mp, *next_mp;
6949 struct list_head *slp = &pring->postbufq;
6950
6951 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006952 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006953 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6954 if (mp->phys == phys) {
6955 list_del_init(&mp->list);
6956 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006957 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006958 return mp;
6959 }
6960 }
6961
James Smart2e0fef82007-06-17 19:56:36 -05006962 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006964 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006965 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006966 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006967 slp->next, slp->prev, pring->postbufq_cnt);
6968 return NULL;
6969}
6970
James Smarte59058c2008-08-24 21:49:00 -04006971/**
James Smart3621a712009-04-06 18:47:14 -04006972 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006973 * @phba: Pointer to HBA context object.
6974 * @cmdiocb: Pointer to driver command iocb object.
6975 * @rspiocb: Pointer to driver response iocb object.
6976 *
6977 * This function is the completion handler for the abort iocbs for
6978 * ELS commands. This function is called from the ELS ring event
6979 * handler with no lock held. This function frees memory resources
6980 * associated with the abort iocb.
6981 **/
dea31012005-04-17 16:05:31 -05006982static void
James Smart2e0fef82007-06-17 19:56:36 -05006983lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6984 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05006985{
James Smart2e0fef82007-06-17 19:56:36 -05006986 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04006987 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05006988 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04006989 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6990
6991 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04006992
6993 if (irsp->ulpStatus) {
6994 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6995 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6996
James Smart2e0fef82007-06-17 19:56:36 -05006997 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04006998 if (phba->sli_rev < LPFC_SLI_REV4) {
6999 if (abort_iotag != 0 &&
7000 abort_iotag <= phba->sli.last_iotag)
7001 abort_iocb =
7002 phba->sli.iocbq_lookup[abort_iotag];
7003 } else
7004 /* For sli4 the abort_tag is the XRI,
7005 * so the abort routine puts the iotag of the iocb
7006 * being aborted in the context field of the abort
7007 * IOCB.
7008 */
7009 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007010
James Smart92d7f7b2007-06-17 19:56:38 -05007011 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007012 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007013 "with tag %x context %x, abort status %x, "
7014 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007015 abort_iocb, abort_iotag, abort_context,
7016 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007017
7018 /*
James Smart58da1ff2008-04-07 10:15:56 -04007019 * If the iocb is not found in Firmware queue the iocb
7020 * might have completed already. Do not free it again.
7021 */
James Smart9b379602008-04-07 10:16:00 -04007022 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007023 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7024 spin_unlock_irq(&phba->hbalock);
7025 lpfc_sli_release_iocbq(phba, cmdiocb);
7026 return;
7027 }
7028 /* For SLI4 the ulpContext field for abort IOCB
7029 * holds the iotag of the IOCB being aborted so
7030 * the local abort_context needs to be reset to
7031 * match the aborted IOCBs ulpContext.
7032 */
7033 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7034 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007035 }
7036 /*
James Smart2680eea2007-04-25 09:52:55 -04007037 * make sure we have the right iocbq before taking it
7038 * off the txcmplq and try to call completion routine.
7039 */
James Smart2e0fef82007-06-17 19:56:36 -05007040 if (!abort_iocb ||
7041 abort_iocb->iocb.ulpContext != abort_context ||
7042 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7043 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007044 else if (phba->sli_rev < LPFC_SLI_REV4) {
7045 /*
7046 * leave the SLI4 aborted command on the txcmplq
7047 * list and the command complete WCQE's XB bit
7048 * will tell whether the SGL (XRI) can be released
7049 * immediately or to the aborted SGL list for the
7050 * following abort XRI from the HBA.
7051 */
James Smart92d7f7b2007-06-17 19:56:38 -05007052 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007053 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007054 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007055
James Smart0ff10d42008-01-11 01:52:36 -05007056 /* Firmware could still be in progress of DMAing
7057 * payload, so don't free data buffer till after
7058 * a hbeat.
7059 */
James Smart341af102010-01-26 23:07:37 -05007060 spin_lock_irq(&phba->hbalock);
James Smart0ff10d42008-01-11 01:52:36 -05007061 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007062 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007063 spin_unlock_irq(&phba->hbalock);
7064
James Smart92d7f7b2007-06-17 19:56:38 -05007065 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007066 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007067 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007068 }
7069 }
7070
James Bottomley604a3e32005-10-29 10:28:33 -05007071 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007072 return;
7073}
7074
James Smarte59058c2008-08-24 21:49:00 -04007075/**
James Smart3621a712009-04-06 18:47:14 -04007076 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007077 * @phba: Pointer to HBA context object.
7078 * @cmdiocb: Pointer to driver command iocb object.
7079 * @rspiocb: Pointer to driver response iocb object.
7080 *
7081 * The function is called from SLI ring event handler with no
7082 * lock held. This function is the completion handler for ELS commands
7083 * which are aborted. The function frees memory resources used for
7084 * the aborted ELS commands.
7085 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007086static void
7087lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7088 struct lpfc_iocbq *rspiocb)
7089{
7090 IOCB_t *irsp = &rspiocb->iocb;
7091
7092 /* ELS cmd tag <ulpIoTag> completes */
7093 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007094 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007095 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007096 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007097 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007098 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7099 lpfc_ct_free_iocb(phba, cmdiocb);
7100 else
7101 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007102 return;
7103}
7104
James Smarte59058c2008-08-24 21:49:00 -04007105/**
James Smart3621a712009-04-06 18:47:14 -04007106 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007107 * @phba: Pointer to HBA context object.
7108 * @pring: Pointer to driver SLI ring object.
7109 * @cmdiocb: Pointer to driver command iocb object.
7110 *
7111 * This function issues an abort iocb for the provided command
7112 * iocb. This function is called with hbalock held.
7113 * The function returns 0 when it fails due to memory allocation
7114 * failure or when the command iocb is an abort request.
7115 **/
dea31012005-04-17 16:05:31 -05007116int
James Smart2e0fef82007-06-17 19:56:36 -05007117lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7118 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007119{
James Smart2e0fef82007-06-17 19:56:36 -05007120 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007121 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007122 IOCB_t *icmd = NULL;
7123 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007124 int retval = IOCB_ERROR;
7125
James Smart92d7f7b2007-06-17 19:56:38 -05007126 /*
7127 * There are certain command types we don't want to abort. And we
7128 * don't want to abort commands that are already in the process of
7129 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007130 */
7131 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007132 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007133 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7134 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007135 return 0;
7136
James Smart858c9f62007-06-17 19:56:39 -05007137 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7138 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007139 */
James Smart858c9f62007-06-17 19:56:39 -05007140 if ((vport->load_flag & FC_UNLOADING) &&
7141 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007142 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7143 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7144 else
7145 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007146 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007147 }
dea31012005-04-17 16:05:31 -05007148
7149 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007150 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007151 if (abtsiocbp == NULL)
7152 return 0;
dea31012005-04-17 16:05:31 -05007153
James Smart07951072007-04-25 09:51:38 -04007154 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007155 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007156 */
7157 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7158
dea31012005-04-17 16:05:31 -05007159 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007160 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7161 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007162 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007163 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007164 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7165 }
James Smartda0436e2009-05-22 14:51:39 -04007166 else
7167 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007168 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007169 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007170
James Smart5ffc2662009-11-18 15:39:44 -05007171 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7172 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007173 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7174 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007175
James Smart2e0fef82007-06-17 19:56:36 -05007176 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007177 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7178 else
7179 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7180
7181 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007182
James Smarte8b62012007-08-02 11:10:09 -04007183 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7184 "0339 Abort xri x%x, original iotag x%x, "
7185 "abort cmd iotag x%x\n",
7186 iabt->un.acxri.abortContextTag,
7187 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007188 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007189
James Smartd7c255b2008-08-24 21:50:00 -04007190 if (retval)
7191 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007192abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007193 /*
7194 * Caller to this routine should check for IOCB_ERROR
7195 * and handle it properly. This routine no longer removes
7196 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007197 */
James Smart2e0fef82007-06-17 19:56:36 -05007198 return retval;
dea31012005-04-17 16:05:31 -05007199}
7200
James Smarte59058c2008-08-24 21:49:00 -04007201/**
James Smart3621a712009-04-06 18:47:14 -04007202 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007203 * @iocbq: Pointer to driver iocb object.
7204 * @vport: Pointer to driver virtual port object.
7205 * @tgt_id: SCSI ID of the target.
7206 * @lun_id: LUN ID of the scsi device.
7207 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7208 *
James Smart3621a712009-04-06 18:47:14 -04007209 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007210 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7211 * 0 if the filtering criteria is met for the given iocb and will return
7212 * 1 if the filtering criteria is not met.
7213 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7214 * given iocb is for the SCSI device specified by vport, tgt_id and
7215 * lun_id parameter.
7216 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7217 * given iocb is for the SCSI target specified by vport and tgt_id
7218 * parameters.
7219 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7220 * given iocb is for the SCSI host associated with the given vport.
7221 * This function is called with no locks held.
7222 **/
dea31012005-04-17 16:05:31 -05007223static int
James Smart51ef4c22007-08-02 11:10:31 -04007224lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7225 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007226 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007227{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007228 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007229 int rc = 1;
7230
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007231 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7232 return rc;
7233
James Smart51ef4c22007-08-02 11:10:31 -04007234 if (iocbq->vport != vport)
7235 return rc;
7236
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007237 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007238
James Smart495a7142008-06-14 22:52:59 -04007239 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007240 return rc;
7241
7242 switch (ctx_cmd) {
7243 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007244 if ((lpfc_cmd->rdata->pnode) &&
7245 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7246 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007247 rc = 0;
7248 break;
7249 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007250 if ((lpfc_cmd->rdata->pnode) &&
7251 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007252 rc = 0;
7253 break;
dea31012005-04-17 16:05:31 -05007254 case LPFC_CTX_HOST:
7255 rc = 0;
7256 break;
7257 default:
7258 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007259 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007260 break;
7261 }
7262
7263 return rc;
7264}
7265
James Smarte59058c2008-08-24 21:49:00 -04007266/**
James Smart3621a712009-04-06 18:47:14 -04007267 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007268 * @vport: Pointer to virtual port.
7269 * @tgt_id: SCSI ID of the target.
7270 * @lun_id: LUN ID of the scsi device.
7271 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7272 *
7273 * This function returns number of FCP commands pending for the vport.
7274 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7275 * commands pending on the vport associated with SCSI device specified
7276 * by tgt_id and lun_id parameters.
7277 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7278 * commands pending on the vport associated with SCSI target specified
7279 * by tgt_id parameter.
7280 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7281 * commands pending on the vport.
7282 * This function returns the number of iocbs which satisfy the filter.
7283 * This function is called without any lock held.
7284 **/
dea31012005-04-17 16:05:31 -05007285int
James Smart51ef4c22007-08-02 11:10:31 -04007286lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7287 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007288{
James Smart51ef4c22007-08-02 11:10:31 -04007289 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007290 struct lpfc_iocbq *iocbq;
7291 int sum, i;
dea31012005-04-17 16:05:31 -05007292
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007293 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7294 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007295
James Smart51ef4c22007-08-02 11:10:31 -04007296 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7297 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007298 sum++;
dea31012005-04-17 16:05:31 -05007299 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007300
dea31012005-04-17 16:05:31 -05007301 return sum;
7302}
7303
James Smarte59058c2008-08-24 21:49:00 -04007304/**
James Smart3621a712009-04-06 18:47:14 -04007305 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007306 * @phba: Pointer to HBA context object
7307 * @cmdiocb: Pointer to command iocb object.
7308 * @rspiocb: Pointer to response iocb object.
7309 *
7310 * This function is called when an aborted FCP iocb completes. This
7311 * function is called by the ring event handler with no lock held.
7312 * This function frees the iocb.
7313 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007314void
James Smart2e0fef82007-06-17 19:56:36 -05007315lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7316 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007317{
James Bottomley604a3e32005-10-29 10:28:33 -05007318 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007319 return;
7320}
7321
James Smarte59058c2008-08-24 21:49:00 -04007322/**
James Smart3621a712009-04-06 18:47:14 -04007323 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007324 * @vport: Pointer to virtual port.
7325 * @pring: Pointer to driver SLI ring object.
7326 * @tgt_id: SCSI ID of the target.
7327 * @lun_id: LUN ID of the scsi device.
7328 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7329 *
7330 * This function sends an abort command for every SCSI command
7331 * associated with the given virtual port pending on the ring
7332 * filtered by lpfc_sli_validate_fcp_iocb function.
7333 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7334 * FCP iocbs associated with lun specified by tgt_id and lun_id
7335 * parameters
7336 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7337 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7338 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7339 * FCP iocbs associated with virtual port.
7340 * This function returns number of iocbs it failed to abort.
7341 * This function is called with no locks held.
7342 **/
dea31012005-04-17 16:05:31 -05007343int
James Smart51ef4c22007-08-02 11:10:31 -04007344lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7345 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007346{
James Smart51ef4c22007-08-02 11:10:31 -04007347 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007348 struct lpfc_iocbq *iocbq;
7349 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007350 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007351 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007352 int i;
dea31012005-04-17 16:05:31 -05007353
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007354 for (i = 1; i <= phba->sli.last_iotag; i++) {
7355 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007356
James Smart51ef4c22007-08-02 11:10:31 -04007357 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007358 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007359 continue;
7360
7361 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007362 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007363 if (abtsiocb == NULL) {
7364 errcnt++;
7365 continue;
7366 }
dea31012005-04-17 16:05:31 -05007367
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007368 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007369 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7370 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007371 if (phba->sli_rev == LPFC_SLI_REV4)
7372 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7373 else
7374 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007375 abtsiocb->iocb.ulpLe = 1;
7376 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007377 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007378
James Smart5ffc2662009-11-18 15:39:44 -05007379 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7380 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007381 if (iocbq->iocb_flag & LPFC_IO_FCP)
7382 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007383
James Smart2e0fef82007-06-17 19:56:36 -05007384 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007385 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7386 else
7387 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7388
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007389 /* Setup callback routine and issue the command. */
7390 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007391 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7392 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007393 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007394 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007395 errcnt++;
7396 continue;
7397 }
7398 }
7399
7400 return errcnt;
7401}
7402
James Smarte59058c2008-08-24 21:49:00 -04007403/**
James Smart3621a712009-04-06 18:47:14 -04007404 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007405 * @phba: Pointer to HBA context object.
7406 * @cmdiocbq: Pointer to command iocb.
7407 * @rspiocbq: Pointer to response iocb.
7408 *
7409 * This function is the completion handler for iocbs issued using
7410 * lpfc_sli_issue_iocb_wait function. This function is called by the
7411 * ring event handler function without any lock held. This function
7412 * can be called from both worker thread context and interrupt
7413 * context. This function also can be called from other thread which
7414 * cleans up the SLI layer objects.
7415 * This function copy the contents of the response iocb to the
7416 * response iocb memory object provided by the caller of
7417 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7418 * sleeps for the iocb completion.
7419 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007420static void
7421lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7422 struct lpfc_iocbq *cmdiocbq,
7423 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007424{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007425 wait_queue_head_t *pdone_q;
7426 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007427
James Smart2e0fef82007-06-17 19:56:36 -05007428 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007429 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7430 if (cmdiocbq->context2 && rspiocbq)
7431 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7432 &rspiocbq->iocb, sizeof(IOCB_t));
7433
7434 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007435 if (pdone_q)
7436 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007437 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007438 return;
7439}
7440
James Smarte59058c2008-08-24 21:49:00 -04007441/**
James Smartd11e31d2009-06-10 17:23:06 -04007442 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7443 * @phba: Pointer to HBA context object..
7444 * @piocbq: Pointer to command iocb.
7445 * @flag: Flag to test.
7446 *
7447 * This routine grabs the hbalock and then test the iocb_flag to
7448 * see if the passed in flag is set.
7449 * Returns:
7450 * 1 if flag is set.
7451 * 0 if flag is not set.
7452 **/
7453static int
7454lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7455 struct lpfc_iocbq *piocbq, uint32_t flag)
7456{
7457 unsigned long iflags;
7458 int ret;
7459
7460 spin_lock_irqsave(&phba->hbalock, iflags);
7461 ret = piocbq->iocb_flag & flag;
7462 spin_unlock_irqrestore(&phba->hbalock, iflags);
7463 return ret;
7464
7465}
7466
7467/**
James Smart3621a712009-04-06 18:47:14 -04007468 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007469 * @phba: Pointer to HBA context object..
7470 * @pring: Pointer to sli ring.
7471 * @piocb: Pointer to command iocb.
7472 * @prspiocbq: Pointer to response iocb.
7473 * @timeout: Timeout in number of seconds.
7474 *
7475 * This function issues the iocb to firmware and waits for the
7476 * iocb to complete. If the iocb command is not
7477 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7478 * Caller should not free the iocb resources if this function
7479 * returns IOCB_TIMEDOUT.
7480 * The function waits for the iocb completion using an
7481 * non-interruptible wait.
7482 * This function will sleep while waiting for iocb completion.
7483 * So, this function should not be called from any context which
7484 * does not allow sleeping. Due to the same reason, this function
7485 * cannot be called with interrupt disabled.
7486 * This function assumes that the iocb completions occur while
7487 * this function sleep. So, this function cannot be called from
7488 * the thread which process iocb completion for this ring.
7489 * This function clears the iocb_flag of the iocb object before
7490 * issuing the iocb and the iocb completion handler sets this
7491 * flag and wakes this thread when the iocb completes.
7492 * The contents of the response iocb will be copied to prspiocbq
7493 * by the completion handler when the command completes.
7494 * This function returns IOCB_SUCCESS when success.
7495 * This function is called with no lock held.
7496 **/
dea31012005-04-17 16:05:31 -05007497int
James Smart2e0fef82007-06-17 19:56:36 -05007498lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007499 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007500 struct lpfc_iocbq *piocb,
7501 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007502 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007503{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007504 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007505 long timeleft, timeout_req = 0;
7506 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007507 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007508
7509 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007510 * If the caller has provided a response iocbq buffer, then context2
7511 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007512 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007513 if (prspiocbq) {
7514 if (piocb->context2)
7515 return IOCB_ERROR;
7516 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007517 }
7518
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007519 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7520 piocb->context_un.wait_queue = &done_q;
7521 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007522
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007523 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7524 creg_val = readl(phba->HCregaddr);
7525 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7526 writel(creg_val, phba->HCregaddr);
7527 readl(phba->HCregaddr); /* flush */
7528 }
7529
James Smartda0436e2009-05-22 14:51:39 -04007530 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007531 if (retval == IOCB_SUCCESS) {
7532 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007533 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007534 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007535 timeout_req);
dea31012005-04-17 16:05:31 -05007536
James Smart7054a602007-04-25 09:52:34 -04007537 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7538 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007539 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007540 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007541 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007542 "0338 IOCB wait timeout error - no "
7543 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007544 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007545 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007547 "0330 IOCB wake NOT set, "
7548 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007549 timeout, (timeleft / jiffies));
7550 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007551 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007552 } else {
7553 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007554 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007555 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007556 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007557 }
7558
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007559 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7560 creg_val = readl(phba->HCregaddr);
7561 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7562 writel(creg_val, phba->HCregaddr);
7563 readl(phba->HCregaddr); /* flush */
7564 }
7565
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007566 if (prspiocbq)
7567 piocb->context2 = NULL;
7568
7569 piocb->context_un.wait_queue = NULL;
7570 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007571 return retval;
7572}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007573
James Smarte59058c2008-08-24 21:49:00 -04007574/**
James Smart3621a712009-04-06 18:47:14 -04007575 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007576 * @phba: Pointer to HBA context object.
7577 * @pmboxq: Pointer to driver mailbox object.
7578 * @timeout: Timeout in number of seconds.
7579 *
7580 * This function issues the mailbox to firmware and waits for the
7581 * mailbox command to complete. If the mailbox command is not
7582 * completed within timeout seconds, it returns MBX_TIMEOUT.
7583 * The function waits for the mailbox completion using an
7584 * interruptible wait. If the thread is woken up due to a
7585 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7586 * should not free the mailbox resources, if this function returns
7587 * MBX_TIMEOUT.
7588 * This function will sleep while waiting for mailbox completion.
7589 * So, this function should not be called from any context which
7590 * does not allow sleeping. Due to the same reason, this function
7591 * cannot be called with interrupt disabled.
7592 * This function assumes that the mailbox completion occurs while
7593 * this function sleep. So, this function cannot be called from
7594 * the worker thread which processes mailbox completion.
7595 * This function is called in the context of HBA management
7596 * applications.
7597 * This function returns MBX_SUCCESS when successful.
7598 * This function is called with no lock held.
7599 **/
dea31012005-04-17 16:05:31 -05007600int
James Smart2e0fef82007-06-17 19:56:36 -05007601lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007602 uint32_t timeout)
7603{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007604 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007605 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007606 unsigned long flag;
dea31012005-04-17 16:05:31 -05007607
7608 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007609 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007610 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007611
James Smart495a7142008-06-14 22:52:59 -04007612 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007613 /* setup wake call as IOCB callback */
7614 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7615 /* setup context field to pass wait_queue pointer to wake function */
7616 pmboxq->context1 = &done_q;
7617
dea31012005-04-17 16:05:31 -05007618 /* now issue the command */
7619 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7620
7621 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007622 wait_event_interruptible_timeout(done_q,
7623 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7624 timeout * HZ);
7625
James Smart858c9f62007-06-17 19:56:39 -05007626 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007627 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007628 /*
7629 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7630 * else do not free the resources.
7631 */
7632 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007633 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007634 else {
James Smart7054a602007-04-25 09:52:34 -04007635 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007636 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7637 }
7638 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007639 }
7640
dea31012005-04-17 16:05:31 -05007641 return retval;
7642}
7643
James Smarte59058c2008-08-24 21:49:00 -04007644/**
James Smart3772a992009-05-22 14:50:54 -04007645 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007646 * @phba: Pointer to HBA context.
7647 *
James Smart3772a992009-05-22 14:50:54 -04007648 * This function is called to shutdown the driver's mailbox sub-system.
7649 * It first marks the mailbox sub-system is in a block state to prevent
7650 * the asynchronous mailbox command from issued off the pending mailbox
7651 * command queue. If the mailbox command sub-system shutdown is due to
7652 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7653 * the mailbox sub-system flush routine to forcefully bring down the
7654 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7655 * as with offline or HBA function reset), this routine will wait for the
7656 * outstanding mailbox command to complete before invoking the mailbox
7657 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007658 **/
James Smart3772a992009-05-22 14:50:54 -04007659void
7660lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007661{
James Smart3772a992009-05-22 14:50:54 -04007662 struct lpfc_sli *psli = &phba->sli;
7663 uint8_t actcmd = MBX_HEARTBEAT;
7664 unsigned long timeout;
7665
7666 spin_lock_irq(&phba->hbalock);
7667 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7668 spin_unlock_irq(&phba->hbalock);
7669
7670 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7671 spin_lock_irq(&phba->hbalock);
7672 if (phba->sli.mbox_active)
7673 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7674 spin_unlock_irq(&phba->hbalock);
7675 /* Determine how long we might wait for the active mailbox
7676 * command to be gracefully completed by firmware.
7677 */
7678 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7679 1000) + jiffies;
7680 while (phba->sli.mbox_active) {
7681 /* Check active mailbox complete status every 2ms */
7682 msleep(2);
7683 if (time_after(jiffies, timeout))
7684 /* Timeout, let the mailbox flush routine to
7685 * forcefully release active mailbox command
7686 */
7687 break;
7688 }
7689 }
7690 lpfc_sli_mbox_sys_flush(phba);
7691}
7692
7693/**
7694 * lpfc_sli_eratt_read - read sli-3 error attention events
7695 * @phba: Pointer to HBA context.
7696 *
7697 * This function is called to read the SLI3 device error attention registers
7698 * for possible error attention events. The caller must hold the hostlock
7699 * with spin_lock_irq().
7700 *
7701 * This fucntion returns 1 when there is Error Attention in the Host Attention
7702 * Register and returns 0 otherwise.
7703 **/
7704static int
7705lpfc_sli_eratt_read(struct lpfc_hba *phba)
7706{
James Smarted957682007-06-17 19:56:37 -05007707 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007708
James Smart3772a992009-05-22 14:50:54 -04007709 /* Read chip Host Attention (HA) register */
7710 ha_copy = readl(phba->HAregaddr);
7711 if (ha_copy & HA_ERATT) {
7712 /* Read host status register to retrieve error event */
7713 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007714
James Smart3772a992009-05-22 14:50:54 -04007715 /* Check if there is a deferred error condition is active */
7716 if ((HS_FFER1 & phba->work_hs) &&
7717 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7718 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007719 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007720 /* Clear all interrupt enable conditions */
7721 writel(0, phba->HCregaddr);
7722 readl(phba->HCregaddr);
7723 }
7724
7725 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007726 phba->work_ha |= HA_ERATT;
7727 /* Indicate polling handles this ERATT */
7728 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007729 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007730 }
James Smart3772a992009-05-22 14:50:54 -04007731 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007732}
7733
James Smarte59058c2008-08-24 21:49:00 -04007734/**
James Smartda0436e2009-05-22 14:51:39 -04007735 * lpfc_sli4_eratt_read - read sli-4 error attention events
7736 * @phba: Pointer to HBA context.
7737 *
7738 * This function is called to read the SLI4 device error attention registers
7739 * for possible error attention events. The caller must hold the hostlock
7740 * with spin_lock_irq().
7741 *
7742 * This fucntion returns 1 when there is Error Attention in the Host Attention
7743 * Register and returns 0 otherwise.
7744 **/
7745static int
7746lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7747{
7748 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007749
7750 /* For now, use the SLI4 device internal unrecoverable error
7751 * registers for error attention. This can be changed later.
7752 */
James Smarta747c9c2009-11-18 15:41:10 -05007753 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7754 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7755 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7756 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7758 "1423 HBA Unrecoverable error: "
7759 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7760 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7761 uerr_sta_lo, uerr_sta_hi,
7762 phba->sli4_hba.ue_mask_lo,
7763 phba->sli4_hba.ue_mask_hi);
7764 phba->work_status[0] = uerr_sta_lo;
7765 phba->work_status[1] = uerr_sta_hi;
7766 /* Set the driver HA work bitmap */
7767 phba->work_ha |= HA_ERATT;
7768 /* Indicate polling handles this ERATT */
7769 phba->hba_flag |= HBA_ERATT_HANDLED;
7770 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007771 }
7772 return 0;
7773}
7774
7775/**
James Smart3621a712009-04-06 18:47:14 -04007776 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007777 * @phba: Pointer to HBA context.
7778 *
James Smart3772a992009-05-22 14:50:54 -04007779 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007780 * error attention register bit for error attention events.
7781 *
7782 * This fucntion returns 1 when there is Error Attention in the Host Attention
7783 * Register and returns 0 otherwise.
7784 **/
7785int
7786lpfc_sli_check_eratt(struct lpfc_hba *phba)
7787{
7788 uint32_t ha_copy;
7789
7790 /* If somebody is waiting to handle an eratt, don't process it
7791 * here. The brdkill function will do this.
7792 */
7793 if (phba->link_flag & LS_IGNORE_ERATT)
7794 return 0;
7795
7796 /* Check if interrupt handler handles this ERATT */
7797 spin_lock_irq(&phba->hbalock);
7798 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7799 /* Interrupt handler has handled ERATT */
7800 spin_unlock_irq(&phba->hbalock);
7801 return 0;
7802 }
7803
James Smarta257bf92009-04-06 18:48:10 -04007804 /*
7805 * If there is deferred error attention, do not check for error
7806 * attention
7807 */
7808 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7809 spin_unlock_irq(&phba->hbalock);
7810 return 0;
7811 }
7812
James Smart3772a992009-05-22 14:50:54 -04007813 /* If PCI channel is offline, don't process it */
7814 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007815 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007816 return 0;
7817 }
7818
7819 switch (phba->sli_rev) {
7820 case LPFC_SLI_REV2:
7821 case LPFC_SLI_REV3:
7822 /* Read chip Host Attention (HA) register */
7823 ha_copy = lpfc_sli_eratt_read(phba);
7824 break;
James Smartda0436e2009-05-22 14:51:39 -04007825 case LPFC_SLI_REV4:
7826 /* Read devcie Uncoverable Error (UERR) registers */
7827 ha_copy = lpfc_sli4_eratt_read(phba);
7828 break;
James Smart3772a992009-05-22 14:50:54 -04007829 default:
7830 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7831 "0299 Invalid SLI revision (%d)\n",
7832 phba->sli_rev);
7833 ha_copy = 0;
7834 break;
James Smart93996272008-08-24 21:50:30 -04007835 }
7836 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007837
7838 return ha_copy;
7839}
7840
7841/**
7842 * lpfc_intr_state_check - Check device state for interrupt handling
7843 * @phba: Pointer to HBA context.
7844 *
7845 * This inline routine checks whether a device or its PCI slot is in a state
7846 * that the interrupt should be handled.
7847 *
7848 * This function returns 0 if the device or the PCI slot is in a state that
7849 * interrupt should be handled, otherwise -EIO.
7850 */
7851static inline int
7852lpfc_intr_state_check(struct lpfc_hba *phba)
7853{
7854 /* If the pci channel is offline, ignore all the interrupts */
7855 if (unlikely(pci_channel_offline(phba->pcidev)))
7856 return -EIO;
7857
7858 /* Update device level interrupt statistics */
7859 phba->sli.slistat.sli_intr++;
7860
7861 /* Ignore all interrupts during initialization. */
7862 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7863 return -EIO;
7864
James Smart93996272008-08-24 21:50:30 -04007865 return 0;
7866}
7867
7868/**
James Smart3772a992009-05-22 14:50:54 -04007869 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007870 * @irq: Interrupt number.
7871 * @dev_id: The device context pointer.
7872 *
James Smart93996272008-08-24 21:50:30 -04007873 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007874 * service routine when device with SLI-3 interface spec is enabled with
7875 * MSI-X multi-message interrupt mode and there are slow-path events in
7876 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7877 * interrupt mode, this function is called as part of the device-level
7878 * interrupt handler. When the PCI slot is in error recovery or the HBA
7879 * is undergoing initialization, the interrupt handler will not process
7880 * the interrupt. The link attention and ELS ring attention events are
7881 * handled by the worker thread. The interrupt handler signals the worker
7882 * thread and returns for these events. This function is called without
7883 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007884 * structures.
7885 *
7886 * This function returns IRQ_HANDLED when interrupt is handled else it
7887 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007888 **/
dea31012005-04-17 16:05:31 -05007889irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007890lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007891{
James Smart2e0fef82007-06-17 19:56:36 -05007892 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007893 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007894 uint32_t work_ha_copy;
7895 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007896 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007897 uint32_t control;
7898
James Smart92d7f7b2007-06-17 19:56:38 -05007899 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007900 struct lpfc_vport *vport;
7901 struct lpfc_nodelist *ndlp;
7902 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007903 LPFC_MBOXQ_t *pmb;
7904 int rc;
7905
dea31012005-04-17 16:05:31 -05007906 /*
7907 * Get the driver's phba structure from the dev_id and
7908 * assume the HBA is not interrupting.
7909 */
James Smart93996272008-08-24 21:50:30 -04007910 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007911
7912 if (unlikely(!phba))
7913 return IRQ_NONE;
7914
dea31012005-04-17 16:05:31 -05007915 /*
James Smart93996272008-08-24 21:50:30 -04007916 * Stuff needs to be attented to when this function is invoked as an
7917 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007918 */
James Smart93996272008-08-24 21:50:30 -04007919 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007920 /* Check device state for handling interrupt */
7921 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007922 return IRQ_NONE;
7923 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007924 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007925 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007926 /* If somebody is waiting to handle an eratt don't process it
7927 * here. The brdkill function will do this.
7928 */
7929 if (phba->link_flag & LS_IGNORE_ERATT)
7930 ha_copy &= ~HA_ERATT;
7931 /* Check the need for handling ERATT in interrupt handler */
7932 if (ha_copy & HA_ERATT) {
7933 if (phba->hba_flag & HBA_ERATT_HANDLED)
7934 /* ERATT polling has handled ERATT */
7935 ha_copy &= ~HA_ERATT;
7936 else
7937 /* Indicate interrupt handler handles ERATT */
7938 phba->hba_flag |= HBA_ERATT_HANDLED;
7939 }
James Smarta257bf92009-04-06 18:48:10 -04007940
7941 /*
7942 * If there is deferred error attention, do not check for any
7943 * interrupt.
7944 */
7945 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007946 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007947 return IRQ_NONE;
7948 }
7949
James Smart93996272008-08-24 21:50:30 -04007950 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05007951 hc_copy = readl(phba->HCregaddr);
7952 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
7953 HC_LAINT_ENA | HC_ERINT_ENA),
7954 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04007955 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7956 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05007957 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04007958 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007959 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007960 } else
7961 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007962
dea31012005-04-17 16:05:31 -05007963 work_ha_copy = ha_copy & phba->work_ha_mask;
7964
James Smart93996272008-08-24 21:50:30 -04007965 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007966 if (work_ha_copy & HA_LATT) {
7967 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7968 /*
7969 * Turn off Link Attention interrupts
7970 * until CLEAR_LA done
7971 */
James Smart5b75da22008-12-04 22:39:35 -05007972 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007973 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7974 control = readl(phba->HCregaddr);
7975 control &= ~HC_LAINT_ENA;
7976 writel(control, phba->HCregaddr);
7977 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007978 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007979 }
7980 else
7981 work_ha_copy &= ~HA_LATT;
7982 }
7983
James Smart93996272008-08-24 21:50:30 -04007984 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05007985 /*
7986 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7987 * the only slow ring.
7988 */
7989 status = (work_ha_copy &
7990 (HA_RXMASK << (4*LPFC_ELS_RING)));
7991 status >>= (4*LPFC_ELS_RING);
7992 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05007993 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05007994 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04007995
7996 lpfc_debugfs_slow_ring_trc(phba,
7997 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7998 control, status,
7999 (uint32_t)phba->sli.slistat.sli_intr);
8000
James Smart858c9f62007-06-17 19:56:39 -05008001 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008002 lpfc_debugfs_slow_ring_trc(phba,
8003 "ISR Disable ring:"
8004 "pwork:x%x hawork:x%x wait:x%x",
8005 phba->work_ha, work_ha_copy,
8006 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008007 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008008
James Smart858c9f62007-06-17 19:56:39 -05008009 control &=
8010 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008011 writel(control, phba->HCregaddr);
8012 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008013 }
James Smarta58cbd52007-08-02 11:09:43 -04008014 else {
8015 lpfc_debugfs_slow_ring_trc(phba,
8016 "ISR slow ring: pwork:"
8017 "x%x hawork:x%x wait:x%x",
8018 phba->work_ha, work_ha_copy,
8019 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008020 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008021 }
James Smart5b75da22008-12-04 22:39:35 -05008022 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008023 }
8024 }
James Smart5b75da22008-12-04 22:39:35 -05008025 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008026 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008027 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008028 /*
8029 * Check if there is a deferred error condition
8030 * is active
8031 */
8032 if ((HS_FFER1 & phba->work_hs) &&
8033 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8034 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8035 phba->hba_flag |= DEFER_ERATT;
8036 /* Clear all interrupt enable conditions */
8037 writel(0, phba->HCregaddr);
8038 readl(phba->HCregaddr);
8039 }
8040 }
8041
James Smart93996272008-08-24 21:50:30 -04008042 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008043 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008044 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008045 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008046 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008047
8048 /* First check out the status word */
8049 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8050 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008051 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008052 /*
8053 * Stray Mailbox Interrupt, mbxCommand <cmd>
8054 * mbxStatus <status>
8055 */
James Smart09372822008-01-11 01:52:54 -05008056 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008057 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008058 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008059 "Interrupt mbxCommand x%x "
8060 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008061 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008062 pmbox->mbxCommand,
8063 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008064 /* clear mailbox attention bit */
8065 work_ha_copy &= ~HA_MBATT;
8066 } else {
James Smart97eab632008-04-07 10:16:05 -04008067 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008068 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008069 phba->last_completion_time = jiffies;
8070 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008071 if (pmb->mbox_cmpl) {
8072 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8073 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008074 }
James Smart09372822008-01-11 01:52:54 -05008075 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8076 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8077
8078 lpfc_debugfs_disc_trc(vport,
8079 LPFC_DISC_TRC_MBOX_VPORT,
8080 "MBOX dflt rpi: : "
8081 "status:x%x rpi:x%x",
8082 (uint32_t)pmbox->mbxStatus,
8083 pmbox->un.varWords[0], 0);
8084
8085 if (!pmbox->mbxStatus) {
8086 mp = (struct lpfc_dmabuf *)
8087 (pmb->context1);
8088 ndlp = (struct lpfc_nodelist *)
8089 pmb->context2;
8090
8091 /* Reg_LOGIN of dflt RPI was
8092 * successful. new lets get
8093 * rid of the RPI using the
8094 * same mbox buffer.
8095 */
8096 lpfc_unreg_login(phba,
8097 vport->vpi,
8098 pmbox->un.varWords[0],
8099 pmb);
8100 pmb->mbox_cmpl =
8101 lpfc_mbx_cmpl_dflt_rpi;
8102 pmb->context1 = mp;
8103 pmb->context2 = ndlp;
8104 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008105 rc = lpfc_sli_issue_mbox(phba,
8106 pmb,
8107 MBX_NOWAIT);
8108 if (rc != MBX_BUSY)
8109 lpfc_printf_log(phba,
8110 KERN_ERR,
8111 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008112 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008113 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008114 if (rc != MBX_NOT_FINISHED)
8115 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008116 }
8117 }
James Smart5b75da22008-12-04 22:39:35 -05008118 spin_lock_irqsave(
8119 &phba->pport->work_port_lock,
8120 iflag);
James Smart09372822008-01-11 01:52:54 -05008121 phba->pport->work_port_events &=
8122 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008123 spin_unlock_irqrestore(
8124 &phba->pport->work_port_lock,
8125 iflag);
James Smart09372822008-01-11 01:52:54 -05008126 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008127 }
James Smart97eab632008-04-07 10:16:05 -04008128 } else
James Smart5b75da22008-12-04 22:39:35 -05008129 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008130
James Smart92d7f7b2007-06-17 19:56:38 -05008131 if ((work_ha_copy & HA_MBATT) &&
8132 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008133send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008134 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008135 do {
8136 rc = lpfc_sli_issue_mbox(phba, NULL,
8137 MBX_NOWAIT);
8138 } while (rc == MBX_NOT_FINISHED);
8139 if (rc != MBX_SUCCESS)
8140 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8141 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008142 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008143 }
8144
James Smart5b75da22008-12-04 22:39:35 -05008145 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008146 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008147 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008148 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008149 }
James Smart93996272008-08-24 21:50:30 -04008150 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008151
James Smart3772a992009-05-22 14:50:54 -04008152} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008153
8154/**
James Smart3772a992009-05-22 14:50:54 -04008155 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008156 * @irq: Interrupt number.
8157 * @dev_id: The device context pointer.
8158 *
8159 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008160 * service routine when device with SLI-3 interface spec is enabled with
8161 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8162 * ring event in the HBA. However, when the device is enabled with either
8163 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8164 * device-level interrupt handler. When the PCI slot is in error recovery
8165 * or the HBA is undergoing initialization, the interrupt handler will not
8166 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8167 * the intrrupt context. This function is called without any lock held.
8168 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008169 *
8170 * This function returns IRQ_HANDLED when interrupt is handled else it
8171 * returns IRQ_NONE.
8172 **/
8173irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008174lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008175{
8176 struct lpfc_hba *phba;
8177 uint32_t ha_copy;
8178 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008179 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008180
8181 /* Get the driver's phba structure from the dev_id and
8182 * assume the HBA is not interrupting.
8183 */
8184 phba = (struct lpfc_hba *) dev_id;
8185
8186 if (unlikely(!phba))
8187 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008188
8189 /*
James Smart93996272008-08-24 21:50:30 -04008190 * Stuff needs to be attented to when this function is invoked as an
8191 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008192 */
James Smart93996272008-08-24 21:50:30 -04008193 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008194 /* Check device state for handling interrupt */
8195 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008196 return IRQ_NONE;
8197 /* Need to read HA REG for FCP ring and other ring events */
8198 ha_copy = readl(phba->HAregaddr);
8199 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008200 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008201 /*
8202 * If there is deferred error attention, do not check for
8203 * any interrupt.
8204 */
8205 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008206 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008207 return IRQ_NONE;
8208 }
James Smart93996272008-08-24 21:50:30 -04008209 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8210 phba->HAregaddr);
8211 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008212 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008213 } else
8214 ha_copy = phba->ha_copy;
8215
8216 /*
8217 * Process all events on FCP ring. Take the optimized path for FCP IO.
8218 */
8219 ha_copy &= ~(phba->work_ha_mask);
8220
8221 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008222 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008223 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008224 lpfc_sli_handle_fast_ring_event(phba,
8225 &phba->sli.ring[LPFC_FCP_RING],
8226 status);
James Smarta4bc3372006-12-02 13:34:16 -05008227
8228 if (phba->cfg_multi_ring_support == 2) {
8229 /*
James Smart93996272008-08-24 21:50:30 -04008230 * Process all events on extra ring. Take the optimized path
8231 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008232 */
James Smart93996272008-08-24 21:50:30 -04008233 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008234 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008235 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008236 lpfc_sli_handle_fast_ring_event(phba,
8237 &phba->sli.ring[LPFC_EXTRA_RING],
8238 status);
8239 }
8240 }
dea31012005-04-17 16:05:31 -05008241 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008242} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008243
James Smart93996272008-08-24 21:50:30 -04008244/**
James Smart3772a992009-05-22 14:50:54 -04008245 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008246 * @irq: Interrupt number.
8247 * @dev_id: The device context pointer.
8248 *
James Smart3772a992009-05-22 14:50:54 -04008249 * This function is the HBA device-level interrupt handler to device with
8250 * SLI-3 interface spec, called from the PCI layer when either MSI or
8251 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8252 * requires driver attention. This function invokes the slow-path interrupt
8253 * attention handling function and fast-path interrupt attention handling
8254 * function in turn to process the relevant HBA attention events. This
8255 * function is called without any lock held. It gets the hbalock to access
8256 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008257 *
8258 * This function returns IRQ_HANDLED when interrupt is handled, else it
8259 * returns IRQ_NONE.
8260 **/
8261irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008262lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008263{
8264 struct lpfc_hba *phba;
8265 irqreturn_t sp_irq_rc, fp_irq_rc;
8266 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008267 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008268
8269 /*
8270 * Get the driver's phba structure from the dev_id and
8271 * assume the HBA is not interrupting.
8272 */
8273 phba = (struct lpfc_hba *) dev_id;
8274
8275 if (unlikely(!phba))
8276 return IRQ_NONE;
8277
James Smart3772a992009-05-22 14:50:54 -04008278 /* Check device state for handling interrupt */
8279 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008280 return IRQ_NONE;
8281
8282 spin_lock(&phba->hbalock);
8283 phba->ha_copy = readl(phba->HAregaddr);
8284 if (unlikely(!phba->ha_copy)) {
8285 spin_unlock(&phba->hbalock);
8286 return IRQ_NONE;
8287 } else if (phba->ha_copy & HA_ERATT) {
8288 if (phba->hba_flag & HBA_ERATT_HANDLED)
8289 /* ERATT polling has handled ERATT */
8290 phba->ha_copy &= ~HA_ERATT;
8291 else
8292 /* Indicate interrupt handler handles ERATT */
8293 phba->hba_flag |= HBA_ERATT_HANDLED;
8294 }
8295
James Smarta257bf92009-04-06 18:48:10 -04008296 /*
8297 * If there is deferred error attention, do not check for any interrupt.
8298 */
8299 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8300 spin_unlock_irq(&phba->hbalock);
8301 return IRQ_NONE;
8302 }
8303
James Smart93996272008-08-24 21:50:30 -04008304 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008305 hc_copy = readl(phba->HCregaddr);
8306 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8307 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8308 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008309 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008310 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008311 readl(phba->HAregaddr); /* flush */
8312 spin_unlock(&phba->hbalock);
8313
8314 /*
8315 * Invokes slow-path host attention interrupt handling as appropriate.
8316 */
8317
8318 /* status of events with mailbox and link attention */
8319 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8320
8321 /* status of events with ELS ring */
8322 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8323 status2 >>= (4*LPFC_ELS_RING);
8324
8325 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008326 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008327 else
8328 sp_irq_rc = IRQ_NONE;
8329
8330 /*
8331 * Invoke fast-path host attention interrupt handling as appropriate.
8332 */
8333
8334 /* status of events with FCP ring */
8335 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8336 status1 >>= (4*LPFC_FCP_RING);
8337
8338 /* status of events with extra ring */
8339 if (phba->cfg_multi_ring_support == 2) {
8340 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8341 status2 >>= (4*LPFC_EXTRA_RING);
8342 } else
8343 status2 = 0;
8344
8345 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008346 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008347 else
8348 fp_irq_rc = IRQ_NONE;
8349
8350 /* Return device-level interrupt handling status */
8351 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008352} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008353
8354/**
8355 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8356 * @phba: pointer to lpfc hba data structure.
8357 *
8358 * This routine is invoked by the worker thread to process all the pending
8359 * SLI4 FCP abort XRI events.
8360 **/
8361void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8362{
8363 struct lpfc_cq_event *cq_event;
8364
8365 /* First, declare the fcp xri abort event has been handled */
8366 spin_lock_irq(&phba->hbalock);
8367 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8368 spin_unlock_irq(&phba->hbalock);
8369 /* Now, handle all the fcp xri abort events */
8370 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8371 /* Get the first event from the head of the event queue */
8372 spin_lock_irq(&phba->hbalock);
8373 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8374 cq_event, struct lpfc_cq_event, list);
8375 spin_unlock_irq(&phba->hbalock);
8376 /* Notify aborted XRI for FCP work queue */
8377 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8378 /* Free the event processed back to the free pool */
8379 lpfc_sli4_cq_event_release(phba, cq_event);
8380 }
8381}
8382
8383/**
8384 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8385 * @phba: pointer to lpfc hba data structure.
8386 *
8387 * This routine is invoked by the worker thread to process all the pending
8388 * SLI4 els abort xri events.
8389 **/
8390void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8391{
8392 struct lpfc_cq_event *cq_event;
8393
8394 /* First, declare the els xri abort event has been handled */
8395 spin_lock_irq(&phba->hbalock);
8396 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8397 spin_unlock_irq(&phba->hbalock);
8398 /* Now, handle all the els xri abort events */
8399 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8400 /* Get the first event from the head of the event queue */
8401 spin_lock_irq(&phba->hbalock);
8402 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8403 cq_event, struct lpfc_cq_event, list);
8404 spin_unlock_irq(&phba->hbalock);
8405 /* Notify aborted XRI for ELS work queue */
8406 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8407 /* Free the event processed back to the free pool */
8408 lpfc_sli4_cq_event_release(phba, cq_event);
8409 }
8410}
8411
James Smart341af102010-01-26 23:07:37 -05008412/**
8413 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8414 * @phba: pointer to lpfc hba data structure
8415 * @pIocbIn: pointer to the rspiocbq
8416 * @pIocbOut: pointer to the cmdiocbq
8417 * @wcqe: pointer to the complete wcqe
8418 *
8419 * This routine transfers the fields of a command iocbq to a response iocbq
8420 * by copying all the IOCB fields from command iocbq and transferring the
8421 * completion status information from the complete wcqe.
8422 **/
James Smart4f774512009-05-22 14:52:35 -04008423static void
James Smart341af102010-01-26 23:07:37 -05008424lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8425 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008426 struct lpfc_iocbq *pIocbOut,
8427 struct lpfc_wcqe_complete *wcqe)
8428{
James Smart341af102010-01-26 23:07:37 -05008429 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008430 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8431
8432 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8433 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008434 /* Map WCQE parameters into irspiocb parameters */
8435 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8436 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8437 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8438 pIocbIn->iocb.un.fcpi.fcpi_parm =
8439 pIocbOut->iocb.un.fcpi.fcpi_parm -
8440 wcqe->total_data_placed;
8441 else
8442 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8443 else
8444 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart341af102010-01-26 23:07:37 -05008445
8446 /* Pick up HBA exchange busy condition */
8447 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8448 spin_lock_irqsave(&phba->hbalock, iflags);
8449 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8450 spin_unlock_irqrestore(&phba->hbalock, iflags);
8451 }
James Smart4f774512009-05-22 14:52:35 -04008452}
8453
8454/**
James Smart45ed1192009-10-02 15:17:02 -04008455 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8456 * @phba: Pointer to HBA context object.
8457 * @wcqe: Pointer to work-queue completion queue entry.
8458 *
8459 * This routine handles an ELS work-queue completion event and construct
8460 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8461 * discovery engine to handle.
8462 *
8463 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8464 **/
8465static struct lpfc_iocbq *
8466lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8467 struct lpfc_iocbq *irspiocbq)
8468{
8469 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8470 struct lpfc_iocbq *cmdiocbq;
8471 struct lpfc_wcqe_complete *wcqe;
8472 unsigned long iflags;
8473
8474 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8475 spin_lock_irqsave(&phba->hbalock, iflags);
8476 pring->stats.iocb_event++;
8477 /* Look up the ELS command IOCB and create pseudo response IOCB */
8478 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8479 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8480 spin_unlock_irqrestore(&phba->hbalock, iflags);
8481
8482 if (unlikely(!cmdiocbq)) {
8483 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8484 "0386 ELS complete with no corresponding "
8485 "cmdiocb: iotag (%d)\n",
8486 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8487 lpfc_sli_release_iocbq(phba, irspiocbq);
8488 return NULL;
8489 }
8490
8491 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008492 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008493
8494 return irspiocbq;
8495}
8496
8497/**
James Smart04c68492009-05-22 14:52:52 -04008498 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8499 * @phba: Pointer to HBA context object.
8500 * @cqe: Pointer to mailbox completion queue entry.
8501 *
8502 * This routine process a mailbox completion queue entry with asynchrous
8503 * event.
8504 *
8505 * Return: true if work posted to worker thread, otherwise false.
8506 **/
8507static bool
8508lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8509{
8510 struct lpfc_cq_event *cq_event;
8511 unsigned long iflags;
8512
8513 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8514 "0392 Async Event: word0:x%x, word1:x%x, "
8515 "word2:x%x, word3:x%x\n", mcqe->word0,
8516 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8517
8518 /* Allocate a new internal CQ_EVENT entry */
8519 cq_event = lpfc_sli4_cq_event_alloc(phba);
8520 if (!cq_event) {
8521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8522 "0394 Failed to allocate CQ_EVENT entry\n");
8523 return false;
8524 }
8525
8526 /* Move the CQE into an asynchronous event entry */
8527 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8528 spin_lock_irqsave(&phba->hbalock, iflags);
8529 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8530 /* Set the async event flag */
8531 phba->hba_flag |= ASYNC_EVENT;
8532 spin_unlock_irqrestore(&phba->hbalock, iflags);
8533
8534 return true;
8535}
8536
8537/**
8538 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8539 * @phba: Pointer to HBA context object.
8540 * @cqe: Pointer to mailbox completion queue entry.
8541 *
8542 * This routine process a mailbox completion queue entry with mailbox
8543 * completion event.
8544 *
8545 * Return: true if work posted to worker thread, otherwise false.
8546 **/
8547static bool
8548lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8549{
8550 uint32_t mcqe_status;
8551 MAILBOX_t *mbox, *pmbox;
8552 struct lpfc_mqe *mqe;
8553 struct lpfc_vport *vport;
8554 struct lpfc_nodelist *ndlp;
8555 struct lpfc_dmabuf *mp;
8556 unsigned long iflags;
8557 LPFC_MBOXQ_t *pmb;
8558 bool workposted = false;
8559 int rc;
8560
8561 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8562 if (!bf_get(lpfc_trailer_completed, mcqe))
8563 goto out_no_mqe_complete;
8564
8565 /* Get the reference to the active mbox command */
8566 spin_lock_irqsave(&phba->hbalock, iflags);
8567 pmb = phba->sli.mbox_active;
8568 if (unlikely(!pmb)) {
8569 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8570 "1832 No pending MBOX command to handle\n");
8571 spin_unlock_irqrestore(&phba->hbalock, iflags);
8572 goto out_no_mqe_complete;
8573 }
8574 spin_unlock_irqrestore(&phba->hbalock, iflags);
8575 mqe = &pmb->u.mqe;
8576 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8577 mbox = phba->mbox;
8578 vport = pmb->vport;
8579
8580 /* Reset heartbeat timer */
8581 phba->last_completion_time = jiffies;
8582 del_timer(&phba->sli.mbox_tmo);
8583
8584 /* Move mbox data to caller's mailbox region, do endian swapping */
8585 if (pmb->mbox_cmpl && mbox)
8586 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8587 /* Set the mailbox status with SLI4 range 0x4000 */
8588 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8589 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8590 bf_set(lpfc_mqe_status, mqe,
8591 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8592
8593 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8594 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8595 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8596 "MBOX dflt rpi: status:x%x rpi:x%x",
8597 mcqe_status,
8598 pmbox->un.varWords[0], 0);
8599 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8600 mp = (struct lpfc_dmabuf *)(pmb->context1);
8601 ndlp = (struct lpfc_nodelist *)pmb->context2;
8602 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8603 * RID of the PPI using the same mbox buffer.
8604 */
8605 lpfc_unreg_login(phba, vport->vpi,
8606 pmbox->un.varWords[0], pmb);
8607 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8608 pmb->context1 = mp;
8609 pmb->context2 = ndlp;
8610 pmb->vport = vport;
8611 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8612 if (rc != MBX_BUSY)
8613 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8614 LOG_SLI, "0385 rc should "
8615 "have been MBX_BUSY\n");
8616 if (rc != MBX_NOT_FINISHED)
8617 goto send_current_mbox;
8618 }
8619 }
8620 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8621 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8622 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8623
8624 /* There is mailbox completion work to do */
8625 spin_lock_irqsave(&phba->hbalock, iflags);
8626 __lpfc_mbox_cmpl_put(phba, pmb);
8627 phba->work_ha |= HA_MBATT;
8628 spin_unlock_irqrestore(&phba->hbalock, iflags);
8629 workposted = true;
8630
8631send_current_mbox:
8632 spin_lock_irqsave(&phba->hbalock, iflags);
8633 /* Release the mailbox command posting token */
8634 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8635 /* Setting active mailbox pointer need to be in sync to flag clear */
8636 phba->sli.mbox_active = NULL;
8637 spin_unlock_irqrestore(&phba->hbalock, iflags);
8638 /* Wake up worker thread to post the next pending mailbox command */
8639 lpfc_worker_wake_up(phba);
8640out_no_mqe_complete:
8641 if (bf_get(lpfc_trailer_consumed, mcqe))
8642 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8643 return workposted;
8644}
8645
8646/**
8647 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8648 * @phba: Pointer to HBA context object.
8649 * @cqe: Pointer to mailbox completion queue entry.
8650 *
8651 * This routine process a mailbox completion queue entry, it invokes the
8652 * proper mailbox complete handling or asynchrous event handling routine
8653 * according to the MCQE's async bit.
8654 *
8655 * Return: true if work posted to worker thread, otherwise false.
8656 **/
8657static bool
8658lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8659{
8660 struct lpfc_mcqe mcqe;
8661 bool workposted;
8662
8663 /* Copy the mailbox MCQE and convert endian order as needed */
8664 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8665
8666 /* Invoke the proper event handling routine */
8667 if (!bf_get(lpfc_trailer_async, &mcqe))
8668 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8669 else
8670 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8671 return workposted;
8672}
8673
8674/**
James Smart4f774512009-05-22 14:52:35 -04008675 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8676 * @phba: Pointer to HBA context object.
8677 * @wcqe: Pointer to work-queue completion queue entry.
8678 *
8679 * This routine handles an ELS work-queue completion event.
8680 *
8681 * Return: true if work posted to worker thread, otherwise false.
8682 **/
8683static bool
8684lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8685 struct lpfc_wcqe_complete *wcqe)
8686{
James Smart4f774512009-05-22 14:52:35 -04008687 struct lpfc_iocbq *irspiocbq;
8688 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008689
James Smart45ed1192009-10-02 15:17:02 -04008690 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008691 irspiocbq = lpfc_sli_get_iocbq(phba);
8692 if (!irspiocbq) {
8693 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8694 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008695 return false;
James Smart4f774512009-05-22 14:52:35 -04008696 }
James Smart4f774512009-05-22 14:52:35 -04008697
James Smart45ed1192009-10-02 15:17:02 -04008698 /* Save off the slow-path queue event for work thread to process */
8699 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008700 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008701 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008702 &phba->sli4_hba.sp_queue_event);
8703 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008704 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008705
James Smart45ed1192009-10-02 15:17:02 -04008706 return true;
James Smart4f774512009-05-22 14:52:35 -04008707}
8708
8709/**
8710 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8711 * @phba: Pointer to HBA context object.
8712 * @wcqe: Pointer to work-queue completion queue entry.
8713 *
8714 * This routine handles slow-path WQ entry comsumed event by invoking the
8715 * proper WQ release routine to the slow-path WQ.
8716 **/
8717static void
8718lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8719 struct lpfc_wcqe_release *wcqe)
8720{
8721 /* Check for the slow-path ELS work queue */
8722 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8723 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8724 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8725 else
8726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8727 "2579 Slow-path wqe consume event carries "
8728 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8729 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8730 phba->sli4_hba.els_wq->queue_id);
8731}
8732
8733/**
8734 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8735 * @phba: Pointer to HBA context object.
8736 * @cq: Pointer to a WQ completion queue.
8737 * @wcqe: Pointer to work-queue completion queue entry.
8738 *
8739 * This routine handles an XRI abort event.
8740 *
8741 * Return: true if work posted to worker thread, otherwise false.
8742 **/
8743static bool
8744lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8745 struct lpfc_queue *cq,
8746 struct sli4_wcqe_xri_aborted *wcqe)
8747{
8748 bool workposted = false;
8749 struct lpfc_cq_event *cq_event;
8750 unsigned long iflags;
8751
8752 /* Allocate a new internal CQ_EVENT entry */
8753 cq_event = lpfc_sli4_cq_event_alloc(phba);
8754 if (!cq_event) {
8755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8756 "0602 Failed to allocate CQ_EVENT entry\n");
8757 return false;
8758 }
8759
8760 /* Move the CQE into the proper xri abort event list */
8761 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8762 switch (cq->subtype) {
8763 case LPFC_FCP:
8764 spin_lock_irqsave(&phba->hbalock, iflags);
8765 list_add_tail(&cq_event->list,
8766 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8767 /* Set the fcp xri abort event flag */
8768 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8769 spin_unlock_irqrestore(&phba->hbalock, iflags);
8770 workposted = true;
8771 break;
8772 case LPFC_ELS:
8773 spin_lock_irqsave(&phba->hbalock, iflags);
8774 list_add_tail(&cq_event->list,
8775 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8776 /* Set the els xri abort event flag */
8777 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8778 spin_unlock_irqrestore(&phba->hbalock, iflags);
8779 workposted = true;
8780 break;
8781 default:
8782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8783 "0603 Invalid work queue CQE subtype (x%x)\n",
8784 cq->subtype);
8785 workposted = false;
8786 break;
8787 }
8788 return workposted;
8789}
8790
8791/**
James Smart4d9ab992009-10-02 15:16:39 -04008792 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008793 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008794 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008795 *
James Smart4d9ab992009-10-02 15:16:39 -04008796 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008797 *
8798 * Return: true if work posted to worker thread, otherwise false.
8799 **/
8800static bool
James Smart4d9ab992009-10-02 15:16:39 -04008801lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8802{
8803 bool workposted = false;
8804 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8805 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8806 struct hbq_dmabuf *dma_buf;
8807 uint32_t status;
8808 unsigned long iflags;
8809
James Smart4d9ab992009-10-02 15:16:39 -04008810 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8811 goto out;
8812
8813 status = bf_get(lpfc_rcqe_status, rcqe);
8814 switch (status) {
8815 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8817 "2537 Receive Frame Truncated!!\n");
8818 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008819 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008820 spin_lock_irqsave(&phba->hbalock, iflags);
8821 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8822 if (!dma_buf) {
8823 spin_unlock_irqrestore(&phba->hbalock, iflags);
8824 goto out;
8825 }
8826 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8827 /* save off the frame for the word thread to process */
8828 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008829 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008830 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008831 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008832 spin_unlock_irqrestore(&phba->hbalock, iflags);
8833 workposted = true;
8834 break;
8835 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8836 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8837 /* Post more buffers if possible */
8838 spin_lock_irqsave(&phba->hbalock, iflags);
8839 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8840 spin_unlock_irqrestore(&phba->hbalock, iflags);
8841 workposted = true;
8842 break;
8843 }
8844out:
8845 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008846}
8847
8848/**
8849 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8850 * @phba: Pointer to HBA context object.
8851 * @cq: Pointer to the completion queue.
8852 * @wcqe: Pointer to a completion queue entry.
8853 *
8854 * This routine process a slow-path work-queue or recieve queue completion queue
8855 * entry.
8856 *
8857 * Return: true if work posted to worker thread, otherwise false.
8858 **/
8859static bool
8860lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008861 struct lpfc_cqe *cqe)
8862{
James Smart45ed1192009-10-02 15:17:02 -04008863 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008864 bool workposted = false;
8865
8866 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008867 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008868
8869 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008870 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008871 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008872 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008873 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008874 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008875 break;
8876 case CQE_CODE_RELEASE_WQE:
8877 /* Process the WQ release event */
8878 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008879 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008880 break;
8881 case CQE_CODE_XRI_ABORTED:
8882 /* Process the WQ XRI abort event */
8883 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008884 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008885 break;
James Smart4d9ab992009-10-02 15:16:39 -04008886 case CQE_CODE_RECEIVE:
8887 /* Process the RQ event */
8888 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008889 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008890 break;
James Smart4f774512009-05-22 14:52:35 -04008891 default:
8892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8893 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008894 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008895 break;
8896 }
8897 return workposted;
8898}
8899
8900/**
James Smart4f774512009-05-22 14:52:35 -04008901 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8902 * @phba: Pointer to HBA context object.
8903 * @eqe: Pointer to fast-path event queue entry.
8904 *
8905 * This routine process a event queue entry from the slow-path event queue.
8906 * It will check the MajorCode and MinorCode to determine this is for a
8907 * completion event on a completion queue, if not, an error shall be logged
8908 * and just return. Otherwise, it will get to the corresponding completion
8909 * queue and process all the entries on that completion queue, rearm the
8910 * completion queue, and then return.
8911 *
8912 **/
8913static void
8914lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8915{
8916 struct lpfc_queue *cq = NULL, *childq, *speq;
8917 struct lpfc_cqe *cqe;
8918 bool workposted = false;
8919 int ecount = 0;
8920 uint16_t cqid;
8921
8922 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8923 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8924 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8925 "0359 Not a valid slow-path completion "
8926 "event: majorcode=x%x, minorcode=x%x\n",
8927 bf_get(lpfc_eqe_major_code, eqe),
8928 bf_get(lpfc_eqe_minor_code, eqe));
8929 return;
8930 }
8931
8932 /* Get the reference to the corresponding CQ */
8933 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8934
8935 /* Search for completion queue pointer matching this cqid */
8936 speq = phba->sli4_hba.sp_eq;
8937 list_for_each_entry(childq, &speq->child_list, list) {
8938 if (childq->queue_id == cqid) {
8939 cq = childq;
8940 break;
8941 }
8942 }
8943 if (unlikely(!cq)) {
8944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8945 "0365 Slow-path CQ identifier (%d) does "
8946 "not exist\n", cqid);
8947 return;
8948 }
8949
8950 /* Process all the entries to the CQ */
8951 switch (cq->type) {
8952 case LPFC_MCQ:
8953 while ((cqe = lpfc_sli4_cq_get(cq))) {
8954 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8955 if (!(++ecount % LPFC_GET_QE_REL_INT))
8956 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8957 }
8958 break;
8959 case LPFC_WCQ:
8960 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04008961 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04008962 if (!(++ecount % LPFC_GET_QE_REL_INT))
8963 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8964 }
8965 break;
8966 default:
8967 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8968 "0370 Invalid completion queue type (%d)\n",
8969 cq->type);
8970 return;
8971 }
8972
8973 /* Catch the no cq entry condition, log an error */
8974 if (unlikely(ecount == 0))
8975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8976 "0371 No entry from the CQ: identifier "
8977 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8978
8979 /* In any case, flash and re-arm the RCQ */
8980 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8981
8982 /* wake up worker thread if there are works to be done */
8983 if (workposted)
8984 lpfc_worker_wake_up(phba);
8985}
8986
8987/**
8988 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8989 * @eqe: Pointer to fast-path completion queue entry.
8990 *
8991 * This routine process a fast-path work queue completion entry from fast-path
8992 * event queue for FCP command response completion.
8993 **/
8994static void
8995lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8996 struct lpfc_wcqe_complete *wcqe)
8997{
8998 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8999 struct lpfc_iocbq *cmdiocbq;
9000 struct lpfc_iocbq irspiocbq;
9001 unsigned long iflags;
9002
9003 spin_lock_irqsave(&phba->hbalock, iflags);
9004 pring->stats.iocb_event++;
9005 spin_unlock_irqrestore(&phba->hbalock, iflags);
9006
9007 /* Check for response status */
9008 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9009 /* If resource errors reported from HBA, reduce queue
9010 * depth of the SCSI device.
9011 */
9012 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9013 IOSTAT_LOCAL_REJECT) &&
9014 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9015 phba->lpfc_rampdown_queue_depth(phba);
9016 }
9017 /* Log the error status */
9018 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9019 "0373 FCP complete error: status=x%x, "
9020 "hw_status=x%x, total_data_specified=%d, "
9021 "parameter=x%x, word3=x%x\n",
9022 bf_get(lpfc_wcqe_c_status, wcqe),
9023 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9024 wcqe->total_data_placed, wcqe->parameter,
9025 wcqe->word3);
9026 }
9027
9028 /* Look up the FCP command IOCB and create pseudo response IOCB */
9029 spin_lock_irqsave(&phba->hbalock, iflags);
9030 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9031 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9032 spin_unlock_irqrestore(&phba->hbalock, iflags);
9033 if (unlikely(!cmdiocbq)) {
9034 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9035 "0374 FCP complete with no corresponding "
9036 "cmdiocb: iotag (%d)\n",
9037 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9038 return;
9039 }
9040 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9041 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9042 "0375 FCP cmdiocb not callback function "
9043 "iotag: (%d)\n",
9044 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9045 return;
9046 }
9047
9048 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009049 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009050
9051 /* Pass the cmd_iocb and the rsp state to the upper layer */
9052 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9053}
9054
9055/**
9056 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9057 * @phba: Pointer to HBA context object.
9058 * @cq: Pointer to completion queue.
9059 * @wcqe: Pointer to work-queue completion queue entry.
9060 *
9061 * This routine handles an fast-path WQ entry comsumed event by invoking the
9062 * proper WQ release routine to the slow-path WQ.
9063 **/
9064static void
9065lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9066 struct lpfc_wcqe_release *wcqe)
9067{
9068 struct lpfc_queue *childwq;
9069 bool wqid_matched = false;
9070 uint16_t fcp_wqid;
9071
9072 /* Check for fast-path FCP work queue release */
9073 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9074 list_for_each_entry(childwq, &cq->child_list, list) {
9075 if (childwq->queue_id == fcp_wqid) {
9076 lpfc_sli4_wq_release(childwq,
9077 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9078 wqid_matched = true;
9079 break;
9080 }
9081 }
9082 /* Report warning log message if no match found */
9083 if (wqid_matched != true)
9084 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9085 "2580 Fast-path wqe consume event carries "
9086 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9087}
9088
9089/**
9090 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9091 * @cq: Pointer to the completion queue.
9092 * @eqe: Pointer to fast-path completion queue entry.
9093 *
9094 * This routine process a fast-path work queue completion entry from fast-path
9095 * event queue for FCP command response completion.
9096 **/
9097static int
9098lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9099 struct lpfc_cqe *cqe)
9100{
9101 struct lpfc_wcqe_release wcqe;
9102 bool workposted = false;
9103
9104 /* Copy the work queue CQE and convert endian order if needed */
9105 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9106
9107 /* Check and process for different type of WCQE and dispatch */
9108 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9109 case CQE_CODE_COMPL_WQE:
9110 /* Process the WQ complete event */
9111 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9112 (struct lpfc_wcqe_complete *)&wcqe);
9113 break;
9114 case CQE_CODE_RELEASE_WQE:
9115 /* Process the WQ release event */
9116 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9117 (struct lpfc_wcqe_release *)&wcqe);
9118 break;
9119 case CQE_CODE_XRI_ABORTED:
9120 /* Process the WQ XRI abort event */
9121 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9122 (struct sli4_wcqe_xri_aborted *)&wcqe);
9123 break;
9124 default:
9125 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9126 "0144 Not a valid WCQE code: x%x\n",
9127 bf_get(lpfc_wcqe_c_code, &wcqe));
9128 break;
9129 }
9130 return workposted;
9131}
9132
9133/**
9134 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9135 * @phba: Pointer to HBA context object.
9136 * @eqe: Pointer to fast-path event queue entry.
9137 *
9138 * This routine process a event queue entry from the fast-path event queue.
9139 * It will check the MajorCode and MinorCode to determine this is for a
9140 * completion event on a completion queue, if not, an error shall be logged
9141 * and just return. Otherwise, it will get to the corresponding completion
9142 * queue and process all the entries on the completion queue, rearm the
9143 * completion queue, and then return.
9144 **/
9145static void
9146lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9147 uint32_t fcp_cqidx)
9148{
9149 struct lpfc_queue *cq;
9150 struct lpfc_cqe *cqe;
9151 bool workposted = false;
9152 uint16_t cqid;
9153 int ecount = 0;
9154
9155 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9156 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9158 "0366 Not a valid fast-path completion "
9159 "event: majorcode=x%x, minorcode=x%x\n",
9160 bf_get(lpfc_eqe_major_code, eqe),
9161 bf_get(lpfc_eqe_minor_code, eqe));
9162 return;
9163 }
9164
9165 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9166 if (unlikely(!cq)) {
9167 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9168 "0367 Fast-path completion queue does not "
9169 "exist\n");
9170 return;
9171 }
9172
9173 /* Get the reference to the corresponding CQ */
9174 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9175 if (unlikely(cqid != cq->queue_id)) {
9176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9177 "0368 Miss-matched fast-path completion "
9178 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9179 cqid, cq->queue_id);
9180 return;
9181 }
9182
9183 /* Process all the entries to the CQ */
9184 while ((cqe = lpfc_sli4_cq_get(cq))) {
9185 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9186 if (!(++ecount % LPFC_GET_QE_REL_INT))
9187 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9188 }
9189
9190 /* Catch the no cq entry condition */
9191 if (unlikely(ecount == 0))
9192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9193 "0369 No entry from fast-path completion "
9194 "queue fcpcqid=%d\n", cq->queue_id);
9195
9196 /* In any case, flash and re-arm the CQ */
9197 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9198
9199 /* wake up worker thread if there are works to be done */
9200 if (workposted)
9201 lpfc_worker_wake_up(phba);
9202}
9203
9204static void
9205lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9206{
9207 struct lpfc_eqe *eqe;
9208
9209 /* walk all the EQ entries and drop on the floor */
9210 while ((eqe = lpfc_sli4_eq_get(eq)))
9211 ;
9212
9213 /* Clear and re-arm the EQ */
9214 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9215}
9216
9217/**
9218 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9219 * @irq: Interrupt number.
9220 * @dev_id: The device context pointer.
9221 *
9222 * This function is directly called from the PCI layer as an interrupt
9223 * service routine when device with SLI-4 interface spec is enabled with
9224 * MSI-X multi-message interrupt mode and there are slow-path events in
9225 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9226 * interrupt mode, this function is called as part of the device-level
9227 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9228 * undergoing initialization, the interrupt handler will not process the
9229 * interrupt. The link attention and ELS ring attention events are handled
9230 * by the worker thread. The interrupt handler signals the worker thread
9231 * and returns for these events. This function is called without any lock
9232 * held. It gets the hbalock to access and update SLI data structures.
9233 *
9234 * This function returns IRQ_HANDLED when interrupt is handled else it
9235 * returns IRQ_NONE.
9236 **/
9237irqreturn_t
9238lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9239{
9240 struct lpfc_hba *phba;
9241 struct lpfc_queue *speq;
9242 struct lpfc_eqe *eqe;
9243 unsigned long iflag;
9244 int ecount = 0;
9245
9246 /*
9247 * Get the driver's phba structure from the dev_id
9248 */
9249 phba = (struct lpfc_hba *)dev_id;
9250
9251 if (unlikely(!phba))
9252 return IRQ_NONE;
9253
9254 /* Get to the EQ struct associated with this vector */
9255 speq = phba->sli4_hba.sp_eq;
9256
9257 /* Check device state for handling interrupt */
9258 if (unlikely(lpfc_intr_state_check(phba))) {
9259 /* Check again for link_state with lock held */
9260 spin_lock_irqsave(&phba->hbalock, iflag);
9261 if (phba->link_state < LPFC_LINK_DOWN)
9262 /* Flush, clear interrupt, and rearm the EQ */
9263 lpfc_sli4_eq_flush(phba, speq);
9264 spin_unlock_irqrestore(&phba->hbalock, iflag);
9265 return IRQ_NONE;
9266 }
9267
9268 /*
9269 * Process all the event on FCP slow-path EQ
9270 */
9271 while ((eqe = lpfc_sli4_eq_get(speq))) {
9272 lpfc_sli4_sp_handle_eqe(phba, eqe);
9273 if (!(++ecount % LPFC_GET_QE_REL_INT))
9274 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9275 }
9276
9277 /* Always clear and re-arm the slow-path EQ */
9278 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9279
9280 /* Catch the no cq entry condition */
9281 if (unlikely(ecount == 0)) {
9282 if (phba->intr_type == MSIX)
9283 /* MSI-X treated interrupt served as no EQ share INT */
9284 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9285 "0357 MSI-X interrupt with no EQE\n");
9286 else
9287 /* Non MSI-X treated on interrupt as EQ share INT */
9288 return IRQ_NONE;
9289 }
9290
9291 return IRQ_HANDLED;
9292} /* lpfc_sli4_sp_intr_handler */
9293
9294/**
9295 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9296 * @irq: Interrupt number.
9297 * @dev_id: The device context pointer.
9298 *
9299 * This function is directly called from the PCI layer as an interrupt
9300 * service routine when device with SLI-4 interface spec is enabled with
9301 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9302 * ring event in the HBA. However, when the device is enabled with either
9303 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9304 * device-level interrupt handler. When the PCI slot is in error recovery
9305 * or the HBA is undergoing initialization, the interrupt handler will not
9306 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9307 * the intrrupt context. This function is called without any lock held.
9308 * It gets the hbalock to access and update SLI data structures. Note that,
9309 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9310 * equal to that of FCP CQ index.
9311 *
9312 * This function returns IRQ_HANDLED when interrupt is handled else it
9313 * returns IRQ_NONE.
9314 **/
9315irqreturn_t
9316lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9317{
9318 struct lpfc_hba *phba;
9319 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9320 struct lpfc_queue *fpeq;
9321 struct lpfc_eqe *eqe;
9322 unsigned long iflag;
9323 int ecount = 0;
9324 uint32_t fcp_eqidx;
9325
9326 /* Get the driver's phba structure from the dev_id */
9327 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9328 phba = fcp_eq_hdl->phba;
9329 fcp_eqidx = fcp_eq_hdl->idx;
9330
9331 if (unlikely(!phba))
9332 return IRQ_NONE;
9333
9334 /* Get to the EQ struct associated with this vector */
9335 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9336
9337 /* Check device state for handling interrupt */
9338 if (unlikely(lpfc_intr_state_check(phba))) {
9339 /* Check again for link_state with lock held */
9340 spin_lock_irqsave(&phba->hbalock, iflag);
9341 if (phba->link_state < LPFC_LINK_DOWN)
9342 /* Flush, clear interrupt, and rearm the EQ */
9343 lpfc_sli4_eq_flush(phba, fpeq);
9344 spin_unlock_irqrestore(&phba->hbalock, iflag);
9345 return IRQ_NONE;
9346 }
9347
9348 /*
9349 * Process all the event on FCP fast-path EQ
9350 */
9351 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9352 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9353 if (!(++ecount % LPFC_GET_QE_REL_INT))
9354 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9355 }
9356
9357 /* Always clear and re-arm the fast-path EQ */
9358 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9359
9360 if (unlikely(ecount == 0)) {
9361 if (phba->intr_type == MSIX)
9362 /* MSI-X treated interrupt served as no EQ share INT */
9363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9364 "0358 MSI-X interrupt with no EQE\n");
9365 else
9366 /* Non MSI-X treated on interrupt as EQ share INT */
9367 return IRQ_NONE;
9368 }
9369
9370 return IRQ_HANDLED;
9371} /* lpfc_sli4_fp_intr_handler */
9372
9373/**
9374 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9375 * @irq: Interrupt number.
9376 * @dev_id: The device context pointer.
9377 *
9378 * This function is the device-level interrupt handler to device with SLI-4
9379 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9380 * interrupt mode is enabled and there is an event in the HBA which requires
9381 * driver attention. This function invokes the slow-path interrupt attention
9382 * handling function and fast-path interrupt attention handling function in
9383 * turn to process the relevant HBA attention events. This function is called
9384 * without any lock held. It gets the hbalock to access and update SLI data
9385 * structures.
9386 *
9387 * This function returns IRQ_HANDLED when interrupt is handled, else it
9388 * returns IRQ_NONE.
9389 **/
9390irqreturn_t
9391lpfc_sli4_intr_handler(int irq, void *dev_id)
9392{
9393 struct lpfc_hba *phba;
9394 irqreturn_t sp_irq_rc, fp_irq_rc;
9395 bool fp_handled = false;
9396 uint32_t fcp_eqidx;
9397
9398 /* Get the driver's phba structure from the dev_id */
9399 phba = (struct lpfc_hba *)dev_id;
9400
9401 if (unlikely(!phba))
9402 return IRQ_NONE;
9403
9404 /*
9405 * Invokes slow-path host attention interrupt handling as appropriate.
9406 */
9407 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9408
9409 /*
9410 * Invoke fast-path host attention interrupt handling as appropriate.
9411 */
9412 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9413 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9414 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9415 if (fp_irq_rc == IRQ_HANDLED)
9416 fp_handled |= true;
9417 }
9418
9419 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9420} /* lpfc_sli4_intr_handler */
9421
9422/**
9423 * lpfc_sli4_queue_free - free a queue structure and associated memory
9424 * @queue: The queue structure to free.
9425 *
9426 * This function frees a queue structure and the DMAable memeory used for
9427 * the host resident queue. This function must be called after destroying the
9428 * queue on the HBA.
9429 **/
9430void
9431lpfc_sli4_queue_free(struct lpfc_queue *queue)
9432{
9433 struct lpfc_dmabuf *dmabuf;
9434
9435 if (!queue)
9436 return;
9437
9438 while (!list_empty(&queue->page_list)) {
9439 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9440 list);
9441 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9442 dmabuf->virt, dmabuf->phys);
9443 kfree(dmabuf);
9444 }
9445 kfree(queue);
9446 return;
9447}
9448
9449/**
9450 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9451 * @phba: The HBA that this queue is being created on.
9452 * @entry_size: The size of each queue entry for this queue.
9453 * @entry count: The number of entries that this queue will handle.
9454 *
9455 * This function allocates a queue structure and the DMAable memory used for
9456 * the host resident queue. This function must be called before creating the
9457 * queue on the HBA.
9458 **/
9459struct lpfc_queue *
9460lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9461 uint32_t entry_count)
9462{
9463 struct lpfc_queue *queue;
9464 struct lpfc_dmabuf *dmabuf;
9465 int x, total_qe_count;
9466 void *dma_pointer;
9467
9468
9469 queue = kzalloc(sizeof(struct lpfc_queue) +
9470 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9471 if (!queue)
9472 return NULL;
9473 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9474 INIT_LIST_HEAD(&queue->list);
9475 INIT_LIST_HEAD(&queue->page_list);
9476 INIT_LIST_HEAD(&queue->child_list);
9477 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9478 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9479 if (!dmabuf)
9480 goto out_fail;
9481 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9482 PAGE_SIZE, &dmabuf->phys,
9483 GFP_KERNEL);
9484 if (!dmabuf->virt) {
9485 kfree(dmabuf);
9486 goto out_fail;
9487 }
James Smartd11e31d2009-06-10 17:23:06 -04009488 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009489 dmabuf->buffer_tag = x;
9490 list_add_tail(&dmabuf->list, &queue->page_list);
9491 /* initialize queue's entry array */
9492 dma_pointer = dmabuf->virt;
9493 for (; total_qe_count < entry_count &&
9494 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9495 total_qe_count++, dma_pointer += entry_size) {
9496 queue->qe[total_qe_count].address = dma_pointer;
9497 }
9498 }
9499 queue->entry_size = entry_size;
9500 queue->entry_count = entry_count;
9501 queue->phba = phba;
9502
9503 return queue;
9504out_fail:
9505 lpfc_sli4_queue_free(queue);
9506 return NULL;
9507}
9508
9509/**
9510 * lpfc_eq_create - Create an Event Queue on the HBA
9511 * @phba: HBA structure that indicates port to create a queue on.
9512 * @eq: The queue structure to use to create the event queue.
9513 * @imax: The maximum interrupt per second limit.
9514 *
9515 * This function creates an event queue, as detailed in @eq, on a port,
9516 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9517 *
9518 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9519 * is used to get the entry count and entry size that are necessary to
9520 * determine the number of pages to allocate and use for this queue. This
9521 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9522 * event queue. This function is asynchronous and will wait for the mailbox
9523 * command to finish before continuing.
9524 *
9525 * On success this function will return a zero. If unable to allocate enough
9526 * memory this function will return ENOMEM. If the queue create mailbox command
9527 * fails this function will return ENXIO.
9528 **/
9529uint32_t
9530lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9531{
9532 struct lpfc_mbx_eq_create *eq_create;
9533 LPFC_MBOXQ_t *mbox;
9534 int rc, length, status = 0;
9535 struct lpfc_dmabuf *dmabuf;
9536 uint32_t shdr_status, shdr_add_status;
9537 union lpfc_sli4_cfg_shdr *shdr;
9538 uint16_t dmult;
9539
9540 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9541 if (!mbox)
9542 return -ENOMEM;
9543 length = (sizeof(struct lpfc_mbx_eq_create) -
9544 sizeof(struct lpfc_sli4_cfg_mhdr));
9545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9546 LPFC_MBOX_OPCODE_EQ_CREATE,
9547 length, LPFC_SLI4_MBX_EMBED);
9548 eq_create = &mbox->u.mqe.un.eq_create;
9549 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9550 eq->page_count);
9551 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9552 LPFC_EQE_SIZE);
9553 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9554 /* Calculate delay multiper from maximum interrupt per second */
9555 dmult = LPFC_DMULT_CONST/imax - 1;
9556 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9557 dmult);
9558 switch (eq->entry_count) {
9559 default:
9560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9561 "0360 Unsupported EQ count. (%d)\n",
9562 eq->entry_count);
9563 if (eq->entry_count < 256)
9564 return -EINVAL;
9565 /* otherwise default to smallest count (drop through) */
9566 case 256:
9567 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9568 LPFC_EQ_CNT_256);
9569 break;
9570 case 512:
9571 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9572 LPFC_EQ_CNT_512);
9573 break;
9574 case 1024:
9575 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9576 LPFC_EQ_CNT_1024);
9577 break;
9578 case 2048:
9579 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9580 LPFC_EQ_CNT_2048);
9581 break;
9582 case 4096:
9583 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9584 LPFC_EQ_CNT_4096);
9585 break;
9586 }
9587 list_for_each_entry(dmabuf, &eq->page_list, list) {
9588 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9589 putPaddrLow(dmabuf->phys);
9590 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9591 putPaddrHigh(dmabuf->phys);
9592 }
9593 mbox->vport = phba->pport;
9594 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9595 mbox->context1 = NULL;
9596 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9597 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9598 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9599 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9600 if (shdr_status || shdr_add_status || rc) {
9601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9602 "2500 EQ_CREATE mailbox failed with "
9603 "status x%x add_status x%x, mbx status x%x\n",
9604 shdr_status, shdr_add_status, rc);
9605 status = -ENXIO;
9606 }
9607 eq->type = LPFC_EQ;
9608 eq->subtype = LPFC_NONE;
9609 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9610 if (eq->queue_id == 0xFFFF)
9611 status = -ENXIO;
9612 eq->host_index = 0;
9613 eq->hba_index = 0;
9614
James Smart8fa38512009-07-19 10:01:03 -04009615 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009616 return status;
9617}
9618
9619/**
9620 * lpfc_cq_create - Create a Completion Queue on the HBA
9621 * @phba: HBA structure that indicates port to create a queue on.
9622 * @cq: The queue structure to use to create the completion queue.
9623 * @eq: The event queue to bind this completion queue to.
9624 *
9625 * This function creates a completion queue, as detailed in @wq, on a port,
9626 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9627 *
9628 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9629 * is used to get the entry count and entry size that are necessary to
9630 * determine the number of pages to allocate and use for this queue. The @eq
9631 * is used to indicate which event queue to bind this completion queue to. This
9632 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9633 * completion queue. This function is asynchronous and will wait for the mailbox
9634 * command to finish before continuing.
9635 *
9636 * On success this function will return a zero. If unable to allocate enough
9637 * memory this function will return ENOMEM. If the queue create mailbox command
9638 * fails this function will return ENXIO.
9639 **/
9640uint32_t
9641lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9642 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9643{
9644 struct lpfc_mbx_cq_create *cq_create;
9645 struct lpfc_dmabuf *dmabuf;
9646 LPFC_MBOXQ_t *mbox;
9647 int rc, length, status = 0;
9648 uint32_t shdr_status, shdr_add_status;
9649 union lpfc_sli4_cfg_shdr *shdr;
9650
9651 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9652 if (!mbox)
9653 return -ENOMEM;
9654 length = (sizeof(struct lpfc_mbx_cq_create) -
9655 sizeof(struct lpfc_sli4_cfg_mhdr));
9656 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9657 LPFC_MBOX_OPCODE_CQ_CREATE,
9658 length, LPFC_SLI4_MBX_EMBED);
9659 cq_create = &mbox->u.mqe.un.cq_create;
9660 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9661 cq->page_count);
9662 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9663 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9664 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9665 switch (cq->entry_count) {
9666 default:
9667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9668 "0361 Unsupported CQ count. (%d)\n",
9669 cq->entry_count);
9670 if (cq->entry_count < 256)
9671 return -EINVAL;
9672 /* otherwise default to smallest count (drop through) */
9673 case 256:
9674 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9675 LPFC_CQ_CNT_256);
9676 break;
9677 case 512:
9678 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9679 LPFC_CQ_CNT_512);
9680 break;
9681 case 1024:
9682 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9683 LPFC_CQ_CNT_1024);
9684 break;
9685 }
9686 list_for_each_entry(dmabuf, &cq->page_list, list) {
9687 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9688 putPaddrLow(dmabuf->phys);
9689 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9690 putPaddrHigh(dmabuf->phys);
9691 }
9692 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9693
9694 /* The IOCTL status is embedded in the mailbox subheader. */
9695 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9696 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9697 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9698 if (shdr_status || shdr_add_status || rc) {
9699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9700 "2501 CQ_CREATE mailbox failed with "
9701 "status x%x add_status x%x, mbx status x%x\n",
9702 shdr_status, shdr_add_status, rc);
9703 status = -ENXIO;
9704 goto out;
9705 }
9706 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9707 if (cq->queue_id == 0xFFFF) {
9708 status = -ENXIO;
9709 goto out;
9710 }
9711 /* link the cq onto the parent eq child list */
9712 list_add_tail(&cq->list, &eq->child_list);
9713 /* Set up completion queue's type and subtype */
9714 cq->type = type;
9715 cq->subtype = subtype;
9716 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9717 cq->host_index = 0;
9718 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009719
James Smart8fa38512009-07-19 10:01:03 -04009720out:
9721 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009722 return status;
9723}
9724
9725/**
James Smart04c68492009-05-22 14:52:52 -04009726 * lpfc_mq_create - Create a mailbox Queue on the HBA
9727 * @phba: HBA structure that indicates port to create a queue on.
9728 * @mq: The queue structure to use to create the mailbox queue.
9729 *
9730 * This function creates a mailbox queue, as detailed in @mq, on a port,
9731 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9732 *
9733 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9734 * is used to get the entry count and entry size that are necessary to
9735 * determine the number of pages to allocate and use for this queue. This
9736 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9737 * mailbox queue. This function is asynchronous and will wait for the mailbox
9738 * command to finish before continuing.
9739 *
9740 * On success this function will return a zero. If unable to allocate enough
9741 * memory this function will return ENOMEM. If the queue create mailbox command
9742 * fails this function will return ENXIO.
9743 **/
9744uint32_t
9745lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9746 struct lpfc_queue *cq, uint32_t subtype)
9747{
9748 struct lpfc_mbx_mq_create *mq_create;
9749 struct lpfc_dmabuf *dmabuf;
9750 LPFC_MBOXQ_t *mbox;
9751 int rc, length, status = 0;
9752 uint32_t shdr_status, shdr_add_status;
9753 union lpfc_sli4_cfg_shdr *shdr;
9754
9755 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9756 if (!mbox)
9757 return -ENOMEM;
9758 length = (sizeof(struct lpfc_mbx_mq_create) -
9759 sizeof(struct lpfc_sli4_cfg_mhdr));
9760 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9761 LPFC_MBOX_OPCODE_MQ_CREATE,
9762 length, LPFC_SLI4_MBX_EMBED);
9763 mq_create = &mbox->u.mqe.un.mq_create;
9764 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9765 mq->page_count);
9766 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9767 cq->queue_id);
9768 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9769 switch (mq->entry_count) {
9770 default:
9771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9772 "0362 Unsupported MQ count. (%d)\n",
9773 mq->entry_count);
9774 if (mq->entry_count < 16)
9775 return -EINVAL;
9776 /* otherwise default to smallest count (drop through) */
9777 case 16:
9778 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9779 LPFC_MQ_CNT_16);
9780 break;
9781 case 32:
9782 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9783 LPFC_MQ_CNT_32);
9784 break;
9785 case 64:
9786 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9787 LPFC_MQ_CNT_64);
9788 break;
9789 case 128:
9790 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9791 LPFC_MQ_CNT_128);
9792 break;
9793 }
9794 list_for_each_entry(dmabuf, &mq->page_list, list) {
9795 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9796 putPaddrLow(dmabuf->phys);
9797 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9798 putPaddrHigh(dmabuf->phys);
9799 }
9800 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9801 /* The IOCTL status is embedded in the mailbox subheader. */
9802 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9803 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9804 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9805 if (shdr_status || shdr_add_status || rc) {
9806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9807 "2502 MQ_CREATE mailbox failed with "
9808 "status x%x add_status x%x, mbx status x%x\n",
9809 shdr_status, shdr_add_status, rc);
9810 status = -ENXIO;
9811 goto out;
9812 }
9813 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9814 if (mq->queue_id == 0xFFFF) {
9815 status = -ENXIO;
9816 goto out;
9817 }
9818 mq->type = LPFC_MQ;
9819 mq->subtype = subtype;
9820 mq->host_index = 0;
9821 mq->hba_index = 0;
9822
9823 /* link the mq onto the parent cq child list */
9824 list_add_tail(&mq->list, &cq->child_list);
9825out:
James Smart8fa38512009-07-19 10:01:03 -04009826 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009827 return status;
9828}
9829
9830/**
James Smart4f774512009-05-22 14:52:35 -04009831 * lpfc_wq_create - Create a Work Queue on the HBA
9832 * @phba: HBA structure that indicates port to create a queue on.
9833 * @wq: The queue structure to use to create the work queue.
9834 * @cq: The completion queue to bind this work queue to.
9835 * @subtype: The subtype of the work queue indicating its functionality.
9836 *
9837 * This function creates a work queue, as detailed in @wq, on a port, described
9838 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9839 *
9840 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9841 * is used to get the entry count and entry size that are necessary to
9842 * determine the number of pages to allocate and use for this queue. The @cq
9843 * is used to indicate which completion queue to bind this work queue to. This
9844 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9845 * work queue. This function is asynchronous and will wait for the mailbox
9846 * command to finish before continuing.
9847 *
9848 * On success this function will return a zero. If unable to allocate enough
9849 * memory this function will return ENOMEM. If the queue create mailbox command
9850 * fails this function will return ENXIO.
9851 **/
9852uint32_t
9853lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9854 struct lpfc_queue *cq, uint32_t subtype)
9855{
9856 struct lpfc_mbx_wq_create *wq_create;
9857 struct lpfc_dmabuf *dmabuf;
9858 LPFC_MBOXQ_t *mbox;
9859 int rc, length, status = 0;
9860 uint32_t shdr_status, shdr_add_status;
9861 union lpfc_sli4_cfg_shdr *shdr;
9862
9863 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9864 if (!mbox)
9865 return -ENOMEM;
9866 length = (sizeof(struct lpfc_mbx_wq_create) -
9867 sizeof(struct lpfc_sli4_cfg_mhdr));
9868 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9869 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9870 length, LPFC_SLI4_MBX_EMBED);
9871 wq_create = &mbox->u.mqe.un.wq_create;
9872 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9873 wq->page_count);
9874 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9875 cq->queue_id);
9876 list_for_each_entry(dmabuf, &wq->page_list, list) {
9877 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9878 putPaddrLow(dmabuf->phys);
9879 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9880 putPaddrHigh(dmabuf->phys);
9881 }
9882 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9883 /* The IOCTL status is embedded in the mailbox subheader. */
9884 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9885 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9886 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9887 if (shdr_status || shdr_add_status || rc) {
9888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9889 "2503 WQ_CREATE mailbox failed with "
9890 "status x%x add_status x%x, mbx status x%x\n",
9891 shdr_status, shdr_add_status, rc);
9892 status = -ENXIO;
9893 goto out;
9894 }
9895 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9896 if (wq->queue_id == 0xFFFF) {
9897 status = -ENXIO;
9898 goto out;
9899 }
9900 wq->type = LPFC_WQ;
9901 wq->subtype = subtype;
9902 wq->host_index = 0;
9903 wq->hba_index = 0;
9904
9905 /* link the wq onto the parent cq child list */
9906 list_add_tail(&wq->list, &cq->child_list);
9907out:
James Smart8fa38512009-07-19 10:01:03 -04009908 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009909 return status;
9910}
9911
9912/**
9913 * lpfc_rq_create - Create a Receive Queue on the HBA
9914 * @phba: HBA structure that indicates port to create a queue on.
9915 * @hrq: The queue structure to use to create the header receive queue.
9916 * @drq: The queue structure to use to create the data receive queue.
9917 * @cq: The completion queue to bind this work queue to.
9918 *
9919 * This function creates a receive buffer queue pair , as detailed in @hrq and
9920 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9921 * to the HBA.
9922 *
9923 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9924 * struct is used to get the entry count that is necessary to determine the
9925 * number of pages to use for this queue. The @cq is used to indicate which
9926 * completion queue to bind received buffers that are posted to these queues to.
9927 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9928 * receive queue pair. This function is asynchronous and will wait for the
9929 * mailbox command to finish before continuing.
9930 *
9931 * On success this function will return a zero. If unable to allocate enough
9932 * memory this function will return ENOMEM. If the queue create mailbox command
9933 * fails this function will return ENXIO.
9934 **/
9935uint32_t
9936lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9937 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9938{
9939 struct lpfc_mbx_rq_create *rq_create;
9940 struct lpfc_dmabuf *dmabuf;
9941 LPFC_MBOXQ_t *mbox;
9942 int rc, length, status = 0;
9943 uint32_t shdr_status, shdr_add_status;
9944 union lpfc_sli4_cfg_shdr *shdr;
9945
9946 if (hrq->entry_count != drq->entry_count)
9947 return -EINVAL;
9948 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9949 if (!mbox)
9950 return -ENOMEM;
9951 length = (sizeof(struct lpfc_mbx_rq_create) -
9952 sizeof(struct lpfc_sli4_cfg_mhdr));
9953 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9954 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9955 length, LPFC_SLI4_MBX_EMBED);
9956 rq_create = &mbox->u.mqe.un.rq_create;
9957 switch (hrq->entry_count) {
9958 default:
9959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9960 "2535 Unsupported RQ count. (%d)\n",
9961 hrq->entry_count);
9962 if (hrq->entry_count < 512)
9963 return -EINVAL;
9964 /* otherwise default to smallest count (drop through) */
9965 case 512:
9966 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9967 LPFC_RQ_RING_SIZE_512);
9968 break;
9969 case 1024:
9970 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9971 LPFC_RQ_RING_SIZE_1024);
9972 break;
9973 case 2048:
9974 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9975 LPFC_RQ_RING_SIZE_2048);
9976 break;
9977 case 4096:
9978 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9979 LPFC_RQ_RING_SIZE_4096);
9980 break;
9981 }
9982 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9983 cq->queue_id);
9984 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9985 hrq->page_count);
9986 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9987 LPFC_HDR_BUF_SIZE);
9988 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9989 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9990 putPaddrLow(dmabuf->phys);
9991 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9992 putPaddrHigh(dmabuf->phys);
9993 }
9994 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9995 /* The IOCTL status is embedded in the mailbox subheader. */
9996 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9997 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9998 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9999 if (shdr_status || shdr_add_status || rc) {
10000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10001 "2504 RQ_CREATE mailbox failed with "
10002 "status x%x add_status x%x, mbx status x%x\n",
10003 shdr_status, shdr_add_status, rc);
10004 status = -ENXIO;
10005 goto out;
10006 }
10007 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10008 if (hrq->queue_id == 0xFFFF) {
10009 status = -ENXIO;
10010 goto out;
10011 }
10012 hrq->type = LPFC_HRQ;
10013 hrq->subtype = subtype;
10014 hrq->host_index = 0;
10015 hrq->hba_index = 0;
10016
10017 /* now create the data queue */
10018 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10019 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10020 length, LPFC_SLI4_MBX_EMBED);
10021 switch (drq->entry_count) {
10022 default:
10023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10024 "2536 Unsupported RQ count. (%d)\n",
10025 drq->entry_count);
10026 if (drq->entry_count < 512)
10027 return -EINVAL;
10028 /* otherwise default to smallest count (drop through) */
10029 case 512:
10030 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10031 LPFC_RQ_RING_SIZE_512);
10032 break;
10033 case 1024:
10034 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10035 LPFC_RQ_RING_SIZE_1024);
10036 break;
10037 case 2048:
10038 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10039 LPFC_RQ_RING_SIZE_2048);
10040 break;
10041 case 4096:
10042 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10043 LPFC_RQ_RING_SIZE_4096);
10044 break;
10045 }
10046 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10047 cq->queue_id);
10048 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10049 drq->page_count);
10050 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10051 LPFC_DATA_BUF_SIZE);
10052 list_for_each_entry(dmabuf, &drq->page_list, list) {
10053 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10054 putPaddrLow(dmabuf->phys);
10055 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10056 putPaddrHigh(dmabuf->phys);
10057 }
10058 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10059 /* The IOCTL status is embedded in the mailbox subheader. */
10060 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10061 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10062 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10063 if (shdr_status || shdr_add_status || rc) {
10064 status = -ENXIO;
10065 goto out;
10066 }
10067 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10068 if (drq->queue_id == 0xFFFF) {
10069 status = -ENXIO;
10070 goto out;
10071 }
10072 drq->type = LPFC_DRQ;
10073 drq->subtype = subtype;
10074 drq->host_index = 0;
10075 drq->hba_index = 0;
10076
10077 /* link the header and data RQs onto the parent cq child list */
10078 list_add_tail(&hrq->list, &cq->child_list);
10079 list_add_tail(&drq->list, &cq->child_list);
10080
10081out:
James Smart8fa38512009-07-19 10:01:03 -040010082 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010083 return status;
10084}
10085
10086/**
10087 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10088 * @eq: The queue structure associated with the queue to destroy.
10089 *
10090 * This function destroys a queue, as detailed in @eq by sending an mailbox
10091 * command, specific to the type of queue, to the HBA.
10092 *
10093 * The @eq struct is used to get the queue ID of the queue to destroy.
10094 *
10095 * On success this function will return a zero. If the queue destroy mailbox
10096 * command fails this function will return ENXIO.
10097 **/
10098uint32_t
10099lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10100{
10101 LPFC_MBOXQ_t *mbox;
10102 int rc, length, status = 0;
10103 uint32_t shdr_status, shdr_add_status;
10104 union lpfc_sli4_cfg_shdr *shdr;
10105
10106 if (!eq)
10107 return -ENODEV;
10108 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10109 if (!mbox)
10110 return -ENOMEM;
10111 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10112 sizeof(struct lpfc_sli4_cfg_mhdr));
10113 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10114 LPFC_MBOX_OPCODE_EQ_DESTROY,
10115 length, LPFC_SLI4_MBX_EMBED);
10116 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10117 eq->queue_id);
10118 mbox->vport = eq->phba->pport;
10119 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10120
10121 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10122 /* The IOCTL status is embedded in the mailbox subheader. */
10123 shdr = (union lpfc_sli4_cfg_shdr *)
10124 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10125 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10126 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10127 if (shdr_status || shdr_add_status || rc) {
10128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10129 "2505 EQ_DESTROY mailbox failed with "
10130 "status x%x add_status x%x, mbx status x%x\n",
10131 shdr_status, shdr_add_status, rc);
10132 status = -ENXIO;
10133 }
10134
10135 /* Remove eq from any list */
10136 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010137 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010138 return status;
10139}
10140
10141/**
10142 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10143 * @cq: The queue structure associated with the queue to destroy.
10144 *
10145 * This function destroys a queue, as detailed in @cq by sending an mailbox
10146 * command, specific to the type of queue, to the HBA.
10147 *
10148 * The @cq struct is used to get the queue ID of the queue to destroy.
10149 *
10150 * On success this function will return a zero. If the queue destroy mailbox
10151 * command fails this function will return ENXIO.
10152 **/
10153uint32_t
10154lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10155{
10156 LPFC_MBOXQ_t *mbox;
10157 int rc, length, status = 0;
10158 uint32_t shdr_status, shdr_add_status;
10159 union lpfc_sli4_cfg_shdr *shdr;
10160
10161 if (!cq)
10162 return -ENODEV;
10163 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10164 if (!mbox)
10165 return -ENOMEM;
10166 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10167 sizeof(struct lpfc_sli4_cfg_mhdr));
10168 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10169 LPFC_MBOX_OPCODE_CQ_DESTROY,
10170 length, LPFC_SLI4_MBX_EMBED);
10171 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10172 cq->queue_id);
10173 mbox->vport = cq->phba->pport;
10174 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10175 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10176 /* The IOCTL status is embedded in the mailbox subheader. */
10177 shdr = (union lpfc_sli4_cfg_shdr *)
10178 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10179 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10180 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10181 if (shdr_status || shdr_add_status || rc) {
10182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10183 "2506 CQ_DESTROY mailbox failed with "
10184 "status x%x add_status x%x, mbx status x%x\n",
10185 shdr_status, shdr_add_status, rc);
10186 status = -ENXIO;
10187 }
10188 /* Remove cq from any list */
10189 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010190 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010191 return status;
10192}
10193
10194/**
James Smart04c68492009-05-22 14:52:52 -040010195 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10196 * @qm: The queue structure associated with the queue to destroy.
10197 *
10198 * This function destroys a queue, as detailed in @mq by sending an mailbox
10199 * command, specific to the type of queue, to the HBA.
10200 *
10201 * The @mq struct is used to get the queue ID of the queue to destroy.
10202 *
10203 * On success this function will return a zero. If the queue destroy mailbox
10204 * command fails this function will return ENXIO.
10205 **/
10206uint32_t
10207lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10208{
10209 LPFC_MBOXQ_t *mbox;
10210 int rc, length, status = 0;
10211 uint32_t shdr_status, shdr_add_status;
10212 union lpfc_sli4_cfg_shdr *shdr;
10213
10214 if (!mq)
10215 return -ENODEV;
10216 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10217 if (!mbox)
10218 return -ENOMEM;
10219 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10220 sizeof(struct lpfc_sli4_cfg_mhdr));
10221 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10222 LPFC_MBOX_OPCODE_MQ_DESTROY,
10223 length, LPFC_SLI4_MBX_EMBED);
10224 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10225 mq->queue_id);
10226 mbox->vport = mq->phba->pport;
10227 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10228 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10229 /* The IOCTL status is embedded in the mailbox subheader. */
10230 shdr = (union lpfc_sli4_cfg_shdr *)
10231 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10232 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10233 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10234 if (shdr_status || shdr_add_status || rc) {
10235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10236 "2507 MQ_DESTROY mailbox failed with "
10237 "status x%x add_status x%x, mbx status x%x\n",
10238 shdr_status, shdr_add_status, rc);
10239 status = -ENXIO;
10240 }
10241 /* Remove mq from any list */
10242 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010243 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010244 return status;
10245}
10246
10247/**
James Smart4f774512009-05-22 14:52:35 -040010248 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10249 * @wq: The queue structure associated with the queue to destroy.
10250 *
10251 * This function destroys a queue, as detailed in @wq by sending an mailbox
10252 * command, specific to the type of queue, to the HBA.
10253 *
10254 * The @wq struct is used to get the queue ID of the queue to destroy.
10255 *
10256 * On success this function will return a zero. If the queue destroy mailbox
10257 * command fails this function will return ENXIO.
10258 **/
10259uint32_t
10260lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10261{
10262 LPFC_MBOXQ_t *mbox;
10263 int rc, length, status = 0;
10264 uint32_t shdr_status, shdr_add_status;
10265 union lpfc_sli4_cfg_shdr *shdr;
10266
10267 if (!wq)
10268 return -ENODEV;
10269 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10270 if (!mbox)
10271 return -ENOMEM;
10272 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10273 sizeof(struct lpfc_sli4_cfg_mhdr));
10274 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10275 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10276 length, LPFC_SLI4_MBX_EMBED);
10277 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10278 wq->queue_id);
10279 mbox->vport = wq->phba->pport;
10280 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10281 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10282 shdr = (union lpfc_sli4_cfg_shdr *)
10283 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10284 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10285 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10286 if (shdr_status || shdr_add_status || rc) {
10287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10288 "2508 WQ_DESTROY mailbox failed with "
10289 "status x%x add_status x%x, mbx status x%x\n",
10290 shdr_status, shdr_add_status, rc);
10291 status = -ENXIO;
10292 }
10293 /* Remove wq from any list */
10294 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010295 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010296 return status;
10297}
10298
10299/**
10300 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10301 * @rq: The queue structure associated with the queue to destroy.
10302 *
10303 * This function destroys a queue, as detailed in @rq by sending an mailbox
10304 * command, specific to the type of queue, to the HBA.
10305 *
10306 * The @rq struct is used to get the queue ID of the queue to destroy.
10307 *
10308 * On success this function will return a zero. If the queue destroy mailbox
10309 * command fails this function will return ENXIO.
10310 **/
10311uint32_t
10312lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10313 struct lpfc_queue *drq)
10314{
10315 LPFC_MBOXQ_t *mbox;
10316 int rc, length, status = 0;
10317 uint32_t shdr_status, shdr_add_status;
10318 union lpfc_sli4_cfg_shdr *shdr;
10319
10320 if (!hrq || !drq)
10321 return -ENODEV;
10322 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10323 if (!mbox)
10324 return -ENOMEM;
10325 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10326 sizeof(struct mbox_header));
10327 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10328 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10329 length, LPFC_SLI4_MBX_EMBED);
10330 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10331 hrq->queue_id);
10332 mbox->vport = hrq->phba->pport;
10333 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10334 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10335 /* The IOCTL status is embedded in the mailbox subheader. */
10336 shdr = (union lpfc_sli4_cfg_shdr *)
10337 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10338 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10339 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10340 if (shdr_status || shdr_add_status || rc) {
10341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10342 "2509 RQ_DESTROY mailbox failed with "
10343 "status x%x add_status x%x, mbx status x%x\n",
10344 shdr_status, shdr_add_status, rc);
10345 if (rc != MBX_TIMEOUT)
10346 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10347 return -ENXIO;
10348 }
10349 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10350 drq->queue_id);
10351 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10352 shdr = (union lpfc_sli4_cfg_shdr *)
10353 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10354 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10355 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10356 if (shdr_status || shdr_add_status || rc) {
10357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10358 "2510 RQ_DESTROY mailbox failed with "
10359 "status x%x add_status x%x, mbx status x%x\n",
10360 shdr_status, shdr_add_status, rc);
10361 status = -ENXIO;
10362 }
10363 list_del_init(&hrq->list);
10364 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010365 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010366 return status;
10367}
10368
10369/**
10370 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10371 * @phba: The virtual port for which this call being executed.
10372 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10373 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10374 * @xritag: the xritag that ties this io to the SGL pages.
10375 *
10376 * This routine will post the sgl pages for the IO that has the xritag
10377 * that is in the iocbq structure. The xritag is assigned during iocbq
10378 * creation and persists for as long as the driver is loaded.
10379 * if the caller has fewer than 256 scatter gather segments to map then
10380 * pdma_phys_addr1 should be 0.
10381 * If the caller needs to map more than 256 scatter gather segment then
10382 * pdma_phys_addr1 should be a valid physical address.
10383 * physical address for SGLs must be 64 byte aligned.
10384 * If you are going to map 2 SGL's then the first one must have 256 entries
10385 * the second sgl can have between 1 and 256 entries.
10386 *
10387 * Return codes:
10388 * 0 - Success
10389 * -ENXIO, -ENOMEM - Failure
10390 **/
10391int
10392lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10393 dma_addr_t pdma_phys_addr0,
10394 dma_addr_t pdma_phys_addr1,
10395 uint16_t xritag)
10396{
10397 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10398 LPFC_MBOXQ_t *mbox;
10399 int rc;
10400 uint32_t shdr_status, shdr_add_status;
10401 union lpfc_sli4_cfg_shdr *shdr;
10402
10403 if (xritag == NO_XRI) {
10404 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10405 "0364 Invalid param:\n");
10406 return -EINVAL;
10407 }
10408
10409 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10410 if (!mbox)
10411 return -ENOMEM;
10412
10413 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10414 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10415 sizeof(struct lpfc_mbx_post_sgl_pages) -
10416 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10417
10418 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10419 &mbox->u.mqe.un.post_sgl_pages;
10420 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10421 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10422
10423 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10424 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10425 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10426 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10427
10428 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10429 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10430 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10431 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10432 if (!phba->sli4_hba.intr_enable)
10433 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10434 else
10435 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10436 /* The IOCTL status is embedded in the mailbox subheader. */
10437 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10438 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10439 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10440 if (rc != MBX_TIMEOUT)
10441 mempool_free(mbox, phba->mbox_mem_pool);
10442 if (shdr_status || shdr_add_status || rc) {
10443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10444 "2511 POST_SGL mailbox failed with "
10445 "status x%x add_status x%x, mbx status x%x\n",
10446 shdr_status, shdr_add_status, rc);
10447 rc = -ENXIO;
10448 }
10449 return 0;
10450}
10451/**
10452 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10453 * @phba: The virtual port for which this call being executed.
10454 *
10455 * This routine will remove all of the sgl pages registered with the hba.
10456 *
10457 * Return codes:
10458 * 0 - Success
10459 * -ENXIO, -ENOMEM - Failure
10460 **/
10461int
10462lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10463{
10464 LPFC_MBOXQ_t *mbox;
10465 int rc;
10466 uint32_t shdr_status, shdr_add_status;
10467 union lpfc_sli4_cfg_shdr *shdr;
10468
10469 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10470 if (!mbox)
10471 return -ENOMEM;
10472
10473 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10474 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10475 LPFC_SLI4_MBX_EMBED);
10476 if (!phba->sli4_hba.intr_enable)
10477 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10478 else
10479 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10480 /* The IOCTL status is embedded in the mailbox subheader. */
10481 shdr = (union lpfc_sli4_cfg_shdr *)
10482 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10483 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10484 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10485 if (rc != MBX_TIMEOUT)
10486 mempool_free(mbox, phba->mbox_mem_pool);
10487 if (shdr_status || shdr_add_status || rc) {
10488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10489 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10490 "status x%x add_status x%x, mbx status x%x\n",
10491 shdr_status, shdr_add_status, rc);
10492 rc = -ENXIO;
10493 }
10494 return rc;
10495}
10496
10497/**
10498 * lpfc_sli4_next_xritag - Get an xritag for the io
10499 * @phba: Pointer to HBA context object.
10500 *
10501 * This function gets an xritag for the iocb. If there is no unused xritag
10502 * it will return 0xffff.
10503 * The function returns the allocated xritag if successful, else returns zero.
10504 * Zero is not a valid xritag.
10505 * The caller is not required to hold any lock.
10506 **/
10507uint16_t
10508lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10509{
10510 uint16_t xritag;
10511
10512 spin_lock_irq(&phba->hbalock);
10513 xritag = phba->sli4_hba.next_xri;
10514 if ((xritag != (uint16_t) -1) && xritag <
10515 (phba->sli4_hba.max_cfg_param.max_xri
10516 + phba->sli4_hba.max_cfg_param.xri_base)) {
10517 phba->sli4_hba.next_xri++;
10518 phba->sli4_hba.max_cfg_param.xri_used++;
10519 spin_unlock_irq(&phba->hbalock);
10520 return xritag;
10521 }
10522 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010523 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010524 "2004 Failed to allocate XRI.last XRITAG is %d"
10525 " Max XRI is %d, Used XRI is %d\n",
10526 phba->sli4_hba.next_xri,
10527 phba->sli4_hba.max_cfg_param.max_xri,
10528 phba->sli4_hba.max_cfg_param.xri_used);
10529 return -1;
10530}
10531
10532/**
10533 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10534 * @phba: pointer to lpfc hba data structure.
10535 *
10536 * This routine is invoked to post a block of driver's sgl pages to the
10537 * HBA using non-embedded mailbox command. No Lock is held. This routine
10538 * is only called when the driver is loading and after all IO has been
10539 * stopped.
10540 **/
10541int
10542lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10543{
10544 struct lpfc_sglq *sglq_entry;
10545 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10546 struct sgl_page_pairs *sgl_pg_pairs;
10547 void *viraddr;
10548 LPFC_MBOXQ_t *mbox;
10549 uint32_t reqlen, alloclen, pg_pairs;
10550 uint32_t mbox_tmo;
10551 uint16_t xritag_start = 0;
10552 int els_xri_cnt, rc = 0;
10553 uint32_t shdr_status, shdr_add_status;
10554 union lpfc_sli4_cfg_shdr *shdr;
10555
10556 /* The number of sgls to be posted */
10557 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10558
10559 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10560 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10561 if (reqlen > PAGE_SIZE) {
10562 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10563 "2559 Block sgl registration required DMA "
10564 "size (%d) great than a page\n", reqlen);
10565 return -ENOMEM;
10566 }
10567 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10568 if (!mbox) {
10569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10570 "2560 Failed to allocate mbox cmd memory\n");
10571 return -ENOMEM;
10572 }
10573
10574 /* Allocate DMA memory and set up the non-embedded mailbox command */
10575 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10576 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10577 LPFC_SLI4_MBX_NEMBED);
10578
10579 if (alloclen < reqlen) {
10580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10581 "0285 Allocated DMA memory size (%d) is "
10582 "less than the requested DMA memory "
10583 "size (%d)\n", alloclen, reqlen);
10584 lpfc_sli4_mbox_cmd_free(phba, mbox);
10585 return -ENOMEM;
10586 }
James Smart4f774512009-05-22 14:52:35 -040010587 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010588 viraddr = mbox->sge_array->addr[0];
10589
10590 /* Set up the SGL pages in the non-embedded DMA pages */
10591 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10592 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10593
10594 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10595 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10596 /* Set up the sge entry */
10597 sgl_pg_pairs->sgl_pg0_addr_lo =
10598 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10599 sgl_pg_pairs->sgl_pg0_addr_hi =
10600 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10601 sgl_pg_pairs->sgl_pg1_addr_lo =
10602 cpu_to_le32(putPaddrLow(0));
10603 sgl_pg_pairs->sgl_pg1_addr_hi =
10604 cpu_to_le32(putPaddrHigh(0));
10605 /* Keep the first xritag on the list */
10606 if (pg_pairs == 0)
10607 xritag_start = sglq_entry->sli4_xritag;
10608 sgl_pg_pairs++;
10609 }
10610 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010611 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010612 /* Perform endian conversion if necessary */
10613 sgl->word0 = cpu_to_le32(sgl->word0);
10614
10615 if (!phba->sli4_hba.intr_enable)
10616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10617 else {
10618 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10619 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10620 }
10621 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10622 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10623 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10624 if (rc != MBX_TIMEOUT)
10625 lpfc_sli4_mbox_cmd_free(phba, mbox);
10626 if (shdr_status || shdr_add_status || rc) {
10627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10628 "2513 POST_SGL_BLOCK mailbox command failed "
10629 "status x%x add_status x%x mbx status x%x\n",
10630 shdr_status, shdr_add_status, rc);
10631 rc = -ENXIO;
10632 }
10633 return rc;
10634}
10635
10636/**
10637 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10638 * @phba: pointer to lpfc hba data structure.
10639 * @sblist: pointer to scsi buffer list.
10640 * @count: number of scsi buffers on the list.
10641 *
10642 * This routine is invoked to post a block of @count scsi sgl pages from a
10643 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10644 * No Lock is held.
10645 *
10646 **/
10647int
10648lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10649 int cnt)
10650{
10651 struct lpfc_scsi_buf *psb;
10652 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10653 struct sgl_page_pairs *sgl_pg_pairs;
10654 void *viraddr;
10655 LPFC_MBOXQ_t *mbox;
10656 uint32_t reqlen, alloclen, pg_pairs;
10657 uint32_t mbox_tmo;
10658 uint16_t xritag_start = 0;
10659 int rc = 0;
10660 uint32_t shdr_status, shdr_add_status;
10661 dma_addr_t pdma_phys_bpl1;
10662 union lpfc_sli4_cfg_shdr *shdr;
10663
10664 /* Calculate the requested length of the dma memory */
10665 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10666 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10667 if (reqlen > PAGE_SIZE) {
10668 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10669 "0217 Block sgl registration required DMA "
10670 "size (%d) great than a page\n", reqlen);
10671 return -ENOMEM;
10672 }
10673 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10674 if (!mbox) {
10675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10676 "0283 Failed to allocate mbox cmd memory\n");
10677 return -ENOMEM;
10678 }
10679
10680 /* Allocate DMA memory and set up the non-embedded mailbox command */
10681 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10682 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10683 LPFC_SLI4_MBX_NEMBED);
10684
10685 if (alloclen < reqlen) {
10686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10687 "2561 Allocated DMA memory size (%d) is "
10688 "less than the requested DMA memory "
10689 "size (%d)\n", alloclen, reqlen);
10690 lpfc_sli4_mbox_cmd_free(phba, mbox);
10691 return -ENOMEM;
10692 }
James Smart4f774512009-05-22 14:52:35 -040010693 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010694 viraddr = mbox->sge_array->addr[0];
10695
10696 /* Set up the SGL pages in the non-embedded DMA pages */
10697 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10698 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10699
10700 pg_pairs = 0;
10701 list_for_each_entry(psb, sblist, list) {
10702 /* Set up the sge entry */
10703 sgl_pg_pairs->sgl_pg0_addr_lo =
10704 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10705 sgl_pg_pairs->sgl_pg0_addr_hi =
10706 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10707 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10708 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10709 else
10710 pdma_phys_bpl1 = 0;
10711 sgl_pg_pairs->sgl_pg1_addr_lo =
10712 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10713 sgl_pg_pairs->sgl_pg1_addr_hi =
10714 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10715 /* Keep the first xritag on the list */
10716 if (pg_pairs == 0)
10717 xritag_start = psb->cur_iocbq.sli4_xritag;
10718 sgl_pg_pairs++;
10719 pg_pairs++;
10720 }
10721 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10722 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10723 /* Perform endian conversion if necessary */
10724 sgl->word0 = cpu_to_le32(sgl->word0);
10725
10726 if (!phba->sli4_hba.intr_enable)
10727 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10728 else {
10729 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10730 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10731 }
10732 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10733 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10734 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10735 if (rc != MBX_TIMEOUT)
10736 lpfc_sli4_mbox_cmd_free(phba, mbox);
10737 if (shdr_status || shdr_add_status || rc) {
10738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10739 "2564 POST_SGL_BLOCK mailbox command failed "
10740 "status x%x add_status x%x mbx status x%x\n",
10741 shdr_status, shdr_add_status, rc);
10742 rc = -ENXIO;
10743 }
10744 return rc;
10745}
10746
10747/**
10748 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10749 * @phba: pointer to lpfc_hba struct that the frame was received on
10750 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10751 *
10752 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10753 * valid type of frame that the LPFC driver will handle. This function will
10754 * return a zero if the frame is a valid frame or a non zero value when the
10755 * frame does not pass the check.
10756 **/
10757static int
10758lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10759{
10760 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10761 char *type_names[] = FC_TYPE_NAMES_INIT;
10762 struct fc_vft_header *fc_vft_hdr;
10763
10764 switch (fc_hdr->fh_r_ctl) {
10765 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10766 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10767 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10768 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10769 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10770 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10771 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10772 case FC_RCTL_DD_CMD_STATUS: /* command status */
10773 case FC_RCTL_ELS_REQ: /* extended link services request */
10774 case FC_RCTL_ELS_REP: /* extended link services reply */
10775 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10776 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10777 case FC_RCTL_BA_NOP: /* basic link service NOP */
10778 case FC_RCTL_BA_ABTS: /* basic link service abort */
10779 case FC_RCTL_BA_RMC: /* remove connection */
10780 case FC_RCTL_BA_ACC: /* basic accept */
10781 case FC_RCTL_BA_RJT: /* basic reject */
10782 case FC_RCTL_BA_PRMT:
10783 case FC_RCTL_ACK_1: /* acknowledge_1 */
10784 case FC_RCTL_ACK_0: /* acknowledge_0 */
10785 case FC_RCTL_P_RJT: /* port reject */
10786 case FC_RCTL_F_RJT: /* fabric reject */
10787 case FC_RCTL_P_BSY: /* port busy */
10788 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10789 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10790 case FC_RCTL_LCR: /* link credit reset */
10791 case FC_RCTL_END: /* end */
10792 break;
10793 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10794 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10795 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10796 return lpfc_fc_frame_check(phba, fc_hdr);
10797 default:
10798 goto drop;
10799 }
10800 switch (fc_hdr->fh_type) {
10801 case FC_TYPE_BLS:
10802 case FC_TYPE_ELS:
10803 case FC_TYPE_FCP:
10804 case FC_TYPE_CT:
10805 break;
10806 case FC_TYPE_IP:
10807 case FC_TYPE_ILS:
10808 default:
10809 goto drop;
10810 }
10811 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10812 "2538 Received frame rctl:%s type:%s\n",
10813 rctl_names[fc_hdr->fh_r_ctl],
10814 type_names[fc_hdr->fh_type]);
10815 return 0;
10816drop:
10817 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10818 "2539 Dropped frame rctl:%s type:%s\n",
10819 rctl_names[fc_hdr->fh_r_ctl],
10820 type_names[fc_hdr->fh_type]);
10821 return 1;
10822}
10823
10824/**
10825 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10826 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10827 *
10828 * This function processes the FC header to retrieve the VFI from the VF
10829 * header, if one exists. This function will return the VFI if one exists
10830 * or 0 if no VSAN Header exists.
10831 **/
10832static uint32_t
10833lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10834{
10835 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10836
10837 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10838 return 0;
10839 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10840}
10841
10842/**
10843 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10844 * @phba: Pointer to the HBA structure to search for the vport on
10845 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10846 * @fcfi: The FC Fabric ID that the frame came from
10847 *
10848 * This function searches the @phba for a vport that matches the content of the
10849 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10850 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10851 * returns the matching vport pointer or NULL if unable to match frame to a
10852 * vport.
10853 **/
10854static struct lpfc_vport *
10855lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10856 uint16_t fcfi)
10857{
10858 struct lpfc_vport **vports;
10859 struct lpfc_vport *vport = NULL;
10860 int i;
10861 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10862 fc_hdr->fh_d_id[1] << 8 |
10863 fc_hdr->fh_d_id[2]);
10864
10865 vports = lpfc_create_vport_work_array(phba);
10866 if (vports != NULL)
10867 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10868 if (phba->fcf.fcfi == fcfi &&
10869 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10870 vports[i]->fc_myDID == did) {
10871 vport = vports[i];
10872 break;
10873 }
10874 }
10875 lpfc_destroy_vport_work_array(phba, vports);
10876 return vport;
10877}
10878
10879/**
James Smart45ed1192009-10-02 15:17:02 -040010880 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10881 * @vport: The vport to work on.
10882 *
10883 * This function updates the receive sequence time stamp for this vport. The
10884 * receive sequence time stamp indicates the time that the last frame of the
10885 * the sequence that has been idle for the longest amount of time was received.
10886 * the driver uses this time stamp to indicate if any received sequences have
10887 * timed out.
10888 **/
10889void
10890lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10891{
10892 struct lpfc_dmabuf *h_buf;
10893 struct hbq_dmabuf *dmabuf = NULL;
10894
10895 /* get the oldest sequence on the rcv list */
10896 h_buf = list_get_first(&vport->rcv_buffer_list,
10897 struct lpfc_dmabuf, list);
10898 if (!h_buf)
10899 return;
10900 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10901 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10902}
10903
10904/**
10905 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10906 * @vport: The vport that the received sequences were sent to.
10907 *
10908 * This function cleans up all outstanding received sequences. This is called
10909 * by the driver when a link event or user action invalidates all the received
10910 * sequences.
10911 **/
10912void
10913lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10914{
10915 struct lpfc_dmabuf *h_buf, *hnext;
10916 struct lpfc_dmabuf *d_buf, *dnext;
10917 struct hbq_dmabuf *dmabuf = NULL;
10918
10919 /* start with the oldest sequence on the rcv list */
10920 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10921 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10922 list_del_init(&dmabuf->hbuf.list);
10923 list_for_each_entry_safe(d_buf, dnext,
10924 &dmabuf->dbuf.list, list) {
10925 list_del_init(&d_buf->list);
10926 lpfc_in_buf_free(vport->phba, d_buf);
10927 }
10928 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10929 }
10930}
10931
10932/**
10933 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10934 * @vport: The vport that the received sequences were sent to.
10935 *
10936 * This function determines whether any received sequences have timed out by
10937 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
10938 * indicates that there is at least one timed out sequence this routine will
10939 * go through the received sequences one at a time from most inactive to most
10940 * active to determine which ones need to be cleaned up. Once it has determined
10941 * that a sequence needs to be cleaned up it will simply free up the resources
10942 * without sending an abort.
10943 **/
10944void
10945lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
10946{
10947 struct lpfc_dmabuf *h_buf, *hnext;
10948 struct lpfc_dmabuf *d_buf, *dnext;
10949 struct hbq_dmabuf *dmabuf = NULL;
10950 unsigned long timeout;
10951 int abort_count = 0;
10952
10953 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10954 vport->rcv_buffer_time_stamp);
10955 if (list_empty(&vport->rcv_buffer_list) ||
10956 time_before(jiffies, timeout))
10957 return;
10958 /* start with the oldest sequence on the rcv list */
10959 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10960 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10961 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10962 dmabuf->time_stamp);
10963 if (time_before(jiffies, timeout))
10964 break;
10965 abort_count++;
10966 list_del_init(&dmabuf->hbuf.list);
10967 list_for_each_entry_safe(d_buf, dnext,
10968 &dmabuf->dbuf.list, list) {
10969 list_del_init(&d_buf->list);
10970 lpfc_in_buf_free(vport->phba, d_buf);
10971 }
10972 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10973 }
10974 if (abort_count)
10975 lpfc_update_rcv_time_stamp(vport);
10976}
10977
10978/**
James Smart4f774512009-05-22 14:52:35 -040010979 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10980 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10981 *
10982 * This function searches through the existing incomplete sequences that have
10983 * been sent to this @vport. If the frame matches one of the incomplete
10984 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10985 * make up that sequence. If no sequence is found that matches this frame then
10986 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10987 * This function returns a pointer to the first dmabuf in the sequence list that
10988 * the frame was linked to.
10989 **/
10990static struct hbq_dmabuf *
10991lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10992{
10993 struct fc_frame_header *new_hdr;
10994 struct fc_frame_header *temp_hdr;
10995 struct lpfc_dmabuf *d_buf;
10996 struct lpfc_dmabuf *h_buf;
10997 struct hbq_dmabuf *seq_dmabuf = NULL;
10998 struct hbq_dmabuf *temp_dmabuf = NULL;
10999
James Smart4d9ab992009-10-02 15:16:39 -040011000 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011001 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011002 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11003 /* Use the hdr_buf to find the sequence that this frame belongs to */
11004 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11005 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11006 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11007 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11008 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11009 continue;
11010 /* found a pending sequence that matches this frame */
11011 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11012 break;
11013 }
11014 if (!seq_dmabuf) {
11015 /*
11016 * This indicates first frame received for this sequence.
11017 * Queue the buffer on the vport's rcv_buffer_list.
11018 */
11019 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011020 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011021 return dmabuf;
11022 }
11023 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011024 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11025 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011026 list_del_init(&seq_dmabuf->hbuf.list);
11027 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11028 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011029 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011030 return dmabuf;
11031 }
James Smart45ed1192009-10-02 15:17:02 -040011032 /* move this sequence to the tail to indicate a young sequence */
11033 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11034 seq_dmabuf->time_stamp = jiffies;
11035 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011036 if (list_empty(&seq_dmabuf->dbuf.list)) {
11037 temp_hdr = dmabuf->hbuf.virt;
11038 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11039 return seq_dmabuf;
11040 }
James Smart4f774512009-05-22 14:52:35 -040011041 /* find the correct place in the sequence to insert this frame */
11042 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11043 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11044 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11045 /*
11046 * If the frame's sequence count is greater than the frame on
11047 * the list then insert the frame right after this frame
11048 */
James Smarteeead812009-12-21 17:01:23 -050011049 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11050 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011051 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11052 return seq_dmabuf;
11053 }
11054 }
11055 return NULL;
11056}
11057
11058/**
James Smart6669f9b2009-10-02 15:16:45 -040011059 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11060 * @vport: pointer to a vitural port
11061 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11062 *
11063 * This function tries to abort from the partially assembed sequence, described
11064 * by the information from basic abbort @dmabuf. It checks to see whether such
11065 * partially assembled sequence held by the driver. If so, it shall free up all
11066 * the frames from the partially assembled sequence.
11067 *
11068 * Return
11069 * true -- if there is matching partially assembled sequence present and all
11070 * the frames freed with the sequence;
11071 * false -- if there is no matching partially assembled sequence present so
11072 * nothing got aborted in the lower layer driver
11073 **/
11074static bool
11075lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11076 struct hbq_dmabuf *dmabuf)
11077{
11078 struct fc_frame_header *new_hdr;
11079 struct fc_frame_header *temp_hdr;
11080 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11081 struct hbq_dmabuf *seq_dmabuf = NULL;
11082
11083 /* Use the hdr_buf to find the sequence that matches this frame */
11084 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11085 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11086 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11087 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11088 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11089 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11090 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11091 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11092 continue;
11093 /* found a pending sequence that matches this frame */
11094 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11095 break;
11096 }
11097
11098 /* Free up all the frames from the partially assembled sequence */
11099 if (seq_dmabuf) {
11100 list_for_each_entry_safe(d_buf, n_buf,
11101 &seq_dmabuf->dbuf.list, list) {
11102 list_del_init(&d_buf->list);
11103 lpfc_in_buf_free(vport->phba, d_buf);
11104 }
11105 return true;
11106 }
11107 return false;
11108}
11109
11110/**
11111 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11112 * @phba: Pointer to HBA context object.
11113 * @cmd_iocbq: pointer to the command iocbq structure.
11114 * @rsp_iocbq: pointer to the response iocbq structure.
11115 *
11116 * This function handles the sequence abort accept iocb command complete
11117 * event. It properly releases the memory allocated to the sequence abort
11118 * accept iocb.
11119 **/
11120static void
11121lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11122 struct lpfc_iocbq *cmd_iocbq,
11123 struct lpfc_iocbq *rsp_iocbq)
11124{
11125 if (cmd_iocbq)
11126 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11127}
11128
11129/**
11130 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11131 * @phba: Pointer to HBA context object.
11132 * @fc_hdr: pointer to a FC frame header.
11133 *
11134 * This function sends a basic accept to a previous unsol sequence abort
11135 * event after aborting the sequence handling.
11136 **/
11137static void
11138lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11139 struct fc_frame_header *fc_hdr)
11140{
11141 struct lpfc_iocbq *ctiocb = NULL;
11142 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011143 uint16_t oxid, rxid;
11144 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011145 IOCB_t *icmd;
11146
11147 if (!lpfc_is_link_up(phba))
11148 return;
11149
11150 sid = sli4_sid_from_fc_hdr(fc_hdr);
11151 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011152 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011153
11154 ndlp = lpfc_findnode_did(phba->pport, sid);
11155 if (!ndlp) {
11156 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11157 "1268 Find ndlp returned NULL for oxid:x%x "
11158 "SID:x%x\n", oxid, sid);
11159 return;
11160 }
11161
11162 /* Allocate buffer for acc iocb */
11163 ctiocb = lpfc_sli_get_iocbq(phba);
11164 if (!ctiocb)
11165 return;
11166
James Smart5ffc2662009-11-18 15:39:44 -050011167 /* Extract the F_CTL field from FC_HDR */
11168 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11169
James Smart6669f9b2009-10-02 15:16:45 -040011170 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011171 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011172 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011173 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11174 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11175 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11176
11177 /* Fill in the rest of iocb fields */
11178 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11179 icmd->ulpBdeCount = 0;
11180 icmd->ulpLe = 1;
11181 icmd->ulpClass = CLASS3;
11182 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011183
James Smart6669f9b2009-10-02 15:16:45 -040011184 ctiocb->iocb_cmpl = NULL;
11185 ctiocb->vport = phba->pport;
11186 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11187
James Smart5ffc2662009-11-18 15:39:44 -050011188 if (fctl & FC_FC_EX_CTX) {
11189 /* ABTS sent by responder to CT exchange, construction
11190 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11191 * field and RX_ID from ABTS for RX_ID field.
11192 */
11193 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11194 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11195 ctiocb->sli4_xritag = oxid;
11196 } else {
11197 /* ABTS sent by initiator to CT exchange, construction
11198 * of BA_ACC will need to allocate a new XRI as for the
11199 * XRI_TAG and RX_ID fields.
11200 */
11201 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11202 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11203 ctiocb->sli4_xritag = NO_XRI;
11204 }
11205 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11206
James Smart6669f9b2009-10-02 15:16:45 -040011207 /* Xmit CT abts accept on exchange <xid> */
11208 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11209 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11210 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11211 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11212}
11213
11214/**
11215 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11216 * @vport: Pointer to the vport on which this sequence was received
11217 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11218 *
11219 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11220 * receive sequence is only partially assembed by the driver, it shall abort
11221 * the partially assembled frames for the sequence. Otherwise, if the
11222 * unsolicited receive sequence has been completely assembled and passed to
11223 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11224 * unsolicited sequence has been aborted. After that, it will issue a basic
11225 * accept to accept the abort.
11226 **/
11227void
11228lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11229 struct hbq_dmabuf *dmabuf)
11230{
11231 struct lpfc_hba *phba = vport->phba;
11232 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011233 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011234 bool abts_par;
11235
James Smart6669f9b2009-10-02 15:16:45 -040011236 /* Make a copy of fc_hdr before the dmabuf being released */
11237 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011238 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011239
James Smart5ffc2662009-11-18 15:39:44 -050011240 if (fctl & FC_FC_EX_CTX) {
11241 /*
11242 * ABTS sent by responder to exchange, just free the buffer
11243 */
James Smart6669f9b2009-10-02 15:16:45 -040011244 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011245 } else {
11246 /*
11247 * ABTS sent by initiator to exchange, need to do cleanup
11248 */
11249 /* Try to abort partially assembled seq */
11250 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11251
11252 /* Send abort to ULP if partially seq abort failed */
11253 if (abts_par == false)
11254 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11255 else
11256 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11257 }
James Smart6669f9b2009-10-02 15:16:45 -040011258 /* Send basic accept (BA_ACC) to the abort requester */
11259 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11260}
11261
11262/**
James Smart4f774512009-05-22 14:52:35 -040011263 * lpfc_seq_complete - Indicates if a sequence is complete
11264 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11265 *
11266 * This function checks the sequence, starting with the frame described by
11267 * @dmabuf, to see if all the frames associated with this sequence are present.
11268 * the frames associated with this sequence are linked to the @dmabuf using the
11269 * dbuf list. This function looks for two major things. 1) That the first frame
11270 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11271 * set. 3) That there are no holes in the sequence count. The function will
11272 * return 1 when the sequence is complete, otherwise it will return 0.
11273 **/
11274static int
11275lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11276{
11277 struct fc_frame_header *hdr;
11278 struct lpfc_dmabuf *d_buf;
11279 struct hbq_dmabuf *seq_dmabuf;
11280 uint32_t fctl;
11281 int seq_count = 0;
11282
11283 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11284 /* make sure first fame of sequence has a sequence count of zero */
11285 if (hdr->fh_seq_cnt != seq_count)
11286 return 0;
11287 fctl = (hdr->fh_f_ctl[0] << 16 |
11288 hdr->fh_f_ctl[1] << 8 |
11289 hdr->fh_f_ctl[2]);
11290 /* If last frame of sequence we can return success. */
11291 if (fctl & FC_FC_END_SEQ)
11292 return 1;
11293 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11294 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11295 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11296 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011297 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011298 return 0;
11299 fctl = (hdr->fh_f_ctl[0] << 16 |
11300 hdr->fh_f_ctl[1] << 8 |
11301 hdr->fh_f_ctl[2]);
11302 /* If last frame of sequence we can return success. */
11303 if (fctl & FC_FC_END_SEQ)
11304 return 1;
11305 }
11306 return 0;
11307}
11308
11309/**
11310 * lpfc_prep_seq - Prep sequence for ULP processing
11311 * @vport: Pointer to the vport on which this sequence was received
11312 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11313 *
11314 * This function takes a sequence, described by a list of frames, and creates
11315 * a list of iocbq structures to describe the sequence. This iocbq list will be
11316 * used to issue to the generic unsolicited sequence handler. This routine
11317 * returns a pointer to the first iocbq in the list. If the function is unable
11318 * to allocate an iocbq then it throw out the received frames that were not
11319 * able to be described and return a pointer to the first iocbq. If unable to
11320 * allocate any iocbqs (including the first) this function will return NULL.
11321 **/
11322static struct lpfc_iocbq *
11323lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11324{
11325 struct lpfc_dmabuf *d_buf, *n_buf;
11326 struct lpfc_iocbq *first_iocbq, *iocbq;
11327 struct fc_frame_header *fc_hdr;
11328 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011329 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011330
11331 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11332 /* remove from receive buffer list */
11333 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011334 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011335 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011336 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011337 /* Get an iocbq struct to fill in. */
11338 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11339 if (first_iocbq) {
11340 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011341 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011342 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11343 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11344 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11345 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11346 vport->vpi + vport->phba->vpi_base;
11347 /* put the first buffer into the first IOCBq */
11348 first_iocbq->context2 = &seq_dmabuf->dbuf;
11349 first_iocbq->context3 = NULL;
11350 first_iocbq->iocb.ulpBdeCount = 1;
11351 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11352 LPFC_DATA_BUF_SIZE;
11353 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011354 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011355 bf_get(lpfc_rcqe_length,
11356 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011357 }
11358 iocbq = first_iocbq;
11359 /*
11360 * Each IOCBq can have two Buffers assigned, so go through the list
11361 * of buffers for this sequence and save two buffers in each IOCBq
11362 */
11363 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11364 if (!iocbq) {
11365 lpfc_in_buf_free(vport->phba, d_buf);
11366 continue;
11367 }
11368 if (!iocbq->context3) {
11369 iocbq->context3 = d_buf;
11370 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011371 pbde = (struct ulp_bde64 *)
11372 &iocbq->iocb.unsli3.sli3Words[4];
11373 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011374 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011375 bf_get(lpfc_rcqe_length,
11376 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011377 } else {
11378 iocbq = lpfc_sli_get_iocbq(vport->phba);
11379 if (!iocbq) {
11380 if (first_iocbq) {
11381 first_iocbq->iocb.ulpStatus =
11382 IOSTAT_FCP_RSP_ERROR;
11383 first_iocbq->iocb.un.ulpWord[4] =
11384 IOERR_NO_RESOURCES;
11385 }
11386 lpfc_in_buf_free(vport->phba, d_buf);
11387 continue;
11388 }
11389 iocbq->context2 = d_buf;
11390 iocbq->context3 = NULL;
11391 iocbq->iocb.ulpBdeCount = 1;
11392 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11393 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011394 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011395 bf_get(lpfc_rcqe_length,
11396 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011397 iocbq->iocb.un.rcvels.remoteID = sid;
11398 list_add_tail(&iocbq->list, &first_iocbq->list);
11399 }
11400 }
11401 return first_iocbq;
11402}
11403
James Smart6669f9b2009-10-02 15:16:45 -040011404static void
11405lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11406 struct hbq_dmabuf *seq_dmabuf)
11407{
11408 struct fc_frame_header *fc_hdr;
11409 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11410 struct lpfc_hba *phba = vport->phba;
11411
11412 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11413 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11414 if (!iocbq) {
11415 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11416 "2707 Ring %d handler: Failed to allocate "
11417 "iocb Rctl x%x Type x%x received\n",
11418 LPFC_ELS_RING,
11419 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11420 return;
11421 }
11422 if (!lpfc_complete_unsol_iocb(phba,
11423 &phba->sli.ring[LPFC_ELS_RING],
11424 iocbq, fc_hdr->fh_r_ctl,
11425 fc_hdr->fh_type))
11426 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11427 "2540 Ring %d handler: unexpected Rctl "
11428 "x%x Type x%x received\n",
11429 LPFC_ELS_RING,
11430 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11431
11432 /* Free iocb created in lpfc_prep_seq */
11433 list_for_each_entry_safe(curr_iocb, next_iocb,
11434 &iocbq->list, list) {
11435 list_del_init(&curr_iocb->list);
11436 lpfc_sli_release_iocbq(phba, curr_iocb);
11437 }
11438 lpfc_sli_release_iocbq(phba, iocbq);
11439}
11440
James Smart4f774512009-05-22 14:52:35 -040011441/**
11442 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11443 * @phba: Pointer to HBA context object.
11444 *
11445 * This function is called with no lock held. This function processes all
11446 * the received buffers and gives it to upper layers when a received buffer
11447 * indicates that it is the final frame in the sequence. The interrupt
11448 * service routine processes received buffers at interrupt contexts and adds
11449 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11450 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11451 * appropriate receive function when the final frame in a sequence is received.
11452 **/
James Smart4d9ab992009-10-02 15:16:39 -040011453void
11454lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11455 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011456{
James Smart4d9ab992009-10-02 15:16:39 -040011457 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011458 struct fc_frame_header *fc_hdr;
11459 struct lpfc_vport *vport;
11460 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011461
James Smart4f774512009-05-22 14:52:35 -040011462 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011463 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11464 /* check to see if this a valid type of frame */
11465 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11466 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11467 return;
11468 }
11469 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11470 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011471 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011472 /* throw out the frame */
11473 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11474 return;
11475 }
James Smart6669f9b2009-10-02 15:16:45 -040011476 /* Handle the basic abort sequence (BA_ABTS) event */
11477 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11478 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11479 return;
11480 }
11481
James Smart4d9ab992009-10-02 15:16:39 -040011482 /* Link this frame */
11483 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11484 if (!seq_dmabuf) {
11485 /* unable to add frame to vport - throw it out */
11486 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11487 return;
11488 }
11489 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011490 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011491 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011492
James Smart6669f9b2009-10-02 15:16:45 -040011493 /* Send the complete sequence to the upper layer protocol */
11494 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011495}
James Smart6fb120a2009-05-22 14:52:59 -040011496
11497/**
11498 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11499 * @phba: pointer to lpfc hba data structure.
11500 *
11501 * This routine is invoked to post rpi header templates to the
11502 * HBA consistent with the SLI-4 interface spec. This routine
11503 * posts a PAGE_SIZE memory region to the port to hold up to
11504 * PAGE_SIZE modulo 64 rpi context headers.
11505 *
11506 * This routine does not require any locks. It's usage is expected
11507 * to be driver load or reset recovery when the driver is
11508 * sequential.
11509 *
11510 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011511 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011512 * EIO - The mailbox failed to complete successfully.
11513 * When this error occurs, the driver is not guaranteed
11514 * to have any rpi regions posted to the device and
11515 * must either attempt to repost the regions or take a
11516 * fatal error.
11517 **/
11518int
11519lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11520{
11521 struct lpfc_rpi_hdr *rpi_page;
11522 uint32_t rc = 0;
11523
11524 /* Post all rpi memory regions to the port. */
11525 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11526 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11527 if (rc != MBX_SUCCESS) {
11528 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11529 "2008 Error %d posting all rpi "
11530 "headers\n", rc);
11531 rc = -EIO;
11532 break;
11533 }
11534 }
11535
11536 return rc;
11537}
11538
11539/**
11540 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11541 * @phba: pointer to lpfc hba data structure.
11542 * @rpi_page: pointer to the rpi memory region.
11543 *
11544 * This routine is invoked to post a single rpi header to the
11545 * HBA consistent with the SLI-4 interface spec. This memory region
11546 * maps up to 64 rpi context regions.
11547 *
11548 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011549 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011550 * ENOMEM - No available memory
11551 * EIO - The mailbox failed to complete successfully.
11552 **/
11553int
11554lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11555{
11556 LPFC_MBOXQ_t *mboxq;
11557 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11558 uint32_t rc = 0;
11559 uint32_t mbox_tmo;
11560 uint32_t shdr_status, shdr_add_status;
11561 union lpfc_sli4_cfg_shdr *shdr;
11562
11563 /* The port is notified of the header region via a mailbox command. */
11564 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11565 if (!mboxq) {
11566 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11567 "2001 Unable to allocate memory for issuing "
11568 "SLI_CONFIG_SPECIAL mailbox command\n");
11569 return -ENOMEM;
11570 }
11571
11572 /* Post all rpi memory regions to the port. */
11573 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11574 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11575 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11576 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11577 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11578 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11579 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11580 hdr_tmpl, rpi_page->page_count);
11581 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11582 rpi_page->start_rpi);
11583 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11584 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011585 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011586 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11587 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11588 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11589 if (rc != MBX_TIMEOUT)
11590 mempool_free(mboxq, phba->mbox_mem_pool);
11591 if (shdr_status || shdr_add_status || rc) {
11592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11593 "2514 POST_RPI_HDR mailbox failed with "
11594 "status x%x add_status x%x, mbx status x%x\n",
11595 shdr_status, shdr_add_status, rc);
11596 rc = -ENXIO;
11597 }
11598 return rc;
11599}
11600
11601/**
11602 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11603 * @phba: pointer to lpfc hba data structure.
11604 *
11605 * This routine is invoked to post rpi header templates to the
11606 * HBA consistent with the SLI-4 interface spec. This routine
11607 * posts a PAGE_SIZE memory region to the port to hold up to
11608 * PAGE_SIZE modulo 64 rpi context headers.
11609 *
11610 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011611 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011612 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11613 **/
11614int
11615lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11616{
11617 int rpi;
11618 uint16_t max_rpi, rpi_base, rpi_limit;
11619 uint16_t rpi_remaining;
11620 struct lpfc_rpi_hdr *rpi_hdr;
11621
11622 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11623 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11624 rpi_limit = phba->sli4_hba.next_rpi;
11625
11626 /*
11627 * The valid rpi range is not guaranteed to be zero-based. Start
11628 * the search at the rpi_base as reported by the port.
11629 */
11630 spin_lock_irq(&phba->hbalock);
11631 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11632 if (rpi >= rpi_limit || rpi < rpi_base)
11633 rpi = LPFC_RPI_ALLOC_ERROR;
11634 else {
11635 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11636 phba->sli4_hba.max_cfg_param.rpi_used++;
11637 phba->sli4_hba.rpi_count++;
11638 }
11639
11640 /*
11641 * Don't try to allocate more rpi header regions if the device limit
11642 * on available rpis max has been exhausted.
11643 */
11644 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11645 (phba->sli4_hba.rpi_count >= max_rpi)) {
11646 spin_unlock_irq(&phba->hbalock);
11647 return rpi;
11648 }
11649
11650 /*
11651 * If the driver is running low on rpi resources, allocate another
11652 * page now. Note that the next_rpi value is used because
11653 * it represents how many are actually in use whereas max_rpi notes
11654 * how many are supported max by the device.
11655 */
11656 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11657 phba->sli4_hba.rpi_count;
11658 spin_unlock_irq(&phba->hbalock);
11659 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11660 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11661 if (!rpi_hdr) {
11662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11663 "2002 Error Could not grow rpi "
11664 "count\n");
11665 } else {
11666 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11667 }
11668 }
11669
11670 return rpi;
11671}
11672
11673/**
11674 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11675 * @phba: pointer to lpfc hba data structure.
11676 *
11677 * This routine is invoked to release an rpi to the pool of
11678 * available rpis maintained by the driver.
11679 **/
11680void
11681lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11682{
11683 spin_lock_irq(&phba->hbalock);
11684 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11685 phba->sli4_hba.rpi_count--;
11686 phba->sli4_hba.max_cfg_param.rpi_used--;
11687 spin_unlock_irq(&phba->hbalock);
11688}
11689
11690/**
11691 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11692 * @phba: pointer to lpfc hba data structure.
11693 *
11694 * This routine is invoked to remove the memory region that
11695 * provided rpi via a bitmask.
11696 **/
11697void
11698lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11699{
11700 kfree(phba->sli4_hba.rpi_bmask);
11701}
11702
11703/**
11704 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11705 * @phba: pointer to lpfc hba data structure.
11706 *
11707 * This routine is invoked to remove the memory region that
11708 * provided rpi via a bitmask.
11709 **/
11710int
11711lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11712{
11713 LPFC_MBOXQ_t *mboxq;
11714 struct lpfc_hba *phba = ndlp->phba;
11715 int rc;
11716
11717 /* The port is notified of the header region via a mailbox command. */
11718 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11719 if (!mboxq)
11720 return -ENOMEM;
11721
11722 /* Post all rpi memory regions to the port. */
11723 lpfc_resume_rpi(mboxq, ndlp);
11724 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11725 if (rc == MBX_NOT_FINISHED) {
11726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11727 "2010 Resume RPI Mailbox failed "
11728 "status %d, mbxStatus x%x\n", rc,
11729 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11730 mempool_free(mboxq, phba->mbox_mem_pool);
11731 return -EIO;
11732 }
11733 return 0;
11734}
11735
11736/**
11737 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11738 * @phba: pointer to lpfc hba data structure.
11739 * @vpi: vpi value to activate with the port.
11740 *
11741 * This routine is invoked to activate a vpi with the
11742 * port when the host intends to use vports with a
11743 * nonzero vpi.
11744 *
11745 * Returns:
11746 * 0 success
11747 * -Evalue otherwise
11748 **/
11749int
11750lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11751{
11752 LPFC_MBOXQ_t *mboxq;
11753 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011754 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011755 uint32_t mbox_tmo;
11756
11757 if (vpi == 0)
11758 return -EINVAL;
11759 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11760 if (!mboxq)
11761 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011762 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011763 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11764 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011765 if (rc != MBX_SUCCESS) {
11766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11767 "2022 INIT VPI Mailbox failed "
11768 "status %d, mbxStatus x%x\n", rc,
11769 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011770 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011771 }
James Smart6a9c52c2009-10-02 15:16:51 -040011772 if (rc != MBX_TIMEOUT)
11773 mempool_free(mboxq, phba->mbox_mem_pool);
11774
11775 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011776}
11777
11778/**
11779 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11780 * @phba: pointer to lpfc hba data structure.
11781 * @mboxq: Pointer to mailbox object.
11782 *
11783 * This routine is invoked to manually add a single FCF record. The caller
11784 * must pass a completely initialized FCF_Record. This routine takes
11785 * care of the nonembedded mailbox operations.
11786 **/
11787static void
11788lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11789{
11790 void *virt_addr;
11791 union lpfc_sli4_cfg_shdr *shdr;
11792 uint32_t shdr_status, shdr_add_status;
11793
11794 virt_addr = mboxq->sge_array->addr[0];
11795 /* The IOCTL status is embedded in the mailbox subheader. */
11796 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11797 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11798 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11799
11800 if ((shdr_status || shdr_add_status) &&
11801 (shdr_status != STATUS_FCF_IN_USE))
11802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11803 "2558 ADD_FCF_RECORD mailbox failed with "
11804 "status x%x add_status x%x\n",
11805 shdr_status, shdr_add_status);
11806
11807 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11808}
11809
11810/**
11811 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11812 * @phba: pointer to lpfc hba data structure.
11813 * @fcf_record: pointer to the initialized fcf record to add.
11814 *
11815 * This routine is invoked to manually add a single FCF record. The caller
11816 * must pass a completely initialized FCF_Record. This routine takes
11817 * care of the nonembedded mailbox operations.
11818 **/
11819int
11820lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11821{
11822 int rc = 0;
11823 LPFC_MBOXQ_t *mboxq;
11824 uint8_t *bytep;
11825 void *virt_addr;
11826 dma_addr_t phys_addr;
11827 struct lpfc_mbx_sge sge;
11828 uint32_t alloc_len, req_len;
11829 uint32_t fcfindex;
11830
11831 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11832 if (!mboxq) {
11833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11834 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11835 return -ENOMEM;
11836 }
11837
11838 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11839 sizeof(uint32_t);
11840
11841 /* Allocate DMA memory and set up the non-embedded mailbox command */
11842 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11843 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11844 req_len, LPFC_SLI4_MBX_NEMBED);
11845 if (alloc_len < req_len) {
11846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11847 "2523 Allocated DMA memory size (x%x) is "
11848 "less than the requested DMA memory "
11849 "size (x%x)\n", alloc_len, req_len);
11850 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11851 return -ENOMEM;
11852 }
11853
11854 /*
11855 * Get the first SGE entry from the non-embedded DMA memory. This
11856 * routine only uses a single SGE.
11857 */
11858 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11859 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011860 virt_addr = mboxq->sge_array->addr[0];
11861 /*
11862 * Configure the FCF record for FCFI 0. This is the driver's
11863 * hardcoded default and gets used in nonFIP mode.
11864 */
11865 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11866 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11867 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11868
11869 /*
11870 * Copy the fcf_index and the FCF Record Data. The data starts after
11871 * the FCoE header plus word10. The data copy needs to be endian
11872 * correct.
11873 */
11874 bytep += sizeof(uint32_t);
11875 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11876 mboxq->vport = phba->pport;
11877 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11878 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11879 if (rc == MBX_NOT_FINISHED) {
11880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11881 "2515 ADD_FCF_RECORD mailbox failed with "
11882 "status 0x%x\n", rc);
11883 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11884 rc = -EIO;
11885 } else
11886 rc = 0;
11887
11888 return rc;
11889}
11890
11891/**
11892 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11893 * @phba: pointer to lpfc hba data structure.
11894 * @fcf_record: pointer to the fcf record to write the default data.
11895 * @fcf_index: FCF table entry index.
11896 *
11897 * This routine is invoked to build the driver's default FCF record. The
11898 * values used are hardcoded. This routine handles memory initialization.
11899 *
11900 **/
11901void
11902lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11903 struct fcf_record *fcf_record,
11904 uint16_t fcf_index)
11905{
11906 memset(fcf_record, 0, sizeof(struct fcf_record));
11907 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11908 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11909 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11910 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11911 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11912 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11913 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11914 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11915 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11916 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11917 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11918 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11919 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011920 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011921 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11922 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11923 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11924 /* Set the VLAN bit map */
11925 if (phba->valid_vlan) {
11926 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11927 = 1 << (phba->vlan_id % 8);
11928 }
11929}
11930
11931/**
11932 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11933 * @phba: pointer to lpfc hba data structure.
11934 * @fcf_index: FCF table entry offset.
11935 *
11936 * This routine is invoked to read up to @fcf_num of FCF record from the
11937 * device starting with the given @fcf_index.
11938 **/
11939int
11940lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11941{
11942 int rc = 0, error;
11943 LPFC_MBOXQ_t *mboxq;
11944 void *virt_addr;
11945 dma_addr_t phys_addr;
11946 uint8_t *bytep;
11947 struct lpfc_mbx_sge sge;
11948 uint32_t alloc_len, req_len;
11949 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11950
James Smart32b97932009-07-19 10:01:21 -040011951 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040011952 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11953 if (!mboxq) {
11954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11955 "2000 Failed to allocate mbox for "
11956 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040011957 error = -ENOMEM;
11958 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011959 }
11960
11961 req_len = sizeof(struct fcf_record) +
11962 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11963
11964 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11965 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11966 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11967 LPFC_SLI4_MBX_NEMBED);
11968
11969 if (alloc_len < req_len) {
11970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11971 "0291 Allocated DMA memory size (x%x) is "
11972 "less than the requested DMA memory "
11973 "size (x%x)\n", alloc_len, req_len);
James Smart4d9ab992009-10-02 15:16:39 -040011974 error = -ENOMEM;
11975 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011976 }
11977
11978 /* Get the first SGE entry from the non-embedded DMA memory. This
11979 * routine only uses a single SGE.
11980 */
11981 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11982 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011983 virt_addr = mboxq->sge_array->addr[0];
11984 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11985
11986 /* Set up command fields */
11987 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11988 /* Perform necessary endian conversion */
11989 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11990 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11991 mboxq->vport = phba->pport;
11992 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11993 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11994 if (rc == MBX_NOT_FINISHED) {
James Smart6fb120a2009-05-22 14:52:59 -040011995 error = -EIO;
James Smart32b97932009-07-19 10:01:21 -040011996 } else {
11997 spin_lock_irq(&phba->hbalock);
11998 phba->hba_flag |= FCF_DISC_INPROGRESS;
11999 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040012000 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012001 }
James Smart4d9ab992009-10-02 15:16:39 -040012002fail_fcfscan:
12003 if (error) {
12004 if (mboxq)
12005 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12006 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12007 spin_lock_irq(&phba->hbalock);
12008 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12009 spin_unlock_irq(&phba->hbalock);
12010 }
James Smart6fb120a2009-05-22 14:52:59 -040012011 return error;
12012}
James Smarta0c87cb2009-07-19 10:01:10 -040012013
12014/**
12015 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12016 * @phba: pointer to lpfc hba data structure.
12017 *
12018 * This function read region 23 and parse TLV for port status to
12019 * decide if the user disaled the port. If the TLV indicates the
12020 * port is disabled, the hba_flag is set accordingly.
12021 **/
12022void
12023lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12024{
12025 LPFC_MBOXQ_t *pmb = NULL;
12026 MAILBOX_t *mb;
12027 uint8_t *rgn23_data = NULL;
12028 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12029 int rc;
12030
12031 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12032 if (!pmb) {
12033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12034 "2600 lpfc_sli_read_serdes_param failed to"
12035 " allocate mailbox memory\n");
12036 goto out;
12037 }
12038 mb = &pmb->u.mb;
12039
12040 /* Get adapter Region 23 data */
12041 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12042 if (!rgn23_data)
12043 goto out;
12044
12045 do {
12046 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12047 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12048
12049 if (rc != MBX_SUCCESS) {
12050 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12051 "2601 lpfc_sli_read_link_ste failed to"
12052 " read config region 23 rc 0x%x Status 0x%x\n",
12053 rc, mb->mbxStatus);
12054 mb->un.varDmp.word_cnt = 0;
12055 }
12056 /*
12057 * dump mem may return a zero when finished or we got a
12058 * mailbox error, either way we are done.
12059 */
12060 if (mb->un.varDmp.word_cnt == 0)
12061 break;
12062 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12063 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12064
12065 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12066 rgn23_data + offset,
12067 mb->un.varDmp.word_cnt);
12068 offset += mb->un.varDmp.word_cnt;
12069 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12070
12071 data_size = offset;
12072 offset = 0;
12073
12074 if (!data_size)
12075 goto out;
12076
12077 /* Check the region signature first */
12078 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12080 "2619 Config region 23 has bad signature\n");
12081 goto out;
12082 }
12083 offset += 4;
12084
12085 /* Check the data structure version */
12086 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12088 "2620 Config region 23 has bad version\n");
12089 goto out;
12090 }
12091 offset += 4;
12092
12093 /* Parse TLV entries in the region */
12094 while (offset < data_size) {
12095 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12096 break;
12097 /*
12098 * If the TLV is not driver specific TLV or driver id is
12099 * not linux driver id, skip the record.
12100 */
12101 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12102 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12103 (rgn23_data[offset + 3] != 0)) {
12104 offset += rgn23_data[offset + 1] * 4 + 4;
12105 continue;
12106 }
12107
12108 /* Driver found a driver specific TLV in the config region */
12109 sub_tlv_len = rgn23_data[offset + 1] * 4;
12110 offset += 4;
12111 tlv_offset = 0;
12112
12113 /*
12114 * Search for configured port state sub-TLV.
12115 */
12116 while ((offset < data_size) &&
12117 (tlv_offset < sub_tlv_len)) {
12118 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12119 offset += 4;
12120 tlv_offset += 4;
12121 break;
12122 }
12123 if (rgn23_data[offset] != PORT_STE_TYPE) {
12124 offset += rgn23_data[offset + 1] * 4 + 4;
12125 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12126 continue;
12127 }
12128
12129 /* This HBA contains PORT_STE configured */
12130 if (!rgn23_data[offset + 2])
12131 phba->hba_flag |= LINK_DISABLED;
12132
12133 goto out;
12134 }
12135 }
12136out:
12137 if (pmb)
12138 mempool_free(pmb, phba->mbox_mem_pool);
12139 kfree(rgn23_data);
12140 return;
12141}