b0fe153452aa38f8adf95c74c25824f6bec6e459
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * 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. *
20 *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
72 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
74
75 static IOCB_t *
76 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77 {
78 return &iocbq->iocb;
79 }
80
81 /**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93 static uint32_t
94 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95 {
96 union lpfc_wqe *temp_wqe;
97 struct lpfc_register doorbell;
98 uint32_t host_index;
99 uint32_t idx;
100
101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
106 /* If the host has not yet processed the next entry then we are done */
107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
109 q->WQ_overflow++;
110 return -ENOMEM;
111 }
112 q->WQ_posted++;
113 /* set consumption flag every once in a while */
114 if (!((q->host_index + 1) % q->entry_repost))
115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
122
123 q->host_index = idx;
124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
127 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
128 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
129 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
130 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
131
132 return 0;
133 }
134
135 /**
136 * lpfc_sli4_wq_release - Updates internal hba index for WQ
137 * @q: The Work Queue to operate on.
138 * @index: The index to advance the hba index to.
139 *
140 * This routine will update the HBA index of a queue to reflect consumption of
141 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
142 * an entry the host calls this function to update the queue's internal
143 * pointers. This routine returns the number of entries that were consumed by
144 * the HBA.
145 **/
146 static uint32_t
147 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
148 {
149 uint32_t released = 0;
150
151 /* sanity check on queue memory */
152 if (unlikely(!q))
153 return 0;
154
155 if (q->hba_index == index)
156 return 0;
157 do {
158 q->hba_index = ((q->hba_index + 1) % q->entry_count);
159 released++;
160 } while (q->hba_index != index);
161 return released;
162 }
163
164 /**
165 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
166 * @q: The Mailbox Queue to operate on.
167 * @wqe: The Mailbox Queue Entry to put on the Work queue.
168 *
169 * This routine will copy the contents of @mqe to the next available entry on
170 * the @q. This function will then ring the Work Queue Doorbell to signal the
171 * HBA to start processing the Work Queue Entry. This function returns 0 if
172 * successful. If no entries are available on @q then this function will return
173 * -ENOMEM.
174 * The caller is expected to hold the hbalock when calling this routine.
175 **/
176 static uint32_t
177 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
178 {
179 struct lpfc_mqe *temp_mqe;
180 struct lpfc_register doorbell;
181 uint32_t host_index;
182
183 /* sanity check on queue memory */
184 if (unlikely(!q))
185 return -ENOMEM;
186 temp_mqe = q->qe[q->host_index].mqe;
187
188 /* If the host has not yet processed the next entry then we are done */
189 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
190 return -ENOMEM;
191 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
192 /* Save off the mailbox pointer for completion */
193 q->phba->mbox = (MAILBOX_t *)temp_mqe;
194
195 /* Update the host index before invoking device */
196 host_index = q->host_index;
197 q->host_index = ((q->host_index + 1) % q->entry_count);
198
199 /* Ring Doorbell */
200 doorbell.word0 = 0;
201 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
202 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
203 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
204 return 0;
205 }
206
207 /**
208 * lpfc_sli4_mq_release - Updates internal hba index for MQ
209 * @q: The Mailbox Queue to operate on.
210 *
211 * This routine will update the HBA index of a queue to reflect consumption of
212 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
213 * an entry the host calls this function to update the queue's internal
214 * pointers. This routine returns the number of entries that were consumed by
215 * the HBA.
216 **/
217 static uint32_t
218 lpfc_sli4_mq_release(struct lpfc_queue *q)
219 {
220 /* sanity check on queue memory */
221 if (unlikely(!q))
222 return 0;
223
224 /* Clear the mailbox pointer for completion */
225 q->phba->mbox = NULL;
226 q->hba_index = ((q->hba_index + 1) % q->entry_count);
227 return 1;
228 }
229
230 /**
231 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
232 * @q: The Event Queue to get the first valid EQE from
233 *
234 * This routine will get the first valid Event Queue Entry from @q, update
235 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
236 * the Queue (no more work to do), or the Queue is full of EQEs that have been
237 * processed, but not popped back to the HBA then this routine will return NULL.
238 **/
239 static struct lpfc_eqe *
240 lpfc_sli4_eq_get(struct lpfc_queue *q)
241 {
242 struct lpfc_eqe *eqe;
243 uint32_t idx;
244
245 /* sanity check on queue memory */
246 if (unlikely(!q))
247 return NULL;
248 eqe = q->qe[q->hba_index].eqe;
249
250 /* If the next EQE is not valid then we are done */
251 if (!bf_get_le32(lpfc_eqe_valid, eqe))
252 return NULL;
253 /* If the host has not yet processed the next entry then we are done */
254 idx = ((q->hba_index + 1) % q->entry_count);
255 if (idx == q->host_index)
256 return NULL;
257
258 q->hba_index = idx;
259 return eqe;
260 }
261
262 /**
263 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
264 * @q: The Event Queue to disable interrupts
265 *
266 **/
267 static inline void
268 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
269 {
270 struct lpfc_register doorbell;
271
272 doorbell.word0 = 0;
273 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
274 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
275 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
276 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
277 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
278 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
279 }
280
281 /**
282 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
283 * @q: The Event Queue that the host has completed processing for.
284 * @arm: Indicates whether the host wants to arms this CQ.
285 *
286 * This routine will mark all Event Queue Entries on @q, from the last
287 * known completed entry to the last entry that was processed, as completed
288 * by clearing the valid bit for each completion queue entry. Then it will
289 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
290 * The internal host index in the @q will be updated by this routine to indicate
291 * that the host has finished processing the entries. The @arm parameter
292 * indicates that the queue should be rearmed when ringing the doorbell.
293 *
294 * This function will return the number of EQEs that were popped.
295 **/
296 uint32_t
297 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
298 {
299 uint32_t released = 0;
300 struct lpfc_eqe *temp_eqe;
301 struct lpfc_register doorbell;
302
303 /* sanity check on queue memory */
304 if (unlikely(!q))
305 return 0;
306
307 /* while there are valid entries */
308 while (q->hba_index != q->host_index) {
309 temp_eqe = q->qe[q->host_index].eqe;
310 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
311 released++;
312 q->host_index = ((q->host_index + 1) % q->entry_count);
313 }
314 if (unlikely(released == 0 && !arm))
315 return 0;
316
317 /* ring doorbell for number popped */
318 doorbell.word0 = 0;
319 if (arm) {
320 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
321 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
322 }
323 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
324 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
325 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
326 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
327 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
328 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
329 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
330 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
331 readl(q->phba->sli4_hba.EQCQDBregaddr);
332 return released;
333 }
334
335 /**
336 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
337 * @q: The Completion Queue to get the first valid CQE from
338 *
339 * This routine will get the first valid Completion Queue Entry from @q, update
340 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
341 * the Queue (no more work to do), or the Queue is full of CQEs that have been
342 * processed, but not popped back to the HBA then this routine will return NULL.
343 **/
344 static struct lpfc_cqe *
345 lpfc_sli4_cq_get(struct lpfc_queue *q)
346 {
347 struct lpfc_cqe *cqe;
348 uint32_t idx;
349
350 /* sanity check on queue memory */
351 if (unlikely(!q))
352 return NULL;
353
354 /* If the next CQE is not valid then we are done */
355 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
356 return NULL;
357 /* If the host has not yet processed the next entry then we are done */
358 idx = ((q->hba_index + 1) % q->entry_count);
359 if (idx == q->host_index)
360 return NULL;
361
362 cqe = q->qe[q->hba_index].cqe;
363 q->hba_index = idx;
364 return cqe;
365 }
366
367 /**
368 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
369 * @q: The Completion Queue that the host has completed processing for.
370 * @arm: Indicates whether the host wants to arms this CQ.
371 *
372 * This routine will mark all Completion queue entries on @q, from the last
373 * known completed entry to the last entry that was processed, as completed
374 * by clearing the valid bit for each completion queue entry. Then it will
375 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
376 * The internal host index in the @q will be updated by this routine to indicate
377 * that the host has finished processing the entries. The @arm parameter
378 * indicates that the queue should be rearmed when ringing the doorbell.
379 *
380 * This function will return the number of CQEs that were released.
381 **/
382 uint32_t
383 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
384 {
385 uint32_t released = 0;
386 struct lpfc_cqe *temp_qe;
387 struct lpfc_register doorbell;
388
389 /* sanity check on queue memory */
390 if (unlikely(!q))
391 return 0;
392 /* while there are valid entries */
393 while (q->hba_index != q->host_index) {
394 temp_qe = q->qe[q->host_index].cqe;
395 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
396 released++;
397 q->host_index = ((q->host_index + 1) % q->entry_count);
398 }
399 if (unlikely(released == 0 && !arm))
400 return 0;
401
402 /* ring doorbell for number popped */
403 doorbell.word0 = 0;
404 if (arm)
405 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
406 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
407 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
408 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
409 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
410 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
411 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
412 return released;
413 }
414
415 /**
416 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
417 * @q: The Header Receive Queue to operate on.
418 * @wqe: The Receive Queue Entry to put on the Receive queue.
419 *
420 * This routine will copy the contents of @wqe to the next available entry on
421 * the @q. This function will then ring the Receive Queue Doorbell to signal the
422 * HBA to start processing the Receive Queue Entry. This function returns the
423 * index that the rqe was copied to if successful. If no entries are available
424 * on @q then this function will return -ENOMEM.
425 * The caller is expected to hold the hbalock when calling this routine.
426 **/
427 static int
428 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
429 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
430 {
431 struct lpfc_rqe *temp_hrqe;
432 struct lpfc_rqe *temp_drqe;
433 struct lpfc_register doorbell;
434 int put_index = hq->host_index;
435
436 /* sanity check on queue memory */
437 if (unlikely(!hq) || unlikely(!dq))
438 return -ENOMEM;
439 temp_hrqe = hq->qe[hq->host_index].rqe;
440 temp_drqe = dq->qe[dq->host_index].rqe;
441
442 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
443 return -EINVAL;
444 if (hq->host_index != dq->host_index)
445 return -EINVAL;
446 /* If the host has not yet processed the next entry then we are done */
447 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
448 return -EBUSY;
449 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
450 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
451
452 /* Update the host index to point to the next slot */
453 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
454 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
455
456 /* Ring The Header Receive Queue Doorbell */
457 if (!(hq->host_index % hq->entry_repost)) {
458 doorbell.word0 = 0;
459 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
460 hq->entry_repost);
461 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
462 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
463 }
464 return put_index;
465 }
466
467 /**
468 * lpfc_sli4_rq_release - Updates internal hba index for RQ
469 * @q: The Header Receive Queue to operate on.
470 *
471 * This routine will update the HBA index of a queue to reflect consumption of
472 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
473 * consumed an entry the host calls this function to update the queue's
474 * internal pointers. This routine returns the number of entries that were
475 * consumed by the HBA.
476 **/
477 static uint32_t
478 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
479 {
480 /* sanity check on queue memory */
481 if (unlikely(!hq) || unlikely(!dq))
482 return 0;
483
484 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
485 return 0;
486 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
487 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
488 return 1;
489 }
490
491 /**
492 * lpfc_cmd_iocb - Get next command iocb entry in the ring
493 * @phba: Pointer to HBA context object.
494 * @pring: Pointer to driver SLI ring object.
495 *
496 * This function returns pointer to next command iocb entry
497 * in the command ring. The caller must hold hbalock to prevent
498 * other threads consume the next command iocb.
499 * SLI-2/SLI-3 provide different sized iocbs.
500 **/
501 static inline IOCB_t *
502 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
503 {
504 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
505 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
506 }
507
508 /**
509 * lpfc_resp_iocb - Get next response iocb entry in the ring
510 * @phba: Pointer to HBA context object.
511 * @pring: Pointer to driver SLI ring object.
512 *
513 * This function returns pointer to next response iocb entry
514 * in the response ring. The caller must hold hbalock to make sure
515 * that no other thread consume the next response iocb.
516 * SLI-2/SLI-3 provide different sized iocbs.
517 **/
518 static inline IOCB_t *
519 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
520 {
521 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
522 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
523 }
524
525 /**
526 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
527 * @phba: Pointer to HBA context object.
528 *
529 * This function is called with hbalock held. This function
530 * allocates a new driver iocb object from the iocb pool. If the
531 * allocation is successful, it returns pointer to the newly
532 * allocated iocb object else it returns NULL.
533 **/
534 struct lpfc_iocbq *
535 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
536 {
537 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
538 struct lpfc_iocbq * iocbq = NULL;
539
540 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
541 if (iocbq)
542 phba->iocb_cnt++;
543 if (phba->iocb_cnt > phba->iocb_max)
544 phba->iocb_max = phba->iocb_cnt;
545 return iocbq;
546 }
547
548 /**
549 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
550 * @phba: Pointer to HBA context object.
551 * @xritag: XRI value.
552 *
553 * This function clears the sglq pointer from the array of acive
554 * sglq's. The xritag that is passed in is used to index into the
555 * array. Before the xritag can be used it needs to be adjusted
556 * by subtracting the xribase.
557 *
558 * Returns sglq ponter = success, NULL = Failure.
559 **/
560 static struct lpfc_sglq *
561 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
562 {
563 struct lpfc_sglq *sglq;
564
565 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
566 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
567 return sglq;
568 }
569
570 /**
571 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
572 * @phba: Pointer to HBA context object.
573 * @xritag: XRI value.
574 *
575 * This function returns the sglq pointer from the array of acive
576 * sglq's. The xritag that is passed in is used to index into the
577 * array. Before the xritag can be used it needs to be adjusted
578 * by subtracting the xribase.
579 *
580 * Returns sglq ponter = success, NULL = Failure.
581 **/
582 struct lpfc_sglq *
583 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
584 {
585 struct lpfc_sglq *sglq;
586
587 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
588 return sglq;
589 }
590
591 /**
592 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
593 * @phba: Pointer to HBA context object.
594 * @xritag: xri used in this exchange.
595 * @rrq: The RRQ to be cleared.
596 *
597 **/
598 void
599 lpfc_clr_rrq_active(struct lpfc_hba *phba,
600 uint16_t xritag,
601 struct lpfc_node_rrq *rrq)
602 {
603 struct lpfc_nodelist *ndlp = NULL;
604
605 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
606 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
607
608 /* The target DID could have been swapped (cable swap)
609 * we should use the ndlp from the findnode if it is
610 * available.
611 */
612 if ((!ndlp) && rrq->ndlp)
613 ndlp = rrq->ndlp;
614
615 if (!ndlp)
616 goto out;
617
618 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
619 rrq->send_rrq = 0;
620 rrq->xritag = 0;
621 rrq->rrq_stop_time = 0;
622 }
623 out:
624 mempool_free(rrq, phba->rrq_pool);
625 }
626
627 /**
628 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
629 * @phba: Pointer to HBA context object.
630 *
631 * This function is called with hbalock held. This function
632 * Checks if stop_time (ratov from setting rrq active) has
633 * been reached, if it has and the send_rrq flag is set then
634 * it will call lpfc_send_rrq. If the send_rrq flag is not set
635 * then it will just call the routine to clear the rrq and
636 * free the rrq resource.
637 * The timer is set to the next rrq that is going to expire before
638 * leaving the routine.
639 *
640 **/
641 void
642 lpfc_handle_rrq_active(struct lpfc_hba *phba)
643 {
644 struct lpfc_node_rrq *rrq;
645 struct lpfc_node_rrq *nextrrq;
646 unsigned long next_time;
647 unsigned long iflags;
648 LIST_HEAD(send_rrq);
649
650 spin_lock_irqsave(&phba->hbalock, iflags);
651 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
652 next_time = jiffies + HZ * (phba->fc_ratov + 1);
653 list_for_each_entry_safe(rrq, nextrrq,
654 &phba->active_rrq_list, list) {
655 if (time_after(jiffies, rrq->rrq_stop_time))
656 list_move(&rrq->list, &send_rrq);
657 else if (time_before(rrq->rrq_stop_time, next_time))
658 next_time = rrq->rrq_stop_time;
659 }
660 spin_unlock_irqrestore(&phba->hbalock, iflags);
661 if (!list_empty(&phba->active_rrq_list))
662 mod_timer(&phba->rrq_tmr, next_time);
663 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
664 list_del(&rrq->list);
665 if (!rrq->send_rrq)
666 /* this call will free the rrq */
667 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
668 else if (lpfc_send_rrq(phba, rrq)) {
669 /* if we send the rrq then the completion handler
670 * will clear the bit in the xribitmap.
671 */
672 lpfc_clr_rrq_active(phba, rrq->xritag,
673 rrq);
674 }
675 }
676 }
677
678 /**
679 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
680 * @vport: Pointer to vport context object.
681 * @xri: The xri used in the exchange.
682 * @did: The targets DID for this exchange.
683 *
684 * returns NULL = rrq not found in the phba->active_rrq_list.
685 * rrq = rrq for this xri and target.
686 **/
687 struct lpfc_node_rrq *
688 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
689 {
690 struct lpfc_hba *phba = vport->phba;
691 struct lpfc_node_rrq *rrq;
692 struct lpfc_node_rrq *nextrrq;
693 unsigned long iflags;
694
695 if (phba->sli_rev != LPFC_SLI_REV4)
696 return NULL;
697 spin_lock_irqsave(&phba->hbalock, iflags);
698 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
699 if (rrq->vport == vport && rrq->xritag == xri &&
700 rrq->nlp_DID == did){
701 list_del(&rrq->list);
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return rrq;
704 }
705 }
706 spin_unlock_irqrestore(&phba->hbalock, iflags);
707 return NULL;
708 }
709
710 /**
711 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
712 * @vport: Pointer to vport context object.
713 * @ndlp: Pointer to the lpfc_node_list structure.
714 * If ndlp is NULL Remove all active RRQs for this vport from the
715 * phba->active_rrq_list and clear the rrq.
716 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
717 **/
718 void
719 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
720
721 {
722 struct lpfc_hba *phba = vport->phba;
723 struct lpfc_node_rrq *rrq;
724 struct lpfc_node_rrq *nextrrq;
725 unsigned long iflags;
726 LIST_HEAD(rrq_list);
727
728 if (phba->sli_rev != LPFC_SLI_REV4)
729 return;
730 if (!ndlp) {
731 lpfc_sli4_vport_delete_els_xri_aborted(vport);
732 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
733 }
734 spin_lock_irqsave(&phba->hbalock, iflags);
735 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
736 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
737 list_move(&rrq->list, &rrq_list);
738 spin_unlock_irqrestore(&phba->hbalock, iflags);
739
740 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
741 list_del(&rrq->list);
742 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
743 }
744 }
745
746 /**
747 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
748 * @phba: Pointer to HBA context object.
749 *
750 * Remove all rrqs from the phba->active_rrq_list and free them by
751 * calling __lpfc_clr_active_rrq
752 *
753 **/
754 void
755 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
756 {
757 struct lpfc_node_rrq *rrq;
758 struct lpfc_node_rrq *nextrrq;
759 unsigned long next_time;
760 unsigned long iflags;
761 LIST_HEAD(rrq_list);
762
763 if (phba->sli_rev != LPFC_SLI_REV4)
764 return;
765 spin_lock_irqsave(&phba->hbalock, iflags);
766 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
767 next_time = jiffies + HZ * (phba->fc_ratov * 2);
768 list_splice_init(&phba->active_rrq_list, &rrq_list);
769 spin_unlock_irqrestore(&phba->hbalock, iflags);
770
771 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
772 list_del(&rrq->list);
773 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
774 }
775 if (!list_empty(&phba->active_rrq_list))
776 mod_timer(&phba->rrq_tmr, next_time);
777 }
778
779
780 /**
781 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
782 * @phba: Pointer to HBA context object.
783 * @ndlp: Targets nodelist pointer for this exchange.
784 * @xritag the xri in the bitmap to test.
785 *
786 * This function is called with hbalock held. This function
787 * returns 0 = rrq not active for this xri
788 * 1 = rrq is valid for this xri.
789 **/
790 int
791 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
792 uint16_t xritag)
793 {
794 if (!ndlp)
795 return 0;
796 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
797 return 1;
798 else
799 return 0;
800 }
801
802 /**
803 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
804 * @phba: Pointer to HBA context object.
805 * @ndlp: nodelist pointer for this target.
806 * @xritag: xri used in this exchange.
807 * @rxid: Remote Exchange ID.
808 * @send_rrq: Flag used to determine if we should send rrq els cmd.
809 *
810 * This function takes the hbalock.
811 * The active bit is always set in the active rrq xri_bitmap even
812 * if there is no slot avaiable for the other rrq information.
813 *
814 * returns 0 rrq actived for this xri
815 * < 0 No memory or invalid ndlp.
816 **/
817 int
818 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
819 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
820 {
821 unsigned long iflags;
822 struct lpfc_node_rrq *rrq;
823 int empty;
824
825 if (!ndlp)
826 return -EINVAL;
827
828 if (!phba->cfg_enable_rrq)
829 return -EINVAL;
830
831 spin_lock_irqsave(&phba->hbalock, iflags);
832 if (phba->pport->load_flag & FC_UNLOADING) {
833 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
834 goto out;
835 }
836
837 /*
838 * set the active bit even if there is no mem available.
839 */
840 if (NLP_CHK_FREE_REQ(ndlp))
841 goto out;
842
843 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
844 goto out;
845
846 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
847 goto out;
848
849 spin_unlock_irqrestore(&phba->hbalock, iflags);
850 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
851 if (!rrq) {
852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
853 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
854 " DID:0x%x Send:%d\n",
855 xritag, rxid, ndlp->nlp_DID, send_rrq);
856 return -EINVAL;
857 }
858 rrq->send_rrq = send_rrq;
859 rrq->xritag = xritag;
860 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
861 rrq->ndlp = ndlp;
862 rrq->nlp_DID = ndlp->nlp_DID;
863 rrq->vport = ndlp->vport;
864 rrq->rxid = rxid;
865 rrq->send_rrq = send_rrq;
866 spin_lock_irqsave(&phba->hbalock, iflags);
867 empty = list_empty(&phba->active_rrq_list);
868 list_add_tail(&rrq->list, &phba->active_rrq_list);
869 phba->hba_flag |= HBA_RRQ_ACTIVE;
870 if (empty)
871 lpfc_worker_wake_up(phba);
872 spin_unlock_irqrestore(&phba->hbalock, iflags);
873 return 0;
874 out:
875 spin_unlock_irqrestore(&phba->hbalock, iflags);
876 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
877 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
878 " DID:0x%x Send:%d\n",
879 xritag, rxid, ndlp->nlp_DID, send_rrq);
880 return -EINVAL;
881 }
882
883 /**
884 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
885 * @phba: Pointer to HBA context object.
886 * @piocb: Pointer to the iocbq.
887 *
888 * This function is called with hbalock held. This function
889 * gets a new driver sglq object from the sglq list. If the
890 * list is not empty then it is successful, it returns pointer to the newly
891 * allocated sglq object else it returns NULL.
892 **/
893 static struct lpfc_sglq *
894 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
895 {
896 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
897 struct lpfc_sglq *sglq = NULL;
898 struct lpfc_sglq *start_sglq = NULL;
899 struct lpfc_scsi_buf *lpfc_cmd;
900 struct lpfc_nodelist *ndlp;
901 int found = 0;
902
903 if (piocbq->iocb_flag & LPFC_IO_FCP) {
904 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
905 ndlp = lpfc_cmd->rdata->pnode;
906 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
907 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
908 ndlp = piocbq->context_un.ndlp;
909 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
910 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
911 ndlp = piocbq->context_un.ndlp;
912 else
913 ndlp = piocbq->context1;
914
915 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
916 start_sglq = sglq;
917 while (!found) {
918 if (!sglq)
919 return NULL;
920 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
921 /* This xri has an rrq outstanding for this DID.
922 * put it back in the list and get another xri.
923 */
924 list_add_tail(&sglq->list, lpfc_sgl_list);
925 sglq = NULL;
926 list_remove_head(lpfc_sgl_list, sglq,
927 struct lpfc_sglq, list);
928 if (sglq == start_sglq) {
929 sglq = NULL;
930 break;
931 } else
932 continue;
933 }
934 sglq->ndlp = ndlp;
935 found = 1;
936 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
937 sglq->state = SGL_ALLOCATED;
938 }
939 return sglq;
940 }
941
942 /**
943 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
944 * @phba: Pointer to HBA context object.
945 *
946 * This function is called with no lock held. This function
947 * allocates a new driver iocb object from the iocb pool. If the
948 * allocation is successful, it returns pointer to the newly
949 * allocated iocb object else it returns NULL.
950 **/
951 struct lpfc_iocbq *
952 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
953 {
954 struct lpfc_iocbq * iocbq = NULL;
955 unsigned long iflags;
956
957 spin_lock_irqsave(&phba->hbalock, iflags);
958 iocbq = __lpfc_sli_get_iocbq(phba);
959 spin_unlock_irqrestore(&phba->hbalock, iflags);
960 return iocbq;
961 }
962
963 /**
964 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
965 * @phba: Pointer to HBA context object.
966 * @iocbq: Pointer to driver iocb object.
967 *
968 * This function is called with hbalock held to release driver
969 * iocb object to the iocb pool. The iotag in the iocb object
970 * does not change for each use of the iocb object. This function
971 * clears all other fields of the iocb object when it is freed.
972 * The sqlq structure that holds the xritag and phys and virtual
973 * mappings for the scatter gather list is retrieved from the
974 * active array of sglq. The get of the sglq pointer also clears
975 * the entry in the array. If the status of the IO indiactes that
976 * this IO was aborted then the sglq entry it put on the
977 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
978 * IO has good status or fails for any other reason then the sglq
979 * entry is added to the free list (lpfc_sgl_list).
980 **/
981 static void
982 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
983 {
984 struct lpfc_sglq *sglq;
985 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
986 unsigned long iflag = 0;
987 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
988
989 if (iocbq->sli4_xritag == NO_XRI)
990 sglq = NULL;
991 else
992 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
993
994 if (sglq) {
995 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
996 (sglq->state != SGL_XRI_ABORTED)) {
997 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
998 iflag);
999 list_add(&sglq->list,
1000 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1001 spin_unlock_irqrestore(
1002 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1003 } else {
1004 sglq->state = SGL_FREED;
1005 sglq->ndlp = NULL;
1006 list_add_tail(&sglq->list,
1007 &phba->sli4_hba.lpfc_sgl_list);
1008
1009 /* Check if TXQ queue needs to be serviced */
1010 if (pring->txq_cnt)
1011 lpfc_worker_wake_up(phba);
1012 }
1013 }
1014
1015
1016 /*
1017 * Clean all volatile data fields, preserve iotag and node struct.
1018 */
1019 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1020 iocbq->sli4_lxritag = NO_XRI;
1021 iocbq->sli4_xritag = NO_XRI;
1022 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1023 }
1024
1025
1026 /**
1027 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1028 * @phba: Pointer to HBA context object.
1029 * @iocbq: Pointer to driver iocb object.
1030 *
1031 * This function is called with hbalock held to release driver
1032 * iocb object to the iocb pool. The iotag in the iocb object
1033 * does not change for each use of the iocb object. This function
1034 * clears all other fields of the iocb object when it is freed.
1035 **/
1036 static void
1037 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1038 {
1039 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1040
1041 /*
1042 * Clean all volatile data fields, preserve iotag and node struct.
1043 */
1044 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1045 iocbq->sli4_xritag = NO_XRI;
1046 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1047 }
1048
1049 /**
1050 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1051 * @phba: Pointer to HBA context object.
1052 * @iocbq: Pointer to driver iocb object.
1053 *
1054 * This function is called with hbalock held to release driver
1055 * iocb object to the iocb pool. The iotag in the iocb object
1056 * does not change for each use of the iocb object. This function
1057 * clears all other fields of the iocb object when it is freed.
1058 **/
1059 static void
1060 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1061 {
1062 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1063 phba->iocb_cnt--;
1064 }
1065
1066 /**
1067 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1068 * @phba: Pointer to HBA context object.
1069 * @iocbq: Pointer to driver iocb object.
1070 *
1071 * This function is called with no lock held to release the iocb to
1072 * iocb pool.
1073 **/
1074 void
1075 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1076 {
1077 unsigned long iflags;
1078
1079 /*
1080 * Clean all volatile data fields, preserve iotag and node struct.
1081 */
1082 spin_lock_irqsave(&phba->hbalock, iflags);
1083 __lpfc_sli_release_iocbq(phba, iocbq);
1084 spin_unlock_irqrestore(&phba->hbalock, iflags);
1085 }
1086
1087 /**
1088 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1089 * @phba: Pointer to HBA context object.
1090 * @iocblist: List of IOCBs.
1091 * @ulpstatus: ULP status in IOCB command field.
1092 * @ulpWord4: ULP word-4 in IOCB command field.
1093 *
1094 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1095 * on the list by invoking the complete callback function associated with the
1096 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1097 * fields.
1098 **/
1099 void
1100 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1101 uint32_t ulpstatus, uint32_t ulpWord4)
1102 {
1103 struct lpfc_iocbq *piocb;
1104
1105 while (!list_empty(iocblist)) {
1106 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1107
1108 if (!piocb->iocb_cmpl)
1109 lpfc_sli_release_iocbq(phba, piocb);
1110 else {
1111 piocb->iocb.ulpStatus = ulpstatus;
1112 piocb->iocb.un.ulpWord[4] = ulpWord4;
1113 (piocb->iocb_cmpl) (phba, piocb, piocb);
1114 }
1115 }
1116 return;
1117 }
1118
1119 /**
1120 * lpfc_sli_iocb_cmd_type - Get the iocb type
1121 * @iocb_cmnd: iocb command code.
1122 *
1123 * This function is called by ring event handler function to get the iocb type.
1124 * This function translates the iocb command to an iocb command type used to
1125 * decide the final disposition of each completed IOCB.
1126 * The function returns
1127 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1128 * LPFC_SOL_IOCB if it is a solicited iocb completion
1129 * LPFC_ABORT_IOCB if it is an abort iocb
1130 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1131 *
1132 * The caller is not required to hold any lock.
1133 **/
1134 static lpfc_iocb_type
1135 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1136 {
1137 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1138
1139 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1140 return 0;
1141
1142 switch (iocb_cmnd) {
1143 case CMD_XMIT_SEQUENCE_CR:
1144 case CMD_XMIT_SEQUENCE_CX:
1145 case CMD_XMIT_BCAST_CN:
1146 case CMD_XMIT_BCAST_CX:
1147 case CMD_ELS_REQUEST_CR:
1148 case CMD_ELS_REQUEST_CX:
1149 case CMD_CREATE_XRI_CR:
1150 case CMD_CREATE_XRI_CX:
1151 case CMD_GET_RPI_CN:
1152 case CMD_XMIT_ELS_RSP_CX:
1153 case CMD_GET_RPI_CR:
1154 case CMD_FCP_IWRITE_CR:
1155 case CMD_FCP_IWRITE_CX:
1156 case CMD_FCP_IREAD_CR:
1157 case CMD_FCP_IREAD_CX:
1158 case CMD_FCP_ICMND_CR:
1159 case CMD_FCP_ICMND_CX:
1160 case CMD_FCP_TSEND_CX:
1161 case CMD_FCP_TRSP_CX:
1162 case CMD_FCP_TRECEIVE_CX:
1163 case CMD_FCP_AUTO_TRSP_CX:
1164 case CMD_ADAPTER_MSG:
1165 case CMD_ADAPTER_DUMP:
1166 case CMD_XMIT_SEQUENCE64_CR:
1167 case CMD_XMIT_SEQUENCE64_CX:
1168 case CMD_XMIT_BCAST64_CN:
1169 case CMD_XMIT_BCAST64_CX:
1170 case CMD_ELS_REQUEST64_CR:
1171 case CMD_ELS_REQUEST64_CX:
1172 case CMD_FCP_IWRITE64_CR:
1173 case CMD_FCP_IWRITE64_CX:
1174 case CMD_FCP_IREAD64_CR:
1175 case CMD_FCP_IREAD64_CX:
1176 case CMD_FCP_ICMND64_CR:
1177 case CMD_FCP_ICMND64_CX:
1178 case CMD_FCP_TSEND64_CX:
1179 case CMD_FCP_TRSP64_CX:
1180 case CMD_FCP_TRECEIVE64_CX:
1181 case CMD_GEN_REQUEST64_CR:
1182 case CMD_GEN_REQUEST64_CX:
1183 case CMD_XMIT_ELS_RSP64_CX:
1184 case DSSCMD_IWRITE64_CR:
1185 case DSSCMD_IWRITE64_CX:
1186 case DSSCMD_IREAD64_CR:
1187 case DSSCMD_IREAD64_CX:
1188 type = LPFC_SOL_IOCB;
1189 break;
1190 case CMD_ABORT_XRI_CN:
1191 case CMD_ABORT_XRI_CX:
1192 case CMD_CLOSE_XRI_CN:
1193 case CMD_CLOSE_XRI_CX:
1194 case CMD_XRI_ABORTED_CX:
1195 case CMD_ABORT_MXRI64_CN:
1196 case CMD_XMIT_BLS_RSP64_CX:
1197 type = LPFC_ABORT_IOCB;
1198 break;
1199 case CMD_RCV_SEQUENCE_CX:
1200 case CMD_RCV_ELS_REQ_CX:
1201 case CMD_RCV_SEQUENCE64_CX:
1202 case CMD_RCV_ELS_REQ64_CX:
1203 case CMD_ASYNC_STATUS:
1204 case CMD_IOCB_RCV_SEQ64_CX:
1205 case CMD_IOCB_RCV_ELS64_CX:
1206 case CMD_IOCB_RCV_CONT64_CX:
1207 case CMD_IOCB_RET_XRI64_CX:
1208 type = LPFC_UNSOL_IOCB;
1209 break;
1210 case CMD_IOCB_XMIT_MSEQ64_CR:
1211 case CMD_IOCB_XMIT_MSEQ64_CX:
1212 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1213 case CMD_IOCB_RCV_ELS_LIST64_CX:
1214 case CMD_IOCB_CLOSE_EXTENDED_CN:
1215 case CMD_IOCB_ABORT_EXTENDED_CN:
1216 case CMD_IOCB_RET_HBQE64_CN:
1217 case CMD_IOCB_FCP_IBIDIR64_CR:
1218 case CMD_IOCB_FCP_IBIDIR64_CX:
1219 case CMD_IOCB_FCP_ITASKMGT64_CX:
1220 case CMD_IOCB_LOGENTRY_CN:
1221 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1222 printk("%s - Unhandled SLI-3 Command x%x\n",
1223 __func__, iocb_cmnd);
1224 type = LPFC_UNKNOWN_IOCB;
1225 break;
1226 default:
1227 type = LPFC_UNKNOWN_IOCB;
1228 break;
1229 }
1230
1231 return type;
1232 }
1233
1234 /**
1235 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1236 * @phba: Pointer to HBA context object.
1237 *
1238 * This function is called from SLI initialization code
1239 * to configure every ring of the HBA's SLI interface. The
1240 * caller is not required to hold any lock. This function issues
1241 * a config_ring mailbox command for each ring.
1242 * This function returns zero if successful else returns a negative
1243 * error code.
1244 **/
1245 static int
1246 lpfc_sli_ring_map(struct lpfc_hba *phba)
1247 {
1248 struct lpfc_sli *psli = &phba->sli;
1249 LPFC_MBOXQ_t *pmb;
1250 MAILBOX_t *pmbox;
1251 int i, rc, ret = 0;
1252
1253 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1254 if (!pmb)
1255 return -ENOMEM;
1256 pmbox = &pmb->u.mb;
1257 phba->link_state = LPFC_INIT_MBX_CMDS;
1258 for (i = 0; i < psli->num_rings; i++) {
1259 lpfc_config_ring(phba, i, pmb);
1260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1261 if (rc != MBX_SUCCESS) {
1262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1263 "0446 Adapter failed to init (%d), "
1264 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1265 "ring %d\n",
1266 rc, pmbox->mbxCommand,
1267 pmbox->mbxStatus, i);
1268 phba->link_state = LPFC_HBA_ERROR;
1269 ret = -ENXIO;
1270 break;
1271 }
1272 }
1273 mempool_free(pmb, phba->mbox_mem_pool);
1274 return ret;
1275 }
1276
1277 /**
1278 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1279 * @phba: Pointer to HBA context object.
1280 * @pring: Pointer to driver SLI ring object.
1281 * @piocb: Pointer to the driver iocb object.
1282 *
1283 * This function is called with hbalock held. The function adds the
1284 * new iocb to txcmplq of the given ring. This function always returns
1285 * 0. If this function is called for ELS ring, this function checks if
1286 * there is a vport associated with the ELS command. This function also
1287 * starts els_tmofunc timer if this is an ELS command.
1288 **/
1289 static int
1290 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1291 struct lpfc_iocbq *piocb)
1292 {
1293 list_add_tail(&piocb->list, &pring->txcmplq);
1294 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1295 pring->txcmplq_cnt++;
1296 if (pring->txcmplq_cnt > pring->txcmplq_max)
1297 pring->txcmplq_max = pring->txcmplq_cnt;
1298
1299 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1300 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1301 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1302 if (!piocb->vport)
1303 BUG();
1304 else
1305 mod_timer(&piocb->vport->els_tmofunc,
1306 jiffies + HZ * (phba->fc_ratov << 1));
1307 }
1308
1309
1310 return 0;
1311 }
1312
1313 /**
1314 * lpfc_sli_ringtx_get - Get first element of the txq
1315 * @phba: Pointer to HBA context object.
1316 * @pring: Pointer to driver SLI ring object.
1317 *
1318 * This function is called with hbalock held to get next
1319 * iocb in txq of the given ring. If there is any iocb in
1320 * the txq, the function returns first iocb in the list after
1321 * removing the iocb from the list, else it returns NULL.
1322 **/
1323 struct lpfc_iocbq *
1324 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1325 {
1326 struct lpfc_iocbq *cmd_iocb;
1327
1328 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1329 if (cmd_iocb != NULL)
1330 pring->txq_cnt--;
1331 return cmd_iocb;
1332 }
1333
1334 /**
1335 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1336 * @phba: Pointer to HBA context object.
1337 * @pring: Pointer to driver SLI ring object.
1338 *
1339 * This function is called with hbalock held and the caller must post the
1340 * iocb without releasing the lock. If the caller releases the lock,
1341 * iocb slot returned by the function is not guaranteed to be available.
1342 * The function returns pointer to the next available iocb slot if there
1343 * is available slot in the ring, else it returns NULL.
1344 * If the get index of the ring is ahead of the put index, the function
1345 * will post an error attention event to the worker thread to take the
1346 * HBA to offline state.
1347 **/
1348 static IOCB_t *
1349 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1350 {
1351 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1352 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1353 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1354 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1355 pring->sli.sli3.next_cmdidx = 0;
1356
1357 if (unlikely(pring->sli.sli3.local_getidx ==
1358 pring->sli.sli3.next_cmdidx)) {
1359
1360 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1361
1362 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1364 "0315 Ring %d issue: portCmdGet %d "
1365 "is bigger than cmd ring %d\n",
1366 pring->ringno,
1367 pring->sli.sli3.local_getidx,
1368 max_cmd_idx);
1369
1370 phba->link_state = LPFC_HBA_ERROR;
1371 /*
1372 * All error attention handlers are posted to
1373 * worker thread
1374 */
1375 phba->work_ha |= HA_ERATT;
1376 phba->work_hs = HS_FFER3;
1377
1378 lpfc_worker_wake_up(phba);
1379
1380 return NULL;
1381 }
1382
1383 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1384 return NULL;
1385 }
1386
1387 return lpfc_cmd_iocb(phba, pring);
1388 }
1389
1390 /**
1391 * lpfc_sli_next_iotag - Get an iotag for the iocb
1392 * @phba: Pointer to HBA context object.
1393 * @iocbq: Pointer to driver iocb object.
1394 *
1395 * This function gets an iotag for the iocb. If there is no unused iotag and
1396 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1397 * array and assigns a new iotag.
1398 * The function returns the allocated iotag if successful, else returns zero.
1399 * Zero is not a valid iotag.
1400 * The caller is not required to hold any lock.
1401 **/
1402 uint16_t
1403 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1404 {
1405 struct lpfc_iocbq **new_arr;
1406 struct lpfc_iocbq **old_arr;
1407 size_t new_len;
1408 struct lpfc_sli *psli = &phba->sli;
1409 uint16_t iotag;
1410
1411 spin_lock_irq(&phba->hbalock);
1412 iotag = psli->last_iotag;
1413 if(++iotag < psli->iocbq_lookup_len) {
1414 psli->last_iotag = iotag;
1415 psli->iocbq_lookup[iotag] = iocbq;
1416 spin_unlock_irq(&phba->hbalock);
1417 iocbq->iotag = iotag;
1418 return iotag;
1419 } else if (psli->iocbq_lookup_len < (0xffff
1420 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1421 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1422 spin_unlock_irq(&phba->hbalock);
1423 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1424 GFP_KERNEL);
1425 if (new_arr) {
1426 spin_lock_irq(&phba->hbalock);
1427 old_arr = psli->iocbq_lookup;
1428 if (new_len <= psli->iocbq_lookup_len) {
1429 /* highly unprobable case */
1430 kfree(new_arr);
1431 iotag = psli->last_iotag;
1432 if(++iotag < psli->iocbq_lookup_len) {
1433 psli->last_iotag = iotag;
1434 psli->iocbq_lookup[iotag] = iocbq;
1435 spin_unlock_irq(&phba->hbalock);
1436 iocbq->iotag = iotag;
1437 return iotag;
1438 }
1439 spin_unlock_irq(&phba->hbalock);
1440 return 0;
1441 }
1442 if (psli->iocbq_lookup)
1443 memcpy(new_arr, old_arr,
1444 ((psli->last_iotag + 1) *
1445 sizeof (struct lpfc_iocbq *)));
1446 psli->iocbq_lookup = new_arr;
1447 psli->iocbq_lookup_len = new_len;
1448 psli->last_iotag = iotag;
1449 psli->iocbq_lookup[iotag] = iocbq;
1450 spin_unlock_irq(&phba->hbalock);
1451 iocbq->iotag = iotag;
1452 kfree(old_arr);
1453 return iotag;
1454 }
1455 } else
1456 spin_unlock_irq(&phba->hbalock);
1457
1458 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1459 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1460 psli->last_iotag);
1461
1462 return 0;
1463 }
1464
1465 /**
1466 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1467 * @phba: Pointer to HBA context object.
1468 * @pring: Pointer to driver SLI ring object.
1469 * @iocb: Pointer to iocb slot in the ring.
1470 * @nextiocb: Pointer to driver iocb object which need to be
1471 * posted to firmware.
1472 *
1473 * This function is called with hbalock held to post a new iocb to
1474 * the firmware. This function copies the new iocb to ring iocb slot and
1475 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1476 * a completion call back for this iocb else the function will free the
1477 * iocb object.
1478 **/
1479 static void
1480 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1481 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1482 {
1483 /*
1484 * Set up an iotag
1485 */
1486 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1487
1488
1489 if (pring->ringno == LPFC_ELS_RING) {
1490 lpfc_debugfs_slow_ring_trc(phba,
1491 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1492 *(((uint32_t *) &nextiocb->iocb) + 4),
1493 *(((uint32_t *) &nextiocb->iocb) + 6),
1494 *(((uint32_t *) &nextiocb->iocb) + 7));
1495 }
1496
1497 /*
1498 * Issue iocb command to adapter
1499 */
1500 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1501 wmb();
1502 pring->stats.iocb_cmd++;
1503
1504 /*
1505 * If there is no completion routine to call, we can release the
1506 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1507 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1508 */
1509 if (nextiocb->iocb_cmpl)
1510 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1511 else
1512 __lpfc_sli_release_iocbq(phba, nextiocb);
1513
1514 /*
1515 * Let the HBA know what IOCB slot will be the next one the
1516 * driver will put a command into.
1517 */
1518 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1519 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1520 }
1521
1522 /**
1523 * lpfc_sli_update_full_ring - Update the chip attention register
1524 * @phba: Pointer to HBA context object.
1525 * @pring: Pointer to driver SLI ring object.
1526 *
1527 * The caller is not required to hold any lock for calling this function.
1528 * This function updates the chip attention bits for the ring to inform firmware
1529 * that there are pending work to be done for this ring and requests an
1530 * interrupt when there is space available in the ring. This function is
1531 * called when the driver is unable to post more iocbs to the ring due
1532 * to unavailability of space in the ring.
1533 **/
1534 static void
1535 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1536 {
1537 int ringno = pring->ringno;
1538
1539 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1540
1541 wmb();
1542
1543 /*
1544 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1545 * The HBA will tell us when an IOCB entry is available.
1546 */
1547 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1548 readl(phba->CAregaddr); /* flush */
1549
1550 pring->stats.iocb_cmd_full++;
1551 }
1552
1553 /**
1554 * lpfc_sli_update_ring - Update chip attention register
1555 * @phba: Pointer to HBA context object.
1556 * @pring: Pointer to driver SLI ring object.
1557 *
1558 * This function updates the chip attention register bit for the
1559 * given ring to inform HBA that there is more work to be done
1560 * in this ring. The caller is not required to hold any lock.
1561 **/
1562 static void
1563 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1564 {
1565 int ringno = pring->ringno;
1566
1567 /*
1568 * Tell the HBA that there is work to do in this ring.
1569 */
1570 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1571 wmb();
1572 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1573 readl(phba->CAregaddr); /* flush */
1574 }
1575 }
1576
1577 /**
1578 * lpfc_sli_resume_iocb - Process iocbs in the txq
1579 * @phba: Pointer to HBA context object.
1580 * @pring: Pointer to driver SLI ring object.
1581 *
1582 * This function is called with hbalock held to post pending iocbs
1583 * in the txq to the firmware. This function is called when driver
1584 * detects space available in the ring.
1585 **/
1586 static void
1587 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1588 {
1589 IOCB_t *iocb;
1590 struct lpfc_iocbq *nextiocb;
1591
1592 /*
1593 * Check to see if:
1594 * (a) there is anything on the txq to send
1595 * (b) link is up
1596 * (c) link attention events can be processed (fcp ring only)
1597 * (d) IOCB processing is not blocked by the outstanding mbox command.
1598 */
1599 if (pring->txq_cnt &&
1600 lpfc_is_link_up(phba) &&
1601 (pring->ringno != phba->sli.fcp_ring ||
1602 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1603
1604 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1605 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1606 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1607
1608 if (iocb)
1609 lpfc_sli_update_ring(phba, pring);
1610 else
1611 lpfc_sli_update_full_ring(phba, pring);
1612 }
1613
1614 return;
1615 }
1616
1617 /**
1618 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1619 * @phba: Pointer to HBA context object.
1620 * @hbqno: HBQ number.
1621 *
1622 * This function is called with hbalock held to get the next
1623 * available slot for the given HBQ. If there is free slot
1624 * available for the HBQ it will return pointer to the next available
1625 * HBQ entry else it will return NULL.
1626 **/
1627 static struct lpfc_hbq_entry *
1628 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1629 {
1630 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1631
1632 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1633 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1634 hbqp->next_hbqPutIdx = 0;
1635
1636 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1637 uint32_t raw_index = phba->hbq_get[hbqno];
1638 uint32_t getidx = le32_to_cpu(raw_index);
1639
1640 hbqp->local_hbqGetIdx = getidx;
1641
1642 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1643 lpfc_printf_log(phba, KERN_ERR,
1644 LOG_SLI | LOG_VPORT,
1645 "1802 HBQ %d: local_hbqGetIdx "
1646 "%u is > than hbqp->entry_count %u\n",
1647 hbqno, hbqp->local_hbqGetIdx,
1648 hbqp->entry_count);
1649
1650 phba->link_state = LPFC_HBA_ERROR;
1651 return NULL;
1652 }
1653
1654 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1655 return NULL;
1656 }
1657
1658 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1659 hbqp->hbqPutIdx;
1660 }
1661
1662 /**
1663 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1664 * @phba: Pointer to HBA context object.
1665 *
1666 * This function is called with no lock held to free all the
1667 * hbq buffers while uninitializing the SLI interface. It also
1668 * frees the HBQ buffers returned by the firmware but not yet
1669 * processed by the upper layers.
1670 **/
1671 void
1672 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1673 {
1674 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1675 struct hbq_dmabuf *hbq_buf;
1676 unsigned long flags;
1677 int i, hbq_count;
1678 uint32_t hbqno;
1679
1680 hbq_count = lpfc_sli_hbq_count();
1681 /* Return all memory used by all HBQs */
1682 spin_lock_irqsave(&phba->hbalock, flags);
1683 for (i = 0; i < hbq_count; ++i) {
1684 list_for_each_entry_safe(dmabuf, next_dmabuf,
1685 &phba->hbqs[i].hbq_buffer_list, list) {
1686 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1687 list_del(&hbq_buf->dbuf.list);
1688 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1689 }
1690 phba->hbqs[i].buffer_count = 0;
1691 }
1692 /* Return all HBQ buffer that are in-fly */
1693 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1694 list) {
1695 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1696 list_del(&hbq_buf->dbuf.list);
1697 if (hbq_buf->tag == -1) {
1698 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1699 (phba, hbq_buf);
1700 } else {
1701 hbqno = hbq_buf->tag >> 16;
1702 if (hbqno >= LPFC_MAX_HBQS)
1703 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1704 (phba, hbq_buf);
1705 else
1706 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1707 hbq_buf);
1708 }
1709 }
1710
1711 /* Mark the HBQs not in use */
1712 phba->hbq_in_use = 0;
1713 spin_unlock_irqrestore(&phba->hbalock, flags);
1714 }
1715
1716 /**
1717 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1718 * @phba: Pointer to HBA context object.
1719 * @hbqno: HBQ number.
1720 * @hbq_buf: Pointer to HBQ buffer.
1721 *
1722 * This function is called with the hbalock held to post a
1723 * hbq buffer to the firmware. If the function finds an empty
1724 * slot in the HBQ, it will post the buffer. The function will return
1725 * pointer to the hbq entry if it successfully post the buffer
1726 * else it will return NULL.
1727 **/
1728 static int
1729 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1730 struct hbq_dmabuf *hbq_buf)
1731 {
1732 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1733 }
1734
1735 /**
1736 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1737 * @phba: Pointer to HBA context object.
1738 * @hbqno: HBQ number.
1739 * @hbq_buf: Pointer to HBQ buffer.
1740 *
1741 * This function is called with the hbalock held to post a hbq buffer to the
1742 * firmware. If the function finds an empty slot in the HBQ, it will post the
1743 * buffer and place it on the hbq_buffer_list. The function will return zero if
1744 * it successfully post the buffer else it will return an error.
1745 **/
1746 static int
1747 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1748 struct hbq_dmabuf *hbq_buf)
1749 {
1750 struct lpfc_hbq_entry *hbqe;
1751 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1752
1753 /* Get next HBQ entry slot to use */
1754 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1755 if (hbqe) {
1756 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1757
1758 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1759 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1760 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1761 hbqe->bde.tus.f.bdeFlags = 0;
1762 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1763 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1764 /* Sync SLIM */
1765 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1766 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1767 /* flush */
1768 readl(phba->hbq_put + hbqno);
1769 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1770 return 0;
1771 } else
1772 return -ENOMEM;
1773 }
1774
1775 /**
1776 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1777 * @phba: Pointer to HBA context object.
1778 * @hbqno: HBQ number.
1779 * @hbq_buf: Pointer to HBQ buffer.
1780 *
1781 * This function is called with the hbalock held to post an RQE to the SLI4
1782 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1783 * the hbq_buffer_list and return zero, otherwise it will return an error.
1784 **/
1785 static int
1786 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1787 struct hbq_dmabuf *hbq_buf)
1788 {
1789 int rc;
1790 struct lpfc_rqe hrqe;
1791 struct lpfc_rqe drqe;
1792
1793 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1794 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1795 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1796 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1797 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1798 &hrqe, &drqe);
1799 if (rc < 0)
1800 return rc;
1801 hbq_buf->tag = rc;
1802 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1803 return 0;
1804 }
1805
1806 /* HBQ for ELS and CT traffic. */
1807 static struct lpfc_hbq_init lpfc_els_hbq = {
1808 .rn = 1,
1809 .entry_count = 256,
1810 .mask_count = 0,
1811 .profile = 0,
1812 .ring_mask = (1 << LPFC_ELS_RING),
1813 .buffer_count = 0,
1814 .init_count = 40,
1815 .add_count = 40,
1816 };
1817
1818 /* HBQ for the extra ring if needed */
1819 static struct lpfc_hbq_init lpfc_extra_hbq = {
1820 .rn = 1,
1821 .entry_count = 200,
1822 .mask_count = 0,
1823 .profile = 0,
1824 .ring_mask = (1 << LPFC_EXTRA_RING),
1825 .buffer_count = 0,
1826 .init_count = 0,
1827 .add_count = 5,
1828 };
1829
1830 /* Array of HBQs */
1831 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1832 &lpfc_els_hbq,
1833 &lpfc_extra_hbq,
1834 };
1835
1836 /**
1837 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1838 * @phba: Pointer to HBA context object.
1839 * @hbqno: HBQ number.
1840 * @count: Number of HBQ buffers to be posted.
1841 *
1842 * This function is called with no lock held to post more hbq buffers to the
1843 * given HBQ. The function returns the number of HBQ buffers successfully
1844 * posted.
1845 **/
1846 static int
1847 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1848 {
1849 uint32_t i, posted = 0;
1850 unsigned long flags;
1851 struct hbq_dmabuf *hbq_buffer;
1852 LIST_HEAD(hbq_buf_list);
1853 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1854 return 0;
1855
1856 if ((phba->hbqs[hbqno].buffer_count + count) >
1857 lpfc_hbq_defs[hbqno]->entry_count)
1858 count = lpfc_hbq_defs[hbqno]->entry_count -
1859 phba->hbqs[hbqno].buffer_count;
1860 if (!count)
1861 return 0;
1862 /* Allocate HBQ entries */
1863 for (i = 0; i < count; i++) {
1864 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1865 if (!hbq_buffer)
1866 break;
1867 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1868 }
1869 /* Check whether HBQ is still in use */
1870 spin_lock_irqsave(&phba->hbalock, flags);
1871 if (!phba->hbq_in_use)
1872 goto err;
1873 while (!list_empty(&hbq_buf_list)) {
1874 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1875 dbuf.list);
1876 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1877 (hbqno << 16));
1878 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1879 phba->hbqs[hbqno].buffer_count++;
1880 posted++;
1881 } else
1882 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1883 }
1884 spin_unlock_irqrestore(&phba->hbalock, flags);
1885 return posted;
1886 err:
1887 spin_unlock_irqrestore(&phba->hbalock, flags);
1888 while (!list_empty(&hbq_buf_list)) {
1889 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1890 dbuf.list);
1891 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1892 }
1893 return 0;
1894 }
1895
1896 /**
1897 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1898 * @phba: Pointer to HBA context object.
1899 * @qno: HBQ number.
1900 *
1901 * This function posts more buffers to the HBQ. This function
1902 * is called with no lock held. The function returns the number of HBQ entries
1903 * successfully allocated.
1904 **/
1905 int
1906 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1907 {
1908 if (phba->sli_rev == LPFC_SLI_REV4)
1909 return 0;
1910 else
1911 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1912 lpfc_hbq_defs[qno]->add_count);
1913 }
1914
1915 /**
1916 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1917 * @phba: Pointer to HBA context object.
1918 * @qno: HBQ queue number.
1919 *
1920 * This function is called from SLI initialization code path with
1921 * no lock held to post initial HBQ buffers to firmware. The
1922 * function returns the number of HBQ entries successfully allocated.
1923 **/
1924 static int
1925 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1926 {
1927 if (phba->sli_rev == LPFC_SLI_REV4)
1928 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1929 lpfc_hbq_defs[qno]->entry_count);
1930 else
1931 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1932 lpfc_hbq_defs[qno]->init_count);
1933 }
1934
1935 /**
1936 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1937 * @phba: Pointer to HBA context object.
1938 * @hbqno: HBQ number.
1939 *
1940 * This function removes the first hbq buffer on an hbq list and returns a
1941 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1942 **/
1943 static struct hbq_dmabuf *
1944 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1945 {
1946 struct lpfc_dmabuf *d_buf;
1947
1948 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1949 if (!d_buf)
1950 return NULL;
1951 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1952 }
1953
1954 /**
1955 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1956 * @phba: Pointer to HBA context object.
1957 * @tag: Tag of the hbq buffer.
1958 *
1959 * This function is called with hbalock held. This function searches
1960 * for the hbq buffer associated with the given tag in the hbq buffer
1961 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1962 * it returns NULL.
1963 **/
1964 static struct hbq_dmabuf *
1965 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1966 {
1967 struct lpfc_dmabuf *d_buf;
1968 struct hbq_dmabuf *hbq_buf;
1969 uint32_t hbqno;
1970
1971 hbqno = tag >> 16;
1972 if (hbqno >= LPFC_MAX_HBQS)
1973 return NULL;
1974
1975 spin_lock_irq(&phba->hbalock);
1976 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1977 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1978 if (hbq_buf->tag == tag) {
1979 spin_unlock_irq(&phba->hbalock);
1980 return hbq_buf;
1981 }
1982 }
1983 spin_unlock_irq(&phba->hbalock);
1984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1985 "1803 Bad hbq tag. Data: x%x x%x\n",
1986 tag, phba->hbqs[tag >> 16].buffer_count);
1987 return NULL;
1988 }
1989
1990 /**
1991 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1992 * @phba: Pointer to HBA context object.
1993 * @hbq_buffer: Pointer to HBQ buffer.
1994 *
1995 * This function is called with hbalock. This function gives back
1996 * the hbq buffer to firmware. If the HBQ does not have space to
1997 * post the buffer, it will free the buffer.
1998 **/
1999 void
2000 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2001 {
2002 uint32_t hbqno;
2003
2004 if (hbq_buffer) {
2005 hbqno = hbq_buffer->tag >> 16;
2006 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2007 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2008 }
2009 }
2010
2011 /**
2012 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2013 * @mbxCommand: mailbox command code.
2014 *
2015 * This function is called by the mailbox event handler function to verify
2016 * that the completed mailbox command is a legitimate mailbox command. If the
2017 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2018 * and the mailbox event handler will take the HBA offline.
2019 **/
2020 static int
2021 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2022 {
2023 uint8_t ret;
2024
2025 switch (mbxCommand) {
2026 case MBX_LOAD_SM:
2027 case MBX_READ_NV:
2028 case MBX_WRITE_NV:
2029 case MBX_WRITE_VPARMS:
2030 case MBX_RUN_BIU_DIAG:
2031 case MBX_INIT_LINK:
2032 case MBX_DOWN_LINK:
2033 case MBX_CONFIG_LINK:
2034 case MBX_CONFIG_RING:
2035 case MBX_RESET_RING:
2036 case MBX_READ_CONFIG:
2037 case MBX_READ_RCONFIG:
2038 case MBX_READ_SPARM:
2039 case MBX_READ_STATUS:
2040 case MBX_READ_RPI:
2041 case MBX_READ_XRI:
2042 case MBX_READ_REV:
2043 case MBX_READ_LNK_STAT:
2044 case MBX_REG_LOGIN:
2045 case MBX_UNREG_LOGIN:
2046 case MBX_CLEAR_LA:
2047 case MBX_DUMP_MEMORY:
2048 case MBX_DUMP_CONTEXT:
2049 case MBX_RUN_DIAGS:
2050 case MBX_RESTART:
2051 case MBX_UPDATE_CFG:
2052 case MBX_DOWN_LOAD:
2053 case MBX_DEL_LD_ENTRY:
2054 case MBX_RUN_PROGRAM:
2055 case MBX_SET_MASK:
2056 case MBX_SET_VARIABLE:
2057 case MBX_UNREG_D_ID:
2058 case MBX_KILL_BOARD:
2059 case MBX_CONFIG_FARP:
2060 case MBX_BEACON:
2061 case MBX_LOAD_AREA:
2062 case MBX_RUN_BIU_DIAG64:
2063 case MBX_CONFIG_PORT:
2064 case MBX_READ_SPARM64:
2065 case MBX_READ_RPI64:
2066 case MBX_REG_LOGIN64:
2067 case MBX_READ_TOPOLOGY:
2068 case MBX_WRITE_WWN:
2069 case MBX_SET_DEBUG:
2070 case MBX_LOAD_EXP_ROM:
2071 case MBX_ASYNCEVT_ENABLE:
2072 case MBX_REG_VPI:
2073 case MBX_UNREG_VPI:
2074 case MBX_HEARTBEAT:
2075 case MBX_PORT_CAPABILITIES:
2076 case MBX_PORT_IOV_CONTROL:
2077 case MBX_SLI4_CONFIG:
2078 case MBX_SLI4_REQ_FTRS:
2079 case MBX_REG_FCFI:
2080 case MBX_UNREG_FCFI:
2081 case MBX_REG_VFI:
2082 case MBX_UNREG_VFI:
2083 case MBX_INIT_VPI:
2084 case MBX_INIT_VFI:
2085 case MBX_RESUME_RPI:
2086 case MBX_READ_EVENT_LOG_STATUS:
2087 case MBX_READ_EVENT_LOG:
2088 case MBX_SECURITY_MGMT:
2089 case MBX_AUTH_PORT:
2090 ret = mbxCommand;
2091 break;
2092 default:
2093 ret = MBX_SHUTDOWN;
2094 break;
2095 }
2096 return ret;
2097 }
2098
2099 /**
2100 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2101 * @phba: Pointer to HBA context object.
2102 * @pmboxq: Pointer to mailbox command.
2103 *
2104 * This is completion handler function for mailbox commands issued from
2105 * lpfc_sli_issue_mbox_wait function. This function is called by the
2106 * mailbox event handler function with no lock held. This function
2107 * will wake up thread waiting on the wait queue pointed by context1
2108 * of the mailbox.
2109 **/
2110 void
2111 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2112 {
2113 wait_queue_head_t *pdone_q;
2114 unsigned long drvr_flag;
2115
2116 /*
2117 * If pdone_q is empty, the driver thread gave up waiting and
2118 * continued running.
2119 */
2120 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2121 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2122 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2123 if (pdone_q)
2124 wake_up_interruptible(pdone_q);
2125 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2126 return;
2127 }
2128
2129
2130 /**
2131 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2132 * @phba: Pointer to HBA context object.
2133 * @pmb: Pointer to mailbox object.
2134 *
2135 * This function is the default mailbox completion handler. It
2136 * frees the memory resources associated with the completed mailbox
2137 * command. If the completed command is a REG_LOGIN mailbox command,
2138 * this function will issue a UREG_LOGIN to re-claim the RPI.
2139 **/
2140 void
2141 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2142 {
2143 struct lpfc_vport *vport = pmb->vport;
2144 struct lpfc_dmabuf *mp;
2145 struct lpfc_nodelist *ndlp;
2146 struct Scsi_Host *shost;
2147 uint16_t rpi, vpi;
2148 int rc;
2149
2150 mp = (struct lpfc_dmabuf *) (pmb->context1);
2151
2152 if (mp) {
2153 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2154 kfree(mp);
2155 }
2156
2157 /*
2158 * If a REG_LOGIN succeeded after node is destroyed or node
2159 * is in re-discovery driver need to cleanup the RPI.
2160 */
2161 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2162 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2163 !pmb->u.mb.mbxStatus) {
2164 rpi = pmb->u.mb.un.varWords[0];
2165 vpi = pmb->u.mb.un.varRegLogin.vpi;
2166 lpfc_unreg_login(phba, vpi, rpi, pmb);
2167 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2168 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2169 if (rc != MBX_NOT_FINISHED)
2170 return;
2171 }
2172
2173 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2174 !(phba->pport->load_flag & FC_UNLOADING) &&
2175 !pmb->u.mb.mbxStatus) {
2176 shost = lpfc_shost_from_vport(vport);
2177 spin_lock_irq(shost->host_lock);
2178 vport->vpi_state |= LPFC_VPI_REGISTERED;
2179 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2180 spin_unlock_irq(shost->host_lock);
2181 }
2182
2183 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2184 ndlp = (struct lpfc_nodelist *)pmb->context2;
2185 lpfc_nlp_put(ndlp);
2186 pmb->context2 = NULL;
2187 }
2188
2189 /* Check security permission status on INIT_LINK mailbox command */
2190 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2191 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2193 "2860 SLI authentication is required "
2194 "for INIT_LINK but has not done yet\n");
2195
2196 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2197 lpfc_sli4_mbox_cmd_free(phba, pmb);
2198 else
2199 mempool_free(pmb, phba->mbox_mem_pool);
2200 }
2201
2202 /**
2203 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2204 * @phba: Pointer to HBA context object.
2205 *
2206 * This function is called with no lock held. This function processes all
2207 * the completed mailbox commands and gives it to upper layers. The interrupt
2208 * service routine processes mailbox completion interrupt and adds completed
2209 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2210 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2211 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2212 * function returns the mailbox commands to the upper layer by calling the
2213 * completion handler function of each mailbox.
2214 **/
2215 int
2216 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2217 {
2218 MAILBOX_t *pmbox;
2219 LPFC_MBOXQ_t *pmb;
2220 int rc;
2221 LIST_HEAD(cmplq);
2222
2223 phba->sli.slistat.mbox_event++;
2224
2225 /* Get all completed mailboxe buffers into the cmplq */
2226 spin_lock_irq(&phba->hbalock);
2227 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2228 spin_unlock_irq(&phba->hbalock);
2229
2230 /* Get a Mailbox buffer to setup mailbox commands for callback */
2231 do {
2232 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2233 if (pmb == NULL)
2234 break;
2235
2236 pmbox = &pmb->u.mb;
2237
2238 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2239 if (pmb->vport) {
2240 lpfc_debugfs_disc_trc(pmb->vport,
2241 LPFC_DISC_TRC_MBOX_VPORT,
2242 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2243 (uint32_t)pmbox->mbxCommand,
2244 pmbox->un.varWords[0],
2245 pmbox->un.varWords[1]);
2246 }
2247 else {
2248 lpfc_debugfs_disc_trc(phba->pport,
2249 LPFC_DISC_TRC_MBOX,
2250 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2251 (uint32_t)pmbox->mbxCommand,
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1]);
2254 }
2255 }
2256
2257 /*
2258 * It is a fatal error if unknown mbox command completion.
2259 */
2260 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2261 MBX_SHUTDOWN) {
2262 /* Unknown mailbox command compl */
2263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2264 "(%d):0323 Unknown Mailbox command "
2265 "x%x (x%x/x%x) Cmpl\n",
2266 pmb->vport ? pmb->vport->vpi : 0,
2267 pmbox->mbxCommand,
2268 lpfc_sli_config_mbox_subsys_get(phba,
2269 pmb),
2270 lpfc_sli_config_mbox_opcode_get(phba,
2271 pmb));
2272 phba->link_state = LPFC_HBA_ERROR;
2273 phba->work_hs = HS_FFER3;
2274 lpfc_handle_eratt(phba);
2275 continue;
2276 }
2277
2278 if (pmbox->mbxStatus) {
2279 phba->sli.slistat.mbox_stat_err++;
2280 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2281 /* Mbox cmd cmpl error - RETRYing */
2282 lpfc_printf_log(phba, KERN_INFO,
2283 LOG_MBOX | LOG_SLI,
2284 "(%d):0305 Mbox cmd cmpl "
2285 "error - RETRYing Data: x%x "
2286 "(x%x/x%x) x%x x%x x%x\n",
2287 pmb->vport ? pmb->vport->vpi : 0,
2288 pmbox->mbxCommand,
2289 lpfc_sli_config_mbox_subsys_get(phba,
2290 pmb),
2291 lpfc_sli_config_mbox_opcode_get(phba,
2292 pmb),
2293 pmbox->mbxStatus,
2294 pmbox->un.varWords[0],
2295 pmb->vport->port_state);
2296 pmbox->mbxStatus = 0;
2297 pmbox->mbxOwner = OWN_HOST;
2298 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2299 if (rc != MBX_NOT_FINISHED)
2300 continue;
2301 }
2302 }
2303
2304 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2305 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2306 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2307 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2308 pmb->vport ? pmb->vport->vpi : 0,
2309 pmbox->mbxCommand,
2310 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2311 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2312 pmb->mbox_cmpl,
2313 *((uint32_t *) pmbox),
2314 pmbox->un.varWords[0],
2315 pmbox->un.varWords[1],
2316 pmbox->un.varWords[2],
2317 pmbox->un.varWords[3],
2318 pmbox->un.varWords[4],
2319 pmbox->un.varWords[5],
2320 pmbox->un.varWords[6],
2321 pmbox->un.varWords[7]);
2322
2323 if (pmb->mbox_cmpl)
2324 pmb->mbox_cmpl(phba,pmb);
2325 } while (1);
2326 return 0;
2327 }
2328
2329 /**
2330 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2331 * @phba: Pointer to HBA context object.
2332 * @pring: Pointer to driver SLI ring object.
2333 * @tag: buffer tag.
2334 *
2335 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2336 * is set in the tag the buffer is posted for a particular exchange,
2337 * the function will return the buffer without replacing the buffer.
2338 * If the buffer is for unsolicited ELS or CT traffic, this function
2339 * returns the buffer and also posts another buffer to the firmware.
2340 **/
2341 static struct lpfc_dmabuf *
2342 lpfc_sli_get_buff(struct lpfc_hba *phba,
2343 struct lpfc_sli_ring *pring,
2344 uint32_t tag)
2345 {
2346 struct hbq_dmabuf *hbq_entry;
2347
2348 if (tag & QUE_BUFTAG_BIT)
2349 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2350 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2351 if (!hbq_entry)
2352 return NULL;
2353 return &hbq_entry->dbuf;
2354 }
2355
2356 /**
2357 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2358 * @phba: Pointer to HBA context object.
2359 * @pring: Pointer to driver SLI ring object.
2360 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2361 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2362 * @fch_type: the type for the first frame of the sequence.
2363 *
2364 * This function is called with no lock held. This function uses the r_ctl and
2365 * type of the received sequence to find the correct callback function to call
2366 * to process the sequence.
2367 **/
2368 static int
2369 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2370 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2371 uint32_t fch_type)
2372 {
2373 int i;
2374
2375 /* unSolicited Responses */
2376 if (pring->prt[0].profile) {
2377 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2378 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2379 saveq);
2380 return 1;
2381 }
2382 /* We must search, based on rctl / type
2383 for the right routine */
2384 for (i = 0; i < pring->num_mask; i++) {
2385 if ((pring->prt[i].rctl == fch_r_ctl) &&
2386 (pring->prt[i].type == fch_type)) {
2387 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2388 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2389 (phba, pring, saveq);
2390 return 1;
2391 }
2392 }
2393 return 0;
2394 }
2395
2396 /**
2397 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2398 * @phba: Pointer to HBA context object.
2399 * @pring: Pointer to driver SLI ring object.
2400 * @saveq: Pointer to the unsolicited iocb.
2401 *
2402 * This function is called with no lock held by the ring event handler
2403 * when there is an unsolicited iocb posted to the response ring by the
2404 * firmware. This function gets the buffer associated with the iocbs
2405 * and calls the event handler for the ring. This function handles both
2406 * qring buffers and hbq buffers.
2407 * When the function returns 1 the caller can free the iocb object otherwise
2408 * upper layer functions will free the iocb objects.
2409 **/
2410 static int
2411 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2412 struct lpfc_iocbq *saveq)
2413 {
2414 IOCB_t * irsp;
2415 WORD5 * w5p;
2416 uint32_t Rctl, Type;
2417 uint32_t match;
2418 struct lpfc_iocbq *iocbq;
2419 struct lpfc_dmabuf *dmzbuf;
2420
2421 match = 0;
2422 irsp = &(saveq->iocb);
2423
2424 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2425 if (pring->lpfc_sli_rcv_async_status)
2426 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2427 else
2428 lpfc_printf_log(phba,
2429 KERN_WARNING,
2430 LOG_SLI,
2431 "0316 Ring %d handler: unexpected "
2432 "ASYNC_STATUS iocb received evt_code "
2433 "0x%x\n",
2434 pring->ringno,
2435 irsp->un.asyncstat.evt_code);
2436 return 1;
2437 }
2438
2439 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2440 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2441 if (irsp->ulpBdeCount > 0) {
2442 dmzbuf = lpfc_sli_get_buff(phba, pring,
2443 irsp->un.ulpWord[3]);
2444 lpfc_in_buf_free(phba, dmzbuf);
2445 }
2446
2447 if (irsp->ulpBdeCount > 1) {
2448 dmzbuf = lpfc_sli_get_buff(phba, pring,
2449 irsp->unsli3.sli3Words[3]);
2450 lpfc_in_buf_free(phba, dmzbuf);
2451 }
2452
2453 if (irsp->ulpBdeCount > 2) {
2454 dmzbuf = lpfc_sli_get_buff(phba, pring,
2455 irsp->unsli3.sli3Words[7]);
2456 lpfc_in_buf_free(phba, dmzbuf);
2457 }
2458
2459 return 1;
2460 }
2461
2462 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2463 if (irsp->ulpBdeCount != 0) {
2464 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2465 irsp->un.ulpWord[3]);
2466 if (!saveq->context2)
2467 lpfc_printf_log(phba,
2468 KERN_ERR,
2469 LOG_SLI,
2470 "0341 Ring %d Cannot find buffer for "
2471 "an unsolicited iocb. tag 0x%x\n",
2472 pring->ringno,
2473 irsp->un.ulpWord[3]);
2474 }
2475 if (irsp->ulpBdeCount == 2) {
2476 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2477 irsp->unsli3.sli3Words[7]);
2478 if (!saveq->context3)
2479 lpfc_printf_log(phba,
2480 KERN_ERR,
2481 LOG_SLI,
2482 "0342 Ring %d Cannot find buffer for an"
2483 " unsolicited iocb. tag 0x%x\n",
2484 pring->ringno,
2485 irsp->unsli3.sli3Words[7]);
2486 }
2487 list_for_each_entry(iocbq, &saveq->list, list) {
2488 irsp = &(iocbq->iocb);
2489 if (irsp->ulpBdeCount != 0) {
2490 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2491 irsp->un.ulpWord[3]);
2492 if (!iocbq->context2)
2493 lpfc_printf_log(phba,
2494 KERN_ERR,
2495 LOG_SLI,
2496 "0343 Ring %d Cannot find "
2497 "buffer for an unsolicited iocb"
2498 ". tag 0x%x\n", pring->ringno,
2499 irsp->un.ulpWord[3]);
2500 }
2501 if (irsp->ulpBdeCount == 2) {
2502 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2503 irsp->unsli3.sli3Words[7]);
2504 if (!iocbq->context3)
2505 lpfc_printf_log(phba,
2506 KERN_ERR,
2507 LOG_SLI,
2508 "0344 Ring %d Cannot find "
2509 "buffer for an unsolicited "
2510 "iocb. tag 0x%x\n",
2511 pring->ringno,
2512 irsp->unsli3.sli3Words[7]);
2513 }
2514 }
2515 }
2516 if (irsp->ulpBdeCount != 0 &&
2517 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2518 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2519 int found = 0;
2520
2521 /* search continue save q for same XRI */
2522 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2523 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2524 saveq->iocb.unsli3.rcvsli3.ox_id) {
2525 list_add_tail(&saveq->list, &iocbq->list);
2526 found = 1;
2527 break;
2528 }
2529 }
2530 if (!found)
2531 list_add_tail(&saveq->clist,
2532 &pring->iocb_continue_saveq);
2533 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2534 list_del_init(&iocbq->clist);
2535 saveq = iocbq;
2536 irsp = &(saveq->iocb);
2537 } else
2538 return 0;
2539 }
2540 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2541 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2542 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2543 Rctl = FC_RCTL_ELS_REQ;
2544 Type = FC_TYPE_ELS;
2545 } else {
2546 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2547 Rctl = w5p->hcsw.Rctl;
2548 Type = w5p->hcsw.Type;
2549
2550 /* Firmware Workaround */
2551 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2552 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2553 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2554 Rctl = FC_RCTL_ELS_REQ;
2555 Type = FC_TYPE_ELS;
2556 w5p->hcsw.Rctl = Rctl;
2557 w5p->hcsw.Type = Type;
2558 }
2559 }
2560
2561 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2562 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2563 "0313 Ring %d handler: unexpected Rctl x%x "
2564 "Type x%x received\n",
2565 pring->ringno, Rctl, Type);
2566
2567 return 1;
2568 }
2569
2570 /**
2571 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2572 * @phba: Pointer to HBA context object.
2573 * @pring: Pointer to driver SLI ring object.
2574 * @prspiocb: Pointer to response iocb object.
2575 *
2576 * This function looks up the iocb_lookup table to get the command iocb
2577 * corresponding to the given response iocb using the iotag of the
2578 * response iocb. This function is called with the hbalock held.
2579 * This function returns the command iocb object if it finds the command
2580 * iocb else returns NULL.
2581 **/
2582 static struct lpfc_iocbq *
2583 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2584 struct lpfc_sli_ring *pring,
2585 struct lpfc_iocbq *prspiocb)
2586 {
2587 struct lpfc_iocbq *cmd_iocb = NULL;
2588 uint16_t iotag;
2589
2590 iotag = prspiocb->iocb.ulpIoTag;
2591
2592 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2593 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2594 list_del_init(&cmd_iocb->list);
2595 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2596 pring->txcmplq_cnt--;
2597 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2598 }
2599 return cmd_iocb;
2600 }
2601
2602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2603 "0317 iotag x%x is out off "
2604 "range: max iotag x%x wd0 x%x\n",
2605 iotag, phba->sli.last_iotag,
2606 *(((uint32_t *) &prspiocb->iocb) + 7));
2607 return NULL;
2608 }
2609
2610 /**
2611 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2612 * @phba: Pointer to HBA context object.
2613 * @pring: Pointer to driver SLI ring object.
2614 * @iotag: IOCB tag.
2615 *
2616 * This function looks up the iocb_lookup table to get the command iocb
2617 * corresponding to the given iotag. This function is called with the
2618 * hbalock held.
2619 * This function returns the command iocb object if it finds the command
2620 * iocb else returns NULL.
2621 **/
2622 static struct lpfc_iocbq *
2623 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2624 struct lpfc_sli_ring *pring, uint16_t iotag)
2625 {
2626 struct lpfc_iocbq *cmd_iocb;
2627
2628 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2629 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2630 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2631 /* remove from txcmpl queue list */
2632 list_del_init(&cmd_iocb->list);
2633 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2634 pring->txcmplq_cnt--;
2635 return cmd_iocb;
2636 }
2637 }
2638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2639 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2640 iotag, phba->sli.last_iotag);
2641 return NULL;
2642 }
2643
2644 /**
2645 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2646 * @phba: Pointer to HBA context object.
2647 * @pring: Pointer to driver SLI ring object.
2648 * @saveq: Pointer to the response iocb to be processed.
2649 *
2650 * This function is called by the ring event handler for non-fcp
2651 * rings when there is a new response iocb in the response ring.
2652 * The caller is not required to hold any locks. This function
2653 * gets the command iocb associated with the response iocb and
2654 * calls the completion handler for the command iocb. If there
2655 * is no completion handler, the function will free the resources
2656 * associated with command iocb. If the response iocb is for
2657 * an already aborted command iocb, the status of the completion
2658 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2659 * This function always returns 1.
2660 **/
2661 static int
2662 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2663 struct lpfc_iocbq *saveq)
2664 {
2665 struct lpfc_iocbq *cmdiocbp;
2666 int rc = 1;
2667 unsigned long iflag;
2668
2669 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2670 spin_lock_irqsave(&phba->hbalock, iflag);
2671 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2672 spin_unlock_irqrestore(&phba->hbalock, iflag);
2673
2674 if (cmdiocbp) {
2675 if (cmdiocbp->iocb_cmpl) {
2676 /*
2677 * If an ELS command failed send an event to mgmt
2678 * application.
2679 */
2680 if (saveq->iocb.ulpStatus &&
2681 (pring->ringno == LPFC_ELS_RING) &&
2682 (cmdiocbp->iocb.ulpCommand ==
2683 CMD_ELS_REQUEST64_CR))
2684 lpfc_send_els_failure_event(phba,
2685 cmdiocbp, saveq);
2686
2687 /*
2688 * Post all ELS completions to the worker thread.
2689 * All other are passed to the completion callback.
2690 */
2691 if (pring->ringno == LPFC_ELS_RING) {
2692 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2693 (cmdiocbp->iocb_flag &
2694 LPFC_DRIVER_ABORTED)) {
2695 spin_lock_irqsave(&phba->hbalock,
2696 iflag);
2697 cmdiocbp->iocb_flag &=
2698 ~LPFC_DRIVER_ABORTED;
2699 spin_unlock_irqrestore(&phba->hbalock,
2700 iflag);
2701 saveq->iocb.ulpStatus =
2702 IOSTAT_LOCAL_REJECT;
2703 saveq->iocb.un.ulpWord[4] =
2704 IOERR_SLI_ABORTED;
2705
2706 /* Firmware could still be in progress
2707 * of DMAing payload, so don't free data
2708 * buffer till after a hbeat.
2709 */
2710 spin_lock_irqsave(&phba->hbalock,
2711 iflag);
2712 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2713 spin_unlock_irqrestore(&phba->hbalock,
2714 iflag);
2715 }
2716 if (phba->sli_rev == LPFC_SLI_REV4) {
2717 if (saveq->iocb_flag &
2718 LPFC_EXCHANGE_BUSY) {
2719 /* Set cmdiocb flag for the
2720 * exchange busy so sgl (xri)
2721 * will not be released until
2722 * the abort xri is received
2723 * from hba.
2724 */
2725 spin_lock_irqsave(
2726 &phba->hbalock, iflag);
2727 cmdiocbp->iocb_flag |=
2728 LPFC_EXCHANGE_BUSY;
2729 spin_unlock_irqrestore(
2730 &phba->hbalock, iflag);
2731 }
2732 if (cmdiocbp->iocb_flag &
2733 LPFC_DRIVER_ABORTED) {
2734 /*
2735 * Clear LPFC_DRIVER_ABORTED
2736 * bit in case it was driver
2737 * initiated abort.
2738 */
2739 spin_lock_irqsave(
2740 &phba->hbalock, iflag);
2741 cmdiocbp->iocb_flag &=
2742 ~LPFC_DRIVER_ABORTED;
2743 spin_unlock_irqrestore(
2744 &phba->hbalock, iflag);
2745 cmdiocbp->iocb.ulpStatus =
2746 IOSTAT_LOCAL_REJECT;
2747 cmdiocbp->iocb.un.ulpWord[4] =
2748 IOERR_ABORT_REQUESTED;
2749 /*
2750 * For SLI4, irsiocb contains
2751 * NO_XRI in sli_xritag, it
2752 * shall not affect releasing
2753 * sgl (xri) process.
2754 */
2755 saveq->iocb.ulpStatus =
2756 IOSTAT_LOCAL_REJECT;
2757 saveq->iocb.un.ulpWord[4] =
2758 IOERR_SLI_ABORTED;
2759 spin_lock_irqsave(
2760 &phba->hbalock, iflag);
2761 saveq->iocb_flag |=
2762 LPFC_DELAY_MEM_FREE;
2763 spin_unlock_irqrestore(
2764 &phba->hbalock, iflag);
2765 }
2766 }
2767 }
2768 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2769 } else
2770 lpfc_sli_release_iocbq(phba, cmdiocbp);
2771 } else {
2772 /*
2773 * Unknown initiating command based on the response iotag.
2774 * This could be the case on the ELS ring because of
2775 * lpfc_els_abort().
2776 */
2777 if (pring->ringno != LPFC_ELS_RING) {
2778 /*
2779 * Ring <ringno> handler: unexpected completion IoTag
2780 * <IoTag>
2781 */
2782 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2783 "0322 Ring %d handler: "
2784 "unexpected completion IoTag x%x "
2785 "Data: x%x x%x x%x x%x\n",
2786 pring->ringno,
2787 saveq->iocb.ulpIoTag,
2788 saveq->iocb.ulpStatus,
2789 saveq->iocb.un.ulpWord[4],
2790 saveq->iocb.ulpCommand,
2791 saveq->iocb.ulpContext);
2792 }
2793 }
2794
2795 return rc;
2796 }
2797
2798 /**
2799 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2800 * @phba: Pointer to HBA context object.
2801 * @pring: Pointer to driver SLI ring object.
2802 *
2803 * This function is called from the iocb ring event handlers when
2804 * put pointer is ahead of the get pointer for a ring. This function signal
2805 * an error attention condition to the worker thread and the worker
2806 * thread will transition the HBA to offline state.
2807 **/
2808 static void
2809 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2810 {
2811 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2812 /*
2813 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2814 * rsp ring <portRspMax>
2815 */
2816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2817 "0312 Ring %d handler: portRspPut %d "
2818 "is bigger than rsp ring %d\n",
2819 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2820 pring->sli.sli3.numRiocb);
2821
2822 phba->link_state = LPFC_HBA_ERROR;
2823
2824 /*
2825 * All error attention handlers are posted to
2826 * worker thread
2827 */
2828 phba->work_ha |= HA_ERATT;
2829 phba->work_hs = HS_FFER3;
2830
2831 lpfc_worker_wake_up(phba);
2832
2833 return;
2834 }
2835
2836 /**
2837 * lpfc_poll_eratt - Error attention polling timer timeout handler
2838 * @ptr: Pointer to address of HBA context object.
2839 *
2840 * This function is invoked by the Error Attention polling timer when the
2841 * timer times out. It will check the SLI Error Attention register for
2842 * possible attention events. If so, it will post an Error Attention event
2843 * and wake up worker thread to process it. Otherwise, it will set up the
2844 * Error Attention polling timer for the next poll.
2845 **/
2846 void lpfc_poll_eratt(unsigned long ptr)
2847 {
2848 struct lpfc_hba *phba;
2849 uint32_t eratt = 0, rem;
2850 uint64_t sli_intr, cnt;
2851
2852 phba = (struct lpfc_hba *)ptr;
2853
2854 /* Here we will also keep track of interrupts per sec of the hba */
2855 sli_intr = phba->sli.slistat.sli_intr;
2856
2857 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2858 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2859 sli_intr);
2860 else
2861 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2862
2863 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2864 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2865 phba->sli.slistat.sli_ips = cnt;
2866
2867 phba->sli.slistat.sli_prev_intr = sli_intr;
2868
2869 /* Check chip HA register for error event */
2870 eratt = lpfc_sli_check_eratt(phba);
2871
2872 if (eratt)
2873 /* Tell the worker thread there is work to do */
2874 lpfc_worker_wake_up(phba);
2875 else
2876 /* Restart the timer for next eratt poll */
2877 mod_timer(&phba->eratt_poll, jiffies +
2878 HZ * LPFC_ERATT_POLL_INTERVAL);
2879 return;
2880 }
2881
2882
2883 /**
2884 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2885 * @phba: Pointer to HBA context object.
2886 * @pring: Pointer to driver SLI ring object.
2887 * @mask: Host attention register mask for this ring.
2888 *
2889 * This function is called from the interrupt context when there is a ring
2890 * event for the fcp ring. The caller does not hold any lock.
2891 * The function processes each response iocb in the response ring until it
2892 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2893 * LE bit set. The function will call the completion handler of the command iocb
2894 * if the response iocb indicates a completion for a command iocb or it is
2895 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2896 * function if this is an unsolicited iocb.
2897 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2898 * to check it explicitly.
2899 */
2900 int
2901 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2902 struct lpfc_sli_ring *pring, uint32_t mask)
2903 {
2904 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2905 IOCB_t *irsp = NULL;
2906 IOCB_t *entry = NULL;
2907 struct lpfc_iocbq *cmdiocbq = NULL;
2908 struct lpfc_iocbq rspiocbq;
2909 uint32_t status;
2910 uint32_t portRspPut, portRspMax;
2911 int rc = 1;
2912 lpfc_iocb_type type;
2913 unsigned long iflag;
2914 uint32_t rsp_cmpl = 0;
2915
2916 spin_lock_irqsave(&phba->hbalock, iflag);
2917 pring->stats.iocb_event++;
2918
2919 /*
2920 * The next available response entry should never exceed the maximum
2921 * entries. If it does, treat it as an adapter hardware error.
2922 */
2923 portRspMax = pring->sli.sli3.numRiocb;
2924 portRspPut = le32_to_cpu(pgp->rspPutInx);
2925 if (unlikely(portRspPut >= portRspMax)) {
2926 lpfc_sli_rsp_pointers_error(phba, pring);
2927 spin_unlock_irqrestore(&phba->hbalock, iflag);
2928 return 1;
2929 }
2930 if (phba->fcp_ring_in_use) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 return 1;
2933 } else
2934 phba->fcp_ring_in_use = 1;
2935
2936 rmb();
2937 while (pring->sli.sli3.rspidx != portRspPut) {
2938 /*
2939 * Fetch an entry off the ring and copy it into a local data
2940 * structure. The copy involves a byte-swap since the
2941 * network byte order and pci byte orders are different.
2942 */
2943 entry = lpfc_resp_iocb(phba, pring);
2944 phba->last_completion_time = jiffies;
2945
2946 if (++pring->sli.sli3.rspidx >= portRspMax)
2947 pring->sli.sli3.rspidx = 0;
2948
2949 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2950 (uint32_t *) &rspiocbq.iocb,
2951 phba->iocb_rsp_size);
2952 INIT_LIST_HEAD(&(rspiocbq.list));
2953 irsp = &rspiocbq.iocb;
2954
2955 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2956 pring->stats.iocb_rsp++;
2957 rsp_cmpl++;
2958
2959 if (unlikely(irsp->ulpStatus)) {
2960 /*
2961 * If resource errors reported from HBA, reduce
2962 * queuedepths of the SCSI device.
2963 */
2964 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2965 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2966 spin_unlock_irqrestore(&phba->hbalock, iflag);
2967 phba->lpfc_rampdown_queue_depth(phba);
2968 spin_lock_irqsave(&phba->hbalock, iflag);
2969 }
2970
2971 /* Rsp ring <ringno> error: IOCB */
2972 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2973 "0336 Rsp Ring %d error: IOCB Data: "
2974 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2975 pring->ringno,
2976 irsp->un.ulpWord[0],
2977 irsp->un.ulpWord[1],
2978 irsp->un.ulpWord[2],
2979 irsp->un.ulpWord[3],
2980 irsp->un.ulpWord[4],
2981 irsp->un.ulpWord[5],
2982 *(uint32_t *)&irsp->un1,
2983 *((uint32_t *)&irsp->un1 + 1));
2984 }
2985
2986 switch (type) {
2987 case LPFC_ABORT_IOCB:
2988 case LPFC_SOL_IOCB:
2989 /*
2990 * Idle exchange closed via ABTS from port. No iocb
2991 * resources need to be recovered.
2992 */
2993 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2995 "0333 IOCB cmd 0x%x"
2996 " processed. Skipping"
2997 " completion\n",
2998 irsp->ulpCommand);
2999 break;
3000 }
3001
3002 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3003 &rspiocbq);
3004 if (unlikely(!cmdiocbq))
3005 break;
3006 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3007 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3008 if (cmdiocbq->iocb_cmpl) {
3009 spin_unlock_irqrestore(&phba->hbalock, iflag);
3010 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3011 &rspiocbq);
3012 spin_lock_irqsave(&phba->hbalock, iflag);
3013 }
3014 break;
3015 case LPFC_UNSOL_IOCB:
3016 spin_unlock_irqrestore(&phba->hbalock, iflag);
3017 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3018 spin_lock_irqsave(&phba->hbalock, iflag);
3019 break;
3020 default:
3021 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3022 char adaptermsg[LPFC_MAX_ADPTMSG];
3023 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3024 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3025 MAX_MSG_DATA);
3026 dev_warn(&((phba->pcidev)->dev),
3027 "lpfc%d: %s\n",
3028 phba->brd_no, adaptermsg);
3029 } else {
3030 /* Unknown IOCB command */
3031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3032 "0334 Unknown IOCB command "
3033 "Data: x%x, x%x x%x x%x x%x\n",
3034 type, irsp->ulpCommand,
3035 irsp->ulpStatus,
3036 irsp->ulpIoTag,
3037 irsp->ulpContext);
3038 }
3039 break;
3040 }
3041
3042 /*
3043 * The response IOCB has been processed. Update the ring
3044 * pointer in SLIM. If the port response put pointer has not
3045 * been updated, sync the pgp->rspPutInx and fetch the new port
3046 * response put pointer.
3047 */
3048 writel(pring->sli.sli3.rspidx,
3049 &phba->host_gp[pring->ringno].rspGetInx);
3050
3051 if (pring->sli.sli3.rspidx == portRspPut)
3052 portRspPut = le32_to_cpu(pgp->rspPutInx);
3053 }
3054
3055 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3056 pring->stats.iocb_rsp_full++;
3057 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3058 writel(status, phba->CAregaddr);
3059 readl(phba->CAregaddr);
3060 }
3061 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3062 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3063 pring->stats.iocb_cmd_empty++;
3064
3065 /* Force update of the local copy of cmdGetInx */
3066 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3067 lpfc_sli_resume_iocb(phba, pring);
3068
3069 if ((pring->lpfc_sli_cmd_available))
3070 (pring->lpfc_sli_cmd_available) (phba, pring);
3071
3072 }
3073
3074 phba->fcp_ring_in_use = 0;
3075 spin_unlock_irqrestore(&phba->hbalock, iflag);
3076 return rc;
3077 }
3078
3079 /**
3080 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3081 * @phba: Pointer to HBA context object.
3082 * @pring: Pointer to driver SLI ring object.
3083 * @rspiocbp: Pointer to driver response IOCB object.
3084 *
3085 * This function is called from the worker thread when there is a slow-path
3086 * response IOCB to process. This function chains all the response iocbs until
3087 * seeing the iocb with the LE bit set. The function will call
3088 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3089 * completion of a command iocb. The function will call the
3090 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3091 * The function frees the resources or calls the completion handler if this
3092 * iocb is an abort completion. The function returns NULL when the response
3093 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3094 * this function shall chain the iocb on to the iocb_continueq and return the
3095 * response iocb passed in.
3096 **/
3097 static struct lpfc_iocbq *
3098 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3099 struct lpfc_iocbq *rspiocbp)
3100 {
3101 struct lpfc_iocbq *saveq;
3102 struct lpfc_iocbq *cmdiocbp;
3103 struct lpfc_iocbq *next_iocb;
3104 IOCB_t *irsp = NULL;
3105 uint32_t free_saveq;
3106 uint8_t iocb_cmd_type;
3107 lpfc_iocb_type type;
3108 unsigned long iflag;
3109 int rc;
3110
3111 spin_lock_irqsave(&phba->hbalock, iflag);
3112 /* First add the response iocb to the countinueq list */
3113 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3114 pring->iocb_continueq_cnt++;
3115
3116 /* Now, determine whether the list is completed for processing */
3117 irsp = &rspiocbp->iocb;
3118 if (irsp->ulpLe) {
3119 /*
3120 * By default, the driver expects to free all resources
3121 * associated with this iocb completion.
3122 */
3123 free_saveq = 1;
3124 saveq = list_get_first(&pring->iocb_continueq,
3125 struct lpfc_iocbq, list);
3126 irsp = &(saveq->iocb);
3127 list_del_init(&pring->iocb_continueq);
3128 pring->iocb_continueq_cnt = 0;
3129
3130 pring->stats.iocb_rsp++;
3131
3132 /*
3133 * If resource errors reported from HBA, reduce
3134 * queuedepths of the SCSI device.
3135 */
3136 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3137 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3138 spin_unlock_irqrestore(&phba->hbalock, iflag);
3139 phba->lpfc_rampdown_queue_depth(phba);
3140 spin_lock_irqsave(&phba->hbalock, iflag);
3141 }
3142
3143 if (irsp->ulpStatus) {
3144 /* Rsp ring <ringno> error: IOCB */
3145 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3146 "0328 Rsp Ring %d error: "
3147 "IOCB Data: "
3148 "x%x x%x x%x x%x "
3149 "x%x x%x x%x x%x "
3150 "x%x x%x x%x x%x "
3151 "x%x x%x x%x x%x\n",
3152 pring->ringno,
3153 irsp->un.ulpWord[0],
3154 irsp->un.ulpWord[1],
3155 irsp->un.ulpWord[2],
3156 irsp->un.ulpWord[3],
3157 irsp->un.ulpWord[4],
3158 irsp->un.ulpWord[5],
3159 *(((uint32_t *) irsp) + 6),
3160 *(((uint32_t *) irsp) + 7),
3161 *(((uint32_t *) irsp) + 8),
3162 *(((uint32_t *) irsp) + 9),
3163 *(((uint32_t *) irsp) + 10),
3164 *(((uint32_t *) irsp) + 11),
3165 *(((uint32_t *) irsp) + 12),
3166 *(((uint32_t *) irsp) + 13),
3167 *(((uint32_t *) irsp) + 14),
3168 *(((uint32_t *) irsp) + 15));
3169 }
3170
3171 /*
3172 * Fetch the IOCB command type and call the correct completion
3173 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3174 * get freed back to the lpfc_iocb_list by the discovery
3175 * kernel thread.
3176 */
3177 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3178 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3179 switch (type) {
3180 case LPFC_SOL_IOCB:
3181 spin_unlock_irqrestore(&phba->hbalock, iflag);
3182 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3183 spin_lock_irqsave(&phba->hbalock, iflag);
3184 break;
3185
3186 case LPFC_UNSOL_IOCB:
3187 spin_unlock_irqrestore(&phba->hbalock, iflag);
3188 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3189 spin_lock_irqsave(&phba->hbalock, iflag);
3190 if (!rc)
3191 free_saveq = 0;
3192 break;
3193
3194 case LPFC_ABORT_IOCB:
3195 cmdiocbp = NULL;
3196 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3197 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3198 saveq);
3199 if (cmdiocbp) {
3200 /* Call the specified completion routine */
3201 if (cmdiocbp->iocb_cmpl) {
3202 spin_unlock_irqrestore(&phba->hbalock,
3203 iflag);
3204 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3205 saveq);
3206 spin_lock_irqsave(&phba->hbalock,
3207 iflag);
3208 } else
3209 __lpfc_sli_release_iocbq(phba,
3210 cmdiocbp);
3211 }
3212 break;
3213
3214 case LPFC_UNKNOWN_IOCB:
3215 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3216 char adaptermsg[LPFC_MAX_ADPTMSG];
3217 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3218 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3219 MAX_MSG_DATA);
3220 dev_warn(&((phba->pcidev)->dev),
3221 "lpfc%d: %s\n",
3222 phba->brd_no, adaptermsg);
3223 } else {
3224 /* Unknown IOCB command */
3225 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3226 "0335 Unknown IOCB "
3227 "command Data: x%x "
3228 "x%x x%x x%x\n",
3229 irsp->ulpCommand,
3230 irsp->ulpStatus,
3231 irsp->ulpIoTag,
3232 irsp->ulpContext);
3233 }
3234 break;
3235 }
3236
3237 if (free_saveq) {
3238 list_for_each_entry_safe(rspiocbp, next_iocb,
3239 &saveq->list, list) {
3240 list_del(&rspiocbp->list);
3241 __lpfc_sli_release_iocbq(phba, rspiocbp);
3242 }
3243 __lpfc_sli_release_iocbq(phba, saveq);
3244 }
3245 rspiocbp = NULL;
3246 }
3247 spin_unlock_irqrestore(&phba->hbalock, iflag);
3248 return rspiocbp;
3249 }
3250
3251 /**
3252 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3253 * @phba: Pointer to HBA context object.
3254 * @pring: Pointer to driver SLI ring object.
3255 * @mask: Host attention register mask for this ring.
3256 *
3257 * This routine wraps the actual slow_ring event process routine from the
3258 * API jump table function pointer from the lpfc_hba struct.
3259 **/
3260 void
3261 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3262 struct lpfc_sli_ring *pring, uint32_t mask)
3263 {
3264 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3265 }
3266
3267 /**
3268 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3269 * @phba: Pointer to HBA context object.
3270 * @pring: Pointer to driver SLI ring object.
3271 * @mask: Host attention register mask for this ring.
3272 *
3273 * This function is called from the worker thread when there is a ring event
3274 * for non-fcp rings. The caller does not hold any lock. The function will
3275 * remove each response iocb in the response ring and calls the handle
3276 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3277 **/
3278 static void
3279 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3280 struct lpfc_sli_ring *pring, uint32_t mask)
3281 {
3282 struct lpfc_pgp *pgp;
3283 IOCB_t *entry;
3284 IOCB_t *irsp = NULL;
3285 struct lpfc_iocbq *rspiocbp = NULL;
3286 uint32_t portRspPut, portRspMax;
3287 unsigned long iflag;
3288 uint32_t status;
3289
3290 pgp = &phba->port_gp[pring->ringno];
3291 spin_lock_irqsave(&phba->hbalock, iflag);
3292 pring->stats.iocb_event++;
3293
3294 /*
3295 * The next available response entry should never exceed the maximum
3296 * entries. If it does, treat it as an adapter hardware error.
3297 */
3298 portRspMax = pring->sli.sli3.numRiocb;
3299 portRspPut = le32_to_cpu(pgp->rspPutInx);
3300 if (portRspPut >= portRspMax) {
3301 /*
3302 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3303 * rsp ring <portRspMax>
3304 */
3305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3306 "0303 Ring %d handler: portRspPut %d "
3307 "is bigger than rsp ring %d\n",
3308 pring->ringno, portRspPut, portRspMax);
3309
3310 phba->link_state = LPFC_HBA_ERROR;
3311 spin_unlock_irqrestore(&phba->hbalock, iflag);
3312
3313 phba->work_hs = HS_FFER3;
3314 lpfc_handle_eratt(phba);
3315
3316 return;
3317 }
3318
3319 rmb();
3320 while (pring->sli.sli3.rspidx != portRspPut) {
3321 /*
3322 * Build a completion list and call the appropriate handler.
3323 * The process is to get the next available response iocb, get
3324 * a free iocb from the list, copy the response data into the
3325 * free iocb, insert to the continuation list, and update the
3326 * next response index to slim. This process makes response
3327 * iocb's in the ring available to DMA as fast as possible but
3328 * pays a penalty for a copy operation. Since the iocb is
3329 * only 32 bytes, this penalty is considered small relative to
3330 * the PCI reads for register values and a slim write. When
3331 * the ulpLe field is set, the entire Command has been
3332 * received.
3333 */
3334 entry = lpfc_resp_iocb(phba, pring);
3335
3336 phba->last_completion_time = jiffies;
3337 rspiocbp = __lpfc_sli_get_iocbq(phba);
3338 if (rspiocbp == NULL) {
3339 printk(KERN_ERR "%s: out of buffers! Failing "
3340 "completion.\n", __func__);
3341 break;
3342 }
3343
3344 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3345 phba->iocb_rsp_size);
3346 irsp = &rspiocbp->iocb;
3347
3348 if (++pring->sli.sli3.rspidx >= portRspMax)
3349 pring->sli.sli3.rspidx = 0;
3350
3351 if (pring->ringno == LPFC_ELS_RING) {
3352 lpfc_debugfs_slow_ring_trc(phba,
3353 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3354 *(((uint32_t *) irsp) + 4),
3355 *(((uint32_t *) irsp) + 6),
3356 *(((uint32_t *) irsp) + 7));
3357 }
3358
3359 writel(pring->sli.sli3.rspidx,
3360 &phba->host_gp[pring->ringno].rspGetInx);
3361
3362 spin_unlock_irqrestore(&phba->hbalock, iflag);
3363 /* Handle the response IOCB */
3364 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3365 spin_lock_irqsave(&phba->hbalock, iflag);
3366
3367 /*
3368 * If the port response put pointer has not been updated, sync
3369 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3370 * response put pointer.
3371 */
3372 if (pring->sli.sli3.rspidx == portRspPut) {
3373 portRspPut = le32_to_cpu(pgp->rspPutInx);
3374 }
3375 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3376
3377 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3378 /* At least one response entry has been freed */
3379 pring->stats.iocb_rsp_full++;
3380 /* SET RxRE_RSP in Chip Att register */
3381 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3382 writel(status, phba->CAregaddr);
3383 readl(phba->CAregaddr); /* flush */
3384 }
3385 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3386 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3387 pring->stats.iocb_cmd_empty++;
3388
3389 /* Force update of the local copy of cmdGetInx */
3390 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3391 lpfc_sli_resume_iocb(phba, pring);
3392
3393 if ((pring->lpfc_sli_cmd_available))
3394 (pring->lpfc_sli_cmd_available) (phba, pring);
3395
3396 }
3397
3398 spin_unlock_irqrestore(&phba->hbalock, iflag);
3399 return;
3400 }
3401
3402 /**
3403 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3404 * @phba: Pointer to HBA context object.
3405 * @pring: Pointer to driver SLI ring object.
3406 * @mask: Host attention register mask for this ring.
3407 *
3408 * This function is called from the worker thread when there is a pending
3409 * ELS response iocb on the driver internal slow-path response iocb worker
3410 * queue. The caller does not hold any lock. The function will remove each
3411 * response iocb from the response worker queue and calls the handle
3412 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3413 **/
3414 static void
3415 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3416 struct lpfc_sli_ring *pring, uint32_t mask)
3417 {
3418 struct lpfc_iocbq *irspiocbq;
3419 struct hbq_dmabuf *dmabuf;
3420 struct lpfc_cq_event *cq_event;
3421 unsigned long iflag;
3422
3423 spin_lock_irqsave(&phba->hbalock, iflag);
3424 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3425 spin_unlock_irqrestore(&phba->hbalock, iflag);
3426 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3427 /* Get the response iocb from the head of work queue */
3428 spin_lock_irqsave(&phba->hbalock, iflag);
3429 list_remove_head(&phba->sli4_hba.sp_queue_event,
3430 cq_event, struct lpfc_cq_event, list);
3431 spin_unlock_irqrestore(&phba->hbalock, iflag);
3432
3433 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3434 case CQE_CODE_COMPL_WQE:
3435 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3436 cq_event);
3437 /* Translate ELS WCQE to response IOCBQ */
3438 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3439 irspiocbq);
3440 if (irspiocbq)
3441 lpfc_sli_sp_handle_rspiocb(phba, pring,
3442 irspiocbq);
3443 break;
3444 case CQE_CODE_RECEIVE:
3445 case CQE_CODE_RECEIVE_V1:
3446 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3447 cq_event);
3448 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3449 break;
3450 default:
3451 break;
3452 }
3453 }
3454 }
3455
3456 /**
3457 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3458 * @phba: Pointer to HBA context object.
3459 * @pring: Pointer to driver SLI ring object.
3460 *
3461 * This function aborts all iocbs in the given ring and frees all the iocb
3462 * objects in txq. This function issues an abort iocb for all the iocb commands
3463 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3464 * the return of this function. The caller is not required to hold any locks.
3465 **/
3466 void
3467 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3468 {
3469 LIST_HEAD(completions);
3470 struct lpfc_iocbq *iocb, *next_iocb;
3471
3472 if (pring->ringno == LPFC_ELS_RING) {
3473 lpfc_fabric_abort_hba(phba);
3474 }
3475
3476 /* Error everything on txq and txcmplq
3477 * First do the txq.
3478 */
3479 spin_lock_irq(&phba->hbalock);
3480 list_splice_init(&pring->txq, &completions);
3481 pring->txq_cnt = 0;
3482
3483 /* Next issue ABTS for everything on the txcmplq */
3484 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3485 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3486
3487 spin_unlock_irq(&phba->hbalock);
3488
3489 /* Cancel all the IOCBs from the completions list */
3490 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3491 IOERR_SLI_ABORTED);
3492 }
3493
3494 /**
3495 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3496 * @phba: Pointer to HBA context object.
3497 *
3498 * This function flushes all iocbs in the fcp ring and frees all the iocb
3499 * objects in txq and txcmplq. This function will not issue abort iocbs
3500 * for all the iocb commands in txcmplq, they will just be returned with
3501 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3502 * slot has been permanently disabled.
3503 **/
3504 void
3505 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3506 {
3507 LIST_HEAD(txq);
3508 LIST_HEAD(txcmplq);
3509 struct lpfc_sli *psli = &phba->sli;
3510 struct lpfc_sli_ring *pring;
3511
3512 /* Currently, only one fcp ring */
3513 pring = &psli->ring[psli->fcp_ring];
3514
3515 spin_lock_irq(&phba->hbalock);
3516 /* Retrieve everything on txq */
3517 list_splice_init(&pring->txq, &txq);
3518 pring->txq_cnt = 0;
3519
3520 /* Retrieve everything on the txcmplq */
3521 list_splice_init(&pring->txcmplq, &txcmplq);
3522 pring->txcmplq_cnt = 0;
3523
3524 /* Indicate the I/O queues are flushed */
3525 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3526 spin_unlock_irq(&phba->hbalock);
3527
3528 /* Flush the txq */
3529 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3530 IOERR_SLI_DOWN);
3531
3532 /* Flush the txcmpq */
3533 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3534 IOERR_SLI_DOWN);
3535 }
3536
3537 /**
3538 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3539 * @phba: Pointer to HBA context object.
3540 * @mask: Bit mask to be checked.
3541 *
3542 * This function reads the host status register and compares
3543 * with the provided bit mask to check if HBA completed
3544 * the restart. This function will wait in a loop for the
3545 * HBA to complete restart. If the HBA does not restart within
3546 * 15 iterations, the function will reset the HBA again. The
3547 * function returns 1 when HBA fail to restart otherwise returns
3548 * zero.
3549 **/
3550 static int
3551 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3552 {
3553 uint32_t status;
3554 int i = 0;
3555 int retval = 0;
3556
3557 /* Read the HBA Host Status Register */
3558 if (lpfc_readl(phba->HSregaddr, &status))
3559 return 1;
3560
3561 /*
3562 * Check status register every 100ms for 5 retries, then every
3563 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3564 * every 2.5 sec for 4.
3565 * Break our of the loop if errors occurred during init.
3566 */
3567 while (((status & mask) != mask) &&
3568 !(status & HS_FFERM) &&
3569 i++ < 20) {
3570
3571 if (i <= 5)
3572 msleep(10);
3573 else if (i <= 10)
3574 msleep(500);
3575 else
3576 msleep(2500);
3577
3578 if (i == 15) {
3579 /* Do post */
3580 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3581 lpfc_sli_brdrestart(phba);
3582 }
3583 /* Read the HBA Host Status Register */
3584 if (lpfc_readl(phba->HSregaddr, &status)) {
3585 retval = 1;
3586 break;
3587 }
3588 }
3589
3590 /* Check to see if any errors occurred during init */
3591 if ((status & HS_FFERM) || (i >= 20)) {
3592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3593 "2751 Adapter failed to restart, "
3594 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3595 status,
3596 readl(phba->MBslimaddr + 0xa8),
3597 readl(phba->MBslimaddr + 0xac));
3598 phba->link_state = LPFC_HBA_ERROR;
3599 retval = 1;
3600 }
3601
3602 return retval;
3603 }
3604
3605 /**
3606 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3607 * @phba: Pointer to HBA context object.
3608 * @mask: Bit mask to be checked.
3609 *
3610 * This function checks the host status register to check if HBA is
3611 * ready. This function will wait in a loop for the HBA to be ready
3612 * If the HBA is not ready , the function will will reset the HBA PCI
3613 * function again. The function returns 1 when HBA fail to be ready
3614 * otherwise returns zero.
3615 **/
3616 static int
3617 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3618 {
3619 uint32_t status;
3620 int retval = 0;
3621
3622 /* Read the HBA Host Status Register */
3623 status = lpfc_sli4_post_status_check(phba);
3624
3625 if (status) {
3626 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3627 lpfc_sli_brdrestart(phba);
3628 status = lpfc_sli4_post_status_check(phba);
3629 }
3630
3631 /* Check to see if any errors occurred during init */
3632 if (status) {
3633 phba->link_state = LPFC_HBA_ERROR;
3634 retval = 1;
3635 } else
3636 phba->sli4_hba.intr_enable = 0;
3637
3638 return retval;
3639 }
3640
3641 /**
3642 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3643 * @phba: Pointer to HBA context object.
3644 * @mask: Bit mask to be checked.
3645 *
3646 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3647 * from the API jump table function pointer from the lpfc_hba struct.
3648 **/
3649 int
3650 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3651 {
3652 return phba->lpfc_sli_brdready(phba, mask);
3653 }
3654
3655 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3656
3657 /**
3658 * lpfc_reset_barrier - Make HBA ready for HBA reset
3659 * @phba: Pointer to HBA context object.
3660 *
3661 * This function is called before resetting an HBA. This function is called
3662 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3663 **/
3664 void lpfc_reset_barrier(struct lpfc_hba *phba)
3665 {
3666 uint32_t __iomem *resp_buf;
3667 uint32_t __iomem *mbox_buf;
3668 volatile uint32_t mbox;
3669 uint32_t hc_copy, ha_copy, resp_data;
3670 int i;
3671 uint8_t hdrtype;
3672
3673 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3674 if (hdrtype != 0x80 ||
3675 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3676 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3677 return;
3678
3679 /*
3680 * Tell the other part of the chip to suspend temporarily all
3681 * its DMA activity.
3682 */
3683 resp_buf = phba->MBslimaddr;
3684
3685 /* Disable the error attention */
3686 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3687 return;
3688 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3689 readl(phba->HCregaddr); /* flush */
3690 phba->link_flag |= LS_IGNORE_ERATT;
3691
3692 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3693 return;
3694 if (ha_copy & HA_ERATT) {
3695 /* Clear Chip error bit */
3696 writel(HA_ERATT, phba->HAregaddr);
3697 phba->pport->stopped = 1;
3698 }
3699
3700 mbox = 0;
3701 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3702 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3703
3704 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3705 mbox_buf = phba->MBslimaddr;
3706 writel(mbox, mbox_buf);
3707
3708 for (i = 0; i < 50; i++) {
3709 if (lpfc_readl((resp_buf + 1), &resp_data))
3710 return;
3711 if (resp_data != ~(BARRIER_TEST_PATTERN))
3712 mdelay(1);
3713 else
3714 break;
3715 }
3716 resp_data = 0;
3717 if (lpfc_readl((resp_buf + 1), &resp_data))
3718 return;
3719 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3720 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3721 phba->pport->stopped)
3722 goto restore_hc;
3723 else
3724 goto clear_errat;
3725 }
3726
3727 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3728 resp_data = 0;
3729 for (i = 0; i < 500; i++) {
3730 if (lpfc_readl(resp_buf, &resp_data))
3731 return;
3732 if (resp_data != mbox)
3733 mdelay(1);
3734 else
3735 break;
3736 }
3737
3738 clear_errat:
3739
3740 while (++i < 500) {
3741 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3742 return;
3743 if (!(ha_copy & HA_ERATT))
3744 mdelay(1);
3745 else
3746 break;
3747 }
3748
3749 if (readl(phba->HAregaddr) & HA_ERATT) {
3750 writel(HA_ERATT, phba->HAregaddr);
3751 phba->pport->stopped = 1;
3752 }
3753
3754 restore_hc:
3755 phba->link_flag &= ~LS_IGNORE_ERATT;
3756 writel(hc_copy, phba->HCregaddr);
3757 readl(phba->HCregaddr); /* flush */
3758 }
3759
3760 /**
3761 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3762 * @phba: Pointer to HBA context object.
3763 *
3764 * This function issues a kill_board mailbox command and waits for
3765 * the error attention interrupt. This function is called for stopping
3766 * the firmware processing. The caller is not required to hold any
3767 * locks. This function calls lpfc_hba_down_post function to free
3768 * any pending commands after the kill. The function will return 1 when it
3769 * fails to kill the board else will return 0.
3770 **/
3771 int
3772 lpfc_sli_brdkill(struct lpfc_hba *phba)
3773 {
3774 struct lpfc_sli *psli;
3775 LPFC_MBOXQ_t *pmb;
3776 uint32_t status;
3777 uint32_t ha_copy;
3778 int retval;
3779 int i = 0;
3780
3781 psli = &phba->sli;
3782
3783 /* Kill HBA */
3784 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3785 "0329 Kill HBA Data: x%x x%x\n",
3786 phba->pport->port_state, psli->sli_flag);
3787
3788 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3789 if (!pmb)
3790 return 1;
3791
3792 /* Disable the error attention */
3793 spin_lock_irq(&phba->hbalock);
3794 if (lpfc_readl(phba->HCregaddr, &status)) {
3795 spin_unlock_irq(&phba->hbalock);
3796 mempool_free(pmb, phba->mbox_mem_pool);
3797 return 1;
3798 }
3799 status &= ~HC_ERINT_ENA;
3800 writel(status, phba->HCregaddr);
3801 readl(phba->HCregaddr); /* flush */
3802 phba->link_flag |= LS_IGNORE_ERATT;
3803 spin_unlock_irq(&phba->hbalock);
3804
3805 lpfc_kill_board(phba, pmb);
3806 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3807 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3808
3809 if (retval != MBX_SUCCESS) {
3810 if (retval != MBX_BUSY)
3811 mempool_free(pmb, phba->mbox_mem_pool);
3812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3813 "2752 KILL_BOARD command failed retval %d\n",
3814 retval);
3815 spin_lock_irq(&phba->hbalock);
3816 phba->link_flag &= ~LS_IGNORE_ERATT;
3817 spin_unlock_irq(&phba->hbalock);
3818 return 1;
3819 }
3820
3821 spin_lock_irq(&phba->hbalock);
3822 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3823 spin_unlock_irq(&phba->hbalock);
3824
3825 mempool_free(pmb, phba->mbox_mem_pool);
3826
3827 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3828 * attention every 100ms for 3 seconds. If we don't get ERATT after
3829 * 3 seconds we still set HBA_ERROR state because the status of the
3830 * board is now undefined.
3831 */
3832 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3833 return 1;
3834 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3835 mdelay(100);
3836 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3837 return 1;
3838 }
3839
3840 del_timer_sync(&psli->mbox_tmo);
3841 if (ha_copy & HA_ERATT) {
3842 writel(HA_ERATT, phba->HAregaddr);
3843 phba->pport->stopped = 1;
3844 }
3845 spin_lock_irq(&phba->hbalock);
3846 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3847 psli->mbox_active = NULL;
3848 phba->link_flag &= ~LS_IGNORE_ERATT;
3849 spin_unlock_irq(&phba->hbalock);
3850
3851 lpfc_hba_down_post(phba);
3852 phba->link_state = LPFC_HBA_ERROR;
3853
3854 return ha_copy & HA_ERATT ? 0 : 1;
3855 }
3856
3857 /**
3858 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3859 * @phba: Pointer to HBA context object.
3860 *
3861 * This function resets the HBA by writing HC_INITFF to the control
3862 * register. After the HBA resets, this function resets all the iocb ring
3863 * indices. This function disables PCI layer parity checking during
3864 * the reset.
3865 * This function returns 0 always.
3866 * The caller is not required to hold any locks.
3867 **/
3868 int
3869 lpfc_sli_brdreset(struct lpfc_hba *phba)
3870 {
3871 struct lpfc_sli *psli;
3872 struct lpfc_sli_ring *pring;
3873 uint16_t cfg_value;
3874 int i;
3875
3876 psli = &phba->sli;
3877
3878 /* Reset HBA */
3879 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3880 "0325 Reset HBA Data: x%x x%x\n",
3881 phba->pport->port_state, psli->sli_flag);
3882
3883 /* perform board reset */
3884 phba->fc_eventTag = 0;
3885 phba->link_events = 0;
3886 phba->pport->fc_myDID = 0;
3887 phba->pport->fc_prevDID = 0;
3888
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3892 (cfg_value &
3893 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3894
3895 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3896
3897 /* Now toggle INITFF bit in the Host Control Register */
3898 writel(HC_INITFF, phba->HCregaddr);
3899 mdelay(1);
3900 readl(phba->HCregaddr); /* flush */
3901 writel(0, phba->HCregaddr);
3902 readl(phba->HCregaddr); /* flush */
3903
3904 /* Restore PCI cmd register */
3905 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3906
3907 /* Initialize relevant SLI info */
3908 for (i = 0; i < psli->num_rings; i++) {
3909 pring = &psli->ring[i];
3910 pring->flag = 0;
3911 pring->sli.sli3.rspidx = 0;
3912 pring->sli.sli3.next_cmdidx = 0;
3913 pring->sli.sli3.local_getidx = 0;
3914 pring->sli.sli3.cmdidx = 0;
3915 pring->missbufcnt = 0;
3916 }
3917
3918 phba->link_state = LPFC_WARM_START;
3919 return 0;
3920 }
3921
3922 /**
3923 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3924 * @phba: Pointer to HBA context object.
3925 *
3926 * This function resets a SLI4 HBA. This function disables PCI layer parity
3927 * checking during resets the device. The caller is not required to hold
3928 * any locks.
3929 *
3930 * This function returns 0 always.
3931 **/
3932 int
3933 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3934 {
3935 struct lpfc_sli *psli = &phba->sli;
3936 uint16_t cfg_value;
3937 int rc;
3938
3939 /* Reset HBA */
3940 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3941 "0295 Reset HBA Data: x%x x%x\n",
3942 phba->pport->port_state, psli->sli_flag);
3943
3944 /* perform board reset */
3945 phba->fc_eventTag = 0;
3946 phba->link_events = 0;
3947 phba->pport->fc_myDID = 0;
3948 phba->pport->fc_prevDID = 0;
3949
3950 spin_lock_irq(&phba->hbalock);
3951 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3952 phba->fcf.fcf_flag = 0;
3953 spin_unlock_irq(&phba->hbalock);
3954
3955 /* Now physically reset the device */
3956 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3957 "0389 Performing PCI function reset!\n");
3958
3959 /* Turn off parity checking and serr during the physical reset */
3960 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3961 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3962 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3963
3964 /* Perform FCoE PCI function reset */
3965 lpfc_sli4_queue_destroy(phba);
3966 rc = lpfc_pci_function_reset(phba);
3967
3968 /* Restore PCI cmd register */
3969 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3970
3971 return rc;
3972 }
3973
3974 /**
3975 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3976 * @phba: Pointer to HBA context object.
3977 *
3978 * This function is called in the SLI initialization code path to
3979 * restart the HBA. The caller is not required to hold any lock.
3980 * This function writes MBX_RESTART mailbox command to the SLIM and
3981 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3982 * function to free any pending commands. The function enables
3983 * POST only during the first initialization. The function returns zero.
3984 * The function does not guarantee completion of MBX_RESTART mailbox
3985 * command before the return of this function.
3986 **/
3987 static int
3988 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3989 {
3990 MAILBOX_t *mb;
3991 struct lpfc_sli *psli;
3992 volatile uint32_t word0;
3993 void __iomem *to_slim;
3994 uint32_t hba_aer_enabled;
3995
3996 spin_lock_irq(&phba->hbalock);
3997
3998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
4001 psli = &phba->sli;
4002
4003 /* Restart HBA */
4004 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4005 "0337 Restart HBA Data: x%x x%x\n",
4006 phba->pport->port_state, psli->sli_flag);
4007
4008 word0 = 0;
4009 mb = (MAILBOX_t *) &word0;
4010 mb->mbxCommand = MBX_RESTART;
4011 mb->mbxHc = 1;
4012
4013 lpfc_reset_barrier(phba);
4014
4015 to_slim = phba->MBslimaddr;
4016 writel(*(uint32_t *) mb, to_slim);
4017 readl(to_slim); /* flush */
4018
4019 /* Only skip post after fc_ffinit is completed */
4020 if (phba->pport->port_state)
4021 word0 = 1; /* This is really setting up word1 */
4022 else
4023 word0 = 0; /* This is really setting up word1 */
4024 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4025 writel(*(uint32_t *) mb, to_slim);
4026 readl(to_slim); /* flush */
4027
4028 lpfc_sli_brdreset(phba);
4029 phba->pport->stopped = 0;
4030 phba->link_state = LPFC_INIT_START;
4031 phba->hba_flag = 0;
4032 spin_unlock_irq(&phba->hbalock);
4033
4034 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4035 psli->stats_start = get_seconds();
4036
4037 /* Give the INITFF and Post time to settle. */
4038 mdelay(100);
4039
4040 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4041 if (hba_aer_enabled)
4042 pci_disable_pcie_error_reporting(phba->pcidev);
4043
4044 lpfc_hba_down_post(phba);
4045
4046 return 0;
4047 }
4048
4049 /**
4050 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4051 * @phba: Pointer to HBA context object.
4052 *
4053 * This function is called in the SLI initialization code path to restart
4054 * a SLI4 HBA. The caller is not required to hold any lock.
4055 * At the end of the function, it calls lpfc_hba_down_post function to
4056 * free any pending commands.
4057 **/
4058 static int
4059 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4060 {
4061 struct lpfc_sli *psli = &phba->sli;
4062 uint32_t hba_aer_enabled;
4063 int rc;
4064
4065 /* Restart HBA */
4066 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4067 "0296 Restart HBA Data: x%x x%x\n",
4068 phba->pport->port_state, psli->sli_flag);
4069
4070 /* Take PCIe device Advanced Error Reporting (AER) state */
4071 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4072
4073 rc = lpfc_sli4_brdreset(phba);
4074
4075 spin_lock_irq(&phba->hbalock);
4076 phba->pport->stopped = 0;
4077 phba->link_state = LPFC_INIT_START;
4078 phba->hba_flag = 0;
4079 spin_unlock_irq(&phba->hbalock);
4080
4081 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4082 psli->stats_start = get_seconds();
4083
4084 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4085 if (hba_aer_enabled)
4086 pci_disable_pcie_error_reporting(phba->pcidev);
4087
4088 lpfc_hba_down_post(phba);
4089
4090 return rc;
4091 }
4092
4093 /**
4094 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4095 * @phba: Pointer to HBA context object.
4096 *
4097 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4098 * API jump table function pointer from the lpfc_hba struct.
4099 **/
4100 int
4101 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4102 {
4103 return phba->lpfc_sli_brdrestart(phba);
4104 }
4105
4106 /**
4107 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4108 * @phba: Pointer to HBA context object.
4109 *
4110 * This function is called after a HBA restart to wait for successful
4111 * restart of the HBA. Successful restart of the HBA is indicated by
4112 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4113 * iteration, the function will restart the HBA again. The function returns
4114 * zero if HBA successfully restarted else returns negative error code.
4115 **/
4116 static int
4117 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4118 {
4119 uint32_t status, i = 0;
4120
4121 /* Read the HBA Host Status Register */
4122 if (lpfc_readl(phba->HSregaddr, &status))
4123 return -EIO;
4124
4125 /* Check status register to see what current state is */
4126 i = 0;
4127 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4128
4129 /* Check every 10ms for 10 retries, then every 100ms for 90
4130 * retries, then every 1 sec for 50 retires for a total of
4131 * ~60 seconds before reset the board again and check every
4132 * 1 sec for 50 retries. The up to 60 seconds before the
4133 * board ready is required by the Falcon FIPS zeroization
4134 * complete, and any reset the board in between shall cause
4135 * restart of zeroization, further delay the board ready.
4136 */
4137 if (i++ >= 200) {
4138 /* Adapter failed to init, timeout, status reg
4139 <status> */
4140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4141 "0436 Adapter failed to init, "
4142 "timeout, status reg x%x, "
4143 "FW Data: A8 x%x AC x%x\n", status,
4144 readl(phba->MBslimaddr + 0xa8),
4145 readl(phba->MBslimaddr + 0xac));
4146 phba->link_state = LPFC_HBA_ERROR;
4147 return -ETIMEDOUT;
4148 }
4149
4150 /* Check to see if any errors occurred during init */
4151 if (status & HS_FFERM) {
4152 /* ERROR: During chipset initialization */
4153 /* Adapter failed to init, chipset, status reg
4154 <status> */
4155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4156 "0437 Adapter failed to init, "
4157 "chipset, status reg x%x, "
4158 "FW Data: A8 x%x AC x%x\n", status,
4159 readl(phba->MBslimaddr + 0xa8),
4160 readl(phba->MBslimaddr + 0xac));
4161 phba->link_state = LPFC_HBA_ERROR;
4162 return -EIO;
4163 }
4164
4165 if (i <= 10)
4166 msleep(10);
4167 else if (i <= 100)
4168 msleep(100);
4169 else
4170 msleep(1000);
4171
4172 if (i == 150) {
4173 /* Do post */
4174 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4175 lpfc_sli_brdrestart(phba);
4176 }
4177 /* Read the HBA Host Status Register */
4178 if (lpfc_readl(phba->HSregaddr, &status))
4179 return -EIO;
4180 }
4181
4182 /* Check to see if any errors occurred during init */
4183 if (status & HS_FFERM) {
4184 /* ERROR: During chipset initialization */
4185 /* Adapter failed to init, chipset, status reg <status> */
4186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4187 "0438 Adapter failed to init, chipset, "
4188 "status reg x%x, "
4189 "FW Data: A8 x%x AC x%x\n", status,
4190 readl(phba->MBslimaddr + 0xa8),
4191 readl(phba->MBslimaddr + 0xac));
4192 phba->link_state = LPFC_HBA_ERROR;
4193 return -EIO;
4194 }
4195
4196 /* Clear all interrupt enable conditions */
4197 writel(0, phba->HCregaddr);
4198 readl(phba->HCregaddr); /* flush */
4199
4200 /* setup host attn register */
4201 writel(0xffffffff, phba->HAregaddr);
4202 readl(phba->HAregaddr); /* flush */
4203 return 0;
4204 }
4205
4206 /**
4207 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4208 *
4209 * This function calculates and returns the number of HBQs required to be
4210 * configured.
4211 **/
4212 int
4213 lpfc_sli_hbq_count(void)
4214 {
4215 return ARRAY_SIZE(lpfc_hbq_defs);
4216 }
4217
4218 /**
4219 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4220 *
4221 * This function adds the number of hbq entries in every HBQ to get
4222 * the total number of hbq entries required for the HBA and returns
4223 * the total count.
4224 **/
4225 static int
4226 lpfc_sli_hbq_entry_count(void)
4227 {
4228 int hbq_count = lpfc_sli_hbq_count();
4229 int count = 0;
4230 int i;
4231
4232 for (i = 0; i < hbq_count; ++i)
4233 count += lpfc_hbq_defs[i]->entry_count;
4234 return count;
4235 }
4236
4237 /**
4238 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4239 *
4240 * This function calculates amount of memory required for all hbq entries
4241 * to be configured and returns the total memory required.
4242 **/
4243 int
4244 lpfc_sli_hbq_size(void)
4245 {
4246 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4247 }
4248
4249 /**
4250 * lpfc_sli_hbq_setup - configure and initialize HBQs
4251 * @phba: Pointer to HBA context object.
4252 *
4253 * This function is called during the SLI initialization to configure
4254 * all the HBQs and post buffers to the HBQ. The caller is not
4255 * required to hold any locks. This function will return zero if successful
4256 * else it will return negative error code.
4257 **/
4258 static int
4259 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4260 {
4261 int hbq_count = lpfc_sli_hbq_count();
4262 LPFC_MBOXQ_t *pmb;
4263 MAILBOX_t *pmbox;
4264 uint32_t hbqno;
4265 uint32_t hbq_entry_index;
4266
4267 /* Get a Mailbox buffer to setup mailbox
4268 * commands for HBA initialization
4269 */
4270 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4271
4272 if (!pmb)
4273 return -ENOMEM;
4274
4275 pmbox = &pmb->u.mb;
4276
4277 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4278 phba->link_state = LPFC_INIT_MBX_CMDS;
4279 phba->hbq_in_use = 1;
4280
4281 hbq_entry_index = 0;
4282 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4283 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4284 phba->hbqs[hbqno].hbqPutIdx = 0;
4285 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4286 phba->hbqs[hbqno].entry_count =
4287 lpfc_hbq_defs[hbqno]->entry_count;
4288 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4289 hbq_entry_index, pmb);
4290 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4291
4292 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4293 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4294 mbxStatus <status>, ring <num> */
4295
4296 lpfc_printf_log(phba, KERN_ERR,
4297 LOG_SLI | LOG_VPORT,
4298 "1805 Adapter failed to init. "
4299 "Data: x%x x%x x%x\n",
4300 pmbox->mbxCommand,
4301 pmbox->mbxStatus, hbqno);
4302
4303 phba->link_state = LPFC_HBA_ERROR;
4304 mempool_free(pmb, phba->mbox_mem_pool);
4305 return -ENXIO;
4306 }
4307 }
4308 phba->hbq_count = hbq_count;
4309
4310 mempool_free(pmb, phba->mbox_mem_pool);
4311
4312 /* Initially populate or replenish the HBQs */
4313 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4314 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4315 return 0;
4316 }
4317
4318 /**
4319 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4320 * @phba: Pointer to HBA context object.
4321 *
4322 * This function is called during the SLI initialization to configure
4323 * all the HBQs and post buffers to the HBQ. The caller is not
4324 * required to hold any locks. This function will return zero if successful
4325 * else it will return negative error code.
4326 **/
4327 static int
4328 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4329 {
4330 phba->hbq_in_use = 1;
4331 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4332 phba->hbq_count = 1;
4333 /* Initially populate or replenish the HBQs */
4334 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4335 return 0;
4336 }
4337
4338 /**
4339 * lpfc_sli_config_port - Issue config port mailbox command
4340 * @phba: Pointer to HBA context object.
4341 * @sli_mode: sli mode - 2/3
4342 *
4343 * This function is called by the sli intialization code path
4344 * to issue config_port mailbox command. This function restarts the
4345 * HBA firmware and issues a config_port mailbox command to configure
4346 * the SLI interface in the sli mode specified by sli_mode
4347 * variable. The caller is not required to hold any locks.
4348 * The function returns 0 if successful, else returns negative error
4349 * code.
4350 **/
4351 int
4352 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4353 {
4354 LPFC_MBOXQ_t *pmb;
4355 uint32_t resetcount = 0, rc = 0, done = 0;
4356
4357 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4358 if (!pmb) {
4359 phba->link_state = LPFC_HBA_ERROR;
4360 return -ENOMEM;
4361 }
4362
4363 phba->sli_rev = sli_mode;
4364 while (resetcount < 2 && !done) {
4365 spin_lock_irq(&phba->hbalock);
4366 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4367 spin_unlock_irq(&phba->hbalock);
4368 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4369 lpfc_sli_brdrestart(phba);
4370 rc = lpfc_sli_chipset_init(phba);
4371 if (rc)
4372 break;
4373
4374 spin_lock_irq(&phba->hbalock);
4375 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4376 spin_unlock_irq(&phba->hbalock);
4377 resetcount++;
4378
4379 /* Call pre CONFIG_PORT mailbox command initialization. A
4380 * value of 0 means the call was successful. Any other
4381 * nonzero value is a failure, but if ERESTART is returned,
4382 * the driver may reset the HBA and try again.
4383 */
4384 rc = lpfc_config_port_prep(phba);
4385 if (rc == -ERESTART) {
4386 phba->link_state = LPFC_LINK_UNKNOWN;
4387 continue;
4388 } else if (rc)
4389 break;
4390
4391 phba->link_state = LPFC_INIT_MBX_CMDS;
4392 lpfc_config_port(phba, pmb);
4393 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4394 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4395 LPFC_SLI3_HBQ_ENABLED |
4396 LPFC_SLI3_CRP_ENABLED |
4397 LPFC_SLI3_BG_ENABLED |
4398 LPFC_SLI3_DSS_ENABLED);
4399 if (rc != MBX_SUCCESS) {
4400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4401 "0442 Adapter failed to init, mbxCmd x%x "
4402 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4403 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4404 spin_lock_irq(&phba->hbalock);
4405 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4406 spin_unlock_irq(&phba->hbalock);
4407 rc = -ENXIO;
4408 } else {
4409 /* Allow asynchronous mailbox command to go through */
4410 spin_lock_irq(&phba->hbalock);
4411 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4412 spin_unlock_irq(&phba->hbalock);
4413 done = 1;
4414
4415 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4416 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4417 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4418 "3110 Port did not grant ASABT\n");
4419 }
4420 }
4421 if (!done) {
4422 rc = -EINVAL;
4423 goto do_prep_failed;
4424 }
4425 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4426 if (!pmb->u.mb.un.varCfgPort.cMA) {
4427 rc = -ENXIO;
4428 goto do_prep_failed;
4429 }
4430 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4431 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4432 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4433 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4434 phba->max_vpi : phba->max_vports;
4435
4436 } else
4437 phba->max_vpi = 0;
4438 phba->fips_level = 0;
4439 phba->fips_spec_rev = 0;
4440 if (pmb->u.mb.un.varCfgPort.gdss) {
4441 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4442 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4443 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4445 "2850 Security Crypto Active. FIPS x%d "
4446 "(Spec Rev: x%d)",
4447 phba->fips_level, phba->fips_spec_rev);
4448 }
4449 if (pmb->u.mb.un.varCfgPort.sec_err) {
4450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4451 "2856 Config Port Security Crypto "
4452 "Error: x%x ",
4453 pmb->u.mb.un.varCfgPort.sec_err);
4454 }
4455 if (pmb->u.mb.un.varCfgPort.gerbm)
4456 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4457 if (pmb->u.mb.un.varCfgPort.gcrp)
4458 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4459
4460 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4461 phba->port_gp = phba->mbox->us.s3_pgp.port;
4462
4463 if (phba->cfg_enable_bg) {
4464 if (pmb->u.mb.un.varCfgPort.gbg)
4465 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4466 else
4467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4468 "0443 Adapter did not grant "
4469 "BlockGuard\n");
4470 }
4471 } else {
4472 phba->hbq_get = NULL;
4473 phba->port_gp = phba->mbox->us.s2.port;
4474 phba->max_vpi = 0;
4475 }
4476 do_prep_failed:
4477 mempool_free(pmb, phba->mbox_mem_pool);
4478 return rc;
4479 }
4480
4481
4482 /**
4483 * lpfc_sli_hba_setup - SLI intialization function
4484 * @phba: Pointer to HBA context object.
4485 *
4486 * This function is the main SLI intialization function. This function
4487 * is called by the HBA intialization code, HBA reset code and HBA
4488 * error attention handler code. Caller is not required to hold any
4489 * locks. This function issues config_port mailbox command to configure
4490 * the SLI, setup iocb rings and HBQ rings. In the end the function
4491 * calls the config_port_post function to issue init_link mailbox
4492 * command and to start the discovery. The function will return zero
4493 * if successful, else it will return negative error code.
4494 **/
4495 int
4496 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4497 {
4498 uint32_t rc;
4499 int mode = 3, i;
4500 int longs;
4501
4502 switch (lpfc_sli_mode) {
4503 case 2:
4504 if (phba->cfg_enable_npiv) {
4505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4506 "1824 NPIV enabled: Override lpfc_sli_mode "
4507 "parameter (%d) to auto (0).\n",
4508 lpfc_sli_mode);
4509 break;
4510 }
4511 mode = 2;
4512 break;
4513 case 0:
4514 case 3:
4515 break;
4516 default:
4517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4518 "1819 Unrecognized lpfc_sli_mode "
4519 "parameter: %d.\n", lpfc_sli_mode);
4520
4521 break;
4522 }
4523
4524 rc = lpfc_sli_config_port(phba, mode);
4525
4526 if (rc && lpfc_sli_mode == 3)
4527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4528 "1820 Unable to select SLI-3. "
4529 "Not supported by adapter.\n");
4530 if (rc && mode != 2)
4531 rc = lpfc_sli_config_port(phba, 2);
4532 if (rc)
4533 goto lpfc_sli_hba_setup_error;
4534
4535 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4536 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4537 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4538 if (!rc) {
4539 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4540 "2709 This device supports "
4541 "Advanced Error Reporting (AER)\n");
4542 spin_lock_irq(&phba->hbalock);
4543 phba->hba_flag |= HBA_AER_ENABLED;
4544 spin_unlock_irq(&phba->hbalock);
4545 } else {
4546 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4547 "2708 This device does not support "
4548 "Advanced Error Reporting (AER)\n");
4549 phba->cfg_aer_support = 0;
4550 }
4551 }
4552
4553 if (phba->sli_rev == 3) {
4554 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4555 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4556 } else {
4557 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4558 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4559 phba->sli3_options = 0;
4560 }
4561
4562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4563 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4564 phba->sli_rev, phba->max_vpi);
4565 rc = lpfc_sli_ring_map(phba);
4566
4567 if (rc)
4568 goto lpfc_sli_hba_setup_error;
4569
4570 /* Initialize VPIs. */
4571 if (phba->sli_rev == LPFC_SLI_REV3) {
4572 /*
4573 * The VPI bitmask and physical ID array are allocated
4574 * and initialized once only - at driver load. A port
4575 * reset doesn't need to reinitialize this memory.
4576 */
4577 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4578 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4579 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4580 GFP_KERNEL);
4581 if (!phba->vpi_bmask) {
4582 rc = -ENOMEM;
4583 goto lpfc_sli_hba_setup_error;
4584 }
4585
4586 phba->vpi_ids = kzalloc(
4587 (phba->max_vpi+1) * sizeof(uint16_t),
4588 GFP_KERNEL);
4589 if (!phba->vpi_ids) {
4590 kfree(phba->vpi_bmask);
4591 rc = -ENOMEM;
4592 goto lpfc_sli_hba_setup_error;
4593 }
4594 for (i = 0; i < phba->max_vpi; i++)
4595 phba->vpi_ids[i] = i;
4596 }
4597 }
4598
4599 /* Init HBQs */
4600 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4601 rc = lpfc_sli_hbq_setup(phba);
4602 if (rc)
4603 goto lpfc_sli_hba_setup_error;
4604 }
4605 spin_lock_irq(&phba->hbalock);
4606 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4607 spin_unlock_irq(&phba->hbalock);
4608
4609 rc = lpfc_config_port_post(phba);
4610 if (rc)
4611 goto lpfc_sli_hba_setup_error;
4612
4613 return rc;
4614
4615 lpfc_sli_hba_setup_error:
4616 phba->link_state = LPFC_HBA_ERROR;
4617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4618 "0445 Firmware initialization failed\n");
4619 return rc;
4620 }
4621
4622 /**
4623 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4624 * @phba: Pointer to HBA context object.
4625 * @mboxq: mailbox pointer.
4626 * This function issue a dump mailbox command to read config region
4627 * 23 and parse the records in the region and populate driver
4628 * data structure.
4629 **/
4630 static int
4631 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4632 {
4633 LPFC_MBOXQ_t *mboxq;
4634 struct lpfc_dmabuf *mp;
4635 struct lpfc_mqe *mqe;
4636 uint32_t data_length;
4637 int rc;
4638
4639 /* Program the default value of vlan_id and fc_map */
4640 phba->valid_vlan = 0;
4641 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4642 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4643 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4644
4645 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4646 if (!mboxq)
4647 return -ENOMEM;
4648
4649 mqe = &mboxq->u.mqe;
4650 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4651 rc = -ENOMEM;
4652 goto out_free_mboxq;
4653 }
4654
4655 mp = (struct lpfc_dmabuf *) mboxq->context1;
4656 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4657
4658 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4659 "(%d):2571 Mailbox cmd x%x Status x%x "
4660 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4661 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4662 "CQ: x%x x%x x%x x%x\n",
4663 mboxq->vport ? mboxq->vport->vpi : 0,
4664 bf_get(lpfc_mqe_command, mqe),
4665 bf_get(lpfc_mqe_status, mqe),
4666 mqe->un.mb_words[0], mqe->un.mb_words[1],
4667 mqe->un.mb_words[2], mqe->un.mb_words[3],
4668 mqe->un.mb_words[4], mqe->un.mb_words[5],
4669 mqe->un.mb_words[6], mqe->un.mb_words[7],
4670 mqe->un.mb_words[8], mqe->un.mb_words[9],
4671 mqe->un.mb_words[10], mqe->un.mb_words[11],
4672 mqe->un.mb_words[12], mqe->un.mb_words[13],
4673 mqe->un.mb_words[14], mqe->un.mb_words[15],
4674 mqe->un.mb_words[16], mqe->un.mb_words[50],
4675 mboxq->mcqe.word0,
4676 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4677 mboxq->mcqe.trailer);
4678
4679 if (rc) {
4680 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4681 kfree(mp);
4682 rc = -EIO;
4683 goto out_free_mboxq;
4684 }
4685 data_length = mqe->un.mb_words[5];
4686 if (data_length > DMP_RGN23_SIZE) {
4687 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4688 kfree(mp);
4689 rc = -EIO;
4690 goto out_free_mboxq;
4691 }
4692
4693 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4694 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4695 kfree(mp);
4696 rc = 0;
4697
4698 out_free_mboxq:
4699 mempool_free(mboxq, phba->mbox_mem_pool);
4700 return rc;
4701 }
4702
4703 /**
4704 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4705 * @phba: pointer to lpfc hba data structure.
4706 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4707 * @vpd: pointer to the memory to hold resulting port vpd data.
4708 * @vpd_size: On input, the number of bytes allocated to @vpd.
4709 * On output, the number of data bytes in @vpd.
4710 *
4711 * This routine executes a READ_REV SLI4 mailbox command. In
4712 * addition, this routine gets the port vpd data.
4713 *
4714 * Return codes
4715 * 0 - successful
4716 * -ENOMEM - could not allocated memory.
4717 **/
4718 static int
4719 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4720 uint8_t *vpd, uint32_t *vpd_size)
4721 {
4722 int rc = 0;
4723 uint32_t dma_size;
4724 struct lpfc_dmabuf *dmabuf;
4725 struct lpfc_mqe *mqe;
4726
4727 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4728 if (!dmabuf)
4729 return -ENOMEM;
4730
4731 /*
4732 * Get a DMA buffer for the vpd data resulting from the READ_REV
4733 * mailbox command.
4734 */
4735 dma_size = *vpd_size;
4736 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4737 dma_size,
4738 &dmabuf->phys,
4739 GFP_KERNEL);
4740 if (!dmabuf->virt) {
4741 kfree(dmabuf);
4742 return -ENOMEM;
4743 }
4744 memset(dmabuf->virt, 0, dma_size);
4745
4746 /*
4747 * The SLI4 implementation of READ_REV conflicts at word1,
4748 * bits 31:16 and SLI4 adds vpd functionality not present
4749 * in SLI3. This code corrects the conflicts.
4750 */
4751 lpfc_read_rev(phba, mboxq);
4752 mqe = &mboxq->u.mqe;
4753 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4754 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4755 mqe->un.read_rev.word1 &= 0x0000FFFF;
4756 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4757 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4758
4759 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4760 if (rc) {
4761 dma_free_coherent(&phba->pcidev->dev, dma_size,
4762 dmabuf->virt, dmabuf->phys);
4763 kfree(dmabuf);
4764 return -EIO;
4765 }
4766
4767 /*
4768 * The available vpd length cannot be bigger than the
4769 * DMA buffer passed to the port. Catch the less than
4770 * case and update the caller's size.
4771 */
4772 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4773 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4774
4775 memcpy(vpd, dmabuf->virt, *vpd_size);
4776
4777 dma_free_coherent(&phba->pcidev->dev, dma_size,
4778 dmabuf->virt, dmabuf->phys);
4779 kfree(dmabuf);
4780 return 0;
4781 }
4782
4783 /**
4784 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4785 * @phba: pointer to lpfc hba data structure.
4786 *
4787 * This routine retrieves SLI4 device physical port name this PCI function
4788 * is attached to.
4789 *
4790 * Return codes
4791 * 0 - sucessful
4792 * otherwise - failed to retrieve physical port name
4793 **/
4794 static int
4795 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4796 {
4797 LPFC_MBOXQ_t *mboxq;
4798 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4799 struct lpfc_controller_attribute *cntl_attr;
4800 struct lpfc_mbx_get_port_name *get_port_name;
4801 void *virtaddr = NULL;
4802 uint32_t alloclen, reqlen;
4803 uint32_t shdr_status, shdr_add_status;
4804 union lpfc_sli4_cfg_shdr *shdr;
4805 char cport_name = 0;
4806 int rc;
4807
4808 /* We assume nothing at this point */
4809 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4810 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4811
4812 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4813 if (!mboxq)
4814 return -ENOMEM;
4815 /* obtain link type and link number via READ_CONFIG */
4816 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4817 lpfc_sli4_read_config(phba);
4818 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4819 goto retrieve_ppname;
4820
4821 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4822 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4823 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4824 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4825 LPFC_SLI4_MBX_NEMBED);
4826 if (alloclen < reqlen) {
4827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4828 "3084 Allocated DMA memory size (%d) is "
4829 "less than the requested DMA memory size "
4830 "(%d)\n", alloclen, reqlen);
4831 rc = -ENOMEM;
4832 goto out_free_mboxq;
4833 }
4834 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4835 virtaddr = mboxq->sge_array->addr[0];
4836 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4837 shdr = &mbx_cntl_attr->cfg_shdr;
4838 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4839 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4840 if (shdr_status || shdr_add_status || rc) {
4841 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4842 "3085 Mailbox x%x (x%x/x%x) failed, "
4843 "rc:x%x, status:x%x, add_status:x%x\n",
4844 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4845 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4846 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4847 rc, shdr_status, shdr_add_status);
4848 rc = -ENXIO;
4849 goto out_free_mboxq;
4850 }
4851 cntl_attr = &mbx_cntl_attr->cntl_attr;
4852 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4853 phba->sli4_hba.lnk_info.lnk_tp =
4854 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4855 phba->sli4_hba.lnk_info.lnk_no =
4856 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4858 "3086 lnk_type:%d, lnk_numb:%d\n",
4859 phba->sli4_hba.lnk_info.lnk_tp,
4860 phba->sli4_hba.lnk_info.lnk_no);
4861
4862 retrieve_ppname:
4863 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4864 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4865 sizeof(struct lpfc_mbx_get_port_name) -
4866 sizeof(struct lpfc_sli4_cfg_mhdr),
4867 LPFC_SLI4_MBX_EMBED);
4868 get_port_name = &mboxq->u.mqe.un.get_port_name;
4869 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4870 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4871 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4872 phba->sli4_hba.lnk_info.lnk_tp);
4873 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4874 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4875 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4876 if (shdr_status || shdr_add_status || rc) {
4877 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4878 "3087 Mailbox x%x (x%x/x%x) failed: "
4879 "rc:x%x, status:x%x, add_status:x%x\n",
4880 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4881 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4882 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4883 rc, shdr_status, shdr_add_status);
4884 rc = -ENXIO;
4885 goto out_free_mboxq;
4886 }
4887 switch (phba->sli4_hba.lnk_info.lnk_no) {
4888 case LPFC_LINK_NUMBER_0:
4889 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4890 &get_port_name->u.response);
4891 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4892 break;
4893 case LPFC_LINK_NUMBER_1:
4894 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4895 &get_port_name->u.response);
4896 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4897 break;
4898 case LPFC_LINK_NUMBER_2:
4899 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4900 &get_port_name->u.response);
4901 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4902 break;
4903 case LPFC_LINK_NUMBER_3:
4904 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4905 &get_port_name->u.response);
4906 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4907 break;
4908 default:
4909 break;
4910 }
4911
4912 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4913 phba->Port[0] = cport_name;
4914 phba->Port[1] = '\0';
4915 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4916 "3091 SLI get port name: %s\n", phba->Port);
4917 }
4918
4919 out_free_mboxq:
4920 if (rc != MBX_TIMEOUT) {
4921 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4922 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4923 else
4924 mempool_free(mboxq, phba->mbox_mem_pool);
4925 }
4926 return rc;
4927 }
4928
4929 /**
4930 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4931 * @phba: pointer to lpfc hba data structure.
4932 *
4933 * This routine is called to explicitly arm the SLI4 device's completion and
4934 * event queues
4935 **/
4936 static void
4937 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4938 {
4939 uint8_t fcp_eqidx;
4940
4941 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4942 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4943 fcp_eqidx = 0;
4944 if (phba->sli4_hba.fcp_cq) {
4945 do {
4946 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4947 LPFC_QUEUE_REARM);
4948 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
4949 }
4950 if (phba->sli4_hba.hba_eq) {
4951 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
4952 fcp_eqidx++)
4953 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
4954 LPFC_QUEUE_REARM);
4955 }
4956 }
4957
4958 /**
4959 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4960 * @phba: Pointer to HBA context object.
4961 * @type: The resource extent type.
4962 * @extnt_count: buffer to hold port available extent count.
4963 * @extnt_size: buffer to hold element count per extent.
4964 *
4965 * This function calls the port and retrievs the number of available
4966 * extents and their size for a particular extent type.
4967 *
4968 * Returns: 0 if successful. Nonzero otherwise.
4969 **/
4970 int
4971 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4972 uint16_t *extnt_count, uint16_t *extnt_size)
4973 {
4974 int rc = 0;
4975 uint32_t length;
4976 uint32_t mbox_tmo;
4977 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4978 LPFC_MBOXQ_t *mbox;
4979
4980 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4981 if (!mbox)
4982 return -ENOMEM;
4983
4984 /* Find out how many extents are available for this resource type */
4985 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4986 sizeof(struct lpfc_sli4_cfg_mhdr));
4987 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4988 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4989 length, LPFC_SLI4_MBX_EMBED);
4990
4991 /* Send an extents count of 0 - the GET doesn't use it. */
4992 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4993 LPFC_SLI4_MBX_EMBED);
4994 if (unlikely(rc)) {
4995 rc = -EIO;
4996 goto err_exit;
4997 }
4998
4999 if (!phba->sli4_hba.intr_enable)
5000 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5001 else {
5002 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5003 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5004 }
5005 if (unlikely(rc)) {
5006 rc = -EIO;
5007 goto err_exit;
5008 }
5009
5010 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5011 if (bf_get(lpfc_mbox_hdr_status,
5012 &rsrc_info->header.cfg_shdr.response)) {
5013 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5014 "2930 Failed to get resource extents "
5015 "Status 0x%x Add'l Status 0x%x\n",
5016 bf_get(lpfc_mbox_hdr_status,
5017 &rsrc_info->header.cfg_shdr.response),
5018 bf_get(lpfc_mbox_hdr_add_status,
5019 &rsrc_info->header.cfg_shdr.response));
5020 rc = -EIO;
5021 goto err_exit;
5022 }
5023
5024 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5025 &rsrc_info->u.rsp);
5026 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5027 &rsrc_info->u.rsp);
5028
5029 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5030 "3162 Retrieved extents type-%d from port: count:%d, "
5031 "size:%d\n", type, *extnt_count, *extnt_size);
5032
5033 err_exit:
5034 mempool_free(mbox, phba->mbox_mem_pool);
5035 return rc;
5036 }
5037
5038 /**
5039 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5040 * @phba: Pointer to HBA context object.
5041 * @type: The extent type to check.
5042 *
5043 * This function reads the current available extents from the port and checks
5044 * if the extent count or extent size has changed since the last access.
5045 * Callers use this routine post port reset to understand if there is a
5046 * extent reprovisioning requirement.
5047 *
5048 * Returns:
5049 * -Error: error indicates problem.
5050 * 1: Extent count or size has changed.
5051 * 0: No changes.
5052 **/
5053 static int
5054 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5055 {
5056 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5057 uint16_t size_diff, rsrc_ext_size;
5058 int rc = 0;
5059 struct lpfc_rsrc_blks *rsrc_entry;
5060 struct list_head *rsrc_blk_list = NULL;
5061
5062 size_diff = 0;
5063 curr_ext_cnt = 0;
5064 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5065 &rsrc_ext_cnt,
5066 &rsrc_ext_size);
5067 if (unlikely(rc))
5068 return -EIO;
5069
5070 switch (type) {
5071 case LPFC_RSC_TYPE_FCOE_RPI:
5072 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5073 break;
5074 case LPFC_RSC_TYPE_FCOE_VPI:
5075 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5076 break;
5077 case LPFC_RSC_TYPE_FCOE_XRI:
5078 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5079 break;
5080 case LPFC_RSC_TYPE_FCOE_VFI:
5081 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5082 break;
5083 default:
5084 break;
5085 }
5086
5087 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5088 curr_ext_cnt++;
5089 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5090 size_diff++;
5091 }
5092
5093 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5094 rc = 1;
5095
5096 return rc;
5097 }
5098
5099 /**
5100 * lpfc_sli4_cfg_post_extnts -
5101 * @phba: Pointer to HBA context object.
5102 * @extnt_cnt - number of available extents.
5103 * @type - the extent type (rpi, xri, vfi, vpi).
5104 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5105 * @mbox - pointer to the caller's allocated mailbox structure.
5106 *
5107 * This function executes the extents allocation request. It also
5108 * takes care of the amount of memory needed to allocate or get the
5109 * allocated extents. It is the caller's responsibility to evaluate
5110 * the response.
5111 *
5112 * Returns:
5113 * -Error: Error value describes the condition found.
5114 * 0: if successful
5115 **/
5116 static int
5117 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5118 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5119 {
5120 int rc = 0;
5121 uint32_t req_len;
5122 uint32_t emb_len;
5123 uint32_t alloc_len, mbox_tmo;
5124
5125 /* Calculate the total requested length of the dma memory */
5126 req_len = extnt_cnt * sizeof(uint16_t);
5127
5128 /*
5129 * Calculate the size of an embedded mailbox. The uint32_t
5130 * accounts for extents-specific word.
5131 */
5132 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5133 sizeof(uint32_t);
5134
5135 /*
5136 * Presume the allocation and response will fit into an embedded
5137 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5138 */
5139 *emb = LPFC_SLI4_MBX_EMBED;
5140 if (req_len > emb_len) {
5141 req_len = extnt_cnt * sizeof(uint16_t) +
5142 sizeof(union lpfc_sli4_cfg_shdr) +
5143 sizeof(uint32_t);
5144 *emb = LPFC_SLI4_MBX_NEMBED;
5145 }
5146
5147 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5148 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5149 req_len, *emb);
5150 if (alloc_len < req_len) {
5151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5152 "2982 Allocated DMA memory size (x%x) is "
5153 "less than the requested DMA memory "
5154 "size (x%x)\n", alloc_len, req_len);
5155 return -ENOMEM;
5156 }
5157 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5158 if (unlikely(rc))
5159 return -EIO;
5160
5161 if (!phba->sli4_hba.intr_enable)
5162 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5163 else {
5164 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5165 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5166 }
5167
5168 if (unlikely(rc))
5169 rc = -EIO;
5170 return rc;
5171 }
5172
5173 /**
5174 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5175 * @phba: Pointer to HBA context object.
5176 * @type: The resource extent type to allocate.
5177 *
5178 * This function allocates the number of elements for the specified
5179 * resource type.
5180 **/
5181 static int
5182 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5183 {
5184 bool emb = false;
5185 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5186 uint16_t rsrc_id, rsrc_start, j, k;
5187 uint16_t *ids;
5188 int i, rc;
5189 unsigned long longs;
5190 unsigned long *bmask;
5191 struct lpfc_rsrc_blks *rsrc_blks;
5192 LPFC_MBOXQ_t *mbox;
5193 uint32_t length;
5194 struct lpfc_id_range *id_array = NULL;
5195 void *virtaddr = NULL;
5196 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5197 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5198 struct list_head *ext_blk_list;
5199
5200 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5201 &rsrc_cnt,
5202 &rsrc_size);
5203 if (unlikely(rc))
5204 return -EIO;
5205
5206 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5208 "3009 No available Resource Extents "
5209 "for resource type 0x%x: Count: 0x%x, "
5210 "Size 0x%x\n", type, rsrc_cnt,
5211 rsrc_size);
5212 return -ENOMEM;
5213 }
5214
5215 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5216 "2903 Post resource extents type-0x%x: "
5217 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5218
5219 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5220 if (!mbox)
5221 return -ENOMEM;
5222
5223 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5224 if (unlikely(rc)) {
5225 rc = -EIO;
5226 goto err_exit;
5227 }
5228
5229 /*
5230 * Figure out where the response is located. Then get local pointers
5231 * to the response data. The port does not guarantee to respond to
5232 * all extents counts request so update the local variable with the
5233 * allocated count from the port.
5234 */
5235 if (emb == LPFC_SLI4_MBX_EMBED) {
5236 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5237 id_array = &rsrc_ext->u.rsp.id[0];
5238 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5239 } else {
5240 virtaddr = mbox->sge_array->addr[0];
5241 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5242 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5243 id_array = &n_rsrc->id;
5244 }
5245
5246 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5247 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5248
5249 /*
5250 * Based on the resource size and count, correct the base and max
5251 * resource values.
5252 */
5253 length = sizeof(struct lpfc_rsrc_blks);
5254 switch (type) {
5255 case LPFC_RSC_TYPE_FCOE_RPI:
5256 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5257 sizeof(unsigned long),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5260 rc = -ENOMEM;
5261 goto err_exit;
5262 }
5263 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5264 sizeof(uint16_t),
5265 GFP_KERNEL);
5266 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5267 kfree(phba->sli4_hba.rpi_bmask);
5268 rc = -ENOMEM;
5269 goto err_exit;
5270 }
5271
5272 /*
5273 * The next_rpi was initialized with the maximum available
5274 * count but the port may allocate a smaller number. Catch
5275 * that case and update the next_rpi.
5276 */
5277 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5278
5279 /* Initialize local ptrs for common extent processing later. */
5280 bmask = phba->sli4_hba.rpi_bmask;
5281 ids = phba->sli4_hba.rpi_ids;
5282 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5283 break;
5284 case LPFC_RSC_TYPE_FCOE_VPI:
5285 phba->vpi_bmask = kzalloc(longs *
5286 sizeof(unsigned long),
5287 GFP_KERNEL);
5288 if (unlikely(!phba->vpi_bmask)) {
5289 rc = -ENOMEM;
5290 goto err_exit;
5291 }
5292 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5293 sizeof(uint16_t),
5294 GFP_KERNEL);
5295 if (unlikely(!phba->vpi_ids)) {
5296 kfree(phba->vpi_bmask);
5297 rc = -ENOMEM;
5298 goto err_exit;
5299 }
5300
5301 /* Initialize local ptrs for common extent processing later. */
5302 bmask = phba->vpi_bmask;
5303 ids = phba->vpi_ids;
5304 ext_blk_list = &phba->lpfc_vpi_blk_list;
5305 break;
5306 case LPFC_RSC_TYPE_FCOE_XRI:
5307 phba->sli4_hba.xri_bmask = kzalloc(longs *
5308 sizeof(unsigned long),
5309 GFP_KERNEL);
5310 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5311 rc = -ENOMEM;
5312 goto err_exit;
5313 }
5314 phba->sli4_hba.max_cfg_param.xri_used = 0;
5315 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5316 sizeof(uint16_t),
5317 GFP_KERNEL);
5318 if (unlikely(!phba->sli4_hba.xri_ids)) {
5319 kfree(phba->sli4_hba.xri_bmask);
5320 rc = -ENOMEM;
5321 goto err_exit;
5322 }
5323
5324 /* Initialize local ptrs for common extent processing later. */
5325 bmask = phba->sli4_hba.xri_bmask;
5326 ids = phba->sli4_hba.xri_ids;
5327 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5328 break;
5329 case LPFC_RSC_TYPE_FCOE_VFI:
5330 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5331 sizeof(unsigned long),
5332 GFP_KERNEL);
5333 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5334 rc = -ENOMEM;
5335 goto err_exit;
5336 }
5337 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5338 sizeof(uint16_t),
5339 GFP_KERNEL);
5340 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5341 kfree(phba->sli4_hba.vfi_bmask);
5342 rc = -ENOMEM;
5343 goto err_exit;
5344 }
5345
5346 /* Initialize local ptrs for common extent processing later. */
5347 bmask = phba->sli4_hba.vfi_bmask;
5348 ids = phba->sli4_hba.vfi_ids;
5349 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5350 break;
5351 default:
5352 /* Unsupported Opcode. Fail call. */
5353 id_array = NULL;
5354 bmask = NULL;
5355 ids = NULL;
5356 ext_blk_list = NULL;
5357 goto err_exit;
5358 }
5359
5360 /*
5361 * Complete initializing the extent configuration with the
5362 * allocated ids assigned to this function. The bitmask serves
5363 * as an index into the array and manages the available ids. The
5364 * array just stores the ids communicated to the port via the wqes.
5365 */
5366 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5367 if ((i % 2) == 0)
5368 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5369 &id_array[k]);
5370 else
5371 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5372 &id_array[k]);
5373
5374 rsrc_blks = kzalloc(length, GFP_KERNEL);
5375 if (unlikely(!rsrc_blks)) {
5376 rc = -ENOMEM;
5377 kfree(bmask);
5378 kfree(ids);
5379 goto err_exit;
5380 }
5381 rsrc_blks->rsrc_start = rsrc_id;
5382 rsrc_blks->rsrc_size = rsrc_size;
5383 list_add_tail(&rsrc_blks->list, ext_blk_list);
5384 rsrc_start = rsrc_id;
5385 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5386 phba->sli4_hba.scsi_xri_start = rsrc_start +
5387 lpfc_sli4_get_els_iocb_cnt(phba);
5388
5389 while (rsrc_id < (rsrc_start + rsrc_size)) {
5390 ids[j] = rsrc_id;
5391 rsrc_id++;
5392 j++;
5393 }
5394 /* Entire word processed. Get next word.*/
5395 if ((i % 2) == 1)
5396 k++;
5397 }
5398 err_exit:
5399 lpfc_sli4_mbox_cmd_free(phba, mbox);
5400 return rc;
5401 }
5402
5403 /**
5404 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5405 * @phba: Pointer to HBA context object.
5406 * @type: the extent's type.
5407 *
5408 * This function deallocates all extents of a particular resource type.
5409 * SLI4 does not allow for deallocating a particular extent range. It
5410 * is the caller's responsibility to release all kernel memory resources.
5411 **/
5412 static int
5413 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5414 {
5415 int rc;
5416 uint32_t length, mbox_tmo = 0;
5417 LPFC_MBOXQ_t *mbox;
5418 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5419 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5420
5421 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5422 if (!mbox)
5423 return -ENOMEM;
5424
5425 /*
5426 * This function sends an embedded mailbox because it only sends the
5427 * the resource type. All extents of this type are released by the
5428 * port.
5429 */
5430 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5431 sizeof(struct lpfc_sli4_cfg_mhdr));
5432 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5433 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5434 length, LPFC_SLI4_MBX_EMBED);
5435
5436 /* Send an extents count of 0 - the dealloc doesn't use it. */
5437 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5438 LPFC_SLI4_MBX_EMBED);
5439 if (unlikely(rc)) {
5440 rc = -EIO;
5441 goto out_free_mbox;
5442 }
5443 if (!phba->sli4_hba.intr_enable)
5444 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5445 else {
5446 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5447 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5448 }
5449 if (unlikely(rc)) {
5450 rc = -EIO;
5451 goto out_free_mbox;
5452 }
5453
5454 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5455 if (bf_get(lpfc_mbox_hdr_status,
5456 &dealloc_rsrc->header.cfg_shdr.response)) {
5457 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5458 "2919 Failed to release resource extents "
5459 "for type %d - Status 0x%x Add'l Status 0x%x. "
5460 "Resource memory not released.\n",
5461 type,
5462 bf_get(lpfc_mbox_hdr_status,
5463 &dealloc_rsrc->header.cfg_shdr.response),
5464 bf_get(lpfc_mbox_hdr_add_status,
5465 &dealloc_rsrc->header.cfg_shdr.response));
5466 rc = -EIO;
5467 goto out_free_mbox;
5468 }
5469
5470 /* Release kernel memory resources for the specific type. */
5471 switch (type) {
5472 case LPFC_RSC_TYPE_FCOE_VPI:
5473 kfree(phba->vpi_bmask);
5474 kfree(phba->vpi_ids);
5475 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5476 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5477 &phba->lpfc_vpi_blk_list, list) {
5478 list_del_init(&rsrc_blk->list);
5479 kfree(rsrc_blk);
5480 }
5481 break;
5482 case LPFC_RSC_TYPE_FCOE_XRI:
5483 kfree(phba->sli4_hba.xri_bmask);
5484 kfree(phba->sli4_hba.xri_ids);
5485 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5486 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5487 list_del_init(&rsrc_blk->list);
5488 kfree(rsrc_blk);
5489 }
5490 break;
5491 case LPFC_RSC_TYPE_FCOE_VFI:
5492 kfree(phba->sli4_hba.vfi_bmask);
5493 kfree(phba->sli4_hba.vfi_ids);
5494 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5495 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5496 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5497 list_del_init(&rsrc_blk->list);
5498 kfree(rsrc_blk);
5499 }
5500 break;
5501 case LPFC_RSC_TYPE_FCOE_RPI:
5502 /* RPI bitmask and physical id array are cleaned up earlier. */
5503 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5504 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5505 list_del_init(&rsrc_blk->list);
5506 kfree(rsrc_blk);
5507 }
5508 break;
5509 default:
5510 break;
5511 }
5512
5513 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5514
5515 out_free_mbox:
5516 mempool_free(mbox, phba->mbox_mem_pool);
5517 return rc;
5518 }
5519
5520 /**
5521 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5522 * @phba: Pointer to HBA context object.
5523 *
5524 * This function allocates all SLI4 resource identifiers.
5525 **/
5526 int
5527 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5528 {
5529 int i, rc, error = 0;
5530 uint16_t count, base;
5531 unsigned long longs;
5532
5533 if (!phba->sli4_hba.rpi_hdrs_in_use)
5534 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5535 if (phba->sli4_hba.extents_in_use) {
5536 /*
5537 * The port supports resource extents. The XRI, VPI, VFI, RPI
5538 * resource extent count must be read and allocated before
5539 * provisioning the resource id arrays.
5540 */
5541 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5542 LPFC_IDX_RSRC_RDY) {
5543 /*
5544 * Extent-based resources are set - the driver could
5545 * be in a port reset. Figure out if any corrective
5546 * actions need to be taken.
5547 */
5548 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5549 LPFC_RSC_TYPE_FCOE_VFI);
5550 if (rc != 0)
5551 error++;
5552 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5553 LPFC_RSC_TYPE_FCOE_VPI);
5554 if (rc != 0)
5555 error++;
5556 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5557 LPFC_RSC_TYPE_FCOE_XRI);
5558 if (rc != 0)
5559 error++;
5560 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5561 LPFC_RSC_TYPE_FCOE_RPI);
5562 if (rc != 0)
5563 error++;
5564
5565 /*
5566 * It's possible that the number of resources
5567 * provided to this port instance changed between
5568 * resets. Detect this condition and reallocate
5569 * resources. Otherwise, there is no action.
5570 */
5571 if (error) {
5572 lpfc_printf_log(phba, KERN_INFO,
5573 LOG_MBOX | LOG_INIT,
5574 "2931 Detected extent resource "
5575 "change. Reallocating all "
5576 "extents.\n");
5577 rc = lpfc_sli4_dealloc_extent(phba,
5578 LPFC_RSC_TYPE_FCOE_VFI);
5579 rc = lpfc_sli4_dealloc_extent(phba,
5580 LPFC_RSC_TYPE_FCOE_VPI);
5581 rc = lpfc_sli4_dealloc_extent(phba,
5582 LPFC_RSC_TYPE_FCOE_XRI);
5583 rc = lpfc_sli4_dealloc_extent(phba,
5584 LPFC_RSC_TYPE_FCOE_RPI);
5585 } else
5586 return 0;
5587 }
5588
5589 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5590 if (unlikely(rc))
5591 goto err_exit;
5592
5593 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5594 if (unlikely(rc))
5595 goto err_exit;
5596
5597 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5598 if (unlikely(rc))
5599 goto err_exit;
5600
5601 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5602 if (unlikely(rc))
5603 goto err_exit;
5604 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5605 LPFC_IDX_RSRC_RDY);
5606 return rc;
5607 } else {
5608 /*
5609 * The port does not support resource extents. The XRI, VPI,
5610 * VFI, RPI resource ids were determined from READ_CONFIG.
5611 * Just allocate the bitmasks and provision the resource id
5612 * arrays. If a port reset is active, the resources don't
5613 * need any action - just exit.
5614 */
5615 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5616 LPFC_IDX_RSRC_RDY) {
5617 lpfc_sli4_dealloc_resource_identifiers(phba);
5618 lpfc_sli4_remove_rpis(phba);
5619 }
5620 /* RPIs. */
5621 count = phba->sli4_hba.max_cfg_param.max_rpi;
5622 base = phba->sli4_hba.max_cfg_param.rpi_base;
5623 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5624 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5625 sizeof(unsigned long),
5626 GFP_KERNEL);
5627 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5628 rc = -ENOMEM;
5629 goto err_exit;
5630 }
5631 phba->sli4_hba.rpi_ids = kzalloc(count *
5632 sizeof(uint16_t),
5633 GFP_KERNEL);
5634 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5635 rc = -ENOMEM;
5636 goto free_rpi_bmask;
5637 }
5638
5639 for (i = 0; i < count; i++)
5640 phba->sli4_hba.rpi_ids[i] = base + i;
5641
5642 /* VPIs. */
5643 count = phba->sli4_hba.max_cfg_param.max_vpi;
5644 base = phba->sli4_hba.max_cfg_param.vpi_base;
5645 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5646 phba->vpi_bmask = kzalloc(longs *
5647 sizeof(unsigned long),
5648 GFP_KERNEL);
5649 if (unlikely(!phba->vpi_bmask)) {
5650 rc = -ENOMEM;
5651 goto free_rpi_ids;
5652 }
5653 phba->vpi_ids = kzalloc(count *
5654 sizeof(uint16_t),
5655 GFP_KERNEL);
5656 if (unlikely(!phba->vpi_ids)) {
5657 rc = -ENOMEM;
5658 goto free_vpi_bmask;
5659 }
5660
5661 for (i = 0; i < count; i++)
5662 phba->vpi_ids[i] = base + i;
5663
5664 /* XRIs. */
5665 count = phba->sli4_hba.max_cfg_param.max_xri;
5666 base = phba->sli4_hba.max_cfg_param.xri_base;
5667 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5668 phba->sli4_hba.xri_bmask = kzalloc(longs *
5669 sizeof(unsigned long),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5672 rc = -ENOMEM;
5673 goto free_vpi_ids;
5674 }
5675 phba->sli4_hba.max_cfg_param.xri_used = 0;
5676 phba->sli4_hba.xri_ids = kzalloc(count *
5677 sizeof(uint16_t),
5678 GFP_KERNEL);
5679 if (unlikely(!phba->sli4_hba.xri_ids)) {
5680 rc = -ENOMEM;
5681 goto free_xri_bmask;
5682 }
5683
5684 for (i = 0; i < count; i++)
5685 phba->sli4_hba.xri_ids[i] = base + i;
5686
5687 /* VFIs. */
5688 count = phba->sli4_hba.max_cfg_param.max_vfi;
5689 base = phba->sli4_hba.max_cfg_param.vfi_base;
5690 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5691 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5692 sizeof(unsigned long),
5693 GFP_KERNEL);
5694 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5695 rc = -ENOMEM;
5696 goto free_xri_ids;
5697 }
5698 phba->sli4_hba.vfi_ids = kzalloc(count *
5699 sizeof(uint16_t),
5700 GFP_KERNEL);
5701 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5702 rc = -ENOMEM;
5703 goto free_vfi_bmask;
5704 }
5705
5706 for (i = 0; i < count; i++)
5707 phba->sli4_hba.vfi_ids[i] = base + i;
5708
5709 /*
5710 * Mark all resources ready. An HBA reset doesn't need
5711 * to reset the initialization.
5712 */
5713 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5714 LPFC_IDX_RSRC_RDY);
5715 return 0;
5716 }
5717
5718 free_vfi_bmask:
5719 kfree(phba->sli4_hba.vfi_bmask);
5720 free_xri_ids:
5721 kfree(phba->sli4_hba.xri_ids);
5722 free_xri_bmask:
5723 kfree(phba->sli4_hba.xri_bmask);
5724 free_vpi_ids:
5725 kfree(phba->vpi_ids);
5726 free_vpi_bmask:
5727 kfree(phba->vpi_bmask);
5728 free_rpi_ids:
5729 kfree(phba->sli4_hba.rpi_ids);
5730 free_rpi_bmask:
5731 kfree(phba->sli4_hba.rpi_bmask);
5732 err_exit:
5733 return rc;
5734 }
5735
5736 /**
5737 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5738 * @phba: Pointer to HBA context object.
5739 *
5740 * This function allocates the number of elements for the specified
5741 * resource type.
5742 **/
5743 int
5744 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5745 {
5746 if (phba->sli4_hba.extents_in_use) {
5747 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5748 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5749 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5750 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5751 } else {
5752 kfree(phba->vpi_bmask);
5753 kfree(phba->vpi_ids);
5754 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5755 kfree(phba->sli4_hba.xri_bmask);
5756 kfree(phba->sli4_hba.xri_ids);
5757 kfree(phba->sli4_hba.vfi_bmask);
5758 kfree(phba->sli4_hba.vfi_ids);
5759 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5760 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5761 }
5762
5763 return 0;
5764 }
5765
5766 /**
5767 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5768 * @phba: Pointer to HBA context object.
5769 * @type: The resource extent type.
5770 * @extnt_count: buffer to hold port extent count response
5771 * @extnt_size: buffer to hold port extent size response.
5772 *
5773 * This function calls the port to read the host allocated extents
5774 * for a particular type.
5775 **/
5776 int
5777 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5778 uint16_t *extnt_cnt, uint16_t *extnt_size)
5779 {
5780 bool emb;
5781 int rc = 0;
5782 uint16_t curr_blks = 0;
5783 uint32_t req_len, emb_len;
5784 uint32_t alloc_len, mbox_tmo;
5785 struct list_head *blk_list_head;
5786 struct lpfc_rsrc_blks *rsrc_blk;
5787 LPFC_MBOXQ_t *mbox;
5788 void *virtaddr = NULL;
5789 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5790 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5791 union lpfc_sli4_cfg_shdr *shdr;
5792
5793 switch (type) {
5794 case LPFC_RSC_TYPE_FCOE_VPI:
5795 blk_list_head = &phba->lpfc_vpi_blk_list;
5796 break;
5797 case LPFC_RSC_TYPE_FCOE_XRI:
5798 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5799 break;
5800 case LPFC_RSC_TYPE_FCOE_VFI:
5801 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5802 break;
5803 case LPFC_RSC_TYPE_FCOE_RPI:
5804 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5805 break;
5806 default:
5807 return -EIO;
5808 }
5809
5810 /* Count the number of extents currently allocatd for this type. */
5811 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5812 if (curr_blks == 0) {
5813 /*
5814 * The GET_ALLOCATED mailbox does not return the size,
5815 * just the count. The size should be just the size
5816 * stored in the current allocated block and all sizes
5817 * for an extent type are the same so set the return
5818 * value now.
5819 */
5820 *extnt_size = rsrc_blk->rsrc_size;
5821 }
5822 curr_blks++;
5823 }
5824
5825 /* Calculate the total requested length of the dma memory. */
5826 req_len = curr_blks * sizeof(uint16_t);
5827
5828 /*
5829 * Calculate the size of an embedded mailbox. The uint32_t
5830 * accounts for extents-specific word.
5831 */
5832 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5833 sizeof(uint32_t);
5834
5835 /*
5836 * Presume the allocation and response will fit into an embedded
5837 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5838 */
5839 emb = LPFC_SLI4_MBX_EMBED;
5840 req_len = emb_len;
5841 if (req_len > emb_len) {
5842 req_len = curr_blks * sizeof(uint16_t) +
5843 sizeof(union lpfc_sli4_cfg_shdr) +
5844 sizeof(uint32_t);
5845 emb = LPFC_SLI4_MBX_NEMBED;
5846 }
5847
5848 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5849 if (!mbox)
5850 return -ENOMEM;
5851 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5852
5853 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5854 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5855 req_len, emb);
5856 if (alloc_len < req_len) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5858 "2983 Allocated DMA memory size (x%x) is "
5859 "less than the requested DMA memory "
5860 "size (x%x)\n", alloc_len, req_len);
5861 rc = -ENOMEM;
5862 goto err_exit;
5863 }
5864 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5865 if (unlikely(rc)) {
5866 rc = -EIO;
5867 goto err_exit;
5868 }
5869
5870 if (!phba->sli4_hba.intr_enable)
5871 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5872 else {
5873 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5874 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5875 }
5876
5877 if (unlikely(rc)) {
5878 rc = -EIO;
5879 goto err_exit;
5880 }
5881
5882 /*
5883 * Figure out where the response is located. Then get local pointers
5884 * to the response data. The port does not guarantee to respond to
5885 * all extents counts request so update the local variable with the
5886 * allocated count from the port.
5887 */
5888 if (emb == LPFC_SLI4_MBX_EMBED) {
5889 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5890 shdr = &rsrc_ext->header.cfg_shdr;
5891 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5892 } else {
5893 virtaddr = mbox->sge_array->addr[0];
5894 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5895 shdr = &n_rsrc->cfg_shdr;
5896 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5897 }
5898
5899 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5900 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5901 "2984 Failed to read allocated resources "
5902 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5903 type,
5904 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5905 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5906 rc = -EIO;
5907 goto err_exit;
5908 }
5909 err_exit:
5910 lpfc_sli4_mbox_cmd_free(phba, mbox);
5911 return rc;
5912 }
5913
5914 /**
5915 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5916 * @phba: pointer to lpfc hba data structure.
5917 *
5918 * This routine walks the list of els buffers that have been allocated and
5919 * repost them to the port by using SGL block post. This is needed after a
5920 * pci_function_reset/warm_start or start. It attempts to construct blocks
5921 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5922 * SGL block post mailbox commands to post them to the port. For single els
5923 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5924 * mailbox command for posting.
5925 *
5926 * Returns: 0 = success, non-zero failure.
5927 **/
5928 static int
5929 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5930 {
5931 struct lpfc_sglq *sglq_entry = NULL;
5932 struct lpfc_sglq *sglq_entry_next = NULL;
5933 struct lpfc_sglq *sglq_entry_first = NULL;
5934 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5935 int last_xritag = NO_XRI;
5936 LIST_HEAD(prep_sgl_list);
5937 LIST_HEAD(blck_sgl_list);
5938 LIST_HEAD(allc_sgl_list);
5939 LIST_HEAD(post_sgl_list);
5940 LIST_HEAD(free_sgl_list);
5941
5942 spin_lock(&phba->hbalock);
5943 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5944 spin_unlock(&phba->hbalock);
5945
5946 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5947 &allc_sgl_list, list) {
5948 list_del_init(&sglq_entry->list);
5949 block_cnt++;
5950 if ((last_xritag != NO_XRI) &&
5951 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5952 /* a hole in xri block, form a sgl posting block */
5953 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5954 post_cnt = block_cnt - 1;
5955 /* prepare list for next posting block */
5956 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5957 block_cnt = 1;
5958 } else {
5959 /* prepare list for next posting block */
5960 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5961 /* enough sgls for non-embed sgl mbox command */
5962 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5963 list_splice_init(&prep_sgl_list,
5964 &blck_sgl_list);
5965 post_cnt = block_cnt;
5966 block_cnt = 0;
5967 }
5968 }
5969 num_posted++;
5970
5971 /* keep track of last sgl's xritag */
5972 last_xritag = sglq_entry->sli4_xritag;
5973
5974 /* end of repost sgl list condition for els buffers */
5975 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5976 if (post_cnt == 0) {
5977 list_splice_init(&prep_sgl_list,
5978 &blck_sgl_list);
5979 post_cnt = block_cnt;
5980 } else if (block_cnt == 1) {
5981 status = lpfc_sli4_post_sgl(phba,
5982 sglq_entry->phys, 0,
5983 sglq_entry->sli4_xritag);
5984 if (!status) {
5985 /* successful, put sgl to posted list */
5986 list_add_tail(&sglq_entry->list,
5987 &post_sgl_list);
5988 } else {
5989 /* Failure, put sgl to free list */
5990 lpfc_printf_log(phba, KERN_WARNING,
5991 LOG_SLI,
5992 "3159 Failed to post els "
5993 "sgl, xritag:x%x\n",
5994 sglq_entry->sli4_xritag);
5995 list_add_tail(&sglq_entry->list,
5996 &free_sgl_list);
5997 spin_lock_irq(&phba->hbalock);
5998 phba->sli4_hba.els_xri_cnt--;
5999 spin_unlock_irq(&phba->hbalock);
6000 }
6001 }
6002 }
6003
6004 /* continue until a nembed page worth of sgls */
6005 if (post_cnt == 0)
6006 continue;
6007
6008 /* post the els buffer list sgls as a block */
6009 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6010 post_cnt);
6011
6012 if (!status) {
6013 /* success, put sgl list to posted sgl list */
6014 list_splice_init(&blck_sgl_list, &post_sgl_list);
6015 } else {
6016 /* Failure, put sgl list to free sgl list */
6017 sglq_entry_first = list_first_entry(&blck_sgl_list,
6018 struct lpfc_sglq,
6019 list);
6020 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6021 "3160 Failed to post els sgl-list, "
6022 "xritag:x%x-x%x\n",
6023 sglq_entry_first->sli4_xritag,
6024 (sglq_entry_first->sli4_xritag +
6025 post_cnt - 1));
6026 list_splice_init(&blck_sgl_list, &free_sgl_list);
6027 spin_lock_irq(&phba->hbalock);
6028 phba->sli4_hba.els_xri_cnt -= post_cnt;
6029 spin_unlock_irq(&phba->hbalock);
6030 }
6031
6032 /* don't reset xirtag due to hole in xri block */
6033 if (block_cnt == 0)
6034 last_xritag = NO_XRI;
6035
6036 /* reset els sgl post count for next round of posting */
6037 post_cnt = 0;
6038 }
6039
6040 /* free the els sgls failed to post */
6041 lpfc_free_sgl_list(phba, &free_sgl_list);
6042
6043 /* push els sgls posted to the availble list */
6044 if (!list_empty(&post_sgl_list)) {
6045 spin_lock(&phba->hbalock);
6046 list_splice_init(&post_sgl_list,
6047 &phba->sli4_hba.lpfc_sgl_list);
6048 spin_unlock(&phba->hbalock);
6049 } else {
6050 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6051 "3161 Failure to post els sgl to port.\n");
6052 return -EIO;
6053 }
6054 return 0;
6055 }
6056
6057 /**
6058 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6059 * @phba: Pointer to HBA context object.
6060 *
6061 * This function is the main SLI4 device intialization PCI function. This
6062 * function is called by the HBA intialization code, HBA reset code and
6063 * HBA error attention handler code. Caller is not required to hold any
6064 * locks.
6065 **/
6066 int
6067 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6068 {
6069 int rc;
6070 LPFC_MBOXQ_t *mboxq;
6071 struct lpfc_mqe *mqe;
6072 uint8_t *vpd;
6073 uint32_t vpd_size;
6074 uint32_t ftr_rsp = 0;
6075 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6076 struct lpfc_vport *vport = phba->pport;
6077 struct lpfc_dmabuf *mp;
6078
6079 /* Perform a PCI function reset to start from clean */
6080 rc = lpfc_pci_function_reset(phba);
6081 if (unlikely(rc))
6082 return -ENODEV;
6083
6084 /* Check the HBA Host Status Register for readyness */
6085 rc = lpfc_sli4_post_status_check(phba);
6086 if (unlikely(rc))
6087 return -ENODEV;
6088 else {
6089 spin_lock_irq(&phba->hbalock);
6090 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6091 spin_unlock_irq(&phba->hbalock);
6092 }
6093
6094 /*
6095 * Allocate a single mailbox container for initializing the
6096 * port.
6097 */
6098 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6099 if (!mboxq)
6100 return -ENOMEM;
6101
6102 /* Issue READ_REV to collect vpd and FW information. */
6103 vpd_size = SLI4_PAGE_SIZE;
6104 vpd = kzalloc(vpd_size, GFP_KERNEL);
6105 if (!vpd) {
6106 rc = -ENOMEM;
6107 goto out_free_mbox;
6108 }
6109
6110 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6111 if (unlikely(rc)) {
6112 kfree(vpd);
6113 goto out_free_mbox;
6114 }
6115 mqe = &mboxq->u.mqe;
6116 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6117 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
6118 phba->hba_flag |= HBA_FCOE_MODE;
6119 else
6120 phba->hba_flag &= ~HBA_FCOE_MODE;
6121
6122 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6123 LPFC_DCBX_CEE_MODE)
6124 phba->hba_flag |= HBA_FIP_SUPPORT;
6125 else
6126 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6127
6128 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6129
6130 if (phba->sli_rev != LPFC_SLI_REV4) {
6131 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6132 "0376 READ_REV Error. SLI Level %d "
6133 "FCoE enabled %d\n",
6134 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6135 rc = -EIO;
6136 kfree(vpd);
6137 goto out_free_mbox;
6138 }
6139
6140 /*
6141 * Continue initialization with default values even if driver failed
6142 * to read FCoE param config regions, only read parameters if the
6143 * board is FCoE
6144 */
6145 if (phba->hba_flag & HBA_FCOE_MODE &&
6146 lpfc_sli4_read_fcoe_params(phba))
6147 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6148 "2570 Failed to read FCoE parameters\n");
6149
6150 /*
6151 * Retrieve sli4 device physical port name, failure of doing it
6152 * is considered as non-fatal.
6153 */
6154 rc = lpfc_sli4_retrieve_pport_name(phba);
6155 if (!rc)
6156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6157 "3080 Successful retrieving SLI4 device "
6158 "physical port name: %s.\n", phba->Port);
6159
6160 /*
6161 * Evaluate the read rev and vpd data. Populate the driver
6162 * state with the results. If this routine fails, the failure
6163 * is not fatal as the driver will use generic values.
6164 */
6165 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6166 if (unlikely(!rc)) {
6167 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6168 "0377 Error %d parsing vpd. "
6169 "Using defaults.\n", rc);
6170 rc = 0;
6171 }
6172 kfree(vpd);
6173
6174 /* Save information as VPD data */
6175 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6176 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6177 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6178 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6179 &mqe->un.read_rev);
6180 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6181 &mqe->un.read_rev);
6182 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6183 &mqe->un.read_rev);
6184 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6185 &mqe->un.read_rev);
6186 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6187 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6188 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6189 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6190 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6191 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6192 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6193 "(%d):0380 READ_REV Status x%x "
6194 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6195 mboxq->vport ? mboxq->vport->vpi : 0,
6196 bf_get(lpfc_mqe_status, mqe),
6197 phba->vpd.rev.opFwName,
6198 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6199 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6200
6201 /*
6202 * Discover the port's supported feature set and match it against the
6203 * hosts requests.
6204 */
6205 lpfc_request_features(phba, mboxq);
6206 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6207 if (unlikely(rc)) {
6208 rc = -EIO;
6209 goto out_free_mbox;
6210 }
6211
6212 /*
6213 * The port must support FCP initiator mode as this is the
6214 * only mode running in the host.
6215 */
6216 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6217 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6218 "0378 No support for fcpi mode.\n");
6219 ftr_rsp++;
6220 }
6221 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6222 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6223 else
6224 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6225 /*
6226 * If the port cannot support the host's requested features
6227 * then turn off the global config parameters to disable the
6228 * feature in the driver. This is not a fatal error.
6229 */
6230 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6231 if (phba->cfg_enable_bg) {
6232 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6233 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6234 else
6235 ftr_rsp++;
6236 }
6237
6238 if (phba->max_vpi && phba->cfg_enable_npiv &&
6239 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6240 ftr_rsp++;
6241
6242 if (ftr_rsp) {
6243 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6244 "0379 Feature Mismatch Data: x%08x %08x "
6245 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6246 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6247 phba->cfg_enable_npiv, phba->max_vpi);
6248 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6249 phba->cfg_enable_bg = 0;
6250 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6251 phba->cfg_enable_npiv = 0;
6252 }
6253
6254 /* These SLI3 features are assumed in SLI4 */
6255 spin_lock_irq(&phba->hbalock);
6256 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6257 spin_unlock_irq(&phba->hbalock);
6258
6259 /*
6260 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6261 * calls depends on these resources to complete port setup.
6262 */
6263 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6264 if (rc) {
6265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6266 "2920 Failed to alloc Resource IDs "
6267 "rc = x%x\n", rc);
6268 goto out_free_mbox;
6269 }
6270
6271 /* Read the port's service parameters. */
6272 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6273 if (rc) {
6274 phba->link_state = LPFC_HBA_ERROR;
6275 rc = -ENOMEM;
6276 goto out_free_mbox;
6277 }
6278
6279 mboxq->vport = vport;
6280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6281 mp = (struct lpfc_dmabuf *) mboxq->context1;
6282 if (rc == MBX_SUCCESS) {
6283 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6284 rc = 0;
6285 }
6286
6287 /*
6288 * This memory was allocated by the lpfc_read_sparam routine. Release
6289 * it to the mbuf pool.
6290 */
6291 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6292 kfree(mp);
6293 mboxq->context1 = NULL;
6294 if (unlikely(rc)) {
6295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6296 "0382 READ_SPARAM command failed "
6297 "status %d, mbxStatus x%x\n",
6298 rc, bf_get(lpfc_mqe_status, mqe));
6299 phba->link_state = LPFC_HBA_ERROR;
6300 rc = -EIO;
6301 goto out_free_mbox;
6302 }
6303
6304 lpfc_update_vport_wwn(vport);
6305
6306 /* Update the fc_host data structures with new wwn. */
6307 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6308 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6309
6310 /* update host els and scsi xri-sgl sizes and mappings */
6311 rc = lpfc_sli4_xri_sgl_update(phba);
6312 if (unlikely(rc)) {
6313 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6314 "1400 Failed to update xri-sgl size and "
6315 "mapping: %d\n", rc);
6316 goto out_free_mbox;
6317 }
6318
6319 /* register the els sgl pool to the port */
6320 rc = lpfc_sli4_repost_els_sgl_list(phba);
6321 if (unlikely(rc)) {
6322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6323 "0582 Error %d during els sgl post "
6324 "operation\n", rc);
6325 rc = -ENODEV;
6326 goto out_free_mbox;
6327 }
6328
6329 /* register the allocated scsi sgl pool to the port */
6330 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6331 if (unlikely(rc)) {
6332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6333 "0383 Error %d during scsi sgl post "
6334 "operation\n", rc);
6335 /* Some Scsi buffers were moved to the abort scsi list */
6336 /* A pci function reset will repost them */
6337 rc = -ENODEV;
6338 goto out_free_mbox;
6339 }
6340
6341 /* Post the rpi header region to the device. */
6342 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6343 if (unlikely(rc)) {
6344 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6345 "0393 Error %d during rpi post operation\n",
6346 rc);
6347 rc = -ENODEV;
6348 goto out_free_mbox;
6349 }
6350 lpfc_sli4_node_prep(phba);
6351
6352 /* Create all the SLI4 queues */
6353 rc = lpfc_sli4_queue_create(phba);
6354 if (rc) {
6355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6356 "3089 Failed to allocate queues\n");
6357 rc = -ENODEV;
6358 goto out_stop_timers;
6359 }
6360 /* Set up all the queues to the device */
6361 rc = lpfc_sli4_queue_setup(phba);
6362 if (unlikely(rc)) {
6363 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6364 "0381 Error %d during queue setup.\n ", rc);
6365 goto out_destroy_queue;
6366 }
6367
6368 /* Arm the CQs and then EQs on device */
6369 lpfc_sli4_arm_cqeq_intr(phba);
6370
6371 /* Indicate device interrupt mode */
6372 phba->sli4_hba.intr_enable = 1;
6373
6374 /* Allow asynchronous mailbox command to go through */
6375 spin_lock_irq(&phba->hbalock);
6376 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6377 spin_unlock_irq(&phba->hbalock);
6378
6379 /* Post receive buffers to the device */
6380 lpfc_sli4_rb_setup(phba);
6381
6382 /* Reset HBA FCF states after HBA reset */
6383 phba->fcf.fcf_flag = 0;
6384 phba->fcf.current_rec.flag = 0;
6385
6386 /* Start the ELS watchdog timer */
6387 mod_timer(&vport->els_tmofunc,
6388 jiffies + HZ * (phba->fc_ratov * 2));
6389
6390 /* Start heart beat timer */
6391 mod_timer(&phba->hb_tmofunc,
6392 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6393 phba->hb_outstanding = 0;
6394 phba->last_completion_time = jiffies;
6395
6396 /* Start error attention (ERATT) polling timer */
6397 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6398
6399 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6400 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6401 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6402 if (!rc) {
6403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6404 "2829 This device supports "
6405 "Advanced Error Reporting (AER)\n");
6406 spin_lock_irq(&phba->hbalock);
6407 phba->hba_flag |= HBA_AER_ENABLED;
6408 spin_unlock_irq(&phba->hbalock);
6409 } else {
6410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6411 "2830 This device does not support "
6412 "Advanced Error Reporting (AER)\n");
6413 phba->cfg_aer_support = 0;
6414 }
6415 rc = 0;
6416 }
6417
6418 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6419 /*
6420 * The FC Port needs to register FCFI (index 0)
6421 */
6422 lpfc_reg_fcfi(phba, mboxq);
6423 mboxq->vport = phba->pport;
6424 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6425 if (rc != MBX_SUCCESS)
6426 goto out_unset_queue;
6427 rc = 0;
6428 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6429 &mboxq->u.mqe.un.reg_fcfi);
6430
6431 /* Check if the port is configured to be disabled */
6432 lpfc_sli_read_link_ste(phba);
6433 }
6434
6435 /*
6436 * The port is ready, set the host's link state to LINK_DOWN
6437 * in preparation for link interrupts.
6438 */
6439 spin_lock_irq(&phba->hbalock);
6440 phba->link_state = LPFC_LINK_DOWN;
6441 spin_unlock_irq(&phba->hbalock);
6442 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6443 (phba->hba_flag & LINK_DISABLED)) {
6444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6445 "3103 Adapter Link is disabled.\n");
6446 lpfc_down_link(phba, mboxq);
6447 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6448 if (rc != MBX_SUCCESS) {
6449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6450 "3104 Adapter failed to issue "
6451 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6452 goto out_unset_queue;
6453 }
6454 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6455 /* don't perform init_link on SLI4 FC port loopback test */
6456 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6457 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6458 if (rc)
6459 goto out_unset_queue;
6460 }
6461 }
6462 mempool_free(mboxq, phba->mbox_mem_pool);
6463 return rc;
6464 out_unset_queue:
6465 /* Unset all the queues set up in this routine when error out */
6466 lpfc_sli4_queue_unset(phba);
6467 out_destroy_queue:
6468 lpfc_sli4_queue_destroy(phba);
6469 out_stop_timers:
6470 lpfc_stop_hba_timers(phba);
6471 out_free_mbox:
6472 mempool_free(mboxq, phba->mbox_mem_pool);
6473 return rc;
6474 }
6475
6476 /**
6477 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6478 * @ptr: context object - pointer to hba structure.
6479 *
6480 * This is the callback function for mailbox timer. The mailbox
6481 * timer is armed when a new mailbox command is issued and the timer
6482 * is deleted when the mailbox complete. The function is called by
6483 * the kernel timer code when a mailbox does not complete within
6484 * expected time. This function wakes up the worker thread to
6485 * process the mailbox timeout and returns. All the processing is
6486 * done by the worker thread function lpfc_mbox_timeout_handler.
6487 **/
6488 void
6489 lpfc_mbox_timeout(unsigned long ptr)
6490 {
6491 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6492 unsigned long iflag;
6493 uint32_t tmo_posted;
6494
6495 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6496 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6497 if (!tmo_posted)
6498 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6499 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6500
6501 if (!tmo_posted)
6502 lpfc_worker_wake_up(phba);
6503 return;
6504 }
6505
6506
6507 /**
6508 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6509 * @phba: Pointer to HBA context object.
6510 *
6511 * This function is called from worker thread when a mailbox command times out.
6512 * The caller is not required to hold any locks. This function will reset the
6513 * HBA and recover all the pending commands.
6514 **/
6515 void
6516 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6517 {
6518 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6519 MAILBOX_t *mb = &pmbox->u.mb;
6520 struct lpfc_sli *psli = &phba->sli;
6521 struct lpfc_sli_ring *pring;
6522
6523 /* Check the pmbox pointer first. There is a race condition
6524 * between the mbox timeout handler getting executed in the
6525 * worklist and the mailbox actually completing. When this
6526 * race condition occurs, the mbox_active will be NULL.
6527 */
6528 spin_lock_irq(&phba->hbalock);
6529 if (pmbox == NULL) {
6530 lpfc_printf_log(phba, KERN_WARNING,
6531 LOG_MBOX | LOG_SLI,
6532 "0353 Active Mailbox cleared - mailbox timeout "
6533 "exiting\n");
6534 spin_unlock_irq(&phba->hbalock);
6535 return;
6536 }
6537
6538 /* Mbox cmd <mbxCommand> timeout */
6539 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6540 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6541 mb->mbxCommand,
6542 phba->pport->port_state,
6543 phba->sli.sli_flag,
6544 phba->sli.mbox_active);
6545 spin_unlock_irq(&phba->hbalock);
6546
6547 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6548 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6549 * it to fail all outstanding SCSI IO.
6550 */
6551 spin_lock_irq(&phba->pport->work_port_lock);
6552 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6553 spin_unlock_irq(&phba->pport->work_port_lock);
6554 spin_lock_irq(&phba->hbalock);
6555 phba->link_state = LPFC_LINK_UNKNOWN;
6556 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6557 spin_unlock_irq(&phba->hbalock);
6558
6559 pring = &psli->ring[psli->fcp_ring];
6560 lpfc_sli_abort_iocb_ring(phba, pring);
6561
6562 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6563 "0345 Resetting board due to mailbox timeout\n");
6564
6565 /* Reset the HBA device */
6566 lpfc_reset_hba(phba);
6567 }
6568
6569 /**
6570 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6571 * @phba: Pointer to HBA context object.
6572 * @pmbox: Pointer to mailbox object.
6573 * @flag: Flag indicating how the mailbox need to be processed.
6574 *
6575 * This function is called by discovery code and HBA management code
6576 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6577 * function gets the hbalock to protect the data structures.
6578 * The mailbox command can be submitted in polling mode, in which case
6579 * this function will wait in a polling loop for the completion of the
6580 * mailbox.
6581 * If the mailbox is submitted in no_wait mode (not polling) the
6582 * function will submit the command and returns immediately without waiting
6583 * for the mailbox completion. The no_wait is supported only when HBA
6584 * is in SLI2/SLI3 mode - interrupts are enabled.
6585 * The SLI interface allows only one mailbox pending at a time. If the
6586 * mailbox is issued in polling mode and there is already a mailbox
6587 * pending, then the function will return an error. If the mailbox is issued
6588 * in NO_WAIT mode and there is a mailbox pending already, the function
6589 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6590 * The sli layer owns the mailbox object until the completion of mailbox
6591 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6592 * return codes the caller owns the mailbox command after the return of
6593 * the function.
6594 **/
6595 static int
6596 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6597 uint32_t flag)
6598 {
6599 MAILBOX_t *mb;
6600 struct lpfc_sli *psli = &phba->sli;
6601 uint32_t status, evtctr;
6602 uint32_t ha_copy, hc_copy;
6603 int i;
6604 unsigned long timeout;
6605 unsigned long drvr_flag = 0;
6606 uint32_t word0, ldata;
6607 void __iomem *to_slim;
6608 int processing_queue = 0;
6609
6610 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6611 if (!pmbox) {
6612 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6613 /* processing mbox queue from intr_handler */
6614 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6615 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6616 return MBX_SUCCESS;
6617 }
6618 processing_queue = 1;
6619 pmbox = lpfc_mbox_get(phba);
6620 if (!pmbox) {
6621 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6622 return MBX_SUCCESS;
6623 }
6624 }
6625
6626 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6627 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6628 if(!pmbox->vport) {
6629 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6630 lpfc_printf_log(phba, KERN_ERR,
6631 LOG_MBOX | LOG_VPORT,
6632 "1806 Mbox x%x failed. No vport\n",
6633 pmbox->u.mb.mbxCommand);
6634 dump_stack();
6635 goto out_not_finished;
6636 }
6637 }
6638
6639 /* If the PCI channel is in offline state, do not post mbox. */
6640 if (unlikely(pci_channel_offline(phba->pcidev))) {
6641 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6642 goto out_not_finished;
6643 }
6644
6645 /* If HBA has a deferred error attention, fail the iocb. */
6646 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6647 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6648 goto out_not_finished;
6649 }
6650
6651 psli = &phba->sli;
6652
6653 mb = &pmbox->u.mb;
6654 status = MBX_SUCCESS;
6655
6656 if (phba->link_state == LPFC_HBA_ERROR) {
6657 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6658
6659 /* Mbox command <mbxCommand> cannot issue */
6660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6661 "(%d):0311 Mailbox command x%x cannot "
6662 "issue Data: x%x x%x\n",
6663 pmbox->vport ? pmbox->vport->vpi : 0,
6664 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6665 goto out_not_finished;
6666 }
6667
6668 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6669 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6670 !(hc_copy & HC_MBINT_ENA)) {
6671 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6673 "(%d):2528 Mailbox command x%x cannot "
6674 "issue Data: x%x x%x\n",
6675 pmbox->vport ? pmbox->vport->vpi : 0,
6676 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6677 goto out_not_finished;
6678 }
6679 }
6680
6681 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6682 /* Polling for a mbox command when another one is already active
6683 * is not allowed in SLI. Also, the driver must have established
6684 * SLI2 mode to queue and process multiple mbox commands.
6685 */
6686
6687 if (flag & MBX_POLL) {
6688 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6689
6690 /* Mbox command <mbxCommand> cannot issue */
6691 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6692 "(%d):2529 Mailbox command x%x "
6693 "cannot issue Data: x%x x%x\n",
6694 pmbox->vport ? pmbox->vport->vpi : 0,
6695 pmbox->u.mb.mbxCommand,
6696 psli->sli_flag, flag);
6697 goto out_not_finished;
6698 }
6699
6700 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6701 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6702 /* Mbox command <mbxCommand> cannot issue */
6703 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6704 "(%d):2530 Mailbox command x%x "
6705 "cannot issue Data: x%x x%x\n",
6706 pmbox->vport ? pmbox->vport->vpi : 0,
6707 pmbox->u.mb.mbxCommand,
6708 psli->sli_flag, flag);
6709 goto out_not_finished;
6710 }
6711
6712 /* Another mailbox command is still being processed, queue this
6713 * command to be processed later.
6714 */
6715 lpfc_mbox_put(phba, pmbox);
6716
6717 /* Mbox cmd issue - BUSY */
6718 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6719 "(%d):0308 Mbox cmd issue - BUSY Data: "
6720 "x%x x%x x%x x%x\n",
6721 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6722 mb->mbxCommand, phba->pport->port_state,
6723 psli->sli_flag, flag);
6724
6725 psli->slistat.mbox_busy++;
6726 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6727
6728 if (pmbox->vport) {
6729 lpfc_debugfs_disc_trc(pmbox->vport,
6730 LPFC_DISC_TRC_MBOX_VPORT,
6731 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6732 (uint32_t)mb->mbxCommand,
6733 mb->un.varWords[0], mb->un.varWords[1]);
6734 }
6735 else {
6736 lpfc_debugfs_disc_trc(phba->pport,
6737 LPFC_DISC_TRC_MBOX,
6738 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6739 (uint32_t)mb->mbxCommand,
6740 mb->un.varWords[0], mb->un.varWords[1]);
6741 }
6742
6743 return MBX_BUSY;
6744 }
6745
6746 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6747
6748 /* If we are not polling, we MUST be in SLI2 mode */
6749 if (flag != MBX_POLL) {
6750 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6751 (mb->mbxCommand != MBX_KILL_BOARD)) {
6752 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6753 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6754 /* Mbox command <mbxCommand> cannot issue */
6755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6756 "(%d):2531 Mailbox command x%x "
6757 "cannot issue Data: x%x x%x\n",
6758 pmbox->vport ? pmbox->vport->vpi : 0,
6759 pmbox->u.mb.mbxCommand,
6760 psli->sli_flag, flag);
6761 goto out_not_finished;
6762 }
6763 /* timeout active mbox command */
6764 mod_timer(&psli->mbox_tmo, (jiffies +
6765 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
6766 }
6767
6768 /* Mailbox cmd <cmd> issue */
6769 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6770 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6771 "x%x\n",
6772 pmbox->vport ? pmbox->vport->vpi : 0,
6773 mb->mbxCommand, phba->pport->port_state,
6774 psli->sli_flag, flag);
6775
6776 if (mb->mbxCommand != MBX_HEARTBEAT) {
6777 if (pmbox->vport) {
6778 lpfc_debugfs_disc_trc(pmbox->vport,
6779 LPFC_DISC_TRC_MBOX_VPORT,
6780 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6781 (uint32_t)mb->mbxCommand,
6782 mb->un.varWords[0], mb->un.varWords[1]);
6783 }
6784 else {
6785 lpfc_debugfs_disc_trc(phba->pport,
6786 LPFC_DISC_TRC_MBOX,
6787 "MBOX Send: cmd:x%x mb:x%x x%x",
6788 (uint32_t)mb->mbxCommand,
6789 mb->un.varWords[0], mb->un.varWords[1]);
6790 }
6791 }
6792
6793 psli->slistat.mbox_cmd++;
6794 evtctr = psli->slistat.mbox_event;
6795
6796 /* next set own bit for the adapter and copy over command word */
6797 mb->mbxOwner = OWN_CHIP;
6798
6799 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6800 /* Populate mbox extension offset word. */
6801 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6802 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6803 = (uint8_t *)phba->mbox_ext
6804 - (uint8_t *)phba->mbox;
6805 }
6806
6807 /* Copy the mailbox extension data */
6808 if (pmbox->in_ext_byte_len && pmbox->context2) {
6809 lpfc_sli_pcimem_bcopy(pmbox->context2,
6810 (uint8_t *)phba->mbox_ext,
6811 pmbox->in_ext_byte_len);
6812 }
6813 /* Copy command data to host SLIM area */
6814 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6815 } else {
6816 /* Populate mbox extension offset word. */
6817 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6818 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6819 = MAILBOX_HBA_EXT_OFFSET;
6820
6821 /* Copy the mailbox extension data */
6822 if (pmbox->in_ext_byte_len && pmbox->context2) {
6823 lpfc_memcpy_to_slim(phba->MBslimaddr +
6824 MAILBOX_HBA_EXT_OFFSET,
6825 pmbox->context2, pmbox->in_ext_byte_len);
6826
6827 }
6828 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6829 /* copy command data into host mbox for cmpl */
6830 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6831 }
6832
6833 /* First copy mbox command data to HBA SLIM, skip past first
6834 word */
6835 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6836 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6837 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6838
6839 /* Next copy over first word, with mbxOwner set */
6840 ldata = *((uint32_t *)mb);
6841 to_slim = phba->MBslimaddr;
6842 writel(ldata, to_slim);
6843 readl(to_slim); /* flush */
6844
6845 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6846 /* switch over to host mailbox */
6847 psli->sli_flag |= LPFC_SLI_ACTIVE;
6848 }
6849 }
6850
6851 wmb();
6852
6853 switch (flag) {
6854 case MBX_NOWAIT:
6855 /* Set up reference to mailbox command */
6856 psli->mbox_active = pmbox;
6857 /* Interrupt board to do it */
6858 writel(CA_MBATT, phba->CAregaddr);
6859 readl(phba->CAregaddr); /* flush */
6860 /* Don't wait for it to finish, just return */
6861 break;
6862
6863 case MBX_POLL:
6864 /* Set up null reference to mailbox command */
6865 psli->mbox_active = NULL;
6866 /* Interrupt board to do it */
6867 writel(CA_MBATT, phba->CAregaddr);
6868 readl(phba->CAregaddr); /* flush */
6869
6870 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6871 /* First read mbox status word */
6872 word0 = *((uint32_t *)phba->mbox);
6873 word0 = le32_to_cpu(word0);
6874 } else {
6875 /* First read mbox status word */
6876 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6877 spin_unlock_irqrestore(&phba->hbalock,
6878 drvr_flag);
6879 goto out_not_finished;
6880 }
6881 }
6882
6883 /* Read the HBA Host Attention Register */
6884 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6885 spin_unlock_irqrestore(&phba->hbalock,
6886 drvr_flag);
6887 goto out_not_finished;
6888 }
6889 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6890 1000) + jiffies;
6891 i = 0;
6892 /* Wait for command to complete */
6893 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6894 (!(ha_copy & HA_MBATT) &&
6895 (phba->link_state > LPFC_WARM_START))) {
6896 if (time_after(jiffies, timeout)) {
6897 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6898 spin_unlock_irqrestore(&phba->hbalock,
6899 drvr_flag);
6900 goto out_not_finished;
6901 }
6902
6903 /* Check if we took a mbox interrupt while we were
6904 polling */
6905 if (((word0 & OWN_CHIP) != OWN_CHIP)
6906 && (evtctr != psli->slistat.mbox_event))
6907 break;
6908
6909 if (i++ > 10) {
6910 spin_unlock_irqrestore(&phba->hbalock,
6911 drvr_flag);
6912 msleep(1);
6913 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6914 }
6915
6916 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6917 /* First copy command data */
6918 word0 = *((uint32_t *)phba->mbox);
6919 word0 = le32_to_cpu(word0);
6920 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6921 MAILBOX_t *slimmb;
6922 uint32_t slimword0;
6923 /* Check real SLIM for any errors */
6924 slimword0 = readl(phba->MBslimaddr);
6925 slimmb = (MAILBOX_t *) & slimword0;
6926 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6927 && slimmb->mbxStatus) {
6928 psli->sli_flag &=
6929 ~LPFC_SLI_ACTIVE;
6930 word0 = slimword0;
6931 }
6932 }
6933 } else {
6934 /* First copy command data */
6935 word0 = readl(phba->MBslimaddr);
6936 }
6937 /* Read the HBA Host Attention Register */
6938 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6939 spin_unlock_irqrestore(&phba->hbalock,
6940 drvr_flag);
6941 goto out_not_finished;
6942 }
6943 }
6944
6945 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6946 /* copy results back to user */
6947 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
6948 /* Copy the mailbox extension data */
6949 if (pmbox->out_ext_byte_len && pmbox->context2) {
6950 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6951 pmbox->context2,
6952 pmbox->out_ext_byte_len);
6953 }
6954 } else {
6955 /* First copy command data */
6956 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6957 MAILBOX_CMD_SIZE);
6958 /* Copy the mailbox extension data */
6959 if (pmbox->out_ext_byte_len && pmbox->context2) {
6960 lpfc_memcpy_from_slim(pmbox->context2,
6961 phba->MBslimaddr +
6962 MAILBOX_HBA_EXT_OFFSET,
6963 pmbox->out_ext_byte_len);
6964 }
6965 }
6966
6967 writel(HA_MBATT, phba->HAregaddr);
6968 readl(phba->HAregaddr); /* flush */
6969
6970 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6971 status = mb->mbxStatus;
6972 }
6973
6974 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6975 return status;
6976
6977 out_not_finished:
6978 if (processing_queue) {
6979 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
6980 lpfc_mbox_cmpl_put(phba, pmbox);
6981 }
6982 return MBX_NOT_FINISHED;
6983 }
6984
6985 /**
6986 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6987 * @phba: Pointer to HBA context object.
6988 *
6989 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6990 * the driver internal pending mailbox queue. It will then try to wait out the
6991 * possible outstanding mailbox command before return.
6992 *
6993 * Returns:
6994 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6995 * the outstanding mailbox command timed out.
6996 **/
6997 static int
6998 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6999 {
7000 struct lpfc_sli *psli = &phba->sli;
7001 int rc = 0;
7002 unsigned long timeout = 0;
7003
7004 /* Mark the asynchronous mailbox command posting as blocked */
7005 spin_lock_irq(&phba->hbalock);
7006 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7007 /* Determine how long we might wait for the active mailbox
7008 * command to be gracefully completed by firmware.
7009 */
7010 if (phba->sli.mbox_active)
7011 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7012 phba->sli.mbox_active) *
7013 1000) + jiffies;
7014 spin_unlock_irq(&phba->hbalock);
7015
7016 /* Wait for the outstnading mailbox command to complete */
7017 while (phba->sli.mbox_active) {
7018 /* Check active mailbox complete status every 2ms */
7019 msleep(2);
7020 if (time_after(jiffies, timeout)) {
7021 /* Timeout, marked the outstanding cmd not complete */
7022 rc = 1;
7023 break;
7024 }
7025 }
7026
7027 /* Can not cleanly block async mailbox command, fails it */
7028 if (rc) {
7029 spin_lock_irq(&phba->hbalock);
7030 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7031 spin_unlock_irq(&phba->hbalock);
7032 }
7033 return rc;
7034 }
7035
7036 /**
7037 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7038 * @phba: Pointer to HBA context object.
7039 *
7040 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7041 * commands from the driver internal pending mailbox queue. It makes sure
7042 * that there is no outstanding mailbox command before resuming posting
7043 * asynchronous mailbox commands. If, for any reason, there is outstanding
7044 * mailbox command, it will try to wait it out before resuming asynchronous
7045 * mailbox command posting.
7046 **/
7047 static void
7048 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7049 {
7050 struct lpfc_sli *psli = &phba->sli;
7051
7052 spin_lock_irq(&phba->hbalock);
7053 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7054 /* Asynchronous mailbox posting is not blocked, do nothing */
7055 spin_unlock_irq(&phba->hbalock);
7056 return;
7057 }
7058
7059 /* Outstanding synchronous mailbox command is guaranteed to be done,
7060 * successful or timeout, after timing-out the outstanding mailbox
7061 * command shall always be removed, so just unblock posting async
7062 * mailbox command and resume
7063 */
7064 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7065 spin_unlock_irq(&phba->hbalock);
7066
7067 /* wake up worker thread to post asynchronlous mailbox command */
7068 lpfc_worker_wake_up(phba);
7069 }
7070
7071 /**
7072 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7073 * @phba: Pointer to HBA context object.
7074 * @mboxq: Pointer to mailbox object.
7075 *
7076 * The function posts a mailbox to the port. The mailbox is expected
7077 * to be comletely filled in and ready for the port to operate on it.
7078 * This routine executes a synchronous completion operation on the
7079 * mailbox by polling for its completion.
7080 *
7081 * The caller must not be holding any locks when calling this routine.
7082 *
7083 * Returns:
7084 * MBX_SUCCESS - mailbox posted successfully
7085 * Any of the MBX error values.
7086 **/
7087 static int
7088 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7089 {
7090 int rc = MBX_SUCCESS;
7091 unsigned long iflag;
7092 uint32_t db_ready;
7093 uint32_t mcqe_status;
7094 uint32_t mbx_cmnd;
7095 unsigned long timeout;
7096 struct lpfc_sli *psli = &phba->sli;
7097 struct lpfc_mqe *mb = &mboxq->u.mqe;
7098 struct lpfc_bmbx_create *mbox_rgn;
7099 struct dma_address *dma_address;
7100 struct lpfc_register bmbx_reg;
7101
7102 /*
7103 * Only one mailbox can be active to the bootstrap mailbox region
7104 * at a time and there is no queueing provided.
7105 */
7106 spin_lock_irqsave(&phba->hbalock, iflag);
7107 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7108 spin_unlock_irqrestore(&phba->hbalock, iflag);
7109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7110 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7111 "cannot issue Data: x%x x%x\n",
7112 mboxq->vport ? mboxq->vport->vpi : 0,
7113 mboxq->u.mb.mbxCommand,
7114 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7115 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7116 psli->sli_flag, MBX_POLL);
7117 return MBXERR_ERROR;
7118 }
7119 /* The server grabs the token and owns it until release */
7120 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7121 phba->sli.mbox_active = mboxq;
7122 spin_unlock_irqrestore(&phba->hbalock, iflag);
7123
7124 /*
7125 * Initialize the bootstrap memory region to avoid stale data areas
7126 * in the mailbox post. Then copy the caller's mailbox contents to
7127 * the bmbx mailbox region.
7128 */
7129 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7130 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7131 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7132 sizeof(struct lpfc_mqe));
7133
7134 /* Post the high mailbox dma address to the port and wait for ready. */
7135 dma_address = &phba->sli4_hba.bmbx.dma_address;
7136 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7137
7138 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7139 * 1000) + jiffies;
7140 do {
7141 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7142 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7143 if (!db_ready)
7144 msleep(2);
7145
7146 if (time_after(jiffies, timeout)) {
7147 rc = MBXERR_ERROR;
7148 goto exit;
7149 }
7150 } while (!db_ready);
7151
7152 /* Post the low mailbox dma address to the port. */
7153 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7154 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7155 * 1000) + jiffies;
7156 do {
7157 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7158 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7159 if (!db_ready)
7160 msleep(2);
7161
7162 if (time_after(jiffies, timeout)) {
7163 rc = MBXERR_ERROR;
7164 goto exit;
7165 }
7166 } while (!db_ready);
7167
7168 /*
7169 * Read the CQ to ensure the mailbox has completed.
7170 * If so, update the mailbox status so that the upper layers
7171 * can complete the request normally.
7172 */
7173 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7174 sizeof(struct lpfc_mqe));
7175 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7176 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7177 sizeof(struct lpfc_mcqe));
7178 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7179 /*
7180 * When the CQE status indicates a failure and the mailbox status
7181 * indicates success then copy the CQE status into the mailbox status
7182 * (and prefix it with x4000).
7183 */
7184 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7185 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7186 bf_set(lpfc_mqe_status, mb,
7187 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7188 rc = MBXERR_ERROR;
7189 } else
7190 lpfc_sli4_swap_str(phba, mboxq);
7191
7192 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7193 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7194 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7195 " x%x x%x CQ: x%x x%x x%x x%x\n",
7196 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7197 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7198 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7199 bf_get(lpfc_mqe_status, mb),
7200 mb->un.mb_words[0], mb->un.mb_words[1],
7201 mb->un.mb_words[2], mb->un.mb_words[3],
7202 mb->un.mb_words[4], mb->un.mb_words[5],
7203 mb->un.mb_words[6], mb->un.mb_words[7],
7204 mb->un.mb_words[8], mb->un.mb_words[9],
7205 mb->un.mb_words[10], mb->un.mb_words[11],
7206 mb->un.mb_words[12], mboxq->mcqe.word0,
7207 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7208 mboxq->mcqe.trailer);
7209 exit:
7210 /* We are holding the token, no needed for lock when release */
7211 spin_lock_irqsave(&phba->hbalock, iflag);
7212 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7213 phba->sli.mbox_active = NULL;
7214 spin_unlock_irqrestore(&phba->hbalock, iflag);
7215 return rc;
7216 }
7217
7218 /**
7219 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7220 * @phba: Pointer to HBA context object.
7221 * @pmbox: Pointer to mailbox object.
7222 * @flag: Flag indicating how the mailbox need to be processed.
7223 *
7224 * This function is called by discovery code and HBA management code to submit
7225 * a mailbox command to firmware with SLI-4 interface spec.
7226 *
7227 * Return codes the caller owns the mailbox command after the return of the
7228 * function.
7229 **/
7230 static int
7231 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7232 uint32_t flag)
7233 {
7234 struct lpfc_sli *psli = &phba->sli;
7235 unsigned long iflags;
7236 int rc;
7237
7238 /* dump from issue mailbox command if setup */
7239 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7240
7241 rc = lpfc_mbox_dev_check(phba);
7242 if (unlikely(rc)) {
7243 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7244 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7245 "cannot issue Data: x%x x%x\n",
7246 mboxq->vport ? mboxq->vport->vpi : 0,
7247 mboxq->u.mb.mbxCommand,
7248 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7249 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7250 psli->sli_flag, flag);
7251 goto out_not_finished;
7252 }
7253
7254 /* Detect polling mode and jump to a handler */
7255 if (!phba->sli4_hba.intr_enable) {
7256 if (flag == MBX_POLL)
7257 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7258 else
7259 rc = -EIO;
7260 if (rc != MBX_SUCCESS)
7261 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7262 "(%d):2541 Mailbox command x%x "
7263 "(x%x/x%x) failure: "
7264 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7265 "Data: x%x x%x\n,",
7266 mboxq->vport ? mboxq->vport->vpi : 0,
7267 mboxq->u.mb.mbxCommand,
7268 lpfc_sli_config_mbox_subsys_get(phba,
7269 mboxq),
7270 lpfc_sli_config_mbox_opcode_get(phba,
7271 mboxq),
7272 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7273 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7274 bf_get(lpfc_mcqe_ext_status,
7275 &mboxq->mcqe),
7276 psli->sli_flag, flag);
7277 return rc;
7278 } else if (flag == MBX_POLL) {
7279 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7280 "(%d):2542 Try to issue mailbox command "
7281 "x%x (x%x/x%x) synchronously ahead of async"
7282 "mailbox command queue: x%x x%x\n",
7283 mboxq->vport ? mboxq->vport->vpi : 0,
7284 mboxq->u.mb.mbxCommand,
7285 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7286 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7287 psli->sli_flag, flag);
7288 /* Try to block the asynchronous mailbox posting */
7289 rc = lpfc_sli4_async_mbox_block(phba);
7290 if (!rc) {
7291 /* Successfully blocked, now issue sync mbox cmd */
7292 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7293 if (rc != MBX_SUCCESS)
7294 lpfc_printf_log(phba, KERN_WARNING,
7295 LOG_MBOX | LOG_SLI,
7296 "(%d):2597 Sync Mailbox command "
7297 "x%x (x%x/x%x) failure: "
7298 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7299 "Data: x%x x%x\n,",
7300 mboxq->vport ? mboxq->vport->vpi : 0,
7301 mboxq->u.mb.mbxCommand,
7302 lpfc_sli_config_mbox_subsys_get(phba,
7303 mboxq),
7304 lpfc_sli_config_mbox_opcode_get(phba,
7305 mboxq),
7306 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7307 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7308 bf_get(lpfc_mcqe_ext_status,
7309 &mboxq->mcqe),
7310 psli->sli_flag, flag);
7311 /* Unblock the async mailbox posting afterward */
7312 lpfc_sli4_async_mbox_unblock(phba);
7313 }
7314 return rc;
7315 }
7316
7317 /* Now, interrupt mode asynchrous mailbox command */
7318 rc = lpfc_mbox_cmd_check(phba, mboxq);
7319 if (rc) {
7320 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7321 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7322 "cannot issue Data: x%x x%x\n",
7323 mboxq->vport ? mboxq->vport->vpi : 0,
7324 mboxq->u.mb.mbxCommand,
7325 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7326 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7327 psli->sli_flag, flag);
7328 goto out_not_finished;
7329 }
7330
7331 /* Put the mailbox command to the driver internal FIFO */
7332 psli->slistat.mbox_busy++;
7333 spin_lock_irqsave(&phba->hbalock, iflags);
7334 lpfc_mbox_put(phba, mboxq);
7335 spin_unlock_irqrestore(&phba->hbalock, iflags);
7336 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7337 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7338 "x%x (x%x/x%x) x%x x%x x%x\n",
7339 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7340 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7341 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7342 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7343 phba->pport->port_state,
7344 psli->sli_flag, MBX_NOWAIT);
7345 /* Wake up worker thread to transport mailbox command from head */
7346 lpfc_worker_wake_up(phba);
7347
7348 return MBX_BUSY;
7349
7350 out_not_finished:
7351 return MBX_NOT_FINISHED;
7352 }
7353
7354 /**
7355 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7356 * @phba: Pointer to HBA context object.
7357 *
7358 * This function is called by worker thread to send a mailbox command to
7359 * SLI4 HBA firmware.
7360 *
7361 **/
7362 int
7363 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7364 {
7365 struct lpfc_sli *psli = &phba->sli;
7366 LPFC_MBOXQ_t *mboxq;
7367 int rc = MBX_SUCCESS;
7368 unsigned long iflags;
7369 struct lpfc_mqe *mqe;
7370 uint32_t mbx_cmnd;
7371
7372 /* Check interrupt mode before post async mailbox command */
7373 if (unlikely(!phba->sli4_hba.intr_enable))
7374 return MBX_NOT_FINISHED;
7375
7376 /* Check for mailbox command service token */
7377 spin_lock_irqsave(&phba->hbalock, iflags);
7378 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7379 spin_unlock_irqrestore(&phba->hbalock, iflags);
7380 return MBX_NOT_FINISHED;
7381 }
7382 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7383 spin_unlock_irqrestore(&phba->hbalock, iflags);
7384 return MBX_NOT_FINISHED;
7385 }
7386 if (unlikely(phba->sli.mbox_active)) {
7387 spin_unlock_irqrestore(&phba->hbalock, iflags);
7388 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7389 "0384 There is pending active mailbox cmd\n");
7390 return MBX_NOT_FINISHED;
7391 }
7392 /* Take the mailbox command service token */
7393 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7394
7395 /* Get the next mailbox command from head of queue */
7396 mboxq = lpfc_mbox_get(phba);
7397
7398 /* If no more mailbox command waiting for post, we're done */
7399 if (!mboxq) {
7400 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7401 spin_unlock_irqrestore(&phba->hbalock, iflags);
7402 return MBX_SUCCESS;
7403 }
7404 phba->sli.mbox_active = mboxq;
7405 spin_unlock_irqrestore(&phba->hbalock, iflags);
7406
7407 /* Check device readiness for posting mailbox command */
7408 rc = lpfc_mbox_dev_check(phba);
7409 if (unlikely(rc))
7410 /* Driver clean routine will clean up pending mailbox */
7411 goto out_not_finished;
7412
7413 /* Prepare the mbox command to be posted */
7414 mqe = &mboxq->u.mqe;
7415 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7416
7417 /* Start timer for the mbox_tmo and log some mailbox post messages */
7418 mod_timer(&psli->mbox_tmo, (jiffies +
7419 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
7420
7421 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7422 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7423 "x%x x%x\n",
7424 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7425 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7426 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7427 phba->pport->port_state, psli->sli_flag);
7428
7429 if (mbx_cmnd != MBX_HEARTBEAT) {
7430 if (mboxq->vport) {
7431 lpfc_debugfs_disc_trc(mboxq->vport,
7432 LPFC_DISC_TRC_MBOX_VPORT,
7433 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7434 mbx_cmnd, mqe->un.mb_words[0],
7435 mqe->un.mb_words[1]);
7436 } else {
7437 lpfc_debugfs_disc_trc(phba->pport,
7438 LPFC_DISC_TRC_MBOX,
7439 "MBOX Send: cmd:x%x mb:x%x x%x",
7440 mbx_cmnd, mqe->un.mb_words[0],
7441 mqe->un.mb_words[1]);
7442 }
7443 }
7444 psli->slistat.mbox_cmd++;
7445
7446 /* Post the mailbox command to the port */
7447 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7448 if (rc != MBX_SUCCESS) {
7449 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7450 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7451 "cannot issue Data: x%x x%x\n",
7452 mboxq->vport ? mboxq->vport->vpi : 0,
7453 mboxq->u.mb.mbxCommand,
7454 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7455 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7456 psli->sli_flag, MBX_NOWAIT);
7457 goto out_not_finished;
7458 }
7459
7460 return rc;
7461
7462 out_not_finished:
7463 spin_lock_irqsave(&phba->hbalock, iflags);
7464 if (phba->sli.mbox_active) {
7465 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7466 __lpfc_mbox_cmpl_put(phba, mboxq);
7467 /* Release the token */
7468 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7469 phba->sli.mbox_active = NULL;
7470 }
7471 spin_unlock_irqrestore(&phba->hbalock, iflags);
7472
7473 return MBX_NOT_FINISHED;
7474 }
7475
7476 /**
7477 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7478 * @phba: Pointer to HBA context object.
7479 * @pmbox: Pointer to mailbox object.
7480 * @flag: Flag indicating how the mailbox need to be processed.
7481 *
7482 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7483 * the API jump table function pointer from the lpfc_hba struct.
7484 *
7485 * Return codes the caller owns the mailbox command after the return of the
7486 * function.
7487 **/
7488 int
7489 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7490 {
7491 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7492 }
7493
7494 /**
7495 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7496 * @phba: The hba struct for which this call is being executed.
7497 * @dev_grp: The HBA PCI-Device group number.
7498 *
7499 * This routine sets up the mbox interface API function jump table in @phba
7500 * struct.
7501 * Returns: 0 - success, -ENODEV - failure.
7502 **/
7503 int
7504 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7505 {
7506
7507 switch (dev_grp) {
7508 case LPFC_PCI_DEV_LP:
7509 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7510 phba->lpfc_sli_handle_slow_ring_event =
7511 lpfc_sli_handle_slow_ring_event_s3;
7512 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7513 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7514 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7515 break;
7516 case LPFC_PCI_DEV_OC:
7517 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7518 phba->lpfc_sli_handle_slow_ring_event =
7519 lpfc_sli_handle_slow_ring_event_s4;
7520 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7521 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7522 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7523 break;
7524 default:
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7526 "1420 Invalid HBA PCI-device group: 0x%x\n",
7527 dev_grp);
7528 return -ENODEV;
7529 break;
7530 }
7531 return 0;
7532 }
7533
7534 /**
7535 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7536 * @phba: Pointer to HBA context object.
7537 * @pring: Pointer to driver SLI ring object.
7538 * @piocb: Pointer to address of newly added command iocb.
7539 *
7540 * This function is called with hbalock held to add a command
7541 * iocb to the txq when SLI layer cannot submit the command iocb
7542 * to the ring.
7543 **/
7544 void
7545 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7546 struct lpfc_iocbq *piocb)
7547 {
7548 /* Insert the caller's iocb in the txq tail for later processing. */
7549 list_add_tail(&piocb->list, &pring->txq);
7550 pring->txq_cnt++;
7551 }
7552
7553 /**
7554 * lpfc_sli_next_iocb - Get the next iocb in the txq
7555 * @phba: Pointer to HBA context object.
7556 * @pring: Pointer to driver SLI ring object.
7557 * @piocb: Pointer to address of newly added command iocb.
7558 *
7559 * This function is called with hbalock held before a new
7560 * iocb is submitted to the firmware. This function checks
7561 * txq to flush the iocbs in txq to Firmware before
7562 * submitting new iocbs to the Firmware.
7563 * If there are iocbs in the txq which need to be submitted
7564 * to firmware, lpfc_sli_next_iocb returns the first element
7565 * of the txq after dequeuing it from txq.
7566 * If there is no iocb in the txq then the function will return
7567 * *piocb and *piocb is set to NULL. Caller needs to check
7568 * *piocb to find if there are more commands in the txq.
7569 **/
7570 static struct lpfc_iocbq *
7571 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7572 struct lpfc_iocbq **piocb)
7573 {
7574 struct lpfc_iocbq * nextiocb;
7575
7576 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7577 if (!nextiocb) {
7578 nextiocb = *piocb;
7579 *piocb = NULL;
7580 }
7581
7582 return nextiocb;
7583 }
7584
7585 /**
7586 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7587 * @phba: Pointer to HBA context object.
7588 * @ring_number: SLI ring number to issue iocb on.
7589 * @piocb: Pointer to command iocb.
7590 * @flag: Flag indicating if this command can be put into txq.
7591 *
7592 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7593 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7594 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7595 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7596 * this function allows only iocbs for posting buffers. This function finds
7597 * next available slot in the command ring and posts the command to the
7598 * available slot and writes the port attention register to request HBA start
7599 * processing new iocb. If there is no slot available in the ring and
7600 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7601 * the function returns IOCB_BUSY.
7602 *
7603 * This function is called with hbalock held. The function will return success
7604 * after it successfully submit the iocb to firmware or after adding to the
7605 * txq.
7606 **/
7607 static int
7608 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7609 struct lpfc_iocbq *piocb, uint32_t flag)
7610 {
7611 struct lpfc_iocbq *nextiocb;
7612 IOCB_t *iocb;
7613 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7614
7615 if (piocb->iocb_cmpl && (!piocb->vport) &&
7616 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7617 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7618 lpfc_printf_log(phba, KERN_ERR,
7619 LOG_SLI | LOG_VPORT,
7620 "1807 IOCB x%x failed. No vport\n",
7621 piocb->iocb.ulpCommand);
7622 dump_stack();
7623 return IOCB_ERROR;
7624 }
7625
7626
7627 /* If the PCI channel is in offline state, do not post iocbs. */
7628 if (unlikely(pci_channel_offline(phba->pcidev)))
7629 return IOCB_ERROR;
7630
7631 /* If HBA has a deferred error attention, fail the iocb. */
7632 if (unlikely(phba->hba_flag & DEFER_ERATT))
7633 return IOCB_ERROR;
7634
7635 /*
7636 * We should never get an IOCB if we are in a < LINK_DOWN state
7637 */
7638 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7639 return IOCB_ERROR;
7640
7641 /*
7642 * Check to see if we are blocking IOCB processing because of a
7643 * outstanding event.
7644 */
7645 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7646 goto iocb_busy;
7647
7648 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7649 /*
7650 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7651 * can be issued if the link is not up.
7652 */
7653 switch (piocb->iocb.ulpCommand) {
7654 case CMD_GEN_REQUEST64_CR:
7655 case CMD_GEN_REQUEST64_CX:
7656 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7657 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7658 FC_RCTL_DD_UNSOL_CMD) ||
7659 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7660 MENLO_TRANSPORT_TYPE))
7661
7662 goto iocb_busy;
7663 break;
7664 case CMD_QUE_RING_BUF_CN:
7665 case CMD_QUE_RING_BUF64_CN:
7666 /*
7667 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7668 * completion, iocb_cmpl MUST be 0.
7669 */
7670 if (piocb->iocb_cmpl)
7671 piocb->iocb_cmpl = NULL;
7672 /*FALLTHROUGH*/
7673 case CMD_CREATE_XRI_CR:
7674 case CMD_CLOSE_XRI_CN:
7675 case CMD_CLOSE_XRI_CX:
7676 break;
7677 default:
7678 goto iocb_busy;
7679 }
7680
7681 /*
7682 * For FCP commands, we must be in a state where we can process link
7683 * attention events.
7684 */
7685 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7686 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7687 goto iocb_busy;
7688 }
7689
7690 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7691 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7692 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7693
7694 if (iocb)
7695 lpfc_sli_update_ring(phba, pring);
7696 else
7697 lpfc_sli_update_full_ring(phba, pring);
7698
7699 if (!piocb)
7700 return IOCB_SUCCESS;
7701
7702 goto out_busy;
7703
7704 iocb_busy:
7705 pring->stats.iocb_cmd_delay++;
7706
7707 out_busy:
7708
7709 if (!(flag & SLI_IOCB_RET_IOCB)) {
7710 __lpfc_sli_ringtx_put(phba, pring, piocb);
7711 return IOCB_SUCCESS;
7712 }
7713
7714 return IOCB_BUSY;
7715 }
7716
7717 /**
7718 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7719 * @phba: Pointer to HBA context object.
7720 * @piocb: Pointer to command iocb.
7721 * @sglq: Pointer to the scatter gather queue object.
7722 *
7723 * This routine converts the bpl or bde that is in the IOCB
7724 * to a sgl list for the sli4 hardware. The physical address
7725 * of the bpl/bde is converted back to a virtual address.
7726 * If the IOCB contains a BPL then the list of BDE's is
7727 * converted to sli4_sge's. If the IOCB contains a single
7728 * BDE then it is converted to a single sli_sge.
7729 * The IOCB is still in cpu endianess so the contents of
7730 * the bpl can be used without byte swapping.
7731 *
7732 * Returns valid XRI = Success, NO_XRI = Failure.
7733 **/
7734 static uint16_t
7735 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7736 struct lpfc_sglq *sglq)
7737 {
7738 uint16_t xritag = NO_XRI;
7739 struct ulp_bde64 *bpl = NULL;
7740 struct ulp_bde64 bde;
7741 struct sli4_sge *sgl = NULL;
7742 struct lpfc_dmabuf *dmabuf;
7743 IOCB_t *icmd;
7744 int numBdes = 0;
7745 int i = 0;
7746 uint32_t offset = 0; /* accumulated offset in the sg request list */
7747 int inbound = 0; /* number of sg reply entries inbound from firmware */
7748
7749 if (!piocbq || !sglq)
7750 return xritag;
7751
7752 sgl = (struct sli4_sge *)sglq->sgl;
7753 icmd = &piocbq->iocb;
7754 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7755 return sglq->sli4_xritag;
7756 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7757 numBdes = icmd->un.genreq64.bdl.bdeSize /
7758 sizeof(struct ulp_bde64);
7759 /* The addrHigh and addrLow fields within the IOCB
7760 * have not been byteswapped yet so there is no
7761 * need to swap them back.
7762 */
7763 if (piocbq->context3)
7764 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7765 else
7766 return xritag;
7767
7768 bpl = (struct ulp_bde64 *)dmabuf->virt;
7769 if (!bpl)
7770 return xritag;
7771
7772 for (i = 0; i < numBdes; i++) {
7773 /* Should already be byte swapped. */
7774 sgl->addr_hi = bpl->addrHigh;
7775 sgl->addr_lo = bpl->addrLow;
7776
7777 sgl->word2 = le32_to_cpu(sgl->word2);
7778 if ((i+1) == numBdes)
7779 bf_set(lpfc_sli4_sge_last, sgl, 1);
7780 else
7781 bf_set(lpfc_sli4_sge_last, sgl, 0);
7782 /* swap the size field back to the cpu so we
7783 * can assign it to the sgl.
7784 */
7785 bde.tus.w = le32_to_cpu(bpl->tus.w);
7786 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7787 /* The offsets in the sgl need to be accumulated
7788 * separately for the request and reply lists.
7789 * The request is always first, the reply follows.
7790 */
7791 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7792 /* add up the reply sg entries */
7793 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7794 inbound++;
7795 /* first inbound? reset the offset */
7796 if (inbound == 1)
7797 offset = 0;
7798 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7799 bf_set(lpfc_sli4_sge_type, sgl,
7800 LPFC_SGE_TYPE_DATA);
7801 offset += bde.tus.f.bdeSize;
7802 }
7803 sgl->word2 = cpu_to_le32(sgl->word2);
7804 bpl++;
7805 sgl++;
7806 }
7807 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7808 /* The addrHigh and addrLow fields of the BDE have not
7809 * been byteswapped yet so they need to be swapped
7810 * before putting them in the sgl.
7811 */
7812 sgl->addr_hi =
7813 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7814 sgl->addr_lo =
7815 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7816 sgl->word2 = le32_to_cpu(sgl->word2);
7817 bf_set(lpfc_sli4_sge_last, sgl, 1);
7818 sgl->word2 = cpu_to_le32(sgl->word2);
7819 sgl->sge_len =
7820 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7821 }
7822 return sglq->sli4_xritag;
7823 }
7824
7825 /**
7826 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7827 * @phba: Pointer to HBA context object.
7828 *
7829 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7830 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7831 * held.
7832 *
7833 * Return: index into SLI4 fast-path FCP queue index.
7834 **/
7835 static inline uint32_t
7836 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7837 {
7838 int i;
7839
7840 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7841 i = smp_processor_id();
7842 else
7843 i = atomic_add_return(1, &phba->fcp_qidx);
7844
7845 i = (i % phba->cfg_fcp_io_channel);
7846 return i;
7847 }
7848
7849 /**
7850 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7851 * @phba: Pointer to HBA context object.
7852 * @piocb: Pointer to command iocb.
7853 * @wqe: Pointer to the work queue entry.
7854 *
7855 * This routine converts the iocb command to its Work Queue Entry
7856 * equivalent. The wqe pointer should not have any fields set when
7857 * this routine is called because it will memcpy over them.
7858 * This routine does not set the CQ_ID or the WQEC bits in the
7859 * wqe.
7860 *
7861 * Returns: 0 = Success, IOCB_ERROR = Failure.
7862 **/
7863 static int
7864 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7865 union lpfc_wqe *wqe)
7866 {
7867 uint32_t xmit_len = 0, total_len = 0;
7868 uint8_t ct = 0;
7869 uint32_t fip;
7870 uint32_t abort_tag;
7871 uint8_t command_type = ELS_COMMAND_NON_FIP;
7872 uint8_t cmnd;
7873 uint16_t xritag;
7874 uint16_t abrt_iotag;
7875 struct lpfc_iocbq *abrtiocbq;
7876 struct ulp_bde64 *bpl = NULL;
7877 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7878 int numBdes, i;
7879 struct ulp_bde64 bde;
7880 struct lpfc_nodelist *ndlp;
7881 uint32_t *pcmd;
7882 uint32_t if_type;
7883
7884 fip = phba->hba_flag & HBA_FIP_SUPPORT;
7885 /* The fcp commands will set command type */
7886 if (iocbq->iocb_flag & LPFC_IO_FCP)
7887 command_type = FCP_COMMAND;
7888 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7889 command_type = ELS_COMMAND_FIP;
7890 else
7891 command_type = ELS_COMMAND_NON_FIP;
7892
7893 /* Some of the fields are in the right position already */
7894 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7895 abort_tag = (uint32_t) iocbq->iotag;
7896 xritag = iocbq->sli4_xritag;
7897 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
7898 /* words0-2 bpl convert bde */
7899 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7900 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7901 sizeof(struct ulp_bde64);
7902 bpl = (struct ulp_bde64 *)
7903 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7904 if (!bpl)
7905 return IOCB_ERROR;
7906
7907 /* Should already be byte swapped. */
7908 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7909 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7910 /* swap the size field back to the cpu so we
7911 * can assign it to the sgl.
7912 */
7913 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
7914 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7915 total_len = 0;
7916 for (i = 0; i < numBdes; i++) {
7917 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7918 total_len += bde.tus.f.bdeSize;
7919 }
7920 } else
7921 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
7922
7923 iocbq->iocb.ulpIoTag = iocbq->iotag;
7924 cmnd = iocbq->iocb.ulpCommand;
7925
7926 switch (iocbq->iocb.ulpCommand) {
7927 case CMD_ELS_REQUEST64_CR:
7928 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7929 ndlp = iocbq->context_un.ndlp;
7930 else
7931 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7932 if (!iocbq->iocb.ulpLe) {
7933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7934 "2007 Only Limited Edition cmd Format"
7935 " supported 0x%x\n",
7936 iocbq->iocb.ulpCommand);
7937 return IOCB_ERROR;
7938 }
7939
7940 wqe->els_req.payload_len = xmit_len;
7941 /* Els_reguest64 has a TMO */
7942 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7943 iocbq->iocb.ulpTimeout);
7944 /* Need a VF for word 4 set the vf bit*/
7945 bf_set(els_req64_vf, &wqe->els_req, 0);
7946 /* And a VFID for word 12 */
7947 bf_set(els_req64_vfid, &wqe->els_req, 0);
7948 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7949 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7950 iocbq->iocb.ulpContext);
7951 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7952 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
7953 /* CCP CCPE PV PRI in word10 were set in the memcpy */
7954 if (command_type == ELS_COMMAND_FIP)
7955 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7956 >> LPFC_FIP_ELS_ID_SHIFT);
7957 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7958 iocbq->context2)->virt);
7959 if_type = bf_get(lpfc_sli_intf_if_type,
7960 &phba->sli4_hba.sli_intf);
7961 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7962 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
7963 *pcmd == ELS_CMD_SCR ||
7964 *pcmd == ELS_CMD_FDISC ||
7965 *pcmd == ELS_CMD_LOGO ||
7966 *pcmd == ELS_CMD_PLOGI)) {
7967 bf_set(els_req64_sp, &wqe->els_req, 1);
7968 bf_set(els_req64_sid, &wqe->els_req,
7969 iocbq->vport->fc_myDID);
7970 if ((*pcmd == ELS_CMD_FLOGI) &&
7971 !(phba->fc_topology ==
7972 LPFC_TOPOLOGY_LOOP))
7973 bf_set(els_req64_sid, &wqe->els_req, 0);
7974 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7975 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7976 phba->vpi_ids[iocbq->vport->vpi]);
7977 } else if (pcmd && iocbq->context1) {
7978 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7979 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7980 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7981 }
7982 }
7983 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7984 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7985 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7986 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7987 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7988 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7989 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7990 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7991 break;
7992 case CMD_XMIT_SEQUENCE64_CX:
7993 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7994 iocbq->iocb.un.ulpWord[3]);
7995 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7996 iocbq->iocb.unsli3.rcvsli3.ox_id);
7997 /* The entire sequence is transmitted for this IOCB */
7998 xmit_len = total_len;
7999 cmnd = CMD_XMIT_SEQUENCE64_CR;
8000 if (phba->link_flag & LS_LOOPBACK_MODE)
8001 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8002 case CMD_XMIT_SEQUENCE64_CR:
8003 /* word3 iocb=io_tag32 wqe=reserved */
8004 wqe->xmit_sequence.rsvd3 = 0;
8005 /* word4 relative_offset memcpy */
8006 /* word5 r_ctl/df_ctl memcpy */
8007 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8008 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8009 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8010 LPFC_WQE_IOD_WRITE);
8011 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8012 LPFC_WQE_LENLOC_WORD12);
8013 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8014 wqe->xmit_sequence.xmit_len = xmit_len;
8015 command_type = OTHER_COMMAND;
8016 break;
8017 case CMD_XMIT_BCAST64_CN:
8018 /* word3 iocb=iotag32 wqe=seq_payload_len */
8019 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8020 /* word4 iocb=rsvd wqe=rsvd */
8021 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8022 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8023 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8024 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8025 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8026 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8027 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8028 LPFC_WQE_LENLOC_WORD3);
8029 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8030 break;
8031 case CMD_FCP_IWRITE64_CR:
8032 command_type = FCP_COMMAND_DATA_OUT;
8033 /* word3 iocb=iotag wqe=payload_offset_len */
8034 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8035 wqe->fcp_iwrite.payload_offset_len =
8036 xmit_len + sizeof(struct fcp_rsp);
8037 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8038 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8039 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8040 iocbq->iocb.ulpFCP2Rcvy);
8041 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8042 /* Always open the exchange */
8043 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
8044 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8045 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8046 LPFC_WQE_LENLOC_WORD4);
8047 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8048 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8049 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8050 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8051 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8052 }
8053 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8054 break;
8055 case CMD_FCP_IREAD64_CR:
8056 /* word3 iocb=iotag wqe=payload_offset_len */
8057 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8058 wqe->fcp_iread.payload_offset_len =
8059 xmit_len + sizeof(struct fcp_rsp);
8060 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8061 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8062 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8063 iocbq->iocb.ulpFCP2Rcvy);
8064 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8065 /* Always open the exchange */
8066 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
8067 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8068 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8069 LPFC_WQE_LENLOC_WORD4);
8070 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8071 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8072 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8073 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8074 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8075 }
8076 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8077 break;
8078 case CMD_FCP_ICMND64_CR:
8079 /* word3 iocb=IO_TAG wqe=reserved */
8080 wqe->fcp_icmd.rsrvd3 = 0;
8081 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8082 /* Always open the exchange */
8083 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8084 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8085 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8086 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8087 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8088 LPFC_WQE_LENLOC_NONE);
8089 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
8090 break;
8091 case CMD_GEN_REQUEST64_CR:
8092 /* For this command calculate the xmit length of the
8093 * request bde.
8094 */
8095 xmit_len = 0;
8096 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8097 sizeof(struct ulp_bde64);
8098 for (i = 0; i < numBdes; i++) {
8099 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8100 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8101 break;
8102 xmit_len += bde.tus.f.bdeSize;
8103 }
8104 /* word3 iocb=IO_TAG wqe=request_payload_len */
8105 wqe->gen_req.request_payload_len = xmit_len;
8106 /* word4 iocb=parameter wqe=relative_offset memcpy */
8107 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8108 /* word6 context tag copied in memcpy */
8109 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8110 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8112 "2015 Invalid CT %x command 0x%x\n",
8113 ct, iocbq->iocb.ulpCommand);
8114 return IOCB_ERROR;
8115 }
8116 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8117 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8118 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8119 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8120 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8121 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8122 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8123 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8124 command_type = OTHER_COMMAND;
8125 break;
8126 case CMD_XMIT_ELS_RSP64_CX:
8127 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8128 /* words0-2 BDE memcpy */
8129 /* word3 iocb=iotag32 wqe=response_payload_len */
8130 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8131 /* word4 */
8132 wqe->xmit_els_rsp.word4 = 0;
8133 /* word5 iocb=rsvd wge=did */
8134 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8135 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8136
8137 if_type = bf_get(lpfc_sli_intf_if_type,
8138 &phba->sli4_hba.sli_intf);
8139 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8140 if (iocbq->vport->fc_flag & FC_PT2PT) {
8141 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8142 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8143 iocbq->vport->fc_myDID);
8144 if (iocbq->vport->fc_myDID == Fabric_DID) {
8145 bf_set(wqe_els_did,
8146 &wqe->xmit_els_rsp.wqe_dest, 0);
8147 }
8148 }
8149 }
8150 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8151 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8152 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8153 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8154 iocbq->iocb.unsli3.rcvsli3.ox_id);
8155 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8156 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8157 phba->vpi_ids[iocbq->vport->vpi]);
8158 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8159 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8160 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8161 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8162 LPFC_WQE_LENLOC_WORD3);
8163 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8164 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8165 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8166 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8167 iocbq->context2)->virt);
8168 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8169 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8170 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8171 iocbq->vport->fc_myDID);
8172 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8173 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8174 phba->vpi_ids[phba->pport->vpi]);
8175 }
8176 command_type = OTHER_COMMAND;
8177 break;
8178 case CMD_CLOSE_XRI_CN:
8179 case CMD_ABORT_XRI_CN:
8180 case CMD_ABORT_XRI_CX:
8181 /* words 0-2 memcpy should be 0 rserved */
8182 /* port will send abts */
8183 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8184 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8185 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8186 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8187 } else
8188 fip = 0;
8189
8190 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8191 /*
8192 * The link is down, or the command was ELS_FIP
8193 * so the fw does not need to send abts
8194 * on the wire.
8195 */
8196 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8197 else
8198 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8199 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8200 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8201 wqe->abort_cmd.rsrvd5 = 0;
8202 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8203 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8204 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8205 /*
8206 * The abort handler will send us CMD_ABORT_XRI_CN or
8207 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8208 */
8209 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8210 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8211 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8212 LPFC_WQE_LENLOC_NONE);
8213 cmnd = CMD_ABORT_XRI_CX;
8214 command_type = OTHER_COMMAND;
8215 xritag = 0;
8216 break;
8217 case CMD_XMIT_BLS_RSP64_CX:
8218 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8219 /* As BLS ABTS RSP WQE is very different from other WQEs,
8220 * we re-construct this WQE here based on information in
8221 * iocbq from scratch.
8222 */
8223 memset(wqe, 0, sizeof(union lpfc_wqe));
8224 /* OX_ID is invariable to who sent ABTS to CT exchange */
8225 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8226 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8227 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8228 LPFC_ABTS_UNSOL_INT) {
8229 /* ABTS sent by initiator to CT exchange, the
8230 * RX_ID field will be filled with the newly
8231 * allocated responder XRI.
8232 */
8233 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8234 iocbq->sli4_xritag);
8235 } else {
8236 /* ABTS sent by responder to CT exchange, the
8237 * RX_ID field will be filled with the responder
8238 * RX_ID from ABTS.
8239 */
8240 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8241 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8242 }
8243 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8244 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8245
8246 /* Use CT=VPI */
8247 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8248 ndlp->nlp_DID);
8249 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8250 iocbq->iocb.ulpContext);
8251 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8252 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8253 phba->vpi_ids[phba->pport->vpi]);
8254 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8255 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8256 LPFC_WQE_LENLOC_NONE);
8257 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8258 command_type = OTHER_COMMAND;
8259 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8260 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8261 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8262 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8263 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8264 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8265 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8266 }
8267
8268 break;
8269 case CMD_XRI_ABORTED_CX:
8270 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8271 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8272 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8273 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8274 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8275 default:
8276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8277 "2014 Invalid command 0x%x\n",
8278 iocbq->iocb.ulpCommand);
8279 return IOCB_ERROR;
8280 break;
8281 }
8282
8283 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8284 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8285 wqe->generic.wqe_com.abort_tag = abort_tag;
8286 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8287 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8288 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8289 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8290 return 0;
8291 }
8292
8293 /**
8294 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8295 * @phba: Pointer to HBA context object.
8296 * @ring_number: SLI ring number to issue iocb on.
8297 * @piocb: Pointer to command iocb.
8298 * @flag: Flag indicating if this command can be put into txq.
8299 *
8300 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8301 * an iocb command to an HBA with SLI-4 interface spec.
8302 *
8303 * This function is called with hbalock held. The function will return success
8304 * after it successfully submit the iocb to firmware or after adding to the
8305 * txq.
8306 **/
8307 static int
8308 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8309 struct lpfc_iocbq *piocb, uint32_t flag)
8310 {
8311 struct lpfc_sglq *sglq;
8312 union lpfc_wqe wqe;
8313 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8314
8315 if (piocb->sli4_xritag == NO_XRI) {
8316 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8317 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8318 sglq = NULL;
8319 else {
8320 if (pring->txq_cnt) {
8321 if (!(flag & SLI_IOCB_RET_IOCB)) {
8322 __lpfc_sli_ringtx_put(phba,
8323 pring, piocb);
8324 return IOCB_SUCCESS;
8325 } else {
8326 return IOCB_BUSY;
8327 }
8328 } else {
8329 sglq = __lpfc_sli_get_sglq(phba, piocb);
8330 if (!sglq) {
8331 if (!(flag & SLI_IOCB_RET_IOCB)) {
8332 __lpfc_sli_ringtx_put(phba,
8333 pring,
8334 piocb);
8335 return IOCB_SUCCESS;
8336 } else
8337 return IOCB_BUSY;
8338 }
8339 }
8340 }
8341 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8342 /* These IO's already have an XRI and a mapped sgl. */
8343 sglq = NULL;
8344 } else {
8345 /*
8346 * This is a continuation of a commandi,(CX) so this
8347 * sglq is on the active list
8348 */
8349 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8350 if (!sglq)
8351 return IOCB_ERROR;
8352 }
8353
8354 if (sglq) {
8355 piocb->sli4_lxritag = sglq->sli4_lxritag;
8356 piocb->sli4_xritag = sglq->sli4_xritag;
8357 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8358 return IOCB_ERROR;
8359 }
8360
8361 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8362 return IOCB_ERROR;
8363
8364 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8365 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8366 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8367 &wqe))
8368 return IOCB_ERROR;
8369 } else {
8370 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8371 return IOCB_ERROR;
8372 }
8373 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8374
8375 return 0;
8376 }
8377
8378 /**
8379 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8380 *
8381 * This routine wraps the actual lockless version for issusing IOCB function
8382 * pointer from the lpfc_hba struct.
8383 *
8384 * Return codes:
8385 * IOCB_ERROR - Error
8386 * IOCB_SUCCESS - Success
8387 * IOCB_BUSY - Busy
8388 **/
8389 int
8390 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8391 struct lpfc_iocbq *piocb, uint32_t flag)
8392 {
8393 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8394 }
8395
8396 /**
8397 * lpfc_sli_api_table_setup - Set up sli api function jump table
8398 * @phba: The hba struct for which this call is being executed.
8399 * @dev_grp: The HBA PCI-Device group number.
8400 *
8401 * This routine sets up the SLI interface API function jump table in @phba
8402 * struct.
8403 * Returns: 0 - success, -ENODEV - failure.
8404 **/
8405 int
8406 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8407 {
8408
8409 switch (dev_grp) {
8410 case LPFC_PCI_DEV_LP:
8411 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8412 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8413 break;
8414 case LPFC_PCI_DEV_OC:
8415 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8416 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8417 break;
8418 default:
8419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8420 "1419 Invalid HBA PCI-device group: 0x%x\n",
8421 dev_grp);
8422 return -ENODEV;
8423 break;
8424 }
8425 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8426 return 0;
8427 }
8428
8429 /**
8430 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8431 * @phba: Pointer to HBA context object.
8432 * @pring: Pointer to driver SLI ring object.
8433 * @piocb: Pointer to command iocb.
8434 * @flag: Flag indicating if this command can be put into txq.
8435 *
8436 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8437 * function. This function gets the hbalock and calls
8438 * __lpfc_sli_issue_iocb function and will return the error returned
8439 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8440 * functions which do not hold hbalock.
8441 **/
8442 int
8443 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8444 struct lpfc_iocbq *piocb, uint32_t flag)
8445 {
8446 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
8447 struct lpfc_sli_ring *pring;
8448 struct lpfc_queue *fpeq;
8449 struct lpfc_eqe *eqe;
8450 unsigned long iflags;
8451 int rc, idx;
8452
8453 if (phba->sli_rev == LPFC_SLI_REV4) {
8454 if (piocb->iocb_flag & LPFC_IO_FCP) {
8455 if (unlikely(!phba->sli4_hba.fcp_wq))
8456 return IOCB_ERROR;
8457 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8458 piocb->fcp_wqidx = idx;
8459 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8460
8461 pring = &phba->sli.ring[ring_number];
8462 spin_lock_irqsave(&pring->ring_lock, iflags);
8463 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8464 flag);
8465 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8466
8467 if (lpfc_fcp_look_ahead) {
8468 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8469
8470 if (atomic_dec_and_test(&fcp_eq_hdl->
8471 fcp_eq_in_use)) {
8472
8473 /* Get associated EQ with this index */
8474 fpeq = phba->sli4_hba.hba_eq[idx];
8475
8476 /* Turn off interrupts from this EQ */
8477 lpfc_sli4_eq_clr_intr(fpeq);
8478
8479 /*
8480 * Process all the events on FCP EQ
8481 */
8482 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8483 lpfc_sli4_hba_handle_eqe(phba,
8484 eqe, idx);
8485 fpeq->EQ_processed++;
8486 }
8487
8488 /* Always clear and re-arm the EQ */
8489 lpfc_sli4_eq_release(fpeq,
8490 LPFC_QUEUE_REARM);
8491 }
8492 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8493 }
8494 } else {
8495 pring = &phba->sli.ring[ring_number];
8496 spin_lock_irqsave(&pring->ring_lock, iflags);
8497 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8498 flag);
8499 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8500
8501 }
8502 } else {
8503 /* For now, SLI2/3 will still use hbalock */
8504 spin_lock_irqsave(&phba->hbalock, iflags);
8505 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8506 spin_unlock_irqrestore(&phba->hbalock, iflags);
8507 }
8508 return rc;
8509 }
8510
8511 /**
8512 * lpfc_extra_ring_setup - Extra ring setup function
8513 * @phba: Pointer to HBA context object.
8514 *
8515 * This function is called while driver attaches with the
8516 * HBA to setup the extra ring. The extra ring is used
8517 * only when driver needs to support target mode functionality
8518 * or IP over FC functionalities.
8519 *
8520 * This function is called with no lock held.
8521 **/
8522 static int
8523 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8524 {
8525 struct lpfc_sli *psli;
8526 struct lpfc_sli_ring *pring;
8527
8528 psli = &phba->sli;
8529
8530 /* Adjust cmd/rsp ring iocb entries more evenly */
8531
8532 /* Take some away from the FCP ring */
8533 pring = &psli->ring[psli->fcp_ring];
8534 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8535 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8536 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8537 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8538
8539 /* and give them to the extra ring */
8540 pring = &psli->ring[psli->extra_ring];
8541
8542 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8543 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8544 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8545 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8546
8547 /* Setup default profile for this ring */
8548 pring->iotag_max = 4096;
8549 pring->num_mask = 1;
8550 pring->prt[0].profile = 0; /* Mask 0 */
8551 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8552 pring->prt[0].type = phba->cfg_multi_ring_type;
8553 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8554 return 0;
8555 }
8556
8557 /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8558 * @vport: pointer to virtual port object.
8559 * @ndlp: nodelist pointer for the impacted rport.
8560 *
8561 * The driver calls this routine in response to a XRI ABORT CQE
8562 * event from the port. In this event, the driver is required to
8563 * recover its login to the rport even though its login may be valid
8564 * from the driver's perspective. The failed ABTS notice from the
8565 * port indicates the rport is not responding.
8566 */
8567 static void
8568 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8569 struct lpfc_nodelist *ndlp)
8570 {
8571 struct Scsi_Host *shost;
8572 struct lpfc_hba *phba;
8573 unsigned long flags = 0;
8574
8575 shost = lpfc_shost_from_vport(vport);
8576 phba = vport->phba;
8577 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8578 lpfc_printf_log(phba, KERN_INFO,
8579 LOG_SLI, "3093 No rport recovery needed. "
8580 "rport in state 0x%x\n",
8581 ndlp->nlp_state);
8582 return;
8583 }
8584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8585 "3094 Start rport recovery on shost id 0x%x "
8586 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8587 "flags 0x%x\n",
8588 shost->host_no, ndlp->nlp_DID,
8589 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8590 ndlp->nlp_flag);
8591 /*
8592 * The rport is not responding. Don't attempt ADISC recovery.
8593 * Remove the FCP-2 flag to force a PLOGI.
8594 */
8595 spin_lock_irqsave(shost->host_lock, flags);
8596 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8597 spin_unlock_irqrestore(shost->host_lock, flags);
8598 lpfc_disc_state_machine(vport, ndlp, NULL,
8599 NLP_EVT_DEVICE_RECOVERY);
8600 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8601 spin_lock_irqsave(shost->host_lock, flags);
8602 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8603 spin_unlock_irqrestore(shost->host_lock, flags);
8604 lpfc_disc_start(vport);
8605 }
8606
8607 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8608 * @phba: Pointer to HBA context object.
8609 * @iocbq: Pointer to iocb object.
8610 *
8611 * The async_event handler calls this routine when it receives
8612 * an ASYNC_STATUS_CN event from the port. The port generates
8613 * this event when an Abort Sequence request to an rport fails
8614 * twice in succession. The abort could be originated by the
8615 * driver or by the port. The ABTS could have been for an ELS
8616 * or FCP IO. The port only generates this event when an ABTS
8617 * fails to complete after one retry.
8618 */
8619 static void
8620 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8621 struct lpfc_iocbq *iocbq)
8622 {
8623 struct lpfc_nodelist *ndlp = NULL;
8624 uint16_t rpi = 0, vpi = 0;
8625 struct lpfc_vport *vport = NULL;
8626
8627 /* The rpi in the ulpContext is vport-sensitive. */
8628 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8629 rpi = iocbq->iocb.ulpContext;
8630
8631 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8632 "3092 Port generated ABTS async event "
8633 "on vpi %d rpi %d status 0x%x\n",
8634 vpi, rpi, iocbq->iocb.ulpStatus);
8635
8636 vport = lpfc_find_vport_by_vpid(phba, vpi);
8637 if (!vport)
8638 goto err_exit;
8639 ndlp = lpfc_findnode_rpi(vport, rpi);
8640 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8641 goto err_exit;
8642
8643 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8644 lpfc_sli_abts_recover_port(vport, ndlp);
8645 return;
8646
8647 err_exit:
8648 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8649 "3095 Event Context not found, no "
8650 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8651 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8652 vpi, rpi);
8653 }
8654
8655 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8656 * @phba: pointer to HBA context object.
8657 * @ndlp: nodelist pointer for the impacted rport.
8658 * @axri: pointer to the wcqe containing the failed exchange.
8659 *
8660 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8661 * port. The port generates this event when an abort exchange request to an
8662 * rport fails twice in succession with no reply. The abort could be originated
8663 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8664 */
8665 void
8666 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8667 struct lpfc_nodelist *ndlp,
8668 struct sli4_wcqe_xri_aborted *axri)
8669 {
8670 struct lpfc_vport *vport;
8671 uint32_t ext_status = 0;
8672
8673 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
8674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8675 "3115 Node Context not found, driver "
8676 "ignoring abts err event\n");
8677 return;
8678 }
8679
8680 vport = ndlp->vport;
8681 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8682 "3116 Port generated FCP XRI ABORT event on "
8683 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8684 ndlp->vport->vpi, ndlp->nlp_rpi,
8685 bf_get(lpfc_wcqe_xa_xri, axri),
8686 bf_get(lpfc_wcqe_xa_status, axri),
8687 axri->parameter);
8688
8689 /*
8690 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8691 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8692 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8693 */
8694 ext_status = axri->parameter & WCQE_PARAM_MASK;
8695 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8696 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
8697 lpfc_sli_abts_recover_port(vport, ndlp);
8698 }
8699
8700 /**
8701 * lpfc_sli_async_event_handler - ASYNC iocb handler function
8702 * @phba: Pointer to HBA context object.
8703 * @pring: Pointer to driver SLI ring object.
8704 * @iocbq: Pointer to iocb object.
8705 *
8706 * This function is called by the slow ring event handler
8707 * function when there is an ASYNC event iocb in the ring.
8708 * This function is called with no lock held.
8709 * Currently this function handles only temperature related
8710 * ASYNC events. The function decodes the temperature sensor
8711 * event message and posts events for the management applications.
8712 **/
8713 static void
8714 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8715 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8716 {
8717 IOCB_t *icmd;
8718 uint16_t evt_code;
8719 struct temp_event temp_event_data;
8720 struct Scsi_Host *shost;
8721 uint32_t *iocb_w;
8722
8723 icmd = &iocbq->iocb;
8724 evt_code = icmd->un.asyncstat.evt_code;
8725
8726 switch (evt_code) {
8727 case ASYNC_TEMP_WARN:
8728 case ASYNC_TEMP_SAFE:
8729 temp_event_data.data = (uint32_t) icmd->ulpContext;
8730 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8731 if (evt_code == ASYNC_TEMP_WARN) {
8732 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8733 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8734 "0347 Adapter is very hot, please take "
8735 "corrective action. temperature : %d Celsius\n",
8736 (uint32_t) icmd->ulpContext);
8737 } else {
8738 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8739 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8740 "0340 Adapter temperature is OK now. "
8741 "temperature : %d Celsius\n",
8742 (uint32_t) icmd->ulpContext);
8743 }
8744
8745 /* Send temperature change event to applications */
8746 shost = lpfc_shost_from_vport(phba->pport);
8747 fc_host_post_vendor_event(shost, fc_get_event_number(),
8748 sizeof(temp_event_data), (char *) &temp_event_data,
8749 LPFC_NL_VENDOR_ID);
8750 break;
8751 case ASYNC_STATUS_CN:
8752 lpfc_sli_abts_err_handler(phba, iocbq);
8753 break;
8754 default:
8755 iocb_w = (uint32_t *) icmd;
8756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8757 "0346 Ring %d handler: unexpected ASYNC_STATUS"
8758 " evt_code 0x%x\n"
8759 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8760 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8761 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8762 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
8763 pring->ringno, icmd->un.asyncstat.evt_code,
8764 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8765 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8766 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8767 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8768
8769 break;
8770 }
8771 }
8772
8773
8774 /**
8775 * lpfc_sli_setup - SLI ring setup function
8776 * @phba: Pointer to HBA context object.
8777 *
8778 * lpfc_sli_setup sets up rings of the SLI interface with
8779 * number of iocbs per ring and iotags. This function is
8780 * called while driver attach to the HBA and before the
8781 * interrupts are enabled. So there is no need for locking.
8782 *
8783 * This function always returns 0.
8784 **/
8785 int
8786 lpfc_sli_setup(struct lpfc_hba *phba)
8787 {
8788 int i, totiocbsize = 0;
8789 struct lpfc_sli *psli = &phba->sli;
8790 struct lpfc_sli_ring *pring;
8791
8792 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8793 if (phba->sli_rev == LPFC_SLI_REV4)
8794 psli->num_rings += phba->cfg_fcp_io_channel;
8795 psli->sli_flag = 0;
8796 psli->fcp_ring = LPFC_FCP_RING;
8797 psli->next_ring = LPFC_FCP_NEXT_RING;
8798 psli->extra_ring = LPFC_EXTRA_RING;
8799
8800 psli->iocbq_lookup = NULL;
8801 psli->iocbq_lookup_len = 0;
8802 psli->last_iotag = 0;
8803
8804 for (i = 0; i < psli->num_rings; i++) {
8805 pring = &psli->ring[i];
8806 switch (i) {
8807 case LPFC_FCP_RING: /* ring 0 - FCP */
8808 /* numCiocb and numRiocb are used in config_port */
8809 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8810 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8811 pring->sli.sli3.numCiocb +=
8812 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8813 pring->sli.sli3.numRiocb +=
8814 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8815 pring->sli.sli3.numCiocb +=
8816 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8817 pring->sli.sli3.numRiocb +=
8818 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8819 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8820 SLI3_IOCB_CMD_SIZE :
8821 SLI2_IOCB_CMD_SIZE;
8822 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8823 SLI3_IOCB_RSP_SIZE :
8824 SLI2_IOCB_RSP_SIZE;
8825 pring->iotag_ctr = 0;
8826 pring->iotag_max =
8827 (phba->cfg_hba_queue_depth * 2);
8828 pring->fast_iotag = pring->iotag_max;
8829 pring->num_mask = 0;
8830 break;
8831 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
8832 /* numCiocb and numRiocb are used in config_port */
8833 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8834 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8835 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8836 SLI3_IOCB_CMD_SIZE :
8837 SLI2_IOCB_CMD_SIZE;
8838 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8839 SLI3_IOCB_RSP_SIZE :
8840 SLI2_IOCB_RSP_SIZE;
8841 pring->iotag_max = phba->cfg_hba_queue_depth;
8842 pring->num_mask = 0;
8843 break;
8844 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8845 /* numCiocb and numRiocb are used in config_port */
8846 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8847 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8848 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8849 SLI3_IOCB_CMD_SIZE :
8850 SLI2_IOCB_CMD_SIZE;
8851 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8852 SLI3_IOCB_RSP_SIZE :
8853 SLI2_IOCB_RSP_SIZE;
8854 pring->fast_iotag = 0;
8855 pring->iotag_ctr = 0;
8856 pring->iotag_max = 4096;
8857 pring->lpfc_sli_rcv_async_status =
8858 lpfc_sli_async_event_handler;
8859 pring->num_mask = LPFC_MAX_RING_MASK;
8860 pring->prt[0].profile = 0; /* Mask 0 */
8861 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8862 pring->prt[0].type = FC_TYPE_ELS;
8863 pring->prt[0].lpfc_sli_rcv_unsol_event =
8864 lpfc_els_unsol_event;
8865 pring->prt[1].profile = 0; /* Mask 1 */
8866 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8867 pring->prt[1].type = FC_TYPE_ELS;
8868 pring->prt[1].lpfc_sli_rcv_unsol_event =
8869 lpfc_els_unsol_event;
8870 pring->prt[2].profile = 0; /* Mask 2 */
8871 /* NameServer Inquiry */
8872 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
8873 /* NameServer */
8874 pring->prt[2].type = FC_TYPE_CT;
8875 pring->prt[2].lpfc_sli_rcv_unsol_event =
8876 lpfc_ct_unsol_event;
8877 pring->prt[3].profile = 0; /* Mask 3 */
8878 /* NameServer response */
8879 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
8880 /* NameServer */
8881 pring->prt[3].type = FC_TYPE_CT;
8882 pring->prt[3].lpfc_sli_rcv_unsol_event =
8883 lpfc_ct_unsol_event;
8884 /* abort unsolicited sequence */
8885 pring->prt[4].profile = 0; /* Mask 4 */
8886 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8887 pring->prt[4].type = FC_TYPE_BLS;
8888 pring->prt[4].lpfc_sli_rcv_unsol_event =
8889 lpfc_sli4_ct_abort_unsol_event;
8890 break;
8891 }
8892 totiocbsize += (pring->sli.sli3.numCiocb *
8893 pring->sli.sli3.sizeCiocb) +
8894 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
8895 }
8896 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
8897 /* Too many cmd / rsp ring entries in SLI2 SLIM */
8898 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8899 "SLI2 SLIM Data: x%x x%lx\n",
8900 phba->brd_no, totiocbsize,
8901 (unsigned long) MAX_SLIM_IOCB_SIZE);
8902 }
8903 if (phba->cfg_multi_ring_support == 2)
8904 lpfc_extra_ring_setup(phba);
8905
8906 return 0;
8907 }
8908
8909 /**
8910 * lpfc_sli_queue_setup - Queue initialization function
8911 * @phba: Pointer to HBA context object.
8912 *
8913 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8914 * ring. This function also initializes ring indices of each ring.
8915 * This function is called during the initialization of the SLI
8916 * interface of an HBA.
8917 * This function is called with no lock held and always returns
8918 * 1.
8919 **/
8920 int
8921 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8922 {
8923 struct lpfc_sli *psli;
8924 struct lpfc_sli_ring *pring;
8925 int i;
8926
8927 psli = &phba->sli;
8928 spin_lock_irq(&phba->hbalock);
8929 INIT_LIST_HEAD(&psli->mboxq);
8930 INIT_LIST_HEAD(&psli->mboxq_cmpl);
8931 /* Initialize list headers for txq and txcmplq as double linked lists */
8932 for (i = 0; i < psli->num_rings; i++) {
8933 pring = &psli->ring[i];
8934 pring->ringno = i;
8935 pring->sli.sli3.next_cmdidx = 0;
8936 pring->sli.sli3.local_getidx = 0;
8937 pring->sli.sli3.cmdidx = 0;
8938 INIT_LIST_HEAD(&pring->txq);
8939 INIT_LIST_HEAD(&pring->txcmplq);
8940 INIT_LIST_HEAD(&pring->iocb_continueq);
8941 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
8942 INIT_LIST_HEAD(&pring->postbufq);
8943 spin_lock_init(&pring->ring_lock);
8944 }
8945 spin_unlock_irq(&phba->hbalock);
8946 return 1;
8947 }
8948
8949 /**
8950 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8951 * @phba: Pointer to HBA context object.
8952 *
8953 * This routine flushes the mailbox command subsystem. It will unconditionally
8954 * flush all the mailbox commands in the three possible stages in the mailbox
8955 * command sub-system: pending mailbox command queue; the outstanding mailbox
8956 * command; and completed mailbox command queue. It is caller's responsibility
8957 * to make sure that the driver is in the proper state to flush the mailbox
8958 * command sub-system. Namely, the posting of mailbox commands into the
8959 * pending mailbox command queue from the various clients must be stopped;
8960 * either the HBA is in a state that it will never works on the outstanding
8961 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8962 * mailbox command has been completed.
8963 **/
8964 static void
8965 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8966 {
8967 LIST_HEAD(completions);
8968 struct lpfc_sli *psli = &phba->sli;
8969 LPFC_MBOXQ_t *pmb;
8970 unsigned long iflag;
8971
8972 /* Flush all the mailbox commands in the mbox system */
8973 spin_lock_irqsave(&phba->hbalock, iflag);
8974 /* The pending mailbox command queue */
8975 list_splice_init(&phba->sli.mboxq, &completions);
8976 /* The outstanding active mailbox command */
8977 if (psli->mbox_active) {
8978 list_add_tail(&psli->mbox_active->list, &completions);
8979 psli->mbox_active = NULL;
8980 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8981 }
8982 /* The completed mailbox command queue */
8983 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8984 spin_unlock_irqrestore(&phba->hbalock, iflag);
8985
8986 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8987 while (!list_empty(&completions)) {
8988 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8989 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8990 if (pmb->mbox_cmpl)
8991 pmb->mbox_cmpl(phba, pmb);
8992 }
8993 }
8994
8995 /**
8996 * lpfc_sli_host_down - Vport cleanup function
8997 * @vport: Pointer to virtual port object.
8998 *
8999 * lpfc_sli_host_down is called to clean up the resources
9000 * associated with a vport before destroying virtual
9001 * port data structures.
9002 * This function does following operations:
9003 * - Free discovery resources associated with this virtual
9004 * port.
9005 * - Free iocbs associated with this virtual port in
9006 * the txq.
9007 * - Send abort for all iocb commands associated with this
9008 * vport in txcmplq.
9009 *
9010 * This function is called with no lock held and always returns 1.
9011 **/
9012 int
9013 lpfc_sli_host_down(struct lpfc_vport *vport)
9014 {
9015 LIST_HEAD(completions);
9016 struct lpfc_hba *phba = vport->phba;
9017 struct lpfc_sli *psli = &phba->sli;
9018 struct lpfc_sli_ring *pring;
9019 struct lpfc_iocbq *iocb, *next_iocb;
9020 int i;
9021 unsigned long flags = 0;
9022 uint16_t prev_pring_flag;
9023
9024 lpfc_cleanup_discovery_resources(vport);
9025
9026 spin_lock_irqsave(&phba->hbalock, flags);
9027 for (i = 0; i < psli->num_rings; i++) {
9028 pring = &psli->ring[i];
9029 prev_pring_flag = pring->flag;
9030 /* Only slow rings */
9031 if (pring->ringno == LPFC_ELS_RING) {
9032 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9033 /* Set the lpfc data pending flag */
9034 set_bit(LPFC_DATA_READY, &phba->data_flags);
9035 }
9036 /*
9037 * Error everything on the txq since these iocbs have not been
9038 * given to the FW yet.
9039 */
9040 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9041 if (iocb->vport != vport)
9042 continue;
9043 list_move_tail(&iocb->list, &completions);
9044 pring->txq_cnt--;
9045 }
9046
9047 /* Next issue ABTS for everything on the txcmplq */
9048 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9049 list) {
9050 if (iocb->vport != vport)
9051 continue;
9052 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9053 }
9054
9055 pring->flag = prev_pring_flag;
9056 }
9057
9058 spin_unlock_irqrestore(&phba->hbalock, flags);
9059
9060 /* Cancel all the IOCBs from the completions list */
9061 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9062 IOERR_SLI_DOWN);
9063 return 1;
9064 }
9065
9066 /**
9067 * lpfc_sli_hba_down - Resource cleanup function for the HBA
9068 * @phba: Pointer to HBA context object.
9069 *
9070 * This function cleans up all iocb, buffers, mailbox commands
9071 * while shutting down the HBA. This function is called with no
9072 * lock held and always returns 1.
9073 * This function does the following to cleanup driver resources:
9074 * - Free discovery resources for each virtual port
9075 * - Cleanup any pending fabric iocbs
9076 * - Iterate through the iocb txq and free each entry
9077 * in the list.
9078 * - Free up any buffer posted to the HBA
9079 * - Free mailbox commands in the mailbox queue.
9080 **/
9081 int
9082 lpfc_sli_hba_down(struct lpfc_hba *phba)
9083 {
9084 LIST_HEAD(completions);
9085 struct lpfc_sli *psli = &phba->sli;
9086 struct lpfc_sli_ring *pring;
9087 struct lpfc_dmabuf *buf_ptr;
9088 unsigned long flags = 0;
9089 int i;
9090
9091 /* Shutdown the mailbox command sub-system */
9092 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9093
9094 lpfc_hba_down_prep(phba);
9095
9096 lpfc_fabric_abort_hba(phba);
9097
9098 spin_lock_irqsave(&phba->hbalock, flags);
9099 for (i = 0; i < psli->num_rings; i++) {
9100 pring = &psli->ring[i];
9101 /* Only slow rings */
9102 if (pring->ringno == LPFC_ELS_RING) {
9103 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9104 /* Set the lpfc data pending flag */
9105 set_bit(LPFC_DATA_READY, &phba->data_flags);
9106 }
9107
9108 /*
9109 * Error everything on the txq since these iocbs have not been
9110 * given to the FW yet.
9111 */
9112 list_splice_init(&pring->txq, &completions);
9113 pring->txq_cnt = 0;
9114
9115 }
9116 spin_unlock_irqrestore(&phba->hbalock, flags);
9117
9118 /* Cancel all the IOCBs from the completions list */
9119 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9120 IOERR_SLI_DOWN);
9121
9122 spin_lock_irqsave(&phba->hbalock, flags);
9123 list_splice_init(&phba->elsbuf, &completions);
9124 phba->elsbuf_cnt = 0;
9125 phba->elsbuf_prev_cnt = 0;
9126 spin_unlock_irqrestore(&phba->hbalock, flags);
9127
9128 while (!list_empty(&completions)) {
9129 list_remove_head(&completions, buf_ptr,
9130 struct lpfc_dmabuf, list);
9131 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9132 kfree(buf_ptr);
9133 }
9134
9135 /* Return any active mbox cmds */
9136 del_timer_sync(&psli->mbox_tmo);
9137
9138 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9139 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9140 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9141
9142 return 1;
9143 }
9144
9145 /**
9146 * lpfc_sli_pcimem_bcopy - SLI memory copy function
9147 * @srcp: Source memory pointer.
9148 * @destp: Destination memory pointer.
9149 * @cnt: Number of words required to be copied.
9150 *
9151 * This function is used for copying data between driver memory
9152 * and the SLI memory. This function also changes the endianness
9153 * of each word if native endianness is different from SLI
9154 * endianness. This function can be called with or without
9155 * lock.
9156 **/
9157 void
9158 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9159 {
9160 uint32_t *src = srcp;
9161 uint32_t *dest = destp;
9162 uint32_t ldata;
9163 int i;
9164
9165 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9166 ldata = *src;
9167 ldata = le32_to_cpu(ldata);
9168 *dest = ldata;
9169 src++;
9170 dest++;
9171 }
9172 }
9173
9174
9175 /**
9176 * lpfc_sli_bemem_bcopy - SLI memory copy function
9177 * @srcp: Source memory pointer.
9178 * @destp: Destination memory pointer.
9179 * @cnt: Number of words required to be copied.
9180 *
9181 * This function is used for copying data between a data structure
9182 * with big endian representation to local endianness.
9183 * This function can be called with or without lock.
9184 **/
9185 void
9186 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9187 {
9188 uint32_t *src = srcp;
9189 uint32_t *dest = destp;
9190 uint32_t ldata;
9191 int i;
9192
9193 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9194 ldata = *src;
9195 ldata = be32_to_cpu(ldata);
9196 *dest = ldata;
9197 src++;
9198 dest++;
9199 }
9200 }
9201
9202 /**
9203 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9204 * @phba: Pointer to HBA context object.
9205 * @pring: Pointer to driver SLI ring object.
9206 * @mp: Pointer to driver buffer object.
9207 *
9208 * This function is called with no lock held.
9209 * It always return zero after adding the buffer to the postbufq
9210 * buffer list.
9211 **/
9212 int
9213 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9214 struct lpfc_dmabuf *mp)
9215 {
9216 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9217 later */
9218 spin_lock_irq(&phba->hbalock);
9219 list_add_tail(&mp->list, &pring->postbufq);
9220 pring->postbufq_cnt++;
9221 spin_unlock_irq(&phba->hbalock);
9222 return 0;
9223 }
9224
9225 /**
9226 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9227 * @phba: Pointer to HBA context object.
9228 *
9229 * When HBQ is enabled, buffers are searched based on tags. This function
9230 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9231 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9232 * does not conflict with tags of buffer posted for unsolicited events.
9233 * The function returns the allocated tag. The function is called with
9234 * no locks held.
9235 **/
9236 uint32_t
9237 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9238 {
9239 spin_lock_irq(&phba->hbalock);
9240 phba->buffer_tag_count++;
9241 /*
9242 * Always set the QUE_BUFTAG_BIT to distiguish between
9243 * a tag assigned by HBQ.
9244 */
9245 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9246 spin_unlock_irq(&phba->hbalock);
9247 return phba->buffer_tag_count;
9248 }
9249
9250 /**
9251 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9252 * @phba: Pointer to HBA context object.
9253 * @pring: Pointer to driver SLI ring object.
9254 * @tag: Buffer tag.
9255 *
9256 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9257 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9258 * iocb is posted to the response ring with the tag of the buffer.
9259 * This function searches the pring->postbufq list using the tag
9260 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9261 * iocb. If the buffer is found then lpfc_dmabuf object of the
9262 * buffer is returned to the caller else NULL is returned.
9263 * This function is called with no lock held.
9264 **/
9265 struct lpfc_dmabuf *
9266 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9267 uint32_t tag)
9268 {
9269 struct lpfc_dmabuf *mp, *next_mp;
9270 struct list_head *slp = &pring->postbufq;
9271
9272 /* Search postbufq, from the beginning, looking for a match on tag */
9273 spin_lock_irq(&phba->hbalock);
9274 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9275 if (mp->buffer_tag == tag) {
9276 list_del_init(&mp->list);
9277 pring->postbufq_cnt--;
9278 spin_unlock_irq(&phba->hbalock);
9279 return mp;
9280 }
9281 }
9282
9283 spin_unlock_irq(&phba->hbalock);
9284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9285 "0402 Cannot find virtual addr for buffer tag on "
9286 "ring %d Data x%lx x%p x%p x%x\n",
9287 pring->ringno, (unsigned long) tag,
9288 slp->next, slp->prev, pring->postbufq_cnt);
9289
9290 return NULL;
9291 }
9292
9293 /**
9294 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9295 * @phba: Pointer to HBA context object.
9296 * @pring: Pointer to driver SLI ring object.
9297 * @phys: DMA address of the buffer.
9298 *
9299 * This function searches the buffer list using the dma_address
9300 * of unsolicited event to find the driver's lpfc_dmabuf object
9301 * corresponding to the dma_address. The function returns the
9302 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9303 * This function is called by the ct and els unsolicited event
9304 * handlers to get the buffer associated with the unsolicited
9305 * event.
9306 *
9307 * This function is called with no lock held.
9308 **/
9309 struct lpfc_dmabuf *
9310 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9311 dma_addr_t phys)
9312 {
9313 struct lpfc_dmabuf *mp, *next_mp;
9314 struct list_head *slp = &pring->postbufq;
9315
9316 /* Search postbufq, from the beginning, looking for a match on phys */
9317 spin_lock_irq(&phba->hbalock);
9318 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9319 if (mp->phys == phys) {
9320 list_del_init(&mp->list);
9321 pring->postbufq_cnt--;
9322 spin_unlock_irq(&phba->hbalock);
9323 return mp;
9324 }
9325 }
9326
9327 spin_unlock_irq(&phba->hbalock);
9328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9329 "0410 Cannot find virtual addr for mapped buf on "
9330 "ring %d Data x%llx x%p x%p x%x\n",
9331 pring->ringno, (unsigned long long)phys,
9332 slp->next, slp->prev, pring->postbufq_cnt);
9333 return NULL;
9334 }
9335
9336 /**
9337 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9338 * @phba: Pointer to HBA context object.
9339 * @cmdiocb: Pointer to driver command iocb object.
9340 * @rspiocb: Pointer to driver response iocb object.
9341 *
9342 * This function is the completion handler for the abort iocbs for
9343 * ELS commands. This function is called from the ELS ring event
9344 * handler with no lock held. This function frees memory resources
9345 * associated with the abort iocb.
9346 **/
9347 static void
9348 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9349 struct lpfc_iocbq *rspiocb)
9350 {
9351 IOCB_t *irsp = &rspiocb->iocb;
9352 uint16_t abort_iotag, abort_context;
9353 struct lpfc_iocbq *abort_iocb = NULL;
9354
9355 if (irsp->ulpStatus) {
9356
9357 /*
9358 * Assume that the port already completed and returned, or
9359 * will return the iocb. Just Log the message.
9360 */
9361 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9362 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9363
9364 spin_lock_irq(&phba->hbalock);
9365 if (phba->sli_rev < LPFC_SLI_REV4) {
9366 if (abort_iotag != 0 &&
9367 abort_iotag <= phba->sli.last_iotag)
9368 abort_iocb =
9369 phba->sli.iocbq_lookup[abort_iotag];
9370 } else
9371 /* For sli4 the abort_tag is the XRI,
9372 * so the abort routine puts the iotag of the iocb
9373 * being aborted in the context field of the abort
9374 * IOCB.
9375 */
9376 abort_iocb = phba->sli.iocbq_lookup[abort_context];
9377
9378 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9379 "0327 Cannot abort els iocb %p "
9380 "with tag %x context %x, abort status %x, "
9381 "abort code %x\n",
9382 abort_iocb, abort_iotag, abort_context,
9383 irsp->ulpStatus, irsp->un.ulpWord[4]);
9384
9385 spin_unlock_irq(&phba->hbalock);
9386 }
9387 lpfc_sli_release_iocbq(phba, cmdiocb);
9388 return;
9389 }
9390
9391 /**
9392 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9393 * @phba: Pointer to HBA context object.
9394 * @cmdiocb: Pointer to driver command iocb object.
9395 * @rspiocb: Pointer to driver response iocb object.
9396 *
9397 * The function is called from SLI ring event handler with no
9398 * lock held. This function is the completion handler for ELS commands
9399 * which are aborted. The function frees memory resources used for
9400 * the aborted ELS commands.
9401 **/
9402 static void
9403 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9404 struct lpfc_iocbq *rspiocb)
9405 {
9406 IOCB_t *irsp = &rspiocb->iocb;
9407
9408 /* ELS cmd tag <ulpIoTag> completes */
9409 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9410 "0139 Ignoring ELS cmd tag x%x completion Data: "
9411 "x%x x%x x%x\n",
9412 irsp->ulpIoTag, irsp->ulpStatus,
9413 irsp->un.ulpWord[4], irsp->ulpTimeout);
9414 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9415 lpfc_ct_free_iocb(phba, cmdiocb);
9416 else
9417 lpfc_els_free_iocb(phba, cmdiocb);
9418 return;
9419 }
9420
9421 /**
9422 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9423 * @phba: Pointer to HBA context object.
9424 * @pring: Pointer to driver SLI ring object.
9425 * @cmdiocb: Pointer to driver command iocb object.
9426 *
9427 * This function issues an abort iocb for the provided command iocb down to
9428 * the port. Other than the case the outstanding command iocb is an abort
9429 * request, this function issues abort out unconditionally. This function is
9430 * called with hbalock held. The function returns 0 when it fails due to
9431 * memory allocation failure or when the command iocb is an abort request.
9432 **/
9433 static int
9434 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9435 struct lpfc_iocbq *cmdiocb)
9436 {
9437 struct lpfc_vport *vport = cmdiocb->vport;
9438 struct lpfc_iocbq *abtsiocbp;
9439 IOCB_t *icmd = NULL;
9440 IOCB_t *iabt = NULL;
9441 int retval;
9442 unsigned long iflags;
9443
9444 /*
9445 * There are certain command types we don't want to abort. And we
9446 * don't want to abort commands that are already in the process of
9447 * being aborted.
9448 */
9449 icmd = &cmdiocb->iocb;
9450 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9451 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9452 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9453 return 0;
9454
9455 /* issue ABTS for this IOCB based on iotag */
9456 abtsiocbp = __lpfc_sli_get_iocbq(phba);
9457 if (abtsiocbp == NULL)
9458 return 0;
9459
9460 /* This signals the response to set the correct status
9461 * before calling the completion handler
9462 */
9463 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9464
9465 iabt = &abtsiocbp->iocb;
9466 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9467 iabt->un.acxri.abortContextTag = icmd->ulpContext;
9468 if (phba->sli_rev == LPFC_SLI_REV4) {
9469 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9470 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9471 }
9472 else
9473 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9474 iabt->ulpLe = 1;
9475 iabt->ulpClass = icmd->ulpClass;
9476
9477 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9478 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9479 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9480 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9481
9482 if (phba->link_state >= LPFC_LINK_UP)
9483 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9484 else
9485 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9486
9487 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9488
9489 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9490 "0339 Abort xri x%x, original iotag x%x, "
9491 "abort cmd iotag x%x\n",
9492 iabt->un.acxri.abortIoTag,
9493 iabt->un.acxri.abortContextTag,
9494 abtsiocbp->iotag);
9495
9496 if (phba->sli_rev == LPFC_SLI_REV4) {
9497 /* Note: both hbalock and ring_lock need to be set here */
9498 spin_lock_irqsave(&pring->ring_lock, iflags);
9499 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9500 abtsiocbp, 0);
9501 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9502 } else {
9503 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9504 abtsiocbp, 0);
9505 }
9506
9507 if (retval)
9508 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9509
9510 /*
9511 * Caller to this routine should check for IOCB_ERROR
9512 * and handle it properly. This routine no longer removes
9513 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9514 */
9515 return retval;
9516 }
9517
9518 /**
9519 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9520 * @phba: Pointer to HBA context object.
9521 * @pring: Pointer to driver SLI ring object.
9522 * @cmdiocb: Pointer to driver command iocb object.
9523 *
9524 * This function issues an abort iocb for the provided command iocb. In case
9525 * of unloading, the abort iocb will not be issued to commands on the ELS
9526 * ring. Instead, the callback function shall be changed to those commands
9527 * so that nothing happens when them finishes. This function is called with
9528 * hbalock held. The function returns 0 when the command iocb is an abort
9529 * request.
9530 **/
9531 int
9532 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9533 struct lpfc_iocbq *cmdiocb)
9534 {
9535 struct lpfc_vport *vport = cmdiocb->vport;
9536 int retval = IOCB_ERROR;
9537 IOCB_t *icmd = NULL;
9538
9539 /*
9540 * There are certain command types we don't want to abort. And we
9541 * don't want to abort commands that are already in the process of
9542 * being aborted.
9543 */
9544 icmd = &cmdiocb->iocb;
9545 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9546 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9547 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9548 return 0;
9549
9550 /*
9551 * If we're unloading, don't abort iocb on the ELS ring, but change
9552 * the callback so that nothing happens when it finishes.
9553 */
9554 if ((vport->load_flag & FC_UNLOADING) &&
9555 (pring->ringno == LPFC_ELS_RING)) {
9556 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9557 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9558 else
9559 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9560 goto abort_iotag_exit;
9561 }
9562
9563 /* Now, we try to issue the abort to the cmdiocb out */
9564 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9565
9566 abort_iotag_exit:
9567 /*
9568 * Caller to this routine should check for IOCB_ERROR
9569 * and handle it properly. This routine no longer removes
9570 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9571 */
9572 return retval;
9573 }
9574
9575 /**
9576 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9577 * @phba: Pointer to HBA context object.
9578 * @pring: Pointer to driver SLI ring object.
9579 *
9580 * This function aborts all iocbs in the given ring and frees all the iocb
9581 * objects in txq. This function issues abort iocbs unconditionally for all
9582 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9583 * to complete before the return of this function. The caller is not required
9584 * to hold any locks.
9585 **/
9586 static void
9587 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9588 {
9589 LIST_HEAD(completions);
9590 struct lpfc_iocbq *iocb, *next_iocb;
9591
9592 if (pring->ringno == LPFC_ELS_RING)
9593 lpfc_fabric_abort_hba(phba);
9594
9595 spin_lock_irq(&phba->hbalock);
9596
9597 /* Take off all the iocbs on txq for cancelling */
9598 list_splice_init(&pring->txq, &completions);
9599 pring->txq_cnt = 0;
9600
9601 /* Next issue ABTS for everything on the txcmplq */
9602 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9603 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9604
9605 spin_unlock_irq(&phba->hbalock);
9606
9607 /* Cancel all the IOCBs from the completions list */
9608 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9609 IOERR_SLI_ABORTED);
9610 }
9611
9612 /**
9613 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9614 * @phba: pointer to lpfc HBA data structure.
9615 *
9616 * This routine will abort all pending and outstanding iocbs to an HBA.
9617 **/
9618 void
9619 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9620 {
9621 struct lpfc_sli *psli = &phba->sli;
9622 struct lpfc_sli_ring *pring;
9623 int i;
9624
9625 for (i = 0; i < psli->num_rings; i++) {
9626 pring = &psli->ring[i];
9627 lpfc_sli_iocb_ring_abort(phba, pring);
9628 }
9629 }
9630
9631 /**
9632 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9633 * @iocbq: Pointer to driver iocb object.
9634 * @vport: Pointer to driver virtual port object.
9635 * @tgt_id: SCSI ID of the target.
9636 * @lun_id: LUN ID of the scsi device.
9637 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9638 *
9639 * This function acts as an iocb filter for functions which abort or count
9640 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9641 * 0 if the filtering criteria is met for the given iocb and will return
9642 * 1 if the filtering criteria is not met.
9643 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9644 * given iocb is for the SCSI device specified by vport, tgt_id and
9645 * lun_id parameter.
9646 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9647 * given iocb is for the SCSI target specified by vport and tgt_id
9648 * parameters.
9649 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9650 * given iocb is for the SCSI host associated with the given vport.
9651 * This function is called with no locks held.
9652 **/
9653 static int
9654 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9655 uint16_t tgt_id, uint64_t lun_id,
9656 lpfc_ctx_cmd ctx_cmd)
9657 {
9658 struct lpfc_scsi_buf *lpfc_cmd;
9659 int rc = 1;
9660
9661 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9662 return rc;
9663
9664 if (iocbq->vport != vport)
9665 return rc;
9666
9667 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9668
9669 if (lpfc_cmd->pCmd == NULL)
9670 return rc;
9671
9672 switch (ctx_cmd) {
9673 case LPFC_CTX_LUN:
9674 if ((lpfc_cmd->rdata->pnode) &&
9675 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9676 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9677 rc = 0;
9678 break;
9679 case LPFC_CTX_TGT:
9680 if ((lpfc_cmd->rdata->pnode) &&
9681 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9682 rc = 0;
9683 break;
9684 case LPFC_CTX_HOST:
9685 rc = 0;
9686 break;
9687 default:
9688 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9689 __func__, ctx_cmd);
9690 break;
9691 }
9692
9693 return rc;
9694 }
9695
9696 /**
9697 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
9698 * @vport: Pointer to virtual port.
9699 * @tgt_id: SCSI ID of the target.
9700 * @lun_id: LUN ID of the scsi device.
9701 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9702 *
9703 * This function returns number of FCP commands pending for the vport.
9704 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9705 * commands pending on the vport associated with SCSI device specified
9706 * by tgt_id and lun_id parameters.
9707 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9708 * commands pending on the vport associated with SCSI target specified
9709 * by tgt_id parameter.
9710 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9711 * commands pending on the vport.
9712 * This function returns the number of iocbs which satisfy the filter.
9713 * This function is called without any lock held.
9714 **/
9715 int
9716 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9717 lpfc_ctx_cmd ctx_cmd)
9718 {
9719 struct lpfc_hba *phba = vport->phba;
9720 struct lpfc_iocbq *iocbq;
9721 int sum, i;
9722
9723 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9724 iocbq = phba->sli.iocbq_lookup[i];
9725
9726 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9727 ctx_cmd) == 0)
9728 sum++;
9729 }
9730
9731 return sum;
9732 }
9733
9734 /**
9735 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
9736 * @phba: Pointer to HBA context object
9737 * @cmdiocb: Pointer to command iocb object.
9738 * @rspiocb: Pointer to response iocb object.
9739 *
9740 * This function is called when an aborted FCP iocb completes. This
9741 * function is called by the ring event handler with no lock held.
9742 * This function frees the iocb.
9743 **/
9744 void
9745 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9746 struct lpfc_iocbq *rspiocb)
9747 {
9748 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9749 "3096 ABORT_XRI_CN completing on xri x%x "
9750 "original iotag x%x, abort cmd iotag x%x "
9751 "status 0x%x, reason 0x%x\n",
9752 cmdiocb->iocb.un.acxri.abortContextTag,
9753 cmdiocb->iocb.un.acxri.abortIoTag,
9754 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9755 rspiocb->iocb.un.ulpWord[4]);
9756 lpfc_sli_release_iocbq(phba, cmdiocb);
9757 return;
9758 }
9759
9760 /**
9761 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
9762 * @vport: Pointer to virtual port.
9763 * @pring: Pointer to driver SLI ring object.
9764 * @tgt_id: SCSI ID of the target.
9765 * @lun_id: LUN ID of the scsi device.
9766 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9767 *
9768 * This function sends an abort command for every SCSI command
9769 * associated with the given virtual port pending on the ring
9770 * filtered by lpfc_sli_validate_fcp_iocb function.
9771 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9772 * FCP iocbs associated with lun specified by tgt_id and lun_id
9773 * parameters
9774 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9775 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9776 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9777 * FCP iocbs associated with virtual port.
9778 * This function returns number of iocbs it failed to abort.
9779 * This function is called with no locks held.
9780 **/
9781 int
9782 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9783 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
9784 {
9785 struct lpfc_hba *phba = vport->phba;
9786 struct lpfc_iocbq *iocbq;
9787 struct lpfc_iocbq *abtsiocb;
9788 IOCB_t *cmd = NULL;
9789 int errcnt = 0, ret_val = 0;
9790 int i;
9791
9792 for (i = 1; i <= phba->sli.last_iotag; i++) {
9793 iocbq = phba->sli.iocbq_lookup[i];
9794
9795 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
9796 abort_cmd) != 0)
9797 continue;
9798
9799 /* issue ABTS for this IOCB based on iotag */
9800 abtsiocb = lpfc_sli_get_iocbq(phba);
9801 if (abtsiocb == NULL) {
9802 errcnt++;
9803 continue;
9804 }
9805
9806 cmd = &iocbq->iocb;
9807 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9808 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
9809 if (phba->sli_rev == LPFC_SLI_REV4)
9810 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9811 else
9812 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
9813 abtsiocb->iocb.ulpLe = 1;
9814 abtsiocb->iocb.ulpClass = cmd->ulpClass;
9815 abtsiocb->vport = phba->pport;
9816
9817 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9818 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
9819 if (iocbq->iocb_flag & LPFC_IO_FCP)
9820 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9821
9822 if (lpfc_is_link_up(phba))
9823 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9824 else
9825 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9826
9827 /* Setup callback routine and issue the command. */
9828 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
9829 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9830 abtsiocb, 0);
9831 if (ret_val == IOCB_ERROR) {
9832 lpfc_sli_release_iocbq(phba, abtsiocb);
9833 errcnt++;
9834 continue;
9835 }
9836 }
9837
9838 return errcnt;
9839 }
9840
9841 /**
9842 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
9843 * @phba: Pointer to HBA context object.
9844 * @cmdiocbq: Pointer to command iocb.
9845 * @rspiocbq: Pointer to response iocb.
9846 *
9847 * This function is the completion handler for iocbs issued using
9848 * lpfc_sli_issue_iocb_wait function. This function is called by the
9849 * ring event handler function without any lock held. This function
9850 * can be called from both worker thread context and interrupt
9851 * context. This function also can be called from other thread which
9852 * cleans up the SLI layer objects.
9853 * This function copy the contents of the response iocb to the
9854 * response iocb memory object provided by the caller of
9855 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9856 * sleeps for the iocb completion.
9857 **/
9858 static void
9859 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9860 struct lpfc_iocbq *cmdiocbq,
9861 struct lpfc_iocbq *rspiocbq)
9862 {
9863 wait_queue_head_t *pdone_q;
9864 unsigned long iflags;
9865 struct lpfc_scsi_buf *lpfc_cmd;
9866
9867 spin_lock_irqsave(&phba->hbalock, iflags);
9868 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9869 if (cmdiocbq->context2 && rspiocbq)
9870 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9871 &rspiocbq->iocb, sizeof(IOCB_t));
9872
9873 /* Set the exchange busy flag for task management commands */
9874 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9875 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9876 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9877 cur_iocbq);
9878 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9879 }
9880
9881 pdone_q = cmdiocbq->context_un.wait_queue;
9882 if (pdone_q)
9883 wake_up(pdone_q);
9884 spin_unlock_irqrestore(&phba->hbalock, iflags);
9885 return;
9886 }
9887
9888 /**
9889 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9890 * @phba: Pointer to HBA context object..
9891 * @piocbq: Pointer to command iocb.
9892 * @flag: Flag to test.
9893 *
9894 * This routine grabs the hbalock and then test the iocb_flag to
9895 * see if the passed in flag is set.
9896 * Returns:
9897 * 1 if flag is set.
9898 * 0 if flag is not set.
9899 **/
9900 static int
9901 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9902 struct lpfc_iocbq *piocbq, uint32_t flag)
9903 {
9904 unsigned long iflags;
9905 int ret;
9906
9907 spin_lock_irqsave(&phba->hbalock, iflags);
9908 ret = piocbq->iocb_flag & flag;
9909 spin_unlock_irqrestore(&phba->hbalock, iflags);
9910 return ret;
9911
9912 }
9913
9914 /**
9915 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
9916 * @phba: Pointer to HBA context object..
9917 * @pring: Pointer to sli ring.
9918 * @piocb: Pointer to command iocb.
9919 * @prspiocbq: Pointer to response iocb.
9920 * @timeout: Timeout in number of seconds.
9921 *
9922 * This function issues the iocb to firmware and waits for the
9923 * iocb to complete. If the iocb command is not
9924 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9925 * Caller should not free the iocb resources if this function
9926 * returns IOCB_TIMEDOUT.
9927 * The function waits for the iocb completion using an
9928 * non-interruptible wait.
9929 * This function will sleep while waiting for iocb completion.
9930 * So, this function should not be called from any context which
9931 * does not allow sleeping. Due to the same reason, this function
9932 * cannot be called with interrupt disabled.
9933 * This function assumes that the iocb completions occur while
9934 * this function sleep. So, this function cannot be called from
9935 * the thread which process iocb completion for this ring.
9936 * This function clears the iocb_flag of the iocb object before
9937 * issuing the iocb and the iocb completion handler sets this
9938 * flag and wakes this thread when the iocb completes.
9939 * The contents of the response iocb will be copied to prspiocbq
9940 * by the completion handler when the command completes.
9941 * This function returns IOCB_SUCCESS when success.
9942 * This function is called with no lock held.
9943 **/
9944 int
9945 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
9946 uint32_t ring_number,
9947 struct lpfc_iocbq *piocb,
9948 struct lpfc_iocbq *prspiocbq,
9949 uint32_t timeout)
9950 {
9951 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9952 long timeleft, timeout_req = 0;
9953 int retval = IOCB_SUCCESS;
9954 uint32_t creg_val;
9955 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9956 /*
9957 * If the caller has provided a response iocbq buffer, then context2
9958 * is NULL or its an error.
9959 */
9960 if (prspiocbq) {
9961 if (piocb->context2)
9962 return IOCB_ERROR;
9963 piocb->context2 = prspiocbq;
9964 }
9965
9966 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9967 piocb->context_un.wait_queue = &done_q;
9968 piocb->iocb_flag &= ~LPFC_IO_WAKE;
9969
9970 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9971 if (lpfc_readl(phba->HCregaddr, &creg_val))
9972 return IOCB_ERROR;
9973 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9974 writel(creg_val, phba->HCregaddr);
9975 readl(phba->HCregaddr); /* flush */
9976 }
9977
9978 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9979 SLI_IOCB_RET_IOCB);
9980 if (retval == IOCB_SUCCESS) {
9981 timeout_req = timeout * HZ;
9982 timeleft = wait_event_timeout(done_q,
9983 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
9984 timeout_req);
9985
9986 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9987 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9988 "0331 IOCB wake signaled\n");
9989 } else if (timeleft == 0) {
9990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9991 "0338 IOCB wait timeout error - no "
9992 "wake response Data x%x\n", timeout);
9993 retval = IOCB_TIMEDOUT;
9994 } else {
9995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9996 "0330 IOCB wake NOT set, "
9997 "Data x%x x%lx\n",
9998 timeout, (timeleft / jiffies));
9999 retval = IOCB_TIMEDOUT;
10000 }
10001 } else if (retval == IOCB_BUSY) {
10002 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10003 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10004 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
10005 return retval;
10006 } else {
10007 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10008 "0332 IOCB wait issue failed, Data x%x\n",
10009 retval);
10010 retval = IOCB_ERROR;
10011 }
10012
10013 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10014 if (lpfc_readl(phba->HCregaddr, &creg_val))
10015 return IOCB_ERROR;
10016 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10017 writel(creg_val, phba->HCregaddr);
10018 readl(phba->HCregaddr); /* flush */
10019 }
10020
10021 if (prspiocbq)
10022 piocb->context2 = NULL;
10023
10024 piocb->context_un.wait_queue = NULL;
10025 piocb->iocb_cmpl = NULL;
10026 return retval;
10027 }
10028
10029 /**
10030 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10031 * @phba: Pointer to HBA context object.
10032 * @pmboxq: Pointer to driver mailbox object.
10033 * @timeout: Timeout in number of seconds.
10034 *
10035 * This function issues the mailbox to firmware and waits for the
10036 * mailbox command to complete. If the mailbox command is not
10037 * completed within timeout seconds, it returns MBX_TIMEOUT.
10038 * The function waits for the mailbox completion using an
10039 * interruptible wait. If the thread is woken up due to a
10040 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10041 * should not free the mailbox resources, if this function returns
10042 * MBX_TIMEOUT.
10043 * This function will sleep while waiting for mailbox completion.
10044 * So, this function should not be called from any context which
10045 * does not allow sleeping. Due to the same reason, this function
10046 * cannot be called with interrupt disabled.
10047 * This function assumes that the mailbox completion occurs while
10048 * this function sleep. So, this function cannot be called from
10049 * the worker thread which processes mailbox completion.
10050 * This function is called in the context of HBA management
10051 * applications.
10052 * This function returns MBX_SUCCESS when successful.
10053 * This function is called with no lock held.
10054 **/
10055 int
10056 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10057 uint32_t timeout)
10058 {
10059 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10060 int retval;
10061 unsigned long flag;
10062
10063 /* The caller must leave context1 empty. */
10064 if (pmboxq->context1)
10065 return MBX_NOT_FINISHED;
10066
10067 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10068 /* setup wake call as IOCB callback */
10069 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10070 /* setup context field to pass wait_queue pointer to wake function */
10071 pmboxq->context1 = &done_q;
10072
10073 /* now issue the command */
10074 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10075 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10076 wait_event_interruptible_timeout(done_q,
10077 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10078 timeout * HZ);
10079
10080 spin_lock_irqsave(&phba->hbalock, flag);
10081 pmboxq->context1 = NULL;
10082 /*
10083 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10084 * else do not free the resources.
10085 */
10086 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10087 retval = MBX_SUCCESS;
10088 lpfc_sli4_swap_str(phba, pmboxq);
10089 } else {
10090 retval = MBX_TIMEOUT;
10091 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10092 }
10093 spin_unlock_irqrestore(&phba->hbalock, flag);
10094 }
10095
10096 return retval;
10097 }
10098
10099 /**
10100 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10101 * @phba: Pointer to HBA context.
10102 *
10103 * This function is called to shutdown the driver's mailbox sub-system.
10104 * It first marks the mailbox sub-system is in a block state to prevent
10105 * the asynchronous mailbox command from issued off the pending mailbox
10106 * command queue. If the mailbox command sub-system shutdown is due to
10107 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10108 * the mailbox sub-system flush routine to forcefully bring down the
10109 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10110 * as with offline or HBA function reset), this routine will wait for the
10111 * outstanding mailbox command to complete before invoking the mailbox
10112 * sub-system flush routine to gracefully bring down mailbox sub-system.
10113 **/
10114 void
10115 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10116 {
10117 struct lpfc_sli *psli = &phba->sli;
10118 unsigned long timeout;
10119
10120 if (mbx_action == LPFC_MBX_NO_WAIT) {
10121 /* delay 100ms for port state */
10122 msleep(100);
10123 lpfc_sli_mbox_sys_flush(phba);
10124 return;
10125 }
10126 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10127
10128 spin_lock_irq(&phba->hbalock);
10129 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10130
10131 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10132 /* Determine how long we might wait for the active mailbox
10133 * command to be gracefully completed by firmware.
10134 */
10135 if (phba->sli.mbox_active)
10136 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10137 phba->sli.mbox_active) *
10138 1000) + jiffies;
10139 spin_unlock_irq(&phba->hbalock);
10140
10141 while (phba->sli.mbox_active) {
10142 /* Check active mailbox complete status every 2ms */
10143 msleep(2);
10144 if (time_after(jiffies, timeout))
10145 /* Timeout, let the mailbox flush routine to
10146 * forcefully release active mailbox command
10147 */
10148 break;
10149 }
10150 } else
10151 spin_unlock_irq(&phba->hbalock);
10152
10153 lpfc_sli_mbox_sys_flush(phba);
10154 }
10155
10156 /**
10157 * lpfc_sli_eratt_read - read sli-3 error attention events
10158 * @phba: Pointer to HBA context.
10159 *
10160 * This function is called to read the SLI3 device error attention registers
10161 * for possible error attention events. The caller must hold the hostlock
10162 * with spin_lock_irq().
10163 *
10164 * This function returns 1 when there is Error Attention in the Host Attention
10165 * Register and returns 0 otherwise.
10166 **/
10167 static int
10168 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10169 {
10170 uint32_t ha_copy;
10171
10172 /* Read chip Host Attention (HA) register */
10173 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10174 goto unplug_err;
10175
10176 if (ha_copy & HA_ERATT) {
10177 /* Read host status register to retrieve error event */
10178 if (lpfc_sli_read_hs(phba))
10179 goto unplug_err;
10180
10181 /* Check if there is a deferred error condition is active */
10182 if ((HS_FFER1 & phba->work_hs) &&
10183 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10184 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10185 phba->hba_flag |= DEFER_ERATT;
10186 /* Clear all interrupt enable conditions */
10187 writel(0, phba->HCregaddr);
10188 readl(phba->HCregaddr);
10189 }
10190
10191 /* Set the driver HA work bitmap */
10192 phba->work_ha |= HA_ERATT;
10193 /* Indicate polling handles this ERATT */
10194 phba->hba_flag |= HBA_ERATT_HANDLED;
10195 return 1;
10196 }
10197 return 0;
10198
10199 unplug_err:
10200 /* Set the driver HS work bitmap */
10201 phba->work_hs |= UNPLUG_ERR;
10202 /* Set the driver HA work bitmap */
10203 phba->work_ha |= HA_ERATT;
10204 /* Indicate polling handles this ERATT */
10205 phba->hba_flag |= HBA_ERATT_HANDLED;
10206 return 1;
10207 }
10208
10209 /**
10210 * lpfc_sli4_eratt_read - read sli-4 error attention events
10211 * @phba: Pointer to HBA context.
10212 *
10213 * This function is called to read the SLI4 device error attention registers
10214 * for possible error attention events. The caller must hold the hostlock
10215 * with spin_lock_irq().
10216 *
10217 * This function returns 1 when there is Error Attention in the Host Attention
10218 * Register and returns 0 otherwise.
10219 **/
10220 static int
10221 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10222 {
10223 uint32_t uerr_sta_hi, uerr_sta_lo;
10224 uint32_t if_type, portsmphr;
10225 struct lpfc_register portstat_reg;
10226
10227 /*
10228 * For now, use the SLI4 device internal unrecoverable error
10229 * registers for error attention. This can be changed later.
10230 */
10231 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10232 switch (if_type) {
10233 case LPFC_SLI_INTF_IF_TYPE_0:
10234 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10235 &uerr_sta_lo) ||
10236 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10237 &uerr_sta_hi)) {
10238 phba->work_hs |= UNPLUG_ERR;
10239 phba->work_ha |= HA_ERATT;
10240 phba->hba_flag |= HBA_ERATT_HANDLED;
10241 return 1;
10242 }
10243 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10244 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10246 "1423 HBA Unrecoverable error: "
10247 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10248 "ue_mask_lo_reg=0x%x, "
10249 "ue_mask_hi_reg=0x%x\n",
10250 uerr_sta_lo, uerr_sta_hi,
10251 phba->sli4_hba.ue_mask_lo,
10252 phba->sli4_hba.ue_mask_hi);
10253 phba->work_status[0] = uerr_sta_lo;
10254 phba->work_status[1] = uerr_sta_hi;
10255 phba->work_ha |= HA_ERATT;
10256 phba->hba_flag |= HBA_ERATT_HANDLED;
10257 return 1;
10258 }
10259 break;
10260 case LPFC_SLI_INTF_IF_TYPE_2:
10261 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10262 &portstat_reg.word0) ||
10263 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10264 &portsmphr)){
10265 phba->work_hs |= UNPLUG_ERR;
10266 phba->work_ha |= HA_ERATT;
10267 phba->hba_flag |= HBA_ERATT_HANDLED;
10268 return 1;
10269 }
10270 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10271 phba->work_status[0] =
10272 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10273 phba->work_status[1] =
10274 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10276 "2885 Port Status Event: "
10277 "port status reg 0x%x, "
10278 "port smphr reg 0x%x, "
10279 "error 1=0x%x, error 2=0x%x\n",
10280 portstat_reg.word0,
10281 portsmphr,
10282 phba->work_status[0],
10283 phba->work_status[1]);
10284 phba->work_ha |= HA_ERATT;
10285 phba->hba_flag |= HBA_ERATT_HANDLED;
10286 return 1;
10287 }
10288 break;
10289 case LPFC_SLI_INTF_IF_TYPE_1:
10290 default:
10291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10292 "2886 HBA Error Attention on unsupported "
10293 "if type %d.", if_type);
10294 return 1;
10295 }
10296
10297 return 0;
10298 }
10299
10300 /**
10301 * lpfc_sli_check_eratt - check error attention events
10302 * @phba: Pointer to HBA context.
10303 *
10304 * This function is called from timer soft interrupt context to check HBA's
10305 * error attention register bit for error attention events.
10306 *
10307 * This function returns 1 when there is Error Attention in the Host Attention
10308 * Register and returns 0 otherwise.
10309 **/
10310 int
10311 lpfc_sli_check_eratt(struct lpfc_hba *phba)
10312 {
10313 uint32_t ha_copy;
10314
10315 /* If somebody is waiting to handle an eratt, don't process it
10316 * here. The brdkill function will do this.
10317 */
10318 if (phba->link_flag & LS_IGNORE_ERATT)
10319 return 0;
10320
10321 /* Check if interrupt handler handles this ERATT */
10322 spin_lock_irq(&phba->hbalock);
10323 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10324 /* Interrupt handler has handled ERATT */
10325 spin_unlock_irq(&phba->hbalock);
10326 return 0;
10327 }
10328
10329 /*
10330 * If there is deferred error attention, do not check for error
10331 * attention
10332 */
10333 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10334 spin_unlock_irq(&phba->hbalock);
10335 return 0;
10336 }
10337
10338 /* If PCI channel is offline, don't process it */
10339 if (unlikely(pci_channel_offline(phba->pcidev))) {
10340 spin_unlock_irq(&phba->hbalock);
10341 return 0;
10342 }
10343
10344 switch (phba->sli_rev) {
10345 case LPFC_SLI_REV2:
10346 case LPFC_SLI_REV3:
10347 /* Read chip Host Attention (HA) register */
10348 ha_copy = lpfc_sli_eratt_read(phba);
10349 break;
10350 case LPFC_SLI_REV4:
10351 /* Read device Uncoverable Error (UERR) registers */
10352 ha_copy = lpfc_sli4_eratt_read(phba);
10353 break;
10354 default:
10355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10356 "0299 Invalid SLI revision (%d)\n",
10357 phba->sli_rev);
10358 ha_copy = 0;
10359 break;
10360 }
10361 spin_unlock_irq(&phba->hbalock);
10362
10363 return ha_copy;
10364 }
10365
10366 /**
10367 * lpfc_intr_state_check - Check device state for interrupt handling
10368 * @phba: Pointer to HBA context.
10369 *
10370 * This inline routine checks whether a device or its PCI slot is in a state
10371 * that the interrupt should be handled.
10372 *
10373 * This function returns 0 if the device or the PCI slot is in a state that
10374 * interrupt should be handled, otherwise -EIO.
10375 */
10376 static inline int
10377 lpfc_intr_state_check(struct lpfc_hba *phba)
10378 {
10379 /* If the pci channel is offline, ignore all the interrupts */
10380 if (unlikely(pci_channel_offline(phba->pcidev)))
10381 return -EIO;
10382
10383 /* Update device level interrupt statistics */
10384 phba->sli.slistat.sli_intr++;
10385
10386 /* Ignore all interrupts during initialization. */
10387 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10388 return -EIO;
10389
10390 return 0;
10391 }
10392
10393 /**
10394 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
10395 * @irq: Interrupt number.
10396 * @dev_id: The device context pointer.
10397 *
10398 * This function is directly called from the PCI layer as an interrupt
10399 * service routine when device with SLI-3 interface spec is enabled with
10400 * MSI-X multi-message interrupt mode and there are slow-path events in
10401 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10402 * interrupt mode, this function is called as part of the device-level
10403 * interrupt handler. When the PCI slot is in error recovery or the HBA
10404 * is undergoing initialization, the interrupt handler will not process
10405 * the interrupt. The link attention and ELS ring attention events are
10406 * handled by the worker thread. The interrupt handler signals the worker
10407 * thread and returns for these events. This function is called without
10408 * any lock held. It gets the hbalock to access and update SLI data
10409 * structures.
10410 *
10411 * This function returns IRQ_HANDLED when interrupt is handled else it
10412 * returns IRQ_NONE.
10413 **/
10414 irqreturn_t
10415 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
10416 {
10417 struct lpfc_hba *phba;
10418 uint32_t ha_copy, hc_copy;
10419 uint32_t work_ha_copy;
10420 unsigned long status;
10421 unsigned long iflag;
10422 uint32_t control;
10423
10424 MAILBOX_t *mbox, *pmbox;
10425 struct lpfc_vport *vport;
10426 struct lpfc_nodelist *ndlp;
10427 struct lpfc_dmabuf *mp;
10428 LPFC_MBOXQ_t *pmb;
10429 int rc;
10430
10431 /*
10432 * Get the driver's phba structure from the dev_id and
10433 * assume the HBA is not interrupting.
10434 */
10435 phba = (struct lpfc_hba *)dev_id;
10436
10437 if (unlikely(!phba))
10438 return IRQ_NONE;
10439
10440 /*
10441 * Stuff needs to be attented to when this function is invoked as an
10442 * individual interrupt handler in MSI-X multi-message interrupt mode
10443 */
10444 if (phba->intr_type == MSIX) {
10445 /* Check device state for handling interrupt */
10446 if (lpfc_intr_state_check(phba))
10447 return IRQ_NONE;
10448 /* Need to read HA REG for slow-path events */
10449 spin_lock_irqsave(&phba->hbalock, iflag);
10450 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10451 goto unplug_error;
10452 /* If somebody is waiting to handle an eratt don't process it
10453 * here. The brdkill function will do this.
10454 */
10455 if (phba->link_flag & LS_IGNORE_ERATT)
10456 ha_copy &= ~HA_ERATT;
10457 /* Check the need for handling ERATT in interrupt handler */
10458 if (ha_copy & HA_ERATT) {
10459 if (phba->hba_flag & HBA_ERATT_HANDLED)
10460 /* ERATT polling has handled ERATT */
10461 ha_copy &= ~HA_ERATT;
10462 else
10463 /* Indicate interrupt handler handles ERATT */
10464 phba->hba_flag |= HBA_ERATT_HANDLED;
10465 }
10466
10467 /*
10468 * If there is deferred error attention, do not check for any
10469 * interrupt.
10470 */
10471 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10472 spin_unlock_irqrestore(&phba->hbalock, iflag);
10473 return IRQ_NONE;
10474 }
10475
10476 /* Clear up only attention source related to slow-path */
10477 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10478 goto unplug_error;
10479
10480 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10481 HC_LAINT_ENA | HC_ERINT_ENA),
10482 phba->HCregaddr);
10483 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10484 phba->HAregaddr);
10485 writel(hc_copy, phba->HCregaddr);
10486 readl(phba->HAregaddr); /* flush */
10487 spin_unlock_irqrestore(&phba->hbalock, iflag);
10488 } else
10489 ha_copy = phba->ha_copy;
10490
10491 work_ha_copy = ha_copy & phba->work_ha_mask;
10492
10493 if (work_ha_copy) {
10494 if (work_ha_copy & HA_LATT) {
10495 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10496 /*
10497 * Turn off Link Attention interrupts
10498 * until CLEAR_LA done
10499 */
10500 spin_lock_irqsave(&phba->hbalock, iflag);
10501 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
10502 if (lpfc_readl(phba->HCregaddr, &control))
10503 goto unplug_error;
10504 control &= ~HC_LAINT_ENA;
10505 writel(control, phba->HCregaddr);
10506 readl(phba->HCregaddr); /* flush */
10507 spin_unlock_irqrestore(&phba->hbalock, iflag);
10508 }
10509 else
10510 work_ha_copy &= ~HA_LATT;
10511 }
10512
10513 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
10514 /*
10515 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10516 * the only slow ring.
10517 */
10518 status = (work_ha_copy &
10519 (HA_RXMASK << (4*LPFC_ELS_RING)));
10520 status >>= (4*LPFC_ELS_RING);
10521 if (status & HA_RXMASK) {
10522 spin_lock_irqsave(&phba->hbalock, iflag);
10523 if (lpfc_readl(phba->HCregaddr, &control))
10524 goto unplug_error;
10525
10526 lpfc_debugfs_slow_ring_trc(phba,
10527 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10528 control, status,
10529 (uint32_t)phba->sli.slistat.sli_intr);
10530
10531 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
10532 lpfc_debugfs_slow_ring_trc(phba,
10533 "ISR Disable ring:"
10534 "pwork:x%x hawork:x%x wait:x%x",
10535 phba->work_ha, work_ha_copy,
10536 (uint32_t)((unsigned long)
10537 &phba->work_waitq));
10538
10539 control &=
10540 ~(HC_R0INT_ENA << LPFC_ELS_RING);
10541 writel(control, phba->HCregaddr);
10542 readl(phba->HCregaddr); /* flush */
10543 }
10544 else {
10545 lpfc_debugfs_slow_ring_trc(phba,
10546 "ISR slow ring: pwork:"
10547 "x%x hawork:x%x wait:x%x",
10548 phba->work_ha, work_ha_copy,
10549 (uint32_t)((unsigned long)
10550 &phba->work_waitq));
10551 }
10552 spin_unlock_irqrestore(&phba->hbalock, iflag);
10553 }
10554 }
10555 spin_lock_irqsave(&phba->hbalock, iflag);
10556 if (work_ha_copy & HA_ERATT) {
10557 if (lpfc_sli_read_hs(phba))
10558 goto unplug_error;
10559 /*
10560 * Check if there is a deferred error condition
10561 * is active
10562 */
10563 if ((HS_FFER1 & phba->work_hs) &&
10564 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10565 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10566 phba->work_hs)) {
10567 phba->hba_flag |= DEFER_ERATT;
10568 /* Clear all interrupt enable conditions */
10569 writel(0, phba->HCregaddr);
10570 readl(phba->HCregaddr);
10571 }
10572 }
10573
10574 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
10575 pmb = phba->sli.mbox_active;
10576 pmbox = &pmb->u.mb;
10577 mbox = phba->mbox;
10578 vport = pmb->vport;
10579
10580 /* First check out the status word */
10581 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10582 if (pmbox->mbxOwner != OWN_HOST) {
10583 spin_unlock_irqrestore(&phba->hbalock, iflag);
10584 /*
10585 * Stray Mailbox Interrupt, mbxCommand <cmd>
10586 * mbxStatus <status>
10587 */
10588 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10589 LOG_SLI,
10590 "(%d):0304 Stray Mailbox "
10591 "Interrupt mbxCommand x%x "
10592 "mbxStatus x%x\n",
10593 (vport ? vport->vpi : 0),
10594 pmbox->mbxCommand,
10595 pmbox->mbxStatus);
10596 /* clear mailbox attention bit */
10597 work_ha_copy &= ~HA_MBATT;
10598 } else {
10599 phba->sli.mbox_active = NULL;
10600 spin_unlock_irqrestore(&phba->hbalock, iflag);
10601 phba->last_completion_time = jiffies;
10602 del_timer(&phba->sli.mbox_tmo);
10603 if (pmb->mbox_cmpl) {
10604 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10605 MAILBOX_CMD_SIZE);
10606 if (pmb->out_ext_byte_len &&
10607 pmb->context2)
10608 lpfc_sli_pcimem_bcopy(
10609 phba->mbox_ext,
10610 pmb->context2,
10611 pmb->out_ext_byte_len);
10612 }
10613 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10614 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10615
10616 lpfc_debugfs_disc_trc(vport,
10617 LPFC_DISC_TRC_MBOX_VPORT,
10618 "MBOX dflt rpi: : "
10619 "status:x%x rpi:x%x",
10620 (uint32_t)pmbox->mbxStatus,
10621 pmbox->un.varWords[0], 0);
10622
10623 if (!pmbox->mbxStatus) {
10624 mp = (struct lpfc_dmabuf *)
10625 (pmb->context1);
10626 ndlp = (struct lpfc_nodelist *)
10627 pmb->context2;
10628
10629 /* Reg_LOGIN of dflt RPI was
10630 * successful. new lets get
10631 * rid of the RPI using the
10632 * same mbox buffer.
10633 */
10634 lpfc_unreg_login(phba,
10635 vport->vpi,
10636 pmbox->un.varWords[0],
10637 pmb);
10638 pmb->mbox_cmpl =
10639 lpfc_mbx_cmpl_dflt_rpi;
10640 pmb->context1 = mp;
10641 pmb->context2 = ndlp;
10642 pmb->vport = vport;
10643 rc = lpfc_sli_issue_mbox(phba,
10644 pmb,
10645 MBX_NOWAIT);
10646 if (rc != MBX_BUSY)
10647 lpfc_printf_log(phba,
10648 KERN_ERR,
10649 LOG_MBOX | LOG_SLI,
10650 "0350 rc should have"
10651 "been MBX_BUSY\n");
10652 if (rc != MBX_NOT_FINISHED)
10653 goto send_current_mbox;
10654 }
10655 }
10656 spin_lock_irqsave(
10657 &phba->pport->work_port_lock,
10658 iflag);
10659 phba->pport->work_port_events &=
10660 ~WORKER_MBOX_TMO;
10661 spin_unlock_irqrestore(
10662 &phba->pport->work_port_lock,
10663 iflag);
10664 lpfc_mbox_cmpl_put(phba, pmb);
10665 }
10666 } else
10667 spin_unlock_irqrestore(&phba->hbalock, iflag);
10668
10669 if ((work_ha_copy & HA_MBATT) &&
10670 (phba->sli.mbox_active == NULL)) {
10671 send_current_mbox:
10672 /* Process next mailbox command if there is one */
10673 do {
10674 rc = lpfc_sli_issue_mbox(phba, NULL,
10675 MBX_NOWAIT);
10676 } while (rc == MBX_NOT_FINISHED);
10677 if (rc != MBX_SUCCESS)
10678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10679 LOG_SLI, "0349 rc should be "
10680 "MBX_SUCCESS\n");
10681 }
10682
10683 spin_lock_irqsave(&phba->hbalock, iflag);
10684 phba->work_ha |= work_ha_copy;
10685 spin_unlock_irqrestore(&phba->hbalock, iflag);
10686 lpfc_worker_wake_up(phba);
10687 }
10688 return IRQ_HANDLED;
10689 unplug_error:
10690 spin_unlock_irqrestore(&phba->hbalock, iflag);
10691 return IRQ_HANDLED;
10692
10693 } /* lpfc_sli_sp_intr_handler */
10694
10695 /**
10696 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
10697 * @irq: Interrupt number.
10698 * @dev_id: The device context pointer.
10699 *
10700 * This function is directly called from the PCI layer as an interrupt
10701 * service routine when device with SLI-3 interface spec is enabled with
10702 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10703 * ring event in the HBA. However, when the device is enabled with either
10704 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10705 * device-level interrupt handler. When the PCI slot is in error recovery
10706 * or the HBA is undergoing initialization, the interrupt handler will not
10707 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10708 * the intrrupt context. This function is called without any lock held.
10709 * It gets the hbalock to access and update SLI data structures.
10710 *
10711 * This function returns IRQ_HANDLED when interrupt is handled else it
10712 * returns IRQ_NONE.
10713 **/
10714 irqreturn_t
10715 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
10716 {
10717 struct lpfc_hba *phba;
10718 uint32_t ha_copy;
10719 unsigned long status;
10720 unsigned long iflag;
10721
10722 /* Get the driver's phba structure from the dev_id and
10723 * assume the HBA is not interrupting.
10724 */
10725 phba = (struct lpfc_hba *) dev_id;
10726
10727 if (unlikely(!phba))
10728 return IRQ_NONE;
10729
10730 /*
10731 * Stuff needs to be attented to when this function is invoked as an
10732 * individual interrupt handler in MSI-X multi-message interrupt mode
10733 */
10734 if (phba->intr_type == MSIX) {
10735 /* Check device state for handling interrupt */
10736 if (lpfc_intr_state_check(phba))
10737 return IRQ_NONE;
10738 /* Need to read HA REG for FCP ring and other ring events */
10739 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10740 return IRQ_HANDLED;
10741 /* Clear up only attention source related to fast-path */
10742 spin_lock_irqsave(&phba->hbalock, iflag);
10743 /*
10744 * If there is deferred error attention, do not check for
10745 * any interrupt.
10746 */
10747 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10748 spin_unlock_irqrestore(&phba->hbalock, iflag);
10749 return IRQ_NONE;
10750 }
10751 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10752 phba->HAregaddr);
10753 readl(phba->HAregaddr); /* flush */
10754 spin_unlock_irqrestore(&phba->hbalock, iflag);
10755 } else
10756 ha_copy = phba->ha_copy;
10757
10758 /*
10759 * Process all events on FCP ring. Take the optimized path for FCP IO.
10760 */
10761 ha_copy &= ~(phba->work_ha_mask);
10762
10763 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10764 status >>= (4*LPFC_FCP_RING);
10765 if (status & HA_RXMASK)
10766 lpfc_sli_handle_fast_ring_event(phba,
10767 &phba->sli.ring[LPFC_FCP_RING],
10768 status);
10769
10770 if (phba->cfg_multi_ring_support == 2) {
10771 /*
10772 * Process all events on extra ring. Take the optimized path
10773 * for extra ring IO.
10774 */
10775 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10776 status >>= (4*LPFC_EXTRA_RING);
10777 if (status & HA_RXMASK) {
10778 lpfc_sli_handle_fast_ring_event(phba,
10779 &phba->sli.ring[LPFC_EXTRA_RING],
10780 status);
10781 }
10782 }
10783 return IRQ_HANDLED;
10784 } /* lpfc_sli_fp_intr_handler */
10785
10786 /**
10787 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
10788 * @irq: Interrupt number.
10789 * @dev_id: The device context pointer.
10790 *
10791 * This function is the HBA device-level interrupt handler to device with
10792 * SLI-3 interface spec, called from the PCI layer when either MSI or
10793 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10794 * requires driver attention. This function invokes the slow-path interrupt
10795 * attention handling function and fast-path interrupt attention handling
10796 * function in turn to process the relevant HBA attention events. This
10797 * function is called without any lock held. It gets the hbalock to access
10798 * and update SLI data structures.
10799 *
10800 * This function returns IRQ_HANDLED when interrupt is handled, else it
10801 * returns IRQ_NONE.
10802 **/
10803 irqreturn_t
10804 lpfc_sli_intr_handler(int irq, void *dev_id)
10805 {
10806 struct lpfc_hba *phba;
10807 irqreturn_t sp_irq_rc, fp_irq_rc;
10808 unsigned long status1, status2;
10809 uint32_t hc_copy;
10810
10811 /*
10812 * Get the driver's phba structure from the dev_id and
10813 * assume the HBA is not interrupting.
10814 */
10815 phba = (struct lpfc_hba *) dev_id;
10816
10817 if (unlikely(!phba))
10818 return IRQ_NONE;
10819
10820 /* Check device state for handling interrupt */
10821 if (lpfc_intr_state_check(phba))
10822 return IRQ_NONE;
10823
10824 spin_lock(&phba->hbalock);
10825 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10826 spin_unlock(&phba->hbalock);
10827 return IRQ_HANDLED;
10828 }
10829
10830 if (unlikely(!phba->ha_copy)) {
10831 spin_unlock(&phba->hbalock);
10832 return IRQ_NONE;
10833 } else if (phba->ha_copy & HA_ERATT) {
10834 if (phba->hba_flag & HBA_ERATT_HANDLED)
10835 /* ERATT polling has handled ERATT */
10836 phba->ha_copy &= ~HA_ERATT;
10837 else
10838 /* Indicate interrupt handler handles ERATT */
10839 phba->hba_flag |= HBA_ERATT_HANDLED;
10840 }
10841
10842 /*
10843 * If there is deferred error attention, do not check for any interrupt.
10844 */
10845 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10846 spin_unlock(&phba->hbalock);
10847 return IRQ_NONE;
10848 }
10849
10850 /* Clear attention sources except link and error attentions */
10851 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10852 spin_unlock(&phba->hbalock);
10853 return IRQ_HANDLED;
10854 }
10855 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10856 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10857 phba->HCregaddr);
10858 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
10859 writel(hc_copy, phba->HCregaddr);
10860 readl(phba->HAregaddr); /* flush */
10861 spin_unlock(&phba->hbalock);
10862
10863 /*
10864 * Invokes slow-path host attention interrupt handling as appropriate.
10865 */
10866
10867 /* status of events with mailbox and link attention */
10868 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10869
10870 /* status of events with ELS ring */
10871 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10872 status2 >>= (4*LPFC_ELS_RING);
10873
10874 if (status1 || (status2 & HA_RXMASK))
10875 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
10876 else
10877 sp_irq_rc = IRQ_NONE;
10878
10879 /*
10880 * Invoke fast-path host attention interrupt handling as appropriate.
10881 */
10882
10883 /* status of events with FCP ring */
10884 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10885 status1 >>= (4*LPFC_FCP_RING);
10886
10887 /* status of events with extra ring */
10888 if (phba->cfg_multi_ring_support == 2) {
10889 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10890 status2 >>= (4*LPFC_EXTRA_RING);
10891 } else
10892 status2 = 0;
10893
10894 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
10895 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
10896 else
10897 fp_irq_rc = IRQ_NONE;
10898
10899 /* Return device-level interrupt handling status */
10900 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
10901 } /* lpfc_sli_intr_handler */
10902
10903 /**
10904 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10905 * @phba: pointer to lpfc hba data structure.
10906 *
10907 * This routine is invoked by the worker thread to process all the pending
10908 * SLI4 FCP abort XRI events.
10909 **/
10910 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10911 {
10912 struct lpfc_cq_event *cq_event;
10913
10914 /* First, declare the fcp xri abort event has been handled */
10915 spin_lock_irq(&phba->hbalock);
10916 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10917 spin_unlock_irq(&phba->hbalock);
10918 /* Now, handle all the fcp xri abort events */
10919 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10920 /* Get the first event from the head of the event queue */
10921 spin_lock_irq(&phba->hbalock);
10922 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10923 cq_event, struct lpfc_cq_event, list);
10924 spin_unlock_irq(&phba->hbalock);
10925 /* Notify aborted XRI for FCP work queue */
10926 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10927 /* Free the event processed back to the free pool */
10928 lpfc_sli4_cq_event_release(phba, cq_event);
10929 }
10930 }
10931
10932 /**
10933 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10934 * @phba: pointer to lpfc hba data structure.
10935 *
10936 * This routine is invoked by the worker thread to process all the pending
10937 * SLI4 els abort xri events.
10938 **/
10939 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10940 {
10941 struct lpfc_cq_event *cq_event;
10942
10943 /* First, declare the els xri abort event has been handled */
10944 spin_lock_irq(&phba->hbalock);
10945 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10946 spin_unlock_irq(&phba->hbalock);
10947 /* Now, handle all the els xri abort events */
10948 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10949 /* Get the first event from the head of the event queue */
10950 spin_lock_irq(&phba->hbalock);
10951 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10952 cq_event, struct lpfc_cq_event, list);
10953 spin_unlock_irq(&phba->hbalock);
10954 /* Notify aborted XRI for ELS work queue */
10955 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10956 /* Free the event processed back to the free pool */
10957 lpfc_sli4_cq_event_release(phba, cq_event);
10958 }
10959 }
10960
10961 /**
10962 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10963 * @phba: pointer to lpfc hba data structure
10964 * @pIocbIn: pointer to the rspiocbq
10965 * @pIocbOut: pointer to the cmdiocbq
10966 * @wcqe: pointer to the complete wcqe
10967 *
10968 * This routine transfers the fields of a command iocbq to a response iocbq
10969 * by copying all the IOCB fields from command iocbq and transferring the
10970 * completion status information from the complete wcqe.
10971 **/
10972 static void
10973 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10974 struct lpfc_iocbq *pIocbIn,
10975 struct lpfc_iocbq *pIocbOut,
10976 struct lpfc_wcqe_complete *wcqe)
10977 {
10978 unsigned long iflags;
10979 uint32_t status;
10980 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10981
10982 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10983 sizeof(struct lpfc_iocbq) - offset);
10984 /* Map WCQE parameters into irspiocb parameters */
10985 status = bf_get(lpfc_wcqe_c_status, wcqe);
10986 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
10987 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10988 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10989 pIocbIn->iocb.un.fcpi.fcpi_parm =
10990 pIocbOut->iocb.un.fcpi.fcpi_parm -
10991 wcqe->total_data_placed;
10992 else
10993 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10994 else {
10995 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10996 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10997 }
10998
10999 /* Convert BG errors for completion status */
11000 if (status == CQE_STATUS_DI_ERROR) {
11001 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11002
11003 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11004 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11005 else
11006 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11007
11008 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11009 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11010 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11011 BGS_GUARD_ERR_MASK;
11012 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11013 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11014 BGS_APPTAG_ERR_MASK;
11015 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11016 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11017 BGS_REFTAG_ERR_MASK;
11018
11019 /* Check to see if there was any good data before the error */
11020 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11021 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11022 BGS_HI_WATER_MARK_PRESENT_MASK;
11023 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11024 wcqe->total_data_placed;
11025 }
11026
11027 /*
11028 * Set ALL the error bits to indicate we don't know what
11029 * type of error it is.
11030 */
11031 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11032 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11033 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11034 BGS_GUARD_ERR_MASK);
11035 }
11036
11037 /* Pick up HBA exchange busy condition */
11038 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11039 spin_lock_irqsave(&phba->hbalock, iflags);
11040 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11041 spin_unlock_irqrestore(&phba->hbalock, iflags);
11042 }
11043 }
11044
11045 /**
11046 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11047 * @phba: Pointer to HBA context object.
11048 * @wcqe: Pointer to work-queue completion queue entry.
11049 *
11050 * This routine handles an ELS work-queue completion event and construct
11051 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11052 * discovery engine to handle.
11053 *
11054 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11055 **/
11056 static struct lpfc_iocbq *
11057 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11058 struct lpfc_iocbq *irspiocbq)
11059 {
11060 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11061 struct lpfc_iocbq *cmdiocbq;
11062 struct lpfc_wcqe_complete *wcqe;
11063 unsigned long iflags;
11064
11065 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11066 spin_lock_irqsave(&pring->ring_lock, iflags);
11067 pring->stats.iocb_event++;
11068 /* Look up the ELS command IOCB and create pseudo response IOCB */
11069 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11070 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11071 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11072
11073 if (unlikely(!cmdiocbq)) {
11074 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11075 "0386 ELS complete with no corresponding "
11076 "cmdiocb: iotag (%d)\n",
11077 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11078 lpfc_sli_release_iocbq(phba, irspiocbq);
11079 return NULL;
11080 }
11081
11082 /* Fake the irspiocbq and copy necessary response information */
11083 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11084
11085 return irspiocbq;
11086 }
11087
11088 /**
11089 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11090 * @phba: Pointer to HBA context object.
11091 * @cqe: Pointer to mailbox completion queue entry.
11092 *
11093 * This routine process a mailbox completion queue entry with asynchrous
11094 * event.
11095 *
11096 * Return: true if work posted to worker thread, otherwise false.
11097 **/
11098 static bool
11099 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11100 {
11101 struct lpfc_cq_event *cq_event;
11102 unsigned long iflags;
11103
11104 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11105 "0392 Async Event: word0:x%x, word1:x%x, "
11106 "word2:x%x, word3:x%x\n", mcqe->word0,
11107 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11108
11109 /* Allocate a new internal CQ_EVENT entry */
11110 cq_event = lpfc_sli4_cq_event_alloc(phba);
11111 if (!cq_event) {
11112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11113 "0394 Failed to allocate CQ_EVENT entry\n");
11114 return false;
11115 }
11116
11117 /* Move the CQE into an asynchronous event entry */
11118 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11119 spin_lock_irqsave(&phba->hbalock, iflags);
11120 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11121 /* Set the async event flag */
11122 phba->hba_flag |= ASYNC_EVENT;
11123 spin_unlock_irqrestore(&phba->hbalock, iflags);
11124
11125 return true;
11126 }
11127
11128 /**
11129 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11130 * @phba: Pointer to HBA context object.
11131 * @cqe: Pointer to mailbox completion queue entry.
11132 *
11133 * This routine process a mailbox completion queue entry with mailbox
11134 * completion event.
11135 *
11136 * Return: true if work posted to worker thread, otherwise false.
11137 **/
11138 static bool
11139 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11140 {
11141 uint32_t mcqe_status;
11142 MAILBOX_t *mbox, *pmbox;
11143 struct lpfc_mqe *mqe;
11144 struct lpfc_vport *vport;
11145 struct lpfc_nodelist *ndlp;
11146 struct lpfc_dmabuf *mp;
11147 unsigned long iflags;
11148 LPFC_MBOXQ_t *pmb;
11149 bool workposted = false;
11150 int rc;
11151
11152 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11153 if (!bf_get(lpfc_trailer_completed, mcqe))
11154 goto out_no_mqe_complete;
11155
11156 /* Get the reference to the active mbox command */
11157 spin_lock_irqsave(&phba->hbalock, iflags);
11158 pmb = phba->sli.mbox_active;
11159 if (unlikely(!pmb)) {
11160 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11161 "1832 No pending MBOX command to handle\n");
11162 spin_unlock_irqrestore(&phba->hbalock, iflags);
11163 goto out_no_mqe_complete;
11164 }
11165 spin_unlock_irqrestore(&phba->hbalock, iflags);
11166 mqe = &pmb->u.mqe;
11167 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11168 mbox = phba->mbox;
11169 vport = pmb->vport;
11170
11171 /* Reset heartbeat timer */
11172 phba->last_completion_time = jiffies;
11173 del_timer(&phba->sli.mbox_tmo);
11174
11175 /* Move mbox data to caller's mailbox region, do endian swapping */
11176 if (pmb->mbox_cmpl && mbox)
11177 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11178
11179 /*
11180 * For mcqe errors, conditionally move a modified error code to
11181 * the mbox so that the error will not be missed.
11182 */
11183 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11184 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11185 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11186 bf_set(lpfc_mqe_status, mqe,
11187 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11188 }
11189 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11190 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11191 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11192 "MBOX dflt rpi: status:x%x rpi:x%x",
11193 mcqe_status,
11194 pmbox->un.varWords[0], 0);
11195 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11196 mp = (struct lpfc_dmabuf *)(pmb->context1);
11197 ndlp = (struct lpfc_nodelist *)pmb->context2;
11198 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11199 * RID of the PPI using the same mbox buffer.
11200 */
11201 lpfc_unreg_login(phba, vport->vpi,
11202 pmbox->un.varWords[0], pmb);
11203 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11204 pmb->context1 = mp;
11205 pmb->context2 = ndlp;
11206 pmb->vport = vport;
11207 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11208 if (rc != MBX_BUSY)
11209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11210 LOG_SLI, "0385 rc should "
11211 "have been MBX_BUSY\n");
11212 if (rc != MBX_NOT_FINISHED)
11213 goto send_current_mbox;
11214 }
11215 }
11216 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11217 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11218 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11219
11220 /* There is mailbox completion work to do */
11221 spin_lock_irqsave(&phba->hbalock, iflags);
11222 __lpfc_mbox_cmpl_put(phba, pmb);
11223 phba->work_ha |= HA_MBATT;
11224 spin_unlock_irqrestore(&phba->hbalock, iflags);
11225 workposted = true;
11226
11227 send_current_mbox:
11228 spin_lock_irqsave(&phba->hbalock, iflags);
11229 /* Release the mailbox command posting token */
11230 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11231 /* Setting active mailbox pointer need to be in sync to flag clear */
11232 phba->sli.mbox_active = NULL;
11233 spin_unlock_irqrestore(&phba->hbalock, iflags);
11234 /* Wake up worker thread to post the next pending mailbox command */
11235 lpfc_worker_wake_up(phba);
11236 out_no_mqe_complete:
11237 if (bf_get(lpfc_trailer_consumed, mcqe))
11238 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11239 return workposted;
11240 }
11241
11242 /**
11243 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11244 * @phba: Pointer to HBA context object.
11245 * @cqe: Pointer to mailbox completion queue entry.
11246 *
11247 * This routine process a mailbox completion queue entry, it invokes the
11248 * proper mailbox complete handling or asynchrous event handling routine
11249 * according to the MCQE's async bit.
11250 *
11251 * Return: true if work posted to worker thread, otherwise false.
11252 **/
11253 static bool
11254 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11255 {
11256 struct lpfc_mcqe mcqe;
11257 bool workposted;
11258
11259 /* Copy the mailbox MCQE and convert endian order as needed */
11260 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11261
11262 /* Invoke the proper event handling routine */
11263 if (!bf_get(lpfc_trailer_async, &mcqe))
11264 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11265 else
11266 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11267 return workposted;
11268 }
11269
11270 /**
11271 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11272 * @phba: Pointer to HBA context object.
11273 * @cq: Pointer to associated CQ
11274 * @wcqe: Pointer to work-queue completion queue entry.
11275 *
11276 * This routine handles an ELS work-queue completion event.
11277 *
11278 * Return: true if work posted to worker thread, otherwise false.
11279 **/
11280 static bool
11281 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11282 struct lpfc_wcqe_complete *wcqe)
11283 {
11284 struct lpfc_iocbq *irspiocbq;
11285 unsigned long iflags;
11286 struct lpfc_sli_ring *pring = cq->pring;
11287
11288 /* Get an irspiocbq for later ELS response processing use */
11289 irspiocbq = lpfc_sli_get_iocbq(phba);
11290 if (!irspiocbq) {
11291 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11292 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11293 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11294 pring->txq_cnt, phba->iocb_cnt,
11295 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11296 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
11297 return false;
11298 }
11299
11300 /* Save off the slow-path queue event for work thread to process */
11301 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
11302 spin_lock_irqsave(&phba->hbalock, iflags);
11303 list_add_tail(&irspiocbq->cq_event.list,
11304 &phba->sli4_hba.sp_queue_event);
11305 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11306 spin_unlock_irqrestore(&phba->hbalock, iflags);
11307
11308 return true;
11309 }
11310
11311 /**
11312 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11313 * @phba: Pointer to HBA context object.
11314 * @wcqe: Pointer to work-queue completion queue entry.
11315 *
11316 * This routine handles slow-path WQ entry comsumed event by invoking the
11317 * proper WQ release routine to the slow-path WQ.
11318 **/
11319 static void
11320 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11321 struct lpfc_wcqe_release *wcqe)
11322 {
11323 /* sanity check on queue memory */
11324 if (unlikely(!phba->sli4_hba.els_wq))
11325 return;
11326 /* Check for the slow-path ELS work queue */
11327 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11328 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11329 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11330 else
11331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11332 "2579 Slow-path wqe consume event carries "
11333 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11334 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11335 phba->sli4_hba.els_wq->queue_id);
11336 }
11337
11338 /**
11339 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11340 * @phba: Pointer to HBA context object.
11341 * @cq: Pointer to a WQ completion queue.
11342 * @wcqe: Pointer to work-queue completion queue entry.
11343 *
11344 * This routine handles an XRI abort event.
11345 *
11346 * Return: true if work posted to worker thread, otherwise false.
11347 **/
11348 static bool
11349 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11350 struct lpfc_queue *cq,
11351 struct sli4_wcqe_xri_aborted *wcqe)
11352 {
11353 bool workposted = false;
11354 struct lpfc_cq_event *cq_event;
11355 unsigned long iflags;
11356
11357 /* Allocate a new internal CQ_EVENT entry */
11358 cq_event = lpfc_sli4_cq_event_alloc(phba);
11359 if (!cq_event) {
11360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11361 "0602 Failed to allocate CQ_EVENT entry\n");
11362 return false;
11363 }
11364
11365 /* Move the CQE into the proper xri abort event list */
11366 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11367 switch (cq->subtype) {
11368 case LPFC_FCP:
11369 spin_lock_irqsave(&phba->hbalock, iflags);
11370 list_add_tail(&cq_event->list,
11371 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11372 /* Set the fcp xri abort event flag */
11373 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11374 spin_unlock_irqrestore(&phba->hbalock, iflags);
11375 workposted = true;
11376 break;
11377 case LPFC_ELS:
11378 spin_lock_irqsave(&phba->hbalock, iflags);
11379 list_add_tail(&cq_event->list,
11380 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11381 /* Set the els xri abort event flag */
11382 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11383 spin_unlock_irqrestore(&phba->hbalock, iflags);
11384 workposted = true;
11385 break;
11386 default:
11387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11388 "0603 Invalid work queue CQE subtype (x%x)\n",
11389 cq->subtype);
11390 workposted = false;
11391 break;
11392 }
11393 return workposted;
11394 }
11395
11396 /**
11397 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11398 * @phba: Pointer to HBA context object.
11399 * @rcqe: Pointer to receive-queue completion queue entry.
11400 *
11401 * This routine process a receive-queue completion queue entry.
11402 *
11403 * Return: true if work posted to worker thread, otherwise false.
11404 **/
11405 static bool
11406 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11407 {
11408 bool workposted = false;
11409 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11410 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11411 struct hbq_dmabuf *dma_buf;
11412 uint32_t status, rq_id;
11413 unsigned long iflags;
11414
11415 /* sanity check on queue memory */
11416 if (unlikely(!hrq) || unlikely(!drq))
11417 return workposted;
11418
11419 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11420 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11421 else
11422 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11423 if (rq_id != hrq->queue_id)
11424 goto out;
11425
11426 status = bf_get(lpfc_rcqe_status, rcqe);
11427 switch (status) {
11428 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11430 "2537 Receive Frame Truncated!!\n");
11431 hrq->RQ_buf_trunc++;
11432 case FC_STATUS_RQ_SUCCESS:
11433 lpfc_sli4_rq_release(hrq, drq);
11434 spin_lock_irqsave(&phba->hbalock, iflags);
11435 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11436 if (!dma_buf) {
11437 hrq->RQ_no_buf_found++;
11438 spin_unlock_irqrestore(&phba->hbalock, iflags);
11439 goto out;
11440 }
11441 hrq->RQ_rcv_buf++;
11442 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11443 /* save off the frame for the word thread to process */
11444 list_add_tail(&dma_buf->cq_event.list,
11445 &phba->sli4_hba.sp_queue_event);
11446 /* Frame received */
11447 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11448 spin_unlock_irqrestore(&phba->hbalock, iflags);
11449 workposted = true;
11450 break;
11451 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11452 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11453 hrq->RQ_no_posted_buf++;
11454 /* Post more buffers if possible */
11455 spin_lock_irqsave(&phba->hbalock, iflags);
11456 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11457 spin_unlock_irqrestore(&phba->hbalock, iflags);
11458 workposted = true;
11459 break;
11460 }
11461 out:
11462 return workposted;
11463 }
11464
11465 /**
11466 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11467 * @phba: Pointer to HBA context object.
11468 * @cq: Pointer to the completion queue.
11469 * @wcqe: Pointer to a completion queue entry.
11470 *
11471 * This routine process a slow-path work-queue or receive queue completion queue
11472 * entry.
11473 *
11474 * Return: true if work posted to worker thread, otherwise false.
11475 **/
11476 static bool
11477 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11478 struct lpfc_cqe *cqe)
11479 {
11480 struct lpfc_cqe cqevt;
11481 bool workposted = false;
11482
11483 /* Copy the work queue CQE and convert endian order if needed */
11484 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
11485
11486 /* Check and process for different type of WCQE and dispatch */
11487 switch (bf_get(lpfc_cqe_code, &cqevt)) {
11488 case CQE_CODE_COMPL_WQE:
11489 /* Process the WQ/RQ complete event */
11490 phba->last_completion_time = jiffies;
11491 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
11492 (struct lpfc_wcqe_complete *)&cqevt);
11493 break;
11494 case CQE_CODE_RELEASE_WQE:
11495 /* Process the WQ release event */
11496 lpfc_sli4_sp_handle_rel_wcqe(phba,
11497 (struct lpfc_wcqe_release *)&cqevt);
11498 break;
11499 case CQE_CODE_XRI_ABORTED:
11500 /* Process the WQ XRI abort event */
11501 phba->last_completion_time = jiffies;
11502 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11503 (struct sli4_wcqe_xri_aborted *)&cqevt);
11504 break;
11505 case CQE_CODE_RECEIVE:
11506 case CQE_CODE_RECEIVE_V1:
11507 /* Process the RQ event */
11508 phba->last_completion_time = jiffies;
11509 workposted = lpfc_sli4_sp_handle_rcqe(phba,
11510 (struct lpfc_rcqe *)&cqevt);
11511 break;
11512 default:
11513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11514 "0388 Not a valid WCQE code: x%x\n",
11515 bf_get(lpfc_cqe_code, &cqevt));
11516 break;
11517 }
11518 return workposted;
11519 }
11520
11521 /**
11522 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11523 * @phba: Pointer to HBA context object.
11524 * @eqe: Pointer to fast-path event queue entry.
11525 *
11526 * This routine process a event queue entry from the slow-path event queue.
11527 * It will check the MajorCode and MinorCode to determine this is for a
11528 * completion event on a completion queue, if not, an error shall be logged
11529 * and just return. Otherwise, it will get to the corresponding completion
11530 * queue and process all the entries on that completion queue, rearm the
11531 * completion queue, and then return.
11532 *
11533 **/
11534 static void
11535 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11536 struct lpfc_queue *speq)
11537 {
11538 struct lpfc_queue *cq = NULL, *childq;
11539 struct lpfc_cqe *cqe;
11540 bool workposted = false;
11541 int ecount = 0;
11542 uint16_t cqid;
11543
11544 /* Get the reference to the corresponding CQ */
11545 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11546
11547 list_for_each_entry(childq, &speq->child_list, list) {
11548 if (childq->queue_id == cqid) {
11549 cq = childq;
11550 break;
11551 }
11552 }
11553 if (unlikely(!cq)) {
11554 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11556 "0365 Slow-path CQ identifier "
11557 "(%d) does not exist\n", cqid);
11558 return;
11559 }
11560
11561 /* Process all the entries to the CQ */
11562 switch (cq->type) {
11563 case LPFC_MCQ:
11564 while ((cqe = lpfc_sli4_cq_get(cq))) {
11565 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
11566 if (!(++ecount % cq->entry_repost))
11567 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11568 cq->CQ_mbox++;
11569 }
11570 break;
11571 case LPFC_WCQ:
11572 while ((cqe = lpfc_sli4_cq_get(cq))) {
11573 if (cq->subtype == LPFC_FCP)
11574 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11575 cqe);
11576 else
11577 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11578 cqe);
11579 if (!(++ecount % cq->entry_repost))
11580 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11581 }
11582
11583 /* Track the max number of CQEs processed in 1 EQ */
11584 if (ecount > cq->CQ_max_cqe)
11585 cq->CQ_max_cqe = ecount;
11586 break;
11587 default:
11588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11589 "0370 Invalid completion queue type (%d)\n",
11590 cq->type);
11591 return;
11592 }
11593
11594 /* Catch the no cq entry condition, log an error */
11595 if (unlikely(ecount == 0))
11596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11597 "0371 No entry from the CQ: identifier "
11598 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11599
11600 /* In any case, flash and re-arm the RCQ */
11601 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11602
11603 /* wake up worker thread if there are works to be done */
11604 if (workposted)
11605 lpfc_worker_wake_up(phba);
11606 }
11607
11608 /**
11609 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11610 * @phba: Pointer to HBA context object.
11611 * @cq: Pointer to associated CQ
11612 * @wcqe: Pointer to work-queue completion queue entry.
11613 *
11614 * This routine process a fast-path work queue completion entry from fast-path
11615 * event queue for FCP command response completion.
11616 **/
11617 static void
11618 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11619 struct lpfc_wcqe_complete *wcqe)
11620 {
11621 struct lpfc_sli_ring *pring = cq->pring;
11622 struct lpfc_iocbq *cmdiocbq;
11623 struct lpfc_iocbq irspiocbq;
11624 unsigned long iflags;
11625
11626 /* Check for response status */
11627 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11628 /* If resource errors reported from HBA, reduce queue
11629 * depth of the SCSI device.
11630 */
11631 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11632 IOSTAT_LOCAL_REJECT) &&
11633 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11634 phba->lpfc_rampdown_queue_depth(phba);
11635 }
11636 /* Log the error status */
11637 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11638 "0373 FCP complete error: status=x%x, "
11639 "hw_status=x%x, total_data_specified=%d, "
11640 "parameter=x%x, word3=x%x\n",
11641 bf_get(lpfc_wcqe_c_status, wcqe),
11642 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11643 wcqe->total_data_placed, wcqe->parameter,
11644 wcqe->word3);
11645 }
11646
11647 /* Look up the FCP command IOCB and create pseudo response IOCB */
11648 spin_lock_irqsave(&pring->ring_lock, iflags);
11649 pring->stats.iocb_event++;
11650 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11651 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11652 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11653 if (unlikely(!cmdiocbq)) {
11654 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11655 "0374 FCP complete with no corresponding "
11656 "cmdiocb: iotag (%d)\n",
11657 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11658 return;
11659 }
11660 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11661 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11662 "0375 FCP cmdiocb not callback function "
11663 "iotag: (%d)\n",
11664 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11665 return;
11666 }
11667
11668 /* Fake the irspiocb and copy necessary response information */
11669 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
11670
11671 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11672 spin_lock_irqsave(&phba->hbalock, iflags);
11673 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11674 spin_unlock_irqrestore(&phba->hbalock, iflags);
11675 }
11676
11677 /* Pass the cmd_iocb and the rsp state to the upper layer */
11678 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11679 }
11680
11681 /**
11682 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11683 * @phba: Pointer to HBA context object.
11684 * @cq: Pointer to completion queue.
11685 * @wcqe: Pointer to work-queue completion queue entry.
11686 *
11687 * This routine handles an fast-path WQ entry comsumed event by invoking the
11688 * proper WQ release routine to the slow-path WQ.
11689 **/
11690 static void
11691 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11692 struct lpfc_wcqe_release *wcqe)
11693 {
11694 struct lpfc_queue *childwq;
11695 bool wqid_matched = false;
11696 uint16_t fcp_wqid;
11697
11698 /* Check for fast-path FCP work queue release */
11699 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11700 list_for_each_entry(childwq, &cq->child_list, list) {
11701 if (childwq->queue_id == fcp_wqid) {
11702 lpfc_sli4_wq_release(childwq,
11703 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11704 wqid_matched = true;
11705 break;
11706 }
11707 }
11708 /* Report warning log message if no match found */
11709 if (wqid_matched != true)
11710 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11711 "2580 Fast-path wqe consume event carries "
11712 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11713 }
11714
11715 /**
11716 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11717 * @cq: Pointer to the completion queue.
11718 * @eqe: Pointer to fast-path completion queue entry.
11719 *
11720 * This routine process a fast-path work queue completion entry from fast-path
11721 * event queue for FCP command response completion.
11722 **/
11723 static int
11724 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11725 struct lpfc_cqe *cqe)
11726 {
11727 struct lpfc_wcqe_release wcqe;
11728 bool workposted = false;
11729
11730 /* Copy the work queue CQE and convert endian order if needed */
11731 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11732
11733 /* Check and process for different type of WCQE and dispatch */
11734 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11735 case CQE_CODE_COMPL_WQE:
11736 cq->CQ_wq++;
11737 /* Process the WQ complete event */
11738 phba->last_completion_time = jiffies;
11739 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
11740 (struct lpfc_wcqe_complete *)&wcqe);
11741 break;
11742 case CQE_CODE_RELEASE_WQE:
11743 cq->CQ_release_wqe++;
11744 /* Process the WQ release event */
11745 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11746 (struct lpfc_wcqe_release *)&wcqe);
11747 break;
11748 case CQE_CODE_XRI_ABORTED:
11749 cq->CQ_xri_aborted++;
11750 /* Process the WQ XRI abort event */
11751 phba->last_completion_time = jiffies;
11752 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11753 (struct sli4_wcqe_xri_aborted *)&wcqe);
11754 break;
11755 default:
11756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11757 "0144 Not a valid WCQE code: x%x\n",
11758 bf_get(lpfc_wcqe_c_code, &wcqe));
11759 break;
11760 }
11761 return workposted;
11762 }
11763
11764 /**
11765 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
11766 * @phba: Pointer to HBA context object.
11767 * @eqe: Pointer to fast-path event queue entry.
11768 *
11769 * This routine process a event queue entry from the fast-path event queue.
11770 * It will check the MajorCode and MinorCode to determine this is for a
11771 * completion event on a completion queue, if not, an error shall be logged
11772 * and just return. Otherwise, it will get to the corresponding completion
11773 * queue and process all the entries on the completion queue, rearm the
11774 * completion queue, and then return.
11775 **/
11776 static void
11777 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11778 uint32_t qidx)
11779 {
11780 struct lpfc_queue *cq;
11781 struct lpfc_cqe *cqe;
11782 bool workposted = false;
11783 uint16_t cqid;
11784 int ecount = 0;
11785
11786 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
11787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11788 "0366 Not a valid completion "
11789 "event: majorcode=x%x, minorcode=x%x\n",
11790 bf_get_le32(lpfc_eqe_major_code, eqe),
11791 bf_get_le32(lpfc_eqe_minor_code, eqe));
11792 return;
11793 }
11794
11795 /* Get the reference to the corresponding CQ */
11796 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11797
11798 /* Check if this is a Slow path event */
11799 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11800 lpfc_sli4_sp_handle_eqe(phba, eqe,
11801 phba->sli4_hba.hba_eq[qidx]);
11802 return;
11803 }
11804
11805 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11806 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11807 "3146 Fast-path completion queues "
11808 "does not exist\n");
11809 return;
11810 }
11811 cq = phba->sli4_hba.fcp_cq[qidx];
11812 if (unlikely(!cq)) {
11813 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11815 "0367 Fast-path completion queue "
11816 "(%d) does not exist\n", qidx);
11817 return;
11818 }
11819
11820 if (unlikely(cqid != cq->queue_id)) {
11821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11822 "0368 Miss-matched fast-path completion "
11823 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11824 cqid, cq->queue_id);
11825 return;
11826 }
11827
11828 /* Process all the entries to the CQ */
11829 while ((cqe = lpfc_sli4_cq_get(cq))) {
11830 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
11831 if (!(++ecount % cq->entry_repost))
11832 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11833 }
11834
11835 /* Track the max number of CQEs processed in 1 EQ */
11836 if (ecount > cq->CQ_max_cqe)
11837 cq->CQ_max_cqe = ecount;
11838
11839 /* Catch the no cq entry condition */
11840 if (unlikely(ecount == 0))
11841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11842 "0369 No entry from fast-path completion "
11843 "queue fcpcqid=%d\n", cq->queue_id);
11844
11845 /* In any case, flash and re-arm the CQ */
11846 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11847
11848 /* wake up worker thread if there are works to be done */
11849 if (workposted)
11850 lpfc_worker_wake_up(phba);
11851 }
11852
11853 static void
11854 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11855 {
11856 struct lpfc_eqe *eqe;
11857
11858 /* walk all the EQ entries and drop on the floor */
11859 while ((eqe = lpfc_sli4_eq_get(eq)))
11860 ;
11861
11862 /* Clear and re-arm the EQ */
11863 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11864 }
11865
11866 /**
11867 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
11868 * @irq: Interrupt number.
11869 * @dev_id: The device context pointer.
11870 *
11871 * This function is directly called from the PCI layer as an interrupt
11872 * service routine when device with SLI-4 interface spec is enabled with
11873 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11874 * ring event in the HBA. However, when the device is enabled with either
11875 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11876 * device-level interrupt handler. When the PCI slot is in error recovery
11877 * or the HBA is undergoing initialization, the interrupt handler will not
11878 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11879 * the intrrupt context. This function is called without any lock held.
11880 * It gets the hbalock to access and update SLI data structures. Note that,
11881 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11882 * equal to that of FCP CQ index.
11883 *
11884 * The link attention and ELS ring attention events are handled
11885 * by the worker thread. The interrupt handler signals the worker thread
11886 * and returns for these events. This function is called without any lock
11887 * held. It gets the hbalock to access and update SLI data structures.
11888 *
11889 * This function returns IRQ_HANDLED when interrupt is handled else it
11890 * returns IRQ_NONE.
11891 **/
11892 irqreturn_t
11893 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
11894 {
11895 struct lpfc_hba *phba;
11896 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11897 struct lpfc_queue *fpeq;
11898 struct lpfc_eqe *eqe;
11899 unsigned long iflag;
11900 int ecount = 0;
11901 uint32_t fcp_eqidx;
11902
11903 /* Get the driver's phba structure from the dev_id */
11904 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11905 phba = fcp_eq_hdl->phba;
11906 fcp_eqidx = fcp_eq_hdl->idx;
11907
11908 if (unlikely(!phba))
11909 return IRQ_NONE;
11910 if (unlikely(!phba->sli4_hba.hba_eq))
11911 return IRQ_NONE;
11912
11913 /* Get to the EQ struct associated with this vector */
11914 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
11915 if (unlikely(!fpeq))
11916 return IRQ_NONE;
11917
11918 if (lpfc_fcp_look_ahead) {
11919 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11920 lpfc_sli4_eq_clr_intr(fpeq);
11921 else {
11922 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11923 return IRQ_NONE;
11924 }
11925 }
11926
11927 /* Check device state for handling interrupt */
11928 if (unlikely(lpfc_intr_state_check(phba))) {
11929 fpeq->EQ_badstate++;
11930 /* Check again for link_state with lock held */
11931 spin_lock_irqsave(&phba->hbalock, iflag);
11932 if (phba->link_state < LPFC_LINK_DOWN)
11933 /* Flush, clear interrupt, and rearm the EQ */
11934 lpfc_sli4_eq_flush(phba, fpeq);
11935 spin_unlock_irqrestore(&phba->hbalock, iflag);
11936 if (lpfc_fcp_look_ahead)
11937 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11938 return IRQ_NONE;
11939 }
11940
11941 /*
11942 * Process all the event on FCP fast-path EQ
11943 */
11944 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11945 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
11946 if (!(++ecount % fpeq->entry_repost))
11947 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11948 fpeq->EQ_processed++;
11949 }
11950
11951 /* Track the max number of EQEs processed in 1 intr */
11952 if (ecount > fpeq->EQ_max_eqe)
11953 fpeq->EQ_max_eqe = ecount;
11954
11955 /* Always clear and re-arm the fast-path EQ */
11956 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11957
11958 if (unlikely(ecount == 0)) {
11959 fpeq->EQ_no_entry++;
11960
11961 if (lpfc_fcp_look_ahead) {
11962 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11963 return IRQ_NONE;
11964 }
11965
11966 if (phba->intr_type == MSIX)
11967 /* MSI-X treated interrupt served as no EQ share INT */
11968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11969 "0358 MSI-X interrupt with no EQE\n");
11970 else
11971 /* Non MSI-X treated on interrupt as EQ share INT */
11972 return IRQ_NONE;
11973 }
11974
11975 if (lpfc_fcp_look_ahead)
11976 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11977 return IRQ_HANDLED;
11978 } /* lpfc_sli4_fp_intr_handler */
11979
11980 /**
11981 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11982 * @irq: Interrupt number.
11983 * @dev_id: The device context pointer.
11984 *
11985 * This function is the device-level interrupt handler to device with SLI-4
11986 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11987 * interrupt mode is enabled and there is an event in the HBA which requires
11988 * driver attention. This function invokes the slow-path interrupt attention
11989 * handling function and fast-path interrupt attention handling function in
11990 * turn to process the relevant HBA attention events. This function is called
11991 * without any lock held. It gets the hbalock to access and update SLI data
11992 * structures.
11993 *
11994 * This function returns IRQ_HANDLED when interrupt is handled, else it
11995 * returns IRQ_NONE.
11996 **/
11997 irqreturn_t
11998 lpfc_sli4_intr_handler(int irq, void *dev_id)
11999 {
12000 struct lpfc_hba *phba;
12001 irqreturn_t hba_irq_rc;
12002 bool hba_handled = false;
12003 uint32_t fcp_eqidx;
12004
12005 /* Get the driver's phba structure from the dev_id */
12006 phba = (struct lpfc_hba *)dev_id;
12007
12008 if (unlikely(!phba))
12009 return IRQ_NONE;
12010
12011 /*
12012 * Invoke fast-path host attention interrupt handling as appropriate.
12013 */
12014 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12015 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12016 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12017 if (hba_irq_rc == IRQ_HANDLED)
12018 hba_handled |= true;
12019 }
12020
12021 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12022 } /* lpfc_sli4_intr_handler */
12023
12024 /**
12025 * lpfc_sli4_queue_free - free a queue structure and associated memory
12026 * @queue: The queue structure to free.
12027 *
12028 * This function frees a queue structure and the DMAable memory used for
12029 * the host resident queue. This function must be called after destroying the
12030 * queue on the HBA.
12031 **/
12032 void
12033 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12034 {
12035 struct lpfc_dmabuf *dmabuf;
12036
12037 if (!queue)
12038 return;
12039
12040 while (!list_empty(&queue->page_list)) {
12041 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12042 list);
12043 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12044 dmabuf->virt, dmabuf->phys);
12045 kfree(dmabuf);
12046 }
12047 kfree(queue);
12048 return;
12049 }
12050
12051 /**
12052 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12053 * @phba: The HBA that this queue is being created on.
12054 * @entry_size: The size of each queue entry for this queue.
12055 * @entry count: The number of entries that this queue will handle.
12056 *
12057 * This function allocates a queue structure and the DMAable memory used for
12058 * the host resident queue. This function must be called before creating the
12059 * queue on the HBA.
12060 **/
12061 struct lpfc_queue *
12062 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12063 uint32_t entry_count)
12064 {
12065 struct lpfc_queue *queue;
12066 struct lpfc_dmabuf *dmabuf;
12067 int x, total_qe_count;
12068 void *dma_pointer;
12069 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12070
12071 if (!phba->sli4_hba.pc_sli4_params.supported)
12072 hw_page_size = SLI4_PAGE_SIZE;
12073
12074 queue = kzalloc(sizeof(struct lpfc_queue) +
12075 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12076 if (!queue)
12077 return NULL;
12078 queue->page_count = (ALIGN(entry_size * entry_count,
12079 hw_page_size))/hw_page_size;
12080 INIT_LIST_HEAD(&queue->list);
12081 INIT_LIST_HEAD(&queue->page_list);
12082 INIT_LIST_HEAD(&queue->child_list);
12083 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12084 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12085 if (!dmabuf)
12086 goto out_fail;
12087 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12088 hw_page_size, &dmabuf->phys,
12089 GFP_KERNEL);
12090 if (!dmabuf->virt) {
12091 kfree(dmabuf);
12092 goto out_fail;
12093 }
12094 memset(dmabuf->virt, 0, hw_page_size);
12095 dmabuf->buffer_tag = x;
12096 list_add_tail(&dmabuf->list, &queue->page_list);
12097 /* initialize queue's entry array */
12098 dma_pointer = dmabuf->virt;
12099 for (; total_qe_count < entry_count &&
12100 dma_pointer < (hw_page_size + dmabuf->virt);
12101 total_qe_count++, dma_pointer += entry_size) {
12102 queue->qe[total_qe_count].address = dma_pointer;
12103 }
12104 }
12105 queue->entry_size = entry_size;
12106 queue->entry_count = entry_count;
12107
12108 /*
12109 * entry_repost is calculated based on the number of entries in the
12110 * queue. This works out except for RQs. If buffers are NOT initially
12111 * posted for every RQE, entry_repost should be adjusted accordingly.
12112 */
12113 queue->entry_repost = (entry_count >> 3);
12114 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12115 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
12116 queue->phba = phba;
12117
12118 return queue;
12119 out_fail:
12120 lpfc_sli4_queue_free(queue);
12121 return NULL;
12122 }
12123
12124 /**
12125 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12126 * @phba: HBA structure that indicates port to create a queue on.
12127 * @startq: The starting FCP EQ to modify
12128 *
12129 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12130 *
12131 * The @phba struct is used to send mailbox command to HBA. The @startq
12132 * is used to get the starting FCP EQ to change.
12133 * This function is asynchronous and will wait for the mailbox
12134 * command to finish before continuing.
12135 *
12136 * On success this function will return a zero. If unable to allocate enough
12137 * memory this function will return -ENOMEM. If the queue create mailbox command
12138 * fails this function will return -ENXIO.
12139 **/
12140 uint32_t
12141 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12142 {
12143 struct lpfc_mbx_modify_eq_delay *eq_delay;
12144 LPFC_MBOXQ_t *mbox;
12145 struct lpfc_queue *eq;
12146 int cnt, rc, length, status = 0;
12147 uint32_t shdr_status, shdr_add_status;
12148 int fcp_eqidx;
12149 union lpfc_sli4_cfg_shdr *shdr;
12150 uint16_t dmult;
12151
12152 if (startq >= phba->cfg_fcp_io_channel)
12153 return 0;
12154
12155 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12156 if (!mbox)
12157 return -ENOMEM;
12158 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12159 sizeof(struct lpfc_sli4_cfg_mhdr));
12160 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12161 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12162 length, LPFC_SLI4_MBX_EMBED);
12163 eq_delay = &mbox->u.mqe.un.eq_delay;
12164
12165 /* Calculate delay multiper from maximum interrupt per second */
12166 dmult = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12167 dmult = LPFC_DMULT_CONST/dmult - 1;
12168
12169 cnt = 0;
12170 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
12171 fcp_eqidx++) {
12172 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
12173 if (!eq)
12174 continue;
12175 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12176 eq_delay->u.request.eq[cnt].phase = 0;
12177 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12178 cnt++;
12179 if (cnt >= LPFC_MAX_EQ_DELAY)
12180 break;
12181 }
12182 eq_delay->u.request.num_eq = cnt;
12183
12184 mbox->vport = phba->pport;
12185 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12186 mbox->context1 = NULL;
12187 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12188 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12191 if (shdr_status || shdr_add_status || rc) {
12192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12193 "2512 MODIFY_EQ_DELAY mailbox failed with "
12194 "status x%x add_status x%x, mbx status x%x\n",
12195 shdr_status, shdr_add_status, rc);
12196 status = -ENXIO;
12197 }
12198 mempool_free(mbox, phba->mbox_mem_pool);
12199 return status;
12200 }
12201
12202 /**
12203 * lpfc_eq_create - Create an Event Queue on the HBA
12204 * @phba: HBA structure that indicates port to create a queue on.
12205 * @eq: The queue structure to use to create the event queue.
12206 * @imax: The maximum interrupt per second limit.
12207 *
12208 * This function creates an event queue, as detailed in @eq, on a port,
12209 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12210 *
12211 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12212 * is used to get the entry count and entry size that are necessary to
12213 * determine the number of pages to allocate and use for this queue. This
12214 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12215 * event queue. This function is asynchronous and will wait for the mailbox
12216 * command to finish before continuing.
12217 *
12218 * On success this function will return a zero. If unable to allocate enough
12219 * memory this function will return -ENOMEM. If the queue create mailbox command
12220 * fails this function will return -ENXIO.
12221 **/
12222 uint32_t
12223 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12224 {
12225 struct lpfc_mbx_eq_create *eq_create;
12226 LPFC_MBOXQ_t *mbox;
12227 int rc, length, status = 0;
12228 struct lpfc_dmabuf *dmabuf;
12229 uint32_t shdr_status, shdr_add_status;
12230 union lpfc_sli4_cfg_shdr *shdr;
12231 uint16_t dmult;
12232 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12233
12234 /* sanity check on queue memory */
12235 if (!eq)
12236 return -ENODEV;
12237 if (!phba->sli4_hba.pc_sli4_params.supported)
12238 hw_page_size = SLI4_PAGE_SIZE;
12239
12240 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12241 if (!mbox)
12242 return -ENOMEM;
12243 length = (sizeof(struct lpfc_mbx_eq_create) -
12244 sizeof(struct lpfc_sli4_cfg_mhdr));
12245 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12246 LPFC_MBOX_OPCODE_EQ_CREATE,
12247 length, LPFC_SLI4_MBX_EMBED);
12248 eq_create = &mbox->u.mqe.un.eq_create;
12249 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12250 eq->page_count);
12251 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12252 LPFC_EQE_SIZE);
12253 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12254 /* Calculate delay multiper from maximum interrupt per second */
12255 dmult = LPFC_DMULT_CONST/imax - 1;
12256 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12257 dmult);
12258 switch (eq->entry_count) {
12259 default:
12260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12261 "0360 Unsupported EQ count. (%d)\n",
12262 eq->entry_count);
12263 if (eq->entry_count < 256)
12264 return -EINVAL;
12265 /* otherwise default to smallest count (drop through) */
12266 case 256:
12267 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12268 LPFC_EQ_CNT_256);
12269 break;
12270 case 512:
12271 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12272 LPFC_EQ_CNT_512);
12273 break;
12274 case 1024:
12275 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12276 LPFC_EQ_CNT_1024);
12277 break;
12278 case 2048:
12279 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12280 LPFC_EQ_CNT_2048);
12281 break;
12282 case 4096:
12283 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12284 LPFC_EQ_CNT_4096);
12285 break;
12286 }
12287 list_for_each_entry(dmabuf, &eq->page_list, list) {
12288 memset(dmabuf->virt, 0, hw_page_size);
12289 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12290 putPaddrLow(dmabuf->phys);
12291 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12292 putPaddrHigh(dmabuf->phys);
12293 }
12294 mbox->vport = phba->pport;
12295 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12296 mbox->context1 = NULL;
12297 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12298 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12301 if (shdr_status || shdr_add_status || rc) {
12302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12303 "2500 EQ_CREATE mailbox failed with "
12304 "status x%x add_status x%x, mbx status x%x\n",
12305 shdr_status, shdr_add_status, rc);
12306 status = -ENXIO;
12307 }
12308 eq->type = LPFC_EQ;
12309 eq->subtype = LPFC_NONE;
12310 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12311 if (eq->queue_id == 0xFFFF)
12312 status = -ENXIO;
12313 eq->host_index = 0;
12314 eq->hba_index = 0;
12315
12316 mempool_free(mbox, phba->mbox_mem_pool);
12317 return status;
12318 }
12319
12320 /**
12321 * lpfc_cq_create - Create a Completion Queue on the HBA
12322 * @phba: HBA structure that indicates port to create a queue on.
12323 * @cq: The queue structure to use to create the completion queue.
12324 * @eq: The event queue to bind this completion queue to.
12325 *
12326 * This function creates a completion queue, as detailed in @wq, on a port,
12327 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12328 *
12329 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12330 * is used to get the entry count and entry size that are necessary to
12331 * determine the number of pages to allocate and use for this queue. The @eq
12332 * is used to indicate which event queue to bind this completion queue to. This
12333 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12334 * completion queue. This function is asynchronous and will wait for the mailbox
12335 * command to finish before continuing.
12336 *
12337 * On success this function will return a zero. If unable to allocate enough
12338 * memory this function will return -ENOMEM. If the queue create mailbox command
12339 * fails this function will return -ENXIO.
12340 **/
12341 uint32_t
12342 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12343 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12344 {
12345 struct lpfc_mbx_cq_create *cq_create;
12346 struct lpfc_dmabuf *dmabuf;
12347 LPFC_MBOXQ_t *mbox;
12348 int rc, length, status = 0;
12349 uint32_t shdr_status, shdr_add_status;
12350 union lpfc_sli4_cfg_shdr *shdr;
12351 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12352
12353 /* sanity check on queue memory */
12354 if (!cq || !eq)
12355 return -ENODEV;
12356 if (!phba->sli4_hba.pc_sli4_params.supported)
12357 hw_page_size = SLI4_PAGE_SIZE;
12358
12359 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12360 if (!mbox)
12361 return -ENOMEM;
12362 length = (sizeof(struct lpfc_mbx_cq_create) -
12363 sizeof(struct lpfc_sli4_cfg_mhdr));
12364 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12365 LPFC_MBOX_OPCODE_CQ_CREATE,
12366 length, LPFC_SLI4_MBX_EMBED);
12367 cq_create = &mbox->u.mqe.un.cq_create;
12368 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
12369 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12370 cq->page_count);
12371 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12372 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
12373 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12374 phba->sli4_hba.pc_sli4_params.cqv);
12375 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
12376 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12377 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
12378 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12379 eq->queue_id);
12380 } else {
12381 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12382 eq->queue_id);
12383 }
12384 switch (cq->entry_count) {
12385 default:
12386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12387 "0361 Unsupported CQ count. (%d)\n",
12388 cq->entry_count);
12389 if (cq->entry_count < 256) {
12390 status = -EINVAL;
12391 goto out;
12392 }
12393 /* otherwise default to smallest count (drop through) */
12394 case 256:
12395 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12396 LPFC_CQ_CNT_256);
12397 break;
12398 case 512:
12399 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12400 LPFC_CQ_CNT_512);
12401 break;
12402 case 1024:
12403 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12404 LPFC_CQ_CNT_1024);
12405 break;
12406 }
12407 list_for_each_entry(dmabuf, &cq->page_list, list) {
12408 memset(dmabuf->virt, 0, hw_page_size);
12409 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12410 putPaddrLow(dmabuf->phys);
12411 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12412 putPaddrHigh(dmabuf->phys);
12413 }
12414 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12415
12416 /* The IOCTL status is embedded in the mailbox subheader. */
12417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12419 if (shdr_status || shdr_add_status || rc) {
12420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12421 "2501 CQ_CREATE mailbox failed with "
12422 "status x%x add_status x%x, mbx status x%x\n",
12423 shdr_status, shdr_add_status, rc);
12424 status = -ENXIO;
12425 goto out;
12426 }
12427 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12428 if (cq->queue_id == 0xFFFF) {
12429 status = -ENXIO;
12430 goto out;
12431 }
12432 /* link the cq onto the parent eq child list */
12433 list_add_tail(&cq->list, &eq->child_list);
12434 /* Set up completion queue's type and subtype */
12435 cq->type = type;
12436 cq->subtype = subtype;
12437 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12438 cq->assoc_qid = eq->queue_id;
12439 cq->host_index = 0;
12440 cq->hba_index = 0;
12441
12442 out:
12443 mempool_free(mbox, phba->mbox_mem_pool);
12444 return status;
12445 }
12446
12447 /**
12448 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12449 * @phba: HBA structure that indicates port to create a queue on.
12450 * @mq: The queue structure to use to create the mailbox queue.
12451 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12452 * @cq: The completion queue to associate with this cq.
12453 *
12454 * This function provides failback (fb) functionality when the
12455 * mq_create_ext fails on older FW generations. It's purpose is identical
12456 * to mq_create_ext otherwise.
12457 *
12458 * This routine cannot fail as all attributes were previously accessed and
12459 * initialized in mq_create_ext.
12460 **/
12461 static void
12462 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12463 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12464 {
12465 struct lpfc_mbx_mq_create *mq_create;
12466 struct lpfc_dmabuf *dmabuf;
12467 int length;
12468
12469 length = (sizeof(struct lpfc_mbx_mq_create) -
12470 sizeof(struct lpfc_sli4_cfg_mhdr));
12471 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12472 LPFC_MBOX_OPCODE_MQ_CREATE,
12473 length, LPFC_SLI4_MBX_EMBED);
12474 mq_create = &mbox->u.mqe.un.mq_create;
12475 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12476 mq->page_count);
12477 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12478 cq->queue_id);
12479 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12480 switch (mq->entry_count) {
12481 case 16:
12482 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12483 LPFC_MQ_RING_SIZE_16);
12484 break;
12485 case 32:
12486 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12487 LPFC_MQ_RING_SIZE_32);
12488 break;
12489 case 64:
12490 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12491 LPFC_MQ_RING_SIZE_64);
12492 break;
12493 case 128:
12494 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12495 LPFC_MQ_RING_SIZE_128);
12496 break;
12497 }
12498 list_for_each_entry(dmabuf, &mq->page_list, list) {
12499 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12500 putPaddrLow(dmabuf->phys);
12501 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12502 putPaddrHigh(dmabuf->phys);
12503 }
12504 }
12505
12506 /**
12507 * lpfc_mq_create - Create a mailbox Queue on the HBA
12508 * @phba: HBA structure that indicates port to create a queue on.
12509 * @mq: The queue structure to use to create the mailbox queue.
12510 * @cq: The completion queue to associate with this cq.
12511 * @subtype: The queue's subtype.
12512 *
12513 * This function creates a mailbox queue, as detailed in @mq, on a port,
12514 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12515 *
12516 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12517 * is used to get the entry count and entry size that are necessary to
12518 * determine the number of pages to allocate and use for this queue. This
12519 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12520 * mailbox queue. This function is asynchronous and will wait for the mailbox
12521 * command to finish before continuing.
12522 *
12523 * On success this function will return a zero. If unable to allocate enough
12524 * memory this function will return -ENOMEM. If the queue create mailbox command
12525 * fails this function will return -ENXIO.
12526 **/
12527 int32_t
12528 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12529 struct lpfc_queue *cq, uint32_t subtype)
12530 {
12531 struct lpfc_mbx_mq_create *mq_create;
12532 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12533 struct lpfc_dmabuf *dmabuf;
12534 LPFC_MBOXQ_t *mbox;
12535 int rc, length, status = 0;
12536 uint32_t shdr_status, shdr_add_status;
12537 union lpfc_sli4_cfg_shdr *shdr;
12538 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12539
12540 /* sanity check on queue memory */
12541 if (!mq || !cq)
12542 return -ENODEV;
12543 if (!phba->sli4_hba.pc_sli4_params.supported)
12544 hw_page_size = SLI4_PAGE_SIZE;
12545
12546 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12547 if (!mbox)
12548 return -ENOMEM;
12549 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12550 sizeof(struct lpfc_sli4_cfg_mhdr));
12551 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12552 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12553 length, LPFC_SLI4_MBX_EMBED);
12554
12555 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
12556 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
12557 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12558 &mq_create_ext->u.request, mq->page_count);
12559 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12560 &mq_create_ext->u.request, 1);
12561 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
12562 &mq_create_ext->u.request, 1);
12563 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12564 &mq_create_ext->u.request, 1);
12565 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12566 &mq_create_ext->u.request, 1);
12567 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12568 &mq_create_ext->u.request, 1);
12569 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
12570 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12571 phba->sli4_hba.pc_sli4_params.mqv);
12572 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12573 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12574 cq->queue_id);
12575 else
12576 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12577 cq->queue_id);
12578 switch (mq->entry_count) {
12579 default:
12580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12581 "0362 Unsupported MQ count. (%d)\n",
12582 mq->entry_count);
12583 if (mq->entry_count < 16) {
12584 status = -EINVAL;
12585 goto out;
12586 }
12587 /* otherwise default to smallest count (drop through) */
12588 case 16:
12589 bf_set(lpfc_mq_context_ring_size,
12590 &mq_create_ext->u.request.context,
12591 LPFC_MQ_RING_SIZE_16);
12592 break;
12593 case 32:
12594 bf_set(lpfc_mq_context_ring_size,
12595 &mq_create_ext->u.request.context,
12596 LPFC_MQ_RING_SIZE_32);
12597 break;
12598 case 64:
12599 bf_set(lpfc_mq_context_ring_size,
12600 &mq_create_ext->u.request.context,
12601 LPFC_MQ_RING_SIZE_64);
12602 break;
12603 case 128:
12604 bf_set(lpfc_mq_context_ring_size,
12605 &mq_create_ext->u.request.context,
12606 LPFC_MQ_RING_SIZE_128);
12607 break;
12608 }
12609 list_for_each_entry(dmabuf, &mq->page_list, list) {
12610 memset(dmabuf->virt, 0, hw_page_size);
12611 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12612 putPaddrLow(dmabuf->phys);
12613 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
12614 putPaddrHigh(dmabuf->phys);
12615 }
12616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12617 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12618 &mq_create_ext->u.response);
12619 if (rc != MBX_SUCCESS) {
12620 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12621 "2795 MQ_CREATE_EXT failed with "
12622 "status x%x. Failback to MQ_CREATE.\n",
12623 rc);
12624 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12625 mq_create = &mbox->u.mqe.un.mq_create;
12626 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12627 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12628 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12629 &mq_create->u.response);
12630 }
12631
12632 /* The IOCTL status is embedded in the mailbox subheader. */
12633 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12634 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12635 if (shdr_status || shdr_add_status || rc) {
12636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12637 "2502 MQ_CREATE mailbox failed with "
12638 "status x%x add_status x%x, mbx status x%x\n",
12639 shdr_status, shdr_add_status, rc);
12640 status = -ENXIO;
12641 goto out;
12642 }
12643 if (mq->queue_id == 0xFFFF) {
12644 status = -ENXIO;
12645 goto out;
12646 }
12647 mq->type = LPFC_MQ;
12648 mq->assoc_qid = cq->queue_id;
12649 mq->subtype = subtype;
12650 mq->host_index = 0;
12651 mq->hba_index = 0;
12652
12653 /* link the mq onto the parent cq child list */
12654 list_add_tail(&mq->list, &cq->child_list);
12655 out:
12656 mempool_free(mbox, phba->mbox_mem_pool);
12657 return status;
12658 }
12659
12660 /**
12661 * lpfc_wq_create - Create a Work Queue on the HBA
12662 * @phba: HBA structure that indicates port to create a queue on.
12663 * @wq: The queue structure to use to create the work queue.
12664 * @cq: The completion queue to bind this work queue to.
12665 * @subtype: The subtype of the work queue indicating its functionality.
12666 *
12667 * This function creates a work queue, as detailed in @wq, on a port, described
12668 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12669 *
12670 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12671 * is used to get the entry count and entry size that are necessary to
12672 * determine the number of pages to allocate and use for this queue. The @cq
12673 * is used to indicate which completion queue to bind this work queue to. This
12674 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12675 * work queue. This function is asynchronous and will wait for the mailbox
12676 * command to finish before continuing.
12677 *
12678 * On success this function will return a zero. If unable to allocate enough
12679 * memory this function will return -ENOMEM. If the queue create mailbox command
12680 * fails this function will return -ENXIO.
12681 **/
12682 uint32_t
12683 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12684 struct lpfc_queue *cq, uint32_t subtype)
12685 {
12686 struct lpfc_mbx_wq_create *wq_create;
12687 struct lpfc_dmabuf *dmabuf;
12688 LPFC_MBOXQ_t *mbox;
12689 int rc, length, status = 0;
12690 uint32_t shdr_status, shdr_add_status;
12691 union lpfc_sli4_cfg_shdr *shdr;
12692 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12693 struct dma_address *page;
12694
12695 /* sanity check on queue memory */
12696 if (!wq || !cq)
12697 return -ENODEV;
12698 if (!phba->sli4_hba.pc_sli4_params.supported)
12699 hw_page_size = SLI4_PAGE_SIZE;
12700
12701 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12702 if (!mbox)
12703 return -ENOMEM;
12704 length = (sizeof(struct lpfc_mbx_wq_create) -
12705 sizeof(struct lpfc_sli4_cfg_mhdr));
12706 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12707 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12708 length, LPFC_SLI4_MBX_EMBED);
12709 wq_create = &mbox->u.mqe.un.wq_create;
12710 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
12711 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12712 wq->page_count);
12713 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12714 cq->queue_id);
12715 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12716 phba->sli4_hba.pc_sli4_params.wqv);
12717 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12718 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12719 wq->entry_count);
12720 switch (wq->entry_size) {
12721 default:
12722 case 64:
12723 bf_set(lpfc_mbx_wq_create_wqe_size,
12724 &wq_create->u.request_1,
12725 LPFC_WQ_WQE_SIZE_64);
12726 break;
12727 case 128:
12728 bf_set(lpfc_mbx_wq_create_wqe_size,
12729 &wq_create->u.request_1,
12730 LPFC_WQ_WQE_SIZE_128);
12731 break;
12732 }
12733 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12734 (PAGE_SIZE/SLI4_PAGE_SIZE));
12735 page = wq_create->u.request_1.page;
12736 } else {
12737 page = wq_create->u.request.page;
12738 }
12739 list_for_each_entry(dmabuf, &wq->page_list, list) {
12740 memset(dmabuf->virt, 0, hw_page_size);
12741 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12742 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
12743 }
12744 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12745 /* The IOCTL status is embedded in the mailbox subheader. */
12746 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12747 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12748 if (shdr_status || shdr_add_status || rc) {
12749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12750 "2503 WQ_CREATE mailbox failed with "
12751 "status x%x add_status x%x, mbx status x%x\n",
12752 shdr_status, shdr_add_status, rc);
12753 status = -ENXIO;
12754 goto out;
12755 }
12756 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12757 if (wq->queue_id == 0xFFFF) {
12758 status = -ENXIO;
12759 goto out;
12760 }
12761 wq->type = LPFC_WQ;
12762 wq->assoc_qid = cq->queue_id;
12763 wq->subtype = subtype;
12764 wq->host_index = 0;
12765 wq->hba_index = 0;
12766 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
12767
12768 /* link the wq onto the parent cq child list */
12769 list_add_tail(&wq->list, &cq->child_list);
12770 out:
12771 mempool_free(mbox, phba->mbox_mem_pool);
12772 return status;
12773 }
12774
12775 /**
12776 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12777 * @phba: HBA structure that indicates port to create a queue on.
12778 * @rq: The queue structure to use for the receive queue.
12779 * @qno: The associated HBQ number
12780 *
12781 *
12782 * For SLI4 we need to adjust the RQ repost value based on
12783 * the number of buffers that are initially posted to the RQ.
12784 */
12785 void
12786 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12787 {
12788 uint32_t cnt;
12789
12790 /* sanity check on queue memory */
12791 if (!rq)
12792 return;
12793 cnt = lpfc_hbq_defs[qno]->entry_count;
12794
12795 /* Recalc repost for RQs based on buffers initially posted */
12796 cnt = (cnt >> 3);
12797 if (cnt < LPFC_QUEUE_MIN_REPOST)
12798 cnt = LPFC_QUEUE_MIN_REPOST;
12799
12800 rq->entry_repost = cnt;
12801 }
12802
12803 /**
12804 * lpfc_rq_create - Create a Receive Queue on the HBA
12805 * @phba: HBA structure that indicates port to create a queue on.
12806 * @hrq: The queue structure to use to create the header receive queue.
12807 * @drq: The queue structure to use to create the data receive queue.
12808 * @cq: The completion queue to bind this work queue to.
12809 *
12810 * This function creates a receive buffer queue pair , as detailed in @hrq and
12811 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12812 * to the HBA.
12813 *
12814 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12815 * struct is used to get the entry count that is necessary to determine the
12816 * number of pages to use for this queue. The @cq is used to indicate which
12817 * completion queue to bind received buffers that are posted to these queues to.
12818 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12819 * receive queue pair. This function is asynchronous and will wait for the
12820 * mailbox command to finish before continuing.
12821 *
12822 * On success this function will return a zero. If unable to allocate enough
12823 * memory this function will return -ENOMEM. If the queue create mailbox command
12824 * fails this function will return -ENXIO.
12825 **/
12826 uint32_t
12827 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12828 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12829 {
12830 struct lpfc_mbx_rq_create *rq_create;
12831 struct lpfc_dmabuf *dmabuf;
12832 LPFC_MBOXQ_t *mbox;
12833 int rc, length, status = 0;
12834 uint32_t shdr_status, shdr_add_status;
12835 union lpfc_sli4_cfg_shdr *shdr;
12836 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12837
12838 /* sanity check on queue memory */
12839 if (!hrq || !drq || !cq)
12840 return -ENODEV;
12841 if (!phba->sli4_hba.pc_sli4_params.supported)
12842 hw_page_size = SLI4_PAGE_SIZE;
12843
12844 if (hrq->entry_count != drq->entry_count)
12845 return -EINVAL;
12846 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12847 if (!mbox)
12848 return -ENOMEM;
12849 length = (sizeof(struct lpfc_mbx_rq_create) -
12850 sizeof(struct lpfc_sli4_cfg_mhdr));
12851 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12852 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12853 length, LPFC_SLI4_MBX_EMBED);
12854 rq_create = &mbox->u.mqe.un.rq_create;
12855 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12856 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12857 phba->sli4_hba.pc_sli4_params.rqv);
12858 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12859 bf_set(lpfc_rq_context_rqe_count_1,
12860 &rq_create->u.request.context,
12861 hrq->entry_count);
12862 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
12863 bf_set(lpfc_rq_context_rqe_size,
12864 &rq_create->u.request.context,
12865 LPFC_RQE_SIZE_8);
12866 bf_set(lpfc_rq_context_page_size,
12867 &rq_create->u.request.context,
12868 (PAGE_SIZE/SLI4_PAGE_SIZE));
12869 } else {
12870 switch (hrq->entry_count) {
12871 default:
12872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12873 "2535 Unsupported RQ count. (%d)\n",
12874 hrq->entry_count);
12875 if (hrq->entry_count < 512) {
12876 status = -EINVAL;
12877 goto out;
12878 }
12879 /* otherwise default to smallest count (drop through) */
12880 case 512:
12881 bf_set(lpfc_rq_context_rqe_count,
12882 &rq_create->u.request.context,
12883 LPFC_RQ_RING_SIZE_512);
12884 break;
12885 case 1024:
12886 bf_set(lpfc_rq_context_rqe_count,
12887 &rq_create->u.request.context,
12888 LPFC_RQ_RING_SIZE_1024);
12889 break;
12890 case 2048:
12891 bf_set(lpfc_rq_context_rqe_count,
12892 &rq_create->u.request.context,
12893 LPFC_RQ_RING_SIZE_2048);
12894 break;
12895 case 4096:
12896 bf_set(lpfc_rq_context_rqe_count,
12897 &rq_create->u.request.context,
12898 LPFC_RQ_RING_SIZE_4096);
12899 break;
12900 }
12901 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12902 LPFC_HDR_BUF_SIZE);
12903 }
12904 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12905 cq->queue_id);
12906 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12907 hrq->page_count);
12908 list_for_each_entry(dmabuf, &hrq->page_list, list) {
12909 memset(dmabuf->virt, 0, hw_page_size);
12910 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12911 putPaddrLow(dmabuf->phys);
12912 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12913 putPaddrHigh(dmabuf->phys);
12914 }
12915 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12916 /* The IOCTL status is embedded in the mailbox subheader. */
12917 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12918 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12919 if (shdr_status || shdr_add_status || rc) {
12920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12921 "2504 RQ_CREATE mailbox failed with "
12922 "status x%x add_status x%x, mbx status x%x\n",
12923 shdr_status, shdr_add_status, rc);
12924 status = -ENXIO;
12925 goto out;
12926 }
12927 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12928 if (hrq->queue_id == 0xFFFF) {
12929 status = -ENXIO;
12930 goto out;
12931 }
12932 hrq->type = LPFC_HRQ;
12933 hrq->assoc_qid = cq->queue_id;
12934 hrq->subtype = subtype;
12935 hrq->host_index = 0;
12936 hrq->hba_index = 0;
12937
12938 /* now create the data queue */
12939 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12940 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12941 length, LPFC_SLI4_MBX_EMBED);
12942 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12943 phba->sli4_hba.pc_sli4_params.rqv);
12944 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12945 bf_set(lpfc_rq_context_rqe_count_1,
12946 &rq_create->u.request.context, hrq->entry_count);
12947 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
12948 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12949 LPFC_RQE_SIZE_8);
12950 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12951 (PAGE_SIZE/SLI4_PAGE_SIZE));
12952 } else {
12953 switch (drq->entry_count) {
12954 default:
12955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12956 "2536 Unsupported RQ count. (%d)\n",
12957 drq->entry_count);
12958 if (drq->entry_count < 512) {
12959 status = -EINVAL;
12960 goto out;
12961 }
12962 /* otherwise default to smallest count (drop through) */
12963 case 512:
12964 bf_set(lpfc_rq_context_rqe_count,
12965 &rq_create->u.request.context,
12966 LPFC_RQ_RING_SIZE_512);
12967 break;
12968 case 1024:
12969 bf_set(lpfc_rq_context_rqe_count,
12970 &rq_create->u.request.context,
12971 LPFC_RQ_RING_SIZE_1024);
12972 break;
12973 case 2048:
12974 bf_set(lpfc_rq_context_rqe_count,
12975 &rq_create->u.request.context,
12976 LPFC_RQ_RING_SIZE_2048);
12977 break;
12978 case 4096:
12979 bf_set(lpfc_rq_context_rqe_count,
12980 &rq_create->u.request.context,
12981 LPFC_RQ_RING_SIZE_4096);
12982 break;
12983 }
12984 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12985 LPFC_DATA_BUF_SIZE);
12986 }
12987 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12988 cq->queue_id);
12989 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12990 drq->page_count);
12991 list_for_each_entry(dmabuf, &drq->page_list, list) {
12992 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12993 putPaddrLow(dmabuf->phys);
12994 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12995 putPaddrHigh(dmabuf->phys);
12996 }
12997 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12998 /* The IOCTL status is embedded in the mailbox subheader. */
12999 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13002 if (shdr_status || shdr_add_status || rc) {
13003 status = -ENXIO;
13004 goto out;
13005 }
13006 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13007 if (drq->queue_id == 0xFFFF) {
13008 status = -ENXIO;
13009 goto out;
13010 }
13011 drq->type = LPFC_DRQ;
13012 drq->assoc_qid = cq->queue_id;
13013 drq->subtype = subtype;
13014 drq->host_index = 0;
13015 drq->hba_index = 0;
13016
13017 /* link the header and data RQs onto the parent cq child list */
13018 list_add_tail(&hrq->list, &cq->child_list);
13019 list_add_tail(&drq->list, &cq->child_list);
13020
13021 out:
13022 mempool_free(mbox, phba->mbox_mem_pool);
13023 return status;
13024 }
13025
13026 /**
13027 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13028 * @eq: The queue structure associated with the queue to destroy.
13029 *
13030 * This function destroys a queue, as detailed in @eq by sending an mailbox
13031 * command, specific to the type of queue, to the HBA.
13032 *
13033 * The @eq struct is used to get the queue ID of the queue to destroy.
13034 *
13035 * On success this function will return a zero. If the queue destroy mailbox
13036 * command fails this function will return -ENXIO.
13037 **/
13038 uint32_t
13039 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13040 {
13041 LPFC_MBOXQ_t *mbox;
13042 int rc, length, status = 0;
13043 uint32_t shdr_status, shdr_add_status;
13044 union lpfc_sli4_cfg_shdr *shdr;
13045
13046 /* sanity check on queue memory */
13047 if (!eq)
13048 return -ENODEV;
13049 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13050 if (!mbox)
13051 return -ENOMEM;
13052 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13053 sizeof(struct lpfc_sli4_cfg_mhdr));
13054 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13055 LPFC_MBOX_OPCODE_EQ_DESTROY,
13056 length, LPFC_SLI4_MBX_EMBED);
13057 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13058 eq->queue_id);
13059 mbox->vport = eq->phba->pport;
13060 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13061
13062 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13063 /* The IOCTL status is embedded in the mailbox subheader. */
13064 shdr = (union lpfc_sli4_cfg_shdr *)
13065 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13066 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13067 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13068 if (shdr_status || shdr_add_status || rc) {
13069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13070 "2505 EQ_DESTROY mailbox failed with "
13071 "status x%x add_status x%x, mbx status x%x\n",
13072 shdr_status, shdr_add_status, rc);
13073 status = -ENXIO;
13074 }
13075
13076 /* Remove eq from any list */
13077 list_del_init(&eq->list);
13078 mempool_free(mbox, eq->phba->mbox_mem_pool);
13079 return status;
13080 }
13081
13082 /**
13083 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13084 * @cq: The queue structure associated with the queue to destroy.
13085 *
13086 * This function destroys a queue, as detailed in @cq by sending an mailbox
13087 * command, specific to the type of queue, to the HBA.
13088 *
13089 * The @cq struct is used to get the queue ID of the queue to destroy.
13090 *
13091 * On success this function will return a zero. If the queue destroy mailbox
13092 * command fails this function will return -ENXIO.
13093 **/
13094 uint32_t
13095 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13096 {
13097 LPFC_MBOXQ_t *mbox;
13098 int rc, length, status = 0;
13099 uint32_t shdr_status, shdr_add_status;
13100 union lpfc_sli4_cfg_shdr *shdr;
13101
13102 /* sanity check on queue memory */
13103 if (!cq)
13104 return -ENODEV;
13105 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13106 if (!mbox)
13107 return -ENOMEM;
13108 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13109 sizeof(struct lpfc_sli4_cfg_mhdr));
13110 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13111 LPFC_MBOX_OPCODE_CQ_DESTROY,
13112 length, LPFC_SLI4_MBX_EMBED);
13113 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13114 cq->queue_id);
13115 mbox->vport = cq->phba->pport;
13116 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13117 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13118 /* The IOCTL status is embedded in the mailbox subheader. */
13119 shdr = (union lpfc_sli4_cfg_shdr *)
13120 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13121 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13122 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13123 if (shdr_status || shdr_add_status || rc) {
13124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13125 "2506 CQ_DESTROY mailbox failed with "
13126 "status x%x add_status x%x, mbx status x%x\n",
13127 shdr_status, shdr_add_status, rc);
13128 status = -ENXIO;
13129 }
13130 /* Remove cq from any list */
13131 list_del_init(&cq->list);
13132 mempool_free(mbox, cq->phba->mbox_mem_pool);
13133 return status;
13134 }
13135
13136 /**
13137 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13138 * @qm: The queue structure associated with the queue to destroy.
13139 *
13140 * This function destroys a queue, as detailed in @mq by sending an mailbox
13141 * command, specific to the type of queue, to the HBA.
13142 *
13143 * The @mq struct is used to get the queue ID of the queue to destroy.
13144 *
13145 * On success this function will return a zero. If the queue destroy mailbox
13146 * command fails this function will return -ENXIO.
13147 **/
13148 uint32_t
13149 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13150 {
13151 LPFC_MBOXQ_t *mbox;
13152 int rc, length, status = 0;
13153 uint32_t shdr_status, shdr_add_status;
13154 union lpfc_sli4_cfg_shdr *shdr;
13155
13156 /* sanity check on queue memory */
13157 if (!mq)
13158 return -ENODEV;
13159 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13160 if (!mbox)
13161 return -ENOMEM;
13162 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13163 sizeof(struct lpfc_sli4_cfg_mhdr));
13164 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13165 LPFC_MBOX_OPCODE_MQ_DESTROY,
13166 length, LPFC_SLI4_MBX_EMBED);
13167 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13168 mq->queue_id);
13169 mbox->vport = mq->phba->pport;
13170 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13171 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13172 /* The IOCTL status is embedded in the mailbox subheader. */
13173 shdr = (union lpfc_sli4_cfg_shdr *)
13174 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13175 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13176 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13177 if (shdr_status || shdr_add_status || rc) {
13178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13179 "2507 MQ_DESTROY mailbox failed with "
13180 "status x%x add_status x%x, mbx status x%x\n",
13181 shdr_status, shdr_add_status, rc);
13182 status = -ENXIO;
13183 }
13184 /* Remove mq from any list */
13185 list_del_init(&mq->list);
13186 mempool_free(mbox, mq->phba->mbox_mem_pool);
13187 return status;
13188 }
13189
13190 /**
13191 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13192 * @wq: The queue structure associated with the queue to destroy.
13193 *
13194 * This function destroys a queue, as detailed in @wq by sending an mailbox
13195 * command, specific to the type of queue, to the HBA.
13196 *
13197 * The @wq struct is used to get the queue ID of the queue to destroy.
13198 *
13199 * On success this function will return a zero. If the queue destroy mailbox
13200 * command fails this function will return -ENXIO.
13201 **/
13202 uint32_t
13203 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13204 {
13205 LPFC_MBOXQ_t *mbox;
13206 int rc, length, status = 0;
13207 uint32_t shdr_status, shdr_add_status;
13208 union lpfc_sli4_cfg_shdr *shdr;
13209
13210 /* sanity check on queue memory */
13211 if (!wq)
13212 return -ENODEV;
13213 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13214 if (!mbox)
13215 return -ENOMEM;
13216 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13217 sizeof(struct lpfc_sli4_cfg_mhdr));
13218 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13219 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13220 length, LPFC_SLI4_MBX_EMBED);
13221 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13222 wq->queue_id);
13223 mbox->vport = wq->phba->pport;
13224 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13225 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13226 shdr = (union lpfc_sli4_cfg_shdr *)
13227 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13228 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13229 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13230 if (shdr_status || shdr_add_status || rc) {
13231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13232 "2508 WQ_DESTROY mailbox failed with "
13233 "status x%x add_status x%x, mbx status x%x\n",
13234 shdr_status, shdr_add_status, rc);
13235 status = -ENXIO;
13236 }
13237 /* Remove wq from any list */
13238 list_del_init(&wq->list);
13239 mempool_free(mbox, wq->phba->mbox_mem_pool);
13240 return status;
13241 }
13242
13243 /**
13244 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13245 * @rq: The queue structure associated with the queue to destroy.
13246 *
13247 * This function destroys a queue, as detailed in @rq by sending an mailbox
13248 * command, specific to the type of queue, to the HBA.
13249 *
13250 * The @rq struct is used to get the queue ID of the queue to destroy.
13251 *
13252 * On success this function will return a zero. If the queue destroy mailbox
13253 * command fails this function will return -ENXIO.
13254 **/
13255 uint32_t
13256 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13257 struct lpfc_queue *drq)
13258 {
13259 LPFC_MBOXQ_t *mbox;
13260 int rc, length, status = 0;
13261 uint32_t shdr_status, shdr_add_status;
13262 union lpfc_sli4_cfg_shdr *shdr;
13263
13264 /* sanity check on queue memory */
13265 if (!hrq || !drq)
13266 return -ENODEV;
13267 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13268 if (!mbox)
13269 return -ENOMEM;
13270 length = (sizeof(struct lpfc_mbx_rq_destroy) -
13271 sizeof(struct lpfc_sli4_cfg_mhdr));
13272 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13273 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13274 length, LPFC_SLI4_MBX_EMBED);
13275 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13276 hrq->queue_id);
13277 mbox->vport = hrq->phba->pport;
13278 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13279 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13280 /* The IOCTL status is embedded in the mailbox subheader. */
13281 shdr = (union lpfc_sli4_cfg_shdr *)
13282 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13285 if (shdr_status || shdr_add_status || rc) {
13286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13287 "2509 RQ_DESTROY mailbox failed with "
13288 "status x%x add_status x%x, mbx status x%x\n",
13289 shdr_status, shdr_add_status, rc);
13290 if (rc != MBX_TIMEOUT)
13291 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13292 return -ENXIO;
13293 }
13294 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13295 drq->queue_id);
13296 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13297 shdr = (union lpfc_sli4_cfg_shdr *)
13298 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13301 if (shdr_status || shdr_add_status || rc) {
13302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13303 "2510 RQ_DESTROY mailbox failed with "
13304 "status x%x add_status x%x, mbx status x%x\n",
13305 shdr_status, shdr_add_status, rc);
13306 status = -ENXIO;
13307 }
13308 list_del_init(&hrq->list);
13309 list_del_init(&drq->list);
13310 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13311 return status;
13312 }
13313
13314 /**
13315 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13316 * @phba: The virtual port for which this call being executed.
13317 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13318 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13319 * @xritag: the xritag that ties this io to the SGL pages.
13320 *
13321 * This routine will post the sgl pages for the IO that has the xritag
13322 * that is in the iocbq structure. The xritag is assigned during iocbq
13323 * creation and persists for as long as the driver is loaded.
13324 * if the caller has fewer than 256 scatter gather segments to map then
13325 * pdma_phys_addr1 should be 0.
13326 * If the caller needs to map more than 256 scatter gather segment then
13327 * pdma_phys_addr1 should be a valid physical address.
13328 * physical address for SGLs must be 64 byte aligned.
13329 * If you are going to map 2 SGL's then the first one must have 256 entries
13330 * the second sgl can have between 1 and 256 entries.
13331 *
13332 * Return codes:
13333 * 0 - Success
13334 * -ENXIO, -ENOMEM - Failure
13335 **/
13336 int
13337 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13338 dma_addr_t pdma_phys_addr0,
13339 dma_addr_t pdma_phys_addr1,
13340 uint16_t xritag)
13341 {
13342 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13343 LPFC_MBOXQ_t *mbox;
13344 int rc;
13345 uint32_t shdr_status, shdr_add_status;
13346 uint32_t mbox_tmo;
13347 union lpfc_sli4_cfg_shdr *shdr;
13348
13349 if (xritag == NO_XRI) {
13350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13351 "0364 Invalid param:\n");
13352 return -EINVAL;
13353 }
13354
13355 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13356 if (!mbox)
13357 return -ENOMEM;
13358
13359 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13360 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13361 sizeof(struct lpfc_mbx_post_sgl_pages) -
13362 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
13363
13364 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13365 &mbox->u.mqe.un.post_sgl_pages;
13366 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13367 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13368
13369 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13370 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13371 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13372 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13373
13374 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13375 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13376 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13377 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13378 if (!phba->sli4_hba.intr_enable)
13379 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13380 else {
13381 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13382 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13383 }
13384 /* The IOCTL status is embedded in the mailbox subheader. */
13385 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13386 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13387 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13388 if (rc != MBX_TIMEOUT)
13389 mempool_free(mbox, phba->mbox_mem_pool);
13390 if (shdr_status || shdr_add_status || rc) {
13391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13392 "2511 POST_SGL mailbox failed with "
13393 "status x%x add_status x%x, mbx status x%x\n",
13394 shdr_status, shdr_add_status, rc);
13395 rc = -ENXIO;
13396 }
13397 return 0;
13398 }
13399
13400 /**
13401 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
13402 * @phba: pointer to lpfc hba data structure.
13403 *
13404 * This routine is invoked to post rpi header templates to the
13405 * HBA consistent with the SLI-4 interface spec. This routine
13406 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13407 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13408 *
13409 * Returns
13410 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13411 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13412 **/
13413 uint16_t
13414 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13415 {
13416 unsigned long xri;
13417
13418 /*
13419 * Fetch the next logical xri. Because this index is logical,
13420 * the driver starts at 0 each time.
13421 */
13422 spin_lock_irq(&phba->hbalock);
13423 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13424 phba->sli4_hba.max_cfg_param.max_xri, 0);
13425 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13426 spin_unlock_irq(&phba->hbalock);
13427 return NO_XRI;
13428 } else {
13429 set_bit(xri, phba->sli4_hba.xri_bmask);
13430 phba->sli4_hba.max_cfg_param.xri_used++;
13431 }
13432 spin_unlock_irq(&phba->hbalock);
13433 return xri;
13434 }
13435
13436 /**
13437 * lpfc_sli4_free_xri - Release an xri for reuse.
13438 * @phba: pointer to lpfc hba data structure.
13439 *
13440 * This routine is invoked to release an xri to the pool of
13441 * available rpis maintained by the driver.
13442 **/
13443 void
13444 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13445 {
13446 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13447 phba->sli4_hba.max_cfg_param.xri_used--;
13448 }
13449 }
13450
13451 /**
13452 * lpfc_sli4_free_xri - Release an xri for reuse.
13453 * @phba: pointer to lpfc hba data structure.
13454 *
13455 * This routine is invoked to release an xri to the pool of
13456 * available rpis maintained by the driver.
13457 **/
13458 void
13459 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13460 {
13461 spin_lock_irq(&phba->hbalock);
13462 __lpfc_sli4_free_xri(phba, xri);
13463 spin_unlock_irq(&phba->hbalock);
13464 }
13465
13466 /**
13467 * lpfc_sli4_next_xritag - Get an xritag for the io
13468 * @phba: Pointer to HBA context object.
13469 *
13470 * This function gets an xritag for the iocb. If there is no unused xritag
13471 * it will return 0xffff.
13472 * The function returns the allocated xritag if successful, else returns zero.
13473 * Zero is not a valid xritag.
13474 * The caller is not required to hold any lock.
13475 **/
13476 uint16_t
13477 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13478 {
13479 uint16_t xri_index;
13480
13481 xri_index = lpfc_sli4_alloc_xri(phba);
13482 if (xri_index == NO_XRI)
13483 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13484 "2004 Failed to allocate XRI.last XRITAG is %d"
13485 " Max XRI is %d, Used XRI is %d\n",
13486 xri_index,
13487 phba->sli4_hba.max_cfg_param.max_xri,
13488 phba->sli4_hba.max_cfg_param.xri_used);
13489 return xri_index;
13490 }
13491
13492 /**
13493 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
13494 * @phba: pointer to lpfc hba data structure.
13495 * @post_sgl_list: pointer to els sgl entry list.
13496 * @count: number of els sgl entries on the list.
13497 *
13498 * This routine is invoked to post a block of driver's sgl pages to the
13499 * HBA using non-embedded mailbox command. No Lock is held. This routine
13500 * is only called when the driver is loading and after all IO has been
13501 * stopped.
13502 **/
13503 static int
13504 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13505 struct list_head *post_sgl_list,
13506 int post_cnt)
13507 {
13508 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
13509 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13510 struct sgl_page_pairs *sgl_pg_pairs;
13511 void *viraddr;
13512 LPFC_MBOXQ_t *mbox;
13513 uint32_t reqlen, alloclen, pg_pairs;
13514 uint32_t mbox_tmo;
13515 uint16_t xritag_start = 0;
13516 int rc = 0;
13517 uint32_t shdr_status, shdr_add_status;
13518 union lpfc_sli4_cfg_shdr *shdr;
13519
13520 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
13521 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13522 if (reqlen > SLI4_PAGE_SIZE) {
13523 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13524 "2559 Block sgl registration required DMA "
13525 "size (%d) great than a page\n", reqlen);
13526 return -ENOMEM;
13527 }
13528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13529 if (!mbox)
13530 return -ENOMEM;
13531
13532 /* Allocate DMA memory and set up the non-embedded mailbox command */
13533 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13534 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13535 LPFC_SLI4_MBX_NEMBED);
13536
13537 if (alloclen < reqlen) {
13538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13539 "0285 Allocated DMA memory size (%d) is "
13540 "less than the requested DMA memory "
13541 "size (%d)\n", alloclen, reqlen);
13542 lpfc_sli4_mbox_cmd_free(phba, mbox);
13543 return -ENOMEM;
13544 }
13545 /* Set up the SGL pages in the non-embedded DMA pages */
13546 viraddr = mbox->sge_array->addr[0];
13547 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13548 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13549
13550 pg_pairs = 0;
13551 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
13552 /* Set up the sge entry */
13553 sgl_pg_pairs->sgl_pg0_addr_lo =
13554 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13555 sgl_pg_pairs->sgl_pg0_addr_hi =
13556 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13557 sgl_pg_pairs->sgl_pg1_addr_lo =
13558 cpu_to_le32(putPaddrLow(0));
13559 sgl_pg_pairs->sgl_pg1_addr_hi =
13560 cpu_to_le32(putPaddrHigh(0));
13561
13562 /* Keep the first xritag on the list */
13563 if (pg_pairs == 0)
13564 xritag_start = sglq_entry->sli4_xritag;
13565 sgl_pg_pairs++;
13566 pg_pairs++;
13567 }
13568
13569 /* Complete initialization and perform endian conversion. */
13570 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13571 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
13572 sgl->word0 = cpu_to_le32(sgl->word0);
13573 if (!phba->sli4_hba.intr_enable)
13574 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13575 else {
13576 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13577 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13578 }
13579 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13580 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13581 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13582 if (rc != MBX_TIMEOUT)
13583 lpfc_sli4_mbox_cmd_free(phba, mbox);
13584 if (shdr_status || shdr_add_status || rc) {
13585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13586 "2513 POST_SGL_BLOCK mailbox command failed "
13587 "status x%x add_status x%x mbx status x%x\n",
13588 shdr_status, shdr_add_status, rc);
13589 rc = -ENXIO;
13590 }
13591 return rc;
13592 }
13593
13594 /**
13595 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13596 * @phba: pointer to lpfc hba data structure.
13597 * @sblist: pointer to scsi buffer list.
13598 * @count: number of scsi buffers on the list.
13599 *
13600 * This routine is invoked to post a block of @count scsi sgl pages from a
13601 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13602 * No Lock is held.
13603 *
13604 **/
13605 int
13606 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13607 struct list_head *sblist,
13608 int count)
13609 {
13610 struct lpfc_scsi_buf *psb;
13611 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13612 struct sgl_page_pairs *sgl_pg_pairs;
13613 void *viraddr;
13614 LPFC_MBOXQ_t *mbox;
13615 uint32_t reqlen, alloclen, pg_pairs;
13616 uint32_t mbox_tmo;
13617 uint16_t xritag_start = 0;
13618 int rc = 0;
13619 uint32_t shdr_status, shdr_add_status;
13620 dma_addr_t pdma_phys_bpl1;
13621 union lpfc_sli4_cfg_shdr *shdr;
13622
13623 /* Calculate the requested length of the dma memory */
13624 reqlen = count * sizeof(struct sgl_page_pairs) +
13625 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13626 if (reqlen > SLI4_PAGE_SIZE) {
13627 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13628 "0217 Block sgl registration required DMA "
13629 "size (%d) great than a page\n", reqlen);
13630 return -ENOMEM;
13631 }
13632 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13633 if (!mbox) {
13634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13635 "0283 Failed to allocate mbox cmd memory\n");
13636 return -ENOMEM;
13637 }
13638
13639 /* Allocate DMA memory and set up the non-embedded mailbox command */
13640 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13641 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13642 LPFC_SLI4_MBX_NEMBED);
13643
13644 if (alloclen < reqlen) {
13645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13646 "2561 Allocated DMA memory size (%d) is "
13647 "less than the requested DMA memory "
13648 "size (%d)\n", alloclen, reqlen);
13649 lpfc_sli4_mbox_cmd_free(phba, mbox);
13650 return -ENOMEM;
13651 }
13652
13653 /* Get the first SGE entry from the non-embedded DMA memory */
13654 viraddr = mbox->sge_array->addr[0];
13655
13656 /* Set up the SGL pages in the non-embedded DMA pages */
13657 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13658 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13659
13660 pg_pairs = 0;
13661 list_for_each_entry(psb, sblist, list) {
13662 /* Set up the sge entry */
13663 sgl_pg_pairs->sgl_pg0_addr_lo =
13664 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13665 sgl_pg_pairs->sgl_pg0_addr_hi =
13666 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13667 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13668 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13669 else
13670 pdma_phys_bpl1 = 0;
13671 sgl_pg_pairs->sgl_pg1_addr_lo =
13672 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13673 sgl_pg_pairs->sgl_pg1_addr_hi =
13674 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13675 /* Keep the first xritag on the list */
13676 if (pg_pairs == 0)
13677 xritag_start = psb->cur_iocbq.sli4_xritag;
13678 sgl_pg_pairs++;
13679 pg_pairs++;
13680 }
13681 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13682 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13683 /* Perform endian conversion if necessary */
13684 sgl->word0 = cpu_to_le32(sgl->word0);
13685
13686 if (!phba->sli4_hba.intr_enable)
13687 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13688 else {
13689 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13690 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13691 }
13692 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13693 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13694 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13695 if (rc != MBX_TIMEOUT)
13696 lpfc_sli4_mbox_cmd_free(phba, mbox);
13697 if (shdr_status || shdr_add_status || rc) {
13698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13699 "2564 POST_SGL_BLOCK mailbox command failed "
13700 "status x%x add_status x%x mbx status x%x\n",
13701 shdr_status, shdr_add_status, rc);
13702 rc = -ENXIO;
13703 }
13704 return rc;
13705 }
13706
13707 /**
13708 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13709 * @phba: pointer to lpfc_hba struct that the frame was received on
13710 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13711 *
13712 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13713 * valid type of frame that the LPFC driver will handle. This function will
13714 * return a zero if the frame is a valid frame or a non zero value when the
13715 * frame does not pass the check.
13716 **/
13717 static int
13718 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13719 {
13720 /* make rctl_names static to save stack space */
13721 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
13722 char *type_names[] = FC_TYPE_NAMES_INIT;
13723 struct fc_vft_header *fc_vft_hdr;
13724 uint32_t *header = (uint32_t *) fc_hdr;
13725
13726 switch (fc_hdr->fh_r_ctl) {
13727 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13728 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13729 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13730 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13731 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13732 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13733 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13734 case FC_RCTL_DD_CMD_STATUS: /* command status */
13735 case FC_RCTL_ELS_REQ: /* extended link services request */
13736 case FC_RCTL_ELS_REP: /* extended link services reply */
13737 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13738 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13739 case FC_RCTL_BA_NOP: /* basic link service NOP */
13740 case FC_RCTL_BA_ABTS: /* basic link service abort */
13741 case FC_RCTL_BA_RMC: /* remove connection */
13742 case FC_RCTL_BA_ACC: /* basic accept */
13743 case FC_RCTL_BA_RJT: /* basic reject */
13744 case FC_RCTL_BA_PRMT:
13745 case FC_RCTL_ACK_1: /* acknowledge_1 */
13746 case FC_RCTL_ACK_0: /* acknowledge_0 */
13747 case FC_RCTL_P_RJT: /* port reject */
13748 case FC_RCTL_F_RJT: /* fabric reject */
13749 case FC_RCTL_P_BSY: /* port busy */
13750 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13751 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13752 case FC_RCTL_LCR: /* link credit reset */
13753 case FC_RCTL_END: /* end */
13754 break;
13755 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13756 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13757 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13758 return lpfc_fc_frame_check(phba, fc_hdr);
13759 default:
13760 goto drop;
13761 }
13762 switch (fc_hdr->fh_type) {
13763 case FC_TYPE_BLS:
13764 case FC_TYPE_ELS:
13765 case FC_TYPE_FCP:
13766 case FC_TYPE_CT:
13767 break;
13768 case FC_TYPE_IP:
13769 case FC_TYPE_ILS:
13770 default:
13771 goto drop;
13772 }
13773
13774 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13775 "2538 Received frame rctl:%s type:%s "
13776 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
13777 rctl_names[fc_hdr->fh_r_ctl],
13778 type_names[fc_hdr->fh_type],
13779 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13780 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13781 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
13782 return 0;
13783 drop:
13784 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13785 "2539 Dropped frame rctl:%s type:%s\n",
13786 rctl_names[fc_hdr->fh_r_ctl],
13787 type_names[fc_hdr->fh_type]);
13788 return 1;
13789 }
13790
13791 /**
13792 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13793 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13794 *
13795 * This function processes the FC header to retrieve the VFI from the VF
13796 * header, if one exists. This function will return the VFI if one exists
13797 * or 0 if no VSAN Header exists.
13798 **/
13799 static uint32_t
13800 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13801 {
13802 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13803
13804 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13805 return 0;
13806 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13807 }
13808
13809 /**
13810 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13811 * @phba: Pointer to the HBA structure to search for the vport on
13812 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13813 * @fcfi: The FC Fabric ID that the frame came from
13814 *
13815 * This function searches the @phba for a vport that matches the content of the
13816 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13817 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13818 * returns the matching vport pointer or NULL if unable to match frame to a
13819 * vport.
13820 **/
13821 static struct lpfc_vport *
13822 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13823 uint16_t fcfi)
13824 {
13825 struct lpfc_vport **vports;
13826 struct lpfc_vport *vport = NULL;
13827 int i;
13828 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13829 fc_hdr->fh_d_id[1] << 8 |
13830 fc_hdr->fh_d_id[2]);
13831
13832 if (did == Fabric_DID)
13833 return phba->pport;
13834 if ((phba->pport->fc_flag & FC_PT2PT) &&
13835 !(phba->link_state == LPFC_HBA_READY))
13836 return phba->pport;
13837
13838 vports = lpfc_create_vport_work_array(phba);
13839 if (vports != NULL)
13840 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13841 if (phba->fcf.fcfi == fcfi &&
13842 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13843 vports[i]->fc_myDID == did) {
13844 vport = vports[i];
13845 break;
13846 }
13847 }
13848 lpfc_destroy_vport_work_array(phba, vports);
13849 return vport;
13850 }
13851
13852 /**
13853 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13854 * @vport: The vport to work on.
13855 *
13856 * This function updates the receive sequence time stamp for this vport. The
13857 * receive sequence time stamp indicates the time that the last frame of the
13858 * the sequence that has been idle for the longest amount of time was received.
13859 * the driver uses this time stamp to indicate if any received sequences have
13860 * timed out.
13861 **/
13862 void
13863 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13864 {
13865 struct lpfc_dmabuf *h_buf;
13866 struct hbq_dmabuf *dmabuf = NULL;
13867
13868 /* get the oldest sequence on the rcv list */
13869 h_buf = list_get_first(&vport->rcv_buffer_list,
13870 struct lpfc_dmabuf, list);
13871 if (!h_buf)
13872 return;
13873 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13874 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13875 }
13876
13877 /**
13878 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13879 * @vport: The vport that the received sequences were sent to.
13880 *
13881 * This function cleans up all outstanding received sequences. This is called
13882 * by the driver when a link event or user action invalidates all the received
13883 * sequences.
13884 **/
13885 void
13886 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13887 {
13888 struct lpfc_dmabuf *h_buf, *hnext;
13889 struct lpfc_dmabuf *d_buf, *dnext;
13890 struct hbq_dmabuf *dmabuf = NULL;
13891
13892 /* start with the oldest sequence on the rcv list */
13893 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13894 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13895 list_del_init(&dmabuf->hbuf.list);
13896 list_for_each_entry_safe(d_buf, dnext,
13897 &dmabuf->dbuf.list, list) {
13898 list_del_init(&d_buf->list);
13899 lpfc_in_buf_free(vport->phba, d_buf);
13900 }
13901 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13902 }
13903 }
13904
13905 /**
13906 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13907 * @vport: The vport that the received sequences were sent to.
13908 *
13909 * This function determines whether any received sequences have timed out by
13910 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13911 * indicates that there is at least one timed out sequence this routine will
13912 * go through the received sequences one at a time from most inactive to most
13913 * active to determine which ones need to be cleaned up. Once it has determined
13914 * that a sequence needs to be cleaned up it will simply free up the resources
13915 * without sending an abort.
13916 **/
13917 void
13918 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13919 {
13920 struct lpfc_dmabuf *h_buf, *hnext;
13921 struct lpfc_dmabuf *d_buf, *dnext;
13922 struct hbq_dmabuf *dmabuf = NULL;
13923 unsigned long timeout;
13924 int abort_count = 0;
13925
13926 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13927 vport->rcv_buffer_time_stamp);
13928 if (list_empty(&vport->rcv_buffer_list) ||
13929 time_before(jiffies, timeout))
13930 return;
13931 /* start with the oldest sequence on the rcv list */
13932 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13933 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13934 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13935 dmabuf->time_stamp);
13936 if (time_before(jiffies, timeout))
13937 break;
13938 abort_count++;
13939 list_del_init(&dmabuf->hbuf.list);
13940 list_for_each_entry_safe(d_buf, dnext,
13941 &dmabuf->dbuf.list, list) {
13942 list_del_init(&d_buf->list);
13943 lpfc_in_buf_free(vport->phba, d_buf);
13944 }
13945 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13946 }
13947 if (abort_count)
13948 lpfc_update_rcv_time_stamp(vport);
13949 }
13950
13951 /**
13952 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13953 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13954 *
13955 * This function searches through the existing incomplete sequences that have
13956 * been sent to this @vport. If the frame matches one of the incomplete
13957 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13958 * make up that sequence. If no sequence is found that matches this frame then
13959 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13960 * This function returns a pointer to the first dmabuf in the sequence list that
13961 * the frame was linked to.
13962 **/
13963 static struct hbq_dmabuf *
13964 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13965 {
13966 struct fc_frame_header *new_hdr;
13967 struct fc_frame_header *temp_hdr;
13968 struct lpfc_dmabuf *d_buf;
13969 struct lpfc_dmabuf *h_buf;
13970 struct hbq_dmabuf *seq_dmabuf = NULL;
13971 struct hbq_dmabuf *temp_dmabuf = NULL;
13972
13973 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13974 dmabuf->time_stamp = jiffies;
13975 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13976 /* Use the hdr_buf to find the sequence that this frame belongs to */
13977 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13978 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13979 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13980 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13981 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13982 continue;
13983 /* found a pending sequence that matches this frame */
13984 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13985 break;
13986 }
13987 if (!seq_dmabuf) {
13988 /*
13989 * This indicates first frame received for this sequence.
13990 * Queue the buffer on the vport's rcv_buffer_list.
13991 */
13992 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13993 lpfc_update_rcv_time_stamp(vport);
13994 return dmabuf;
13995 }
13996 temp_hdr = seq_dmabuf->hbuf.virt;
13997 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13998 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13999 list_del_init(&seq_dmabuf->hbuf.list);
14000 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14001 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14002 lpfc_update_rcv_time_stamp(vport);
14003 return dmabuf;
14004 }
14005 /* move this sequence to the tail to indicate a young sequence */
14006 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14007 seq_dmabuf->time_stamp = jiffies;
14008 lpfc_update_rcv_time_stamp(vport);
14009 if (list_empty(&seq_dmabuf->dbuf.list)) {
14010 temp_hdr = dmabuf->hbuf.virt;
14011 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14012 return seq_dmabuf;
14013 }
14014 /* find the correct place in the sequence to insert this frame */
14015 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14016 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14017 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14018 /*
14019 * If the frame's sequence count is greater than the frame on
14020 * the list then insert the frame right after this frame
14021 */
14022 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14023 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14024 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14025 return seq_dmabuf;
14026 }
14027 }
14028 return NULL;
14029 }
14030
14031 /**
14032 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14033 * @vport: pointer to a vitural port
14034 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14035 *
14036 * This function tries to abort from the partially assembed sequence, described
14037 * by the information from basic abbort @dmabuf. It checks to see whether such
14038 * partially assembled sequence held by the driver. If so, it shall free up all
14039 * the frames from the partially assembled sequence.
14040 *
14041 * Return
14042 * true -- if there is matching partially assembled sequence present and all
14043 * the frames freed with the sequence;
14044 * false -- if there is no matching partially assembled sequence present so
14045 * nothing got aborted in the lower layer driver
14046 **/
14047 static bool
14048 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14049 struct hbq_dmabuf *dmabuf)
14050 {
14051 struct fc_frame_header *new_hdr;
14052 struct fc_frame_header *temp_hdr;
14053 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14054 struct hbq_dmabuf *seq_dmabuf = NULL;
14055
14056 /* Use the hdr_buf to find the sequence that matches this frame */
14057 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14058 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14059 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14060 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14061 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14062 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14063 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14064 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14065 continue;
14066 /* found a pending sequence that matches this frame */
14067 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14068 break;
14069 }
14070
14071 /* Free up all the frames from the partially assembled sequence */
14072 if (seq_dmabuf) {
14073 list_for_each_entry_safe(d_buf, n_buf,
14074 &seq_dmabuf->dbuf.list, list) {
14075 list_del_init(&d_buf->list);
14076 lpfc_in_buf_free(vport->phba, d_buf);
14077 }
14078 return true;
14079 }
14080 return false;
14081 }
14082
14083 /**
14084 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
14085 * @phba: Pointer to HBA context object.
14086 * @cmd_iocbq: pointer to the command iocbq structure.
14087 * @rsp_iocbq: pointer to the response iocbq structure.
14088 *
14089 * This function handles the sequence abort response iocb command complete
14090 * event. It properly releases the memory allocated to the sequence abort
14091 * accept iocb.
14092 **/
14093 static void
14094 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
14095 struct lpfc_iocbq *cmd_iocbq,
14096 struct lpfc_iocbq *rsp_iocbq)
14097 {
14098 if (cmd_iocbq)
14099 lpfc_sli_release_iocbq(phba, cmd_iocbq);
14100
14101 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14102 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14104 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14105 rsp_iocbq->iocb.ulpStatus,
14106 rsp_iocbq->iocb.un.ulpWord[4]);
14107 }
14108
14109 /**
14110 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14111 * @phba: Pointer to HBA context object.
14112 * @xri: xri id in transaction.
14113 *
14114 * This function validates the xri maps to the known range of XRIs allocated an
14115 * used by the driver.
14116 **/
14117 uint16_t
14118 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14119 uint16_t xri)
14120 {
14121 int i;
14122
14123 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14124 if (xri == phba->sli4_hba.xri_ids[i])
14125 return i;
14126 }
14127 return NO_XRI;
14128 }
14129
14130 /**
14131 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
14132 * @phba: Pointer to HBA context object.
14133 * @fc_hdr: pointer to a FC frame header.
14134 *
14135 * This function sends a basic response to a previous unsol sequence abort
14136 * event after aborting the sequence handling.
14137 **/
14138 static void
14139 lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
14140 struct fc_frame_header *fc_hdr)
14141 {
14142 struct lpfc_iocbq *ctiocb = NULL;
14143 struct lpfc_nodelist *ndlp;
14144 uint16_t oxid, rxid, xri, lxri;
14145 uint32_t sid, fctl;
14146 IOCB_t *icmd;
14147 int rc;
14148
14149 if (!lpfc_is_link_up(phba))
14150 return;
14151
14152 sid = sli4_sid_from_fc_hdr(fc_hdr);
14153 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
14154 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
14155
14156 ndlp = lpfc_findnode_did(phba->pport, sid);
14157 if (!ndlp) {
14158 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14159 "1268 Find ndlp returned NULL for oxid:x%x "
14160 "SID:x%x\n", oxid, sid);
14161 return;
14162 }
14163
14164 /* Allocate buffer for rsp iocb */
14165 ctiocb = lpfc_sli_get_iocbq(phba);
14166 if (!ctiocb)
14167 return;
14168
14169 /* Extract the F_CTL field from FC_HDR */
14170 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14171
14172 icmd = &ctiocb->iocb;
14173 icmd->un.xseq64.bdl.bdeSize = 0;
14174 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
14175 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14176 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14177 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14178
14179 /* Fill in the rest of iocb fields */
14180 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14181 icmd->ulpBdeCount = 0;
14182 icmd->ulpLe = 1;
14183 icmd->ulpClass = CLASS3;
14184 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
14185 ctiocb->context1 = ndlp;
14186
14187 ctiocb->iocb_cmpl = NULL;
14188 ctiocb->vport = phba->pport;
14189 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
14190 ctiocb->sli4_lxritag = NO_XRI;
14191 ctiocb->sli4_xritag = NO_XRI;
14192
14193 if (fctl & FC_FC_EX_CTX)
14194 /* Exchange responder sent the abort so we
14195 * own the oxid.
14196 */
14197 xri = oxid;
14198 else
14199 xri = rxid;
14200 lxri = lpfc_sli4_xri_inrange(phba, xri);
14201 if (lxri != NO_XRI)
14202 lpfc_set_rrq_active(phba, ndlp, lxri,
14203 (xri == oxid) ? rxid : oxid, 0);
14204 /* If the oxid maps to the FCP XRI range or if it is out of range,
14205 * send a BLS_RJT. The driver no longer has that exchange.
14206 * Override the IOCB for a BA_RJT.
14207 */
14208 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
14209 phba->sli4_hba.max_cfg_param.xri_base) ||
14210 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
14211 phba->sli4_hba.max_cfg_param.xri_base)) {
14212 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14213 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14214 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14215 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14216 }
14217
14218 if (fctl & FC_FC_EX_CTX) {
14219 /* ABTS sent by responder to CT exchange, construction
14220 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14221 * field and RX_ID from ABTS for RX_ID field.
14222 */
14223 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14224 } else {
14225 /* ABTS sent by initiator to CT exchange, construction
14226 * of BA_ACC will need to allocate a new XRI as for the
14227 * XRI_TAG field.
14228 */
14229 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14230 }
14231 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
14232 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
14233
14234 /* Xmit CT abts response on exchange <xid> */
14235 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14236 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14237 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14238
14239 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14240 if (rc == IOCB_ERROR) {
14241 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14242 "2925 Failed to issue CT ABTS RSP x%x on "
14243 "xri x%x, Data x%x\n",
14244 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14245 phba->link_state);
14246 lpfc_sli_release_iocbq(phba, ctiocb);
14247 }
14248 }
14249
14250 /**
14251 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14252 * @vport: Pointer to the vport on which this sequence was received
14253 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14254 *
14255 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14256 * receive sequence is only partially assembed by the driver, it shall abort
14257 * the partially assembled frames for the sequence. Otherwise, if the
14258 * unsolicited receive sequence has been completely assembled and passed to
14259 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14260 * unsolicited sequence has been aborted. After that, it will issue a basic
14261 * accept to accept the abort.
14262 **/
14263 void
14264 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14265 struct hbq_dmabuf *dmabuf)
14266 {
14267 struct lpfc_hba *phba = vport->phba;
14268 struct fc_frame_header fc_hdr;
14269 uint32_t fctl;
14270 bool abts_par;
14271
14272 /* Make a copy of fc_hdr before the dmabuf being released */
14273 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
14274 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
14275
14276 if (fctl & FC_FC_EX_CTX) {
14277 /*
14278 * ABTS sent by responder to exchange, just free the buffer
14279 */
14280 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14281 } else {
14282 /*
14283 * ABTS sent by initiator to exchange, need to do cleanup
14284 */
14285 /* Try to abort partially assembled seq */
14286 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14287
14288 /* Send abort to ULP if partially seq abort failed */
14289 if (abts_par == false)
14290 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14291 else
14292 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14293 }
14294 /* Send basic accept (BA_ACC) to the abort requester */
14295 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
14296 }
14297
14298 /**
14299 * lpfc_seq_complete - Indicates if a sequence is complete
14300 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14301 *
14302 * This function checks the sequence, starting with the frame described by
14303 * @dmabuf, to see if all the frames associated with this sequence are present.
14304 * the frames associated with this sequence are linked to the @dmabuf using the
14305 * dbuf list. This function looks for two major things. 1) That the first frame
14306 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14307 * set. 3) That there are no holes in the sequence count. The function will
14308 * return 1 when the sequence is complete, otherwise it will return 0.
14309 **/
14310 static int
14311 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14312 {
14313 struct fc_frame_header *hdr;
14314 struct lpfc_dmabuf *d_buf;
14315 struct hbq_dmabuf *seq_dmabuf;
14316 uint32_t fctl;
14317 int seq_count = 0;
14318
14319 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14320 /* make sure first fame of sequence has a sequence count of zero */
14321 if (hdr->fh_seq_cnt != seq_count)
14322 return 0;
14323 fctl = (hdr->fh_f_ctl[0] << 16 |
14324 hdr->fh_f_ctl[1] << 8 |
14325 hdr->fh_f_ctl[2]);
14326 /* If last frame of sequence we can return success. */
14327 if (fctl & FC_FC_END_SEQ)
14328 return 1;
14329 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14330 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14331 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14332 /* If there is a hole in the sequence count then fail. */
14333 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
14334 return 0;
14335 fctl = (hdr->fh_f_ctl[0] << 16 |
14336 hdr->fh_f_ctl[1] << 8 |
14337 hdr->fh_f_ctl[2]);
14338 /* If last frame of sequence we can return success. */
14339 if (fctl & FC_FC_END_SEQ)
14340 return 1;
14341 }
14342 return 0;
14343 }
14344
14345 /**
14346 * lpfc_prep_seq - Prep sequence for ULP processing
14347 * @vport: Pointer to the vport on which this sequence was received
14348 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14349 *
14350 * This function takes a sequence, described by a list of frames, and creates
14351 * a list of iocbq structures to describe the sequence. This iocbq list will be
14352 * used to issue to the generic unsolicited sequence handler. This routine
14353 * returns a pointer to the first iocbq in the list. If the function is unable
14354 * to allocate an iocbq then it throw out the received frames that were not
14355 * able to be described and return a pointer to the first iocbq. If unable to
14356 * allocate any iocbqs (including the first) this function will return NULL.
14357 **/
14358 static struct lpfc_iocbq *
14359 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14360 {
14361 struct hbq_dmabuf *hbq_buf;
14362 struct lpfc_dmabuf *d_buf, *n_buf;
14363 struct lpfc_iocbq *first_iocbq, *iocbq;
14364 struct fc_frame_header *fc_hdr;
14365 uint32_t sid;
14366 uint32_t len, tot_len;
14367 struct ulp_bde64 *pbde;
14368
14369 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14370 /* remove from receive buffer list */
14371 list_del_init(&seq_dmabuf->hbuf.list);
14372 lpfc_update_rcv_time_stamp(vport);
14373 /* get the Remote Port's SID */
14374 sid = sli4_sid_from_fc_hdr(fc_hdr);
14375 tot_len = 0;
14376 /* Get an iocbq struct to fill in. */
14377 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14378 if (first_iocbq) {
14379 /* Initialize the first IOCB. */
14380 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
14381 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14382
14383 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14384 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14385 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14386 first_iocbq->iocb.un.rcvels.parmRo =
14387 sli4_did_from_fc_hdr(fc_hdr);
14388 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14389 } else
14390 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
14391 first_iocbq->iocb.ulpContext = NO_XRI;
14392 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14393 be16_to_cpu(fc_hdr->fh_ox_id);
14394 /* iocbq is prepped for internal consumption. Physical vpi. */
14395 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14396 vport->phba->vpi_ids[vport->vpi];
14397 /* put the first buffer into the first IOCBq */
14398 first_iocbq->context2 = &seq_dmabuf->dbuf;
14399 first_iocbq->context3 = NULL;
14400 first_iocbq->iocb.ulpBdeCount = 1;
14401 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14402 LPFC_DATA_BUF_SIZE;
14403 first_iocbq->iocb.un.rcvels.remoteID = sid;
14404 tot_len = bf_get(lpfc_rcqe_length,
14405 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14406 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14407 }
14408 iocbq = first_iocbq;
14409 /*
14410 * Each IOCBq can have two Buffers assigned, so go through the list
14411 * of buffers for this sequence and save two buffers in each IOCBq
14412 */
14413 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14414 if (!iocbq) {
14415 lpfc_in_buf_free(vport->phba, d_buf);
14416 continue;
14417 }
14418 if (!iocbq->context3) {
14419 iocbq->context3 = d_buf;
14420 iocbq->iocb.ulpBdeCount++;
14421 pbde = (struct ulp_bde64 *)
14422 &iocbq->iocb.unsli3.sli3Words[4];
14423 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14424
14425 /* We need to get the size out of the right CQE */
14426 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14427 len = bf_get(lpfc_rcqe_length,
14428 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14429 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14430 tot_len += len;
14431 } else {
14432 iocbq = lpfc_sli_get_iocbq(vport->phba);
14433 if (!iocbq) {
14434 if (first_iocbq) {
14435 first_iocbq->iocb.ulpStatus =
14436 IOSTAT_FCP_RSP_ERROR;
14437 first_iocbq->iocb.un.ulpWord[4] =
14438 IOERR_NO_RESOURCES;
14439 }
14440 lpfc_in_buf_free(vport->phba, d_buf);
14441 continue;
14442 }
14443 iocbq->context2 = d_buf;
14444 iocbq->context3 = NULL;
14445 iocbq->iocb.ulpBdeCount = 1;
14446 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14447 LPFC_DATA_BUF_SIZE;
14448
14449 /* We need to get the size out of the right CQE */
14450 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14451 len = bf_get(lpfc_rcqe_length,
14452 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14453 tot_len += len;
14454 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14455
14456 iocbq->iocb.un.rcvels.remoteID = sid;
14457 list_add_tail(&iocbq->list, &first_iocbq->list);
14458 }
14459 }
14460 return first_iocbq;
14461 }
14462
14463 static void
14464 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14465 struct hbq_dmabuf *seq_dmabuf)
14466 {
14467 struct fc_frame_header *fc_hdr;
14468 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14469 struct lpfc_hba *phba = vport->phba;
14470
14471 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14472 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14473 if (!iocbq) {
14474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14475 "2707 Ring %d handler: Failed to allocate "
14476 "iocb Rctl x%x Type x%x received\n",
14477 LPFC_ELS_RING,
14478 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14479 return;
14480 }
14481 if (!lpfc_complete_unsol_iocb(phba,
14482 &phba->sli.ring[LPFC_ELS_RING],
14483 iocbq, fc_hdr->fh_r_ctl,
14484 fc_hdr->fh_type))
14485 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14486 "2540 Ring %d handler: unexpected Rctl "
14487 "x%x Type x%x received\n",
14488 LPFC_ELS_RING,
14489 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14490
14491 /* Free iocb created in lpfc_prep_seq */
14492 list_for_each_entry_safe(curr_iocb, next_iocb,
14493 &iocbq->list, list) {
14494 list_del_init(&curr_iocb->list);
14495 lpfc_sli_release_iocbq(phba, curr_iocb);
14496 }
14497 lpfc_sli_release_iocbq(phba, iocbq);
14498 }
14499
14500 /**
14501 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14502 * @phba: Pointer to HBA context object.
14503 *
14504 * This function is called with no lock held. This function processes all
14505 * the received buffers and gives it to upper layers when a received buffer
14506 * indicates that it is the final frame in the sequence. The interrupt
14507 * service routine processes received buffers at interrupt contexts and adds
14508 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14509 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14510 * appropriate receive function when the final frame in a sequence is received.
14511 **/
14512 void
14513 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14514 struct hbq_dmabuf *dmabuf)
14515 {
14516 struct hbq_dmabuf *seq_dmabuf;
14517 struct fc_frame_header *fc_hdr;
14518 struct lpfc_vport *vport;
14519 uint32_t fcfi;
14520 uint32_t did;
14521
14522 /* Process each received buffer */
14523 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14524 /* check to see if this a valid type of frame */
14525 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14526 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14527 return;
14528 }
14529 if ((bf_get(lpfc_cqe_code,
14530 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14531 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14532 &dmabuf->cq_event.cqe.rcqe_cmpl);
14533 else
14534 fcfi = bf_get(lpfc_rcqe_fcf_id,
14535 &dmabuf->cq_event.cqe.rcqe_cmpl);
14536
14537 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
14538 if (!vport) {
14539 /* throw out the frame */
14540 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14541 return;
14542 }
14543
14544 /* d_id this frame is directed to */
14545 did = sli4_did_from_fc_hdr(fc_hdr);
14546
14547 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14548 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14549 (did != Fabric_DID)) {
14550 /*
14551 * Throw out the frame if we are not pt2pt.
14552 * The pt2pt protocol allows for discovery frames
14553 * to be received without a registered VPI.
14554 */
14555 if (!(vport->fc_flag & FC_PT2PT) ||
14556 (phba->link_state == LPFC_HBA_READY)) {
14557 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14558 return;
14559 }
14560 }
14561
14562 /* Handle the basic abort sequence (BA_ABTS) event */
14563 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14564 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14565 return;
14566 }
14567
14568 /* Link this frame */
14569 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14570 if (!seq_dmabuf) {
14571 /* unable to add frame to vport - throw it out */
14572 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14573 return;
14574 }
14575 /* If not last frame in sequence continue processing frames. */
14576 if (!lpfc_seq_complete(seq_dmabuf))
14577 return;
14578
14579 /* Send the complete sequence to the upper layer protocol */
14580 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
14581 }
14582
14583 /**
14584 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14585 * @phba: pointer to lpfc hba data structure.
14586 *
14587 * This routine is invoked to post rpi header templates to the
14588 * HBA consistent with the SLI-4 interface spec. This routine
14589 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14590 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14591 *
14592 * This routine does not require any locks. It's usage is expected
14593 * to be driver load or reset recovery when the driver is
14594 * sequential.
14595 *
14596 * Return codes
14597 * 0 - successful
14598 * -EIO - The mailbox failed to complete successfully.
14599 * When this error occurs, the driver is not guaranteed
14600 * to have any rpi regions posted to the device and
14601 * must either attempt to repost the regions or take a
14602 * fatal error.
14603 **/
14604 int
14605 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14606 {
14607 struct lpfc_rpi_hdr *rpi_page;
14608 uint32_t rc = 0;
14609 uint16_t lrpi = 0;
14610
14611 /* SLI4 ports that support extents do not require RPI headers. */
14612 if (!phba->sli4_hba.rpi_hdrs_in_use)
14613 goto exit;
14614 if (phba->sli4_hba.extents_in_use)
14615 return -EIO;
14616
14617 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
14618 /*
14619 * Assign the rpi headers a physical rpi only if the driver
14620 * has not initialized those resources. A port reset only
14621 * needs the headers posted.
14622 */
14623 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14624 LPFC_RPI_RSRC_RDY)
14625 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14626
14627 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14628 if (rc != MBX_SUCCESS) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14630 "2008 Error %d posting all rpi "
14631 "headers\n", rc);
14632 rc = -EIO;
14633 break;
14634 }
14635 }
14636
14637 exit:
14638 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14639 LPFC_RPI_RSRC_RDY);
14640 return rc;
14641 }
14642
14643 /**
14644 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14645 * @phba: pointer to lpfc hba data structure.
14646 * @rpi_page: pointer to the rpi memory region.
14647 *
14648 * This routine is invoked to post a single rpi header to the
14649 * HBA consistent with the SLI-4 interface spec. This memory region
14650 * maps up to 64 rpi context regions.
14651 *
14652 * Return codes
14653 * 0 - successful
14654 * -ENOMEM - No available memory
14655 * -EIO - The mailbox failed to complete successfully.
14656 **/
14657 int
14658 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14659 {
14660 LPFC_MBOXQ_t *mboxq;
14661 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14662 uint32_t rc = 0;
14663 uint32_t shdr_status, shdr_add_status;
14664 union lpfc_sli4_cfg_shdr *shdr;
14665
14666 /* SLI4 ports that support extents do not require RPI headers. */
14667 if (!phba->sli4_hba.rpi_hdrs_in_use)
14668 return rc;
14669 if (phba->sli4_hba.extents_in_use)
14670 return -EIO;
14671
14672 /* The port is notified of the header region via a mailbox command. */
14673 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14674 if (!mboxq) {
14675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14676 "2001 Unable to allocate memory for issuing "
14677 "SLI_CONFIG_SPECIAL mailbox command\n");
14678 return -ENOMEM;
14679 }
14680
14681 /* Post all rpi memory regions to the port. */
14682 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
14683 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14684 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14685 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
14686 sizeof(struct lpfc_sli4_cfg_mhdr),
14687 LPFC_SLI4_MBX_EMBED);
14688
14689
14690 /* Post the physical rpi to the port for this rpi header. */
14691 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14692 rpi_page->start_rpi);
14693 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14694 hdr_tmpl, rpi_page->page_count);
14695
14696 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14697 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
14698 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
14699 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14702 if (rc != MBX_TIMEOUT)
14703 mempool_free(mboxq, phba->mbox_mem_pool);
14704 if (shdr_status || shdr_add_status || rc) {
14705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14706 "2514 POST_RPI_HDR mailbox failed with "
14707 "status x%x add_status x%x, mbx status x%x\n",
14708 shdr_status, shdr_add_status, rc);
14709 rc = -ENXIO;
14710 }
14711 return rc;
14712 }
14713
14714 /**
14715 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14716 * @phba: pointer to lpfc hba data structure.
14717 *
14718 * This routine is invoked to post rpi header templates to the
14719 * HBA consistent with the SLI-4 interface spec. This routine
14720 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14721 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14722 *
14723 * Returns
14724 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14725 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14726 **/
14727 int
14728 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14729 {
14730 unsigned long rpi;
14731 uint16_t max_rpi, rpi_limit;
14732 uint16_t rpi_remaining, lrpi = 0;
14733 struct lpfc_rpi_hdr *rpi_hdr;
14734
14735 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
14736 rpi_limit = phba->sli4_hba.next_rpi;
14737
14738 /*
14739 * Fetch the next logical rpi. Because this index is logical,
14740 * the driver starts at 0 each time.
14741 */
14742 spin_lock_irq(&phba->hbalock);
14743 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14744 if (rpi >= rpi_limit)
14745 rpi = LPFC_RPI_ALLOC_ERROR;
14746 else {
14747 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14748 phba->sli4_hba.max_cfg_param.rpi_used++;
14749 phba->sli4_hba.rpi_count++;
14750 }
14751
14752 /*
14753 * Don't try to allocate more rpi header regions if the device limit
14754 * has been exhausted.
14755 */
14756 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14757 (phba->sli4_hba.rpi_count >= max_rpi)) {
14758 spin_unlock_irq(&phba->hbalock);
14759 return rpi;
14760 }
14761
14762 /*
14763 * RPI header postings are not required for SLI4 ports capable of
14764 * extents.
14765 */
14766 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14767 spin_unlock_irq(&phba->hbalock);
14768 return rpi;
14769 }
14770
14771 /*
14772 * If the driver is running low on rpi resources, allocate another
14773 * page now. Note that the next_rpi value is used because
14774 * it represents how many are actually in use whereas max_rpi notes
14775 * how many are supported max by the device.
14776 */
14777 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
14778 spin_unlock_irq(&phba->hbalock);
14779 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14780 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14781 if (!rpi_hdr) {
14782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14783 "2002 Error Could not grow rpi "
14784 "count\n");
14785 } else {
14786 lrpi = rpi_hdr->start_rpi;
14787 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14788 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14789 }
14790 }
14791
14792 return rpi;
14793 }
14794
14795 /**
14796 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14797 * @phba: pointer to lpfc hba data structure.
14798 *
14799 * This routine is invoked to release an rpi to the pool of
14800 * available rpis maintained by the driver.
14801 **/
14802 void
14803 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14804 {
14805 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14806 phba->sli4_hba.rpi_count--;
14807 phba->sli4_hba.max_cfg_param.rpi_used--;
14808 }
14809 }
14810
14811 /**
14812 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14813 * @phba: pointer to lpfc hba data structure.
14814 *
14815 * This routine is invoked to release an rpi to the pool of
14816 * available rpis maintained by the driver.
14817 **/
14818 void
14819 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14820 {
14821 spin_lock_irq(&phba->hbalock);
14822 __lpfc_sli4_free_rpi(phba, rpi);
14823 spin_unlock_irq(&phba->hbalock);
14824 }
14825
14826 /**
14827 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14828 * @phba: pointer to lpfc hba data structure.
14829 *
14830 * This routine is invoked to remove the memory region that
14831 * provided rpi via a bitmask.
14832 **/
14833 void
14834 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14835 {
14836 kfree(phba->sli4_hba.rpi_bmask);
14837 kfree(phba->sli4_hba.rpi_ids);
14838 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
14839 }
14840
14841 /**
14842 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14843 * @phba: pointer to lpfc hba data structure.
14844 *
14845 * This routine is invoked to remove the memory region that
14846 * provided rpi via a bitmask.
14847 **/
14848 int
14849 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14850 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
14851 {
14852 LPFC_MBOXQ_t *mboxq;
14853 struct lpfc_hba *phba = ndlp->phba;
14854 int rc;
14855
14856 /* The port is notified of the header region via a mailbox command. */
14857 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14858 if (!mboxq)
14859 return -ENOMEM;
14860
14861 /* Post all rpi memory regions to the port. */
14862 lpfc_resume_rpi(mboxq, ndlp);
14863 if (cmpl) {
14864 mboxq->mbox_cmpl = cmpl;
14865 mboxq->context1 = arg;
14866 mboxq->context2 = ndlp;
14867 } else
14868 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14869 mboxq->vport = ndlp->vport;
14870 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14871 if (rc == MBX_NOT_FINISHED) {
14872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14873 "2010 Resume RPI Mailbox failed "
14874 "status %d, mbxStatus x%x\n", rc,
14875 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14876 mempool_free(mboxq, phba->mbox_mem_pool);
14877 return -EIO;
14878 }
14879 return 0;
14880 }
14881
14882 /**
14883 * lpfc_sli4_init_vpi - Initialize a vpi with the port
14884 * @vport: Pointer to the vport for which the vpi is being initialized
14885 *
14886 * This routine is invoked to activate a vpi with the port.
14887 *
14888 * Returns:
14889 * 0 success
14890 * -Evalue otherwise
14891 **/
14892 int
14893 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
14894 {
14895 LPFC_MBOXQ_t *mboxq;
14896 int rc = 0;
14897 int retval = MBX_SUCCESS;
14898 uint32_t mbox_tmo;
14899 struct lpfc_hba *phba = vport->phba;
14900 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14901 if (!mboxq)
14902 return -ENOMEM;
14903 lpfc_init_vpi(phba, mboxq, vport->vpi);
14904 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
14905 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
14906 if (rc != MBX_SUCCESS) {
14907 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
14908 "2022 INIT VPI Mailbox failed "
14909 "status %d, mbxStatus x%x\n", rc,
14910 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14911 retval = -EIO;
14912 }
14913 if (rc != MBX_TIMEOUT)
14914 mempool_free(mboxq, vport->phba->mbox_mem_pool);
14915
14916 return retval;
14917 }
14918
14919 /**
14920 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14921 * @phba: pointer to lpfc hba data structure.
14922 * @mboxq: Pointer to mailbox object.
14923 *
14924 * This routine is invoked to manually add a single FCF record. The caller
14925 * must pass a completely initialized FCF_Record. This routine takes
14926 * care of the nonembedded mailbox operations.
14927 **/
14928 static void
14929 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14930 {
14931 void *virt_addr;
14932 union lpfc_sli4_cfg_shdr *shdr;
14933 uint32_t shdr_status, shdr_add_status;
14934
14935 virt_addr = mboxq->sge_array->addr[0];
14936 /* The IOCTL status is embedded in the mailbox subheader. */
14937 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14938 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14939 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14940
14941 if ((shdr_status || shdr_add_status) &&
14942 (shdr_status != STATUS_FCF_IN_USE))
14943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14944 "2558 ADD_FCF_RECORD mailbox failed with "
14945 "status x%x add_status x%x\n",
14946 shdr_status, shdr_add_status);
14947
14948 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14949 }
14950
14951 /**
14952 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14953 * @phba: pointer to lpfc hba data structure.
14954 * @fcf_record: pointer to the initialized fcf record to add.
14955 *
14956 * This routine is invoked to manually add a single FCF record. The caller
14957 * must pass a completely initialized FCF_Record. This routine takes
14958 * care of the nonembedded mailbox operations.
14959 **/
14960 int
14961 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14962 {
14963 int rc = 0;
14964 LPFC_MBOXQ_t *mboxq;
14965 uint8_t *bytep;
14966 void *virt_addr;
14967 dma_addr_t phys_addr;
14968 struct lpfc_mbx_sge sge;
14969 uint32_t alloc_len, req_len;
14970 uint32_t fcfindex;
14971
14972 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14973 if (!mboxq) {
14974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14975 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14976 return -ENOMEM;
14977 }
14978
14979 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14980 sizeof(uint32_t);
14981
14982 /* Allocate DMA memory and set up the non-embedded mailbox command */
14983 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14984 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14985 req_len, LPFC_SLI4_MBX_NEMBED);
14986 if (alloc_len < req_len) {
14987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14988 "2523 Allocated DMA memory size (x%x) is "
14989 "less than the requested DMA memory "
14990 "size (x%x)\n", alloc_len, req_len);
14991 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14992 return -ENOMEM;
14993 }
14994
14995 /*
14996 * Get the first SGE entry from the non-embedded DMA memory. This
14997 * routine only uses a single SGE.
14998 */
14999 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15000 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
15001 virt_addr = mboxq->sge_array->addr[0];
15002 /*
15003 * Configure the FCF record for FCFI 0. This is the driver's
15004 * hardcoded default and gets used in nonFIP mode.
15005 */
15006 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15007 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15008 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15009
15010 /*
15011 * Copy the fcf_index and the FCF Record Data. The data starts after
15012 * the FCoE header plus word10. The data copy needs to be endian
15013 * correct.
15014 */
15015 bytep += sizeof(uint32_t);
15016 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15017 mboxq->vport = phba->pport;
15018 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15019 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15020 if (rc == MBX_NOT_FINISHED) {
15021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15022 "2515 ADD_FCF_RECORD mailbox failed with "
15023 "status 0x%x\n", rc);
15024 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15025 rc = -EIO;
15026 } else
15027 rc = 0;
15028
15029 return rc;
15030 }
15031
15032 /**
15033 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15034 * @phba: pointer to lpfc hba data structure.
15035 * @fcf_record: pointer to the fcf record to write the default data.
15036 * @fcf_index: FCF table entry index.
15037 *
15038 * This routine is invoked to build the driver's default FCF record. The
15039 * values used are hardcoded. This routine handles memory initialization.
15040 *
15041 **/
15042 void
15043 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15044 struct fcf_record *fcf_record,
15045 uint16_t fcf_index)
15046 {
15047 memset(fcf_record, 0, sizeof(struct fcf_record));
15048 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15049 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15050 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15051 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15052 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15053 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15054 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15055 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15056 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15057 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15058 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15059 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15060 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
15061 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
15062 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15063 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15064 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15065 /* Set the VLAN bit map */
15066 if (phba->valid_vlan) {
15067 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15068 = 1 << (phba->vlan_id % 8);
15069 }
15070 }
15071
15072 /**
15073 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
15074 * @phba: pointer to lpfc hba data structure.
15075 * @fcf_index: FCF table entry offset.
15076 *
15077 * This routine is invoked to scan the entire FCF table by reading FCF
15078 * record and processing it one at a time starting from the @fcf_index
15079 * for initial FCF discovery or fast FCF failover rediscovery.
15080 *
15081 * Return 0 if the mailbox command is submitted successfully, none 0
15082 * otherwise.
15083 **/
15084 int
15085 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15086 {
15087 int rc = 0, error;
15088 LPFC_MBOXQ_t *mboxq;
15089
15090 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
15091 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
15092 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15093 if (!mboxq) {
15094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15095 "2000 Failed to allocate mbox for "
15096 "READ_FCF cmd\n");
15097 error = -ENOMEM;
15098 goto fail_fcf_scan;
15099 }
15100 /* Construct the read FCF record mailbox command */
15101 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15102 if (rc) {
15103 error = -EINVAL;
15104 goto fail_fcf_scan;
15105 }
15106 /* Issue the mailbox command asynchronously */
15107 mboxq->vport = phba->pport;
15108 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
15109
15110 spin_lock_irq(&phba->hbalock);
15111 phba->hba_flag |= FCF_TS_INPROG;
15112 spin_unlock_irq(&phba->hbalock);
15113
15114 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15115 if (rc == MBX_NOT_FINISHED)
15116 error = -EIO;
15117 else {
15118 /* Reset eligible FCF count for new scan */
15119 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
15120 phba->fcf.eligible_fcf_cnt = 0;
15121 error = 0;
15122 }
15123 fail_fcf_scan:
15124 if (error) {
15125 if (mboxq)
15126 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15127 /* FCF scan failed, clear FCF_TS_INPROG flag */
15128 spin_lock_irq(&phba->hbalock);
15129 phba->hba_flag &= ~FCF_TS_INPROG;
15130 spin_unlock_irq(&phba->hbalock);
15131 }
15132 return error;
15133 }
15134
15135 /**
15136 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
15137 * @phba: pointer to lpfc hba data structure.
15138 * @fcf_index: FCF table entry offset.
15139 *
15140 * This routine is invoked to read an FCF record indicated by @fcf_index
15141 * and to use it for FLOGI roundrobin FCF failover.
15142 *
15143 * Return 0 if the mailbox command is submitted successfully, none 0
15144 * otherwise.
15145 **/
15146 int
15147 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15148 {
15149 int rc = 0, error;
15150 LPFC_MBOXQ_t *mboxq;
15151
15152 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15153 if (!mboxq) {
15154 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15155 "2763 Failed to allocate mbox for "
15156 "READ_FCF cmd\n");
15157 error = -ENOMEM;
15158 goto fail_fcf_read;
15159 }
15160 /* Construct the read FCF record mailbox command */
15161 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15162 if (rc) {
15163 error = -EINVAL;
15164 goto fail_fcf_read;
15165 }
15166 /* Issue the mailbox command asynchronously */
15167 mboxq->vport = phba->pport;
15168 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15169 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15170 if (rc == MBX_NOT_FINISHED)
15171 error = -EIO;
15172 else
15173 error = 0;
15174
15175 fail_fcf_read:
15176 if (error && mboxq)
15177 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15178 return error;
15179 }
15180
15181 /**
15182 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15183 * @phba: pointer to lpfc hba data structure.
15184 * @fcf_index: FCF table entry offset.
15185 *
15186 * This routine is invoked to read an FCF record indicated by @fcf_index to
15187 * determine whether it's eligible for FLOGI roundrobin failover list.
15188 *
15189 * Return 0 if the mailbox command is submitted successfully, none 0
15190 * otherwise.
15191 **/
15192 int
15193 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15194 {
15195 int rc = 0, error;
15196 LPFC_MBOXQ_t *mboxq;
15197
15198 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15199 if (!mboxq) {
15200 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15201 "2758 Failed to allocate mbox for "
15202 "READ_FCF cmd\n");
15203 error = -ENOMEM;
15204 goto fail_fcf_read;
15205 }
15206 /* Construct the read FCF record mailbox command */
15207 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15208 if (rc) {
15209 error = -EINVAL;
15210 goto fail_fcf_read;
15211 }
15212 /* Issue the mailbox command asynchronously */
15213 mboxq->vport = phba->pport;
15214 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15216 if (rc == MBX_NOT_FINISHED)
15217 error = -EIO;
15218 else
15219 error = 0;
15220
15221 fail_fcf_read:
15222 if (error && mboxq)
15223 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15224 return error;
15225 }
15226
15227 /**
15228 * lpfc_check_next_fcf_pri
15229 * phba pointer to the lpfc_hba struct for this port.
15230 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15231 * routine when the rr_bmask is empty. The FCF indecies are put into the
15232 * rr_bmask based on their priority level. Starting from the highest priority
15233 * to the lowest. The most likely FCF candidate will be in the highest
15234 * priority group. When this routine is called it searches the fcf_pri list for
15235 * next lowest priority group and repopulates the rr_bmask with only those
15236 * fcf_indexes.
15237 * returns:
15238 * 1=success 0=failure
15239 **/
15240 int
15241 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15242 {
15243 uint16_t next_fcf_pri;
15244 uint16_t last_index;
15245 struct lpfc_fcf_pri *fcf_pri;
15246 int rc;
15247 int ret = 0;
15248
15249 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15250 LPFC_SLI4_FCF_TBL_INDX_MAX);
15251 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15252 "3060 Last IDX %d\n", last_index);
15253 if (list_empty(&phba->fcf.fcf_pri_list)) {
15254 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15255 "3061 Last IDX %d\n", last_index);
15256 return 0; /* Empty rr list */
15257 }
15258 next_fcf_pri = 0;
15259 /*
15260 * Clear the rr_bmask and set all of the bits that are at this
15261 * priority.
15262 */
15263 memset(phba->fcf.fcf_rr_bmask, 0,
15264 sizeof(*phba->fcf.fcf_rr_bmask));
15265 spin_lock_irq(&phba->hbalock);
15266 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15267 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15268 continue;
15269 /*
15270 * the 1st priority that has not FLOGI failed
15271 * will be the highest.
15272 */
15273 if (!next_fcf_pri)
15274 next_fcf_pri = fcf_pri->fcf_rec.priority;
15275 spin_unlock_irq(&phba->hbalock);
15276 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15277 rc = lpfc_sli4_fcf_rr_index_set(phba,
15278 fcf_pri->fcf_rec.fcf_index);
15279 if (rc)
15280 return 0;
15281 }
15282 spin_lock_irq(&phba->hbalock);
15283 }
15284 /*
15285 * if next_fcf_pri was not set above and the list is not empty then
15286 * we have failed flogis on all of them. So reset flogi failed
15287 * and start at the begining.
15288 */
15289 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15290 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15291 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15292 /*
15293 * the 1st priority that has not FLOGI failed
15294 * will be the highest.
15295 */
15296 if (!next_fcf_pri)
15297 next_fcf_pri = fcf_pri->fcf_rec.priority;
15298 spin_unlock_irq(&phba->hbalock);
15299 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15300 rc = lpfc_sli4_fcf_rr_index_set(phba,
15301 fcf_pri->fcf_rec.fcf_index);
15302 if (rc)
15303 return 0;
15304 }
15305 spin_lock_irq(&phba->hbalock);
15306 }
15307 } else
15308 ret = 1;
15309 spin_unlock_irq(&phba->hbalock);
15310
15311 return ret;
15312 }
15313 /**
15314 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15315 * @phba: pointer to lpfc hba data structure.
15316 *
15317 * This routine is to get the next eligible FCF record index in a round
15318 * robin fashion. If the next eligible FCF record index equals to the
15319 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
15320 * shall be returned, otherwise, the next eligible FCF record's index
15321 * shall be returned.
15322 **/
15323 uint16_t
15324 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15325 {
15326 uint16_t next_fcf_index;
15327
15328 /* Search start from next bit of currently registered FCF index */
15329 next_priority:
15330 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15331 LPFC_SLI4_FCF_TBL_INDX_MAX;
15332 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15333 LPFC_SLI4_FCF_TBL_INDX_MAX,
15334 next_fcf_index);
15335
15336 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
15337 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15338 /*
15339 * If we have wrapped then we need to clear the bits that
15340 * have been tested so that we can detect when we should
15341 * change the priority level.
15342 */
15343 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15344 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
15345 }
15346
15347
15348 /* Check roundrobin failover list empty condition */
15349 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15350 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15351 /*
15352 * If next fcf index is not found check if there are lower
15353 * Priority level fcf's in the fcf_priority list.
15354 * Set up the rr_bmask with all of the avaiable fcf bits
15355 * at that level and continue the selection process.
15356 */
15357 if (lpfc_check_next_fcf_pri_level(phba))
15358 goto next_priority;
15359 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15360 "2844 No roundrobin failover FCF available\n");
15361 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15362 return LPFC_FCOE_FCF_NEXT_NONE;
15363 else {
15364 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15365 "3063 Only FCF available idx %d, flag %x\n",
15366 next_fcf_index,
15367 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15368 return next_fcf_index;
15369 }
15370 }
15371
15372 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15373 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15374 LPFC_FCF_FLOGI_FAILED)
15375 goto next_priority;
15376
15377 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15378 "2845 Get next roundrobin failover FCF (x%x)\n",
15379 next_fcf_index);
15380
15381 return next_fcf_index;
15382 }
15383
15384 /**
15385 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15386 * @phba: pointer to lpfc hba data structure.
15387 *
15388 * This routine sets the FCF record index in to the eligible bmask for
15389 * roundrobin failover search. It checks to make sure that the index
15390 * does not go beyond the range of the driver allocated bmask dimension
15391 * before setting the bit.
15392 *
15393 * Returns 0 if the index bit successfully set, otherwise, it returns
15394 * -EINVAL.
15395 **/
15396 int
15397 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15398 {
15399 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15400 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15401 "2610 FCF (x%x) reached driver's book "
15402 "keeping dimension:x%x\n",
15403 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15404 return -EINVAL;
15405 }
15406 /* Set the eligible FCF record index bmask */
15407 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15408
15409 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15410 "2790 Set FCF (x%x) to roundrobin FCF failover "
15411 "bmask\n", fcf_index);
15412
15413 return 0;
15414 }
15415
15416 /**
15417 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
15418 * @phba: pointer to lpfc hba data structure.
15419 *
15420 * This routine clears the FCF record index from the eligible bmask for
15421 * roundrobin failover search. It checks to make sure that the index
15422 * does not go beyond the range of the driver allocated bmask dimension
15423 * before clearing the bit.
15424 **/
15425 void
15426 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15427 {
15428 struct lpfc_fcf_pri *fcf_pri;
15429 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15430 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15431 "2762 FCF (x%x) reached driver's book "
15432 "keeping dimension:x%x\n",
15433 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15434 return;
15435 }
15436 /* Clear the eligible FCF record index bmask */
15437 spin_lock_irq(&phba->hbalock);
15438 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15439 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15440 list_del_init(&fcf_pri->list);
15441 break;
15442 }
15443 }
15444 spin_unlock_irq(&phba->hbalock);
15445 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15446
15447 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15448 "2791 Clear FCF (x%x) from roundrobin failover "
15449 "bmask\n", fcf_index);
15450 }
15451
15452 /**
15453 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15454 * @phba: pointer to lpfc hba data structure.
15455 *
15456 * This routine is the completion routine for the rediscover FCF table mailbox
15457 * command. If the mailbox command returned failure, it will try to stop the
15458 * FCF rediscover wait timer.
15459 **/
15460 void
15461 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15462 {
15463 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15464 uint32_t shdr_status, shdr_add_status;
15465
15466 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15467
15468 shdr_status = bf_get(lpfc_mbox_hdr_status,
15469 &redisc_fcf->header.cfg_shdr.response);
15470 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15471 &redisc_fcf->header.cfg_shdr.response);
15472 if (shdr_status || shdr_add_status) {
15473 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15474 "2746 Requesting for FCF rediscovery failed "
15475 "status x%x add_status x%x\n",
15476 shdr_status, shdr_add_status);
15477 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
15478 spin_lock_irq(&phba->hbalock);
15479 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
15480 spin_unlock_irq(&phba->hbalock);
15481 /*
15482 * CVL event triggered FCF rediscover request failed,
15483 * last resort to re-try current registered FCF entry.
15484 */
15485 lpfc_retry_pport_discovery(phba);
15486 } else {
15487 spin_lock_irq(&phba->hbalock);
15488 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
15489 spin_unlock_irq(&phba->hbalock);
15490 /*
15491 * DEAD FCF event triggered FCF rediscover request
15492 * failed, last resort to fail over as a link down
15493 * to FCF registration.
15494 */
15495 lpfc_sli4_fcf_dead_failthrough(phba);
15496 }
15497 } else {
15498 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15499 "2775 Start FCF rediscover quiescent timer\n");
15500 /*
15501 * Start FCF rediscovery wait timer for pending FCF
15502 * before rescan FCF record table.
15503 */
15504 lpfc_fcf_redisc_wait_start_timer(phba);
15505 }
15506
15507 mempool_free(mbox, phba->mbox_mem_pool);
15508 }
15509
15510 /**
15511 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
15512 * @phba: pointer to lpfc hba data structure.
15513 *
15514 * This routine is invoked to request for rediscovery of the entire FCF table
15515 * by the port.
15516 **/
15517 int
15518 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15519 {
15520 LPFC_MBOXQ_t *mbox;
15521 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15522 int rc, length;
15523
15524 /* Cancel retry delay timers to all vports before FCF rediscover */
15525 lpfc_cancel_all_vport_retry_delay_timer(phba);
15526
15527 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15528 if (!mbox) {
15529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15530 "2745 Failed to allocate mbox for "
15531 "requesting FCF rediscover.\n");
15532 return -ENOMEM;
15533 }
15534
15535 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15536 sizeof(struct lpfc_sli4_cfg_mhdr));
15537 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15538 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15539 length, LPFC_SLI4_MBX_EMBED);
15540
15541 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15542 /* Set count to 0 for invalidating the entire FCF database */
15543 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15544
15545 /* Issue the mailbox command asynchronously */
15546 mbox->vport = phba->pport;
15547 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15548 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15549
15550 if (rc == MBX_NOT_FINISHED) {
15551 mempool_free(mbox, phba->mbox_mem_pool);
15552 return -EIO;
15553 }
15554 return 0;
15555 }
15556
15557 /**
15558 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15559 * @phba: pointer to lpfc hba data structure.
15560 *
15561 * This function is the failover routine as a last resort to the FCF DEAD
15562 * event when driver failed to perform fast FCF failover.
15563 **/
15564 void
15565 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15566 {
15567 uint32_t link_state;
15568
15569 /*
15570 * Last resort as FCF DEAD event failover will treat this as
15571 * a link down, but save the link state because we don't want
15572 * it to be changed to Link Down unless it is already down.
15573 */
15574 link_state = phba->link_state;
15575 lpfc_linkdown(phba);
15576 phba->link_state = link_state;
15577
15578 /* Unregister FCF if no devices connected to it */
15579 lpfc_unregister_unused_fcf(phba);
15580 }
15581
15582 /**
15583 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
15584 * @phba: pointer to lpfc hba data structure.
15585 * @rgn23_data: pointer to configure region 23 data.
15586 *
15587 * This function gets SLI3 port configure region 23 data through memory dump
15588 * mailbox command. When it successfully retrieves data, the size of the data
15589 * will be returned, otherwise, 0 will be returned.
15590 **/
15591 static uint32_t
15592 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15593 {
15594 LPFC_MBOXQ_t *pmb = NULL;
15595 MAILBOX_t *mb;
15596 uint32_t offset = 0;
15597 int rc;
15598
15599 if (!rgn23_data)
15600 return 0;
15601
15602 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15603 if (!pmb) {
15604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15605 "2600 failed to allocate mailbox memory\n");
15606 return 0;
15607 }
15608 mb = &pmb->u.mb;
15609
15610 do {
15611 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15612 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15613
15614 if (rc != MBX_SUCCESS) {
15615 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15616 "2601 failed to read config "
15617 "region 23, rc 0x%x Status 0x%x\n",
15618 rc, mb->mbxStatus);
15619 mb->un.varDmp.word_cnt = 0;
15620 }
15621 /*
15622 * dump mem may return a zero when finished or we got a
15623 * mailbox error, either way we are done.
15624 */
15625 if (mb->un.varDmp.word_cnt == 0)
15626 break;
15627 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15628 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15629
15630 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15631 rgn23_data + offset,
15632 mb->un.varDmp.word_cnt);
15633 offset += mb->un.varDmp.word_cnt;
15634 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15635
15636 mempool_free(pmb, phba->mbox_mem_pool);
15637 return offset;
15638 }
15639
15640 /**
15641 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15642 * @phba: pointer to lpfc hba data structure.
15643 * @rgn23_data: pointer to configure region 23 data.
15644 *
15645 * This function gets SLI4 port configure region 23 data through memory dump
15646 * mailbox command. When it successfully retrieves data, the size of the data
15647 * will be returned, otherwise, 0 will be returned.
15648 **/
15649 static uint32_t
15650 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15651 {
15652 LPFC_MBOXQ_t *mboxq = NULL;
15653 struct lpfc_dmabuf *mp = NULL;
15654 struct lpfc_mqe *mqe;
15655 uint32_t data_length = 0;
15656 int rc;
15657
15658 if (!rgn23_data)
15659 return 0;
15660
15661 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15662 if (!mboxq) {
15663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15664 "3105 failed to allocate mailbox memory\n");
15665 return 0;
15666 }
15667
15668 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15669 goto out;
15670 mqe = &mboxq->u.mqe;
15671 mp = (struct lpfc_dmabuf *) mboxq->context1;
15672 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15673 if (rc)
15674 goto out;
15675 data_length = mqe->un.mb_words[5];
15676 if (data_length == 0)
15677 goto out;
15678 if (data_length > DMP_RGN23_SIZE) {
15679 data_length = 0;
15680 goto out;
15681 }
15682 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15683 out:
15684 mempool_free(mboxq, phba->mbox_mem_pool);
15685 if (mp) {
15686 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15687 kfree(mp);
15688 }
15689 return data_length;
15690 }
15691
15692 /**
15693 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15694 * @phba: pointer to lpfc hba data structure.
15695 *
15696 * This function read region 23 and parse TLV for port status to
15697 * decide if the user disaled the port. If the TLV indicates the
15698 * port is disabled, the hba_flag is set accordingly.
15699 **/
15700 void
15701 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15702 {
15703 uint8_t *rgn23_data = NULL;
15704 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15705 uint32_t offset = 0;
15706
15707 /* Get adapter Region 23 data */
15708 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15709 if (!rgn23_data)
15710 goto out;
15711
15712 if (phba->sli_rev < LPFC_SLI_REV4)
15713 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15714 else {
15715 if_type = bf_get(lpfc_sli_intf_if_type,
15716 &phba->sli4_hba.sli_intf);
15717 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15718 goto out;
15719 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15720 }
15721
15722 if (!data_size)
15723 goto out;
15724
15725 /* Check the region signature first */
15726 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15728 "2619 Config region 23 has bad signature\n");
15729 goto out;
15730 }
15731 offset += 4;
15732
15733 /* Check the data structure version */
15734 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15736 "2620 Config region 23 has bad version\n");
15737 goto out;
15738 }
15739 offset += 4;
15740
15741 /* Parse TLV entries in the region */
15742 while (offset < data_size) {
15743 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15744 break;
15745 /*
15746 * If the TLV is not driver specific TLV or driver id is
15747 * not linux driver id, skip the record.
15748 */
15749 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15750 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15751 (rgn23_data[offset + 3] != 0)) {
15752 offset += rgn23_data[offset + 1] * 4 + 4;
15753 continue;
15754 }
15755
15756 /* Driver found a driver specific TLV in the config region */
15757 sub_tlv_len = rgn23_data[offset + 1] * 4;
15758 offset += 4;
15759 tlv_offset = 0;
15760
15761 /*
15762 * Search for configured port state sub-TLV.
15763 */
15764 while ((offset < data_size) &&
15765 (tlv_offset < sub_tlv_len)) {
15766 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15767 offset += 4;
15768 tlv_offset += 4;
15769 break;
15770 }
15771 if (rgn23_data[offset] != PORT_STE_TYPE) {
15772 offset += rgn23_data[offset + 1] * 4 + 4;
15773 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15774 continue;
15775 }
15776
15777 /* This HBA contains PORT_STE configured */
15778 if (!rgn23_data[offset + 2])
15779 phba->hba_flag |= LINK_DISABLED;
15780
15781 goto out;
15782 }
15783 }
15784
15785 out:
15786 kfree(rgn23_data);
15787 return;
15788 }
15789
15790 /**
15791 * lpfc_wr_object - write an object to the firmware
15792 * @phba: HBA structure that indicates port to create a queue on.
15793 * @dmabuf_list: list of dmabufs to write to the port.
15794 * @size: the total byte value of the objects to write to the port.
15795 * @offset: the current offset to be used to start the transfer.
15796 *
15797 * This routine will create a wr_object mailbox command to send to the port.
15798 * the mailbox command will be constructed using the dma buffers described in
15799 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15800 * BDEs that the imbedded mailbox can support. The @offset variable will be
15801 * used to indicate the starting offset of the transfer and will also return
15802 * the offset after the write object mailbox has completed. @size is used to
15803 * determine the end of the object and whether the eof bit should be set.
15804 *
15805 * Return 0 is successful and offset will contain the the new offset to use
15806 * for the next write.
15807 * Return negative value for error cases.
15808 **/
15809 int
15810 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15811 uint32_t size, uint32_t *offset)
15812 {
15813 struct lpfc_mbx_wr_object *wr_object;
15814 LPFC_MBOXQ_t *mbox;
15815 int rc = 0, i = 0;
15816 uint32_t shdr_status, shdr_add_status;
15817 uint32_t mbox_tmo;
15818 union lpfc_sli4_cfg_shdr *shdr;
15819 struct lpfc_dmabuf *dmabuf;
15820 uint32_t written = 0;
15821
15822 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15823 if (!mbox)
15824 return -ENOMEM;
15825
15826 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15827 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15828 sizeof(struct lpfc_mbx_wr_object) -
15829 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15830
15831 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15832 wr_object->u.request.write_offset = *offset;
15833 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15834 wr_object->u.request.object_name[0] =
15835 cpu_to_le32(wr_object->u.request.object_name[0]);
15836 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15837 list_for_each_entry(dmabuf, dmabuf_list, list) {
15838 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15839 break;
15840 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15841 wr_object->u.request.bde[i].addrHigh =
15842 putPaddrHigh(dmabuf->phys);
15843 if (written + SLI4_PAGE_SIZE >= size) {
15844 wr_object->u.request.bde[i].tus.f.bdeSize =
15845 (size - written);
15846 written += (size - written);
15847 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15848 } else {
15849 wr_object->u.request.bde[i].tus.f.bdeSize =
15850 SLI4_PAGE_SIZE;
15851 written += SLI4_PAGE_SIZE;
15852 }
15853 i++;
15854 }
15855 wr_object->u.request.bde_count = i;
15856 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15857 if (!phba->sli4_hba.intr_enable)
15858 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15859 else {
15860 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15861 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15862 }
15863 /* The IOCTL status is embedded in the mailbox subheader. */
15864 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15865 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15866 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15867 if (rc != MBX_TIMEOUT)
15868 mempool_free(mbox, phba->mbox_mem_pool);
15869 if (shdr_status || shdr_add_status || rc) {
15870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15871 "3025 Write Object mailbox failed with "
15872 "status x%x add_status x%x, mbx status x%x\n",
15873 shdr_status, shdr_add_status, rc);
15874 rc = -ENXIO;
15875 } else
15876 *offset += wr_object->u.response.actual_write_length;
15877 return rc;
15878 }
15879
15880 /**
15881 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15882 * @vport: pointer to vport data structure.
15883 *
15884 * This function iterate through the mailboxq and clean up all REG_LOGIN
15885 * and REG_VPI mailbox commands associated with the vport. This function
15886 * is called when driver want to restart discovery of the vport due to
15887 * a Clear Virtual Link event.
15888 **/
15889 void
15890 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15891 {
15892 struct lpfc_hba *phba = vport->phba;
15893 LPFC_MBOXQ_t *mb, *nextmb;
15894 struct lpfc_dmabuf *mp;
15895 struct lpfc_nodelist *ndlp;
15896 struct lpfc_nodelist *act_mbx_ndlp = NULL;
15897 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
15898 LIST_HEAD(mbox_cmd_list);
15899 uint8_t restart_loop;
15900
15901 /* Clean up internally queued mailbox commands with the vport */
15902 spin_lock_irq(&phba->hbalock);
15903 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15904 if (mb->vport != vport)
15905 continue;
15906
15907 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15908 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15909 continue;
15910
15911 list_del(&mb->list);
15912 list_add_tail(&mb->list, &mbox_cmd_list);
15913 }
15914 /* Clean up active mailbox command with the vport */
15915 mb = phba->sli.mbox_active;
15916 if (mb && (mb->vport == vport)) {
15917 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15918 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15919 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15920 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15921 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15922 /* Put reference count for delayed processing */
15923 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15924 /* Unregister the RPI when mailbox complete */
15925 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15926 }
15927 }
15928 /* Cleanup any mailbox completions which are not yet processed */
15929 do {
15930 restart_loop = 0;
15931 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15932 /*
15933 * If this mailox is already processed or it is
15934 * for another vport ignore it.
15935 */
15936 if ((mb->vport != vport) ||
15937 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15938 continue;
15939
15940 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15941 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15942 continue;
15943
15944 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15945 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15946 ndlp = (struct lpfc_nodelist *)mb->context2;
15947 /* Unregister the RPI when mailbox complete */
15948 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15949 restart_loop = 1;
15950 spin_unlock_irq(&phba->hbalock);
15951 spin_lock(shost->host_lock);
15952 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15953 spin_unlock(shost->host_lock);
15954 spin_lock_irq(&phba->hbalock);
15955 break;
15956 }
15957 }
15958 } while (restart_loop);
15959
15960 spin_unlock_irq(&phba->hbalock);
15961
15962 /* Release the cleaned-up mailbox commands */
15963 while (!list_empty(&mbox_cmd_list)) {
15964 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
15965 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15966 mp = (struct lpfc_dmabuf *) (mb->context1);
15967 if (mp) {
15968 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15969 kfree(mp);
15970 }
15971 ndlp = (struct lpfc_nodelist *) mb->context2;
15972 mb->context2 = NULL;
15973 if (ndlp) {
15974 spin_lock(shost->host_lock);
15975 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15976 spin_unlock(shost->host_lock);
15977 lpfc_nlp_put(ndlp);
15978 }
15979 }
15980 mempool_free(mb, phba->mbox_mem_pool);
15981 }
15982
15983 /* Release the ndlp with the cleaned-up active mailbox command */
15984 if (act_mbx_ndlp) {
15985 spin_lock(shost->host_lock);
15986 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15987 spin_unlock(shost->host_lock);
15988 lpfc_nlp_put(act_mbx_ndlp);
15989 }
15990 }
15991
15992 /**
15993 * lpfc_drain_txq - Drain the txq
15994 * @phba: Pointer to HBA context object.
15995 *
15996 * This function attempt to submit IOCBs on the txq
15997 * to the adapter. For SLI4 adapters, the txq contains
15998 * ELS IOCBs that have been deferred because the there
15999 * are no SGLs. This congestion can occur with large
16000 * vport counts during node discovery.
16001 **/
16002
16003 uint32_t
16004 lpfc_drain_txq(struct lpfc_hba *phba)
16005 {
16006 LIST_HEAD(completions);
16007 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16008 struct lpfc_iocbq *piocbq = 0;
16009 unsigned long iflags = 0;
16010 char *fail_msg = NULL;
16011 struct lpfc_sglq *sglq;
16012 union lpfc_wqe wqe;
16013
16014 spin_lock_irqsave(&phba->hbalock, iflags);
16015 if (pring->txq_cnt > pring->txq_max)
16016 pring->txq_max = pring->txq_cnt;
16017
16018 spin_unlock_irqrestore(&phba->hbalock, iflags);
16019
16020 while (pring->txq_cnt) {
16021 spin_lock_irqsave(&phba->hbalock, iflags);
16022
16023 piocbq = lpfc_sli_ringtx_get(phba, pring);
16024 if (!piocbq) {
16025 spin_unlock_irqrestore(&phba->hbalock, iflags);
16026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16027 "2823 txq empty and txq_cnt is %d\n ",
16028 pring->txq_cnt);
16029 break;
16030 }
16031 sglq = __lpfc_sli_get_sglq(phba, piocbq);
16032 if (!sglq) {
16033 __lpfc_sli_ringtx_put(phba, pring, piocbq);
16034 spin_unlock_irqrestore(&phba->hbalock, iflags);
16035 break;
16036 }
16037
16038 /* The xri and iocb resources secured,
16039 * attempt to issue request
16040 */
16041 piocbq->sli4_lxritag = sglq->sli4_lxritag;
16042 piocbq->sli4_xritag = sglq->sli4_xritag;
16043 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16044 fail_msg = "to convert bpl to sgl";
16045 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16046 fail_msg = "to convert iocb to wqe";
16047 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16048 fail_msg = " - Wq is full";
16049 else
16050 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16051
16052 if (fail_msg) {
16053 /* Failed means we can't issue and need to cancel */
16054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16055 "2822 IOCB failed %s iotag 0x%x "
16056 "xri 0x%x\n",
16057 fail_msg,
16058 piocbq->iotag, piocbq->sli4_xritag);
16059 list_add_tail(&piocbq->list, &completions);
16060 }
16061 spin_unlock_irqrestore(&phba->hbalock, iflags);
16062 }
16063
16064 /* Cancel all the IOCBs that cannot be issued */
16065 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16066 IOERR_SLI_ABORTED);
16067
16068 return pring->txq_cnt;
16069 }
This page took 0.376787 seconds and 4 git commands to generate.