blob: 1d2f65c4eb0b1be37a1a6013b3e57c8dd54317cd [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);
266 return released;
267}
268
269/**
270 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
271 * @q: The Completion Queue to get the first valid CQE from
272 *
273 * This routine will get the first valid Completion Queue Entry from @q, update
274 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
275 * the Queue (no more work to do), or the Queue is full of CQEs that have been
276 * processed, but not popped back to the HBA then this routine will return NULL.
277 **/
278static struct lpfc_cqe *
279lpfc_sli4_cq_get(struct lpfc_queue *q)
280{
281 struct lpfc_cqe *cqe;
282
283 /* If the next CQE is not valid then we are done */
284 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
285 return NULL;
286 /* If the host has not yet processed the next entry then we are done */
287 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
288 return NULL;
289
290 cqe = q->qe[q->hba_index].cqe;
291 q->hba_index = ((q->hba_index + 1) % q->entry_count);
292 return cqe;
293}
294
295/**
296 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
297 * @q: The Completion Queue that the host has completed processing for.
298 * @arm: Indicates whether the host wants to arms this CQ.
299 *
300 * This routine will mark all Completion queue entries on @q, from the last
301 * known completed entry to the last entry that was processed, as completed
302 * by clearing the valid bit for each completion queue entry. Then it will
303 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
304 * The internal host index in the @q will be updated by this routine to indicate
305 * that the host has finished processing the entries. The @arm parameter
306 * indicates that the queue should be rearmed when ringing the doorbell.
307 *
308 * This function will return the number of CQEs that were released.
309 **/
310uint32_t
311lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
312{
313 uint32_t released = 0;
314 struct lpfc_cqe *temp_qe;
315 struct lpfc_register doorbell;
316
317 /* while there are valid entries */
318 while (q->hba_index != q->host_index) {
319 temp_qe = q->qe[q->host_index].cqe;
320 bf_set(lpfc_cqe_valid, temp_qe, 0);
321 released++;
322 q->host_index = ((q->host_index + 1) % q->entry_count);
323 }
324 if (unlikely(released == 0 && !arm))
325 return 0;
326
327 /* ring doorbell for number popped */
328 doorbell.word0 = 0;
329 if (arm)
330 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
331 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
332 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
333 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
334 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
335 return released;
336}
337
338/**
339 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
340 * @q: The Header Receive Queue to operate on.
341 * @wqe: The Receive Queue Entry to put on the Receive queue.
342 *
343 * This routine will copy the contents of @wqe to the next available entry on
344 * the @q. This function will then ring the Receive Queue Doorbell to signal the
345 * HBA to start processing the Receive Queue Entry. This function returns the
346 * index that the rqe was copied to if successful. If no entries are available
347 * on @q then this function will return -ENOMEM.
348 * The caller is expected to hold the hbalock when calling this routine.
349 **/
350static int
351lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
352 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
353{
354 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
355 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
356 struct lpfc_register doorbell;
357 int put_index = hq->host_index;
358
359 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
360 return -EINVAL;
361 if (hq->host_index != dq->host_index)
362 return -EINVAL;
363 /* If the host has not yet processed the next entry then we are done */
364 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
365 return -EBUSY;
366 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
367 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
368
369 /* Update the host index to point to the next slot */
370 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
371 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
372
373 /* Ring The Header Receive Queue Doorbell */
374 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
375 doorbell.word0 = 0;
376 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
377 LPFC_RQ_POST_BATCH);
378 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
379 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
380 }
381 return put_index;
382}
383
384/**
385 * lpfc_sli4_rq_release - Updates internal hba index for RQ
386 * @q: The Header Receive Queue to operate on.
387 *
388 * This routine will update the HBA index of a queue to reflect consumption of
389 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
390 * consumed an entry the host calls this function to update the queue's
391 * internal pointers. This routine returns the number of entries that were
392 * consumed by the HBA.
393 **/
394static uint32_t
395lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
396{
397 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
398 return 0;
399 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
400 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
401 return 1;
402}
403
James Smarte59058c2008-08-24 21:49:00 -0400404/**
James Smart3621a712009-04-06 18:47:14 -0400405 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400406 * @phba: Pointer to HBA context object.
407 * @pring: Pointer to driver SLI ring object.
408 *
409 * This function returns pointer to next command iocb entry
410 * in the command ring. The caller must hold hbalock to prevent
411 * other threads consume the next command iocb.
412 * SLI-2/SLI-3 provide different sized iocbs.
413 **/
James Smarted957682007-06-17 19:56:37 -0500414static inline IOCB_t *
415lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
416{
417 return (IOCB_t *) (((char *) pring->cmdringaddr) +
418 pring->cmdidx * phba->iocb_cmd_size);
419}
420
James Smarte59058c2008-08-24 21:49:00 -0400421/**
James Smart3621a712009-04-06 18:47:14 -0400422 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400423 * @phba: Pointer to HBA context object.
424 * @pring: Pointer to driver SLI ring object.
425 *
426 * This function returns pointer to next response iocb entry
427 * in the response ring. The caller must hold hbalock to make sure
428 * that no other thread consume the next response iocb.
429 * SLI-2/SLI-3 provide different sized iocbs.
430 **/
James Smarted957682007-06-17 19:56:37 -0500431static inline IOCB_t *
432lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
433{
434 return (IOCB_t *) (((char *) pring->rspringaddr) +
435 pring->rspidx * phba->iocb_rsp_size);
436}
437
James Smarte59058c2008-08-24 21:49:00 -0400438/**
James Smart3621a712009-04-06 18:47:14 -0400439 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400440 * @phba: Pointer to HBA context object.
441 *
442 * This function is called with hbalock held. This function
443 * allocates a new driver iocb object from the iocb pool. If the
444 * allocation is successful, it returns pointer to the newly
445 * allocated iocb object else it returns NULL.
446 **/
James Smart2e0fef82007-06-17 19:56:36 -0500447static struct lpfc_iocbq *
448__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400449{
450 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
451 struct lpfc_iocbq * iocbq = NULL;
452
453 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
454 return iocbq;
455}
456
James Smarte59058c2008-08-24 21:49:00 -0400457/**
James Smartda0436e2009-05-22 14:51:39 -0400458 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
459 * @phba: Pointer to HBA context object.
460 * @xritag: XRI value.
461 *
462 * This function clears the sglq pointer from the array of acive
463 * sglq's. The xritag that is passed in is used to index into the
464 * array. Before the xritag can be used it needs to be adjusted
465 * by subtracting the xribase.
466 *
467 * Returns sglq ponter = success, NULL = Failure.
468 **/
469static struct lpfc_sglq *
470__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
471{
472 uint16_t adj_xri;
473 struct lpfc_sglq *sglq;
474 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
475 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
476 return NULL;
477 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
478 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
479 return sglq;
480}
481
482/**
483 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
484 * @phba: Pointer to HBA context object.
485 * @xritag: XRI value.
486 *
487 * This function returns the sglq pointer from the array of acive
488 * sglq's. The xritag that is passed in is used to index into the
489 * array. Before the xritag can be used it needs to be adjusted
490 * by subtracting the xribase.
491 *
492 * Returns sglq ponter = success, NULL = Failure.
493 **/
494static struct lpfc_sglq *
495__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
496{
497 uint16_t adj_xri;
498 struct lpfc_sglq *sglq;
499 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
500 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
501 return NULL;
502 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
503 return sglq;
504}
505
506/**
507 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
508 * @phba: Pointer to HBA context object.
509 *
510 * This function is called with hbalock held. This function
511 * Gets a new driver sglq object from the sglq list. If the
512 * list is not empty then it is successful, it returns pointer to the newly
513 * allocated sglq object else it returns NULL.
514 **/
515static struct lpfc_sglq *
516__lpfc_sli_get_sglq(struct lpfc_hba *phba)
517{
518 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
519 struct lpfc_sglq *sglq = NULL;
520 uint16_t adj_xri;
521 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400522 if (!sglq)
523 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400524 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
525 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
526 return sglq;
527}
528
529/**
James Smart3621a712009-04-06 18:47:14 -0400530 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400531 * @phba: Pointer to HBA context object.
532 *
533 * This function is called with no lock held. This function
534 * allocates a new driver iocb object from the iocb pool. If the
535 * allocation is successful, it returns pointer to the newly
536 * allocated iocb object else it returns NULL.
537 **/
James Smart2e0fef82007-06-17 19:56:36 -0500538struct lpfc_iocbq *
539lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500540{
James Smart2e0fef82007-06-17 19:56:36 -0500541 struct lpfc_iocbq * iocbq = NULL;
542 unsigned long iflags;
543
544 spin_lock_irqsave(&phba->hbalock, iflags);
545 iocbq = __lpfc_sli_get_iocbq(phba);
546 spin_unlock_irqrestore(&phba->hbalock, iflags);
547 return iocbq;
548}
549
James Smarte59058c2008-08-24 21:49:00 -0400550/**
James Smart4f774512009-05-22 14:52:35 -0400551 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
552 * @phba: Pointer to HBA context object.
553 * @iocbq: Pointer to driver iocb object.
554 *
555 * This function is called with hbalock held to release driver
556 * iocb object to the iocb pool. The iotag in the iocb object
557 * does not change for each use of the iocb object. This function
558 * clears all other fields of the iocb object when it is freed.
559 * The sqlq structure that holds the xritag and phys and virtual
560 * mappings for the scatter gather list is retrieved from the
561 * active array of sglq. The get of the sglq pointer also clears
562 * the entry in the array. If the status of the IO indiactes that
563 * this IO was aborted then the sglq entry it put on the
564 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
565 * IO has good status or fails for any other reason then the sglq
566 * entry is added to the free list (lpfc_sgl_list).
567 **/
568static void
569__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
570{
571 struct lpfc_sglq *sglq;
572 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
573 unsigned long iflag;
574
575 if (iocbq->sli4_xritag == NO_XRI)
576 sglq = NULL;
577 else
578 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
579 if (sglq) {
580 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
James Smart6669f9b2009-10-02 15:16:45 -0400581 && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
James Smart4f774512009-05-22 14:52:35 -0400582 && (iocbq->iocb.un.ulpWord[4]
James Smart6669f9b2009-10-02 15:16:45 -0400583 == IOERR_ABORT_REQUESTED))) {
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:
764 case DSSCMD_INVALIDATE_DEK:
765 case DSSCMD_SET_KEK:
766 case DSSCMD_GET_KEK_ID:
767 case DSSCMD_GEN_XFER:
dea31012005-04-17 16:05:31 -0500768 type = LPFC_SOL_IOCB;
769 break;
770 case CMD_ABORT_XRI_CN:
771 case CMD_ABORT_XRI_CX:
772 case CMD_CLOSE_XRI_CN:
773 case CMD_CLOSE_XRI_CX:
774 case CMD_XRI_ABORTED_CX:
775 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400776 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500777 type = LPFC_ABORT_IOCB;
778 break;
779 case CMD_RCV_SEQUENCE_CX:
780 case CMD_RCV_ELS_REQ_CX:
781 case CMD_RCV_SEQUENCE64_CX:
782 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400783 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500784 case CMD_IOCB_RCV_SEQ64_CX:
785 case CMD_IOCB_RCV_ELS64_CX:
786 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500787 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500788 type = LPFC_UNSOL_IOCB;
789 break;
James Smart3163f722008-02-08 18:50:25 -0500790 case CMD_IOCB_XMIT_MSEQ64_CR:
791 case CMD_IOCB_XMIT_MSEQ64_CX:
792 case CMD_IOCB_RCV_SEQ_LIST64_CX:
793 case CMD_IOCB_RCV_ELS_LIST64_CX:
794 case CMD_IOCB_CLOSE_EXTENDED_CN:
795 case CMD_IOCB_ABORT_EXTENDED_CN:
796 case CMD_IOCB_RET_HBQE64_CN:
797 case CMD_IOCB_FCP_IBIDIR64_CR:
798 case CMD_IOCB_FCP_IBIDIR64_CX:
799 case CMD_IOCB_FCP_ITASKMGT64_CX:
800 case CMD_IOCB_LOGENTRY_CN:
801 case CMD_IOCB_LOGENTRY_ASYNC_CN:
802 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700803 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500804 type = LPFC_UNKNOWN_IOCB;
805 break;
dea31012005-04-17 16:05:31 -0500806 default:
807 type = LPFC_UNKNOWN_IOCB;
808 break;
809 }
810
811 return type;
812}
813
James Smarte59058c2008-08-24 21:49:00 -0400814/**
James Smart3621a712009-04-06 18:47:14 -0400815 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400816 * @phba: Pointer to HBA context object.
817 *
818 * This function is called from SLI initialization code
819 * to configure every ring of the HBA's SLI interface. The
820 * caller is not required to hold any lock. This function issues
821 * a config_ring mailbox command for each ring.
822 * This function returns zero if successful else returns a negative
823 * error code.
824 **/
dea31012005-04-17 16:05:31 -0500825static int
James Smarted957682007-06-17 19:56:37 -0500826lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500827{
828 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500829 LPFC_MBOXQ_t *pmb;
830 MAILBOX_t *pmbox;
831 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500832
James Smarted957682007-06-17 19:56:37 -0500833 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
834 if (!pmb)
835 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400836 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500837 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500838 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500839 lpfc_config_ring(phba, i, pmb);
840 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
841 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400843 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500844 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
845 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400846 rc, pmbox->mbxCommand,
847 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500848 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500849 ret = -ENXIO;
850 break;
dea31012005-04-17 16:05:31 -0500851 }
852 }
James Smarted957682007-06-17 19:56:37 -0500853 mempool_free(pmb, phba->mbox_mem_pool);
854 return ret;
dea31012005-04-17 16:05:31 -0500855}
856
James Smarte59058c2008-08-24 21:49:00 -0400857/**
James Smart3621a712009-04-06 18:47:14 -0400858 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400859 * @phba: Pointer to HBA context object.
860 * @pring: Pointer to driver SLI ring object.
861 * @piocb: Pointer to the driver iocb object.
862 *
863 * This function is called with hbalock held. The function adds the
864 * new iocb to txcmplq of the given ring. This function always returns
865 * 0. If this function is called for ELS ring, this function checks if
866 * there is a vport associated with the ELS command. This function also
867 * starts els_tmofunc timer if this is an ELS command.
868 **/
dea31012005-04-17 16:05:31 -0500869static int
James Smart2e0fef82007-06-17 19:56:36 -0500870lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
871 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500872{
dea31012005-04-17 16:05:31 -0500873 list_add_tail(&piocb->list, &pring->txcmplq);
874 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500875 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
876 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
877 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
878 if (!piocb->vport)
879 BUG();
880 else
881 mod_timer(&piocb->vport->els_tmofunc,
882 jiffies + HZ * (phba->fc_ratov << 1));
883 }
884
dea31012005-04-17 16:05:31 -0500885
James Smart2e0fef82007-06-17 19:56:36 -0500886 return 0;
dea31012005-04-17 16:05:31 -0500887}
888
James Smarte59058c2008-08-24 21:49:00 -0400889/**
James Smart3621a712009-04-06 18:47:14 -0400890 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400891 * @phba: Pointer to HBA context object.
892 * @pring: Pointer to driver SLI ring object.
893 *
894 * This function is called with hbalock held to get next
895 * iocb in txq of the given ring. If there is any iocb in
896 * the txq, the function returns first iocb in the list after
897 * removing the iocb from the list, else it returns NULL.
898 **/
dea31012005-04-17 16:05:31 -0500899static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500900lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500901{
dea31012005-04-17 16:05:31 -0500902 struct lpfc_iocbq *cmd_iocb;
903
James Smart858c9f62007-06-17 19:56:39 -0500904 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
905 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500906 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500907 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500908}
909
James Smarte59058c2008-08-24 21:49:00 -0400910/**
James Smart3621a712009-04-06 18:47:14 -0400911 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400912 * @phba: Pointer to HBA context object.
913 * @pring: Pointer to driver SLI ring object.
914 *
915 * This function is called with hbalock held and the caller must post the
916 * iocb without releasing the lock. If the caller releases the lock,
917 * iocb slot returned by the function is not guaranteed to be available.
918 * The function returns pointer to the next available iocb slot if there
919 * is available slot in the ring, else it returns NULL.
920 * If the get index of the ring is ahead of the put index, the function
921 * will post an error attention event to the worker thread to take the
922 * HBA to offline state.
923 **/
dea31012005-04-17 16:05:31 -0500924static IOCB_t *
925lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
926{
James Smart34b02dc2008-08-24 21:49:55 -0400927 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500928 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500929 if ((pring->next_cmdidx == pring->cmdidx) &&
930 (++pring->next_cmdidx >= max_cmd_idx))
931 pring->next_cmdidx = 0;
932
933 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
934
935 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
936
937 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
938 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400939 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200940 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400941 pring->ringno,
dea31012005-04-17 16:05:31 -0500942 pring->local_getidx, max_cmd_idx);
943
James Smart2e0fef82007-06-17 19:56:36 -0500944 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500945 /*
946 * All error attention handlers are posted to
947 * worker thread
948 */
949 phba->work_ha |= HA_ERATT;
950 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500951
James Smart5e9d9b82008-06-14 22:52:53 -0400952 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500953
954 return NULL;
955 }
956
957 if (pring->local_getidx == pring->next_cmdidx)
958 return NULL;
959 }
960
James Smarted957682007-06-17 19:56:37 -0500961 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500962}
963
James Smarte59058c2008-08-24 21:49:00 -0400964/**
James Smart3621a712009-04-06 18:47:14 -0400965 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400966 * @phba: Pointer to HBA context object.
967 * @iocbq: Pointer to driver iocb object.
968 *
969 * This function gets an iotag for the iocb. If there is no unused iotag and
970 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
971 * array and assigns a new iotag.
972 * The function returns the allocated iotag if successful, else returns zero.
973 * Zero is not a valid iotag.
974 * The caller is not required to hold any lock.
975 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500976uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500977lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500978{
James Smart2e0fef82007-06-17 19:56:36 -0500979 struct lpfc_iocbq **new_arr;
980 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500981 size_t new_len;
982 struct lpfc_sli *psli = &phba->sli;
983 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500984
James Smart2e0fef82007-06-17 19:56:36 -0500985 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500986 iotag = psli->last_iotag;
987 if(++iotag < psli->iocbq_lookup_len) {
988 psli->last_iotag = iotag;
989 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500990 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500991 iocbq->iotag = iotag;
992 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500993 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500994 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
995 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500996 spin_unlock_irq(&phba->hbalock);
997 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500998 GFP_KERNEL);
999 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001000 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001001 old_arr = psli->iocbq_lookup;
1002 if (new_len <= psli->iocbq_lookup_len) {
1003 /* highly unprobable case */
1004 kfree(new_arr);
1005 iotag = psli->last_iotag;
1006 if(++iotag < psli->iocbq_lookup_len) {
1007 psli->last_iotag = iotag;
1008 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001010 iocbq->iotag = iotag;
1011 return iotag;
1012 }
James Smart2e0fef82007-06-17 19:56:36 -05001013 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001014 return 0;
1015 }
1016 if (psli->iocbq_lookup)
1017 memcpy(new_arr, old_arr,
1018 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001019 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001020 psli->iocbq_lookup = new_arr;
1021 psli->iocbq_lookup_len = new_len;
1022 psli->last_iotag = iotag;
1023 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001024 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001025 iocbq->iotag = iotag;
1026 kfree(old_arr);
1027 return iotag;
1028 }
James Smart8f6d98d2006-08-01 07:34:00 -04001029 } else
James Smart2e0fef82007-06-17 19:56:36 -05001030 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001031
James Bottomley604a3e32005-10-29 10:28:33 -05001032 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001033 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1034 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001035
James Bottomley604a3e32005-10-29 10:28:33 -05001036 return 0;
dea31012005-04-17 16:05:31 -05001037}
1038
James Smarte59058c2008-08-24 21:49:00 -04001039/**
James Smart3621a712009-04-06 18:47:14 -04001040 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001041 * @phba: Pointer to HBA context object.
1042 * @pring: Pointer to driver SLI ring object.
1043 * @iocb: Pointer to iocb slot in the ring.
1044 * @nextiocb: Pointer to driver iocb object which need to be
1045 * posted to firmware.
1046 *
1047 * This function is called with hbalock held to post a new iocb to
1048 * the firmware. This function copies the new iocb to ring iocb slot and
1049 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1050 * a completion call back for this iocb else the function will free the
1051 * iocb object.
1052 **/
dea31012005-04-17 16:05:31 -05001053static void
1054lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1055 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1056{
1057 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001058 * Set up an iotag
dea31012005-04-17 16:05:31 -05001059 */
James Bottomley604a3e32005-10-29 10:28:33 -05001060 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001061
James Smarte2a0a9d2008-12-04 22:40:02 -05001062
James Smarta58cbd52007-08-02 11:09:43 -04001063 if (pring->ringno == LPFC_ELS_RING) {
1064 lpfc_debugfs_slow_ring_trc(phba,
1065 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1066 *(((uint32_t *) &nextiocb->iocb) + 4),
1067 *(((uint32_t *) &nextiocb->iocb) + 6),
1068 *(((uint32_t *) &nextiocb->iocb) + 7));
1069 }
1070
dea31012005-04-17 16:05:31 -05001071 /*
1072 * Issue iocb command to adapter
1073 */
James Smart92d7f7b2007-06-17 19:56:38 -05001074 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001075 wmb();
1076 pring->stats.iocb_cmd++;
1077
1078 /*
1079 * If there is no completion routine to call, we can release the
1080 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1081 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1082 */
1083 if (nextiocb->iocb_cmpl)
1084 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001085 else
James Smart2e0fef82007-06-17 19:56:36 -05001086 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001087
1088 /*
1089 * Let the HBA know what IOCB slot will be the next one the
1090 * driver will put a command into.
1091 */
1092 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001093 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001094}
1095
James Smarte59058c2008-08-24 21:49:00 -04001096/**
James Smart3621a712009-04-06 18:47:14 -04001097 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001098 * @phba: Pointer to HBA context object.
1099 * @pring: Pointer to driver SLI ring object.
1100 *
1101 * The caller is not required to hold any lock for calling this function.
1102 * This function updates the chip attention bits for the ring to inform firmware
1103 * that there are pending work to be done for this ring and requests an
1104 * interrupt when there is space available in the ring. This function is
1105 * called when the driver is unable to post more iocbs to the ring due
1106 * to unavailability of space in the ring.
1107 **/
dea31012005-04-17 16:05:31 -05001108static void
James Smart2e0fef82007-06-17 19:56:36 -05001109lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001110{
1111 int ringno = pring->ringno;
1112
1113 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1114
1115 wmb();
1116
1117 /*
1118 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1119 * The HBA will tell us when an IOCB entry is available.
1120 */
1121 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1122 readl(phba->CAregaddr); /* flush */
1123
1124 pring->stats.iocb_cmd_full++;
1125}
1126
James Smarte59058c2008-08-24 21:49:00 -04001127/**
James Smart3621a712009-04-06 18:47:14 -04001128 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001129 * @phba: Pointer to HBA context object.
1130 * @pring: Pointer to driver SLI ring object.
1131 *
1132 * This function updates the chip attention register bit for the
1133 * given ring to inform HBA that there is more work to be done
1134 * in this ring. The caller is not required to hold any lock.
1135 **/
dea31012005-04-17 16:05:31 -05001136static void
James Smart2e0fef82007-06-17 19:56:36 -05001137lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001138{
1139 int ringno = pring->ringno;
1140
1141 /*
1142 * Tell the HBA that there is work to do in this ring.
1143 */
James Smart34b02dc2008-08-24 21:49:55 -04001144 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1145 wmb();
1146 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1147 readl(phba->CAregaddr); /* flush */
1148 }
dea31012005-04-17 16:05:31 -05001149}
1150
James Smarte59058c2008-08-24 21:49:00 -04001151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001153 * @phba: Pointer to HBA context object.
1154 * @pring: Pointer to driver SLI ring object.
1155 *
1156 * This function is called with hbalock held to post pending iocbs
1157 * in the txq to the firmware. This function is called when driver
1158 * detects space available in the ring.
1159 **/
dea31012005-04-17 16:05:31 -05001160static void
James Smart2e0fef82007-06-17 19:56:36 -05001161lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001162{
1163 IOCB_t *iocb;
1164 struct lpfc_iocbq *nextiocb;
1165
1166 /*
1167 * Check to see if:
1168 * (a) there is anything on the txq to send
1169 * (b) link is up
1170 * (c) link attention events can be processed (fcp ring only)
1171 * (d) IOCB processing is not blocked by the outstanding mbox command.
1172 */
1173 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001174 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001175 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001176 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001177
1178 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1179 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1180 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1181
1182 if (iocb)
1183 lpfc_sli_update_ring(phba, pring);
1184 else
1185 lpfc_sli_update_full_ring(phba, pring);
1186 }
1187
1188 return;
1189}
1190
James Smarte59058c2008-08-24 21:49:00 -04001191/**
James Smart3621a712009-04-06 18:47:14 -04001192 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001193 * @phba: Pointer to HBA context object.
1194 * @hbqno: HBQ number.
1195 *
1196 * This function is called with hbalock held to get the next
1197 * available slot for the given HBQ. If there is free slot
1198 * available for the HBQ it will return pointer to the next available
1199 * HBQ entry else it will return NULL.
1200 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001201static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001202lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1203{
1204 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1205
1206 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1207 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1208 hbqp->next_hbqPutIdx = 0;
1209
1210 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001211 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001212 uint32_t getidx = le32_to_cpu(raw_index);
1213
1214 hbqp->local_hbqGetIdx = getidx;
1215
1216 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1217 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001218 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001219 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001220 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001221 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001222 hbqp->entry_count);
1223
1224 phba->link_state = LPFC_HBA_ERROR;
1225 return NULL;
1226 }
1227
1228 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1229 return NULL;
1230 }
1231
James Smart51ef4c22007-08-02 11:10:31 -04001232 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1233 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001234}
1235
James Smarte59058c2008-08-24 21:49:00 -04001236/**
James Smart3621a712009-04-06 18:47:14 -04001237 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001238 * @phba: Pointer to HBA context object.
1239 *
1240 * This function is called with no lock held to free all the
1241 * hbq buffers while uninitializing the SLI interface. It also
1242 * frees the HBQ buffers returned by the firmware but not yet
1243 * processed by the upper layers.
1244 **/
James Smarted957682007-06-17 19:56:37 -05001245void
1246lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1247{
James Smart92d7f7b2007-06-17 19:56:38 -05001248 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1249 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001250 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001251 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001252 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001253
James Smart51ef4c22007-08-02 11:10:31 -04001254 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001255 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001256 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001257 for (i = 0; i < hbq_count; ++i) {
1258 list_for_each_entry_safe(dmabuf, next_dmabuf,
1259 &phba->hbqs[i].hbq_buffer_list, list) {
1260 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1261 list_del(&hbq_buf->dbuf.list);
1262 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1263 }
James Smarta8adb832007-10-27 13:37:53 -04001264 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001265 }
James Smart3163f722008-02-08 18:50:25 -05001266 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001267 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1268 list) {
James Smart3163f722008-02-08 18:50:25 -05001269 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1270 list_del(&hbq_buf->dbuf.list);
1271 if (hbq_buf->tag == -1) {
1272 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1273 (phba, hbq_buf);
1274 } else {
1275 hbqno = hbq_buf->tag >> 16;
1276 if (hbqno >= LPFC_MAX_HBQS)
1277 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1278 (phba, hbq_buf);
1279 else
1280 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1281 hbq_buf);
1282 }
1283 }
1284
1285 /* Mark the HBQs not in use */
1286 phba->hbq_in_use = 0;
1287 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001288}
1289
James Smarte59058c2008-08-24 21:49:00 -04001290/**
James Smart3621a712009-04-06 18:47:14 -04001291 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001292 * @phba: Pointer to HBA context object.
1293 * @hbqno: HBQ number.
1294 * @hbq_buf: Pointer to HBQ buffer.
1295 *
1296 * This function is called with the hbalock held to post a
1297 * hbq buffer to the firmware. If the function finds an empty
1298 * slot in the HBQ, it will post the buffer. The function will return
1299 * pointer to the hbq entry if it successfully post the buffer
1300 * else it will return NULL.
1301 **/
James Smart3772a992009-05-22 14:50:54 -04001302static int
James Smarted957682007-06-17 19:56:37 -05001303lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001304 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001305{
James Smart3772a992009-05-22 14:50:54 -04001306 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1307}
1308
1309/**
1310 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1311 * @phba: Pointer to HBA context object.
1312 * @hbqno: HBQ number.
1313 * @hbq_buf: Pointer to HBQ buffer.
1314 *
1315 * This function is called with the hbalock held to post a hbq buffer to the
1316 * firmware. If the function finds an empty slot in the HBQ, it will post the
1317 * buffer and place it on the hbq_buffer_list. The function will return zero if
1318 * it successfully post the buffer else it will return an error.
1319 **/
1320static int
1321lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1322 struct hbq_dmabuf *hbq_buf)
1323{
James Smarted957682007-06-17 19:56:37 -05001324 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001325 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001326
1327 /* Get next HBQ entry slot to use */
1328 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1329 if (hbqe) {
1330 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1331
James Smart92d7f7b2007-06-17 19:56:38 -05001332 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1333 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001334 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001335 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001336 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1337 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1338 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001339 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1340 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001341 /* flush */
James Smarted957682007-06-17 19:56:37 -05001342 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001343 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001344 return 0;
1345 } else
1346 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001347}
1348
James Smart4f774512009-05-22 14:52:35 -04001349/**
1350 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1351 * @phba: Pointer to HBA context object.
1352 * @hbqno: HBQ number.
1353 * @hbq_buf: Pointer to HBQ buffer.
1354 *
1355 * This function is called with the hbalock held to post an RQE to the SLI4
1356 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1357 * the hbq_buffer_list and return zero, otherwise it will return an error.
1358 **/
1359static int
1360lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1361 struct hbq_dmabuf *hbq_buf)
1362{
1363 int rc;
1364 struct lpfc_rqe hrqe;
1365 struct lpfc_rqe drqe;
1366
1367 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1368 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1369 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1370 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1371 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1372 &hrqe, &drqe);
1373 if (rc < 0)
1374 return rc;
1375 hbq_buf->tag = rc;
1376 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1377 return 0;
1378}
1379
James Smarte59058c2008-08-24 21:49:00 -04001380/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001381static struct lpfc_hbq_init lpfc_els_hbq = {
1382 .rn = 1,
1383 .entry_count = 200,
1384 .mask_count = 0,
1385 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001386 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001387 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001388 .init_count = 40,
1389 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001390};
James Smarted957682007-06-17 19:56:37 -05001391
James Smarte59058c2008-08-24 21:49:00 -04001392/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001393static struct lpfc_hbq_init lpfc_extra_hbq = {
1394 .rn = 1,
1395 .entry_count = 200,
1396 .mask_count = 0,
1397 .profile = 0,
1398 .ring_mask = (1 << LPFC_EXTRA_RING),
1399 .buffer_count = 0,
1400 .init_count = 0,
1401 .add_count = 5,
1402};
1403
James Smarte59058c2008-08-24 21:49:00 -04001404/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001405struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001406 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001407 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001408};
1409
James Smarte59058c2008-08-24 21:49:00 -04001410/**
James Smart3621a712009-04-06 18:47:14 -04001411 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001412 * @phba: Pointer to HBA context object.
1413 * @hbqno: HBQ number.
1414 * @count: Number of HBQ buffers to be posted.
1415 *
James Smartd7c255b2008-08-24 21:50:00 -04001416 * This function is called with no lock held to post more hbq buffers to the
1417 * given HBQ. The function returns the number of HBQ buffers successfully
1418 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001419 **/
James Smart311464e2007-08-02 11:10:37 -04001420static int
James Smart92d7f7b2007-06-17 19:56:38 -05001421lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1422{
James Smartd7c255b2008-08-24 21:50:00 -04001423 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001424 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001425 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001426 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001427 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001428 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001429
James Smartd7c255b2008-08-24 21:50:00 -04001430 if ((phba->hbqs[hbqno].buffer_count + count) >
1431 lpfc_hbq_defs[hbqno]->entry_count)
1432 count = lpfc_hbq_defs[hbqno]->entry_count -
1433 phba->hbqs[hbqno].buffer_count;
1434 if (!count)
1435 return 0;
1436 /* Allocate HBQ entries */
1437 for (i = 0; i < count; i++) {
1438 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1439 if (!hbq_buffer)
1440 break;
1441 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1442 }
James Smart3163f722008-02-08 18:50:25 -05001443 /* Check whether HBQ is still in use */
1444 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001445 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001446 goto err;
1447 while (!list_empty(&hbq_buf_list)) {
1448 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1449 dbuf.list);
1450 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1451 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001452 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001453 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001454 posted++;
1455 } else
James Smart51ef4c22007-08-02 11:10:31 -04001456 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001457 }
James Smart3163f722008-02-08 18:50:25 -05001458 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001459 return posted;
1460err:
1461 spin_unlock_irqrestore(&phba->hbalock, flags);
1462 while (!list_empty(&hbq_buf_list)) {
1463 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1464 dbuf.list);
1465 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1466 }
James Smart92d7f7b2007-06-17 19:56:38 -05001467 return 0;
James Smarted957682007-06-17 19:56:37 -05001468}
1469
James Smarte59058c2008-08-24 21:49:00 -04001470/**
James Smart3621a712009-04-06 18:47:14 -04001471 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001472 * @phba: Pointer to HBA context object.
1473 * @qno: HBQ number.
1474 *
1475 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001476 * is called with no lock held. The function returns the number of HBQ entries
1477 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001478 **/
James Smarted957682007-06-17 19:56:37 -05001479int
James Smart92d7f7b2007-06-17 19:56:38 -05001480lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001481{
James Smart92d7f7b2007-06-17 19:56:38 -05001482 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1483 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -05001484}
1485
James Smarte59058c2008-08-24 21:49:00 -04001486/**
James Smart3621a712009-04-06 18:47:14 -04001487 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001488 * @phba: Pointer to HBA context object.
1489 * @qno: HBQ queue number.
1490 *
1491 * This function is called from SLI initialization code path with
1492 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001493 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001494 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001495static int
James Smart92d7f7b2007-06-17 19:56:38 -05001496lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001497{
James Smart92d7f7b2007-06-17 19:56:38 -05001498 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1499 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -05001500}
1501
James Smarte59058c2008-08-24 21:49:00 -04001502/**
James Smart3772a992009-05-22 14:50:54 -04001503 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1504 * @phba: Pointer to HBA context object.
1505 * @hbqno: HBQ number.
1506 *
1507 * This function removes the first hbq buffer on an hbq list and returns a
1508 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1509 **/
1510static struct hbq_dmabuf *
1511lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1512{
1513 struct lpfc_dmabuf *d_buf;
1514
1515 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1516 if (!d_buf)
1517 return NULL;
1518 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1519}
1520
1521/**
James Smart3621a712009-04-06 18:47:14 -04001522 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001523 * @phba: Pointer to HBA context object.
1524 * @tag: Tag of the hbq buffer.
1525 *
1526 * This function is called with hbalock held. This function searches
1527 * for the hbq buffer associated with the given tag in the hbq buffer
1528 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1529 * it returns NULL.
1530 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001531static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001532lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1533{
James Smart92d7f7b2007-06-17 19:56:38 -05001534 struct lpfc_dmabuf *d_buf;
1535 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001536 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001537
James Smart51ef4c22007-08-02 11:10:31 -04001538 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001539 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001540 return NULL;
1541
James Smart3772a992009-05-22 14:50:54 -04001542 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001543 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001544 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001545 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001546 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001547 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001548 }
1549 }
James Smart3772a992009-05-22 14:50:54 -04001550 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001552 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001553 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001554 return NULL;
James Smarted957682007-06-17 19:56:37 -05001555}
1556
James Smarte59058c2008-08-24 21:49:00 -04001557/**
James Smart3621a712009-04-06 18:47:14 -04001558 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001559 * @phba: Pointer to HBA context object.
1560 * @hbq_buffer: Pointer to HBQ buffer.
1561 *
1562 * This function is called with hbalock. This function gives back
1563 * the hbq buffer to firmware. If the HBQ does not have space to
1564 * post the buffer, it will free the buffer.
1565 **/
James Smarted957682007-06-17 19:56:37 -05001566void
James Smart51ef4c22007-08-02 11:10:31 -04001567lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001568{
1569 uint32_t hbqno;
1570
James Smart51ef4c22007-08-02 11:10:31 -04001571 if (hbq_buffer) {
1572 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001573 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001574 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001575 }
1576}
1577
James Smarte59058c2008-08-24 21:49:00 -04001578/**
James Smart3621a712009-04-06 18:47:14 -04001579 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001580 * @mbxCommand: mailbox command code.
1581 *
1582 * This function is called by the mailbox event handler function to verify
1583 * that the completed mailbox command is a legitimate mailbox command. If the
1584 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1585 * and the mailbox event handler will take the HBA offline.
1586 **/
dea31012005-04-17 16:05:31 -05001587static int
1588lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1589{
1590 uint8_t ret;
1591
1592 switch (mbxCommand) {
1593 case MBX_LOAD_SM:
1594 case MBX_READ_NV:
1595 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001596 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001597 case MBX_RUN_BIU_DIAG:
1598 case MBX_INIT_LINK:
1599 case MBX_DOWN_LINK:
1600 case MBX_CONFIG_LINK:
1601 case MBX_CONFIG_RING:
1602 case MBX_RESET_RING:
1603 case MBX_READ_CONFIG:
1604 case MBX_READ_RCONFIG:
1605 case MBX_READ_SPARM:
1606 case MBX_READ_STATUS:
1607 case MBX_READ_RPI:
1608 case MBX_READ_XRI:
1609 case MBX_READ_REV:
1610 case MBX_READ_LNK_STAT:
1611 case MBX_REG_LOGIN:
1612 case MBX_UNREG_LOGIN:
1613 case MBX_READ_LA:
1614 case MBX_CLEAR_LA:
1615 case MBX_DUMP_MEMORY:
1616 case MBX_DUMP_CONTEXT:
1617 case MBX_RUN_DIAGS:
1618 case MBX_RESTART:
1619 case MBX_UPDATE_CFG:
1620 case MBX_DOWN_LOAD:
1621 case MBX_DEL_LD_ENTRY:
1622 case MBX_RUN_PROGRAM:
1623 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001624 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001625 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001626 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001627 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001628 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001629 case MBX_LOAD_AREA:
1630 case MBX_RUN_BIU_DIAG64:
1631 case MBX_CONFIG_PORT:
1632 case MBX_READ_SPARM64:
1633 case MBX_READ_RPI64:
1634 case MBX_REG_LOGIN64:
1635 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001636 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001637 case MBX_SET_DEBUG:
1638 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001639 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001640 case MBX_REG_VPI:
1641 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001642 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001643 case MBX_PORT_CAPABILITIES:
1644 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001645 case MBX_SLI4_CONFIG:
1646 case MBX_SLI4_REQ_FTRS:
1647 case MBX_REG_FCFI:
1648 case MBX_UNREG_FCFI:
1649 case MBX_REG_VFI:
1650 case MBX_UNREG_VFI:
1651 case MBX_INIT_VPI:
1652 case MBX_INIT_VFI:
1653 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001654 ret = mbxCommand;
1655 break;
1656 default:
1657 ret = MBX_SHUTDOWN;
1658 break;
1659 }
James Smart2e0fef82007-06-17 19:56:36 -05001660 return ret;
dea31012005-04-17 16:05:31 -05001661}
James Smarte59058c2008-08-24 21:49:00 -04001662
1663/**
James Smart3621a712009-04-06 18:47:14 -04001664 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001665 * @phba: Pointer to HBA context object.
1666 * @pmboxq: Pointer to mailbox command.
1667 *
1668 * This is completion handler function for mailbox commands issued from
1669 * lpfc_sli_issue_mbox_wait function. This function is called by the
1670 * mailbox event handler function with no lock held. This function
1671 * will wake up thread waiting on the wait queue pointed by context1
1672 * of the mailbox.
1673 **/
James Smart04c68492009-05-22 14:52:52 -04001674void
James Smart2e0fef82007-06-17 19:56:36 -05001675lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001676{
1677 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001678 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001679
1680 /*
1681 * If pdone_q is empty, the driver thread gave up waiting and
1682 * continued running.
1683 */
James Smart7054a602007-04-25 09:52:34 -04001684 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001685 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001686 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1687 if (pdone_q)
1688 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001690 return;
1691}
1692
James Smarte59058c2008-08-24 21:49:00 -04001693
1694/**
James Smart3621a712009-04-06 18:47:14 -04001695 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001696 * @phba: Pointer to HBA context object.
1697 * @pmb: Pointer to mailbox object.
1698 *
1699 * This function is the default mailbox completion handler. It
1700 * frees the memory resources associated with the completed mailbox
1701 * command. If the completed command is a REG_LOGIN mailbox command,
1702 * this function will issue a UREG_LOGIN to re-claim the RPI.
1703 **/
dea31012005-04-17 16:05:31 -05001704void
James Smart2e0fef82007-06-17 19:56:36 -05001705lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001706{
1707 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001708 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001709 int rc;
1710
dea31012005-04-17 16:05:31 -05001711 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001712
dea31012005-04-17 16:05:31 -05001713 if (mp) {
1714 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1715 kfree(mp);
1716 }
James Smart7054a602007-04-25 09:52:34 -04001717
James Smart04c68492009-05-22 14:52:52 -04001718 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1719 (phba->sli_rev == LPFC_SLI_REV4))
1720 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1721
James Smart7054a602007-04-25 09:52:34 -04001722 /*
1723 * If a REG_LOGIN succeeded after node is destroyed or node
1724 * is in re-discovery driver need to cleanup the RPI.
1725 */
James Smart2e0fef82007-06-17 19:56:36 -05001726 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001727 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1728 !pmb->u.mb.mbxStatus) {
1729 rpi = pmb->u.mb.un.varWords[0];
1730 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1731 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001732 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001733 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1734 if (rc != MBX_NOT_FINISHED)
1735 return;
1736 }
1737
James Smart04c68492009-05-22 14:52:52 -04001738 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1739 lpfc_sli4_mbox_cmd_free(phba, pmb);
1740 else
1741 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001742}
1743
James Smarte59058c2008-08-24 21:49:00 -04001744/**
James Smart3621a712009-04-06 18:47:14 -04001745 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001746 * @phba: Pointer to HBA context object.
1747 *
1748 * This function is called with no lock held. This function processes all
1749 * the completed mailbox commands and gives it to upper layers. The interrupt
1750 * service routine processes mailbox completion interrupt and adds completed
1751 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1752 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1753 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1754 * function returns the mailbox commands to the upper layer by calling the
1755 * completion handler function of each mailbox.
1756 **/
dea31012005-04-17 16:05:31 -05001757int
James Smart2e0fef82007-06-17 19:56:36 -05001758lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001759{
James Smart92d7f7b2007-06-17 19:56:38 -05001760 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001761 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001762 int rc;
1763 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001764
1765 phba->sli.slistat.mbox_event++;
1766
James Smart92d7f7b2007-06-17 19:56:38 -05001767 /* Get all completed mailboxe buffers into the cmplq */
1768 spin_lock_irq(&phba->hbalock);
1769 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1770 spin_unlock_irq(&phba->hbalock);
1771
dea31012005-04-17 16:05:31 -05001772 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001773 do {
1774 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1775 if (pmb == NULL)
1776 break;
1777
James Smart04c68492009-05-22 14:52:52 -04001778 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001779
James Smart858c9f62007-06-17 19:56:39 -05001780 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1781 if (pmb->vport) {
1782 lpfc_debugfs_disc_trc(pmb->vport,
1783 LPFC_DISC_TRC_MBOX_VPORT,
1784 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1785 (uint32_t)pmbox->mbxCommand,
1786 pmbox->un.varWords[0],
1787 pmbox->un.varWords[1]);
1788 }
1789 else {
1790 lpfc_debugfs_disc_trc(phba->pport,
1791 LPFC_DISC_TRC_MBOX,
1792 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1793 (uint32_t)pmbox->mbxCommand,
1794 pmbox->un.varWords[0],
1795 pmbox->un.varWords[1]);
1796 }
1797 }
1798
dea31012005-04-17 16:05:31 -05001799 /*
1800 * It is a fatal error if unknown mbox command completion.
1801 */
1802 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1803 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001804 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001805 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001806 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001807 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001808 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001809 pmbox->mbxCommand,
1810 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001811 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001812 phba->work_hs = HS_FFER3;
1813 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001814 continue;
dea31012005-04-17 16:05:31 -05001815 }
1816
dea31012005-04-17 16:05:31 -05001817 if (pmbox->mbxStatus) {
1818 phba->sli.slistat.mbox_stat_err++;
1819 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1820 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001821 lpfc_printf_log(phba, KERN_INFO,
1822 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001823 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001824 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001825 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001826 pmb->vport ? pmb->vport->vpi :0,
1827 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001828 lpfc_sli4_mbox_opcode_get(phba,
1829 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001830 pmbox->mbxStatus,
1831 pmbox->un.varWords[0],
1832 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001833 pmbox->mbxStatus = 0;
1834 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001835 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001836 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001837 continue;
dea31012005-04-17 16:05:31 -05001838 }
1839 }
1840
1841 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001842 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001843 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001844 "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 -05001845 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001846 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001847 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001848 pmb->mbox_cmpl,
1849 *((uint32_t *) pmbox),
1850 pmbox->un.varWords[0],
1851 pmbox->un.varWords[1],
1852 pmbox->un.varWords[2],
1853 pmbox->un.varWords[3],
1854 pmbox->un.varWords[4],
1855 pmbox->un.varWords[5],
1856 pmbox->un.varWords[6],
1857 pmbox->un.varWords[7]);
1858
James Smart92d7f7b2007-06-17 19:56:38 -05001859 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001860 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001861 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001862 return 0;
dea31012005-04-17 16:05:31 -05001863}
James Smart92d7f7b2007-06-17 19:56:38 -05001864
James Smarte59058c2008-08-24 21:49:00 -04001865/**
James Smart3621a712009-04-06 18:47:14 -04001866 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001867 * @phba: Pointer to HBA context object.
1868 * @pring: Pointer to driver SLI ring object.
1869 * @tag: buffer tag.
1870 *
1871 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1872 * is set in the tag the buffer is posted for a particular exchange,
1873 * the function will return the buffer without replacing the buffer.
1874 * If the buffer is for unsolicited ELS or CT traffic, this function
1875 * returns the buffer and also posts another buffer to the firmware.
1876 **/
James Smart76bb24e2007-10-27 13:38:00 -04001877static struct lpfc_dmabuf *
1878lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001879 struct lpfc_sli_ring *pring,
1880 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001881{
James Smart9f1e1b52008-12-04 22:39:40 -05001882 struct hbq_dmabuf *hbq_entry;
1883
James Smart76bb24e2007-10-27 13:38:00 -04001884 if (tag & QUE_BUFTAG_BIT)
1885 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001886 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1887 if (!hbq_entry)
1888 return NULL;
1889 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001890}
James Smart57127f12007-10-27 13:37:05 -04001891
James Smart3772a992009-05-22 14:50:54 -04001892/**
1893 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1894 * @phba: Pointer to HBA context object.
1895 * @pring: Pointer to driver SLI ring object.
1896 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1897 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1898 * @fch_type: the type for the first frame of the sequence.
1899 *
1900 * This function is called with no lock held. This function uses the r_ctl and
1901 * type of the received sequence to find the correct callback function to call
1902 * to process the sequence.
1903 **/
1904static int
1905lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1906 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1907 uint32_t fch_type)
1908{
1909 int i;
1910
1911 /* unSolicited Responses */
1912 if (pring->prt[0].profile) {
1913 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1914 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1915 saveq);
1916 return 1;
1917 }
1918 /* We must search, based on rctl / type
1919 for the right routine */
1920 for (i = 0; i < pring->num_mask; i++) {
1921 if ((pring->prt[i].rctl == fch_r_ctl) &&
1922 (pring->prt[i].type == fch_type)) {
1923 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1924 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1925 (phba, pring, saveq);
1926 return 1;
1927 }
1928 }
1929 return 0;
1930}
James Smarte59058c2008-08-24 21:49:00 -04001931
1932/**
James Smart3621a712009-04-06 18:47:14 -04001933 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001934 * @phba: Pointer to HBA context object.
1935 * @pring: Pointer to driver SLI ring object.
1936 * @saveq: Pointer to the unsolicited iocb.
1937 *
1938 * This function is called with no lock held by the ring event handler
1939 * when there is an unsolicited iocb posted to the response ring by the
1940 * firmware. This function gets the buffer associated with the iocbs
1941 * and calls the event handler for the ring. This function handles both
1942 * qring buffers and hbq buffers.
1943 * When the function returns 1 the caller can free the iocb object otherwise
1944 * upper layer functions will free the iocb objects.
1945 **/
dea31012005-04-17 16:05:31 -05001946static int
1947lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1948 struct lpfc_iocbq *saveq)
1949{
1950 IOCB_t * irsp;
1951 WORD5 * w5p;
1952 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001953 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001954 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001955 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001956
1957 match = 0;
1958 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001959
1960 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1961 if (pring->lpfc_sli_rcv_async_status)
1962 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1963 else
1964 lpfc_printf_log(phba,
1965 KERN_WARNING,
1966 LOG_SLI,
1967 "0316 Ring %d handler: unexpected "
1968 "ASYNC_STATUS iocb received evt_code "
1969 "0x%x\n",
1970 pring->ringno,
1971 irsp->un.asyncstat.evt_code);
1972 return 1;
1973 }
1974
James Smart3163f722008-02-08 18:50:25 -05001975 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1976 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1977 if (irsp->ulpBdeCount > 0) {
1978 dmzbuf = lpfc_sli_get_buff(phba, pring,
1979 irsp->un.ulpWord[3]);
1980 lpfc_in_buf_free(phba, dmzbuf);
1981 }
1982
1983 if (irsp->ulpBdeCount > 1) {
1984 dmzbuf = lpfc_sli_get_buff(phba, pring,
1985 irsp->unsli3.sli3Words[3]);
1986 lpfc_in_buf_free(phba, dmzbuf);
1987 }
1988
1989 if (irsp->ulpBdeCount > 2) {
1990 dmzbuf = lpfc_sli_get_buff(phba, pring,
1991 irsp->unsli3.sli3Words[7]);
1992 lpfc_in_buf_free(phba, dmzbuf);
1993 }
1994
1995 return 1;
1996 }
1997
James Smart92d7f7b2007-06-17 19:56:38 -05001998 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001999 if (irsp->ulpBdeCount != 0) {
2000 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002001 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002002 if (!saveq->context2)
2003 lpfc_printf_log(phba,
2004 KERN_ERR,
2005 LOG_SLI,
2006 "0341 Ring %d Cannot find buffer for "
2007 "an unsolicited iocb. tag 0x%x\n",
2008 pring->ringno,
2009 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002010 }
2011 if (irsp->ulpBdeCount == 2) {
2012 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002013 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002014 if (!saveq->context3)
2015 lpfc_printf_log(phba,
2016 KERN_ERR,
2017 LOG_SLI,
2018 "0342 Ring %d Cannot find buffer for an"
2019 " unsolicited iocb. tag 0x%x\n",
2020 pring->ringno,
2021 irsp->unsli3.sli3Words[7]);
2022 }
2023 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002024 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002025 if (irsp->ulpBdeCount != 0) {
2026 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2027 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002028 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002029 lpfc_printf_log(phba,
2030 KERN_ERR,
2031 LOG_SLI,
2032 "0343 Ring %d Cannot find "
2033 "buffer for an unsolicited iocb"
2034 ". tag 0x%x\n", pring->ringno,
2035 irsp->un.ulpWord[3]);
2036 }
2037 if (irsp->ulpBdeCount == 2) {
2038 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2039 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002040 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002041 lpfc_printf_log(phba,
2042 KERN_ERR,
2043 LOG_SLI,
2044 "0344 Ring %d Cannot find "
2045 "buffer for an unsolicited "
2046 "iocb. tag 0x%x\n",
2047 pring->ringno,
2048 irsp->unsli3.sli3Words[7]);
2049 }
2050 }
James Smart92d7f7b2007-06-17 19:56:38 -05002051 }
James Smart9c2face2008-01-11 01:53:18 -05002052 if (irsp->ulpBdeCount != 0 &&
2053 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2054 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2055 int found = 0;
2056
2057 /* search continue save q for same XRI */
2058 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2059 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2060 list_add_tail(&saveq->list, &iocbq->list);
2061 found = 1;
2062 break;
2063 }
2064 }
2065 if (!found)
2066 list_add_tail(&saveq->clist,
2067 &pring->iocb_continue_saveq);
2068 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2069 list_del_init(&iocbq->clist);
2070 saveq = iocbq;
2071 irsp = &(saveq->iocb);
2072 } else
2073 return 0;
2074 }
2075 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2076 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2077 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002078 Rctl = FC_RCTL_ELS_REQ;
2079 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002080 } else {
2081 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2082 Rctl = w5p->hcsw.Rctl;
2083 Type = w5p->hcsw.Type;
2084
2085 /* Firmware Workaround */
2086 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2087 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2088 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002089 Rctl = FC_RCTL_ELS_REQ;
2090 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002091 w5p->hcsw.Rctl = Rctl;
2092 w5p->hcsw.Type = Type;
2093 }
2094 }
James Smart92d7f7b2007-06-17 19:56:38 -05002095
James Smart3772a992009-05-22 14:50:54 -04002096 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002097 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002098 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002099 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002100 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002101
James Smart92d7f7b2007-06-17 19:56:38 -05002102 return 1;
dea31012005-04-17 16:05:31 -05002103}
2104
James Smarte59058c2008-08-24 21:49:00 -04002105/**
James Smart3621a712009-04-06 18:47:14 -04002106 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002107 * @phba: Pointer to HBA context object.
2108 * @pring: Pointer to driver SLI ring object.
2109 * @prspiocb: Pointer to response iocb object.
2110 *
2111 * This function looks up the iocb_lookup table to get the command iocb
2112 * corresponding to the given response iocb using the iotag of the
2113 * response iocb. This function is called with the hbalock held.
2114 * This function returns the command iocb object if it finds the command
2115 * iocb else returns NULL.
2116 **/
dea31012005-04-17 16:05:31 -05002117static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002118lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2119 struct lpfc_sli_ring *pring,
2120 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002121{
dea31012005-04-17 16:05:31 -05002122 struct lpfc_iocbq *cmd_iocb = NULL;
2123 uint16_t iotag;
2124
James Bottomley604a3e32005-10-29 10:28:33 -05002125 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002126
James Bottomley604a3e32005-10-29 10:28:33 -05002127 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2128 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002129 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002130 pring->txcmplq_cnt--;
2131 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002132 }
2133
dea31012005-04-17 16:05:31 -05002134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002135 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002136 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002137 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002138 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002139 return NULL;
2140}
2141
James Smarte59058c2008-08-24 21:49:00 -04002142/**
James Smart3772a992009-05-22 14:50:54 -04002143 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2144 * @phba: Pointer to HBA context object.
2145 * @pring: Pointer to driver SLI ring object.
2146 * @iotag: IOCB tag.
2147 *
2148 * This function looks up the iocb_lookup table to get the command iocb
2149 * corresponding to the given iotag. This function is called with the
2150 * hbalock held.
2151 * This function returns the command iocb object if it finds the command
2152 * iocb else returns NULL.
2153 **/
2154static struct lpfc_iocbq *
2155lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2156 struct lpfc_sli_ring *pring, uint16_t iotag)
2157{
2158 struct lpfc_iocbq *cmd_iocb;
2159
2160 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2161 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2162 list_del_init(&cmd_iocb->list);
2163 pring->txcmplq_cnt--;
2164 return cmd_iocb;
2165 }
2166
2167 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2168 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2169 iotag, phba->sli.last_iotag);
2170 return NULL;
2171}
2172
2173/**
James Smart3621a712009-04-06 18:47:14 -04002174 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002175 * @phba: Pointer to HBA context object.
2176 * @pring: Pointer to driver SLI ring object.
2177 * @saveq: Pointer to the response iocb to be processed.
2178 *
2179 * This function is called by the ring event handler for non-fcp
2180 * rings when there is a new response iocb in the response ring.
2181 * The caller is not required to hold any locks. This function
2182 * gets the command iocb associated with the response iocb and
2183 * calls the completion handler for the command iocb. If there
2184 * is no completion handler, the function will free the resources
2185 * associated with command iocb. If the response iocb is for
2186 * an already aborted command iocb, the status of the completion
2187 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2188 * This function always returns 1.
2189 **/
dea31012005-04-17 16:05:31 -05002190static int
James Smart2e0fef82007-06-17 19:56:36 -05002191lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002192 struct lpfc_iocbq *saveq)
2193{
James Smart2e0fef82007-06-17 19:56:36 -05002194 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002195 int rc = 1;
2196 unsigned long iflag;
2197
2198 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002199 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002200 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002201 spin_unlock_irqrestore(&phba->hbalock, iflag);
2202
dea31012005-04-17 16:05:31 -05002203 if (cmdiocbp) {
2204 if (cmdiocbp->iocb_cmpl) {
2205 /*
James Smartea2151b2008-09-07 11:52:10 -04002206 * If an ELS command failed send an event to mgmt
2207 * application.
2208 */
2209 if (saveq->iocb.ulpStatus &&
2210 (pring->ringno == LPFC_ELS_RING) &&
2211 (cmdiocbp->iocb.ulpCommand ==
2212 CMD_ELS_REQUEST64_CR))
2213 lpfc_send_els_failure_event(phba,
2214 cmdiocbp, saveq);
2215
2216 /*
dea31012005-04-17 16:05:31 -05002217 * Post all ELS completions to the worker thread.
2218 * All other are passed to the completion callback.
2219 */
2220 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04002221 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2222 cmdiocbp->iocb_flag &=
2223 ~LPFC_DRIVER_ABORTED;
2224 saveq->iocb.ulpStatus =
2225 IOSTAT_LOCAL_REJECT;
2226 saveq->iocb.un.ulpWord[4] =
2227 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002228
2229 /* Firmware could still be in progress
2230 * of DMAing payload, so don't free data
2231 * buffer till after a hbeat.
2232 */
2233 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04002234 }
dea31012005-04-17 16:05:31 -05002235 }
James Smart2e0fef82007-06-17 19:56:36 -05002236 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002237 } else
2238 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002239 } else {
2240 /*
2241 * Unknown initiating command based on the response iotag.
2242 * This could be the case on the ELS ring because of
2243 * lpfc_els_abort().
2244 */
2245 if (pring->ringno != LPFC_ELS_RING) {
2246 /*
2247 * Ring <ringno> handler: unexpected completion IoTag
2248 * <IoTag>
2249 */
James Smarta257bf92009-04-06 18:48:10 -04002250 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002251 "0322 Ring %d handler: "
2252 "unexpected completion IoTag x%x "
2253 "Data: x%x x%x x%x x%x\n",
2254 pring->ringno,
2255 saveq->iocb.ulpIoTag,
2256 saveq->iocb.ulpStatus,
2257 saveq->iocb.un.ulpWord[4],
2258 saveq->iocb.ulpCommand,
2259 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002260 }
2261 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002262
dea31012005-04-17 16:05:31 -05002263 return rc;
2264}
2265
James Smarte59058c2008-08-24 21:49:00 -04002266/**
James Smart3621a712009-04-06 18:47:14 -04002267 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002268 * @phba: Pointer to HBA context object.
2269 * @pring: Pointer to driver SLI ring object.
2270 *
2271 * This function is called from the iocb ring event handlers when
2272 * put pointer is ahead of the get pointer for a ring. This function signal
2273 * an error attention condition to the worker thread and the worker
2274 * thread will transition the HBA to offline state.
2275 **/
James Smart2e0fef82007-06-17 19:56:36 -05002276static void
2277lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002278{
James Smart34b02dc2008-08-24 21:49:55 -04002279 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002280 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002281 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002282 * rsp ring <portRspMax>
2283 */
2284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002285 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002286 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002287 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002288 pring->numRiocb);
2289
James Smart2e0fef82007-06-17 19:56:36 -05002290 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002291
2292 /*
2293 * All error attention handlers are posted to
2294 * worker thread
2295 */
2296 phba->work_ha |= HA_ERATT;
2297 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002298
James Smart5e9d9b82008-06-14 22:52:53 -04002299 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002300
2301 return;
2302}
2303
James Smarte59058c2008-08-24 21:49:00 -04002304/**
James Smart3621a712009-04-06 18:47:14 -04002305 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002306 * @ptr: Pointer to address of HBA context object.
2307 *
2308 * This function is invoked by the Error Attention polling timer when the
2309 * timer times out. It will check the SLI Error Attention register for
2310 * possible attention events. If so, it will post an Error Attention event
2311 * and wake up worker thread to process it. Otherwise, it will set up the
2312 * Error Attention polling timer for the next poll.
2313 **/
2314void lpfc_poll_eratt(unsigned long ptr)
2315{
2316 struct lpfc_hba *phba;
2317 uint32_t eratt = 0;
2318
2319 phba = (struct lpfc_hba *)ptr;
2320
2321 /* Check chip HA register for error event */
2322 eratt = lpfc_sli_check_eratt(phba);
2323
2324 if (eratt)
2325 /* Tell the worker thread there is work to do */
2326 lpfc_worker_wake_up(phba);
2327 else
2328 /* Restart the timer for next eratt poll */
2329 mod_timer(&phba->eratt_poll, jiffies +
2330 HZ * LPFC_ERATT_POLL_INTERVAL);
2331 return;
2332}
2333
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002334
James Smarte59058c2008-08-24 21:49:00 -04002335/**
James Smart3621a712009-04-06 18:47:14 -04002336 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002337 * @phba: Pointer to HBA context object.
2338 * @pring: Pointer to driver SLI ring object.
2339 * @mask: Host attention register mask for this ring.
2340 *
2341 * This function is called from the interrupt context when there is a ring
2342 * event for the fcp ring. The caller does not hold any lock.
2343 * The function processes each response iocb in the response ring until it
2344 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2345 * LE bit set. The function will call the completion handler of the command iocb
2346 * if the response iocb indicates a completion for a command iocb or it is
2347 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2348 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002349 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002350 * to check it explicitly.
2351 */
2352int
James Smart2e0fef82007-06-17 19:56:36 -05002353lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2354 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002355{
James Smart34b02dc2008-08-24 21:49:55 -04002356 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002357 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002358 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002359 struct lpfc_iocbq *cmdiocbq = NULL;
2360 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002361 uint32_t status;
2362 uint32_t portRspPut, portRspMax;
2363 int rc = 1;
2364 lpfc_iocb_type type;
2365 unsigned long iflag;
2366 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002367
James Smart2e0fef82007-06-17 19:56:36 -05002368 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002369 pring->stats.iocb_event++;
2370
dea31012005-04-17 16:05:31 -05002371 /*
2372 * The next available response entry should never exceed the maximum
2373 * entries. If it does, treat it as an adapter hardware error.
2374 */
2375 portRspMax = pring->numRiocb;
2376 portRspPut = le32_to_cpu(pgp->rspPutInx);
2377 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002378 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002379 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002380 return 1;
2381 }
James Smart45ed1192009-10-02 15:17:02 -04002382 if (phba->fcp_ring_in_use) {
2383 spin_unlock_irqrestore(&phba->hbalock, iflag);
2384 return 1;
2385 } else
2386 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002387
2388 rmb();
2389 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002390 /*
2391 * Fetch an entry off the ring and copy it into a local data
2392 * structure. The copy involves a byte-swap since the
2393 * network byte order and pci byte orders are different.
2394 */
James Smarted957682007-06-17 19:56:37 -05002395 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002396 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002397
2398 if (++pring->rspidx >= portRspMax)
2399 pring->rspidx = 0;
2400
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002401 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2402 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002403 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002404 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002405 irsp = &rspiocbq.iocb;
2406
dea31012005-04-17 16:05:31 -05002407 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2408 pring->stats.iocb_rsp++;
2409 rsp_cmpl++;
2410
2411 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002412 /*
2413 * If resource errors reported from HBA, reduce
2414 * queuedepths of the SCSI device.
2415 */
2416 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2417 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2418 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002419 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002420 spin_lock_irqsave(&phba->hbalock, iflag);
2421 }
2422
dea31012005-04-17 16:05:31 -05002423 /* Rsp ring <ringno> error: IOCB */
2424 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002425 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002426 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002427 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002428 irsp->un.ulpWord[0],
2429 irsp->un.ulpWord[1],
2430 irsp->un.ulpWord[2],
2431 irsp->un.ulpWord[3],
2432 irsp->un.ulpWord[4],
2433 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002434 *(uint32_t *)&irsp->un1,
2435 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002436 }
2437
2438 switch (type) {
2439 case LPFC_ABORT_IOCB:
2440 case LPFC_SOL_IOCB:
2441 /*
2442 * Idle exchange closed via ABTS from port. No iocb
2443 * resources need to be recovered.
2444 */
2445 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002446 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002447 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002448 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002449 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002450 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002451 break;
2452 }
2453
James Bottomley604a3e32005-10-29 10:28:33 -05002454 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2455 &rspiocbq);
dea31012005-04-17 16:05:31 -05002456 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
James Smart2e0fef82007-06-17 19:56:36 -05002457 spin_unlock_irqrestore(&phba->hbalock,
2458 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002459 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2460 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002461 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002462 iflag);
2463 }
dea31012005-04-17 16:05:31 -05002464 break;
James Smarta4bc3372006-12-02 13:34:16 -05002465 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002466 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002467 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002468 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002469 break;
dea31012005-04-17 16:05:31 -05002470 default:
2471 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2472 char adaptermsg[LPFC_MAX_ADPTMSG];
2473 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2474 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2475 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002476 dev_warn(&((phba->pcidev)->dev),
2477 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002478 phba->brd_no, adaptermsg);
2479 } else {
2480 /* Unknown IOCB command */
2481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002482 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002483 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002484 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002485 irsp->ulpStatus,
2486 irsp->ulpIoTag,
2487 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002488 }
2489 break;
2490 }
2491
2492 /*
2493 * The response IOCB has been processed. Update the ring
2494 * pointer in SLIM. If the port response put pointer has not
2495 * been updated, sync the pgp->rspPutInx and fetch the new port
2496 * response put pointer.
2497 */
James Smarted957682007-06-17 19:56:37 -05002498 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002499
2500 if (pring->rspidx == portRspPut)
2501 portRspPut = le32_to_cpu(pgp->rspPutInx);
2502 }
2503
2504 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2505 pring->stats.iocb_rsp_full++;
2506 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2507 writel(status, phba->CAregaddr);
2508 readl(phba->CAregaddr);
2509 }
2510 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2511 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2512 pring->stats.iocb_cmd_empty++;
2513
2514 /* Force update of the local copy of cmdGetInx */
2515 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2516 lpfc_sli_resume_iocb(phba, pring);
2517
2518 if ((pring->lpfc_sli_cmd_available))
2519 (pring->lpfc_sli_cmd_available) (phba, pring);
2520
2521 }
2522
James Smart45ed1192009-10-02 15:17:02 -04002523 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002524 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002525 return rc;
2526}
2527
James Smarte59058c2008-08-24 21:49:00 -04002528/**
James Smart3772a992009-05-22 14:50:54 -04002529 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2530 * @phba: Pointer to HBA context object.
2531 * @pring: Pointer to driver SLI ring object.
2532 * @rspiocbp: Pointer to driver response IOCB object.
2533 *
2534 * This function is called from the worker thread when there is a slow-path
2535 * response IOCB to process. This function chains all the response iocbs until
2536 * seeing the iocb with the LE bit set. The function will call
2537 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2538 * completion of a command iocb. The function will call the
2539 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2540 * The function frees the resources or calls the completion handler if this
2541 * iocb is an abort completion. The function returns NULL when the response
2542 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2543 * this function shall chain the iocb on to the iocb_continueq and return the
2544 * response iocb passed in.
2545 **/
2546static struct lpfc_iocbq *
2547lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2548 struct lpfc_iocbq *rspiocbp)
2549{
2550 struct lpfc_iocbq *saveq;
2551 struct lpfc_iocbq *cmdiocbp;
2552 struct lpfc_iocbq *next_iocb;
2553 IOCB_t *irsp = NULL;
2554 uint32_t free_saveq;
2555 uint8_t iocb_cmd_type;
2556 lpfc_iocb_type type;
2557 unsigned long iflag;
2558 int rc;
2559
2560 spin_lock_irqsave(&phba->hbalock, iflag);
2561 /* First add the response iocb to the countinueq list */
2562 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2563 pring->iocb_continueq_cnt++;
2564
2565 /* Now, determine whetehr the list is completed for processing */
2566 irsp = &rspiocbp->iocb;
2567 if (irsp->ulpLe) {
2568 /*
2569 * By default, the driver expects to free all resources
2570 * associated with this iocb completion.
2571 */
2572 free_saveq = 1;
2573 saveq = list_get_first(&pring->iocb_continueq,
2574 struct lpfc_iocbq, list);
2575 irsp = &(saveq->iocb);
2576 list_del_init(&pring->iocb_continueq);
2577 pring->iocb_continueq_cnt = 0;
2578
2579 pring->stats.iocb_rsp++;
2580
2581 /*
2582 * If resource errors reported from HBA, reduce
2583 * queuedepths of the SCSI device.
2584 */
2585 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2586 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2587 spin_unlock_irqrestore(&phba->hbalock, iflag);
2588 phba->lpfc_rampdown_queue_depth(phba);
2589 spin_lock_irqsave(&phba->hbalock, iflag);
2590 }
2591
2592 if (irsp->ulpStatus) {
2593 /* Rsp ring <ringno> error: IOCB */
2594 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2595 "0328 Rsp Ring %d error: "
2596 "IOCB Data: "
2597 "x%x x%x x%x x%x "
2598 "x%x x%x x%x x%x "
2599 "x%x x%x x%x x%x "
2600 "x%x x%x x%x x%x\n",
2601 pring->ringno,
2602 irsp->un.ulpWord[0],
2603 irsp->un.ulpWord[1],
2604 irsp->un.ulpWord[2],
2605 irsp->un.ulpWord[3],
2606 irsp->un.ulpWord[4],
2607 irsp->un.ulpWord[5],
2608 *(((uint32_t *) irsp) + 6),
2609 *(((uint32_t *) irsp) + 7),
2610 *(((uint32_t *) irsp) + 8),
2611 *(((uint32_t *) irsp) + 9),
2612 *(((uint32_t *) irsp) + 10),
2613 *(((uint32_t *) irsp) + 11),
2614 *(((uint32_t *) irsp) + 12),
2615 *(((uint32_t *) irsp) + 13),
2616 *(((uint32_t *) irsp) + 14),
2617 *(((uint32_t *) irsp) + 15));
2618 }
2619
2620 /*
2621 * Fetch the IOCB command type and call the correct completion
2622 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2623 * get freed back to the lpfc_iocb_list by the discovery
2624 * kernel thread.
2625 */
2626 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2627 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2628 switch (type) {
2629 case LPFC_SOL_IOCB:
2630 spin_unlock_irqrestore(&phba->hbalock, iflag);
2631 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2632 spin_lock_irqsave(&phba->hbalock, iflag);
2633 break;
2634
2635 case LPFC_UNSOL_IOCB:
2636 spin_unlock_irqrestore(&phba->hbalock, iflag);
2637 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2638 spin_lock_irqsave(&phba->hbalock, iflag);
2639 if (!rc)
2640 free_saveq = 0;
2641 break;
2642
2643 case LPFC_ABORT_IOCB:
2644 cmdiocbp = NULL;
2645 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2646 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2647 saveq);
2648 if (cmdiocbp) {
2649 /* Call the specified completion routine */
2650 if (cmdiocbp->iocb_cmpl) {
2651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2653 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2654 saveq);
2655 spin_lock_irqsave(&phba->hbalock,
2656 iflag);
2657 } else
2658 __lpfc_sli_release_iocbq(phba,
2659 cmdiocbp);
2660 }
2661 break;
2662
2663 case LPFC_UNKNOWN_IOCB:
2664 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2665 char adaptermsg[LPFC_MAX_ADPTMSG];
2666 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2667 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2668 MAX_MSG_DATA);
2669 dev_warn(&((phba->pcidev)->dev),
2670 "lpfc%d: %s\n",
2671 phba->brd_no, adaptermsg);
2672 } else {
2673 /* Unknown IOCB command */
2674 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2675 "0335 Unknown IOCB "
2676 "command Data: x%x "
2677 "x%x x%x x%x\n",
2678 irsp->ulpCommand,
2679 irsp->ulpStatus,
2680 irsp->ulpIoTag,
2681 irsp->ulpContext);
2682 }
2683 break;
2684 }
2685
2686 if (free_saveq) {
2687 list_for_each_entry_safe(rspiocbp, next_iocb,
2688 &saveq->list, list) {
2689 list_del(&rspiocbp->list);
2690 __lpfc_sli_release_iocbq(phba, rspiocbp);
2691 }
2692 __lpfc_sli_release_iocbq(phba, saveq);
2693 }
2694 rspiocbp = NULL;
2695 }
2696 spin_unlock_irqrestore(&phba->hbalock, iflag);
2697 return rspiocbp;
2698}
2699
2700/**
2701 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002702 * @phba: Pointer to HBA context object.
2703 * @pring: Pointer to driver SLI ring object.
2704 * @mask: Host attention register mask for this ring.
2705 *
James Smart3772a992009-05-22 14:50:54 -04002706 * This routine wraps the actual slow_ring event process routine from the
2707 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002708 **/
James Smart3772a992009-05-22 14:50:54 -04002709void
James Smart2e0fef82007-06-17 19:56:36 -05002710lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2711 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002712{
James Smart3772a992009-05-22 14:50:54 -04002713 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2714}
2715
2716/**
2717 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2718 * @phba: Pointer to HBA context object.
2719 * @pring: Pointer to driver SLI ring object.
2720 * @mask: Host attention register mask for this ring.
2721 *
2722 * This function is called from the worker thread when there is a ring event
2723 * for non-fcp rings. The caller does not hold any lock. The function will
2724 * remove each response iocb in the response ring and calls the handle
2725 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2726 **/
2727static void
2728lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2729 struct lpfc_sli_ring *pring, uint32_t mask)
2730{
James Smart34b02dc2008-08-24 21:49:55 -04002731 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002732 IOCB_t *entry;
2733 IOCB_t *irsp = NULL;
2734 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002735 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002736 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002737 uint32_t status;
dea31012005-04-17 16:05:31 -05002738
James Smart34b02dc2008-08-24 21:49:55 -04002739 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002740 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002741 pring->stats.iocb_event++;
2742
dea31012005-04-17 16:05:31 -05002743 /*
2744 * The next available response entry should never exceed the maximum
2745 * entries. If it does, treat it as an adapter hardware error.
2746 */
2747 portRspMax = pring->numRiocb;
2748 portRspPut = le32_to_cpu(pgp->rspPutInx);
2749 if (portRspPut >= portRspMax) {
2750 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002752 * rsp ring <portRspMax>
2753 */
James Smarted957682007-06-17 19:56:37 -05002754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002755 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002756 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002757 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002758
James Smart2e0fef82007-06-17 19:56:36 -05002759 phba->link_state = LPFC_HBA_ERROR;
2760 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002761
2762 phba->work_hs = HS_FFER3;
2763 lpfc_handle_eratt(phba);
2764
James Smart3772a992009-05-22 14:50:54 -04002765 return;
dea31012005-04-17 16:05:31 -05002766 }
2767
2768 rmb();
dea31012005-04-17 16:05:31 -05002769 while (pring->rspidx != portRspPut) {
2770 /*
2771 * Build a completion list and call the appropriate handler.
2772 * The process is to get the next available response iocb, get
2773 * a free iocb from the list, copy the response data into the
2774 * free iocb, insert to the continuation list, and update the
2775 * next response index to slim. This process makes response
2776 * iocb's in the ring available to DMA as fast as possible but
2777 * pays a penalty for a copy operation. Since the iocb is
2778 * only 32 bytes, this penalty is considered small relative to
2779 * the PCI reads for register values and a slim write. When
2780 * the ulpLe field is set, the entire Command has been
2781 * received.
2782 */
James Smarted957682007-06-17 19:56:37 -05002783 entry = lpfc_resp_iocb(phba, pring);
2784
James Smart858c9f62007-06-17 19:56:39 -05002785 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002786 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002787 if (rspiocbp == NULL) {
2788 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002789 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002790 break;
2791 }
2792
James Smarted957682007-06-17 19:56:37 -05002793 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2794 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002795 irsp = &rspiocbp->iocb;
2796
2797 if (++pring->rspidx >= portRspMax)
2798 pring->rspidx = 0;
2799
James Smarta58cbd52007-08-02 11:09:43 -04002800 if (pring->ringno == LPFC_ELS_RING) {
2801 lpfc_debugfs_slow_ring_trc(phba,
2802 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2803 *(((uint32_t *) irsp) + 4),
2804 *(((uint32_t *) irsp) + 6),
2805 *(((uint32_t *) irsp) + 7));
2806 }
2807
James Smarted957682007-06-17 19:56:37 -05002808 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002809
James Smart3772a992009-05-22 14:50:54 -04002810 spin_unlock_irqrestore(&phba->hbalock, iflag);
2811 /* Handle the response IOCB */
2812 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2813 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002814
2815 /*
2816 * If the port response put pointer has not been updated, sync
2817 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2818 * response put pointer.
2819 */
2820 if (pring->rspidx == portRspPut) {
2821 portRspPut = le32_to_cpu(pgp->rspPutInx);
2822 }
2823 } /* while (pring->rspidx != portRspPut) */
2824
James Smart92d7f7b2007-06-17 19:56:38 -05002825 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002826 /* At least one response entry has been freed */
2827 pring->stats.iocb_rsp_full++;
2828 /* SET RxRE_RSP in Chip Att register */
2829 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2830 writel(status, phba->CAregaddr);
2831 readl(phba->CAregaddr); /* flush */
2832 }
2833 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2834 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2835 pring->stats.iocb_cmd_empty++;
2836
2837 /* Force update of the local copy of cmdGetInx */
2838 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2839 lpfc_sli_resume_iocb(phba, pring);
2840
2841 if ((pring->lpfc_sli_cmd_available))
2842 (pring->lpfc_sli_cmd_available) (phba, pring);
2843
2844 }
2845
James Smart2e0fef82007-06-17 19:56:36 -05002846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002847 return;
dea31012005-04-17 16:05:31 -05002848}
2849
James Smarte59058c2008-08-24 21:49:00 -04002850/**
James Smart4f774512009-05-22 14:52:35 -04002851 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2852 * @phba: Pointer to HBA context object.
2853 * @pring: Pointer to driver SLI ring object.
2854 * @mask: Host attention register mask for this ring.
2855 *
2856 * This function is called from the worker thread when there is a pending
2857 * ELS response iocb on the driver internal slow-path response iocb worker
2858 * queue. The caller does not hold any lock. The function will remove each
2859 * response iocb from the response worker queue and calls the handle
2860 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2861 **/
2862static void
2863lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2864 struct lpfc_sli_ring *pring, uint32_t mask)
2865{
2866 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002867 struct hbq_dmabuf *dmabuf;
2868 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002869 unsigned long iflag;
2870
James Smart45ed1192009-10-02 15:17:02 -04002871 spin_lock_irqsave(&phba->hbalock, iflag);
2872 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2873 spin_unlock_irqrestore(&phba->hbalock, iflag);
2874 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002875 /* Get the response iocb from the head of work queue */
2876 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002877 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002878 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002879 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002880
2881 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2882 case CQE_CODE_COMPL_WQE:
2883 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2884 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04002885 /* Translate ELS WCQE to response IOCBQ */
2886 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2887 irspiocbq);
2888 if (irspiocbq)
2889 lpfc_sli_sp_handle_rspiocb(phba, pring,
2890 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04002891 break;
2892 case CQE_CODE_RECEIVE:
2893 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2894 cq_event);
2895 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2896 break;
2897 default:
2898 break;
2899 }
James Smart4f774512009-05-22 14:52:35 -04002900 }
2901}
2902
2903/**
James Smart3621a712009-04-06 18:47:14 -04002904 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002905 * @phba: Pointer to HBA context object.
2906 * @pring: Pointer to driver SLI ring object.
2907 *
2908 * This function aborts all iocbs in the given ring and frees all the iocb
2909 * objects in txq. This function issues an abort iocb for all the iocb commands
2910 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2911 * the return of this function. The caller is not required to hold any locks.
2912 **/
James Smart2e0fef82007-06-17 19:56:36 -05002913void
dea31012005-04-17 16:05:31 -05002914lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2915{
James Smart2534ba72007-04-25 09:52:20 -04002916 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002917 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002918
James Smart92d7f7b2007-06-17 19:56:38 -05002919 if (pring->ringno == LPFC_ELS_RING) {
2920 lpfc_fabric_abort_hba(phba);
2921 }
2922
dea31012005-04-17 16:05:31 -05002923 /* Error everything on txq and txcmplq
2924 * First do the txq.
2925 */
James Smart2e0fef82007-06-17 19:56:36 -05002926 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002927 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002928 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002929
2930 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002931 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2932 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2933
James Smart2e0fef82007-06-17 19:56:36 -05002934 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002935
James Smarta257bf92009-04-06 18:48:10 -04002936 /* Cancel all the IOCBs from the completions list */
2937 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2938 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05002939}
2940
James Smarte59058c2008-08-24 21:49:00 -04002941/**
James Smart3621a712009-04-06 18:47:14 -04002942 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04002943 * @phba: Pointer to HBA context object.
2944 *
2945 * This function flushes all iocbs in the fcp ring and frees all the iocb
2946 * objects in txq and txcmplq. This function will not issue abort iocbs
2947 * for all the iocb commands in txcmplq, they will just be returned with
2948 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2949 * slot has been permanently disabled.
2950 **/
2951void
2952lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2953{
2954 LIST_HEAD(txq);
2955 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04002956 struct lpfc_sli *psli = &phba->sli;
2957 struct lpfc_sli_ring *pring;
2958
2959 /* Currently, only one fcp ring */
2960 pring = &psli->ring[psli->fcp_ring];
2961
2962 spin_lock_irq(&phba->hbalock);
2963 /* Retrieve everything on txq */
2964 list_splice_init(&pring->txq, &txq);
2965 pring->txq_cnt = 0;
2966
2967 /* Retrieve everything on the txcmplq */
2968 list_splice_init(&pring->txcmplq, &txcmplq);
2969 pring->txcmplq_cnt = 0;
2970 spin_unlock_irq(&phba->hbalock);
2971
2972 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04002973 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
2974 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002975
2976 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04002977 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
2978 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04002979}
2980
2981/**
James Smart3772a992009-05-22 14:50:54 -04002982 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04002983 * @phba: Pointer to HBA context object.
2984 * @mask: Bit mask to be checked.
2985 *
2986 * This function reads the host status register and compares
2987 * with the provided bit mask to check if HBA completed
2988 * the restart. This function will wait in a loop for the
2989 * HBA to complete restart. If the HBA does not restart within
2990 * 15 iterations, the function will reset the HBA again. The
2991 * function returns 1 when HBA fail to restart otherwise returns
2992 * zero.
2993 **/
James Smart3772a992009-05-22 14:50:54 -04002994static int
2995lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05002996{
Jamie Wellnitz41415862006-02-28 19:25:27 -05002997 uint32_t status;
2998 int i = 0;
2999 int retval = 0;
dea31012005-04-17 16:05:31 -05003000
Jamie Wellnitz41415862006-02-28 19:25:27 -05003001 /* Read the HBA Host Status Register */
3002 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003003
Jamie Wellnitz41415862006-02-28 19:25:27 -05003004 /*
3005 * Check status register every 100ms for 5 retries, then every
3006 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3007 * every 2.5 sec for 4.
3008 * Break our of the loop if errors occurred during init.
3009 */
3010 while (((status & mask) != mask) &&
3011 !(status & HS_FFERM) &&
3012 i++ < 20) {
dea31012005-04-17 16:05:31 -05003013
Jamie Wellnitz41415862006-02-28 19:25:27 -05003014 if (i <= 5)
3015 msleep(10);
3016 else if (i <= 10)
3017 msleep(500);
3018 else
3019 msleep(2500);
dea31012005-04-17 16:05:31 -05003020
Jamie Wellnitz41415862006-02-28 19:25:27 -05003021 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003022 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003023 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003024 lpfc_sli_brdrestart(phba);
3025 }
3026 /* Read the HBA Host Status Register */
3027 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003028 }
dea31012005-04-17 16:05:31 -05003029
Jamie Wellnitz41415862006-02-28 19:25:27 -05003030 /* Check to see if any errors occurred during init */
3031 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003032 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003033 retval = 1;
3034 }
dea31012005-04-17 16:05:31 -05003035
Jamie Wellnitz41415862006-02-28 19:25:27 -05003036 return retval;
dea31012005-04-17 16:05:31 -05003037}
3038
James Smartda0436e2009-05-22 14:51:39 -04003039/**
3040 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3041 * @phba: Pointer to HBA context object.
3042 * @mask: Bit mask to be checked.
3043 *
3044 * This function checks the host status register to check if HBA is
3045 * ready. This function will wait in a loop for the HBA to be ready
3046 * If the HBA is not ready , the function will will reset the HBA PCI
3047 * function again. The function returns 1 when HBA fail to be ready
3048 * otherwise returns zero.
3049 **/
3050static int
3051lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3052{
3053 uint32_t status;
3054 int retval = 0;
3055
3056 /* Read the HBA Host Status Register */
3057 status = lpfc_sli4_post_status_check(phba);
3058
3059 if (status) {
3060 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3061 lpfc_sli_brdrestart(phba);
3062 status = lpfc_sli4_post_status_check(phba);
3063 }
3064
3065 /* Check to see if any errors occurred during init */
3066 if (status) {
3067 phba->link_state = LPFC_HBA_ERROR;
3068 retval = 1;
3069 } else
3070 phba->sli4_hba.intr_enable = 0;
3071
3072 return retval;
3073}
3074
3075/**
3076 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3077 * @phba: Pointer to HBA context object.
3078 * @mask: Bit mask to be checked.
3079 *
3080 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3081 * from the API jump table function pointer from the lpfc_hba struct.
3082 **/
3083int
3084lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3085{
3086 return phba->lpfc_sli_brdready(phba, mask);
3087}
3088
James Smart92908312006-03-07 15:04:13 -05003089#define BARRIER_TEST_PATTERN (0xdeadbeef)
3090
James Smarte59058c2008-08-24 21:49:00 -04003091/**
James Smart3621a712009-04-06 18:47:14 -04003092 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003093 * @phba: Pointer to HBA context object.
3094 *
3095 * This function is called before resetting an HBA. This
3096 * function requests HBA to quiesce DMAs before a reset.
3097 **/
James Smart2e0fef82007-06-17 19:56:36 -05003098void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003099{
James Smart65a29c12006-07-06 15:50:50 -04003100 uint32_t __iomem *resp_buf;
3101 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003102 volatile uint32_t mbox;
3103 uint32_t hc_copy;
3104 int i;
3105 uint8_t hdrtype;
3106
3107 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3108 if (hdrtype != 0x80 ||
3109 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3110 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3111 return;
3112
3113 /*
3114 * Tell the other part of the chip to suspend temporarily all
3115 * its DMA activity.
3116 */
James Smart65a29c12006-07-06 15:50:50 -04003117 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003118
3119 /* Disable the error attention */
3120 hc_copy = readl(phba->HCregaddr);
3121 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3122 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003123 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003124
3125 if (readl(phba->HAregaddr) & HA_ERATT) {
3126 /* Clear Chip error bit */
3127 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003128 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003129 }
3130
3131 mbox = 0;
3132 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3133 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3134
3135 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003136 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003137 writel(mbox, mbox_buf);
3138
3139 for (i = 0;
3140 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3141 mdelay(1);
3142
3143 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003144 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003145 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003146 goto restore_hc;
3147 else
3148 goto clear_errat;
3149 }
3150
3151 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3152 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3153 mdelay(1);
3154
3155clear_errat:
3156
3157 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3158 mdelay(1);
3159
3160 if (readl(phba->HAregaddr) & HA_ERATT) {
3161 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003162 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003163 }
3164
3165restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003166 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003167 writel(hc_copy, phba->HCregaddr);
3168 readl(phba->HCregaddr); /* flush */
3169}
3170
James Smarte59058c2008-08-24 21:49:00 -04003171/**
James Smart3621a712009-04-06 18:47:14 -04003172 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003173 * @phba: Pointer to HBA context object.
3174 *
3175 * This function issues a kill_board mailbox command and waits for
3176 * the error attention interrupt. This function is called for stopping
3177 * the firmware processing. The caller is not required to hold any
3178 * locks. This function calls lpfc_hba_down_post function to free
3179 * any pending commands after the kill. The function will return 1 when it
3180 * fails to kill the board else will return 0.
3181 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003182int
James Smart2e0fef82007-06-17 19:56:36 -05003183lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003184{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003185 struct lpfc_sli *psli;
3186 LPFC_MBOXQ_t *pmb;
3187 uint32_t status;
3188 uint32_t ha_copy;
3189 int retval;
3190 int i = 0;
3191
3192 psli = &phba->sli;
3193
3194 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003195 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003196 "0329 Kill HBA Data: x%x x%x\n",
3197 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003198
James Smart98c9ea52007-10-27 13:37:33 -04003199 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3200 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003201 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003202
3203 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003204 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003205 status = readl(phba->HCregaddr);
3206 status &= ~HC_ERINT_ENA;
3207 writel(status, phba->HCregaddr);
3208 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003209 phba->link_flag |= LS_IGNORE_ERATT;
3210 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003211
3212 lpfc_kill_board(phba, pmb);
3213 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3214 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3215
3216 if (retval != MBX_SUCCESS) {
3217 if (retval != MBX_BUSY)
3218 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003219 spin_lock_irq(&phba->hbalock);
3220 phba->link_flag &= ~LS_IGNORE_ERATT;
3221 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003222 return 1;
3223 }
3224
James Smartf4b4c682009-05-22 14:53:12 -04003225 spin_lock_irq(&phba->hbalock);
3226 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3227 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003228
Jamie Wellnitz41415862006-02-28 19:25:27 -05003229 mempool_free(pmb, phba->mbox_mem_pool);
3230
3231 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3232 * attention every 100ms for 3 seconds. If we don't get ERATT after
3233 * 3 seconds we still set HBA_ERROR state because the status of the
3234 * board is now undefined.
3235 */
3236 ha_copy = readl(phba->HAregaddr);
3237
3238 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3239 mdelay(100);
3240 ha_copy = readl(phba->HAregaddr);
3241 }
3242
3243 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003244 if (ha_copy & HA_ERATT) {
3245 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003246 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003247 }
James Smart2e0fef82007-06-17 19:56:36 -05003248 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003249 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003250 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003251 phba->link_flag &= ~LS_IGNORE_ERATT;
3252 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003253
Jamie Wellnitz41415862006-02-28 19:25:27 -05003254 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003255 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003256
James Smart2e0fef82007-06-17 19:56:36 -05003257 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003258}
3259
James Smarte59058c2008-08-24 21:49:00 -04003260/**
James Smart3772a992009-05-22 14:50:54 -04003261 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003262 * @phba: Pointer to HBA context object.
3263 *
3264 * This function resets the HBA by writing HC_INITFF to the control
3265 * register. After the HBA resets, this function resets all the iocb ring
3266 * indices. This function disables PCI layer parity checking during
3267 * the reset.
3268 * This function returns 0 always.
3269 * The caller is not required to hold any locks.
3270 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003271int
James Smart2e0fef82007-06-17 19:56:36 -05003272lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003273{
3274 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003275 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003276 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003277 int i;
dea31012005-04-17 16:05:31 -05003278
Jamie Wellnitz41415862006-02-28 19:25:27 -05003279 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003280
Jamie Wellnitz41415862006-02-28 19:25:27 -05003281 /* Reset HBA */
3282 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003283 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003284 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003285
3286 /* perform board reset */
3287 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003288 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003289 phba->pport->fc_myDID = 0;
3290 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003291
Jamie Wellnitz41415862006-02-28 19:25:27 -05003292 /* Turn off parity checking and serr during the physical reset */
3293 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3294 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3295 (cfg_value &
3296 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3297
James Smart3772a992009-05-22 14:50:54 -04003298 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3299
Jamie Wellnitz41415862006-02-28 19:25:27 -05003300 /* Now toggle INITFF bit in the Host Control Register */
3301 writel(HC_INITFF, phba->HCregaddr);
3302 mdelay(1);
3303 readl(phba->HCregaddr); /* flush */
3304 writel(0, phba->HCregaddr);
3305 readl(phba->HCregaddr); /* flush */
3306
3307 /* Restore PCI cmd register */
3308 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003309
3310 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003311 for (i = 0; i < psli->num_rings; i++) {
3312 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003313 pring->flag = 0;
3314 pring->rspidx = 0;
3315 pring->next_cmdidx = 0;
3316 pring->local_getidx = 0;
3317 pring->cmdidx = 0;
3318 pring->missbufcnt = 0;
3319 }
dea31012005-04-17 16:05:31 -05003320
James Smart2e0fef82007-06-17 19:56:36 -05003321 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003322 return 0;
3323}
3324
James Smarte59058c2008-08-24 21:49:00 -04003325/**
James Smartda0436e2009-05-22 14:51:39 -04003326 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3327 * @phba: Pointer to HBA context object.
3328 *
3329 * This function resets a SLI4 HBA. This function disables PCI layer parity
3330 * checking during resets the device. The caller is not required to hold
3331 * any locks.
3332 *
3333 * This function returns 0 always.
3334 **/
3335int
3336lpfc_sli4_brdreset(struct lpfc_hba *phba)
3337{
3338 struct lpfc_sli *psli = &phba->sli;
3339 uint16_t cfg_value;
3340 uint8_t qindx;
3341
3342 /* Reset HBA */
3343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3344 "0295 Reset HBA Data: x%x x%x\n",
3345 phba->pport->port_state, psli->sli_flag);
3346
3347 /* perform board reset */
3348 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003349 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003350 phba->pport->fc_myDID = 0;
3351 phba->pport->fc_prevDID = 0;
3352
3353 /* Turn off parity checking and serr during the physical reset */
3354 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3355 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3356 (cfg_value &
3357 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3358
3359 spin_lock_irq(&phba->hbalock);
3360 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3361 phba->fcf.fcf_flag = 0;
3362 /* Clean up the child queue list for the CQs */
3363 list_del_init(&phba->sli4_hba.mbx_wq->list);
3364 list_del_init(&phba->sli4_hba.els_wq->list);
3365 list_del_init(&phba->sli4_hba.hdr_rq->list);
3366 list_del_init(&phba->sli4_hba.dat_rq->list);
3367 list_del_init(&phba->sli4_hba.mbx_cq->list);
3368 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003369 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3370 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3371 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3372 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3373 spin_unlock_irq(&phba->hbalock);
3374
3375 /* Now physically reset the device */
3376 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3377 "0389 Performing PCI function reset!\n");
3378 /* Perform FCoE PCI function reset */
3379 lpfc_pci_function_reset(phba);
3380
3381 return 0;
3382}
3383
3384/**
3385 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003386 * @phba: Pointer to HBA context object.
3387 *
3388 * This function is called in the SLI initialization code path to
3389 * restart the HBA. The caller is not required to hold any lock.
3390 * This function writes MBX_RESTART mailbox command to the SLIM and
3391 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3392 * function to free any pending commands. The function enables
3393 * POST only during the first initialization. The function returns zero.
3394 * The function does not guarantee completion of MBX_RESTART mailbox
3395 * command before the return of this function.
3396 **/
James Smartda0436e2009-05-22 14:51:39 -04003397static int
3398lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003399{
3400 MAILBOX_t *mb;
3401 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003402 volatile uint32_t word0;
3403 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003404 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003405
James Smart2e0fef82007-06-17 19:56:36 -05003406 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003407
James Smart0d878412009-10-02 15:16:56 -04003408 /* Take PCIe device Advanced Error Reporting (AER) state */
3409 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3410
Jamie Wellnitz41415862006-02-28 19:25:27 -05003411 psli = &phba->sli;
3412
3413 /* Restart HBA */
3414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003415 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003416 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003417
3418 word0 = 0;
3419 mb = (MAILBOX_t *) &word0;
3420 mb->mbxCommand = MBX_RESTART;
3421 mb->mbxHc = 1;
3422
James Smart92908312006-03-07 15:04:13 -05003423 lpfc_reset_barrier(phba);
3424
Jamie Wellnitz41415862006-02-28 19:25:27 -05003425 to_slim = phba->MBslimaddr;
3426 writel(*(uint32_t *) mb, to_slim);
3427 readl(to_slim); /* flush */
3428
3429 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003430 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003431 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003432 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003433 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003434 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003435 writel(*(uint32_t *) mb, to_slim);
3436 readl(to_slim); /* flush */
3437
3438 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003439 phba->pport->stopped = 0;
3440 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003441 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003442 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003443
James Smart64ba8812006-08-02 15:24:34 -04003444 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3445 psli->stats_start = get_seconds();
3446
James Smarteaf15d52008-12-04 22:39:29 -05003447 /* Give the INITFF and Post time to settle. */
3448 mdelay(100);
dea31012005-04-17 16:05:31 -05003449
James Smart0d878412009-10-02 15:16:56 -04003450 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3451 if (hba_aer_enabled)
3452 pci_disable_pcie_error_reporting(phba->pcidev);
3453
Jamie Wellnitz41415862006-02-28 19:25:27 -05003454 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003455
3456 return 0;
3457}
3458
James Smarte59058c2008-08-24 21:49:00 -04003459/**
James Smartda0436e2009-05-22 14:51:39 -04003460 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3461 * @phba: Pointer to HBA context object.
3462 *
3463 * This function is called in the SLI initialization code path to restart
3464 * a SLI4 HBA. The caller is not required to hold any lock.
3465 * At the end of the function, it calls lpfc_hba_down_post function to
3466 * free any pending commands.
3467 **/
3468static int
3469lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3470{
3471 struct lpfc_sli *psli = &phba->sli;
3472
3473
3474 /* Restart HBA */
3475 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3476 "0296 Restart HBA Data: x%x x%x\n",
3477 phba->pport->port_state, psli->sli_flag);
3478
3479 lpfc_sli4_brdreset(phba);
3480
3481 spin_lock_irq(&phba->hbalock);
3482 phba->pport->stopped = 0;
3483 phba->link_state = LPFC_INIT_START;
3484 phba->hba_flag = 0;
3485 spin_unlock_irq(&phba->hbalock);
3486
3487 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3488 psli->stats_start = get_seconds();
3489
3490 lpfc_hba_down_post(phba);
3491
3492 return 0;
3493}
3494
3495/**
3496 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3497 * @phba: Pointer to HBA context object.
3498 *
3499 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3500 * API jump table function pointer from the lpfc_hba struct.
3501**/
3502int
3503lpfc_sli_brdrestart(struct lpfc_hba *phba)
3504{
3505 return phba->lpfc_sli_brdrestart(phba);
3506}
3507
3508/**
James Smart3621a712009-04-06 18:47:14 -04003509 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003510 * @phba: Pointer to HBA context object.
3511 *
3512 * This function is called after a HBA restart to wait for successful
3513 * restart of the HBA. Successful restart of the HBA is indicated by
3514 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3515 * iteration, the function will restart the HBA again. The function returns
3516 * zero if HBA successfully restarted else returns negative error code.
3517 **/
dea31012005-04-17 16:05:31 -05003518static int
3519lpfc_sli_chipset_init(struct lpfc_hba *phba)
3520{
3521 uint32_t status, i = 0;
3522
3523 /* Read the HBA Host Status Register */
3524 status = readl(phba->HSregaddr);
3525
3526 /* Check status register to see what current state is */
3527 i = 0;
3528 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3529
3530 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3531 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3532 * 4.
3533 */
3534 if (i++ >= 20) {
3535 /* Adapter failed to init, timeout, status reg
3536 <status> */
James Smarted957682007-06-17 19:56:37 -05003537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003538 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003539 "timeout, status reg x%x, "
3540 "FW Data: A8 x%x AC x%x\n", status,
3541 readl(phba->MBslimaddr + 0xa8),
3542 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003543 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003544 return -ETIMEDOUT;
3545 }
3546
3547 /* Check to see if any errors occurred during init */
3548 if (status & HS_FFERM) {
3549 /* ERROR: During chipset initialization */
3550 /* Adapter failed to init, chipset, status reg
3551 <status> */
James Smarted957682007-06-17 19:56:37 -05003552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003553 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003554 "chipset, status reg x%x, "
3555 "FW Data: A8 x%x AC x%x\n", status,
3556 readl(phba->MBslimaddr + 0xa8),
3557 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003558 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003559 return -EIO;
3560 }
3561
3562 if (i <= 5) {
3563 msleep(10);
3564 } else if (i <= 10) {
3565 msleep(500);
3566 } else {
3567 msleep(2500);
3568 }
3569
3570 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003571 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003572 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003573 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003574 }
3575 /* Read the HBA Host Status Register */
3576 status = readl(phba->HSregaddr);
3577 }
3578
3579 /* Check to see if any errors occurred during init */
3580 if (status & HS_FFERM) {
3581 /* ERROR: During chipset initialization */
3582 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003584 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003585 "status reg x%x, "
3586 "FW Data: A8 x%x AC x%x\n", status,
3587 readl(phba->MBslimaddr + 0xa8),
3588 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003589 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003590 return -EIO;
3591 }
3592
3593 /* Clear all interrupt enable conditions */
3594 writel(0, phba->HCregaddr);
3595 readl(phba->HCregaddr); /* flush */
3596
3597 /* setup host attn register */
3598 writel(0xffffffff, phba->HAregaddr);
3599 readl(phba->HAregaddr); /* flush */
3600 return 0;
3601}
3602
James Smarte59058c2008-08-24 21:49:00 -04003603/**
James Smart3621a712009-04-06 18:47:14 -04003604 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003605 *
3606 * This function calculates and returns the number of HBQs required to be
3607 * configured.
3608 **/
James Smart78b2d852007-08-02 11:10:21 -04003609int
James Smarted957682007-06-17 19:56:37 -05003610lpfc_sli_hbq_count(void)
3611{
James Smart92d7f7b2007-06-17 19:56:38 -05003612 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003613}
3614
James Smarte59058c2008-08-24 21:49:00 -04003615/**
James Smart3621a712009-04-06 18:47:14 -04003616 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003617 *
3618 * This function adds the number of hbq entries in every HBQ to get
3619 * the total number of hbq entries required for the HBA and returns
3620 * the total count.
3621 **/
James Smarted957682007-06-17 19:56:37 -05003622static int
3623lpfc_sli_hbq_entry_count(void)
3624{
3625 int hbq_count = lpfc_sli_hbq_count();
3626 int count = 0;
3627 int i;
3628
3629 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003630 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003631 return count;
3632}
3633
James Smarte59058c2008-08-24 21:49:00 -04003634/**
James Smart3621a712009-04-06 18:47:14 -04003635 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003636 *
3637 * This function calculates amount of memory required for all hbq entries
3638 * to be configured and returns the total memory required.
3639 **/
dea31012005-04-17 16:05:31 -05003640int
James Smarted957682007-06-17 19:56:37 -05003641lpfc_sli_hbq_size(void)
3642{
3643 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3644}
3645
James Smarte59058c2008-08-24 21:49:00 -04003646/**
James Smart3621a712009-04-06 18:47:14 -04003647 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003648 * @phba: Pointer to HBA context object.
3649 *
3650 * This function is called during the SLI initialization to configure
3651 * all the HBQs and post buffers to the HBQ. The caller is not
3652 * required to hold any locks. This function will return zero if successful
3653 * else it will return negative error code.
3654 **/
James Smarted957682007-06-17 19:56:37 -05003655static int
3656lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3657{
3658 int hbq_count = lpfc_sli_hbq_count();
3659 LPFC_MBOXQ_t *pmb;
3660 MAILBOX_t *pmbox;
3661 uint32_t hbqno;
3662 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003663
James Smart92d7f7b2007-06-17 19:56:38 -05003664 /* Get a Mailbox buffer to setup mailbox
3665 * commands for HBA initialization
3666 */
James Smarted957682007-06-17 19:56:37 -05003667 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3668
3669 if (!pmb)
3670 return -ENOMEM;
3671
James Smart04c68492009-05-22 14:52:52 -04003672 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003673
3674 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3675 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003676 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003677
3678 hbq_entry_index = 0;
3679 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3680 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3681 phba->hbqs[hbqno].hbqPutIdx = 0;
3682 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3683 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003684 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003685 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3686 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003687 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3688
3689 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3690 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3691 mbxStatus <status>, ring <num> */
3692
3693 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003694 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003695 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003696 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003697 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003698 pmbox->mbxStatus, hbqno);
3699
3700 phba->link_state = LPFC_HBA_ERROR;
3701 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003702 return ENXIO;
3703 }
3704 }
3705 phba->hbq_count = hbq_count;
3706
James Smarted957682007-06-17 19:56:37 -05003707 mempool_free(pmb, phba->mbox_mem_pool);
3708
James Smart92d7f7b2007-06-17 19:56:38 -05003709 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003710 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3711 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003712 return 0;
3713}
3714
James Smarte59058c2008-08-24 21:49:00 -04003715/**
James Smart4f774512009-05-22 14:52:35 -04003716 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3717 * @phba: Pointer to HBA context object.
3718 *
3719 * This function is called during the SLI initialization to configure
3720 * all the HBQs and post buffers to the HBQ. The caller is not
3721 * required to hold any locks. This function will return zero if successful
3722 * else it will return negative error code.
3723 **/
3724static int
3725lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3726{
3727 phba->hbq_in_use = 1;
3728 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3729 phba->hbq_count = 1;
3730 /* Initially populate or replenish the HBQs */
3731 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3732 return 0;
3733}
3734
3735/**
James Smart3621a712009-04-06 18:47:14 -04003736 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003737 * @phba: Pointer to HBA context object.
3738 * @sli_mode: sli mode - 2/3
3739 *
3740 * This function is called by the sli intialization code path
3741 * to issue config_port mailbox command. This function restarts the
3742 * HBA firmware and issues a config_port mailbox command to configure
3743 * the SLI interface in the sli mode specified by sli_mode
3744 * variable. The caller is not required to hold any locks.
3745 * The function returns 0 if successful, else returns negative error
3746 * code.
3747 **/
James Smart93996272008-08-24 21:50:30 -04003748int
3749lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003750{
3751 LPFC_MBOXQ_t *pmb;
3752 uint32_t resetcount = 0, rc = 0, done = 0;
3753
3754 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3755 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003756 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003757 return -ENOMEM;
3758 }
3759
James Smarted957682007-06-17 19:56:37 -05003760 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003761 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003762 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003763 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003764 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003765 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003766 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003767 rc = lpfc_sli_chipset_init(phba);
3768 if (rc)
3769 break;
3770
James Smart2e0fef82007-06-17 19:56:36 -05003771 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003772 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003773 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003774 resetcount++;
3775
James Smarted957682007-06-17 19:56:37 -05003776 /* Call pre CONFIG_PORT mailbox command initialization. A
3777 * value of 0 means the call was successful. Any other
3778 * nonzero value is a failure, but if ERESTART is returned,
3779 * the driver may reset the HBA and try again.
3780 */
dea31012005-04-17 16:05:31 -05003781 rc = lpfc_config_port_prep(phba);
3782 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003783 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003784 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003785 } else if (rc)
dea31012005-04-17 16:05:31 -05003786 break;
James Smart2e0fef82007-06-17 19:56:36 -05003787 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003788 lpfc_config_port(phba, pmb);
3789 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003790 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3791 LPFC_SLI3_HBQ_ENABLED |
3792 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003793 LPFC_SLI3_INB_ENABLED |
3794 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003795 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003797 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003798 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003799 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003800 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003801 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003802 spin_unlock_irq(&phba->hbalock);
3803 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003804 } else {
3805 /* Allow asynchronous mailbox command to go through */
3806 spin_lock_irq(&phba->hbalock);
3807 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3808 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003809 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003810 }
dea31012005-04-17 16:05:31 -05003811 }
James Smarted957682007-06-17 19:56:37 -05003812 if (!done) {
3813 rc = -EINVAL;
3814 goto do_prep_failed;
3815 }
James Smart04c68492009-05-22 14:52:52 -04003816 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3817 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003818 rc = -ENXIO;
3819 goto do_prep_failed;
3820 }
James Smart04c68492009-05-22 14:52:52 -04003821 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003822 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003823 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3824 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3825 phba->max_vpi : phba->max_vports;
3826
James Smart34b02dc2008-08-24 21:49:55 -04003827 } else
3828 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003829 if (pmb->u.mb.un.varCfgPort.gdss)
3830 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3831 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003832 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003833 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003834 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003835 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003836 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003837 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003838 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3839 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3840 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3841 phba->inb_last_counter =
3842 phba->mbox->us.s3_inb_pgp.counter;
3843 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003844 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003845 phba->port_gp = phba->mbox->us.s3_pgp.port;
3846 phba->inb_ha_copy = NULL;
3847 phba->inb_counter = NULL;
3848 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003849
3850 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003851 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003852 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3853 else
3854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3855 "0443 Adapter did not grant "
3856 "BlockGuard\n");
3857 }
James Smart34b02dc2008-08-24 21:49:55 -04003858 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003859 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003860 phba->port_gp = phba->mbox->us.s2.port;
3861 phba->inb_ha_copy = NULL;
3862 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003863 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003864 }
James Smart92d7f7b2007-06-17 19:56:38 -05003865do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003866 mempool_free(pmb, phba->mbox_mem_pool);
3867 return rc;
3868}
3869
James Smarte59058c2008-08-24 21:49:00 -04003870
3871/**
James Smart3621a712009-04-06 18:47:14 -04003872 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003873 * @phba: Pointer to HBA context object.
3874 *
3875 * This function is the main SLI intialization function. This function
3876 * is called by the HBA intialization code, HBA reset code and HBA
3877 * error attention handler code. Caller is not required to hold any
3878 * locks. This function issues config_port mailbox command to configure
3879 * the SLI, setup iocb rings and HBQ rings. In the end the function
3880 * calls the config_port_post function to issue init_link mailbox
3881 * command and to start the discovery. The function will return zero
3882 * if successful, else it will return negative error code.
3883 **/
James Smarted957682007-06-17 19:56:37 -05003884int
3885lpfc_sli_hba_setup(struct lpfc_hba *phba)
3886{
3887 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003888 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003889
3890 switch (lpfc_sli_mode) {
3891 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003892 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003894 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003895 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003896 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003897 break;
3898 }
James Smarted957682007-06-17 19:56:37 -05003899 mode = 2;
3900 break;
3901 case 0:
3902 case 3:
3903 break;
3904 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003906 "1819 Unrecognized lpfc_sli_mode "
3907 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003908
3909 break;
3910 }
3911
James Smart93996272008-08-24 21:50:30 -04003912 rc = lpfc_sli_config_port(phba, mode);
3913
James Smarted957682007-06-17 19:56:37 -05003914 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003916 "1820 Unable to select SLI-3. "
3917 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003918 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003919 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003920 if (rc)
dea31012005-04-17 16:05:31 -05003921 goto lpfc_sli_hba_setup_error;
3922
James Smart0d878412009-10-02 15:16:56 -04003923 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
3924 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
3925 rc = pci_enable_pcie_error_reporting(phba->pcidev);
3926 if (!rc) {
3927 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3928 "2709 This device supports "
3929 "Advanced Error Reporting (AER)\n");
3930 spin_lock_irq(&phba->hbalock);
3931 phba->hba_flag |= HBA_AER_ENABLED;
3932 spin_unlock_irq(&phba->hbalock);
3933 } else {
3934 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3935 "2708 This device does not support "
3936 "Advanced Error Reporting (AER)\n");
3937 phba->cfg_aer_support = 0;
3938 }
3939 }
3940
James Smarted957682007-06-17 19:56:37 -05003941 if (phba->sli_rev == 3) {
3942 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3943 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05003944 } else {
3945 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3946 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05003947 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05003948 }
3949
3950 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003951 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3952 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05003953 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05003954
3955 if (rc)
3956 goto lpfc_sli_hba_setup_error;
3957
James Smart93996272008-08-24 21:50:30 -04003958 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05003959 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3960 rc = lpfc_sli_hbq_setup(phba);
3961 if (rc)
3962 goto lpfc_sli_hba_setup_error;
3963 }
James Smart04c68492009-05-22 14:52:52 -04003964 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003965 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04003966 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003967
3968 rc = lpfc_config_port_post(phba);
3969 if (rc)
3970 goto lpfc_sli_hba_setup_error;
3971
James Smarted957682007-06-17 19:56:37 -05003972 return rc;
3973
James Smart92d7f7b2007-06-17 19:56:38 -05003974lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05003975 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05003976 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003977 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05003978 return rc;
3979}
3980
James Smartda0436e2009-05-22 14:51:39 -04003981/**
3982 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
3983 * @phba: Pointer to HBA context object.
3984 * @mboxq: mailbox pointer.
3985 * This function issue a dump mailbox command to read config region
3986 * 23 and parse the records in the region and populate driver
3987 * data structure.
3988 **/
3989static int
3990lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
3991 LPFC_MBOXQ_t *mboxq)
3992{
3993 struct lpfc_dmabuf *mp;
3994 struct lpfc_mqe *mqe;
3995 uint32_t data_length;
3996 int rc;
3997
3998 /* Program the default value of vlan_id and fc_map */
3999 phba->valid_vlan = 0;
4000 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4001 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4002 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4003
4004 mqe = &mboxq->u.mqe;
4005 if (lpfc_dump_fcoe_param(phba, mboxq))
4006 return -ENOMEM;
4007
4008 mp = (struct lpfc_dmabuf *) mboxq->context1;
4009 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4010
4011 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4012 "(%d):2571 Mailbox cmd x%x Status x%x "
4013 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4014 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4015 "CQ: x%x x%x x%x x%x\n",
4016 mboxq->vport ? mboxq->vport->vpi : 0,
4017 bf_get(lpfc_mqe_command, mqe),
4018 bf_get(lpfc_mqe_status, mqe),
4019 mqe->un.mb_words[0], mqe->un.mb_words[1],
4020 mqe->un.mb_words[2], mqe->un.mb_words[3],
4021 mqe->un.mb_words[4], mqe->un.mb_words[5],
4022 mqe->un.mb_words[6], mqe->un.mb_words[7],
4023 mqe->un.mb_words[8], mqe->un.mb_words[9],
4024 mqe->un.mb_words[10], mqe->un.mb_words[11],
4025 mqe->un.mb_words[12], mqe->un.mb_words[13],
4026 mqe->un.mb_words[14], mqe->un.mb_words[15],
4027 mqe->un.mb_words[16], mqe->un.mb_words[50],
4028 mboxq->mcqe.word0,
4029 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4030 mboxq->mcqe.trailer);
4031
4032 if (rc) {
4033 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4034 kfree(mp);
4035 return -EIO;
4036 }
4037 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004038 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004039 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4040 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004041 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004042 }
James Smartda0436e2009-05-22 14:51:39 -04004043
4044 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4045 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4046 kfree(mp);
4047 return 0;
4048}
4049
4050/**
4051 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4052 * @phba: pointer to lpfc hba data structure.
4053 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4054 * @vpd: pointer to the memory to hold resulting port vpd data.
4055 * @vpd_size: On input, the number of bytes allocated to @vpd.
4056 * On output, the number of data bytes in @vpd.
4057 *
4058 * This routine executes a READ_REV SLI4 mailbox command. In
4059 * addition, this routine gets the port vpd data.
4060 *
4061 * Return codes
4062 * 0 - sucessful
4063 * ENOMEM - could not allocated memory.
4064 **/
4065static int
4066lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4067 uint8_t *vpd, uint32_t *vpd_size)
4068{
4069 int rc = 0;
4070 uint32_t dma_size;
4071 struct lpfc_dmabuf *dmabuf;
4072 struct lpfc_mqe *mqe;
4073
4074 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4075 if (!dmabuf)
4076 return -ENOMEM;
4077
4078 /*
4079 * Get a DMA buffer for the vpd data resulting from the READ_REV
4080 * mailbox command.
4081 */
4082 dma_size = *vpd_size;
4083 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4084 dma_size,
4085 &dmabuf->phys,
4086 GFP_KERNEL);
4087 if (!dmabuf->virt) {
4088 kfree(dmabuf);
4089 return -ENOMEM;
4090 }
4091 memset(dmabuf->virt, 0, dma_size);
4092
4093 /*
4094 * The SLI4 implementation of READ_REV conflicts at word1,
4095 * bits 31:16 and SLI4 adds vpd functionality not present
4096 * in SLI3. This code corrects the conflicts.
4097 */
4098 lpfc_read_rev(phba, mboxq);
4099 mqe = &mboxq->u.mqe;
4100 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4101 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4102 mqe->un.read_rev.word1 &= 0x0000FFFF;
4103 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4104 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4105
4106 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4107 if (rc) {
4108 dma_free_coherent(&phba->pcidev->dev, dma_size,
4109 dmabuf->virt, dmabuf->phys);
4110 return -EIO;
4111 }
4112
James Smartda0436e2009-05-22 14:51:39 -04004113 /*
4114 * The available vpd length cannot be bigger than the
4115 * DMA buffer passed to the port. Catch the less than
4116 * case and update the caller's size.
4117 */
4118 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4119 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4120
4121 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4122 dma_free_coherent(&phba->pcidev->dev, dma_size,
4123 dmabuf->virt, dmabuf->phys);
4124 kfree(dmabuf);
4125 return 0;
4126}
4127
4128/**
4129 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4130 * @phba: pointer to lpfc hba data structure.
4131 *
4132 * This routine is called to explicitly arm the SLI4 device's completion and
4133 * event queues
4134 **/
4135static void
4136lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4137{
4138 uint8_t fcp_eqidx;
4139
4140 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4141 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004142 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4143 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4144 LPFC_QUEUE_REARM);
4145 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4146 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4147 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4148 LPFC_QUEUE_REARM);
4149}
4150
4151/**
4152 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4153 * @phba: Pointer to HBA context object.
4154 *
4155 * This function is the main SLI4 device intialization PCI function. This
4156 * function is called by the HBA intialization code, HBA reset code and
4157 * HBA error attention handler code. Caller is not required to hold any
4158 * locks.
4159 **/
4160int
4161lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4162{
4163 int rc;
4164 LPFC_MBOXQ_t *mboxq;
4165 struct lpfc_mqe *mqe;
4166 uint8_t *vpd;
4167 uint32_t vpd_size;
4168 uint32_t ftr_rsp = 0;
4169 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4170 struct lpfc_vport *vport = phba->pport;
4171 struct lpfc_dmabuf *mp;
4172
4173 /* Perform a PCI function reset to start from clean */
4174 rc = lpfc_pci_function_reset(phba);
4175 if (unlikely(rc))
4176 return -ENODEV;
4177
4178 /* Check the HBA Host Status Register for readyness */
4179 rc = lpfc_sli4_post_status_check(phba);
4180 if (unlikely(rc))
4181 return -ENODEV;
4182 else {
4183 spin_lock_irq(&phba->hbalock);
4184 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4185 spin_unlock_irq(&phba->hbalock);
4186 }
4187
4188 /*
4189 * Allocate a single mailbox container for initializing the
4190 * port.
4191 */
4192 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4193 if (!mboxq)
4194 return -ENOMEM;
4195
4196 /*
4197 * Continue initialization with default values even if driver failed
4198 * to read FCoE param config regions
4199 */
4200 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004202 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004203
4204 /* Issue READ_REV to collect vpd and FW information. */
4205 vpd_size = PAGE_SIZE;
4206 vpd = kzalloc(vpd_size, GFP_KERNEL);
4207 if (!vpd) {
4208 rc = -ENOMEM;
4209 goto out_free_mbox;
4210 }
4211
4212 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4213 if (unlikely(rc))
4214 goto out_free_vpd;
4215
4216 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004217 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4218 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4219 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004220
4221 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4222 LPFC_DCBX_CEE_MODE)
4223 phba->hba_flag |= HBA_FIP_SUPPORT;
4224 else
4225 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4226
James Smartf1126682009-06-10 17:22:44 -04004227 if (phba->sli_rev != LPFC_SLI_REV4 ||
4228 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004229 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4230 "0376 READ_REV Error. SLI Level %d "
4231 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004232 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004233 rc = -EIO;
4234 goto out_free_vpd;
4235 }
James Smartda0436e2009-05-22 14:51:39 -04004236 /*
4237 * Evaluate the read rev and vpd data. Populate the driver
4238 * state with the results. If this routine fails, the failure
4239 * is not fatal as the driver will use generic values.
4240 */
4241 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4242 if (unlikely(!rc)) {
4243 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4244 "0377 Error %d parsing vpd. "
4245 "Using defaults.\n", rc);
4246 rc = 0;
4247 }
4248
James Smartf1126682009-06-10 17:22:44 -04004249 /* Save information as VPD data */
4250 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4251 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4252 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4253 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4254 &mqe->un.read_rev);
4255 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4256 &mqe->un.read_rev);
4257 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4258 &mqe->un.read_rev);
4259 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4260 &mqe->un.read_rev);
4261 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4262 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4263 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4264 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4265 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4266 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4267 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4268 "(%d):0380 READ_REV Status x%x "
4269 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4270 mboxq->vport ? mboxq->vport->vpi : 0,
4271 bf_get(lpfc_mqe_status, mqe),
4272 phba->vpd.rev.opFwName,
4273 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4274 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004275
4276 /*
4277 * Discover the port's supported feature set and match it against the
4278 * hosts requests.
4279 */
4280 lpfc_request_features(phba, mboxq);
4281 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4282 if (unlikely(rc)) {
4283 rc = -EIO;
4284 goto out_free_vpd;
4285 }
4286
4287 /*
4288 * The port must support FCP initiator mode as this is the
4289 * only mode running in the host.
4290 */
4291 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4292 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4293 "0378 No support for fcpi mode.\n");
4294 ftr_rsp++;
4295 }
4296
4297 /*
4298 * If the port cannot support the host's requested features
4299 * then turn off the global config parameters to disable the
4300 * feature in the driver. This is not a fatal error.
4301 */
4302 if ((phba->cfg_enable_bg) &&
4303 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4304 ftr_rsp++;
4305
4306 if (phba->max_vpi && phba->cfg_enable_npiv &&
4307 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4308 ftr_rsp++;
4309
4310 if (ftr_rsp) {
4311 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4312 "0379 Feature Mismatch Data: x%08x %08x "
4313 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4314 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4315 phba->cfg_enable_npiv, phba->max_vpi);
4316 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4317 phba->cfg_enable_bg = 0;
4318 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4319 phba->cfg_enable_npiv = 0;
4320 }
4321
4322 /* These SLI3 features are assumed in SLI4 */
4323 spin_lock_irq(&phba->hbalock);
4324 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4325 spin_unlock_irq(&phba->hbalock);
4326
4327 /* Read the port's service parameters. */
4328 lpfc_read_sparam(phba, mboxq, vport->vpi);
4329 mboxq->vport = vport;
4330 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4331 mp = (struct lpfc_dmabuf *) mboxq->context1;
4332 if (rc == MBX_SUCCESS) {
4333 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4334 rc = 0;
4335 }
4336
4337 /*
4338 * This memory was allocated by the lpfc_read_sparam routine. Release
4339 * it to the mbuf pool.
4340 */
4341 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4342 kfree(mp);
4343 mboxq->context1 = NULL;
4344 if (unlikely(rc)) {
4345 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4346 "0382 READ_SPARAM command failed "
4347 "status %d, mbxStatus x%x\n",
4348 rc, bf_get(lpfc_mqe_status, mqe));
4349 phba->link_state = LPFC_HBA_ERROR;
4350 rc = -EIO;
4351 goto out_free_vpd;
4352 }
4353
4354 if (phba->cfg_soft_wwnn)
4355 u64_to_wwn(phba->cfg_soft_wwnn,
4356 vport->fc_sparam.nodeName.u.wwn);
4357 if (phba->cfg_soft_wwpn)
4358 u64_to_wwn(phba->cfg_soft_wwpn,
4359 vport->fc_sparam.portName.u.wwn);
4360 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4361 sizeof(struct lpfc_name));
4362 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4363 sizeof(struct lpfc_name));
4364
4365 /* Update the fc_host data structures with new wwn. */
4366 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4367 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4368
4369 /* Register SGL pool to the device using non-embedded mailbox command */
4370 rc = lpfc_sli4_post_sgl_list(phba);
4371 if (unlikely(rc)) {
4372 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004373 "0582 Error %d during sgl post operation\n",
4374 rc);
James Smartda0436e2009-05-22 14:51:39 -04004375 rc = -ENODEV;
4376 goto out_free_vpd;
4377 }
4378
4379 /* Register SCSI SGL pool to the device */
4380 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4381 if (unlikely(rc)) {
4382 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004383 "0383 Error %d during scsi sgl post "
4384 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004385 /* Some Scsi buffers were moved to the abort scsi list */
4386 /* A pci function reset will repost them */
4387 rc = -ENODEV;
4388 goto out_free_vpd;
4389 }
4390
4391 /* Post the rpi header region to the device. */
4392 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4393 if (unlikely(rc)) {
4394 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4395 "0393 Error %d during rpi post operation\n",
4396 rc);
4397 rc = -ENODEV;
4398 goto out_free_vpd;
4399 }
James Smartda0436e2009-05-22 14:51:39 -04004400
4401 /* Set up all the queues to the device */
4402 rc = lpfc_sli4_queue_setup(phba);
4403 if (unlikely(rc)) {
4404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4405 "0381 Error %d during queue setup.\n ", rc);
4406 goto out_stop_timers;
4407 }
4408
4409 /* Arm the CQs and then EQs on device */
4410 lpfc_sli4_arm_cqeq_intr(phba);
4411
4412 /* Indicate device interrupt mode */
4413 phba->sli4_hba.intr_enable = 1;
4414
4415 /* Allow asynchronous mailbox command to go through */
4416 spin_lock_irq(&phba->hbalock);
4417 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4418 spin_unlock_irq(&phba->hbalock);
4419
4420 /* Post receive buffers to the device */
4421 lpfc_sli4_rb_setup(phba);
4422
4423 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004424 mod_timer(&vport->els_tmofunc,
4425 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004426
4427 /* Start heart beat timer */
4428 mod_timer(&phba->hb_tmofunc,
4429 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4430 phba->hb_outstanding = 0;
4431 phba->last_completion_time = jiffies;
4432
4433 /* Start error attention (ERATT) polling timer */
4434 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4435
4436 /*
4437 * The port is ready, set the host's link state to LINK_DOWN
4438 * in preparation for link interrupts.
4439 */
4440 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4441 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4442 lpfc_set_loopback_flag(phba);
4443 /* Change driver state to LPFC_LINK_DOWN right before init link */
4444 spin_lock_irq(&phba->hbalock);
4445 phba->link_state = LPFC_LINK_DOWN;
4446 spin_unlock_irq(&phba->hbalock);
4447 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4448 if (unlikely(rc != MBX_NOT_FINISHED)) {
4449 kfree(vpd);
4450 return 0;
4451 } else
4452 rc = -EIO;
4453
4454 /* Unset all the queues set up in this routine when error out */
4455 if (rc)
4456 lpfc_sli4_queue_unset(phba);
4457
4458out_stop_timers:
4459 if (rc)
4460 lpfc_stop_hba_timers(phba);
4461out_free_vpd:
4462 kfree(vpd);
4463out_free_mbox:
4464 mempool_free(mboxq, phba->mbox_mem_pool);
4465 return rc;
4466}
James Smarte59058c2008-08-24 21:49:00 -04004467
4468/**
James Smart3621a712009-04-06 18:47:14 -04004469 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004470 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004471 *
James Smarte59058c2008-08-24 21:49:00 -04004472 * This is the callback function for mailbox timer. The mailbox
4473 * timer is armed when a new mailbox command is issued and the timer
4474 * is deleted when the mailbox complete. The function is called by
4475 * the kernel timer code when a mailbox does not complete within
4476 * expected time. This function wakes up the worker thread to
4477 * process the mailbox timeout and returns. All the processing is
4478 * done by the worker thread function lpfc_mbox_timeout_handler.
4479 **/
dea31012005-04-17 16:05:31 -05004480void
4481lpfc_mbox_timeout(unsigned long ptr)
4482{
James Smart92d7f7b2007-06-17 19:56:38 -05004483 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004484 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004485 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004486
James Smart2e0fef82007-06-17 19:56:36 -05004487 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004488 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004489 if (!tmo_posted)
4490 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4491 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4492
James Smart5e9d9b82008-06-14 22:52:53 -04004493 if (!tmo_posted)
4494 lpfc_worker_wake_up(phba);
4495 return;
dea31012005-04-17 16:05:31 -05004496}
4497
James Smarte59058c2008-08-24 21:49:00 -04004498
4499/**
James Smart3621a712009-04-06 18:47:14 -04004500 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004501 * @phba: Pointer to HBA context object.
4502 *
4503 * This function is called from worker thread when a mailbox command times out.
4504 * The caller is not required to hold any locks. This function will reset the
4505 * HBA and recover all the pending commands.
4506 **/
dea31012005-04-17 16:05:31 -05004507void
4508lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4509{
James Smart2e0fef82007-06-17 19:56:36 -05004510 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004511 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004512 struct lpfc_sli *psli = &phba->sli;
4513 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004514
James Smarta257bf92009-04-06 18:48:10 -04004515 /* Check the pmbox pointer first. There is a race condition
4516 * between the mbox timeout handler getting executed in the
4517 * worklist and the mailbox actually completing. When this
4518 * race condition occurs, the mbox_active will be NULL.
4519 */
4520 spin_lock_irq(&phba->hbalock);
4521 if (pmbox == NULL) {
4522 lpfc_printf_log(phba, KERN_WARNING,
4523 LOG_MBOX | LOG_SLI,
4524 "0353 Active Mailbox cleared - mailbox timeout "
4525 "exiting\n");
4526 spin_unlock_irq(&phba->hbalock);
4527 return;
4528 }
4529
dea31012005-04-17 16:05:31 -05004530 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004531 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004532 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004533 mb->mbxCommand,
4534 phba->pport->port_state,
4535 phba->sli.sli_flag,
4536 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004537 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004538
James Smart1dcb58e2007-04-25 09:51:30 -04004539 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4540 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4541 * it to fail all oustanding SCSI IO.
4542 */
James Smart2e0fef82007-06-17 19:56:36 -05004543 spin_lock_irq(&phba->pport->work_port_lock);
4544 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4545 spin_unlock_irq(&phba->pport->work_port_lock);
4546 spin_lock_irq(&phba->hbalock);
4547 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004548 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004549 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004550
4551 pring = &psli->ring[psli->fcp_ring];
4552 lpfc_sli_abort_iocb_ring(phba, pring);
4553
4554 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004555 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004556
4557 /* Reset the HBA device */
4558 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004559}
4560
James Smarte59058c2008-08-24 21:49:00 -04004561/**
James Smart3772a992009-05-22 14:50:54 -04004562 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004563 * @phba: Pointer to HBA context object.
4564 * @pmbox: Pointer to mailbox object.
4565 * @flag: Flag indicating how the mailbox need to be processed.
4566 *
4567 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004568 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4569 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004570 * The mailbox command can be submitted in polling mode, in which case
4571 * this function will wait in a polling loop for the completion of the
4572 * mailbox.
4573 * If the mailbox is submitted in no_wait mode (not polling) the
4574 * function will submit the command and returns immediately without waiting
4575 * for the mailbox completion. The no_wait is supported only when HBA
4576 * is in SLI2/SLI3 mode - interrupts are enabled.
4577 * The SLI interface allows only one mailbox pending at a time. If the
4578 * mailbox is issued in polling mode and there is already a mailbox
4579 * pending, then the function will return an error. If the mailbox is issued
4580 * in NO_WAIT mode and there is a mailbox pending already, the function
4581 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4582 * The sli layer owns the mailbox object until the completion of mailbox
4583 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4584 * return codes the caller owns the mailbox command after the return of
4585 * the function.
4586 **/
James Smart3772a992009-05-22 14:50:54 -04004587static int
4588lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4589 uint32_t flag)
dea31012005-04-17 16:05:31 -05004590{
dea31012005-04-17 16:05:31 -05004591 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004592 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004593 uint32_t status, evtctr;
4594 uint32_t ha_copy;
4595 int i;
James Smart09372822008-01-11 01:52:54 -05004596 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004597 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004598 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004599 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004600 int processing_queue = 0;
4601
4602 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4603 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004604 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004605 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004606 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4607 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4608 return MBX_SUCCESS;
4609 }
James Smart58da1ff2008-04-07 10:15:56 -04004610 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004611 pmbox = lpfc_mbox_get(phba);
4612 if (!pmbox) {
4613 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4614 return MBX_SUCCESS;
4615 }
4616 }
dea31012005-04-17 16:05:31 -05004617
James Smarted957682007-06-17 19:56:37 -05004618 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004619 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004620 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004621 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004622 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004623 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004624 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004625 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004626 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004627 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004628 }
4629 }
4630
Linas Vepstas8d63f372007-02-14 14:28:36 -06004631 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004632 if (unlikely(pci_channel_offline(phba->pcidev))) {
4633 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4634 goto out_not_finished;
4635 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004636
James Smarta257bf92009-04-06 18:48:10 -04004637 /* If HBA has a deferred error attention, fail the iocb. */
4638 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4639 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4640 goto out_not_finished;
4641 }
4642
dea31012005-04-17 16:05:31 -05004643 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004644
James Smart3772a992009-05-22 14:50:54 -04004645 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004646 status = MBX_SUCCESS;
4647
James Smart2e0fef82007-06-17 19:56:36 -05004648 if (phba->link_state == LPFC_HBA_ERROR) {
4649 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004650
4651 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4653 "(%d):0311 Mailbox command x%x cannot "
4654 "issue Data: x%x x%x\n",
4655 pmbox->vport ? pmbox->vport->vpi : 0,
4656 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004657 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004658 }
4659
James Smart92908312006-03-07 15:04:13 -05004660 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4661 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004662 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004663 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4664 "(%d):2528 Mailbox command x%x cannot "
4665 "issue Data: x%x x%x\n",
4666 pmbox->vport ? pmbox->vport->vpi : 0,
4667 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004668 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004669 }
4670
dea31012005-04-17 16:05:31 -05004671 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4672 /* Polling for a mbox command when another one is already active
4673 * is not allowed in SLI. Also, the driver must have established
4674 * SLI2 mode to queue and process multiple mbox commands.
4675 */
4676
4677 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004678 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004679
4680 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004681 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4682 "(%d):2529 Mailbox command x%x "
4683 "cannot issue Data: x%x x%x\n",
4684 pmbox->vport ? pmbox->vport->vpi : 0,
4685 pmbox->u.mb.mbxCommand,
4686 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004687 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004688 }
4689
James Smart3772a992009-05-22 14:50:54 -04004690 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004691 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004692 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004693 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4694 "(%d):2530 Mailbox command x%x "
4695 "cannot issue Data: x%x x%x\n",
4696 pmbox->vport ? pmbox->vport->vpi : 0,
4697 pmbox->u.mb.mbxCommand,
4698 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004699 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004700 }
4701
dea31012005-04-17 16:05:31 -05004702 /* Another mailbox command is still being processed, queue this
4703 * command to be processed later.
4704 */
4705 lpfc_mbox_put(phba, pmbox);
4706
4707 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004708 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004709 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004710 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004711 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4712 mb->mbxCommand, phba->pport->port_state,
4713 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004714
4715 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004716 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004717
James Smart858c9f62007-06-17 19:56:39 -05004718 if (pmbox->vport) {
4719 lpfc_debugfs_disc_trc(pmbox->vport,
4720 LPFC_DISC_TRC_MBOX_VPORT,
4721 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4722 (uint32_t)mb->mbxCommand,
4723 mb->un.varWords[0], mb->un.varWords[1]);
4724 }
4725 else {
4726 lpfc_debugfs_disc_trc(phba->pport,
4727 LPFC_DISC_TRC_MBOX,
4728 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4729 (uint32_t)mb->mbxCommand,
4730 mb->un.varWords[0], mb->un.varWords[1]);
4731 }
4732
James Smart2e0fef82007-06-17 19:56:36 -05004733 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004734 }
4735
dea31012005-04-17 16:05:31 -05004736 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4737
4738 /* If we are not polling, we MUST be in SLI2 mode */
4739 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004740 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004741 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004742 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004743 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004744 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004745 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4746 "(%d):2531 Mailbox command x%x "
4747 "cannot issue Data: x%x x%x\n",
4748 pmbox->vport ? pmbox->vport->vpi : 0,
4749 pmbox->u.mb.mbxCommand,
4750 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004751 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004752 }
4753 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004754 mod_timer(&psli->mbox_tmo, (jiffies +
4755 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004756 }
4757
4758 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004759 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004760 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004761 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004762 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004763 mb->mbxCommand, phba->pport->port_state,
4764 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004765
James Smart858c9f62007-06-17 19:56:39 -05004766 if (mb->mbxCommand != MBX_HEARTBEAT) {
4767 if (pmbox->vport) {
4768 lpfc_debugfs_disc_trc(pmbox->vport,
4769 LPFC_DISC_TRC_MBOX_VPORT,
4770 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4771 (uint32_t)mb->mbxCommand,
4772 mb->un.varWords[0], mb->un.varWords[1]);
4773 }
4774 else {
4775 lpfc_debugfs_disc_trc(phba->pport,
4776 LPFC_DISC_TRC_MBOX,
4777 "MBOX Send: cmd:x%x mb:x%x x%x",
4778 (uint32_t)mb->mbxCommand,
4779 mb->un.varWords[0], mb->un.varWords[1]);
4780 }
4781 }
4782
dea31012005-04-17 16:05:31 -05004783 psli->slistat.mbox_cmd++;
4784 evtctr = psli->slistat.mbox_event;
4785
4786 /* next set own bit for the adapter and copy over command word */
4787 mb->mbxOwner = OWN_CHIP;
4788
James Smart3772a992009-05-22 14:50:54 -04004789 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004790 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004791 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004792 } else {
James Smart92908312006-03-07 15:04:13 -05004793 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004794 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004795 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004796 }
4797
4798 /* First copy mbox command data to HBA SLIM, skip past first
4799 word */
4800 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4801 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4802 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4803
4804 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004805 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004806 to_slim = phba->MBslimaddr;
4807 writel(ldata, to_slim);
4808 readl(to_slim); /* flush */
4809
4810 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4811 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004812 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004813 }
4814 }
4815
4816 wmb();
dea31012005-04-17 16:05:31 -05004817
4818 switch (flag) {
4819 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004820 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004821 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004822 /* Interrupt board to do it */
4823 writel(CA_MBATT, phba->CAregaddr);
4824 readl(phba->CAregaddr); /* flush */
4825 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004826 break;
4827
4828 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004829 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004830 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004831 /* Interrupt board to do it */
4832 writel(CA_MBATT, phba->CAregaddr);
4833 readl(phba->CAregaddr); /* flush */
4834
James Smart3772a992009-05-22 14:50:54 -04004835 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004836 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004837 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004838 word0 = le32_to_cpu(word0);
4839 } else {
4840 /* First read mbox status word */
4841 word0 = readl(phba->MBslimaddr);
4842 }
4843
4844 /* Read the HBA Host Attention Register */
4845 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004846 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4847 mb->mbxCommand) *
4848 1000) + jiffies;
4849 i = 0;
dea31012005-04-17 16:05:31 -05004850 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004851 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4852 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004853 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004854 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004855 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004856 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004857 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004858 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004859 }
4860
4861 /* Check if we took a mbox interrupt while we were
4862 polling */
4863 if (((word0 & OWN_CHIP) != OWN_CHIP)
4864 && (evtctr != psli->slistat.mbox_event))
4865 break;
4866
James Smart09372822008-01-11 01:52:54 -05004867 if (i++ > 10) {
4868 spin_unlock_irqrestore(&phba->hbalock,
4869 drvr_flag);
4870 msleep(1);
4871 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4872 }
dea31012005-04-17 16:05:31 -05004873
James Smart3772a992009-05-22 14:50:54 -04004874 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004875 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004876 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004877 word0 = le32_to_cpu(word0);
4878 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4879 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004880 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004881 /* Check real SLIM for any errors */
4882 slimword0 = readl(phba->MBslimaddr);
4883 slimmb = (MAILBOX_t *) & slimword0;
4884 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4885 && slimmb->mbxStatus) {
4886 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004887 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004888 word0 = slimword0;
4889 }
4890 }
4891 } else {
4892 /* First copy command data */
4893 word0 = readl(phba->MBslimaddr);
4894 }
4895 /* Read the HBA Host Attention Register */
4896 ha_copy = readl(phba->HAregaddr);
4897 }
4898
James Smart3772a992009-05-22 14:50:54 -04004899 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004900 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04004901 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004902 } else {
4903 /* First copy command data */
4904 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4905 MAILBOX_CMD_SIZE);
4906 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4907 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05004908 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05004909 phba->MBslimaddr + DMP_RSP_OFFSET,
4910 mb->un.varDmp.word_cnt);
4911 }
4912 }
4913
4914 writel(HA_MBATT, phba->HAregaddr);
4915 readl(phba->HAregaddr); /* flush */
4916
4917 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4918 status = mb->mbxStatus;
4919 }
4920
James Smart2e0fef82007-06-17 19:56:36 -05004921 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4922 return status;
James Smart58da1ff2008-04-07 10:15:56 -04004923
4924out_not_finished:
4925 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04004926 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04004927 lpfc_mbox_cmpl_put(phba, pmbox);
4928 }
4929 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004930}
4931
James Smarte59058c2008-08-24 21:49:00 -04004932/**
James Smartf1126682009-06-10 17:22:44 -04004933 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
4934 * @phba: Pointer to HBA context object.
4935 *
4936 * The function blocks the posting of SLI4 asynchronous mailbox commands from
4937 * the driver internal pending mailbox queue. It will then try to wait out the
4938 * possible outstanding mailbox command before return.
4939 *
4940 * Returns:
4941 * 0 - the outstanding mailbox command completed; otherwise, the wait for
4942 * the outstanding mailbox command timed out.
4943 **/
4944static int
4945lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
4946{
4947 struct lpfc_sli *psli = &phba->sli;
4948 uint8_t actcmd = MBX_HEARTBEAT;
4949 int rc = 0;
4950 unsigned long timeout;
4951
4952 /* Mark the asynchronous mailbox command posting as blocked */
4953 spin_lock_irq(&phba->hbalock);
4954 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
4955 if (phba->sli.mbox_active)
4956 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
4957 spin_unlock_irq(&phba->hbalock);
4958 /* Determine how long we might wait for the active mailbox
4959 * command to be gracefully completed by firmware.
4960 */
4961 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
4962 jiffies;
4963 /* Wait for the outstnading mailbox command to complete */
4964 while (phba->sli.mbox_active) {
4965 /* Check active mailbox complete status every 2ms */
4966 msleep(2);
4967 if (time_after(jiffies, timeout)) {
4968 /* Timeout, marked the outstanding cmd not complete */
4969 rc = 1;
4970 break;
4971 }
4972 }
4973
4974 /* Can not cleanly block async mailbox command, fails it */
4975 if (rc) {
4976 spin_lock_irq(&phba->hbalock);
4977 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4978 spin_unlock_irq(&phba->hbalock);
4979 }
4980 return rc;
4981}
4982
4983/**
4984 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
4985 * @phba: Pointer to HBA context object.
4986 *
4987 * The function unblocks and resume posting of SLI4 asynchronous mailbox
4988 * commands from the driver internal pending mailbox queue. It makes sure
4989 * that there is no outstanding mailbox command before resuming posting
4990 * asynchronous mailbox commands. If, for any reason, there is outstanding
4991 * mailbox command, it will try to wait it out before resuming asynchronous
4992 * mailbox command posting.
4993 **/
4994static void
4995lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
4996{
4997 struct lpfc_sli *psli = &phba->sli;
4998
4999 spin_lock_irq(&phba->hbalock);
5000 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5001 /* Asynchronous mailbox posting is not blocked, do nothing */
5002 spin_unlock_irq(&phba->hbalock);
5003 return;
5004 }
5005
5006 /* Outstanding synchronous mailbox command is guaranteed to be done,
5007 * successful or timeout, after timing-out the outstanding mailbox
5008 * command shall always be removed, so just unblock posting async
5009 * mailbox command and resume
5010 */
5011 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5012 spin_unlock_irq(&phba->hbalock);
5013
5014 /* wake up worker thread to post asynchronlous mailbox command */
5015 lpfc_worker_wake_up(phba);
5016}
5017
5018/**
James Smartda0436e2009-05-22 14:51:39 -04005019 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5020 * @phba: Pointer to HBA context object.
5021 * @mboxq: Pointer to mailbox object.
5022 *
5023 * The function posts a mailbox to the port. The mailbox is expected
5024 * to be comletely filled in and ready for the port to operate on it.
5025 * This routine executes a synchronous completion operation on the
5026 * mailbox by polling for its completion.
5027 *
5028 * The caller must not be holding any locks when calling this routine.
5029 *
5030 * Returns:
5031 * MBX_SUCCESS - mailbox posted successfully
5032 * Any of the MBX error values.
5033 **/
5034static int
5035lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5036{
5037 int rc = MBX_SUCCESS;
5038 unsigned long iflag;
5039 uint32_t db_ready;
5040 uint32_t mcqe_status;
5041 uint32_t mbx_cmnd;
5042 unsigned long timeout;
5043 struct lpfc_sli *psli = &phba->sli;
5044 struct lpfc_mqe *mb = &mboxq->u.mqe;
5045 struct lpfc_bmbx_create *mbox_rgn;
5046 struct dma_address *dma_address;
5047 struct lpfc_register bmbx_reg;
5048
5049 /*
5050 * Only one mailbox can be active to the bootstrap mailbox region
5051 * at a time and there is no queueing provided.
5052 */
5053 spin_lock_irqsave(&phba->hbalock, iflag);
5054 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5055 spin_unlock_irqrestore(&phba->hbalock, iflag);
5056 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5057 "(%d):2532 Mailbox command x%x (x%x) "
5058 "cannot issue Data: x%x x%x\n",
5059 mboxq->vport ? mboxq->vport->vpi : 0,
5060 mboxq->u.mb.mbxCommand,
5061 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5062 psli->sli_flag, MBX_POLL);
5063 return MBXERR_ERROR;
5064 }
5065 /* The server grabs the token and owns it until release */
5066 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5067 phba->sli.mbox_active = mboxq;
5068 spin_unlock_irqrestore(&phba->hbalock, iflag);
5069
5070 /*
5071 * Initialize the bootstrap memory region to avoid stale data areas
5072 * in the mailbox post. Then copy the caller's mailbox contents to
5073 * the bmbx mailbox region.
5074 */
5075 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5076 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5077 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5078 sizeof(struct lpfc_mqe));
5079
5080 /* Post the high mailbox dma address to the port and wait for ready. */
5081 dma_address = &phba->sli4_hba.bmbx.dma_address;
5082 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5083
5084 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5085 * 1000) + jiffies;
5086 do {
5087 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5088 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5089 if (!db_ready)
5090 msleep(2);
5091
5092 if (time_after(jiffies, timeout)) {
5093 rc = MBXERR_ERROR;
5094 goto exit;
5095 }
5096 } while (!db_ready);
5097
5098 /* Post the low mailbox dma address to the port. */
5099 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5100 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5101 * 1000) + jiffies;
5102 do {
5103 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5104 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5105 if (!db_ready)
5106 msleep(2);
5107
5108 if (time_after(jiffies, timeout)) {
5109 rc = MBXERR_ERROR;
5110 goto exit;
5111 }
5112 } while (!db_ready);
5113
5114 /*
5115 * Read the CQ to ensure the mailbox has completed.
5116 * If so, update the mailbox status so that the upper layers
5117 * can complete the request normally.
5118 */
5119 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5120 sizeof(struct lpfc_mqe));
5121 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5122 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5123 sizeof(struct lpfc_mcqe));
5124 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5125
5126 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5127 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5128 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5129 rc = MBXERR_ERROR;
5130 }
5131
5132 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5133 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5134 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5135 " x%x x%x CQ: x%x x%x x%x x%x\n",
5136 mboxq->vport ? mboxq->vport->vpi : 0,
5137 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5138 bf_get(lpfc_mqe_status, mb),
5139 mb->un.mb_words[0], mb->un.mb_words[1],
5140 mb->un.mb_words[2], mb->un.mb_words[3],
5141 mb->un.mb_words[4], mb->un.mb_words[5],
5142 mb->un.mb_words[6], mb->un.mb_words[7],
5143 mb->un.mb_words[8], mb->un.mb_words[9],
5144 mb->un.mb_words[10], mb->un.mb_words[11],
5145 mb->un.mb_words[12], mboxq->mcqe.word0,
5146 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5147 mboxq->mcqe.trailer);
5148exit:
5149 /* We are holding the token, no needed for lock when release */
5150 spin_lock_irqsave(&phba->hbalock, iflag);
5151 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5152 phba->sli.mbox_active = NULL;
5153 spin_unlock_irqrestore(&phba->hbalock, iflag);
5154 return rc;
5155}
5156
5157/**
5158 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5159 * @phba: Pointer to HBA context object.
5160 * @pmbox: Pointer to mailbox object.
5161 * @flag: Flag indicating how the mailbox need to be processed.
5162 *
5163 * This function is called by discovery code and HBA management code to submit
5164 * a mailbox command to firmware with SLI-4 interface spec.
5165 *
5166 * Return codes the caller owns the mailbox command after the return of the
5167 * function.
5168 **/
5169static int
5170lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5171 uint32_t flag)
5172{
5173 struct lpfc_sli *psli = &phba->sli;
5174 unsigned long iflags;
5175 int rc;
5176
James Smart8fa38512009-07-19 10:01:03 -04005177 rc = lpfc_mbox_dev_check(phba);
5178 if (unlikely(rc)) {
5179 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5180 "(%d):2544 Mailbox command x%x (x%x) "
5181 "cannot issue Data: x%x x%x\n",
5182 mboxq->vport ? mboxq->vport->vpi : 0,
5183 mboxq->u.mb.mbxCommand,
5184 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5185 psli->sli_flag, flag);
5186 goto out_not_finished;
5187 }
5188
James Smartda0436e2009-05-22 14:51:39 -04005189 /* Detect polling mode and jump to a handler */
5190 if (!phba->sli4_hba.intr_enable) {
5191 if (flag == MBX_POLL)
5192 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5193 else
5194 rc = -EIO;
5195 if (rc != MBX_SUCCESS)
5196 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5197 "(%d):2541 Mailbox command x%x "
5198 "(x%x) cannot issue Data: x%x x%x\n",
5199 mboxq->vport ? mboxq->vport->vpi : 0,
5200 mboxq->u.mb.mbxCommand,
5201 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5202 psli->sli_flag, flag);
5203 return rc;
5204 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005205 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5206 "(%d):2542 Try to issue mailbox command "
5207 "x%x (x%x) synchronously ahead of async"
5208 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005209 mboxq->vport ? mboxq->vport->vpi : 0,
5210 mboxq->u.mb.mbxCommand,
5211 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5212 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005213 /* Try to block the asynchronous mailbox posting */
5214 rc = lpfc_sli4_async_mbox_block(phba);
5215 if (!rc) {
5216 /* Successfully blocked, now issue sync mbox cmd */
5217 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5218 if (rc != MBX_SUCCESS)
5219 lpfc_printf_log(phba, KERN_ERR,
5220 LOG_MBOX | LOG_SLI,
5221 "(%d):2597 Mailbox command "
5222 "x%x (x%x) cannot issue "
5223 "Data: x%x x%x\n",
5224 mboxq->vport ?
5225 mboxq->vport->vpi : 0,
5226 mboxq->u.mb.mbxCommand,
5227 lpfc_sli4_mbox_opcode_get(phba,
5228 mboxq),
5229 psli->sli_flag, flag);
5230 /* Unblock the async mailbox posting afterward */
5231 lpfc_sli4_async_mbox_unblock(phba);
5232 }
5233 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005234 }
5235
5236 /* Now, interrupt mode asynchrous mailbox command */
5237 rc = lpfc_mbox_cmd_check(phba, mboxq);
5238 if (rc) {
5239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5240 "(%d):2543 Mailbox command x%x (x%x) "
5241 "cannot issue Data: x%x x%x\n",
5242 mboxq->vport ? mboxq->vport->vpi : 0,
5243 mboxq->u.mb.mbxCommand,
5244 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5245 psli->sli_flag, flag);
5246 goto out_not_finished;
5247 }
James Smartda0436e2009-05-22 14:51:39 -04005248
5249 /* Put the mailbox command to the driver internal FIFO */
5250 psli->slistat.mbox_busy++;
5251 spin_lock_irqsave(&phba->hbalock, iflags);
5252 lpfc_mbox_put(phba, mboxq);
5253 spin_unlock_irqrestore(&phba->hbalock, iflags);
5254 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5255 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5256 "x%x (x%x) x%x x%x x%x\n",
5257 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5258 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5259 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5260 phba->pport->port_state,
5261 psli->sli_flag, MBX_NOWAIT);
5262 /* Wake up worker thread to transport mailbox command from head */
5263 lpfc_worker_wake_up(phba);
5264
5265 return MBX_BUSY;
5266
5267out_not_finished:
5268 return MBX_NOT_FINISHED;
5269}
5270
5271/**
5272 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5273 * @phba: Pointer to HBA context object.
5274 *
5275 * This function is called by worker thread to send a mailbox command to
5276 * SLI4 HBA firmware.
5277 *
5278 **/
5279int
5280lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5281{
5282 struct lpfc_sli *psli = &phba->sli;
5283 LPFC_MBOXQ_t *mboxq;
5284 int rc = MBX_SUCCESS;
5285 unsigned long iflags;
5286 struct lpfc_mqe *mqe;
5287 uint32_t mbx_cmnd;
5288
5289 /* Check interrupt mode before post async mailbox command */
5290 if (unlikely(!phba->sli4_hba.intr_enable))
5291 return MBX_NOT_FINISHED;
5292
5293 /* Check for mailbox command service token */
5294 spin_lock_irqsave(&phba->hbalock, iflags);
5295 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5296 spin_unlock_irqrestore(&phba->hbalock, iflags);
5297 return MBX_NOT_FINISHED;
5298 }
5299 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5300 spin_unlock_irqrestore(&phba->hbalock, iflags);
5301 return MBX_NOT_FINISHED;
5302 }
5303 if (unlikely(phba->sli.mbox_active)) {
5304 spin_unlock_irqrestore(&phba->hbalock, iflags);
5305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5306 "0384 There is pending active mailbox cmd\n");
5307 return MBX_NOT_FINISHED;
5308 }
5309 /* Take the mailbox command service token */
5310 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5311
5312 /* Get the next mailbox command from head of queue */
5313 mboxq = lpfc_mbox_get(phba);
5314
5315 /* If no more mailbox command waiting for post, we're done */
5316 if (!mboxq) {
5317 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5318 spin_unlock_irqrestore(&phba->hbalock, iflags);
5319 return MBX_SUCCESS;
5320 }
5321 phba->sli.mbox_active = mboxq;
5322 spin_unlock_irqrestore(&phba->hbalock, iflags);
5323
5324 /* Check device readiness for posting mailbox command */
5325 rc = lpfc_mbox_dev_check(phba);
5326 if (unlikely(rc))
5327 /* Driver clean routine will clean up pending mailbox */
5328 goto out_not_finished;
5329
5330 /* Prepare the mbox command to be posted */
5331 mqe = &mboxq->u.mqe;
5332 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5333
5334 /* Start timer for the mbox_tmo and log some mailbox post messages */
5335 mod_timer(&psli->mbox_tmo, (jiffies +
5336 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5337
5338 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5339 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5340 "x%x x%x\n",
5341 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5342 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5343 phba->pport->port_state, psli->sli_flag);
5344
5345 if (mbx_cmnd != MBX_HEARTBEAT) {
5346 if (mboxq->vport) {
5347 lpfc_debugfs_disc_trc(mboxq->vport,
5348 LPFC_DISC_TRC_MBOX_VPORT,
5349 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5350 mbx_cmnd, mqe->un.mb_words[0],
5351 mqe->un.mb_words[1]);
5352 } else {
5353 lpfc_debugfs_disc_trc(phba->pport,
5354 LPFC_DISC_TRC_MBOX,
5355 "MBOX Send: cmd:x%x mb:x%x x%x",
5356 mbx_cmnd, mqe->un.mb_words[0],
5357 mqe->un.mb_words[1]);
5358 }
5359 }
5360 psli->slistat.mbox_cmd++;
5361
5362 /* Post the mailbox command to the port */
5363 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5364 if (rc != MBX_SUCCESS) {
5365 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5366 "(%d):2533 Mailbox command x%x (x%x) "
5367 "cannot issue Data: x%x x%x\n",
5368 mboxq->vport ? mboxq->vport->vpi : 0,
5369 mboxq->u.mb.mbxCommand,
5370 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5371 psli->sli_flag, MBX_NOWAIT);
5372 goto out_not_finished;
5373 }
5374
5375 return rc;
5376
5377out_not_finished:
5378 spin_lock_irqsave(&phba->hbalock, iflags);
5379 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5380 __lpfc_mbox_cmpl_put(phba, mboxq);
5381 /* Release the token */
5382 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5383 phba->sli.mbox_active = NULL;
5384 spin_unlock_irqrestore(&phba->hbalock, iflags);
5385
5386 return MBX_NOT_FINISHED;
5387}
5388
5389/**
5390 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5391 * @phba: Pointer to HBA context object.
5392 * @pmbox: Pointer to mailbox object.
5393 * @flag: Flag indicating how the mailbox need to be processed.
5394 *
5395 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5396 * the API jump table function pointer from the lpfc_hba struct.
5397 *
5398 * Return codes the caller owns the mailbox command after the return of the
5399 * function.
5400 **/
5401int
5402lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5403{
5404 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5405}
5406
5407/**
5408 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5409 * @phba: The hba struct for which this call is being executed.
5410 * @dev_grp: The HBA PCI-Device group number.
5411 *
5412 * This routine sets up the mbox interface API function jump table in @phba
5413 * struct.
5414 * Returns: 0 - success, -ENODEV - failure.
5415 **/
5416int
5417lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5418{
5419
5420 switch (dev_grp) {
5421 case LPFC_PCI_DEV_LP:
5422 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5423 phba->lpfc_sli_handle_slow_ring_event =
5424 lpfc_sli_handle_slow_ring_event_s3;
5425 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5426 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5427 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5428 break;
5429 case LPFC_PCI_DEV_OC:
5430 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5431 phba->lpfc_sli_handle_slow_ring_event =
5432 lpfc_sli_handle_slow_ring_event_s4;
5433 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5434 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5435 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5436 break;
5437 default:
5438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5439 "1420 Invalid HBA PCI-device group: 0x%x\n",
5440 dev_grp);
5441 return -ENODEV;
5442 break;
5443 }
5444 return 0;
5445}
5446
5447/**
James Smart3621a712009-04-06 18:47:14 -04005448 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005449 * @phba: Pointer to HBA context object.
5450 * @pring: Pointer to driver SLI ring object.
5451 * @piocb: Pointer to address of newly added command iocb.
5452 *
5453 * This function is called with hbalock held to add a command
5454 * iocb to the txq when SLI layer cannot submit the command iocb
5455 * to the ring.
5456 **/
James Smart858c9f62007-06-17 19:56:39 -05005457static void
James Smart92d7f7b2007-06-17 19:56:38 -05005458__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005459 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005460{
5461 /* Insert the caller's iocb in the txq tail for later processing. */
5462 list_add_tail(&piocb->list, &pring->txq);
5463 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005464}
5465
James Smarte59058c2008-08-24 21:49:00 -04005466/**
James Smart3621a712009-04-06 18:47:14 -04005467 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005468 * @phba: Pointer to HBA context object.
5469 * @pring: Pointer to driver SLI ring object.
5470 * @piocb: Pointer to address of newly added command iocb.
5471 *
5472 * This function is called with hbalock held before a new
5473 * iocb is submitted to the firmware. This function checks
5474 * txq to flush the iocbs in txq to Firmware before
5475 * submitting new iocbs to the Firmware.
5476 * If there are iocbs in the txq which need to be submitted
5477 * to firmware, lpfc_sli_next_iocb returns the first element
5478 * of the txq after dequeuing it from txq.
5479 * If there is no iocb in the txq then the function will return
5480 * *piocb and *piocb is set to NULL. Caller needs to check
5481 * *piocb to find if there are more commands in the txq.
5482 **/
dea31012005-04-17 16:05:31 -05005483static struct lpfc_iocbq *
5484lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005485 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005486{
5487 struct lpfc_iocbq * nextiocb;
5488
5489 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5490 if (!nextiocb) {
5491 nextiocb = *piocb;
5492 *piocb = NULL;
5493 }
5494
5495 return nextiocb;
5496}
5497
James Smarte59058c2008-08-24 21:49:00 -04005498/**
James Smart3772a992009-05-22 14:50:54 -04005499 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005500 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005501 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005502 * @piocb: Pointer to command iocb.
5503 * @flag: Flag indicating if this command can be put into txq.
5504 *
James Smart3772a992009-05-22 14:50:54 -04005505 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5506 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5507 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5508 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5509 * this function allows only iocbs for posting buffers. This function finds
5510 * next available slot in the command ring and posts the command to the
5511 * available slot and writes the port attention register to request HBA start
5512 * processing new iocb. If there is no slot available in the ring and
5513 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5514 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005515 *
James Smart3772a992009-05-22 14:50:54 -04005516 * This function is called with hbalock held. The function will return success
5517 * after it successfully submit the iocb to firmware or after adding to the
5518 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005519 **/
James Smart98c9ea52007-10-27 13:37:33 -04005520static int
James Smart3772a992009-05-22 14:50:54 -04005521__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005522 struct lpfc_iocbq *piocb, uint32_t flag)
5523{
5524 struct lpfc_iocbq *nextiocb;
5525 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005526 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005527
James Smart92d7f7b2007-06-17 19:56:38 -05005528 if (piocb->iocb_cmpl && (!piocb->vport) &&
5529 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5530 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5531 lpfc_printf_log(phba, KERN_ERR,
5532 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005533 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005534 piocb->iocb.ulpCommand);
5535 dump_stack();
5536 return IOCB_ERROR;
5537 }
5538
5539
Linas Vepstas8d63f372007-02-14 14:28:36 -06005540 /* If the PCI channel is in offline state, do not post iocbs. */
5541 if (unlikely(pci_channel_offline(phba->pcidev)))
5542 return IOCB_ERROR;
5543
James Smarta257bf92009-04-06 18:48:10 -04005544 /* If HBA has a deferred error attention, fail the iocb. */
5545 if (unlikely(phba->hba_flag & DEFER_ERATT))
5546 return IOCB_ERROR;
5547
dea31012005-04-17 16:05:31 -05005548 /*
5549 * We should never get an IOCB if we are in a < LINK_DOWN state
5550 */
James Smart2e0fef82007-06-17 19:56:36 -05005551 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005552 return IOCB_ERROR;
5553
5554 /*
5555 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005556 * outstanding event.
dea31012005-04-17 16:05:31 -05005557 */
James Smart0b727fe2007-10-27 13:37:25 -04005558 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005559 goto iocb_busy;
5560
James Smart2e0fef82007-06-17 19:56:36 -05005561 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005562 /*
James Smart2680eea2007-04-25 09:52:55 -04005563 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005564 * can be issued if the link is not up.
5565 */
5566 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005567 case CMD_GEN_REQUEST64_CR:
5568 case CMD_GEN_REQUEST64_CX:
5569 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5570 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005571 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005572 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5573 MENLO_TRANSPORT_TYPE))
5574
5575 goto iocb_busy;
5576 break;
dea31012005-04-17 16:05:31 -05005577 case CMD_QUE_RING_BUF_CN:
5578 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005579 /*
5580 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5581 * completion, iocb_cmpl MUST be 0.
5582 */
5583 if (piocb->iocb_cmpl)
5584 piocb->iocb_cmpl = NULL;
5585 /*FALLTHROUGH*/
5586 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005587 case CMD_CLOSE_XRI_CN:
5588 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005589 break;
5590 default:
5591 goto iocb_busy;
5592 }
5593
5594 /*
5595 * For FCP commands, we must be in a state where we can process link
5596 * attention events.
5597 */
5598 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005599 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005600 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005601 }
dea31012005-04-17 16:05:31 -05005602
dea31012005-04-17 16:05:31 -05005603 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5604 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5605 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5606
5607 if (iocb)
5608 lpfc_sli_update_ring(phba, pring);
5609 else
5610 lpfc_sli_update_full_ring(phba, pring);
5611
5612 if (!piocb)
5613 return IOCB_SUCCESS;
5614
5615 goto out_busy;
5616
5617 iocb_busy:
5618 pring->stats.iocb_cmd_delay++;
5619
5620 out_busy:
5621
5622 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005623 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005624 return IOCB_SUCCESS;
5625 }
5626
5627 return IOCB_BUSY;
5628}
5629
James Smart3772a992009-05-22 14:50:54 -04005630/**
James Smart4f774512009-05-22 14:52:35 -04005631 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5632 * @phba: Pointer to HBA context object.
5633 * @piocb: Pointer to command iocb.
5634 * @sglq: Pointer to the scatter gather queue object.
5635 *
5636 * This routine converts the bpl or bde that is in the IOCB
5637 * to a sgl list for the sli4 hardware. The physical address
5638 * of the bpl/bde is converted back to a virtual address.
5639 * If the IOCB contains a BPL then the list of BDE's is
5640 * converted to sli4_sge's. If the IOCB contains a single
5641 * BDE then it is converted to a single sli_sge.
5642 * The IOCB is still in cpu endianess so the contents of
5643 * the bpl can be used without byte swapping.
5644 *
5645 * Returns valid XRI = Success, NO_XRI = Failure.
5646**/
5647static uint16_t
5648lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5649 struct lpfc_sglq *sglq)
5650{
5651 uint16_t xritag = NO_XRI;
5652 struct ulp_bde64 *bpl = NULL;
5653 struct ulp_bde64 bde;
5654 struct sli4_sge *sgl = NULL;
5655 IOCB_t *icmd;
5656 int numBdes = 0;
5657 int i = 0;
5658
5659 if (!piocbq || !sglq)
5660 return xritag;
5661
5662 sgl = (struct sli4_sge *)sglq->sgl;
5663 icmd = &piocbq->iocb;
5664 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5665 numBdes = icmd->un.genreq64.bdl.bdeSize /
5666 sizeof(struct ulp_bde64);
5667 /* The addrHigh and addrLow fields within the IOCB
5668 * have not been byteswapped yet so there is no
5669 * need to swap them back.
5670 */
5671 bpl = (struct ulp_bde64 *)
5672 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5673
5674 if (!bpl)
5675 return xritag;
5676
5677 for (i = 0; i < numBdes; i++) {
5678 /* Should already be byte swapped. */
5679 sgl->addr_hi = bpl->addrHigh;
5680 sgl->addr_lo = bpl->addrLow;
5681 /* swap the size field back to the cpu so we
5682 * can assign it to the sgl.
5683 */
5684 bde.tus.w = le32_to_cpu(bpl->tus.w);
5685 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5686 if ((i+1) == numBdes)
5687 bf_set(lpfc_sli4_sge_last, sgl, 1);
5688 else
5689 bf_set(lpfc_sli4_sge_last, sgl, 0);
5690 sgl->word2 = cpu_to_le32(sgl->word2);
5691 sgl->word3 = cpu_to_le32(sgl->word3);
5692 bpl++;
5693 sgl++;
5694 }
5695 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5696 /* The addrHigh and addrLow fields of the BDE have not
5697 * been byteswapped yet so they need to be swapped
5698 * before putting them in the sgl.
5699 */
5700 sgl->addr_hi =
5701 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5702 sgl->addr_lo =
5703 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5704 bf_set(lpfc_sli4_sge_len, sgl,
5705 icmd->un.genreq64.bdl.bdeSize);
5706 bf_set(lpfc_sli4_sge_last, sgl, 1);
5707 sgl->word2 = cpu_to_le32(sgl->word2);
5708 sgl->word3 = cpu_to_le32(sgl->word3);
5709 }
5710 return sglq->sli4_xritag;
5711}
5712
5713/**
5714 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5715 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005716 *
5717 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005718 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5719 * held.
James Smart4f774512009-05-22 14:52:35 -04005720 *
5721 * Return: index into SLI4 fast-path FCP queue index.
5722 **/
5723static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005724lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005725{
James Smart8fa38512009-07-19 10:01:03 -04005726 ++phba->fcp_qidx;
5727 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5728 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005729
James Smart8fa38512009-07-19 10:01:03 -04005730 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005731}
5732
5733/**
5734 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5735 * @phba: Pointer to HBA context object.
5736 * @piocb: Pointer to command iocb.
5737 * @wqe: Pointer to the work queue entry.
5738 *
5739 * This routine converts the iocb command to its Work Queue Entry
5740 * equivalent. The wqe pointer should not have any fields set when
5741 * this routine is called because it will memcpy over them.
5742 * This routine does not set the CQ_ID or the WQEC bits in the
5743 * wqe.
5744 *
5745 * Returns: 0 = Success, IOCB_ERROR = Failure.
5746 **/
5747static int
5748lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5749 union lpfc_wqe *wqe)
5750{
James Smart5ffc2662009-11-18 15:39:44 -05005751 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005752 uint8_t ct = 0;
5753 uint32_t fip;
5754 uint32_t abort_tag;
5755 uint8_t command_type = ELS_COMMAND_NON_FIP;
5756 uint8_t cmnd;
5757 uint16_t xritag;
5758 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005759 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005760 int numBdes, i;
5761 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005762
James Smart45ed1192009-10-02 15:17:02 -04005763 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005764 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005765 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005766 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005767 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005768 command_type = ELS_COMMAND_FIP;
5769 else
5770 command_type = ELS_COMMAND_NON_FIP;
5771
James Smart4f774512009-05-22 14:52:35 -04005772 /* Some of the fields are in the right position already */
5773 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5774 abort_tag = (uint32_t) iocbq->iotag;
5775 xritag = iocbq->sli4_xritag;
5776 wqe->words[7] = 0; /* The ct field has moved so reset */
5777 /* words0-2 bpl convert bde */
5778 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005779 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5780 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005781 bpl = (struct ulp_bde64 *)
5782 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5783 if (!bpl)
5784 return IOCB_ERROR;
5785
5786 /* Should already be byte swapped. */
5787 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5788 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5789 /* swap the size field back to the cpu so we
5790 * can assign it to the sgl.
5791 */
5792 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005793 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5794 total_len = 0;
5795 for (i = 0; i < numBdes; i++) {
5796 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5797 total_len += bde.tus.f.bdeSize;
5798 }
James Smart4f774512009-05-22 14:52:35 -04005799 } else
James Smart5ffc2662009-11-18 15:39:44 -05005800 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005801
5802 iocbq->iocb.ulpIoTag = iocbq->iotag;
5803 cmnd = iocbq->iocb.ulpCommand;
5804
5805 switch (iocbq->iocb.ulpCommand) {
5806 case CMD_ELS_REQUEST64_CR:
5807 if (!iocbq->iocb.ulpLe) {
5808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5809 "2007 Only Limited Edition cmd Format"
5810 " supported 0x%x\n",
5811 iocbq->iocb.ulpCommand);
5812 return IOCB_ERROR;
5813 }
James Smart5ffc2662009-11-18 15:39:44 -05005814 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005815 /* Els_reguest64 has a TMO */
5816 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5817 iocbq->iocb.ulpTimeout);
5818 /* Need a VF for word 4 set the vf bit*/
5819 bf_set(els_req64_vf, &wqe->els_req, 0);
5820 /* And a VFID for word 12 */
5821 bf_set(els_req64_vfid, &wqe->els_req, 0);
5822 /*
5823 * Set ct field to 3, indicates that the context_tag field
5824 * contains the FCFI and remote N_Port_ID is
5825 * in word 5.
5826 */
5827
5828 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5829 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5830 iocbq->iocb.ulpContext);
5831
James Smart4f774512009-05-22 14:52:35 -04005832 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5833 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5834 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005835
5836 if (command_type == ELS_COMMAND_FIP) {
5837 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5838 >> LPFC_FIP_ELS_ID_SHIFT);
5839 }
5840 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5841
James Smart4f774512009-05-22 14:52:35 -04005842 break;
James Smart5ffc2662009-11-18 15:39:44 -05005843 case CMD_XMIT_SEQUENCE64_CX:
5844 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5845 iocbq->iocb.un.ulpWord[3]);
5846 wqe->generic.word3 = 0;
5847 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5848 bf_set(wqe_xc, &wqe->generic, 1);
5849 /* The entire sequence is transmitted for this IOCB */
5850 xmit_len = total_len;
5851 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005852 case CMD_XMIT_SEQUENCE64_CR:
5853 /* word3 iocb=io_tag32 wqe=payload_offset */
5854 /* payload offset used for multilpe outstanding
5855 * sequences on the same exchange
5856 */
5857 wqe->words[3] = 0;
5858 /* word4 relative_offset memcpy */
5859 /* word5 r_ctl/df_ctl memcpy */
5860 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005861 wqe->xmit_sequence.xmit_len = xmit_len;
5862 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005863 break;
5864 case CMD_XMIT_BCAST64_CN:
5865 /* word3 iocb=iotag32 wqe=payload_len */
5866 wqe->words[3] = 0; /* no definition for this in wqe */
5867 /* word4 iocb=rsvd wqe=rsvd */
5868 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5869 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5870 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5871 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5872 break;
5873 case CMD_FCP_IWRITE64_CR:
5874 command_type = FCP_COMMAND_DATA_OUT;
5875 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5876 * confusing.
5877 * word3 is payload_len: byte offset to the sgl entry for the
5878 * fcp_command.
5879 * word4 is total xfer len, same as the IOCB->ulpParameter.
5880 * word5 is initial xfer len 0 = wait for xfer-ready
5881 */
5882
5883 /* Always wait for xfer-ready before sending data */
5884 wqe->fcp_iwrite.initial_xfer_len = 0;
5885 /* word 4 (xfer length) should have been set on the memcpy */
5886
5887 /* allow write to fall through to read */
5888 case CMD_FCP_IREAD64_CR:
5889 /* FCP_CMD is always the 1st sgl entry */
5890 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05005891 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04005892
5893 /* word 4 (xfer length) should have been set on the memcpy */
5894
5895 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5896 iocbq->iocb.ulpFCP2Rcvy);
5897 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5898 /* The XC bit and the XS bit are similar. The driver never
5899 * tracked whether or not the exchange was previouslly open.
5900 * XC = Exchange create, 0 is create. 1 is already open.
5901 * XS = link cmd: 1 do not close the exchange after command.
5902 * XS = 0 close exchange when command completes.
5903 * The only time we would not set the XC bit is when the XS bit
5904 * is set and we are sending our 2nd or greater command on
5905 * this exchange.
5906 */
James Smart4f774512009-05-22 14:52:35 -04005907 /* Always open the exchange */
5908 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5909
5910 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5911 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04005912 break;
5913 case CMD_FCP_ICMND64_CR:
5914 /* Always open the exchange */
5915 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5916
5917 wqe->words[4] = 0;
5918 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5919 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04005920 break;
5921 case CMD_GEN_REQUEST64_CR:
5922 /* word3 command length is described as byte offset to the
5923 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5924 * sgl[0] = cmnd
5925 * sgl[1] = rsp.
5926 *
5927 */
James Smart5ffc2662009-11-18 15:39:44 -05005928 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005929 /* Word4 parameter copied in the memcpy */
5930 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
5931 /* word6 context tag copied in memcpy */
5932 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
5933 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5935 "2015 Invalid CT %x command 0x%x\n",
5936 ct, iocbq->iocb.ulpCommand);
5937 return IOCB_ERROR;
5938 }
5939 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
5940 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
5941 iocbq->iocb.ulpTimeout);
5942
5943 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5944 command_type = OTHER_COMMAND;
5945 break;
5946 case CMD_XMIT_ELS_RSP64_CX:
5947 /* words0-2 BDE memcpy */
5948 /* word3 iocb=iotag32 wqe=rsvd */
5949 wqe->words[3] = 0;
5950 /* word4 iocb=did wge=rsvd. */
5951 wqe->words[4] = 0;
5952 /* word5 iocb=rsvd wge=did */
5953 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
5954 iocbq->iocb.un.elsreq64.remoteID);
5955
5956 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5957 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5958
5959 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5960 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5961 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
5962 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5963 iocbq->vport->vpi + phba->vpi_base);
5964 command_type = OTHER_COMMAND;
5965 break;
5966 case CMD_CLOSE_XRI_CN:
5967 case CMD_ABORT_XRI_CN:
5968 case CMD_ABORT_XRI_CX:
5969 /* words 0-2 memcpy should be 0 rserved */
5970 /* port will send abts */
5971 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5972 /*
5973 * The link is down so the fw does not need to send abts
5974 * on the wire.
5975 */
5976 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
5977 else
5978 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
5979 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
5980 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5981 wqe->words[5] = 0;
5982 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5983 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5984 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5985 wqe->generic.abort_tag = abort_tag;
5986 /*
5987 * The abort handler will send us CMD_ABORT_XRI_CN or
5988 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
5989 */
5990 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
5991 cmnd = CMD_ABORT_XRI_CX;
5992 command_type = OTHER_COMMAND;
5993 xritag = 0;
5994 break;
James Smart6669f9b2009-10-02 15:16:45 -04005995 case CMD_XMIT_BLS_RSP64_CX:
5996 /* As BLS ABTS-ACC WQE is very different from other WQEs,
5997 * we re-construct this WQE here based on information in
5998 * iocbq from scratch.
5999 */
6000 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006001 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006002 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006003 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6004 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6005 LPFC_ABTS_UNSOL_INT) {
6006 /* ABTS sent by initiator to CT exchange, the
6007 * RX_ID field will be filled with the newly
6008 * allocated responder XRI.
6009 */
6010 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6011 iocbq->sli4_xritag);
6012 } else {
6013 /* ABTS sent by responder to CT exchange, the
6014 * RX_ID field will be filled with the responder
6015 * RX_ID from ABTS.
6016 */
6017 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6018 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6019 }
James Smart6669f9b2009-10-02 15:16:45 -04006020 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6021 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6022 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6023 iocbq->iocb.ulpContext);
6024 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6025 command_type = OTHER_COMMAND;
6026 break;
James Smart4f774512009-05-22 14:52:35 -04006027 case CMD_XRI_ABORTED_CX:
6028 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6029 /* words0-2 are all 0's no bde */
6030 /* word3 and word4 are rsvrd */
6031 wqe->words[3] = 0;
6032 wqe->words[4] = 0;
6033 /* word5 iocb=rsvd wge=did */
6034 /* There is no remote port id in the IOCB? */
6035 /* Let this fall through and fail */
6036 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6037 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6038 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6039 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6040 default:
6041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6042 "2014 Invalid command 0x%x\n",
6043 iocbq->iocb.ulpCommand);
6044 return IOCB_ERROR;
6045 break;
6046
6047 }
6048 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6049 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6050 wqe->generic.abort_tag = abort_tag;
6051 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6052 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6053 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6054 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6055
6056 return 0;
6057}
6058
6059/**
6060 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6061 * @phba: Pointer to HBA context object.
6062 * @ring_number: SLI ring number to issue iocb on.
6063 * @piocb: Pointer to command iocb.
6064 * @flag: Flag indicating if this command can be put into txq.
6065 *
6066 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6067 * an iocb command to an HBA with SLI-4 interface spec.
6068 *
6069 * This function is called with hbalock held. The function will return success
6070 * after it successfully submit the iocb to firmware or after adding to the
6071 * txq.
6072 **/
6073static int
6074__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6075 struct lpfc_iocbq *piocb, uint32_t flag)
6076{
6077 struct lpfc_sglq *sglq;
6078 uint16_t xritag;
6079 union lpfc_wqe wqe;
6080 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006081
6082 if (piocb->sli4_xritag == NO_XRI) {
6083 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006084 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006085 sglq = NULL;
6086 else {
6087 sglq = __lpfc_sli_get_sglq(phba);
6088 if (!sglq)
6089 return IOCB_ERROR;
6090 piocb->sli4_xritag = sglq->sli4_xritag;
6091 }
6092 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6093 sglq = NULL; /* These IO's already have an XRI and
6094 * a mapped sgl.
6095 */
6096 } else {
6097 /* This is a continuation of a commandi,(CX) so this
6098 * sglq is on the active list
6099 */
6100 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6101 if (!sglq)
6102 return IOCB_ERROR;
6103 }
6104
6105 if (sglq) {
6106 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6107 if (xritag != sglq->sli4_xritag)
6108 return IOCB_ERROR;
6109 }
6110
6111 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6112 return IOCB_ERROR;
6113
6114 if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart5ffc2662009-11-18 15:39:44 -05006115 /*
6116 * For FCP command IOCB, get a new WQ index to distribute
6117 * WQE across the WQsr. On the other hand, for abort IOCB,
6118 * it carries the same WQ index to the original command
6119 * IOCB.
6120 */
6121 if ((piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6122 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN))
6123 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6124 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6125 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006126 return IOCB_ERROR;
6127 } else {
6128 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6129 return IOCB_ERROR;
6130 }
6131 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6132
6133 return 0;
6134}
6135
6136/**
James Smart3772a992009-05-22 14:50:54 -04006137 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6138 *
6139 * This routine wraps the actual lockless version for issusing IOCB function
6140 * pointer from the lpfc_hba struct.
6141 *
6142 * Return codes:
6143 * IOCB_ERROR - Error
6144 * IOCB_SUCCESS - Success
6145 * IOCB_BUSY - Busy
6146 **/
6147static inline int
6148__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6149 struct lpfc_iocbq *piocb, uint32_t flag)
6150{
6151 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6152}
6153
6154/**
6155 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6156 * @phba: The hba struct for which this call is being executed.
6157 * @dev_grp: The HBA PCI-Device group number.
6158 *
6159 * This routine sets up the SLI interface API function jump table in @phba
6160 * struct.
6161 * Returns: 0 - success, -ENODEV - failure.
6162 **/
6163int
6164lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6165{
6166
6167 switch (dev_grp) {
6168 case LPFC_PCI_DEV_LP:
6169 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6170 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6171 break;
James Smart4f774512009-05-22 14:52:35 -04006172 case LPFC_PCI_DEV_OC:
6173 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6174 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6175 break;
James Smart3772a992009-05-22 14:50:54 -04006176 default:
6177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6178 "1419 Invalid HBA PCI-device group: 0x%x\n",
6179 dev_grp);
6180 return -ENODEV;
6181 break;
6182 }
6183 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6184 return 0;
6185}
James Smart92d7f7b2007-06-17 19:56:38 -05006186
James Smarte59058c2008-08-24 21:49:00 -04006187/**
James Smart3621a712009-04-06 18:47:14 -04006188 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006189 * @phba: Pointer to HBA context object.
6190 * @pring: Pointer to driver SLI ring object.
6191 * @piocb: Pointer to command iocb.
6192 * @flag: Flag indicating if this command can be put into txq.
6193 *
6194 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6195 * function. This function gets the hbalock and calls
6196 * __lpfc_sli_issue_iocb function and will return the error returned
6197 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6198 * functions which do not hold hbalock.
6199 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006200int
James Smart3772a992009-05-22 14:50:54 -04006201lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006202 struct lpfc_iocbq *piocb, uint32_t flag)
6203{
6204 unsigned long iflags;
6205 int rc;
6206
6207 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006208 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006209 spin_unlock_irqrestore(&phba->hbalock, iflags);
6210
6211 return rc;
6212}
6213
James Smarte59058c2008-08-24 21:49:00 -04006214/**
James Smart3621a712009-04-06 18:47:14 -04006215 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006216 * @phba: Pointer to HBA context object.
6217 *
6218 * This function is called while driver attaches with the
6219 * HBA to setup the extra ring. The extra ring is used
6220 * only when driver needs to support target mode functionality
6221 * or IP over FC functionalities.
6222 *
6223 * This function is called with no lock held.
6224 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006225static int
6226lpfc_extra_ring_setup( struct lpfc_hba *phba)
6227{
6228 struct lpfc_sli *psli;
6229 struct lpfc_sli_ring *pring;
6230
6231 psli = &phba->sli;
6232
6233 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006234
6235 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006236 pring = &psli->ring[psli->fcp_ring];
6237 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6238 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6239 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6240 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6241
James Smarta4bc3372006-12-02 13:34:16 -05006242 /* and give them to the extra ring */
6243 pring = &psli->ring[psli->extra_ring];
6244
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006245 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6246 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6247 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6248 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6249
6250 /* Setup default profile for this ring */
6251 pring->iotag_max = 4096;
6252 pring->num_mask = 1;
6253 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006254 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6255 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006256 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6257 return 0;
6258}
6259
James Smarte59058c2008-08-24 21:49:00 -04006260/**
James Smart3621a712009-04-06 18:47:14 -04006261 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006262 * @phba: Pointer to HBA context object.
6263 * @pring: Pointer to driver SLI ring object.
6264 * @iocbq: Pointer to iocb object.
6265 *
6266 * This function is called by the slow ring event handler
6267 * function when there is an ASYNC event iocb in the ring.
6268 * This function is called with no lock held.
6269 * Currently this function handles only temperature related
6270 * ASYNC events. The function decodes the temperature sensor
6271 * event message and posts events for the management applications.
6272 **/
James Smart98c9ea52007-10-27 13:37:33 -04006273static void
James Smart57127f12007-10-27 13:37:05 -04006274lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6275 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6276{
6277 IOCB_t *icmd;
6278 uint16_t evt_code;
6279 uint16_t temp;
6280 struct temp_event temp_event_data;
6281 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006282 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006283
6284 icmd = &iocbq->iocb;
6285 evt_code = icmd->un.asyncstat.evt_code;
6286 temp = icmd->ulpContext;
6287
6288 if ((evt_code != ASYNC_TEMP_WARN) &&
6289 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006290 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006291 lpfc_printf_log(phba,
6292 KERN_ERR,
6293 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006294 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006295 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006296 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6297 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6298 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6299 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006300 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006301 icmd->un.asyncstat.evt_code,
6302 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6303 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6304 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6305 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6306
James Smart57127f12007-10-27 13:37:05 -04006307 return;
6308 }
6309 temp_event_data.data = (uint32_t)temp;
6310 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6311 if (evt_code == ASYNC_TEMP_WARN) {
6312 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6313 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006314 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006315 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006316 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006317 "corrective action. temperature : %d Celsius\n",
6318 temp);
6319 }
6320 if (evt_code == ASYNC_TEMP_SAFE) {
6321 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6322 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006323 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006324 LOG_TEMP,
6325 "0340 Adapter temperature is OK now. "
6326 "temperature : %d Celsius\n",
6327 temp);
6328 }
6329
6330 /* Send temperature change event to applications */
6331 shost = lpfc_shost_from_vport(phba->pport);
6332 fc_host_post_vendor_event(shost, fc_get_event_number(),
6333 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006334 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006335
6336}
6337
6338
James Smarte59058c2008-08-24 21:49:00 -04006339/**
James Smart3621a712009-04-06 18:47:14 -04006340 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006341 * @phba: Pointer to HBA context object.
6342 *
6343 * lpfc_sli_setup sets up rings of the SLI interface with
6344 * number of iocbs per ring and iotags. This function is
6345 * called while driver attach to the HBA and before the
6346 * interrupts are enabled. So there is no need for locking.
6347 *
6348 * This function always returns 0.
6349 **/
dea31012005-04-17 16:05:31 -05006350int
6351lpfc_sli_setup(struct lpfc_hba *phba)
6352{
James Smarted957682007-06-17 19:56:37 -05006353 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006354 struct lpfc_sli *psli = &phba->sli;
6355 struct lpfc_sli_ring *pring;
6356
6357 psli->num_rings = MAX_CONFIGURED_RINGS;
6358 psli->sli_flag = 0;
6359 psli->fcp_ring = LPFC_FCP_RING;
6360 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006361 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006362
James Bottomley604a3e32005-10-29 10:28:33 -05006363 psli->iocbq_lookup = NULL;
6364 psli->iocbq_lookup_len = 0;
6365 psli->last_iotag = 0;
6366
dea31012005-04-17 16:05:31 -05006367 for (i = 0; i < psli->num_rings; i++) {
6368 pring = &psli->ring[i];
6369 switch (i) {
6370 case LPFC_FCP_RING: /* ring 0 - FCP */
6371 /* numCiocb and numRiocb are used in config_port */
6372 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6373 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6374 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6375 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6376 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6377 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006378 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006379 SLI3_IOCB_CMD_SIZE :
6380 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006381 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006382 SLI3_IOCB_RSP_SIZE :
6383 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006384 pring->iotag_ctr = 0;
6385 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006386 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006387 pring->fast_iotag = pring->iotag_max;
6388 pring->num_mask = 0;
6389 break;
James Smarta4bc3372006-12-02 13:34:16 -05006390 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006391 /* numCiocb and numRiocb are used in config_port */
6392 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6393 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006394 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006395 SLI3_IOCB_CMD_SIZE :
6396 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006397 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006398 SLI3_IOCB_RSP_SIZE :
6399 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006400 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006401 pring->num_mask = 0;
6402 break;
6403 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6404 /* numCiocb and numRiocb are used in config_port */
6405 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6406 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006407 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006408 SLI3_IOCB_CMD_SIZE :
6409 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006410 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006411 SLI3_IOCB_RSP_SIZE :
6412 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006413 pring->fast_iotag = 0;
6414 pring->iotag_ctr = 0;
6415 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006416 pring->lpfc_sli_rcv_async_status =
6417 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006418 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006419 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006420 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6421 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006422 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006423 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006424 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006425 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6426 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006427 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006428 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006429 pring->prt[2].profile = 0; /* Mask 2 */
6430 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006431 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006432 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006433 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006434 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006435 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006436 pring->prt[3].profile = 0; /* Mask 3 */
6437 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006438 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006439 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006440 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006441 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006442 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006443 /* abort unsolicited sequence */
6444 pring->prt[4].profile = 0; /* Mask 4 */
6445 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6446 pring->prt[4].type = FC_TYPE_BLS;
6447 pring->prt[4].lpfc_sli_rcv_unsol_event =
6448 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006449 break;
6450 }
James Smarted957682007-06-17 19:56:37 -05006451 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006452 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006453 }
James Smarted957682007-06-17 19:56:37 -05006454 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006455 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006456 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6457 "SLI2 SLIM Data: x%x x%lx\n",
6458 phba->brd_no, totiocbsize,
6459 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006460 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006461 if (phba->cfg_multi_ring_support == 2)
6462 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006463
6464 return 0;
6465}
6466
James Smarte59058c2008-08-24 21:49:00 -04006467/**
James Smart3621a712009-04-06 18:47:14 -04006468 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006469 * @phba: Pointer to HBA context object.
6470 *
6471 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6472 * ring. This function also initializes ring indices of each ring.
6473 * This function is called during the initialization of the SLI
6474 * interface of an HBA.
6475 * This function is called with no lock held and always returns
6476 * 1.
6477 **/
dea31012005-04-17 16:05:31 -05006478int
James Smart2e0fef82007-06-17 19:56:36 -05006479lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006480{
6481 struct lpfc_sli *psli;
6482 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006483 int i;
dea31012005-04-17 16:05:31 -05006484
6485 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006486 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006487 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006488 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006489 /* Initialize list headers for txq and txcmplq as double linked lists */
6490 for (i = 0; i < psli->num_rings; i++) {
6491 pring = &psli->ring[i];
6492 pring->ringno = i;
6493 pring->next_cmdidx = 0;
6494 pring->local_getidx = 0;
6495 pring->cmdidx = 0;
6496 INIT_LIST_HEAD(&pring->txq);
6497 INIT_LIST_HEAD(&pring->txcmplq);
6498 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006499 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006500 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006501 }
James Smart2e0fef82007-06-17 19:56:36 -05006502 spin_unlock_irq(&phba->hbalock);
6503 return 1;
dea31012005-04-17 16:05:31 -05006504}
6505
James Smarte59058c2008-08-24 21:49:00 -04006506/**
James Smart04c68492009-05-22 14:52:52 -04006507 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6508 * @phba: Pointer to HBA context object.
6509 *
6510 * This routine flushes the mailbox command subsystem. It will unconditionally
6511 * flush all the mailbox commands in the three possible stages in the mailbox
6512 * command sub-system: pending mailbox command queue; the outstanding mailbox
6513 * command; and completed mailbox command queue. It is caller's responsibility
6514 * to make sure that the driver is in the proper state to flush the mailbox
6515 * command sub-system. Namely, the posting of mailbox commands into the
6516 * pending mailbox command queue from the various clients must be stopped;
6517 * either the HBA is in a state that it will never works on the outstanding
6518 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6519 * mailbox command has been completed.
6520 **/
6521static void
6522lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6523{
6524 LIST_HEAD(completions);
6525 struct lpfc_sli *psli = &phba->sli;
6526 LPFC_MBOXQ_t *pmb;
6527 unsigned long iflag;
6528
6529 /* Flush all the mailbox commands in the mbox system */
6530 spin_lock_irqsave(&phba->hbalock, iflag);
6531 /* The pending mailbox command queue */
6532 list_splice_init(&phba->sli.mboxq, &completions);
6533 /* The outstanding active mailbox command */
6534 if (psli->mbox_active) {
6535 list_add_tail(&psli->mbox_active->list, &completions);
6536 psli->mbox_active = NULL;
6537 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6538 }
6539 /* The completed mailbox command queue */
6540 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6541 spin_unlock_irqrestore(&phba->hbalock, iflag);
6542
6543 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6544 while (!list_empty(&completions)) {
6545 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6546 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6547 if (pmb->mbox_cmpl)
6548 pmb->mbox_cmpl(phba, pmb);
6549 }
6550}
6551
6552/**
James Smart3621a712009-04-06 18:47:14 -04006553 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006554 * @vport: Pointer to virtual port object.
6555 *
6556 * lpfc_sli_host_down is called to clean up the resources
6557 * associated with a vport before destroying virtual
6558 * port data structures.
6559 * This function does following operations:
6560 * - Free discovery resources associated with this virtual
6561 * port.
6562 * - Free iocbs associated with this virtual port in
6563 * the txq.
6564 * - Send abort for all iocb commands associated with this
6565 * vport in txcmplq.
6566 *
6567 * This function is called with no lock held and always returns 1.
6568 **/
dea31012005-04-17 16:05:31 -05006569int
James Smart92d7f7b2007-06-17 19:56:38 -05006570lpfc_sli_host_down(struct lpfc_vport *vport)
6571{
James Smart858c9f62007-06-17 19:56:39 -05006572 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006573 struct lpfc_hba *phba = vport->phba;
6574 struct lpfc_sli *psli = &phba->sli;
6575 struct lpfc_sli_ring *pring;
6576 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006577 int i;
6578 unsigned long flags = 0;
6579 uint16_t prev_pring_flag;
6580
6581 lpfc_cleanup_discovery_resources(vport);
6582
6583 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006584 for (i = 0; i < psli->num_rings; i++) {
6585 pring = &psli->ring[i];
6586 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006587 /* Only slow rings */
6588 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006589 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006590 /* Set the lpfc data pending flag */
6591 set_bit(LPFC_DATA_READY, &phba->data_flags);
6592 }
James Smart92d7f7b2007-06-17 19:56:38 -05006593 /*
6594 * Error everything on the txq since these iocbs have not been
6595 * given to the FW yet.
6596 */
James Smart92d7f7b2007-06-17 19:56:38 -05006597 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6598 if (iocb->vport != vport)
6599 continue;
James Smart858c9f62007-06-17 19:56:39 -05006600 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006601 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006602 }
6603
6604 /* Next issue ABTS for everything on the txcmplq */
6605 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6606 list) {
6607 if (iocb->vport != vport)
6608 continue;
6609 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6610 }
6611
6612 pring->flag = prev_pring_flag;
6613 }
6614
6615 spin_unlock_irqrestore(&phba->hbalock, flags);
6616
James Smarta257bf92009-04-06 18:48:10 -04006617 /* Cancel all the IOCBs from the completions list */
6618 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6619 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006620 return 1;
6621}
6622
James Smarte59058c2008-08-24 21:49:00 -04006623/**
James Smart3621a712009-04-06 18:47:14 -04006624 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006625 * @phba: Pointer to HBA context object.
6626 *
6627 * This function cleans up all iocb, buffers, mailbox commands
6628 * while shutting down the HBA. This function is called with no
6629 * lock held and always returns 1.
6630 * This function does the following to cleanup driver resources:
6631 * - Free discovery resources for each virtual port
6632 * - Cleanup any pending fabric iocbs
6633 * - Iterate through the iocb txq and free each entry
6634 * in the list.
6635 * - Free up any buffer posted to the HBA
6636 * - Free mailbox commands in the mailbox queue.
6637 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006638int
James Smart2e0fef82007-06-17 19:56:36 -05006639lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006640{
James Smart2534ba72007-04-25 09:52:20 -04006641 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006642 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006643 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006644 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006645 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006646 int i;
6647
6648 /* Shutdown the mailbox command sub-system */
6649 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006650
dea31012005-04-17 16:05:31 -05006651 lpfc_hba_down_prep(phba);
6652
James Smart92d7f7b2007-06-17 19:56:38 -05006653 lpfc_fabric_abort_hba(phba);
6654
James Smart2e0fef82007-06-17 19:56:36 -05006655 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006656 for (i = 0; i < psli->num_rings; i++) {
6657 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006658 /* Only slow rings */
6659 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006660 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006661 /* Set the lpfc data pending flag */
6662 set_bit(LPFC_DATA_READY, &phba->data_flags);
6663 }
dea31012005-04-17 16:05:31 -05006664
6665 /*
6666 * Error everything on the txq since these iocbs have not been
6667 * given to the FW yet.
6668 */
James Smart2534ba72007-04-25 09:52:20 -04006669 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006670 pring->txq_cnt = 0;
6671
dea31012005-04-17 16:05:31 -05006672 }
James Smart2e0fef82007-06-17 19:56:36 -05006673 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006674
James Smarta257bf92009-04-06 18:48:10 -04006675 /* Cancel all the IOCBs from the completions list */
6676 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6677 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006678
James Smart0ff10d42008-01-11 01:52:36 -05006679 spin_lock_irqsave(&phba->hbalock, flags);
6680 list_splice_init(&phba->elsbuf, &completions);
6681 phba->elsbuf_cnt = 0;
6682 phba->elsbuf_prev_cnt = 0;
6683 spin_unlock_irqrestore(&phba->hbalock, flags);
6684
6685 while (!list_empty(&completions)) {
6686 list_remove_head(&completions, buf_ptr,
6687 struct lpfc_dmabuf, list);
6688 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6689 kfree(buf_ptr);
6690 }
6691
dea31012005-04-17 16:05:31 -05006692 /* Return any active mbox cmds */
6693 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006694
James Smartda0436e2009-05-22 14:51:39 -04006695 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006696 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006697 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006698
James Smartda0436e2009-05-22 14:51:39 -04006699 return 1;
6700}
James Smart92d7f7b2007-06-17 19:56:38 -05006701
James Smartda0436e2009-05-22 14:51:39 -04006702/**
6703 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6704 * @phba: Pointer to HBA context object.
6705 *
6706 * This function cleans up all queues, iocb, buffers, mailbox commands while
6707 * shutting down the SLI4 HBA FCoE function. This function is called with no
6708 * lock held and always returns 1.
6709 *
6710 * This function does the following to cleanup driver FCoE function resources:
6711 * - Free discovery resources for each virtual port
6712 * - Cleanup any pending fabric iocbs
6713 * - Iterate through the iocb txq and free each entry in the list.
6714 * - Free up any buffer posted to the HBA.
6715 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6716 * - Free mailbox commands in the mailbox queue.
6717 **/
6718int
6719lpfc_sli4_hba_down(struct lpfc_hba *phba)
6720{
6721 /* Stop the SLI4 device port */
6722 lpfc_stop_port(phba);
6723
6724 /* Tear down the queues in the HBA */
6725 lpfc_sli4_queue_unset(phba);
6726
6727 /* unregister default FCFI from the HBA */
6728 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6729
dea31012005-04-17 16:05:31 -05006730 return 1;
6731}
6732
James Smarte59058c2008-08-24 21:49:00 -04006733/**
James Smart3621a712009-04-06 18:47:14 -04006734 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006735 * @srcp: Source memory pointer.
6736 * @destp: Destination memory pointer.
6737 * @cnt: Number of words required to be copied.
6738 *
6739 * This function is used for copying data between driver memory
6740 * and the SLI memory. This function also changes the endianness
6741 * of each word if native endianness is different from SLI
6742 * endianness. This function can be called with or without
6743 * lock.
6744 **/
dea31012005-04-17 16:05:31 -05006745void
6746lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6747{
6748 uint32_t *src = srcp;
6749 uint32_t *dest = destp;
6750 uint32_t ldata;
6751 int i;
6752
6753 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6754 ldata = *src;
6755 ldata = le32_to_cpu(ldata);
6756 *dest = ldata;
6757 src++;
6758 dest++;
6759 }
6760}
6761
James Smarte59058c2008-08-24 21:49:00 -04006762
6763/**
James Smarta0c87cb2009-07-19 10:01:10 -04006764 * lpfc_sli_bemem_bcopy - SLI memory copy function
6765 * @srcp: Source memory pointer.
6766 * @destp: Destination memory pointer.
6767 * @cnt: Number of words required to be copied.
6768 *
6769 * This function is used for copying data between a data structure
6770 * with big endian representation to local endianness.
6771 * This function can be called with or without lock.
6772 **/
6773void
6774lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6775{
6776 uint32_t *src = srcp;
6777 uint32_t *dest = destp;
6778 uint32_t ldata;
6779 int i;
6780
6781 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6782 ldata = *src;
6783 ldata = be32_to_cpu(ldata);
6784 *dest = ldata;
6785 src++;
6786 dest++;
6787 }
6788}
6789
6790/**
James Smart3621a712009-04-06 18:47:14 -04006791 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006792 * @phba: Pointer to HBA context object.
6793 * @pring: Pointer to driver SLI ring object.
6794 * @mp: Pointer to driver buffer object.
6795 *
6796 * This function is called with no lock held.
6797 * It always return zero after adding the buffer to the postbufq
6798 * buffer list.
6799 **/
dea31012005-04-17 16:05:31 -05006800int
James Smart2e0fef82007-06-17 19:56:36 -05006801lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6802 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006803{
6804 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6805 later */
James Smart2e0fef82007-06-17 19:56:36 -05006806 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006807 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006808 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006809 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006810 return 0;
6811}
6812
James Smarte59058c2008-08-24 21:49:00 -04006813/**
James Smart3621a712009-04-06 18:47:14 -04006814 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006815 * @phba: Pointer to HBA context object.
6816 *
6817 * When HBQ is enabled, buffers are searched based on tags. This function
6818 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6819 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6820 * does not conflict with tags of buffer posted for unsolicited events.
6821 * The function returns the allocated tag. The function is called with
6822 * no locks held.
6823 **/
James Smart76bb24e2007-10-27 13:38:00 -04006824uint32_t
6825lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6826{
6827 spin_lock_irq(&phba->hbalock);
6828 phba->buffer_tag_count++;
6829 /*
6830 * Always set the QUE_BUFTAG_BIT to distiguish between
6831 * a tag assigned by HBQ.
6832 */
6833 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6834 spin_unlock_irq(&phba->hbalock);
6835 return phba->buffer_tag_count;
6836}
6837
James Smarte59058c2008-08-24 21:49:00 -04006838/**
James Smart3621a712009-04-06 18:47:14 -04006839 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006840 * @phba: Pointer to HBA context object.
6841 * @pring: Pointer to driver SLI ring object.
6842 * @tag: Buffer tag.
6843 *
6844 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6845 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6846 * iocb is posted to the response ring with the tag of the buffer.
6847 * This function searches the pring->postbufq list using the tag
6848 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6849 * iocb. If the buffer is found then lpfc_dmabuf object of the
6850 * buffer is returned to the caller else NULL is returned.
6851 * This function is called with no lock held.
6852 **/
James Smart76bb24e2007-10-27 13:38:00 -04006853struct lpfc_dmabuf *
6854lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6855 uint32_t tag)
6856{
6857 struct lpfc_dmabuf *mp, *next_mp;
6858 struct list_head *slp = &pring->postbufq;
6859
6860 /* Search postbufq, from the begining, looking for a match on tag */
6861 spin_lock_irq(&phba->hbalock);
6862 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6863 if (mp->buffer_tag == tag) {
6864 list_del_init(&mp->list);
6865 pring->postbufq_cnt--;
6866 spin_unlock_irq(&phba->hbalock);
6867 return mp;
6868 }
6869 }
6870
6871 spin_unlock_irq(&phba->hbalock);
6872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006873 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006874 "ring %d Data x%lx x%p x%p x%x\n",
6875 pring->ringno, (unsigned long) tag,
6876 slp->next, slp->prev, pring->postbufq_cnt);
6877
6878 return NULL;
6879}
dea31012005-04-17 16:05:31 -05006880
James Smarte59058c2008-08-24 21:49:00 -04006881/**
James Smart3621a712009-04-06 18:47:14 -04006882 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006883 * @phba: Pointer to HBA context object.
6884 * @pring: Pointer to driver SLI ring object.
6885 * @phys: DMA address of the buffer.
6886 *
6887 * This function searches the buffer list using the dma_address
6888 * of unsolicited event to find the driver's lpfc_dmabuf object
6889 * corresponding to the dma_address. The function returns the
6890 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6891 * This function is called by the ct and els unsolicited event
6892 * handlers to get the buffer associated with the unsolicited
6893 * event.
6894 *
6895 * This function is called with no lock held.
6896 **/
dea31012005-04-17 16:05:31 -05006897struct lpfc_dmabuf *
6898lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6899 dma_addr_t phys)
6900{
6901 struct lpfc_dmabuf *mp, *next_mp;
6902 struct list_head *slp = &pring->postbufq;
6903
6904 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006905 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006906 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6907 if (mp->phys == phys) {
6908 list_del_init(&mp->list);
6909 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006910 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006911 return mp;
6912 }
6913 }
6914
James Smart2e0fef82007-06-17 19:56:36 -05006915 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006917 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006918 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006919 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006920 slp->next, slp->prev, pring->postbufq_cnt);
6921 return NULL;
6922}
6923
James Smarte59058c2008-08-24 21:49:00 -04006924/**
James Smart3621a712009-04-06 18:47:14 -04006925 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006926 * @phba: Pointer to HBA context object.
6927 * @cmdiocb: Pointer to driver command iocb object.
6928 * @rspiocb: Pointer to driver response iocb object.
6929 *
6930 * This function is the completion handler for the abort iocbs for
6931 * ELS commands. This function is called from the ELS ring event
6932 * handler with no lock held. This function frees memory resources
6933 * associated with the abort iocb.
6934 **/
dea31012005-04-17 16:05:31 -05006935static void
James Smart2e0fef82007-06-17 19:56:36 -05006936lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6937 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05006938{
James Smart2e0fef82007-06-17 19:56:36 -05006939 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04006940 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05006941 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04006942 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6943
6944 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04006945
6946 if (irsp->ulpStatus) {
6947 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6948 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6949
James Smart2e0fef82007-06-17 19:56:36 -05006950 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04006951 if (phba->sli_rev < LPFC_SLI_REV4) {
6952 if (abort_iotag != 0 &&
6953 abort_iotag <= phba->sli.last_iotag)
6954 abort_iocb =
6955 phba->sli.iocbq_lookup[abort_iotag];
6956 } else
6957 /* For sli4 the abort_tag is the XRI,
6958 * so the abort routine puts the iotag of the iocb
6959 * being aborted in the context field of the abort
6960 * IOCB.
6961 */
6962 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04006963
James Smart92d7f7b2007-06-17 19:56:38 -05006964 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006965 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05006966 "with tag %x context %x, abort status %x, "
6967 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04006968 abort_iocb, abort_iotag, abort_context,
6969 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04006970
6971 /*
James Smart58da1ff2008-04-07 10:15:56 -04006972 * If the iocb is not found in Firmware queue the iocb
6973 * might have completed already. Do not free it again.
6974 */
James Smart9b379602008-04-07 10:16:00 -04006975 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04006976 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
6977 spin_unlock_irq(&phba->hbalock);
6978 lpfc_sli_release_iocbq(phba, cmdiocb);
6979 return;
6980 }
6981 /* For SLI4 the ulpContext field for abort IOCB
6982 * holds the iotag of the IOCB being aborted so
6983 * the local abort_context needs to be reset to
6984 * match the aborted IOCBs ulpContext.
6985 */
6986 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
6987 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04006988 }
6989 /*
James Smart2680eea2007-04-25 09:52:55 -04006990 * make sure we have the right iocbq before taking it
6991 * off the txcmplq and try to call completion routine.
6992 */
James Smart2e0fef82007-06-17 19:56:36 -05006993 if (!abort_iocb ||
6994 abort_iocb->iocb.ulpContext != abort_context ||
6995 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
6996 spin_unlock_irq(&phba->hbalock);
6997 else {
James Smart92d7f7b2007-06-17 19:56:38 -05006998 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04006999 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007000 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007001
James Smart0ff10d42008-01-11 01:52:36 -05007002 /* Firmware could still be in progress of DMAing
7003 * payload, so don't free data buffer till after
7004 * a hbeat.
7005 */
7006 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7007
James Smart92d7f7b2007-06-17 19:56:38 -05007008 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7009 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7010 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7011 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007012 }
7013 }
7014
James Bottomley604a3e32005-10-29 10:28:33 -05007015 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007016 return;
7017}
7018
James Smarte59058c2008-08-24 21:49:00 -04007019/**
James Smart3621a712009-04-06 18:47:14 -04007020 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007021 * @phba: Pointer to HBA context object.
7022 * @cmdiocb: Pointer to driver command iocb object.
7023 * @rspiocb: Pointer to driver response iocb object.
7024 *
7025 * The function is called from SLI ring event handler with no
7026 * lock held. This function is the completion handler for ELS commands
7027 * which are aborted. The function frees memory resources used for
7028 * the aborted ELS commands.
7029 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007030static void
7031lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7032 struct lpfc_iocbq *rspiocb)
7033{
7034 IOCB_t *irsp = &rspiocb->iocb;
7035
7036 /* ELS cmd tag <ulpIoTag> completes */
7037 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007038 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007039 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007040 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007041 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007042 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7043 lpfc_ct_free_iocb(phba, cmdiocb);
7044 else
7045 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007046 return;
7047}
7048
James Smarte59058c2008-08-24 21:49:00 -04007049/**
James Smart3621a712009-04-06 18:47:14 -04007050 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007051 * @phba: Pointer to HBA context object.
7052 * @pring: Pointer to driver SLI ring object.
7053 * @cmdiocb: Pointer to driver command iocb object.
7054 *
7055 * This function issues an abort iocb for the provided command
7056 * iocb. This function is called with hbalock held.
7057 * The function returns 0 when it fails due to memory allocation
7058 * failure or when the command iocb is an abort request.
7059 **/
dea31012005-04-17 16:05:31 -05007060int
James Smart2e0fef82007-06-17 19:56:36 -05007061lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7062 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007063{
James Smart2e0fef82007-06-17 19:56:36 -05007064 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007065 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007066 IOCB_t *icmd = NULL;
7067 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007068 int retval = IOCB_ERROR;
7069
James Smart92d7f7b2007-06-17 19:56:38 -05007070 /*
7071 * There are certain command types we don't want to abort. And we
7072 * don't want to abort commands that are already in the process of
7073 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007074 */
7075 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007076 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007077 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7078 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007079 return 0;
7080
James Smart858c9f62007-06-17 19:56:39 -05007081 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7082 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007083 */
James Smart858c9f62007-06-17 19:56:39 -05007084 if ((vport->load_flag & FC_UNLOADING) &&
7085 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007086 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7087 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7088 else
7089 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007090 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007091 }
dea31012005-04-17 16:05:31 -05007092
7093 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007094 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007095 if (abtsiocbp == NULL)
7096 return 0;
dea31012005-04-17 16:05:31 -05007097
James Smart07951072007-04-25 09:51:38 -04007098 /* This signals the response to set the correct status
7099 * before calling the completion handler.
7100 */
7101 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7102
dea31012005-04-17 16:05:31 -05007103 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007104 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7105 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007106 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007107 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007108 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7109 }
James Smartda0436e2009-05-22 14:51:39 -04007110 else
7111 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007112 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007113 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007114
James Smart5ffc2662009-11-18 15:39:44 -05007115 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7116 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7117
James Smart2e0fef82007-06-17 19:56:36 -05007118 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007119 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7120 else
7121 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7122
7123 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007124
James Smarte8b62012007-08-02 11:10:09 -04007125 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7126 "0339 Abort xri x%x, original iotag x%x, "
7127 "abort cmd iotag x%x\n",
7128 iabt->un.acxri.abortContextTag,
7129 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007130 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007131
James Smartd7c255b2008-08-24 21:50:00 -04007132 if (retval)
7133 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007134abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007135 /*
7136 * Caller to this routine should check for IOCB_ERROR
7137 * and handle it properly. This routine no longer removes
7138 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007139 */
James Smart2e0fef82007-06-17 19:56:36 -05007140 return retval;
dea31012005-04-17 16:05:31 -05007141}
7142
James Smarte59058c2008-08-24 21:49:00 -04007143/**
James Smart3621a712009-04-06 18:47:14 -04007144 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007145 * @iocbq: Pointer to driver iocb object.
7146 * @vport: Pointer to driver virtual port object.
7147 * @tgt_id: SCSI ID of the target.
7148 * @lun_id: LUN ID of the scsi device.
7149 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7150 *
James Smart3621a712009-04-06 18:47:14 -04007151 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007152 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7153 * 0 if the filtering criteria is met for the given iocb and will return
7154 * 1 if the filtering criteria is not met.
7155 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7156 * given iocb is for the SCSI device specified by vport, tgt_id and
7157 * lun_id parameter.
7158 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7159 * given iocb is for the SCSI target specified by vport and tgt_id
7160 * parameters.
7161 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7162 * given iocb is for the SCSI host associated with the given vport.
7163 * This function is called with no locks held.
7164 **/
dea31012005-04-17 16:05:31 -05007165static int
James Smart51ef4c22007-08-02 11:10:31 -04007166lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7167 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007168 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007169{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007170 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007171 int rc = 1;
7172
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007173 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7174 return rc;
7175
James Smart51ef4c22007-08-02 11:10:31 -04007176 if (iocbq->vport != vport)
7177 return rc;
7178
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007179 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007180
James Smart495a7142008-06-14 22:52:59 -04007181 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007182 return rc;
7183
7184 switch (ctx_cmd) {
7185 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007186 if ((lpfc_cmd->rdata->pnode) &&
7187 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7188 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007189 rc = 0;
7190 break;
7191 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007192 if ((lpfc_cmd->rdata->pnode) &&
7193 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007194 rc = 0;
7195 break;
dea31012005-04-17 16:05:31 -05007196 case LPFC_CTX_HOST:
7197 rc = 0;
7198 break;
7199 default:
7200 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007201 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007202 break;
7203 }
7204
7205 return rc;
7206}
7207
James Smarte59058c2008-08-24 21:49:00 -04007208/**
James Smart3621a712009-04-06 18:47:14 -04007209 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007210 * @vport: Pointer to virtual port.
7211 * @tgt_id: SCSI ID of the target.
7212 * @lun_id: LUN ID of the scsi device.
7213 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7214 *
7215 * This function returns number of FCP commands pending for the vport.
7216 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7217 * commands pending on the vport associated with SCSI device specified
7218 * by tgt_id and lun_id parameters.
7219 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7220 * commands pending on the vport associated with SCSI target specified
7221 * by tgt_id parameter.
7222 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7223 * commands pending on the vport.
7224 * This function returns the number of iocbs which satisfy the filter.
7225 * This function is called without any lock held.
7226 **/
dea31012005-04-17 16:05:31 -05007227int
James Smart51ef4c22007-08-02 11:10:31 -04007228lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7229 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007230{
James Smart51ef4c22007-08-02 11:10:31 -04007231 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007232 struct lpfc_iocbq *iocbq;
7233 int sum, i;
dea31012005-04-17 16:05:31 -05007234
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007235 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7236 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007237
James Smart51ef4c22007-08-02 11:10:31 -04007238 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7239 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007240 sum++;
dea31012005-04-17 16:05:31 -05007241 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007242
dea31012005-04-17 16:05:31 -05007243 return sum;
7244}
7245
James Smarte59058c2008-08-24 21:49:00 -04007246/**
James Smart3621a712009-04-06 18:47:14 -04007247 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007248 * @phba: Pointer to HBA context object
7249 * @cmdiocb: Pointer to command iocb object.
7250 * @rspiocb: Pointer to response iocb object.
7251 *
7252 * This function is called when an aborted FCP iocb completes. This
7253 * function is called by the ring event handler with no lock held.
7254 * This function frees the iocb.
7255 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007256void
James Smart2e0fef82007-06-17 19:56:36 -05007257lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7258 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007259{
James Bottomley604a3e32005-10-29 10:28:33 -05007260 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007261 return;
7262}
7263
James Smarte59058c2008-08-24 21:49:00 -04007264/**
James Smart3621a712009-04-06 18:47:14 -04007265 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007266 * @vport: Pointer to virtual port.
7267 * @pring: Pointer to driver SLI ring object.
7268 * @tgt_id: SCSI ID of the target.
7269 * @lun_id: LUN ID of the scsi device.
7270 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7271 *
7272 * This function sends an abort command for every SCSI command
7273 * associated with the given virtual port pending on the ring
7274 * filtered by lpfc_sli_validate_fcp_iocb function.
7275 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7276 * FCP iocbs associated with lun specified by tgt_id and lun_id
7277 * parameters
7278 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7279 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7280 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7281 * FCP iocbs associated with virtual port.
7282 * This function returns number of iocbs it failed to abort.
7283 * This function is called with no locks held.
7284 **/
dea31012005-04-17 16:05:31 -05007285int
James Smart51ef4c22007-08-02 11:10:31 -04007286lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7287 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_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 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007292 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007293 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007294 int i;
dea31012005-04-17 16:05:31 -05007295
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007296 for (i = 1; i <= phba->sli.last_iotag; i++) {
7297 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007298
James Smart51ef4c22007-08-02 11:10:31 -04007299 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007300 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007301 continue;
7302
7303 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007304 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007305 if (abtsiocb == NULL) {
7306 errcnt++;
7307 continue;
7308 }
dea31012005-04-17 16:05:31 -05007309
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007310 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007311 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7312 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007313 if (phba->sli_rev == LPFC_SLI_REV4)
7314 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7315 else
7316 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007317 abtsiocb->iocb.ulpLe = 1;
7318 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007319 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007320
James Smart5ffc2662009-11-18 15:39:44 -05007321 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7322 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7323
James Smart2e0fef82007-06-17 19:56:36 -05007324 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007325 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7326 else
7327 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7328
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007329 /* Setup callback routine and issue the command. */
7330 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007331 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7332 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007333 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007334 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007335 errcnt++;
7336 continue;
7337 }
7338 }
7339
7340 return errcnt;
7341}
7342
James Smarte59058c2008-08-24 21:49:00 -04007343/**
James Smart3621a712009-04-06 18:47:14 -04007344 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007345 * @phba: Pointer to HBA context object.
7346 * @cmdiocbq: Pointer to command iocb.
7347 * @rspiocbq: Pointer to response iocb.
7348 *
7349 * This function is the completion handler for iocbs issued using
7350 * lpfc_sli_issue_iocb_wait function. This function is called by the
7351 * ring event handler function without any lock held. This function
7352 * can be called from both worker thread context and interrupt
7353 * context. This function also can be called from other thread which
7354 * cleans up the SLI layer objects.
7355 * This function copy the contents of the response iocb to the
7356 * response iocb memory object provided by the caller of
7357 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7358 * sleeps for the iocb completion.
7359 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007360static void
7361lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7362 struct lpfc_iocbq *cmdiocbq,
7363 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007364{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007365 wait_queue_head_t *pdone_q;
7366 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007367
James Smart2e0fef82007-06-17 19:56:36 -05007368 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007369 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7370 if (cmdiocbq->context2 && rspiocbq)
7371 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7372 &rspiocbq->iocb, sizeof(IOCB_t));
7373
7374 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007375 if (pdone_q)
7376 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007377 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007378 return;
7379}
7380
James Smarte59058c2008-08-24 21:49:00 -04007381/**
James Smartd11e31d2009-06-10 17:23:06 -04007382 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7383 * @phba: Pointer to HBA context object..
7384 * @piocbq: Pointer to command iocb.
7385 * @flag: Flag to test.
7386 *
7387 * This routine grabs the hbalock and then test the iocb_flag to
7388 * see if the passed in flag is set.
7389 * Returns:
7390 * 1 if flag is set.
7391 * 0 if flag is not set.
7392 **/
7393static int
7394lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7395 struct lpfc_iocbq *piocbq, uint32_t flag)
7396{
7397 unsigned long iflags;
7398 int ret;
7399
7400 spin_lock_irqsave(&phba->hbalock, iflags);
7401 ret = piocbq->iocb_flag & flag;
7402 spin_unlock_irqrestore(&phba->hbalock, iflags);
7403 return ret;
7404
7405}
7406
7407/**
James Smart3621a712009-04-06 18:47:14 -04007408 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007409 * @phba: Pointer to HBA context object..
7410 * @pring: Pointer to sli ring.
7411 * @piocb: Pointer to command iocb.
7412 * @prspiocbq: Pointer to response iocb.
7413 * @timeout: Timeout in number of seconds.
7414 *
7415 * This function issues the iocb to firmware and waits for the
7416 * iocb to complete. If the iocb command is not
7417 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7418 * Caller should not free the iocb resources if this function
7419 * returns IOCB_TIMEDOUT.
7420 * The function waits for the iocb completion using an
7421 * non-interruptible wait.
7422 * This function will sleep while waiting for iocb completion.
7423 * So, this function should not be called from any context which
7424 * does not allow sleeping. Due to the same reason, this function
7425 * cannot be called with interrupt disabled.
7426 * This function assumes that the iocb completions occur while
7427 * this function sleep. So, this function cannot be called from
7428 * the thread which process iocb completion for this ring.
7429 * This function clears the iocb_flag of the iocb object before
7430 * issuing the iocb and the iocb completion handler sets this
7431 * flag and wakes this thread when the iocb completes.
7432 * The contents of the response iocb will be copied to prspiocbq
7433 * by the completion handler when the command completes.
7434 * This function returns IOCB_SUCCESS when success.
7435 * This function is called with no lock held.
7436 **/
dea31012005-04-17 16:05:31 -05007437int
James Smart2e0fef82007-06-17 19:56:36 -05007438lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007439 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007440 struct lpfc_iocbq *piocb,
7441 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007442 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007443{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007444 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007445 long timeleft, timeout_req = 0;
7446 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007447 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007448
7449 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007450 * If the caller has provided a response iocbq buffer, then context2
7451 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007452 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007453 if (prspiocbq) {
7454 if (piocb->context2)
7455 return IOCB_ERROR;
7456 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007457 }
7458
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007459 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7460 piocb->context_un.wait_queue = &done_q;
7461 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007462
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007463 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7464 creg_val = readl(phba->HCregaddr);
7465 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7466 writel(creg_val, phba->HCregaddr);
7467 readl(phba->HCregaddr); /* flush */
7468 }
7469
James Smartda0436e2009-05-22 14:51:39 -04007470 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007471 if (retval == IOCB_SUCCESS) {
7472 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007473 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007474 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007475 timeout_req);
dea31012005-04-17 16:05:31 -05007476
James Smart7054a602007-04-25 09:52:34 -04007477 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7478 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007479 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007480 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007482 "0338 IOCB wait timeout error - no "
7483 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007484 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007485 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007486 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007487 "0330 IOCB wake NOT set, "
7488 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007489 timeout, (timeleft / jiffies));
7490 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007491 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007492 } else {
7493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007494 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007495 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007496 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007497 }
7498
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007499 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7500 creg_val = readl(phba->HCregaddr);
7501 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7502 writel(creg_val, phba->HCregaddr);
7503 readl(phba->HCregaddr); /* flush */
7504 }
7505
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007506 if (prspiocbq)
7507 piocb->context2 = NULL;
7508
7509 piocb->context_un.wait_queue = NULL;
7510 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007511 return retval;
7512}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007513
James Smarte59058c2008-08-24 21:49:00 -04007514/**
James Smart3621a712009-04-06 18:47:14 -04007515 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007516 * @phba: Pointer to HBA context object.
7517 * @pmboxq: Pointer to driver mailbox object.
7518 * @timeout: Timeout in number of seconds.
7519 *
7520 * This function issues the mailbox to firmware and waits for the
7521 * mailbox command to complete. If the mailbox command is not
7522 * completed within timeout seconds, it returns MBX_TIMEOUT.
7523 * The function waits for the mailbox completion using an
7524 * interruptible wait. If the thread is woken up due to a
7525 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7526 * should not free the mailbox resources, if this function returns
7527 * MBX_TIMEOUT.
7528 * This function will sleep while waiting for mailbox completion.
7529 * So, this function should not be called from any context which
7530 * does not allow sleeping. Due to the same reason, this function
7531 * cannot be called with interrupt disabled.
7532 * This function assumes that the mailbox completion occurs while
7533 * this function sleep. So, this function cannot be called from
7534 * the worker thread which processes mailbox completion.
7535 * This function is called in the context of HBA management
7536 * applications.
7537 * This function returns MBX_SUCCESS when successful.
7538 * This function is called with no lock held.
7539 **/
dea31012005-04-17 16:05:31 -05007540int
James Smart2e0fef82007-06-17 19:56:36 -05007541lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007542 uint32_t timeout)
7543{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007544 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007545 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007546 unsigned long flag;
dea31012005-04-17 16:05:31 -05007547
7548 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007549 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007550 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007551
James Smart495a7142008-06-14 22:52:59 -04007552 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007553 /* setup wake call as IOCB callback */
7554 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7555 /* setup context field to pass wait_queue pointer to wake function */
7556 pmboxq->context1 = &done_q;
7557
dea31012005-04-17 16:05:31 -05007558 /* now issue the command */
7559 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7560
7561 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007562 wait_event_interruptible_timeout(done_q,
7563 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7564 timeout * HZ);
7565
James Smart858c9f62007-06-17 19:56:39 -05007566 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007567 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007568 /*
7569 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7570 * else do not free the resources.
7571 */
7572 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007573 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007574 else {
James Smart7054a602007-04-25 09:52:34 -04007575 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007576 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7577 }
7578 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007579 }
7580
dea31012005-04-17 16:05:31 -05007581 return retval;
7582}
7583
James Smarte59058c2008-08-24 21:49:00 -04007584/**
James Smart3772a992009-05-22 14:50:54 -04007585 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007586 * @phba: Pointer to HBA context.
7587 *
James Smart3772a992009-05-22 14:50:54 -04007588 * This function is called to shutdown the driver's mailbox sub-system.
7589 * It first marks the mailbox sub-system is in a block state to prevent
7590 * the asynchronous mailbox command from issued off the pending mailbox
7591 * command queue. If the mailbox command sub-system shutdown is due to
7592 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7593 * the mailbox sub-system flush routine to forcefully bring down the
7594 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7595 * as with offline or HBA function reset), this routine will wait for the
7596 * outstanding mailbox command to complete before invoking the mailbox
7597 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007598 **/
James Smart3772a992009-05-22 14:50:54 -04007599void
7600lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007601{
James Smart3772a992009-05-22 14:50:54 -04007602 struct lpfc_sli *psli = &phba->sli;
7603 uint8_t actcmd = MBX_HEARTBEAT;
7604 unsigned long timeout;
7605
7606 spin_lock_irq(&phba->hbalock);
7607 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7608 spin_unlock_irq(&phba->hbalock);
7609
7610 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7611 spin_lock_irq(&phba->hbalock);
7612 if (phba->sli.mbox_active)
7613 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7614 spin_unlock_irq(&phba->hbalock);
7615 /* Determine how long we might wait for the active mailbox
7616 * command to be gracefully completed by firmware.
7617 */
7618 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7619 1000) + jiffies;
7620 while (phba->sli.mbox_active) {
7621 /* Check active mailbox complete status every 2ms */
7622 msleep(2);
7623 if (time_after(jiffies, timeout))
7624 /* Timeout, let the mailbox flush routine to
7625 * forcefully release active mailbox command
7626 */
7627 break;
7628 }
7629 }
7630 lpfc_sli_mbox_sys_flush(phba);
7631}
7632
7633/**
7634 * lpfc_sli_eratt_read - read sli-3 error attention events
7635 * @phba: Pointer to HBA context.
7636 *
7637 * This function is called to read the SLI3 device error attention registers
7638 * for possible error attention events. The caller must hold the hostlock
7639 * with spin_lock_irq().
7640 *
7641 * This fucntion returns 1 when there is Error Attention in the Host Attention
7642 * Register and returns 0 otherwise.
7643 **/
7644static int
7645lpfc_sli_eratt_read(struct lpfc_hba *phba)
7646{
James Smarted957682007-06-17 19:56:37 -05007647 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007648
James Smart3772a992009-05-22 14:50:54 -04007649 /* Read chip Host Attention (HA) register */
7650 ha_copy = readl(phba->HAregaddr);
7651 if (ha_copy & HA_ERATT) {
7652 /* Read host status register to retrieve error event */
7653 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007654
James Smart3772a992009-05-22 14:50:54 -04007655 /* Check if there is a deferred error condition is active */
7656 if ((HS_FFER1 & phba->work_hs) &&
7657 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7658 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007659 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007660 /* Clear all interrupt enable conditions */
7661 writel(0, phba->HCregaddr);
7662 readl(phba->HCregaddr);
7663 }
7664
7665 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007666 phba->work_ha |= HA_ERATT;
7667 /* Indicate polling handles this ERATT */
7668 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007669 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007670 }
James Smart3772a992009-05-22 14:50:54 -04007671 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007672}
7673
James Smarte59058c2008-08-24 21:49:00 -04007674/**
James Smartda0436e2009-05-22 14:51:39 -04007675 * lpfc_sli4_eratt_read - read sli-4 error attention events
7676 * @phba: Pointer to HBA context.
7677 *
7678 * This function is called to read the SLI4 device error attention registers
7679 * for possible error attention events. The caller must hold the hostlock
7680 * with spin_lock_irq().
7681 *
7682 * This fucntion returns 1 when there is Error Attention in the Host Attention
7683 * Register and returns 0 otherwise.
7684 **/
7685static int
7686lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7687{
7688 uint32_t uerr_sta_hi, uerr_sta_lo;
7689 uint32_t onlnreg0, onlnreg1;
7690
7691 /* For now, use the SLI4 device internal unrecoverable error
7692 * registers for error attention. This can be changed later.
7693 */
7694 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7695 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7696 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7697 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7698 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7699 if (uerr_sta_lo || uerr_sta_hi) {
7700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7701 "1423 HBA Unrecoverable error: "
7702 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7703 "online0_reg=0x%x, online1_reg=0x%x\n",
7704 uerr_sta_lo, uerr_sta_hi,
7705 onlnreg0, onlnreg1);
James Smartda0436e2009-05-22 14:51:39 -04007706 phba->work_status[0] = uerr_sta_lo;
7707 phba->work_status[1] = uerr_sta_hi;
James Smartda0436e2009-05-22 14:51:39 -04007708 /* Set the driver HA work bitmap */
7709 phba->work_ha |= HA_ERATT;
7710 /* Indicate polling handles this ERATT */
7711 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smartda0436e2009-05-22 14:51:39 -04007712 return 1;
7713 }
7714 }
7715 return 0;
7716}
7717
7718/**
James Smart3621a712009-04-06 18:47:14 -04007719 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007720 * @phba: Pointer to HBA context.
7721 *
James Smart3772a992009-05-22 14:50:54 -04007722 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007723 * error attention register bit for error attention events.
7724 *
7725 * This fucntion returns 1 when there is Error Attention in the Host Attention
7726 * Register and returns 0 otherwise.
7727 **/
7728int
7729lpfc_sli_check_eratt(struct lpfc_hba *phba)
7730{
7731 uint32_t ha_copy;
7732
7733 /* If somebody is waiting to handle an eratt, don't process it
7734 * here. The brdkill function will do this.
7735 */
7736 if (phba->link_flag & LS_IGNORE_ERATT)
7737 return 0;
7738
7739 /* Check if interrupt handler handles this ERATT */
7740 spin_lock_irq(&phba->hbalock);
7741 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7742 /* Interrupt handler has handled ERATT */
7743 spin_unlock_irq(&phba->hbalock);
7744 return 0;
7745 }
7746
James Smarta257bf92009-04-06 18:48:10 -04007747 /*
7748 * If there is deferred error attention, do not check for error
7749 * attention
7750 */
7751 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7752 spin_unlock_irq(&phba->hbalock);
7753 return 0;
7754 }
7755
James Smart3772a992009-05-22 14:50:54 -04007756 /* If PCI channel is offline, don't process it */
7757 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007758 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007759 return 0;
7760 }
7761
7762 switch (phba->sli_rev) {
7763 case LPFC_SLI_REV2:
7764 case LPFC_SLI_REV3:
7765 /* Read chip Host Attention (HA) register */
7766 ha_copy = lpfc_sli_eratt_read(phba);
7767 break;
James Smartda0436e2009-05-22 14:51:39 -04007768 case LPFC_SLI_REV4:
7769 /* Read devcie Uncoverable Error (UERR) registers */
7770 ha_copy = lpfc_sli4_eratt_read(phba);
7771 break;
James Smart3772a992009-05-22 14:50:54 -04007772 default:
7773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7774 "0299 Invalid SLI revision (%d)\n",
7775 phba->sli_rev);
7776 ha_copy = 0;
7777 break;
James Smart93996272008-08-24 21:50:30 -04007778 }
7779 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007780
7781 return ha_copy;
7782}
7783
7784/**
7785 * lpfc_intr_state_check - Check device state for interrupt handling
7786 * @phba: Pointer to HBA context.
7787 *
7788 * This inline routine checks whether a device or its PCI slot is in a state
7789 * that the interrupt should be handled.
7790 *
7791 * This function returns 0 if the device or the PCI slot is in a state that
7792 * interrupt should be handled, otherwise -EIO.
7793 */
7794static inline int
7795lpfc_intr_state_check(struct lpfc_hba *phba)
7796{
7797 /* If the pci channel is offline, ignore all the interrupts */
7798 if (unlikely(pci_channel_offline(phba->pcidev)))
7799 return -EIO;
7800
7801 /* Update device level interrupt statistics */
7802 phba->sli.slistat.sli_intr++;
7803
7804 /* Ignore all interrupts during initialization. */
7805 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7806 return -EIO;
7807
James Smart93996272008-08-24 21:50:30 -04007808 return 0;
7809}
7810
7811/**
James Smart3772a992009-05-22 14:50:54 -04007812 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007813 * @irq: Interrupt number.
7814 * @dev_id: The device context pointer.
7815 *
James Smart93996272008-08-24 21:50:30 -04007816 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007817 * service routine when device with SLI-3 interface spec is enabled with
7818 * MSI-X multi-message interrupt mode and there are slow-path events in
7819 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7820 * interrupt mode, this function is called as part of the device-level
7821 * interrupt handler. When the PCI slot is in error recovery or the HBA
7822 * is undergoing initialization, the interrupt handler will not process
7823 * the interrupt. The link attention and ELS ring attention events are
7824 * handled by the worker thread. The interrupt handler signals the worker
7825 * thread and returns for these events. This function is called without
7826 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007827 * structures.
7828 *
7829 * This function returns IRQ_HANDLED when interrupt is handled else it
7830 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007831 **/
dea31012005-04-17 16:05:31 -05007832irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007833lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007834{
James Smart2e0fef82007-06-17 19:56:36 -05007835 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05007836 uint32_t ha_copy;
7837 uint32_t work_ha_copy;
7838 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007839 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007840 uint32_t control;
7841
James Smart92d7f7b2007-06-17 19:56:38 -05007842 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007843 struct lpfc_vport *vport;
7844 struct lpfc_nodelist *ndlp;
7845 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007846 LPFC_MBOXQ_t *pmb;
7847 int rc;
7848
dea31012005-04-17 16:05:31 -05007849 /*
7850 * Get the driver's phba structure from the dev_id and
7851 * assume the HBA is not interrupting.
7852 */
James Smart93996272008-08-24 21:50:30 -04007853 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007854
7855 if (unlikely(!phba))
7856 return IRQ_NONE;
7857
dea31012005-04-17 16:05:31 -05007858 /*
James Smart93996272008-08-24 21:50:30 -04007859 * Stuff needs to be attented to when this function is invoked as an
7860 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007861 */
James Smart93996272008-08-24 21:50:30 -04007862 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007863 /* Check device state for handling interrupt */
7864 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007865 return IRQ_NONE;
7866 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007867 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007868 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007869 /* If somebody is waiting to handle an eratt don't process it
7870 * here. The brdkill function will do this.
7871 */
7872 if (phba->link_flag & LS_IGNORE_ERATT)
7873 ha_copy &= ~HA_ERATT;
7874 /* Check the need for handling ERATT in interrupt handler */
7875 if (ha_copy & HA_ERATT) {
7876 if (phba->hba_flag & HBA_ERATT_HANDLED)
7877 /* ERATT polling has handled ERATT */
7878 ha_copy &= ~HA_ERATT;
7879 else
7880 /* Indicate interrupt handler handles ERATT */
7881 phba->hba_flag |= HBA_ERATT_HANDLED;
7882 }
James Smarta257bf92009-04-06 18:48:10 -04007883
7884 /*
7885 * If there is deferred error attention, do not check for any
7886 * interrupt.
7887 */
7888 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007889 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007890 return IRQ_NONE;
7891 }
7892
James Smart93996272008-08-24 21:50:30 -04007893 /* Clear up only attention source related to slow-path */
7894 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7895 phba->HAregaddr);
7896 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007897 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007898 } else
7899 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007900
dea31012005-04-17 16:05:31 -05007901 work_ha_copy = ha_copy & phba->work_ha_mask;
7902
James Smart93996272008-08-24 21:50:30 -04007903 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007904 if (work_ha_copy & HA_LATT) {
7905 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7906 /*
7907 * Turn off Link Attention interrupts
7908 * until CLEAR_LA done
7909 */
James Smart5b75da22008-12-04 22:39:35 -05007910 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007911 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7912 control = readl(phba->HCregaddr);
7913 control &= ~HC_LAINT_ENA;
7914 writel(control, phba->HCregaddr);
7915 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007916 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007917 }
7918 else
7919 work_ha_copy &= ~HA_LATT;
7920 }
7921
James Smart93996272008-08-24 21:50:30 -04007922 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05007923 /*
7924 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7925 * the only slow ring.
7926 */
7927 status = (work_ha_copy &
7928 (HA_RXMASK << (4*LPFC_ELS_RING)));
7929 status >>= (4*LPFC_ELS_RING);
7930 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05007931 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05007932 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04007933
7934 lpfc_debugfs_slow_ring_trc(phba,
7935 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7936 control, status,
7937 (uint32_t)phba->sli.slistat.sli_intr);
7938
James Smart858c9f62007-06-17 19:56:39 -05007939 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04007940 lpfc_debugfs_slow_ring_trc(phba,
7941 "ISR Disable ring:"
7942 "pwork:x%x hawork:x%x wait:x%x",
7943 phba->work_ha, work_ha_copy,
7944 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007945 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007946
James Smart858c9f62007-06-17 19:56:39 -05007947 control &=
7948 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05007949 writel(control, phba->HCregaddr);
7950 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05007951 }
James Smarta58cbd52007-08-02 11:09:43 -04007952 else {
7953 lpfc_debugfs_slow_ring_trc(phba,
7954 "ISR slow ring: pwork:"
7955 "x%x hawork:x%x wait:x%x",
7956 phba->work_ha, work_ha_copy,
7957 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007958 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007959 }
James Smart5b75da22008-12-04 22:39:35 -05007960 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007961 }
7962 }
James Smart5b75da22008-12-04 22:39:35 -05007963 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007964 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04007965 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04007966 /*
7967 * Check if there is a deferred error condition
7968 * is active
7969 */
7970 if ((HS_FFER1 & phba->work_hs) &&
7971 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7972 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7973 phba->hba_flag |= DEFER_ERATT;
7974 /* Clear all interrupt enable conditions */
7975 writel(0, phba->HCregaddr);
7976 readl(phba->HCregaddr);
7977 }
7978 }
7979
James Smart93996272008-08-24 21:50:30 -04007980 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007981 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04007982 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04007983 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05007984 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05007985
7986 /* First check out the status word */
7987 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
7988 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05007989 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007990 /*
7991 * Stray Mailbox Interrupt, mbxCommand <cmd>
7992 * mbxStatus <status>
7993 */
James Smart09372822008-01-11 01:52:54 -05007994 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05007995 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007996 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05007997 "Interrupt mbxCommand x%x "
7998 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007999 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008000 pmbox->mbxCommand,
8001 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008002 /* clear mailbox attention bit */
8003 work_ha_copy &= ~HA_MBATT;
8004 } else {
James Smart97eab632008-04-07 10:16:05 -04008005 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008006 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008007 phba->last_completion_time = jiffies;
8008 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008009 if (pmb->mbox_cmpl) {
8010 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8011 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008012 }
James Smart09372822008-01-11 01:52:54 -05008013 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8014 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8015
8016 lpfc_debugfs_disc_trc(vport,
8017 LPFC_DISC_TRC_MBOX_VPORT,
8018 "MBOX dflt rpi: : "
8019 "status:x%x rpi:x%x",
8020 (uint32_t)pmbox->mbxStatus,
8021 pmbox->un.varWords[0], 0);
8022
8023 if (!pmbox->mbxStatus) {
8024 mp = (struct lpfc_dmabuf *)
8025 (pmb->context1);
8026 ndlp = (struct lpfc_nodelist *)
8027 pmb->context2;
8028
8029 /* Reg_LOGIN of dflt RPI was
8030 * successful. new lets get
8031 * rid of the RPI using the
8032 * same mbox buffer.
8033 */
8034 lpfc_unreg_login(phba,
8035 vport->vpi,
8036 pmbox->un.varWords[0],
8037 pmb);
8038 pmb->mbox_cmpl =
8039 lpfc_mbx_cmpl_dflt_rpi;
8040 pmb->context1 = mp;
8041 pmb->context2 = ndlp;
8042 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008043 rc = lpfc_sli_issue_mbox(phba,
8044 pmb,
8045 MBX_NOWAIT);
8046 if (rc != MBX_BUSY)
8047 lpfc_printf_log(phba,
8048 KERN_ERR,
8049 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008050 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008051 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008052 if (rc != MBX_NOT_FINISHED)
8053 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008054 }
8055 }
James Smart5b75da22008-12-04 22:39:35 -05008056 spin_lock_irqsave(
8057 &phba->pport->work_port_lock,
8058 iflag);
James Smart09372822008-01-11 01:52:54 -05008059 phba->pport->work_port_events &=
8060 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008061 spin_unlock_irqrestore(
8062 &phba->pport->work_port_lock,
8063 iflag);
James Smart09372822008-01-11 01:52:54 -05008064 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008065 }
James Smart97eab632008-04-07 10:16:05 -04008066 } else
James Smart5b75da22008-12-04 22:39:35 -05008067 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008068
James Smart92d7f7b2007-06-17 19:56:38 -05008069 if ((work_ha_copy & HA_MBATT) &&
8070 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008071send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008072 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008073 do {
8074 rc = lpfc_sli_issue_mbox(phba, NULL,
8075 MBX_NOWAIT);
8076 } while (rc == MBX_NOT_FINISHED);
8077 if (rc != MBX_SUCCESS)
8078 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8079 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008080 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008081 }
8082
James Smart5b75da22008-12-04 22:39:35 -05008083 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008084 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008085 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008086 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008087 }
James Smart93996272008-08-24 21:50:30 -04008088 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008089
James Smart3772a992009-05-22 14:50:54 -04008090} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008091
8092/**
James Smart3772a992009-05-22 14:50:54 -04008093 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008094 * @irq: Interrupt number.
8095 * @dev_id: The device context pointer.
8096 *
8097 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008098 * service routine when device with SLI-3 interface spec is enabled with
8099 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8100 * ring event in the HBA. However, when the device is enabled with either
8101 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8102 * device-level interrupt handler. When the PCI slot is in error recovery
8103 * or the HBA is undergoing initialization, the interrupt handler will not
8104 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8105 * the intrrupt context. This function is called without any lock held.
8106 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008107 *
8108 * This function returns IRQ_HANDLED when interrupt is handled else it
8109 * returns IRQ_NONE.
8110 **/
8111irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008112lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008113{
8114 struct lpfc_hba *phba;
8115 uint32_t ha_copy;
8116 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008117 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008118
8119 /* Get the driver's phba structure from the dev_id and
8120 * assume the HBA is not interrupting.
8121 */
8122 phba = (struct lpfc_hba *) dev_id;
8123
8124 if (unlikely(!phba))
8125 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008126
8127 /*
James Smart93996272008-08-24 21:50:30 -04008128 * Stuff needs to be attented to when this function is invoked as an
8129 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008130 */
James Smart93996272008-08-24 21:50:30 -04008131 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008132 /* Check device state for handling interrupt */
8133 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008134 return IRQ_NONE;
8135 /* Need to read HA REG for FCP ring and other ring events */
8136 ha_copy = readl(phba->HAregaddr);
8137 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008138 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008139 /*
8140 * If there is deferred error attention, do not check for
8141 * any interrupt.
8142 */
8143 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008144 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008145 return IRQ_NONE;
8146 }
James Smart93996272008-08-24 21:50:30 -04008147 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8148 phba->HAregaddr);
8149 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008150 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008151 } else
8152 ha_copy = phba->ha_copy;
8153
8154 /*
8155 * Process all events on FCP ring. Take the optimized path for FCP IO.
8156 */
8157 ha_copy &= ~(phba->work_ha_mask);
8158
8159 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008160 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008161 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008162 lpfc_sli_handle_fast_ring_event(phba,
8163 &phba->sli.ring[LPFC_FCP_RING],
8164 status);
James Smarta4bc3372006-12-02 13:34:16 -05008165
8166 if (phba->cfg_multi_ring_support == 2) {
8167 /*
James Smart93996272008-08-24 21:50:30 -04008168 * Process all events on extra ring. Take the optimized path
8169 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008170 */
James Smart93996272008-08-24 21:50:30 -04008171 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008172 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008173 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008174 lpfc_sli_handle_fast_ring_event(phba,
8175 &phba->sli.ring[LPFC_EXTRA_RING],
8176 status);
8177 }
8178 }
dea31012005-04-17 16:05:31 -05008179 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008180} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008181
James Smart93996272008-08-24 21:50:30 -04008182/**
James Smart3772a992009-05-22 14:50:54 -04008183 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008184 * @irq: Interrupt number.
8185 * @dev_id: The device context pointer.
8186 *
James Smart3772a992009-05-22 14:50:54 -04008187 * This function is the HBA device-level interrupt handler to device with
8188 * SLI-3 interface spec, called from the PCI layer when either MSI or
8189 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8190 * requires driver attention. This function invokes the slow-path interrupt
8191 * attention handling function and fast-path interrupt attention handling
8192 * function in turn to process the relevant HBA attention events. This
8193 * function is called without any lock held. It gets the hbalock to access
8194 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008195 *
8196 * This function returns IRQ_HANDLED when interrupt is handled, else it
8197 * returns IRQ_NONE.
8198 **/
8199irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008200lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008201{
8202 struct lpfc_hba *phba;
8203 irqreturn_t sp_irq_rc, fp_irq_rc;
8204 unsigned long status1, status2;
8205
8206 /*
8207 * Get the driver's phba structure from the dev_id and
8208 * assume the HBA is not interrupting.
8209 */
8210 phba = (struct lpfc_hba *) dev_id;
8211
8212 if (unlikely(!phba))
8213 return IRQ_NONE;
8214
James Smart3772a992009-05-22 14:50:54 -04008215 /* Check device state for handling interrupt */
8216 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008217 return IRQ_NONE;
8218
8219 spin_lock(&phba->hbalock);
8220 phba->ha_copy = readl(phba->HAregaddr);
8221 if (unlikely(!phba->ha_copy)) {
8222 spin_unlock(&phba->hbalock);
8223 return IRQ_NONE;
8224 } else if (phba->ha_copy & HA_ERATT) {
8225 if (phba->hba_flag & HBA_ERATT_HANDLED)
8226 /* ERATT polling has handled ERATT */
8227 phba->ha_copy &= ~HA_ERATT;
8228 else
8229 /* Indicate interrupt handler handles ERATT */
8230 phba->hba_flag |= HBA_ERATT_HANDLED;
8231 }
8232
James Smarta257bf92009-04-06 18:48:10 -04008233 /*
8234 * If there is deferred error attention, do not check for any interrupt.
8235 */
8236 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8237 spin_unlock_irq(&phba->hbalock);
8238 return IRQ_NONE;
8239 }
8240
James Smart93996272008-08-24 21:50:30 -04008241 /* Clear attention sources except link and error attentions */
8242 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8243 readl(phba->HAregaddr); /* flush */
8244 spin_unlock(&phba->hbalock);
8245
8246 /*
8247 * Invokes slow-path host attention interrupt handling as appropriate.
8248 */
8249
8250 /* status of events with mailbox and link attention */
8251 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8252
8253 /* status of events with ELS ring */
8254 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8255 status2 >>= (4*LPFC_ELS_RING);
8256
8257 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008258 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008259 else
8260 sp_irq_rc = IRQ_NONE;
8261
8262 /*
8263 * Invoke fast-path host attention interrupt handling as appropriate.
8264 */
8265
8266 /* status of events with FCP ring */
8267 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8268 status1 >>= (4*LPFC_FCP_RING);
8269
8270 /* status of events with extra ring */
8271 if (phba->cfg_multi_ring_support == 2) {
8272 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8273 status2 >>= (4*LPFC_EXTRA_RING);
8274 } else
8275 status2 = 0;
8276
8277 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008278 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008279 else
8280 fp_irq_rc = IRQ_NONE;
8281
8282 /* Return device-level interrupt handling status */
8283 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008284} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008285
8286/**
8287 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8288 * @phba: pointer to lpfc hba data structure.
8289 *
8290 * This routine is invoked by the worker thread to process all the pending
8291 * SLI4 FCP abort XRI events.
8292 **/
8293void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8294{
8295 struct lpfc_cq_event *cq_event;
8296
8297 /* First, declare the fcp xri abort event has been handled */
8298 spin_lock_irq(&phba->hbalock);
8299 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8300 spin_unlock_irq(&phba->hbalock);
8301 /* Now, handle all the fcp xri abort events */
8302 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8303 /* Get the first event from the head of the event queue */
8304 spin_lock_irq(&phba->hbalock);
8305 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8306 cq_event, struct lpfc_cq_event, list);
8307 spin_unlock_irq(&phba->hbalock);
8308 /* Notify aborted XRI for FCP work queue */
8309 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8310 /* Free the event processed back to the free pool */
8311 lpfc_sli4_cq_event_release(phba, cq_event);
8312 }
8313}
8314
8315/**
8316 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8317 * @phba: pointer to lpfc hba data structure.
8318 *
8319 * This routine is invoked by the worker thread to process all the pending
8320 * SLI4 els abort xri events.
8321 **/
8322void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8323{
8324 struct lpfc_cq_event *cq_event;
8325
8326 /* First, declare the els xri abort event has been handled */
8327 spin_lock_irq(&phba->hbalock);
8328 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8329 spin_unlock_irq(&phba->hbalock);
8330 /* Now, handle all the els xri abort events */
8331 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8332 /* Get the first event from the head of the event queue */
8333 spin_lock_irq(&phba->hbalock);
8334 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8335 cq_event, struct lpfc_cq_event, list);
8336 spin_unlock_irq(&phba->hbalock);
8337 /* Notify aborted XRI for ELS work queue */
8338 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8339 /* Free the event processed back to the free pool */
8340 lpfc_sli4_cq_event_release(phba, cq_event);
8341 }
8342}
8343
8344static void
8345lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8346 struct lpfc_iocbq *pIocbOut,
8347 struct lpfc_wcqe_complete *wcqe)
8348{
8349 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8350
8351 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8352 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008353 /* Map WCQE parameters into irspiocb parameters */
8354 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8355 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8356 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8357 pIocbIn->iocb.un.fcpi.fcpi_parm =
8358 pIocbOut->iocb.un.fcpi.fcpi_parm -
8359 wcqe->total_data_placed;
8360 else
8361 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8362 else
8363 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart4f774512009-05-22 14:52:35 -04008364}
8365
8366/**
James Smart45ed1192009-10-02 15:17:02 -04008367 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8368 * @phba: Pointer to HBA context object.
8369 * @wcqe: Pointer to work-queue completion queue entry.
8370 *
8371 * This routine handles an ELS work-queue completion event and construct
8372 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8373 * discovery engine to handle.
8374 *
8375 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8376 **/
8377static struct lpfc_iocbq *
8378lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8379 struct lpfc_iocbq *irspiocbq)
8380{
8381 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8382 struct lpfc_iocbq *cmdiocbq;
8383 struct lpfc_wcqe_complete *wcqe;
8384 unsigned long iflags;
8385
8386 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8387 spin_lock_irqsave(&phba->hbalock, iflags);
8388 pring->stats.iocb_event++;
8389 /* Look up the ELS command IOCB and create pseudo response IOCB */
8390 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8391 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8392 spin_unlock_irqrestore(&phba->hbalock, iflags);
8393
8394 if (unlikely(!cmdiocbq)) {
8395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8396 "0386 ELS complete with no corresponding "
8397 "cmdiocb: iotag (%d)\n",
8398 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8399 lpfc_sli_release_iocbq(phba, irspiocbq);
8400 return NULL;
8401 }
8402
8403 /* Fake the irspiocbq and copy necessary response information */
8404 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8405
8406 return irspiocbq;
8407}
8408
8409/**
James Smart04c68492009-05-22 14:52:52 -04008410 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8411 * @phba: Pointer to HBA context object.
8412 * @cqe: Pointer to mailbox completion queue entry.
8413 *
8414 * This routine process a mailbox completion queue entry with asynchrous
8415 * event.
8416 *
8417 * Return: true if work posted to worker thread, otherwise false.
8418 **/
8419static bool
8420lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8421{
8422 struct lpfc_cq_event *cq_event;
8423 unsigned long iflags;
8424
8425 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8426 "0392 Async Event: word0:x%x, word1:x%x, "
8427 "word2:x%x, word3:x%x\n", mcqe->word0,
8428 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8429
8430 /* Allocate a new internal CQ_EVENT entry */
8431 cq_event = lpfc_sli4_cq_event_alloc(phba);
8432 if (!cq_event) {
8433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8434 "0394 Failed to allocate CQ_EVENT entry\n");
8435 return false;
8436 }
8437
8438 /* Move the CQE into an asynchronous event entry */
8439 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8440 spin_lock_irqsave(&phba->hbalock, iflags);
8441 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8442 /* Set the async event flag */
8443 phba->hba_flag |= ASYNC_EVENT;
8444 spin_unlock_irqrestore(&phba->hbalock, iflags);
8445
8446 return true;
8447}
8448
8449/**
8450 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8451 * @phba: Pointer to HBA context object.
8452 * @cqe: Pointer to mailbox completion queue entry.
8453 *
8454 * This routine process a mailbox completion queue entry with mailbox
8455 * completion event.
8456 *
8457 * Return: true if work posted to worker thread, otherwise false.
8458 **/
8459static bool
8460lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8461{
8462 uint32_t mcqe_status;
8463 MAILBOX_t *mbox, *pmbox;
8464 struct lpfc_mqe *mqe;
8465 struct lpfc_vport *vport;
8466 struct lpfc_nodelist *ndlp;
8467 struct lpfc_dmabuf *mp;
8468 unsigned long iflags;
8469 LPFC_MBOXQ_t *pmb;
8470 bool workposted = false;
8471 int rc;
8472
8473 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8474 if (!bf_get(lpfc_trailer_completed, mcqe))
8475 goto out_no_mqe_complete;
8476
8477 /* Get the reference to the active mbox command */
8478 spin_lock_irqsave(&phba->hbalock, iflags);
8479 pmb = phba->sli.mbox_active;
8480 if (unlikely(!pmb)) {
8481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8482 "1832 No pending MBOX command to handle\n");
8483 spin_unlock_irqrestore(&phba->hbalock, iflags);
8484 goto out_no_mqe_complete;
8485 }
8486 spin_unlock_irqrestore(&phba->hbalock, iflags);
8487 mqe = &pmb->u.mqe;
8488 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8489 mbox = phba->mbox;
8490 vport = pmb->vport;
8491
8492 /* Reset heartbeat timer */
8493 phba->last_completion_time = jiffies;
8494 del_timer(&phba->sli.mbox_tmo);
8495
8496 /* Move mbox data to caller's mailbox region, do endian swapping */
8497 if (pmb->mbox_cmpl && mbox)
8498 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8499 /* Set the mailbox status with SLI4 range 0x4000 */
8500 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8501 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8502 bf_set(lpfc_mqe_status, mqe,
8503 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8504
8505 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8506 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8507 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8508 "MBOX dflt rpi: status:x%x rpi:x%x",
8509 mcqe_status,
8510 pmbox->un.varWords[0], 0);
8511 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8512 mp = (struct lpfc_dmabuf *)(pmb->context1);
8513 ndlp = (struct lpfc_nodelist *)pmb->context2;
8514 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8515 * RID of the PPI using the same mbox buffer.
8516 */
8517 lpfc_unreg_login(phba, vport->vpi,
8518 pmbox->un.varWords[0], pmb);
8519 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8520 pmb->context1 = mp;
8521 pmb->context2 = ndlp;
8522 pmb->vport = vport;
8523 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8524 if (rc != MBX_BUSY)
8525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8526 LOG_SLI, "0385 rc should "
8527 "have been MBX_BUSY\n");
8528 if (rc != MBX_NOT_FINISHED)
8529 goto send_current_mbox;
8530 }
8531 }
8532 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8533 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8534 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8535
8536 /* There is mailbox completion work to do */
8537 spin_lock_irqsave(&phba->hbalock, iflags);
8538 __lpfc_mbox_cmpl_put(phba, pmb);
8539 phba->work_ha |= HA_MBATT;
8540 spin_unlock_irqrestore(&phba->hbalock, iflags);
8541 workposted = true;
8542
8543send_current_mbox:
8544 spin_lock_irqsave(&phba->hbalock, iflags);
8545 /* Release the mailbox command posting token */
8546 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8547 /* Setting active mailbox pointer need to be in sync to flag clear */
8548 phba->sli.mbox_active = NULL;
8549 spin_unlock_irqrestore(&phba->hbalock, iflags);
8550 /* Wake up worker thread to post the next pending mailbox command */
8551 lpfc_worker_wake_up(phba);
8552out_no_mqe_complete:
8553 if (bf_get(lpfc_trailer_consumed, mcqe))
8554 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8555 return workposted;
8556}
8557
8558/**
8559 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8560 * @phba: Pointer to HBA context object.
8561 * @cqe: Pointer to mailbox completion queue entry.
8562 *
8563 * This routine process a mailbox completion queue entry, it invokes the
8564 * proper mailbox complete handling or asynchrous event handling routine
8565 * according to the MCQE's async bit.
8566 *
8567 * Return: true if work posted to worker thread, otherwise false.
8568 **/
8569static bool
8570lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8571{
8572 struct lpfc_mcqe mcqe;
8573 bool workposted;
8574
8575 /* Copy the mailbox MCQE and convert endian order as needed */
8576 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8577
8578 /* Invoke the proper event handling routine */
8579 if (!bf_get(lpfc_trailer_async, &mcqe))
8580 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8581 else
8582 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8583 return workposted;
8584}
8585
8586/**
James Smart4f774512009-05-22 14:52:35 -04008587 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8588 * @phba: Pointer to HBA context object.
8589 * @wcqe: Pointer to work-queue completion queue entry.
8590 *
8591 * This routine handles an ELS work-queue completion event.
8592 *
8593 * Return: true if work posted to worker thread, otherwise false.
8594 **/
8595static bool
8596lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8597 struct lpfc_wcqe_complete *wcqe)
8598{
James Smart4f774512009-05-22 14:52:35 -04008599 struct lpfc_iocbq *irspiocbq;
8600 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008601
James Smart45ed1192009-10-02 15:17:02 -04008602 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008603 irspiocbq = lpfc_sli_get_iocbq(phba);
8604 if (!irspiocbq) {
8605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8606 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008607 return false;
James Smart4f774512009-05-22 14:52:35 -04008608 }
James Smart4f774512009-05-22 14:52:35 -04008609
James Smart45ed1192009-10-02 15:17:02 -04008610 /* Save off the slow-path queue event for work thread to process */
8611 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008612 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008613 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008614 &phba->sli4_hba.sp_queue_event);
8615 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008616 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008617
James Smart45ed1192009-10-02 15:17:02 -04008618 return true;
James Smart4f774512009-05-22 14:52:35 -04008619}
8620
8621/**
8622 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8623 * @phba: Pointer to HBA context object.
8624 * @wcqe: Pointer to work-queue completion queue entry.
8625 *
8626 * This routine handles slow-path WQ entry comsumed event by invoking the
8627 * proper WQ release routine to the slow-path WQ.
8628 **/
8629static void
8630lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8631 struct lpfc_wcqe_release *wcqe)
8632{
8633 /* Check for the slow-path ELS work queue */
8634 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8635 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8636 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8637 else
8638 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8639 "2579 Slow-path wqe consume event carries "
8640 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8641 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8642 phba->sli4_hba.els_wq->queue_id);
8643}
8644
8645/**
8646 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8647 * @phba: Pointer to HBA context object.
8648 * @cq: Pointer to a WQ completion queue.
8649 * @wcqe: Pointer to work-queue completion queue entry.
8650 *
8651 * This routine handles an XRI abort event.
8652 *
8653 * Return: true if work posted to worker thread, otherwise false.
8654 **/
8655static bool
8656lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8657 struct lpfc_queue *cq,
8658 struct sli4_wcqe_xri_aborted *wcqe)
8659{
8660 bool workposted = false;
8661 struct lpfc_cq_event *cq_event;
8662 unsigned long iflags;
8663
8664 /* Allocate a new internal CQ_EVENT entry */
8665 cq_event = lpfc_sli4_cq_event_alloc(phba);
8666 if (!cq_event) {
8667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8668 "0602 Failed to allocate CQ_EVENT entry\n");
8669 return false;
8670 }
8671
8672 /* Move the CQE into the proper xri abort event list */
8673 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8674 switch (cq->subtype) {
8675 case LPFC_FCP:
8676 spin_lock_irqsave(&phba->hbalock, iflags);
8677 list_add_tail(&cq_event->list,
8678 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8679 /* Set the fcp xri abort event flag */
8680 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8681 spin_unlock_irqrestore(&phba->hbalock, iflags);
8682 workposted = true;
8683 break;
8684 case LPFC_ELS:
8685 spin_lock_irqsave(&phba->hbalock, iflags);
8686 list_add_tail(&cq_event->list,
8687 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8688 /* Set the els xri abort event flag */
8689 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8690 spin_unlock_irqrestore(&phba->hbalock, iflags);
8691 workposted = true;
8692 break;
8693 default:
8694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8695 "0603 Invalid work queue CQE subtype (x%x)\n",
8696 cq->subtype);
8697 workposted = false;
8698 break;
8699 }
8700 return workposted;
8701}
8702
8703/**
James Smart4d9ab992009-10-02 15:16:39 -04008704 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008705 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008706 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008707 *
James Smart4d9ab992009-10-02 15:16:39 -04008708 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008709 *
8710 * Return: true if work posted to worker thread, otherwise false.
8711 **/
8712static bool
James Smart4d9ab992009-10-02 15:16:39 -04008713lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8714{
8715 bool workposted = false;
8716 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8717 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8718 struct hbq_dmabuf *dma_buf;
8719 uint32_t status;
8720 unsigned long iflags;
8721
James Smart4d9ab992009-10-02 15:16:39 -04008722 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8723 goto out;
8724
8725 status = bf_get(lpfc_rcqe_status, rcqe);
8726 switch (status) {
8727 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8729 "2537 Receive Frame Truncated!!\n");
8730 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008731 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008732 spin_lock_irqsave(&phba->hbalock, iflags);
8733 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8734 if (!dma_buf) {
8735 spin_unlock_irqrestore(&phba->hbalock, iflags);
8736 goto out;
8737 }
8738 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8739 /* save off the frame for the word thread to process */
8740 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008741 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008742 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008743 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008744 spin_unlock_irqrestore(&phba->hbalock, iflags);
8745 workposted = true;
8746 break;
8747 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8748 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8749 /* Post more buffers if possible */
8750 spin_lock_irqsave(&phba->hbalock, iflags);
8751 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8752 spin_unlock_irqrestore(&phba->hbalock, iflags);
8753 workposted = true;
8754 break;
8755 }
8756out:
8757 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008758}
8759
8760/**
8761 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8762 * @phba: Pointer to HBA context object.
8763 * @cq: Pointer to the completion queue.
8764 * @wcqe: Pointer to a completion queue entry.
8765 *
8766 * This routine process a slow-path work-queue or recieve queue completion queue
8767 * entry.
8768 *
8769 * Return: true if work posted to worker thread, otherwise false.
8770 **/
8771static bool
8772lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008773 struct lpfc_cqe *cqe)
8774{
James Smart45ed1192009-10-02 15:17:02 -04008775 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008776 bool workposted = false;
8777
8778 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008779 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008780
8781 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008782 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008783 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008784 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008785 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008786 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008787 break;
8788 case CQE_CODE_RELEASE_WQE:
8789 /* Process the WQ release event */
8790 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008791 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008792 break;
8793 case CQE_CODE_XRI_ABORTED:
8794 /* Process the WQ XRI abort event */
8795 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008796 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008797 break;
James Smart4d9ab992009-10-02 15:16:39 -04008798 case CQE_CODE_RECEIVE:
8799 /* Process the RQ event */
8800 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008801 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008802 break;
James Smart4f774512009-05-22 14:52:35 -04008803 default:
8804 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8805 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008806 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008807 break;
8808 }
8809 return workposted;
8810}
8811
8812/**
James Smart4f774512009-05-22 14:52:35 -04008813 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8814 * @phba: Pointer to HBA context object.
8815 * @eqe: Pointer to fast-path event queue entry.
8816 *
8817 * This routine process a event queue entry from the slow-path event queue.
8818 * It will check the MajorCode and MinorCode to determine this is for a
8819 * completion event on a completion queue, if not, an error shall be logged
8820 * and just return. Otherwise, it will get to the corresponding completion
8821 * queue and process all the entries on that completion queue, rearm the
8822 * completion queue, and then return.
8823 *
8824 **/
8825static void
8826lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8827{
8828 struct lpfc_queue *cq = NULL, *childq, *speq;
8829 struct lpfc_cqe *cqe;
8830 bool workposted = false;
8831 int ecount = 0;
8832 uint16_t cqid;
8833
8834 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8835 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8836 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8837 "0359 Not a valid slow-path completion "
8838 "event: majorcode=x%x, minorcode=x%x\n",
8839 bf_get(lpfc_eqe_major_code, eqe),
8840 bf_get(lpfc_eqe_minor_code, eqe));
8841 return;
8842 }
8843
8844 /* Get the reference to the corresponding CQ */
8845 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8846
8847 /* Search for completion queue pointer matching this cqid */
8848 speq = phba->sli4_hba.sp_eq;
8849 list_for_each_entry(childq, &speq->child_list, list) {
8850 if (childq->queue_id == cqid) {
8851 cq = childq;
8852 break;
8853 }
8854 }
8855 if (unlikely(!cq)) {
8856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8857 "0365 Slow-path CQ identifier (%d) does "
8858 "not exist\n", cqid);
8859 return;
8860 }
8861
8862 /* Process all the entries to the CQ */
8863 switch (cq->type) {
8864 case LPFC_MCQ:
8865 while ((cqe = lpfc_sli4_cq_get(cq))) {
8866 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8867 if (!(++ecount % LPFC_GET_QE_REL_INT))
8868 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8869 }
8870 break;
8871 case LPFC_WCQ:
8872 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04008873 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04008874 if (!(++ecount % LPFC_GET_QE_REL_INT))
8875 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8876 }
8877 break;
8878 default:
8879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8880 "0370 Invalid completion queue type (%d)\n",
8881 cq->type);
8882 return;
8883 }
8884
8885 /* Catch the no cq entry condition, log an error */
8886 if (unlikely(ecount == 0))
8887 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8888 "0371 No entry from the CQ: identifier "
8889 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8890
8891 /* In any case, flash and re-arm the RCQ */
8892 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8893
8894 /* wake up worker thread if there are works to be done */
8895 if (workposted)
8896 lpfc_worker_wake_up(phba);
8897}
8898
8899/**
8900 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8901 * @eqe: Pointer to fast-path completion queue entry.
8902 *
8903 * This routine process a fast-path work queue completion entry from fast-path
8904 * event queue for FCP command response completion.
8905 **/
8906static void
8907lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8908 struct lpfc_wcqe_complete *wcqe)
8909{
8910 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8911 struct lpfc_iocbq *cmdiocbq;
8912 struct lpfc_iocbq irspiocbq;
8913 unsigned long iflags;
8914
8915 spin_lock_irqsave(&phba->hbalock, iflags);
8916 pring->stats.iocb_event++;
8917 spin_unlock_irqrestore(&phba->hbalock, iflags);
8918
8919 /* Check for response status */
8920 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8921 /* If resource errors reported from HBA, reduce queue
8922 * depth of the SCSI device.
8923 */
8924 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8925 IOSTAT_LOCAL_REJECT) &&
8926 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8927 phba->lpfc_rampdown_queue_depth(phba);
8928 }
8929 /* Log the error status */
8930 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8931 "0373 FCP complete error: status=x%x, "
8932 "hw_status=x%x, total_data_specified=%d, "
8933 "parameter=x%x, word3=x%x\n",
8934 bf_get(lpfc_wcqe_c_status, wcqe),
8935 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8936 wcqe->total_data_placed, wcqe->parameter,
8937 wcqe->word3);
8938 }
8939
8940 /* Look up the FCP command IOCB and create pseudo response IOCB */
8941 spin_lock_irqsave(&phba->hbalock, iflags);
8942 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8943 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8944 spin_unlock_irqrestore(&phba->hbalock, iflags);
8945 if (unlikely(!cmdiocbq)) {
8946 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8947 "0374 FCP complete with no corresponding "
8948 "cmdiocb: iotag (%d)\n",
8949 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8950 return;
8951 }
8952 if (unlikely(!cmdiocbq->iocb_cmpl)) {
8953 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8954 "0375 FCP cmdiocb not callback function "
8955 "iotag: (%d)\n",
8956 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8957 return;
8958 }
8959
8960 /* Fake the irspiocb and copy necessary response information */
8961 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8962
8963 /* Pass the cmd_iocb and the rsp state to the upper layer */
8964 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8965}
8966
8967/**
8968 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8969 * @phba: Pointer to HBA context object.
8970 * @cq: Pointer to completion queue.
8971 * @wcqe: Pointer to work-queue completion queue entry.
8972 *
8973 * This routine handles an fast-path WQ entry comsumed event by invoking the
8974 * proper WQ release routine to the slow-path WQ.
8975 **/
8976static void
8977lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8978 struct lpfc_wcqe_release *wcqe)
8979{
8980 struct lpfc_queue *childwq;
8981 bool wqid_matched = false;
8982 uint16_t fcp_wqid;
8983
8984 /* Check for fast-path FCP work queue release */
8985 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
8986 list_for_each_entry(childwq, &cq->child_list, list) {
8987 if (childwq->queue_id == fcp_wqid) {
8988 lpfc_sli4_wq_release(childwq,
8989 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8990 wqid_matched = true;
8991 break;
8992 }
8993 }
8994 /* Report warning log message if no match found */
8995 if (wqid_matched != true)
8996 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8997 "2580 Fast-path wqe consume event carries "
8998 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
8999}
9000
9001/**
9002 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9003 * @cq: Pointer to the completion queue.
9004 * @eqe: Pointer to fast-path completion queue entry.
9005 *
9006 * This routine process a fast-path work queue completion entry from fast-path
9007 * event queue for FCP command response completion.
9008 **/
9009static int
9010lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9011 struct lpfc_cqe *cqe)
9012{
9013 struct lpfc_wcqe_release wcqe;
9014 bool workposted = false;
9015
9016 /* Copy the work queue CQE and convert endian order if needed */
9017 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9018
9019 /* Check and process for different type of WCQE and dispatch */
9020 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9021 case CQE_CODE_COMPL_WQE:
9022 /* Process the WQ complete event */
9023 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9024 (struct lpfc_wcqe_complete *)&wcqe);
9025 break;
9026 case CQE_CODE_RELEASE_WQE:
9027 /* Process the WQ release event */
9028 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9029 (struct lpfc_wcqe_release *)&wcqe);
9030 break;
9031 case CQE_CODE_XRI_ABORTED:
9032 /* Process the WQ XRI abort event */
9033 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9034 (struct sli4_wcqe_xri_aborted *)&wcqe);
9035 break;
9036 default:
9037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9038 "0144 Not a valid WCQE code: x%x\n",
9039 bf_get(lpfc_wcqe_c_code, &wcqe));
9040 break;
9041 }
9042 return workposted;
9043}
9044
9045/**
9046 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9047 * @phba: Pointer to HBA context object.
9048 * @eqe: Pointer to fast-path event queue entry.
9049 *
9050 * This routine process a event queue entry from the fast-path event queue.
9051 * It will check the MajorCode and MinorCode to determine this is for a
9052 * completion event on a completion queue, if not, an error shall be logged
9053 * and just return. Otherwise, it will get to the corresponding completion
9054 * queue and process all the entries on the completion queue, rearm the
9055 * completion queue, and then return.
9056 **/
9057static void
9058lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9059 uint32_t fcp_cqidx)
9060{
9061 struct lpfc_queue *cq;
9062 struct lpfc_cqe *cqe;
9063 bool workposted = false;
9064 uint16_t cqid;
9065 int ecount = 0;
9066
9067 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9068 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9069 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9070 "0366 Not a valid fast-path completion "
9071 "event: majorcode=x%x, minorcode=x%x\n",
9072 bf_get(lpfc_eqe_major_code, eqe),
9073 bf_get(lpfc_eqe_minor_code, eqe));
9074 return;
9075 }
9076
9077 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9078 if (unlikely(!cq)) {
9079 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9080 "0367 Fast-path completion queue does not "
9081 "exist\n");
9082 return;
9083 }
9084
9085 /* Get the reference to the corresponding CQ */
9086 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9087 if (unlikely(cqid != cq->queue_id)) {
9088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9089 "0368 Miss-matched fast-path completion "
9090 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9091 cqid, cq->queue_id);
9092 return;
9093 }
9094
9095 /* Process all the entries to the CQ */
9096 while ((cqe = lpfc_sli4_cq_get(cq))) {
9097 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9098 if (!(++ecount % LPFC_GET_QE_REL_INT))
9099 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9100 }
9101
9102 /* Catch the no cq entry condition */
9103 if (unlikely(ecount == 0))
9104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9105 "0369 No entry from fast-path completion "
9106 "queue fcpcqid=%d\n", cq->queue_id);
9107
9108 /* In any case, flash and re-arm the CQ */
9109 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9110
9111 /* wake up worker thread if there are works to be done */
9112 if (workposted)
9113 lpfc_worker_wake_up(phba);
9114}
9115
9116static void
9117lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9118{
9119 struct lpfc_eqe *eqe;
9120
9121 /* walk all the EQ entries and drop on the floor */
9122 while ((eqe = lpfc_sli4_eq_get(eq)))
9123 ;
9124
9125 /* Clear and re-arm the EQ */
9126 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9127}
9128
9129/**
9130 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9131 * @irq: Interrupt number.
9132 * @dev_id: The device context pointer.
9133 *
9134 * This function is directly called from the PCI layer as an interrupt
9135 * service routine when device with SLI-4 interface spec is enabled with
9136 * MSI-X multi-message interrupt mode and there are slow-path events in
9137 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9138 * interrupt mode, this function is called as part of the device-level
9139 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9140 * undergoing initialization, the interrupt handler will not process the
9141 * interrupt. The link attention and ELS ring attention events are handled
9142 * by the worker thread. The interrupt handler signals the worker thread
9143 * and returns for these events. This function is called without any lock
9144 * held. It gets the hbalock to access and update SLI data structures.
9145 *
9146 * This function returns IRQ_HANDLED when interrupt is handled else it
9147 * returns IRQ_NONE.
9148 **/
9149irqreturn_t
9150lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9151{
9152 struct lpfc_hba *phba;
9153 struct lpfc_queue *speq;
9154 struct lpfc_eqe *eqe;
9155 unsigned long iflag;
9156 int ecount = 0;
9157
9158 /*
9159 * Get the driver's phba structure from the dev_id
9160 */
9161 phba = (struct lpfc_hba *)dev_id;
9162
9163 if (unlikely(!phba))
9164 return IRQ_NONE;
9165
9166 /* Get to the EQ struct associated with this vector */
9167 speq = phba->sli4_hba.sp_eq;
9168
9169 /* Check device state for handling interrupt */
9170 if (unlikely(lpfc_intr_state_check(phba))) {
9171 /* Check again for link_state with lock held */
9172 spin_lock_irqsave(&phba->hbalock, iflag);
9173 if (phba->link_state < LPFC_LINK_DOWN)
9174 /* Flush, clear interrupt, and rearm the EQ */
9175 lpfc_sli4_eq_flush(phba, speq);
9176 spin_unlock_irqrestore(&phba->hbalock, iflag);
9177 return IRQ_NONE;
9178 }
9179
9180 /*
9181 * Process all the event on FCP slow-path EQ
9182 */
9183 while ((eqe = lpfc_sli4_eq_get(speq))) {
9184 lpfc_sli4_sp_handle_eqe(phba, eqe);
9185 if (!(++ecount % LPFC_GET_QE_REL_INT))
9186 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9187 }
9188
9189 /* Always clear and re-arm the slow-path EQ */
9190 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9191
9192 /* Catch the no cq entry condition */
9193 if (unlikely(ecount == 0)) {
9194 if (phba->intr_type == MSIX)
9195 /* MSI-X treated interrupt served as no EQ share INT */
9196 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9197 "0357 MSI-X interrupt with no EQE\n");
9198 else
9199 /* Non MSI-X treated on interrupt as EQ share INT */
9200 return IRQ_NONE;
9201 }
9202
9203 return IRQ_HANDLED;
9204} /* lpfc_sli4_sp_intr_handler */
9205
9206/**
9207 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9208 * @irq: Interrupt number.
9209 * @dev_id: The device context pointer.
9210 *
9211 * This function is directly called from the PCI layer as an interrupt
9212 * service routine when device with SLI-4 interface spec is enabled with
9213 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9214 * ring event in the HBA. However, when the device is enabled with either
9215 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9216 * device-level interrupt handler. When the PCI slot is in error recovery
9217 * or the HBA is undergoing initialization, the interrupt handler will not
9218 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9219 * the intrrupt context. This function is called without any lock held.
9220 * It gets the hbalock to access and update SLI data structures. Note that,
9221 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9222 * equal to that of FCP CQ index.
9223 *
9224 * This function returns IRQ_HANDLED when interrupt is handled else it
9225 * returns IRQ_NONE.
9226 **/
9227irqreturn_t
9228lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9229{
9230 struct lpfc_hba *phba;
9231 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9232 struct lpfc_queue *fpeq;
9233 struct lpfc_eqe *eqe;
9234 unsigned long iflag;
9235 int ecount = 0;
9236 uint32_t fcp_eqidx;
9237
9238 /* Get the driver's phba structure from the dev_id */
9239 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9240 phba = fcp_eq_hdl->phba;
9241 fcp_eqidx = fcp_eq_hdl->idx;
9242
9243 if (unlikely(!phba))
9244 return IRQ_NONE;
9245
9246 /* Get to the EQ struct associated with this vector */
9247 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9248
9249 /* Check device state for handling interrupt */
9250 if (unlikely(lpfc_intr_state_check(phba))) {
9251 /* Check again for link_state with lock held */
9252 spin_lock_irqsave(&phba->hbalock, iflag);
9253 if (phba->link_state < LPFC_LINK_DOWN)
9254 /* Flush, clear interrupt, and rearm the EQ */
9255 lpfc_sli4_eq_flush(phba, fpeq);
9256 spin_unlock_irqrestore(&phba->hbalock, iflag);
9257 return IRQ_NONE;
9258 }
9259
9260 /*
9261 * Process all the event on FCP fast-path EQ
9262 */
9263 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9264 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9265 if (!(++ecount % LPFC_GET_QE_REL_INT))
9266 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9267 }
9268
9269 /* Always clear and re-arm the fast-path EQ */
9270 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9271
9272 if (unlikely(ecount == 0)) {
9273 if (phba->intr_type == MSIX)
9274 /* MSI-X treated interrupt served as no EQ share INT */
9275 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9276 "0358 MSI-X interrupt with no EQE\n");
9277 else
9278 /* Non MSI-X treated on interrupt as EQ share INT */
9279 return IRQ_NONE;
9280 }
9281
9282 return IRQ_HANDLED;
9283} /* lpfc_sli4_fp_intr_handler */
9284
9285/**
9286 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9287 * @irq: Interrupt number.
9288 * @dev_id: The device context pointer.
9289 *
9290 * This function is the device-level interrupt handler to device with SLI-4
9291 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9292 * interrupt mode is enabled and there is an event in the HBA which requires
9293 * driver attention. This function invokes the slow-path interrupt attention
9294 * handling function and fast-path interrupt attention handling function in
9295 * turn to process the relevant HBA attention events. This function is called
9296 * without any lock held. It gets the hbalock to access and update SLI data
9297 * structures.
9298 *
9299 * This function returns IRQ_HANDLED when interrupt is handled, else it
9300 * returns IRQ_NONE.
9301 **/
9302irqreturn_t
9303lpfc_sli4_intr_handler(int irq, void *dev_id)
9304{
9305 struct lpfc_hba *phba;
9306 irqreturn_t sp_irq_rc, fp_irq_rc;
9307 bool fp_handled = false;
9308 uint32_t fcp_eqidx;
9309
9310 /* Get the driver's phba structure from the dev_id */
9311 phba = (struct lpfc_hba *)dev_id;
9312
9313 if (unlikely(!phba))
9314 return IRQ_NONE;
9315
9316 /*
9317 * Invokes slow-path host attention interrupt handling as appropriate.
9318 */
9319 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9320
9321 /*
9322 * Invoke fast-path host attention interrupt handling as appropriate.
9323 */
9324 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9325 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9326 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9327 if (fp_irq_rc == IRQ_HANDLED)
9328 fp_handled |= true;
9329 }
9330
9331 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9332} /* lpfc_sli4_intr_handler */
9333
9334/**
9335 * lpfc_sli4_queue_free - free a queue structure and associated memory
9336 * @queue: The queue structure to free.
9337 *
9338 * This function frees a queue structure and the DMAable memeory used for
9339 * the host resident queue. This function must be called after destroying the
9340 * queue on the HBA.
9341 **/
9342void
9343lpfc_sli4_queue_free(struct lpfc_queue *queue)
9344{
9345 struct lpfc_dmabuf *dmabuf;
9346
9347 if (!queue)
9348 return;
9349
9350 while (!list_empty(&queue->page_list)) {
9351 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9352 list);
9353 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9354 dmabuf->virt, dmabuf->phys);
9355 kfree(dmabuf);
9356 }
9357 kfree(queue);
9358 return;
9359}
9360
9361/**
9362 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9363 * @phba: The HBA that this queue is being created on.
9364 * @entry_size: The size of each queue entry for this queue.
9365 * @entry count: The number of entries that this queue will handle.
9366 *
9367 * This function allocates a queue structure and the DMAable memory used for
9368 * the host resident queue. This function must be called before creating the
9369 * queue on the HBA.
9370 **/
9371struct lpfc_queue *
9372lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9373 uint32_t entry_count)
9374{
9375 struct lpfc_queue *queue;
9376 struct lpfc_dmabuf *dmabuf;
9377 int x, total_qe_count;
9378 void *dma_pointer;
9379
9380
9381 queue = kzalloc(sizeof(struct lpfc_queue) +
9382 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9383 if (!queue)
9384 return NULL;
9385 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9386 INIT_LIST_HEAD(&queue->list);
9387 INIT_LIST_HEAD(&queue->page_list);
9388 INIT_LIST_HEAD(&queue->child_list);
9389 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9390 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9391 if (!dmabuf)
9392 goto out_fail;
9393 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9394 PAGE_SIZE, &dmabuf->phys,
9395 GFP_KERNEL);
9396 if (!dmabuf->virt) {
9397 kfree(dmabuf);
9398 goto out_fail;
9399 }
James Smartd11e31d2009-06-10 17:23:06 -04009400 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009401 dmabuf->buffer_tag = x;
9402 list_add_tail(&dmabuf->list, &queue->page_list);
9403 /* initialize queue's entry array */
9404 dma_pointer = dmabuf->virt;
9405 for (; total_qe_count < entry_count &&
9406 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9407 total_qe_count++, dma_pointer += entry_size) {
9408 queue->qe[total_qe_count].address = dma_pointer;
9409 }
9410 }
9411 queue->entry_size = entry_size;
9412 queue->entry_count = entry_count;
9413 queue->phba = phba;
9414
9415 return queue;
9416out_fail:
9417 lpfc_sli4_queue_free(queue);
9418 return NULL;
9419}
9420
9421/**
9422 * lpfc_eq_create - Create an Event Queue on the HBA
9423 * @phba: HBA structure that indicates port to create a queue on.
9424 * @eq: The queue structure to use to create the event queue.
9425 * @imax: The maximum interrupt per second limit.
9426 *
9427 * This function creates an event queue, as detailed in @eq, on a port,
9428 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9429 *
9430 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9431 * is used to get the entry count and entry size that are necessary to
9432 * determine the number of pages to allocate and use for this queue. This
9433 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9434 * event queue. This function is asynchronous and will wait for the mailbox
9435 * command to finish before continuing.
9436 *
9437 * On success this function will return a zero. If unable to allocate enough
9438 * memory this function will return ENOMEM. If the queue create mailbox command
9439 * fails this function will return ENXIO.
9440 **/
9441uint32_t
9442lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9443{
9444 struct lpfc_mbx_eq_create *eq_create;
9445 LPFC_MBOXQ_t *mbox;
9446 int rc, length, status = 0;
9447 struct lpfc_dmabuf *dmabuf;
9448 uint32_t shdr_status, shdr_add_status;
9449 union lpfc_sli4_cfg_shdr *shdr;
9450 uint16_t dmult;
9451
9452 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9453 if (!mbox)
9454 return -ENOMEM;
9455 length = (sizeof(struct lpfc_mbx_eq_create) -
9456 sizeof(struct lpfc_sli4_cfg_mhdr));
9457 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9458 LPFC_MBOX_OPCODE_EQ_CREATE,
9459 length, LPFC_SLI4_MBX_EMBED);
9460 eq_create = &mbox->u.mqe.un.eq_create;
9461 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9462 eq->page_count);
9463 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9464 LPFC_EQE_SIZE);
9465 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9466 /* Calculate delay multiper from maximum interrupt per second */
9467 dmult = LPFC_DMULT_CONST/imax - 1;
9468 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9469 dmult);
9470 switch (eq->entry_count) {
9471 default:
9472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9473 "0360 Unsupported EQ count. (%d)\n",
9474 eq->entry_count);
9475 if (eq->entry_count < 256)
9476 return -EINVAL;
9477 /* otherwise default to smallest count (drop through) */
9478 case 256:
9479 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9480 LPFC_EQ_CNT_256);
9481 break;
9482 case 512:
9483 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9484 LPFC_EQ_CNT_512);
9485 break;
9486 case 1024:
9487 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9488 LPFC_EQ_CNT_1024);
9489 break;
9490 case 2048:
9491 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9492 LPFC_EQ_CNT_2048);
9493 break;
9494 case 4096:
9495 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9496 LPFC_EQ_CNT_4096);
9497 break;
9498 }
9499 list_for_each_entry(dmabuf, &eq->page_list, list) {
9500 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9501 putPaddrLow(dmabuf->phys);
9502 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9503 putPaddrHigh(dmabuf->phys);
9504 }
9505 mbox->vport = phba->pport;
9506 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9507 mbox->context1 = NULL;
9508 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9509 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9510 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9511 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9512 if (shdr_status || shdr_add_status || rc) {
9513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9514 "2500 EQ_CREATE mailbox failed with "
9515 "status x%x add_status x%x, mbx status x%x\n",
9516 shdr_status, shdr_add_status, rc);
9517 status = -ENXIO;
9518 }
9519 eq->type = LPFC_EQ;
9520 eq->subtype = LPFC_NONE;
9521 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9522 if (eq->queue_id == 0xFFFF)
9523 status = -ENXIO;
9524 eq->host_index = 0;
9525 eq->hba_index = 0;
9526
James Smart8fa38512009-07-19 10:01:03 -04009527 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009528 return status;
9529}
9530
9531/**
9532 * lpfc_cq_create - Create a Completion Queue on the HBA
9533 * @phba: HBA structure that indicates port to create a queue on.
9534 * @cq: The queue structure to use to create the completion queue.
9535 * @eq: The event queue to bind this completion queue to.
9536 *
9537 * This function creates a completion queue, as detailed in @wq, on a port,
9538 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9539 *
9540 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9541 * is used to get the entry count and entry size that are necessary to
9542 * determine the number of pages to allocate and use for this queue. The @eq
9543 * is used to indicate which event queue to bind this completion queue to. This
9544 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9545 * completion queue. This function is asynchronous and will wait for the mailbox
9546 * command to finish before continuing.
9547 *
9548 * On success this function will return a zero. If unable to allocate enough
9549 * memory this function will return ENOMEM. If the queue create mailbox command
9550 * fails this function will return ENXIO.
9551 **/
9552uint32_t
9553lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9554 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9555{
9556 struct lpfc_mbx_cq_create *cq_create;
9557 struct lpfc_dmabuf *dmabuf;
9558 LPFC_MBOXQ_t *mbox;
9559 int rc, length, status = 0;
9560 uint32_t shdr_status, shdr_add_status;
9561 union lpfc_sli4_cfg_shdr *shdr;
9562
9563 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9564 if (!mbox)
9565 return -ENOMEM;
9566 length = (sizeof(struct lpfc_mbx_cq_create) -
9567 sizeof(struct lpfc_sli4_cfg_mhdr));
9568 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9569 LPFC_MBOX_OPCODE_CQ_CREATE,
9570 length, LPFC_SLI4_MBX_EMBED);
9571 cq_create = &mbox->u.mqe.un.cq_create;
9572 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9573 cq->page_count);
9574 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9575 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9576 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9577 switch (cq->entry_count) {
9578 default:
9579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9580 "0361 Unsupported CQ count. (%d)\n",
9581 cq->entry_count);
9582 if (cq->entry_count < 256)
9583 return -EINVAL;
9584 /* otherwise default to smallest count (drop through) */
9585 case 256:
9586 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9587 LPFC_CQ_CNT_256);
9588 break;
9589 case 512:
9590 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9591 LPFC_CQ_CNT_512);
9592 break;
9593 case 1024:
9594 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9595 LPFC_CQ_CNT_1024);
9596 break;
9597 }
9598 list_for_each_entry(dmabuf, &cq->page_list, list) {
9599 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9600 putPaddrLow(dmabuf->phys);
9601 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9602 putPaddrHigh(dmabuf->phys);
9603 }
9604 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9605
9606 /* The IOCTL status is embedded in the mailbox subheader. */
9607 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9608 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9609 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9610 if (shdr_status || shdr_add_status || rc) {
9611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9612 "2501 CQ_CREATE mailbox failed with "
9613 "status x%x add_status x%x, mbx status x%x\n",
9614 shdr_status, shdr_add_status, rc);
9615 status = -ENXIO;
9616 goto out;
9617 }
9618 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9619 if (cq->queue_id == 0xFFFF) {
9620 status = -ENXIO;
9621 goto out;
9622 }
9623 /* link the cq onto the parent eq child list */
9624 list_add_tail(&cq->list, &eq->child_list);
9625 /* Set up completion queue's type and subtype */
9626 cq->type = type;
9627 cq->subtype = subtype;
9628 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9629 cq->host_index = 0;
9630 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009631
James Smart8fa38512009-07-19 10:01:03 -04009632out:
9633 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009634 return status;
9635}
9636
9637/**
James Smart04c68492009-05-22 14:52:52 -04009638 * lpfc_mq_create - Create a mailbox Queue on the HBA
9639 * @phba: HBA structure that indicates port to create a queue on.
9640 * @mq: The queue structure to use to create the mailbox queue.
9641 *
9642 * This function creates a mailbox queue, as detailed in @mq, on a port,
9643 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9644 *
9645 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9646 * is used to get the entry count and entry size that are necessary to
9647 * determine the number of pages to allocate and use for this queue. This
9648 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9649 * mailbox queue. This function is asynchronous and will wait for the mailbox
9650 * command to finish before continuing.
9651 *
9652 * On success this function will return a zero. If unable to allocate enough
9653 * memory this function will return ENOMEM. If the queue create mailbox command
9654 * fails this function will return ENXIO.
9655 **/
9656uint32_t
9657lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9658 struct lpfc_queue *cq, uint32_t subtype)
9659{
9660 struct lpfc_mbx_mq_create *mq_create;
9661 struct lpfc_dmabuf *dmabuf;
9662 LPFC_MBOXQ_t *mbox;
9663 int rc, length, status = 0;
9664 uint32_t shdr_status, shdr_add_status;
9665 union lpfc_sli4_cfg_shdr *shdr;
9666
9667 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9668 if (!mbox)
9669 return -ENOMEM;
9670 length = (sizeof(struct lpfc_mbx_mq_create) -
9671 sizeof(struct lpfc_sli4_cfg_mhdr));
9672 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9673 LPFC_MBOX_OPCODE_MQ_CREATE,
9674 length, LPFC_SLI4_MBX_EMBED);
9675 mq_create = &mbox->u.mqe.un.mq_create;
9676 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9677 mq->page_count);
9678 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9679 cq->queue_id);
9680 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9681 switch (mq->entry_count) {
9682 default:
9683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9684 "0362 Unsupported MQ count. (%d)\n",
9685 mq->entry_count);
9686 if (mq->entry_count < 16)
9687 return -EINVAL;
9688 /* otherwise default to smallest count (drop through) */
9689 case 16:
9690 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9691 LPFC_MQ_CNT_16);
9692 break;
9693 case 32:
9694 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9695 LPFC_MQ_CNT_32);
9696 break;
9697 case 64:
9698 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9699 LPFC_MQ_CNT_64);
9700 break;
9701 case 128:
9702 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9703 LPFC_MQ_CNT_128);
9704 break;
9705 }
9706 list_for_each_entry(dmabuf, &mq->page_list, list) {
9707 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9708 putPaddrLow(dmabuf->phys);
9709 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9710 putPaddrHigh(dmabuf->phys);
9711 }
9712 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9713 /* The IOCTL status is embedded in the mailbox subheader. */
9714 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9715 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9716 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9717 if (shdr_status || shdr_add_status || rc) {
9718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9719 "2502 MQ_CREATE mailbox failed with "
9720 "status x%x add_status x%x, mbx status x%x\n",
9721 shdr_status, shdr_add_status, rc);
9722 status = -ENXIO;
9723 goto out;
9724 }
9725 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9726 if (mq->queue_id == 0xFFFF) {
9727 status = -ENXIO;
9728 goto out;
9729 }
9730 mq->type = LPFC_MQ;
9731 mq->subtype = subtype;
9732 mq->host_index = 0;
9733 mq->hba_index = 0;
9734
9735 /* link the mq onto the parent cq child list */
9736 list_add_tail(&mq->list, &cq->child_list);
9737out:
James Smart8fa38512009-07-19 10:01:03 -04009738 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009739 return status;
9740}
9741
9742/**
James Smart4f774512009-05-22 14:52:35 -04009743 * lpfc_wq_create - Create a Work Queue on the HBA
9744 * @phba: HBA structure that indicates port to create a queue on.
9745 * @wq: The queue structure to use to create the work queue.
9746 * @cq: The completion queue to bind this work queue to.
9747 * @subtype: The subtype of the work queue indicating its functionality.
9748 *
9749 * This function creates a work queue, as detailed in @wq, on a port, described
9750 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9751 *
9752 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9753 * is used to get the entry count and entry size that are necessary to
9754 * determine the number of pages to allocate and use for this queue. The @cq
9755 * is used to indicate which completion queue to bind this work queue to. This
9756 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9757 * work queue. This function is asynchronous and will wait for the mailbox
9758 * command to finish before continuing.
9759 *
9760 * On success this function will return a zero. If unable to allocate enough
9761 * memory this function will return ENOMEM. If the queue create mailbox command
9762 * fails this function will return ENXIO.
9763 **/
9764uint32_t
9765lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9766 struct lpfc_queue *cq, uint32_t subtype)
9767{
9768 struct lpfc_mbx_wq_create *wq_create;
9769 struct lpfc_dmabuf *dmabuf;
9770 LPFC_MBOXQ_t *mbox;
9771 int rc, length, status = 0;
9772 uint32_t shdr_status, shdr_add_status;
9773 union lpfc_sli4_cfg_shdr *shdr;
9774
9775 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9776 if (!mbox)
9777 return -ENOMEM;
9778 length = (sizeof(struct lpfc_mbx_wq_create) -
9779 sizeof(struct lpfc_sli4_cfg_mhdr));
9780 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9781 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9782 length, LPFC_SLI4_MBX_EMBED);
9783 wq_create = &mbox->u.mqe.un.wq_create;
9784 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9785 wq->page_count);
9786 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9787 cq->queue_id);
9788 list_for_each_entry(dmabuf, &wq->page_list, list) {
9789 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9790 putPaddrLow(dmabuf->phys);
9791 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9792 putPaddrHigh(dmabuf->phys);
9793 }
9794 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9795 /* The IOCTL status is embedded in the mailbox subheader. */
9796 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9797 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9798 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9799 if (shdr_status || shdr_add_status || rc) {
9800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9801 "2503 WQ_CREATE mailbox failed with "
9802 "status x%x add_status x%x, mbx status x%x\n",
9803 shdr_status, shdr_add_status, rc);
9804 status = -ENXIO;
9805 goto out;
9806 }
9807 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9808 if (wq->queue_id == 0xFFFF) {
9809 status = -ENXIO;
9810 goto out;
9811 }
9812 wq->type = LPFC_WQ;
9813 wq->subtype = subtype;
9814 wq->host_index = 0;
9815 wq->hba_index = 0;
9816
9817 /* link the wq onto the parent cq child list */
9818 list_add_tail(&wq->list, &cq->child_list);
9819out:
James Smart8fa38512009-07-19 10:01:03 -04009820 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009821 return status;
9822}
9823
9824/**
9825 * lpfc_rq_create - Create a Receive Queue on the HBA
9826 * @phba: HBA structure that indicates port to create a queue on.
9827 * @hrq: The queue structure to use to create the header receive queue.
9828 * @drq: The queue structure to use to create the data receive queue.
9829 * @cq: The completion queue to bind this work queue to.
9830 *
9831 * This function creates a receive buffer queue pair , as detailed in @hrq and
9832 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9833 * to the HBA.
9834 *
9835 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9836 * struct is used to get the entry count that is necessary to determine the
9837 * number of pages to use for this queue. The @cq is used to indicate which
9838 * completion queue to bind received buffers that are posted to these queues to.
9839 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9840 * receive queue pair. This function is asynchronous and will wait for the
9841 * mailbox command to finish before continuing.
9842 *
9843 * On success this function will return a zero. If unable to allocate enough
9844 * memory this function will return ENOMEM. If the queue create mailbox command
9845 * fails this function will return ENXIO.
9846 **/
9847uint32_t
9848lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9849 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9850{
9851 struct lpfc_mbx_rq_create *rq_create;
9852 struct lpfc_dmabuf *dmabuf;
9853 LPFC_MBOXQ_t *mbox;
9854 int rc, length, status = 0;
9855 uint32_t shdr_status, shdr_add_status;
9856 union lpfc_sli4_cfg_shdr *shdr;
9857
9858 if (hrq->entry_count != drq->entry_count)
9859 return -EINVAL;
9860 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9861 if (!mbox)
9862 return -ENOMEM;
9863 length = (sizeof(struct lpfc_mbx_rq_create) -
9864 sizeof(struct lpfc_sli4_cfg_mhdr));
9865 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9866 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9867 length, LPFC_SLI4_MBX_EMBED);
9868 rq_create = &mbox->u.mqe.un.rq_create;
9869 switch (hrq->entry_count) {
9870 default:
9871 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9872 "2535 Unsupported RQ count. (%d)\n",
9873 hrq->entry_count);
9874 if (hrq->entry_count < 512)
9875 return -EINVAL;
9876 /* otherwise default to smallest count (drop through) */
9877 case 512:
9878 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9879 LPFC_RQ_RING_SIZE_512);
9880 break;
9881 case 1024:
9882 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9883 LPFC_RQ_RING_SIZE_1024);
9884 break;
9885 case 2048:
9886 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9887 LPFC_RQ_RING_SIZE_2048);
9888 break;
9889 case 4096:
9890 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9891 LPFC_RQ_RING_SIZE_4096);
9892 break;
9893 }
9894 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9895 cq->queue_id);
9896 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9897 hrq->page_count);
9898 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9899 LPFC_HDR_BUF_SIZE);
9900 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9901 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9902 putPaddrLow(dmabuf->phys);
9903 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9904 putPaddrHigh(dmabuf->phys);
9905 }
9906 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9907 /* The IOCTL status is embedded in the mailbox subheader. */
9908 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9909 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9910 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9911 if (shdr_status || shdr_add_status || rc) {
9912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9913 "2504 RQ_CREATE mailbox failed with "
9914 "status x%x add_status x%x, mbx status x%x\n",
9915 shdr_status, shdr_add_status, rc);
9916 status = -ENXIO;
9917 goto out;
9918 }
9919 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9920 if (hrq->queue_id == 0xFFFF) {
9921 status = -ENXIO;
9922 goto out;
9923 }
9924 hrq->type = LPFC_HRQ;
9925 hrq->subtype = subtype;
9926 hrq->host_index = 0;
9927 hrq->hba_index = 0;
9928
9929 /* now create the data queue */
9930 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9931 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9932 length, LPFC_SLI4_MBX_EMBED);
9933 switch (drq->entry_count) {
9934 default:
9935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9936 "2536 Unsupported RQ count. (%d)\n",
9937 drq->entry_count);
9938 if (drq->entry_count < 512)
9939 return -EINVAL;
9940 /* otherwise default to smallest count (drop through) */
9941 case 512:
9942 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9943 LPFC_RQ_RING_SIZE_512);
9944 break;
9945 case 1024:
9946 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9947 LPFC_RQ_RING_SIZE_1024);
9948 break;
9949 case 2048:
9950 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9951 LPFC_RQ_RING_SIZE_2048);
9952 break;
9953 case 4096:
9954 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9955 LPFC_RQ_RING_SIZE_4096);
9956 break;
9957 }
9958 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9959 cq->queue_id);
9960 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9961 drq->page_count);
9962 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9963 LPFC_DATA_BUF_SIZE);
9964 list_for_each_entry(dmabuf, &drq->page_list, list) {
9965 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9966 putPaddrLow(dmabuf->phys);
9967 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9968 putPaddrHigh(dmabuf->phys);
9969 }
9970 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9971 /* The IOCTL status is embedded in the mailbox subheader. */
9972 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9973 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9974 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9975 if (shdr_status || shdr_add_status || rc) {
9976 status = -ENXIO;
9977 goto out;
9978 }
9979 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9980 if (drq->queue_id == 0xFFFF) {
9981 status = -ENXIO;
9982 goto out;
9983 }
9984 drq->type = LPFC_DRQ;
9985 drq->subtype = subtype;
9986 drq->host_index = 0;
9987 drq->hba_index = 0;
9988
9989 /* link the header and data RQs onto the parent cq child list */
9990 list_add_tail(&hrq->list, &cq->child_list);
9991 list_add_tail(&drq->list, &cq->child_list);
9992
9993out:
James Smart8fa38512009-07-19 10:01:03 -04009994 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009995 return status;
9996}
9997
9998/**
9999 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10000 * @eq: The queue structure associated with the queue to destroy.
10001 *
10002 * This function destroys a queue, as detailed in @eq by sending an mailbox
10003 * command, specific to the type of queue, to the HBA.
10004 *
10005 * The @eq struct is used to get the queue ID of the queue to destroy.
10006 *
10007 * On success this function will return a zero. If the queue destroy mailbox
10008 * command fails this function will return ENXIO.
10009 **/
10010uint32_t
10011lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10012{
10013 LPFC_MBOXQ_t *mbox;
10014 int rc, length, status = 0;
10015 uint32_t shdr_status, shdr_add_status;
10016 union lpfc_sli4_cfg_shdr *shdr;
10017
10018 if (!eq)
10019 return -ENODEV;
10020 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10021 if (!mbox)
10022 return -ENOMEM;
10023 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10024 sizeof(struct lpfc_sli4_cfg_mhdr));
10025 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10026 LPFC_MBOX_OPCODE_EQ_DESTROY,
10027 length, LPFC_SLI4_MBX_EMBED);
10028 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10029 eq->queue_id);
10030 mbox->vport = eq->phba->pport;
10031 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10032
10033 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10034 /* The IOCTL status is embedded in the mailbox subheader. */
10035 shdr = (union lpfc_sli4_cfg_shdr *)
10036 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10037 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10038 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10039 if (shdr_status || shdr_add_status || rc) {
10040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10041 "2505 EQ_DESTROY mailbox failed with "
10042 "status x%x add_status x%x, mbx status x%x\n",
10043 shdr_status, shdr_add_status, rc);
10044 status = -ENXIO;
10045 }
10046
10047 /* Remove eq from any list */
10048 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010049 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010050 return status;
10051}
10052
10053/**
10054 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10055 * @cq: The queue structure associated with the queue to destroy.
10056 *
10057 * This function destroys a queue, as detailed in @cq by sending an mailbox
10058 * command, specific to the type of queue, to the HBA.
10059 *
10060 * The @cq struct is used to get the queue ID of the queue to destroy.
10061 *
10062 * On success this function will return a zero. If the queue destroy mailbox
10063 * command fails this function will return ENXIO.
10064 **/
10065uint32_t
10066lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10067{
10068 LPFC_MBOXQ_t *mbox;
10069 int rc, length, status = 0;
10070 uint32_t shdr_status, shdr_add_status;
10071 union lpfc_sli4_cfg_shdr *shdr;
10072
10073 if (!cq)
10074 return -ENODEV;
10075 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10076 if (!mbox)
10077 return -ENOMEM;
10078 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10079 sizeof(struct lpfc_sli4_cfg_mhdr));
10080 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10081 LPFC_MBOX_OPCODE_CQ_DESTROY,
10082 length, LPFC_SLI4_MBX_EMBED);
10083 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10084 cq->queue_id);
10085 mbox->vport = cq->phba->pport;
10086 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10087 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10088 /* The IOCTL status is embedded in the mailbox subheader. */
10089 shdr = (union lpfc_sli4_cfg_shdr *)
10090 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10091 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10092 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10093 if (shdr_status || shdr_add_status || rc) {
10094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10095 "2506 CQ_DESTROY mailbox failed with "
10096 "status x%x add_status x%x, mbx status x%x\n",
10097 shdr_status, shdr_add_status, rc);
10098 status = -ENXIO;
10099 }
10100 /* Remove cq from any list */
10101 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010102 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010103 return status;
10104}
10105
10106/**
James Smart04c68492009-05-22 14:52:52 -040010107 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10108 * @qm: The queue structure associated with the queue to destroy.
10109 *
10110 * This function destroys a queue, as detailed in @mq by sending an mailbox
10111 * command, specific to the type of queue, to the HBA.
10112 *
10113 * The @mq struct is used to get the queue ID of the queue to destroy.
10114 *
10115 * On success this function will return a zero. If the queue destroy mailbox
10116 * command fails this function will return ENXIO.
10117 **/
10118uint32_t
10119lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10120{
10121 LPFC_MBOXQ_t *mbox;
10122 int rc, length, status = 0;
10123 uint32_t shdr_status, shdr_add_status;
10124 union lpfc_sli4_cfg_shdr *shdr;
10125
10126 if (!mq)
10127 return -ENODEV;
10128 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10129 if (!mbox)
10130 return -ENOMEM;
10131 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10132 sizeof(struct lpfc_sli4_cfg_mhdr));
10133 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10134 LPFC_MBOX_OPCODE_MQ_DESTROY,
10135 length, LPFC_SLI4_MBX_EMBED);
10136 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10137 mq->queue_id);
10138 mbox->vport = mq->phba->pport;
10139 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10140 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10141 /* The IOCTL status is embedded in the mailbox subheader. */
10142 shdr = (union lpfc_sli4_cfg_shdr *)
10143 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10144 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10145 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10146 if (shdr_status || shdr_add_status || rc) {
10147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10148 "2507 MQ_DESTROY mailbox failed with "
10149 "status x%x add_status x%x, mbx status x%x\n",
10150 shdr_status, shdr_add_status, rc);
10151 status = -ENXIO;
10152 }
10153 /* Remove mq from any list */
10154 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010155 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010156 return status;
10157}
10158
10159/**
James Smart4f774512009-05-22 14:52:35 -040010160 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10161 * @wq: The queue structure associated with the queue to destroy.
10162 *
10163 * This function destroys a queue, as detailed in @wq by sending an mailbox
10164 * command, specific to the type of queue, to the HBA.
10165 *
10166 * The @wq struct is used to get the queue ID of the queue to destroy.
10167 *
10168 * On success this function will return a zero. If the queue destroy mailbox
10169 * command fails this function will return ENXIO.
10170 **/
10171uint32_t
10172lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10173{
10174 LPFC_MBOXQ_t *mbox;
10175 int rc, length, status = 0;
10176 uint32_t shdr_status, shdr_add_status;
10177 union lpfc_sli4_cfg_shdr *shdr;
10178
10179 if (!wq)
10180 return -ENODEV;
10181 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10182 if (!mbox)
10183 return -ENOMEM;
10184 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10185 sizeof(struct lpfc_sli4_cfg_mhdr));
10186 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10187 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10188 length, LPFC_SLI4_MBX_EMBED);
10189 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10190 wq->queue_id);
10191 mbox->vport = wq->phba->pport;
10192 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10193 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10194 shdr = (union lpfc_sli4_cfg_shdr *)
10195 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10196 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10197 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10198 if (shdr_status || shdr_add_status || rc) {
10199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10200 "2508 WQ_DESTROY mailbox failed with "
10201 "status x%x add_status x%x, mbx status x%x\n",
10202 shdr_status, shdr_add_status, rc);
10203 status = -ENXIO;
10204 }
10205 /* Remove wq from any list */
10206 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010207 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010208 return status;
10209}
10210
10211/**
10212 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10213 * @rq: The queue structure associated with the queue to destroy.
10214 *
10215 * This function destroys a queue, as detailed in @rq by sending an mailbox
10216 * command, specific to the type of queue, to the HBA.
10217 *
10218 * The @rq struct is used to get the queue ID of the queue to destroy.
10219 *
10220 * On success this function will return a zero. If the queue destroy mailbox
10221 * command fails this function will return ENXIO.
10222 **/
10223uint32_t
10224lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10225 struct lpfc_queue *drq)
10226{
10227 LPFC_MBOXQ_t *mbox;
10228 int rc, length, status = 0;
10229 uint32_t shdr_status, shdr_add_status;
10230 union lpfc_sli4_cfg_shdr *shdr;
10231
10232 if (!hrq || !drq)
10233 return -ENODEV;
10234 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10235 if (!mbox)
10236 return -ENOMEM;
10237 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10238 sizeof(struct mbox_header));
10239 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10240 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10241 length, LPFC_SLI4_MBX_EMBED);
10242 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10243 hrq->queue_id);
10244 mbox->vport = hrq->phba->pport;
10245 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10246 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10247 /* The IOCTL status is embedded in the mailbox subheader. */
10248 shdr = (union lpfc_sli4_cfg_shdr *)
10249 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10250 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10251 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10252 if (shdr_status || shdr_add_status || rc) {
10253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10254 "2509 RQ_DESTROY mailbox failed with "
10255 "status x%x add_status x%x, mbx status x%x\n",
10256 shdr_status, shdr_add_status, rc);
10257 if (rc != MBX_TIMEOUT)
10258 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10259 return -ENXIO;
10260 }
10261 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10262 drq->queue_id);
10263 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10264 shdr = (union lpfc_sli4_cfg_shdr *)
10265 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10266 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10267 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10268 if (shdr_status || shdr_add_status || rc) {
10269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10270 "2510 RQ_DESTROY mailbox failed with "
10271 "status x%x add_status x%x, mbx status x%x\n",
10272 shdr_status, shdr_add_status, rc);
10273 status = -ENXIO;
10274 }
10275 list_del_init(&hrq->list);
10276 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010277 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010278 return status;
10279}
10280
10281/**
10282 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10283 * @phba: The virtual port for which this call being executed.
10284 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10285 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10286 * @xritag: the xritag that ties this io to the SGL pages.
10287 *
10288 * This routine will post the sgl pages for the IO that has the xritag
10289 * that is in the iocbq structure. The xritag is assigned during iocbq
10290 * creation and persists for as long as the driver is loaded.
10291 * if the caller has fewer than 256 scatter gather segments to map then
10292 * pdma_phys_addr1 should be 0.
10293 * If the caller needs to map more than 256 scatter gather segment then
10294 * pdma_phys_addr1 should be a valid physical address.
10295 * physical address for SGLs must be 64 byte aligned.
10296 * If you are going to map 2 SGL's then the first one must have 256 entries
10297 * the second sgl can have between 1 and 256 entries.
10298 *
10299 * Return codes:
10300 * 0 - Success
10301 * -ENXIO, -ENOMEM - Failure
10302 **/
10303int
10304lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10305 dma_addr_t pdma_phys_addr0,
10306 dma_addr_t pdma_phys_addr1,
10307 uint16_t xritag)
10308{
10309 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10310 LPFC_MBOXQ_t *mbox;
10311 int rc;
10312 uint32_t shdr_status, shdr_add_status;
10313 union lpfc_sli4_cfg_shdr *shdr;
10314
10315 if (xritag == NO_XRI) {
10316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10317 "0364 Invalid param:\n");
10318 return -EINVAL;
10319 }
10320
10321 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10322 if (!mbox)
10323 return -ENOMEM;
10324
10325 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10326 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10327 sizeof(struct lpfc_mbx_post_sgl_pages) -
10328 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10329
10330 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10331 &mbox->u.mqe.un.post_sgl_pages;
10332 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10333 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10334
10335 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10336 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10337 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10338 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10339
10340 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10341 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10342 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10343 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10344 if (!phba->sli4_hba.intr_enable)
10345 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10346 else
10347 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10348 /* The IOCTL status is embedded in the mailbox subheader. */
10349 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10350 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10351 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10352 if (rc != MBX_TIMEOUT)
10353 mempool_free(mbox, phba->mbox_mem_pool);
10354 if (shdr_status || shdr_add_status || rc) {
10355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10356 "2511 POST_SGL mailbox failed with "
10357 "status x%x add_status x%x, mbx status x%x\n",
10358 shdr_status, shdr_add_status, rc);
10359 rc = -ENXIO;
10360 }
10361 return 0;
10362}
10363/**
10364 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10365 * @phba: The virtual port for which this call being executed.
10366 *
10367 * This routine will remove all of the sgl pages registered with the hba.
10368 *
10369 * Return codes:
10370 * 0 - Success
10371 * -ENXIO, -ENOMEM - Failure
10372 **/
10373int
10374lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10375{
10376 LPFC_MBOXQ_t *mbox;
10377 int rc;
10378 uint32_t shdr_status, shdr_add_status;
10379 union lpfc_sli4_cfg_shdr *shdr;
10380
10381 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10382 if (!mbox)
10383 return -ENOMEM;
10384
10385 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10386 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10387 LPFC_SLI4_MBX_EMBED);
10388 if (!phba->sli4_hba.intr_enable)
10389 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10390 else
10391 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10392 /* The IOCTL status is embedded in the mailbox subheader. */
10393 shdr = (union lpfc_sli4_cfg_shdr *)
10394 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10395 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10397 if (rc != MBX_TIMEOUT)
10398 mempool_free(mbox, phba->mbox_mem_pool);
10399 if (shdr_status || shdr_add_status || rc) {
10400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10401 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10402 "status x%x add_status x%x, mbx status x%x\n",
10403 shdr_status, shdr_add_status, rc);
10404 rc = -ENXIO;
10405 }
10406 return rc;
10407}
10408
10409/**
10410 * lpfc_sli4_next_xritag - Get an xritag for the io
10411 * @phba: Pointer to HBA context object.
10412 *
10413 * This function gets an xritag for the iocb. If there is no unused xritag
10414 * it will return 0xffff.
10415 * The function returns the allocated xritag if successful, else returns zero.
10416 * Zero is not a valid xritag.
10417 * The caller is not required to hold any lock.
10418 **/
10419uint16_t
10420lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10421{
10422 uint16_t xritag;
10423
10424 spin_lock_irq(&phba->hbalock);
10425 xritag = phba->sli4_hba.next_xri;
10426 if ((xritag != (uint16_t) -1) && xritag <
10427 (phba->sli4_hba.max_cfg_param.max_xri
10428 + phba->sli4_hba.max_cfg_param.xri_base)) {
10429 phba->sli4_hba.next_xri++;
10430 phba->sli4_hba.max_cfg_param.xri_used++;
10431 spin_unlock_irq(&phba->hbalock);
10432 return xritag;
10433 }
10434 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010435 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010436 "2004 Failed to allocate XRI.last XRITAG is %d"
10437 " Max XRI is %d, Used XRI is %d\n",
10438 phba->sli4_hba.next_xri,
10439 phba->sli4_hba.max_cfg_param.max_xri,
10440 phba->sli4_hba.max_cfg_param.xri_used);
10441 return -1;
10442}
10443
10444/**
10445 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10446 * @phba: pointer to lpfc hba data structure.
10447 *
10448 * This routine is invoked to post a block of driver's sgl pages to the
10449 * HBA using non-embedded mailbox command. No Lock is held. This routine
10450 * is only called when the driver is loading and after all IO has been
10451 * stopped.
10452 **/
10453int
10454lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10455{
10456 struct lpfc_sglq *sglq_entry;
10457 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10458 struct sgl_page_pairs *sgl_pg_pairs;
10459 void *viraddr;
10460 LPFC_MBOXQ_t *mbox;
10461 uint32_t reqlen, alloclen, pg_pairs;
10462 uint32_t mbox_tmo;
10463 uint16_t xritag_start = 0;
10464 int els_xri_cnt, rc = 0;
10465 uint32_t shdr_status, shdr_add_status;
10466 union lpfc_sli4_cfg_shdr *shdr;
10467
10468 /* The number of sgls to be posted */
10469 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10470
10471 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10472 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10473 if (reqlen > PAGE_SIZE) {
10474 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10475 "2559 Block sgl registration required DMA "
10476 "size (%d) great than a page\n", reqlen);
10477 return -ENOMEM;
10478 }
10479 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10480 if (!mbox) {
10481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10482 "2560 Failed to allocate mbox cmd memory\n");
10483 return -ENOMEM;
10484 }
10485
10486 /* Allocate DMA memory and set up the non-embedded mailbox command */
10487 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10488 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10489 LPFC_SLI4_MBX_NEMBED);
10490
10491 if (alloclen < reqlen) {
10492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10493 "0285 Allocated DMA memory size (%d) is "
10494 "less than the requested DMA memory "
10495 "size (%d)\n", alloclen, reqlen);
10496 lpfc_sli4_mbox_cmd_free(phba, mbox);
10497 return -ENOMEM;
10498 }
James Smart4f774512009-05-22 14:52:35 -040010499 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010500 viraddr = mbox->sge_array->addr[0];
10501
10502 /* Set up the SGL pages in the non-embedded DMA pages */
10503 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10504 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10505
10506 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10507 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10508 /* Set up the sge entry */
10509 sgl_pg_pairs->sgl_pg0_addr_lo =
10510 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10511 sgl_pg_pairs->sgl_pg0_addr_hi =
10512 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10513 sgl_pg_pairs->sgl_pg1_addr_lo =
10514 cpu_to_le32(putPaddrLow(0));
10515 sgl_pg_pairs->sgl_pg1_addr_hi =
10516 cpu_to_le32(putPaddrHigh(0));
10517 /* Keep the first xritag on the list */
10518 if (pg_pairs == 0)
10519 xritag_start = sglq_entry->sli4_xritag;
10520 sgl_pg_pairs++;
10521 }
10522 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010523 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010524 /* Perform endian conversion if necessary */
10525 sgl->word0 = cpu_to_le32(sgl->word0);
10526
10527 if (!phba->sli4_hba.intr_enable)
10528 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10529 else {
10530 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10531 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10532 }
10533 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10534 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10535 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10536 if (rc != MBX_TIMEOUT)
10537 lpfc_sli4_mbox_cmd_free(phba, mbox);
10538 if (shdr_status || shdr_add_status || rc) {
10539 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10540 "2513 POST_SGL_BLOCK mailbox command failed "
10541 "status x%x add_status x%x mbx status x%x\n",
10542 shdr_status, shdr_add_status, rc);
10543 rc = -ENXIO;
10544 }
10545 return rc;
10546}
10547
10548/**
10549 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10550 * @phba: pointer to lpfc hba data structure.
10551 * @sblist: pointer to scsi buffer list.
10552 * @count: number of scsi buffers on the list.
10553 *
10554 * This routine is invoked to post a block of @count scsi sgl pages from a
10555 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10556 * No Lock is held.
10557 *
10558 **/
10559int
10560lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10561 int cnt)
10562{
10563 struct lpfc_scsi_buf *psb;
10564 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10565 struct sgl_page_pairs *sgl_pg_pairs;
10566 void *viraddr;
10567 LPFC_MBOXQ_t *mbox;
10568 uint32_t reqlen, alloclen, pg_pairs;
10569 uint32_t mbox_tmo;
10570 uint16_t xritag_start = 0;
10571 int rc = 0;
10572 uint32_t shdr_status, shdr_add_status;
10573 dma_addr_t pdma_phys_bpl1;
10574 union lpfc_sli4_cfg_shdr *shdr;
10575
10576 /* Calculate the requested length of the dma memory */
10577 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10578 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10579 if (reqlen > PAGE_SIZE) {
10580 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10581 "0217 Block sgl registration required DMA "
10582 "size (%d) great than a page\n", reqlen);
10583 return -ENOMEM;
10584 }
10585 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10586 if (!mbox) {
10587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10588 "0283 Failed to allocate mbox cmd memory\n");
10589 return -ENOMEM;
10590 }
10591
10592 /* Allocate DMA memory and set up the non-embedded mailbox command */
10593 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10594 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10595 LPFC_SLI4_MBX_NEMBED);
10596
10597 if (alloclen < reqlen) {
10598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10599 "2561 Allocated DMA memory size (%d) is "
10600 "less than the requested DMA memory "
10601 "size (%d)\n", alloclen, reqlen);
10602 lpfc_sli4_mbox_cmd_free(phba, mbox);
10603 return -ENOMEM;
10604 }
James Smart4f774512009-05-22 14:52:35 -040010605 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010606 viraddr = mbox->sge_array->addr[0];
10607
10608 /* Set up the SGL pages in the non-embedded DMA pages */
10609 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10610 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10611
10612 pg_pairs = 0;
10613 list_for_each_entry(psb, sblist, list) {
10614 /* Set up the sge entry */
10615 sgl_pg_pairs->sgl_pg0_addr_lo =
10616 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10617 sgl_pg_pairs->sgl_pg0_addr_hi =
10618 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10619 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10620 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10621 else
10622 pdma_phys_bpl1 = 0;
10623 sgl_pg_pairs->sgl_pg1_addr_lo =
10624 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10625 sgl_pg_pairs->sgl_pg1_addr_hi =
10626 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10627 /* Keep the first xritag on the list */
10628 if (pg_pairs == 0)
10629 xritag_start = psb->cur_iocbq.sli4_xritag;
10630 sgl_pg_pairs++;
10631 pg_pairs++;
10632 }
10633 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10634 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10635 /* Perform endian conversion if necessary */
10636 sgl->word0 = cpu_to_le32(sgl->word0);
10637
10638 if (!phba->sli4_hba.intr_enable)
10639 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10640 else {
10641 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10642 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10643 }
10644 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10645 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10647 if (rc != MBX_TIMEOUT)
10648 lpfc_sli4_mbox_cmd_free(phba, mbox);
10649 if (shdr_status || shdr_add_status || rc) {
10650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10651 "2564 POST_SGL_BLOCK mailbox command failed "
10652 "status x%x add_status x%x mbx status x%x\n",
10653 shdr_status, shdr_add_status, rc);
10654 rc = -ENXIO;
10655 }
10656 return rc;
10657}
10658
10659/**
10660 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10661 * @phba: pointer to lpfc_hba struct that the frame was received on
10662 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10663 *
10664 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10665 * valid type of frame that the LPFC driver will handle. This function will
10666 * return a zero if the frame is a valid frame or a non zero value when the
10667 * frame does not pass the check.
10668 **/
10669static int
10670lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10671{
10672 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10673 char *type_names[] = FC_TYPE_NAMES_INIT;
10674 struct fc_vft_header *fc_vft_hdr;
10675
10676 switch (fc_hdr->fh_r_ctl) {
10677 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10678 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10679 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10680 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10681 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10682 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10683 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10684 case FC_RCTL_DD_CMD_STATUS: /* command status */
10685 case FC_RCTL_ELS_REQ: /* extended link services request */
10686 case FC_RCTL_ELS_REP: /* extended link services reply */
10687 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10688 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10689 case FC_RCTL_BA_NOP: /* basic link service NOP */
10690 case FC_RCTL_BA_ABTS: /* basic link service abort */
10691 case FC_RCTL_BA_RMC: /* remove connection */
10692 case FC_RCTL_BA_ACC: /* basic accept */
10693 case FC_RCTL_BA_RJT: /* basic reject */
10694 case FC_RCTL_BA_PRMT:
10695 case FC_RCTL_ACK_1: /* acknowledge_1 */
10696 case FC_RCTL_ACK_0: /* acknowledge_0 */
10697 case FC_RCTL_P_RJT: /* port reject */
10698 case FC_RCTL_F_RJT: /* fabric reject */
10699 case FC_RCTL_P_BSY: /* port busy */
10700 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10701 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10702 case FC_RCTL_LCR: /* link credit reset */
10703 case FC_RCTL_END: /* end */
10704 break;
10705 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10706 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10707 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10708 return lpfc_fc_frame_check(phba, fc_hdr);
10709 default:
10710 goto drop;
10711 }
10712 switch (fc_hdr->fh_type) {
10713 case FC_TYPE_BLS:
10714 case FC_TYPE_ELS:
10715 case FC_TYPE_FCP:
10716 case FC_TYPE_CT:
10717 break;
10718 case FC_TYPE_IP:
10719 case FC_TYPE_ILS:
10720 default:
10721 goto drop;
10722 }
10723 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10724 "2538 Received frame rctl:%s type:%s\n",
10725 rctl_names[fc_hdr->fh_r_ctl],
10726 type_names[fc_hdr->fh_type]);
10727 return 0;
10728drop:
10729 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10730 "2539 Dropped frame rctl:%s type:%s\n",
10731 rctl_names[fc_hdr->fh_r_ctl],
10732 type_names[fc_hdr->fh_type]);
10733 return 1;
10734}
10735
10736/**
10737 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10738 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10739 *
10740 * This function processes the FC header to retrieve the VFI from the VF
10741 * header, if one exists. This function will return the VFI if one exists
10742 * or 0 if no VSAN Header exists.
10743 **/
10744static uint32_t
10745lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10746{
10747 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10748
10749 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10750 return 0;
10751 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10752}
10753
10754/**
10755 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10756 * @phba: Pointer to the HBA structure to search for the vport on
10757 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10758 * @fcfi: The FC Fabric ID that the frame came from
10759 *
10760 * This function searches the @phba for a vport that matches the content of the
10761 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10762 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10763 * returns the matching vport pointer or NULL if unable to match frame to a
10764 * vport.
10765 **/
10766static struct lpfc_vport *
10767lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10768 uint16_t fcfi)
10769{
10770 struct lpfc_vport **vports;
10771 struct lpfc_vport *vport = NULL;
10772 int i;
10773 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10774 fc_hdr->fh_d_id[1] << 8 |
10775 fc_hdr->fh_d_id[2]);
10776
10777 vports = lpfc_create_vport_work_array(phba);
10778 if (vports != NULL)
10779 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10780 if (phba->fcf.fcfi == fcfi &&
10781 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10782 vports[i]->fc_myDID == did) {
10783 vport = vports[i];
10784 break;
10785 }
10786 }
10787 lpfc_destroy_vport_work_array(phba, vports);
10788 return vport;
10789}
10790
10791/**
James Smart45ed1192009-10-02 15:17:02 -040010792 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10793 * @vport: The vport to work on.
10794 *
10795 * This function updates the receive sequence time stamp for this vport. The
10796 * receive sequence time stamp indicates the time that the last frame of the
10797 * the sequence that has been idle for the longest amount of time was received.
10798 * the driver uses this time stamp to indicate if any received sequences have
10799 * timed out.
10800 **/
10801void
10802lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10803{
10804 struct lpfc_dmabuf *h_buf;
10805 struct hbq_dmabuf *dmabuf = NULL;
10806
10807 /* get the oldest sequence on the rcv list */
10808 h_buf = list_get_first(&vport->rcv_buffer_list,
10809 struct lpfc_dmabuf, list);
10810 if (!h_buf)
10811 return;
10812 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10813 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10814}
10815
10816/**
10817 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10818 * @vport: The vport that the received sequences were sent to.
10819 *
10820 * This function cleans up all outstanding received sequences. This is called
10821 * by the driver when a link event or user action invalidates all the received
10822 * sequences.
10823 **/
10824void
10825lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10826{
10827 struct lpfc_dmabuf *h_buf, *hnext;
10828 struct lpfc_dmabuf *d_buf, *dnext;
10829 struct hbq_dmabuf *dmabuf = NULL;
10830
10831 /* start with the oldest sequence on the rcv list */
10832 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10833 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10834 list_del_init(&dmabuf->hbuf.list);
10835 list_for_each_entry_safe(d_buf, dnext,
10836 &dmabuf->dbuf.list, list) {
10837 list_del_init(&d_buf->list);
10838 lpfc_in_buf_free(vport->phba, d_buf);
10839 }
10840 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10841 }
10842}
10843
10844/**
10845 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10846 * @vport: The vport that the received sequences were sent to.
10847 *
10848 * This function determines whether any received sequences have timed out by
10849 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
10850 * indicates that there is at least one timed out sequence this routine will
10851 * go through the received sequences one at a time from most inactive to most
10852 * active to determine which ones need to be cleaned up. Once it has determined
10853 * that a sequence needs to be cleaned up it will simply free up the resources
10854 * without sending an abort.
10855 **/
10856void
10857lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
10858{
10859 struct lpfc_dmabuf *h_buf, *hnext;
10860 struct lpfc_dmabuf *d_buf, *dnext;
10861 struct hbq_dmabuf *dmabuf = NULL;
10862 unsigned long timeout;
10863 int abort_count = 0;
10864
10865 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10866 vport->rcv_buffer_time_stamp);
10867 if (list_empty(&vport->rcv_buffer_list) ||
10868 time_before(jiffies, timeout))
10869 return;
10870 /* start with the oldest sequence on the rcv list */
10871 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10872 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10873 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10874 dmabuf->time_stamp);
10875 if (time_before(jiffies, timeout))
10876 break;
10877 abort_count++;
10878 list_del_init(&dmabuf->hbuf.list);
10879 list_for_each_entry_safe(d_buf, dnext,
10880 &dmabuf->dbuf.list, list) {
10881 list_del_init(&d_buf->list);
10882 lpfc_in_buf_free(vport->phba, d_buf);
10883 }
10884 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10885 }
10886 if (abort_count)
10887 lpfc_update_rcv_time_stamp(vport);
10888}
10889
10890/**
James Smart4f774512009-05-22 14:52:35 -040010891 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10892 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10893 *
10894 * This function searches through the existing incomplete sequences that have
10895 * been sent to this @vport. If the frame matches one of the incomplete
10896 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10897 * make up that sequence. If no sequence is found that matches this frame then
10898 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10899 * This function returns a pointer to the first dmabuf in the sequence list that
10900 * the frame was linked to.
10901 **/
10902static struct hbq_dmabuf *
10903lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10904{
10905 struct fc_frame_header *new_hdr;
10906 struct fc_frame_header *temp_hdr;
10907 struct lpfc_dmabuf *d_buf;
10908 struct lpfc_dmabuf *h_buf;
10909 struct hbq_dmabuf *seq_dmabuf = NULL;
10910 struct hbq_dmabuf *temp_dmabuf = NULL;
10911
James Smart4d9ab992009-10-02 15:16:39 -040010912 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040010913 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010914 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10915 /* Use the hdr_buf to find the sequence that this frame belongs to */
10916 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10917 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10918 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10919 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10920 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10921 continue;
10922 /* found a pending sequence that matches this frame */
10923 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10924 break;
10925 }
10926 if (!seq_dmabuf) {
10927 /*
10928 * This indicates first frame received for this sequence.
10929 * Queue the buffer on the vport's rcv_buffer_list.
10930 */
10931 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040010932 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040010933 return dmabuf;
10934 }
10935 temp_hdr = seq_dmabuf->hbuf.virt;
10936 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
James Smart4d9ab992009-10-02 15:16:39 -040010937 list_del_init(&seq_dmabuf->hbuf.list);
10938 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10939 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040010940 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040010941 return dmabuf;
10942 }
James Smart45ed1192009-10-02 15:17:02 -040010943 /* move this sequence to the tail to indicate a young sequence */
10944 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
10945 seq_dmabuf->time_stamp = jiffies;
10946 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040010947 /* find the correct place in the sequence to insert this frame */
10948 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10949 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10950 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10951 /*
10952 * If the frame's sequence count is greater than the frame on
10953 * the list then insert the frame right after this frame
10954 */
10955 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10956 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10957 return seq_dmabuf;
10958 }
10959 }
10960 return NULL;
10961}
10962
10963/**
James Smart6669f9b2009-10-02 15:16:45 -040010964 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
10965 * @vport: pointer to a vitural port
10966 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10967 *
10968 * This function tries to abort from the partially assembed sequence, described
10969 * by the information from basic abbort @dmabuf. It checks to see whether such
10970 * partially assembled sequence held by the driver. If so, it shall free up all
10971 * the frames from the partially assembled sequence.
10972 *
10973 * Return
10974 * true -- if there is matching partially assembled sequence present and all
10975 * the frames freed with the sequence;
10976 * false -- if there is no matching partially assembled sequence present so
10977 * nothing got aborted in the lower layer driver
10978 **/
10979static bool
10980lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
10981 struct hbq_dmabuf *dmabuf)
10982{
10983 struct fc_frame_header *new_hdr;
10984 struct fc_frame_header *temp_hdr;
10985 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
10986 struct hbq_dmabuf *seq_dmabuf = NULL;
10987
10988 /* Use the hdr_buf to find the sequence that matches this frame */
10989 INIT_LIST_HEAD(&dmabuf->dbuf.list);
10990 INIT_LIST_HEAD(&dmabuf->hbuf.list);
10991 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10992 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10993 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10994 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10995 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10996 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10997 continue;
10998 /* found a pending sequence that matches this frame */
10999 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11000 break;
11001 }
11002
11003 /* Free up all the frames from the partially assembled sequence */
11004 if (seq_dmabuf) {
11005 list_for_each_entry_safe(d_buf, n_buf,
11006 &seq_dmabuf->dbuf.list, list) {
11007 list_del_init(&d_buf->list);
11008 lpfc_in_buf_free(vport->phba, d_buf);
11009 }
11010 return true;
11011 }
11012 return false;
11013}
11014
11015/**
11016 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11017 * @phba: Pointer to HBA context object.
11018 * @cmd_iocbq: pointer to the command iocbq structure.
11019 * @rsp_iocbq: pointer to the response iocbq structure.
11020 *
11021 * This function handles the sequence abort accept iocb command complete
11022 * event. It properly releases the memory allocated to the sequence abort
11023 * accept iocb.
11024 **/
11025static void
11026lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11027 struct lpfc_iocbq *cmd_iocbq,
11028 struct lpfc_iocbq *rsp_iocbq)
11029{
11030 if (cmd_iocbq)
11031 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11032}
11033
11034/**
11035 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11036 * @phba: Pointer to HBA context object.
11037 * @fc_hdr: pointer to a FC frame header.
11038 *
11039 * This function sends a basic accept to a previous unsol sequence abort
11040 * event after aborting the sequence handling.
11041 **/
11042static void
11043lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11044 struct fc_frame_header *fc_hdr)
11045{
11046 struct lpfc_iocbq *ctiocb = NULL;
11047 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011048 uint16_t oxid, rxid;
11049 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011050 IOCB_t *icmd;
11051
11052 if (!lpfc_is_link_up(phba))
11053 return;
11054
11055 sid = sli4_sid_from_fc_hdr(fc_hdr);
11056 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011057 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011058
11059 ndlp = lpfc_findnode_did(phba->pport, sid);
11060 if (!ndlp) {
11061 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11062 "1268 Find ndlp returned NULL for oxid:x%x "
11063 "SID:x%x\n", oxid, sid);
11064 return;
11065 }
11066
11067 /* Allocate buffer for acc iocb */
11068 ctiocb = lpfc_sli_get_iocbq(phba);
11069 if (!ctiocb)
11070 return;
11071
James Smart5ffc2662009-11-18 15:39:44 -050011072 /* Extract the F_CTL field from FC_HDR */
11073 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11074
James Smart6669f9b2009-10-02 15:16:45 -040011075 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011076 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011077 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011078 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11079 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11080 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11081
11082 /* Fill in the rest of iocb fields */
11083 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11084 icmd->ulpBdeCount = 0;
11085 icmd->ulpLe = 1;
11086 icmd->ulpClass = CLASS3;
11087 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011088
James Smart6669f9b2009-10-02 15:16:45 -040011089 ctiocb->iocb_cmpl = NULL;
11090 ctiocb->vport = phba->pport;
11091 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11092
James Smart5ffc2662009-11-18 15:39:44 -050011093 if (fctl & FC_FC_EX_CTX) {
11094 /* ABTS sent by responder to CT exchange, construction
11095 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11096 * field and RX_ID from ABTS for RX_ID field.
11097 */
11098 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11099 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11100 ctiocb->sli4_xritag = oxid;
11101 } else {
11102 /* ABTS sent by initiator to CT exchange, construction
11103 * of BA_ACC will need to allocate a new XRI as for the
11104 * XRI_TAG and RX_ID fields.
11105 */
11106 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11107 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11108 ctiocb->sli4_xritag = NO_XRI;
11109 }
11110 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11111
James Smart6669f9b2009-10-02 15:16:45 -040011112 /* Xmit CT abts accept on exchange <xid> */
11113 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11114 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11115 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11116 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11117}
11118
11119/**
11120 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11121 * @vport: Pointer to the vport on which this sequence was received
11122 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11123 *
11124 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11125 * receive sequence is only partially assembed by the driver, it shall abort
11126 * the partially assembled frames for the sequence. Otherwise, if the
11127 * unsolicited receive sequence has been completely assembled and passed to
11128 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11129 * unsolicited sequence has been aborted. After that, it will issue a basic
11130 * accept to accept the abort.
11131 **/
11132void
11133lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11134 struct hbq_dmabuf *dmabuf)
11135{
11136 struct lpfc_hba *phba = vport->phba;
11137 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011138 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011139 bool abts_par;
11140
James Smart6669f9b2009-10-02 15:16:45 -040011141 /* Make a copy of fc_hdr before the dmabuf being released */
11142 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011143 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011144
James Smart5ffc2662009-11-18 15:39:44 -050011145 if (fctl & FC_FC_EX_CTX) {
11146 /*
11147 * ABTS sent by responder to exchange, just free the buffer
11148 */
James Smart6669f9b2009-10-02 15:16:45 -040011149 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011150 } else {
11151 /*
11152 * ABTS sent by initiator to exchange, need to do cleanup
11153 */
11154 /* Try to abort partially assembled seq */
11155 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11156
11157 /* Send abort to ULP if partially seq abort failed */
11158 if (abts_par == false)
11159 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11160 else
11161 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11162 }
James Smart6669f9b2009-10-02 15:16:45 -040011163 /* Send basic accept (BA_ACC) to the abort requester */
11164 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11165}
11166
11167/**
James Smart4f774512009-05-22 14:52:35 -040011168 * lpfc_seq_complete - Indicates if a sequence is complete
11169 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11170 *
11171 * This function checks the sequence, starting with the frame described by
11172 * @dmabuf, to see if all the frames associated with this sequence are present.
11173 * the frames associated with this sequence are linked to the @dmabuf using the
11174 * dbuf list. This function looks for two major things. 1) That the first frame
11175 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11176 * set. 3) That there are no holes in the sequence count. The function will
11177 * return 1 when the sequence is complete, otherwise it will return 0.
11178 **/
11179static int
11180lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11181{
11182 struct fc_frame_header *hdr;
11183 struct lpfc_dmabuf *d_buf;
11184 struct hbq_dmabuf *seq_dmabuf;
11185 uint32_t fctl;
11186 int seq_count = 0;
11187
11188 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11189 /* make sure first fame of sequence has a sequence count of zero */
11190 if (hdr->fh_seq_cnt != seq_count)
11191 return 0;
11192 fctl = (hdr->fh_f_ctl[0] << 16 |
11193 hdr->fh_f_ctl[1] << 8 |
11194 hdr->fh_f_ctl[2]);
11195 /* If last frame of sequence we can return success. */
11196 if (fctl & FC_FC_END_SEQ)
11197 return 1;
11198 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11199 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11200 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11201 /* If there is a hole in the sequence count then fail. */
11202 if (++seq_count != hdr->fh_seq_cnt)
11203 return 0;
11204 fctl = (hdr->fh_f_ctl[0] << 16 |
11205 hdr->fh_f_ctl[1] << 8 |
11206 hdr->fh_f_ctl[2]);
11207 /* If last frame of sequence we can return success. */
11208 if (fctl & FC_FC_END_SEQ)
11209 return 1;
11210 }
11211 return 0;
11212}
11213
11214/**
11215 * lpfc_prep_seq - Prep sequence for ULP processing
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 takes a sequence, described by a list of frames, and creates
11220 * a list of iocbq structures to describe the sequence. This iocbq list will be
11221 * used to issue to the generic unsolicited sequence handler. This routine
11222 * returns a pointer to the first iocbq in the list. If the function is unable
11223 * to allocate an iocbq then it throw out the received frames that were not
11224 * able to be described and return a pointer to the first iocbq. If unable to
11225 * allocate any iocbqs (including the first) this function will return NULL.
11226 **/
11227static struct lpfc_iocbq *
11228lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11229{
11230 struct lpfc_dmabuf *d_buf, *n_buf;
11231 struct lpfc_iocbq *first_iocbq, *iocbq;
11232 struct fc_frame_header *fc_hdr;
11233 uint32_t sid;
11234
11235 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11236 /* remove from receive buffer list */
11237 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011238 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011239 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011240 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011241 /* Get an iocbq struct to fill in. */
11242 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11243 if (first_iocbq) {
11244 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011245 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011246 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11247 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11248 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11249 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11250 vport->vpi + vport->phba->vpi_base;
11251 /* put the first buffer into the first IOCBq */
11252 first_iocbq->context2 = &seq_dmabuf->dbuf;
11253 first_iocbq->context3 = NULL;
11254 first_iocbq->iocb.ulpBdeCount = 1;
11255 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11256 LPFC_DATA_BUF_SIZE;
11257 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011258 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011259 bf_get(lpfc_rcqe_length,
11260 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011261 }
11262 iocbq = first_iocbq;
11263 /*
11264 * Each IOCBq can have two Buffers assigned, so go through the list
11265 * of buffers for this sequence and save two buffers in each IOCBq
11266 */
11267 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11268 if (!iocbq) {
11269 lpfc_in_buf_free(vport->phba, d_buf);
11270 continue;
11271 }
11272 if (!iocbq->context3) {
11273 iocbq->context3 = d_buf;
11274 iocbq->iocb.ulpBdeCount++;
11275 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
11276 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011277 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011278 bf_get(lpfc_rcqe_length,
11279 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011280 } else {
11281 iocbq = lpfc_sli_get_iocbq(vport->phba);
11282 if (!iocbq) {
11283 if (first_iocbq) {
11284 first_iocbq->iocb.ulpStatus =
11285 IOSTAT_FCP_RSP_ERROR;
11286 first_iocbq->iocb.un.ulpWord[4] =
11287 IOERR_NO_RESOURCES;
11288 }
11289 lpfc_in_buf_free(vport->phba, d_buf);
11290 continue;
11291 }
11292 iocbq->context2 = d_buf;
11293 iocbq->context3 = NULL;
11294 iocbq->iocb.ulpBdeCount = 1;
11295 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11296 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011297 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011298 bf_get(lpfc_rcqe_length,
11299 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011300 iocbq->iocb.un.rcvels.remoteID = sid;
11301 list_add_tail(&iocbq->list, &first_iocbq->list);
11302 }
11303 }
11304 return first_iocbq;
11305}
11306
James Smart6669f9b2009-10-02 15:16:45 -040011307static void
11308lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11309 struct hbq_dmabuf *seq_dmabuf)
11310{
11311 struct fc_frame_header *fc_hdr;
11312 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11313 struct lpfc_hba *phba = vport->phba;
11314
11315 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11316 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11317 if (!iocbq) {
11318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11319 "2707 Ring %d handler: Failed to allocate "
11320 "iocb Rctl x%x Type x%x received\n",
11321 LPFC_ELS_RING,
11322 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11323 return;
11324 }
11325 if (!lpfc_complete_unsol_iocb(phba,
11326 &phba->sli.ring[LPFC_ELS_RING],
11327 iocbq, fc_hdr->fh_r_ctl,
11328 fc_hdr->fh_type))
11329 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11330 "2540 Ring %d handler: unexpected Rctl "
11331 "x%x Type x%x received\n",
11332 LPFC_ELS_RING,
11333 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11334
11335 /* Free iocb created in lpfc_prep_seq */
11336 list_for_each_entry_safe(curr_iocb, next_iocb,
11337 &iocbq->list, list) {
11338 list_del_init(&curr_iocb->list);
11339 lpfc_sli_release_iocbq(phba, curr_iocb);
11340 }
11341 lpfc_sli_release_iocbq(phba, iocbq);
11342}
11343
James Smart4f774512009-05-22 14:52:35 -040011344/**
11345 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11346 * @phba: Pointer to HBA context object.
11347 *
11348 * This function is called with no lock held. This function processes all
11349 * the received buffers and gives it to upper layers when a received buffer
11350 * indicates that it is the final frame in the sequence. The interrupt
11351 * service routine processes received buffers at interrupt contexts and adds
11352 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11353 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11354 * appropriate receive function when the final frame in a sequence is received.
11355 **/
James Smart4d9ab992009-10-02 15:16:39 -040011356void
11357lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11358 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011359{
James Smart4d9ab992009-10-02 15:16:39 -040011360 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011361 struct fc_frame_header *fc_hdr;
11362 struct lpfc_vport *vport;
11363 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011364
James Smart4f774512009-05-22 14:52:35 -040011365 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011366 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11367 /* check to see if this a valid type of frame */
11368 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11369 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11370 return;
11371 }
11372 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11373 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011374 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011375 /* throw out the frame */
11376 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11377 return;
11378 }
James Smart6669f9b2009-10-02 15:16:45 -040011379 /* Handle the basic abort sequence (BA_ABTS) event */
11380 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11381 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11382 return;
11383 }
11384
James Smart4d9ab992009-10-02 15:16:39 -040011385 /* Link this frame */
11386 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11387 if (!seq_dmabuf) {
11388 /* unable to add frame to vport - throw it out */
11389 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11390 return;
11391 }
11392 /* If not last frame in sequence continue processing frames. */
11393 if (!lpfc_seq_complete(seq_dmabuf)) {
11394 /*
James Smart45ed1192009-10-02 15:17:02 -040011395 * When saving off frames post a new one and mark this
11396 * frame to be freed when it is finished.
11397 **/
James Smart4d9ab992009-10-02 15:16:39 -040011398 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11399 dmabuf->tag = -1;
11400 return;
11401 }
James Smart6669f9b2009-10-02 15:16:45 -040011402 /* Send the complete sequence to the upper layer protocol */
11403 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011404}
James Smart6fb120a2009-05-22 14:52:59 -040011405
11406/**
11407 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11408 * @phba: pointer to lpfc hba data structure.
11409 *
11410 * This routine is invoked to post rpi header templates to the
11411 * HBA consistent with the SLI-4 interface spec. This routine
11412 * posts a PAGE_SIZE memory region to the port to hold up to
11413 * PAGE_SIZE modulo 64 rpi context headers.
11414 *
11415 * This routine does not require any locks. It's usage is expected
11416 * to be driver load or reset recovery when the driver is
11417 * sequential.
11418 *
11419 * Return codes
11420 * 0 - sucessful
11421 * EIO - The mailbox failed to complete successfully.
11422 * When this error occurs, the driver is not guaranteed
11423 * to have any rpi regions posted to the device and
11424 * must either attempt to repost the regions or take a
11425 * fatal error.
11426 **/
11427int
11428lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11429{
11430 struct lpfc_rpi_hdr *rpi_page;
11431 uint32_t rc = 0;
11432
11433 /* Post all rpi memory regions to the port. */
11434 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11435 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11436 if (rc != MBX_SUCCESS) {
11437 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11438 "2008 Error %d posting all rpi "
11439 "headers\n", rc);
11440 rc = -EIO;
11441 break;
11442 }
11443 }
11444
11445 return rc;
11446}
11447
11448/**
11449 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11450 * @phba: pointer to lpfc hba data structure.
11451 * @rpi_page: pointer to the rpi memory region.
11452 *
11453 * This routine is invoked to post a single rpi header to the
11454 * HBA consistent with the SLI-4 interface spec. This memory region
11455 * maps up to 64 rpi context regions.
11456 *
11457 * Return codes
11458 * 0 - sucessful
11459 * ENOMEM - No available memory
11460 * EIO - The mailbox failed to complete successfully.
11461 **/
11462int
11463lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11464{
11465 LPFC_MBOXQ_t *mboxq;
11466 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11467 uint32_t rc = 0;
11468 uint32_t mbox_tmo;
11469 uint32_t shdr_status, shdr_add_status;
11470 union lpfc_sli4_cfg_shdr *shdr;
11471
11472 /* The port is notified of the header region via a mailbox command. */
11473 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11474 if (!mboxq) {
11475 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11476 "2001 Unable to allocate memory for issuing "
11477 "SLI_CONFIG_SPECIAL mailbox command\n");
11478 return -ENOMEM;
11479 }
11480
11481 /* Post all rpi memory regions to the port. */
11482 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11483 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11484 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11485 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11486 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11487 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11488 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11489 hdr_tmpl, rpi_page->page_count);
11490 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11491 rpi_page->start_rpi);
11492 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11493 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011495 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11498 if (rc != MBX_TIMEOUT)
11499 mempool_free(mboxq, phba->mbox_mem_pool);
11500 if (shdr_status || shdr_add_status || rc) {
11501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11502 "2514 POST_RPI_HDR mailbox failed with "
11503 "status x%x add_status x%x, mbx status x%x\n",
11504 shdr_status, shdr_add_status, rc);
11505 rc = -ENXIO;
11506 }
11507 return rc;
11508}
11509
11510/**
11511 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11512 * @phba: pointer to lpfc hba data structure.
11513 *
11514 * This routine is invoked to post rpi header templates to the
11515 * HBA consistent with the SLI-4 interface spec. This routine
11516 * posts a PAGE_SIZE memory region to the port to hold up to
11517 * PAGE_SIZE modulo 64 rpi context headers.
11518 *
11519 * Returns
11520 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11521 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11522 **/
11523int
11524lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11525{
11526 int rpi;
11527 uint16_t max_rpi, rpi_base, rpi_limit;
11528 uint16_t rpi_remaining;
11529 struct lpfc_rpi_hdr *rpi_hdr;
11530
11531 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11532 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11533 rpi_limit = phba->sli4_hba.next_rpi;
11534
11535 /*
11536 * The valid rpi range is not guaranteed to be zero-based. Start
11537 * the search at the rpi_base as reported by the port.
11538 */
11539 spin_lock_irq(&phba->hbalock);
11540 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11541 if (rpi >= rpi_limit || rpi < rpi_base)
11542 rpi = LPFC_RPI_ALLOC_ERROR;
11543 else {
11544 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11545 phba->sli4_hba.max_cfg_param.rpi_used++;
11546 phba->sli4_hba.rpi_count++;
11547 }
11548
11549 /*
11550 * Don't try to allocate more rpi header regions if the device limit
11551 * on available rpis max has been exhausted.
11552 */
11553 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11554 (phba->sli4_hba.rpi_count >= max_rpi)) {
11555 spin_unlock_irq(&phba->hbalock);
11556 return rpi;
11557 }
11558
11559 /*
11560 * If the driver is running low on rpi resources, allocate another
11561 * page now. Note that the next_rpi value is used because
11562 * it represents how many are actually in use whereas max_rpi notes
11563 * how many are supported max by the device.
11564 */
11565 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11566 phba->sli4_hba.rpi_count;
11567 spin_unlock_irq(&phba->hbalock);
11568 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11569 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11570 if (!rpi_hdr) {
11571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11572 "2002 Error Could not grow rpi "
11573 "count\n");
11574 } else {
11575 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11576 }
11577 }
11578
11579 return rpi;
11580}
11581
11582/**
11583 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11584 * @phba: pointer to lpfc hba data structure.
11585 *
11586 * This routine is invoked to release an rpi to the pool of
11587 * available rpis maintained by the driver.
11588 **/
11589void
11590lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11591{
11592 spin_lock_irq(&phba->hbalock);
11593 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11594 phba->sli4_hba.rpi_count--;
11595 phba->sli4_hba.max_cfg_param.rpi_used--;
11596 spin_unlock_irq(&phba->hbalock);
11597}
11598
11599/**
11600 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11601 * @phba: pointer to lpfc hba data structure.
11602 *
11603 * This routine is invoked to remove the memory region that
11604 * provided rpi via a bitmask.
11605 **/
11606void
11607lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11608{
11609 kfree(phba->sli4_hba.rpi_bmask);
11610}
11611
11612/**
11613 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11614 * @phba: pointer to lpfc hba data structure.
11615 *
11616 * This routine is invoked to remove the memory region that
11617 * provided rpi via a bitmask.
11618 **/
11619int
11620lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11621{
11622 LPFC_MBOXQ_t *mboxq;
11623 struct lpfc_hba *phba = ndlp->phba;
11624 int rc;
11625
11626 /* The port is notified of the header region via a mailbox command. */
11627 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11628 if (!mboxq)
11629 return -ENOMEM;
11630
11631 /* Post all rpi memory regions to the port. */
11632 lpfc_resume_rpi(mboxq, ndlp);
11633 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11634 if (rc == MBX_NOT_FINISHED) {
11635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11636 "2010 Resume RPI Mailbox failed "
11637 "status %d, mbxStatus x%x\n", rc,
11638 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11639 mempool_free(mboxq, phba->mbox_mem_pool);
11640 return -EIO;
11641 }
11642 return 0;
11643}
11644
11645/**
11646 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11647 * @phba: pointer to lpfc hba data structure.
11648 * @vpi: vpi value to activate with the port.
11649 *
11650 * This routine is invoked to activate a vpi with the
11651 * port when the host intends to use vports with a
11652 * nonzero vpi.
11653 *
11654 * Returns:
11655 * 0 success
11656 * -Evalue otherwise
11657 **/
11658int
11659lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11660{
11661 LPFC_MBOXQ_t *mboxq;
11662 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011663 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011664 uint32_t mbox_tmo;
11665
11666 if (vpi == 0)
11667 return -EINVAL;
11668 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11669 if (!mboxq)
11670 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011671 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011672 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11673 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011674 if (rc != MBX_SUCCESS) {
11675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11676 "2022 INIT VPI Mailbox failed "
11677 "status %d, mbxStatus x%x\n", rc,
11678 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011679 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011680 }
James Smart6a9c52c2009-10-02 15:16:51 -040011681 if (rc != MBX_TIMEOUT)
11682 mempool_free(mboxq, phba->mbox_mem_pool);
11683
11684 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011685}
11686
11687/**
11688 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11689 * @phba: pointer to lpfc hba data structure.
11690 * @mboxq: Pointer to mailbox object.
11691 *
11692 * This routine is invoked to manually add a single FCF record. The caller
11693 * must pass a completely initialized FCF_Record. This routine takes
11694 * care of the nonembedded mailbox operations.
11695 **/
11696static void
11697lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11698{
11699 void *virt_addr;
11700 union lpfc_sli4_cfg_shdr *shdr;
11701 uint32_t shdr_status, shdr_add_status;
11702
11703 virt_addr = mboxq->sge_array->addr[0];
11704 /* The IOCTL status is embedded in the mailbox subheader. */
11705 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11706 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11707 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11708
11709 if ((shdr_status || shdr_add_status) &&
11710 (shdr_status != STATUS_FCF_IN_USE))
11711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11712 "2558 ADD_FCF_RECORD mailbox failed with "
11713 "status x%x add_status x%x\n",
11714 shdr_status, shdr_add_status);
11715
11716 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11717}
11718
11719/**
11720 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11721 * @phba: pointer to lpfc hba data structure.
11722 * @fcf_record: pointer to the initialized fcf record to add.
11723 *
11724 * This routine is invoked to manually add a single FCF record. The caller
11725 * must pass a completely initialized FCF_Record. This routine takes
11726 * care of the nonembedded mailbox operations.
11727 **/
11728int
11729lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11730{
11731 int rc = 0;
11732 LPFC_MBOXQ_t *mboxq;
11733 uint8_t *bytep;
11734 void *virt_addr;
11735 dma_addr_t phys_addr;
11736 struct lpfc_mbx_sge sge;
11737 uint32_t alloc_len, req_len;
11738 uint32_t fcfindex;
11739
11740 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11741 if (!mboxq) {
11742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11743 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11744 return -ENOMEM;
11745 }
11746
11747 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11748 sizeof(uint32_t);
11749
11750 /* Allocate DMA memory and set up the non-embedded mailbox command */
11751 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11752 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11753 req_len, LPFC_SLI4_MBX_NEMBED);
11754 if (alloc_len < req_len) {
11755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11756 "2523 Allocated DMA memory size (x%x) is "
11757 "less than the requested DMA memory "
11758 "size (x%x)\n", alloc_len, req_len);
11759 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11760 return -ENOMEM;
11761 }
11762
11763 /*
11764 * Get the first SGE entry from the non-embedded DMA memory. This
11765 * routine only uses a single SGE.
11766 */
11767 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11768 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011769 virt_addr = mboxq->sge_array->addr[0];
11770 /*
11771 * Configure the FCF record for FCFI 0. This is the driver's
11772 * hardcoded default and gets used in nonFIP mode.
11773 */
11774 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11775 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11776 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11777
11778 /*
11779 * Copy the fcf_index and the FCF Record Data. The data starts after
11780 * the FCoE header plus word10. The data copy needs to be endian
11781 * correct.
11782 */
11783 bytep += sizeof(uint32_t);
11784 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11785 mboxq->vport = phba->pport;
11786 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11787 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11788 if (rc == MBX_NOT_FINISHED) {
11789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11790 "2515 ADD_FCF_RECORD mailbox failed with "
11791 "status 0x%x\n", rc);
11792 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11793 rc = -EIO;
11794 } else
11795 rc = 0;
11796
11797 return rc;
11798}
11799
11800/**
11801 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11802 * @phba: pointer to lpfc hba data structure.
11803 * @fcf_record: pointer to the fcf record to write the default data.
11804 * @fcf_index: FCF table entry index.
11805 *
11806 * This routine is invoked to build the driver's default FCF record. The
11807 * values used are hardcoded. This routine handles memory initialization.
11808 *
11809 **/
11810void
11811lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11812 struct fcf_record *fcf_record,
11813 uint16_t fcf_index)
11814{
11815 memset(fcf_record, 0, sizeof(struct fcf_record));
11816 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11817 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11818 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11819 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11820 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11821 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11822 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11823 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11824 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11825 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11826 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11827 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11828 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011829 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011830 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11831 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11832 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11833 /* Set the VLAN bit map */
11834 if (phba->valid_vlan) {
11835 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11836 = 1 << (phba->vlan_id % 8);
11837 }
11838}
11839
11840/**
11841 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11842 * @phba: pointer to lpfc hba data structure.
11843 * @fcf_index: FCF table entry offset.
11844 *
11845 * This routine is invoked to read up to @fcf_num of FCF record from the
11846 * device starting with the given @fcf_index.
11847 **/
11848int
11849lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11850{
11851 int rc = 0, error;
11852 LPFC_MBOXQ_t *mboxq;
11853 void *virt_addr;
11854 dma_addr_t phys_addr;
11855 uint8_t *bytep;
11856 struct lpfc_mbx_sge sge;
11857 uint32_t alloc_len, req_len;
11858 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11859
James Smart32b97932009-07-19 10:01:21 -040011860 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040011861 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11862 if (!mboxq) {
11863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11864 "2000 Failed to allocate mbox for "
11865 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040011866 error = -ENOMEM;
11867 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011868 }
11869
11870 req_len = sizeof(struct fcf_record) +
11871 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11872
11873 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11874 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11875 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11876 LPFC_SLI4_MBX_NEMBED);
11877
11878 if (alloc_len < req_len) {
11879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11880 "0291 Allocated DMA memory size (x%x) is "
11881 "less than the requested DMA memory "
11882 "size (x%x)\n", alloc_len, req_len);
James Smart4d9ab992009-10-02 15:16:39 -040011883 error = -ENOMEM;
11884 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011885 }
11886
11887 /* Get the first SGE entry from the non-embedded DMA memory. This
11888 * routine only uses a single SGE.
11889 */
11890 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11891 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011892 virt_addr = mboxq->sge_array->addr[0];
11893 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11894
11895 /* Set up command fields */
11896 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11897 /* Perform necessary endian conversion */
11898 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11899 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11900 mboxq->vport = phba->pport;
11901 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11902 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11903 if (rc == MBX_NOT_FINISHED) {
James Smart6fb120a2009-05-22 14:52:59 -040011904 error = -EIO;
James Smart32b97932009-07-19 10:01:21 -040011905 } else {
11906 spin_lock_irq(&phba->hbalock);
11907 phba->hba_flag |= FCF_DISC_INPROGRESS;
11908 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040011909 error = 0;
James Smart32b97932009-07-19 10:01:21 -040011910 }
James Smart4d9ab992009-10-02 15:16:39 -040011911fail_fcfscan:
11912 if (error) {
11913 if (mboxq)
11914 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11915 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11916 spin_lock_irq(&phba->hbalock);
11917 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11918 spin_unlock_irq(&phba->hbalock);
11919 }
James Smart6fb120a2009-05-22 14:52:59 -040011920 return error;
11921}
James Smarta0c87cb2009-07-19 10:01:10 -040011922
11923/**
11924 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11925 * @phba: pointer to lpfc hba data structure.
11926 *
11927 * This function read region 23 and parse TLV for port status to
11928 * decide if the user disaled the port. If the TLV indicates the
11929 * port is disabled, the hba_flag is set accordingly.
11930 **/
11931void
11932lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11933{
11934 LPFC_MBOXQ_t *pmb = NULL;
11935 MAILBOX_t *mb;
11936 uint8_t *rgn23_data = NULL;
11937 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11938 int rc;
11939
11940 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11941 if (!pmb) {
11942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11943 "2600 lpfc_sli_read_serdes_param failed to"
11944 " allocate mailbox memory\n");
11945 goto out;
11946 }
11947 mb = &pmb->u.mb;
11948
11949 /* Get adapter Region 23 data */
11950 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11951 if (!rgn23_data)
11952 goto out;
11953
11954 do {
11955 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11956 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11957
11958 if (rc != MBX_SUCCESS) {
11959 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11960 "2601 lpfc_sli_read_link_ste failed to"
11961 " read config region 23 rc 0x%x Status 0x%x\n",
11962 rc, mb->mbxStatus);
11963 mb->un.varDmp.word_cnt = 0;
11964 }
11965 /*
11966 * dump mem may return a zero when finished or we got a
11967 * mailbox error, either way we are done.
11968 */
11969 if (mb->un.varDmp.word_cnt == 0)
11970 break;
11971 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11972 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11973
11974 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11975 rgn23_data + offset,
11976 mb->un.varDmp.word_cnt);
11977 offset += mb->un.varDmp.word_cnt;
11978 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11979
11980 data_size = offset;
11981 offset = 0;
11982
11983 if (!data_size)
11984 goto out;
11985
11986 /* Check the region signature first */
11987 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
11988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11989 "2619 Config region 23 has bad signature\n");
11990 goto out;
11991 }
11992 offset += 4;
11993
11994 /* Check the data structure version */
11995 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
11996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11997 "2620 Config region 23 has bad version\n");
11998 goto out;
11999 }
12000 offset += 4;
12001
12002 /* Parse TLV entries in the region */
12003 while (offset < data_size) {
12004 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12005 break;
12006 /*
12007 * If the TLV is not driver specific TLV or driver id is
12008 * not linux driver id, skip the record.
12009 */
12010 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12011 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12012 (rgn23_data[offset + 3] != 0)) {
12013 offset += rgn23_data[offset + 1] * 4 + 4;
12014 continue;
12015 }
12016
12017 /* Driver found a driver specific TLV in the config region */
12018 sub_tlv_len = rgn23_data[offset + 1] * 4;
12019 offset += 4;
12020 tlv_offset = 0;
12021
12022 /*
12023 * Search for configured port state sub-TLV.
12024 */
12025 while ((offset < data_size) &&
12026 (tlv_offset < sub_tlv_len)) {
12027 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12028 offset += 4;
12029 tlv_offset += 4;
12030 break;
12031 }
12032 if (rgn23_data[offset] != PORT_STE_TYPE) {
12033 offset += rgn23_data[offset + 1] * 4 + 4;
12034 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12035 continue;
12036 }
12037
12038 /* This HBA contains PORT_STE configured */
12039 if (!rgn23_data[offset + 2])
12040 phba->hba_flag |= LINK_DISABLED;
12041
12042 goto out;
12043 }
12044 }
12045out:
12046 if (pmb)
12047 mempool_free(pmb, phba->mbox_mem_pool);
12048 kfree(rgn23_data);
12049 return;
12050}