]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/lpfc/lpfc_sli.c
UBUNTU: Ubuntu-4.10.0-37.41
[mirror_ubuntu-zesty-kernel.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-2016 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 #include <linux/lockdep.h>
28
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/fc/fc_fs.h>
35 #include <linux/aer.h>
36
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_compat.h"
48 #include "lpfc_debugfs.h"
49 #include "lpfc_vport.h"
50 #include "lpfc_version.h"
51
52 /* There are only four IOCB completion types. */
53 typedef enum _lpfc_iocb_type {
54 LPFC_UNKNOWN_IOCB,
55 LPFC_UNSOL_IOCB,
56 LPFC_SOL_IOCB,
57 LPFC_ABORT_IOCB
58 } lpfc_iocb_type;
59
60
61 /* Provide function prototypes local to this module. */
62 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint32_t);
64 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
65 uint8_t *, uint32_t *);
66 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
67 struct lpfc_iocbq *);
68 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
69 struct hbq_dmabuf *);
70 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
71 struct lpfc_cqe *);
72 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
73 int);
74 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
75 uint32_t);
76 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
77 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
78
79 static IOCB_t *
80 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
81 {
82 return &iocbq->iocb;
83 }
84
85 /**
86 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
87 * @q: The Work Queue to operate on.
88 * @wqe: The work Queue Entry to put on the Work queue.
89 *
90 * This routine will copy the contents of @wqe to the next available entry on
91 * the @q. This function will then ring the Work Queue Doorbell to signal the
92 * HBA to start processing the Work Queue Entry. This function returns 0 if
93 * successful. If no entries are available on @q then this function will return
94 * -ENOMEM.
95 * The caller is expected to hold the hbalock when calling this routine.
96 **/
97 static uint32_t
98 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
99 {
100 union lpfc_wqe *temp_wqe;
101 struct lpfc_register doorbell;
102 uint32_t host_index;
103 uint32_t idx;
104
105 /* sanity check on queue memory */
106 if (unlikely(!q))
107 return -ENOMEM;
108 temp_wqe = q->qe[q->host_index].wqe;
109
110 /* If the host has not yet processed the next entry then we are done */
111 idx = ((q->host_index + 1) % q->entry_count);
112 if (idx == q->hba_index) {
113 q->WQ_overflow++;
114 return -ENOMEM;
115 }
116 q->WQ_posted++;
117 /* set consumption flag every once in a while */
118 if (!((q->host_index + 1) % q->entry_repost))
119 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
120 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
121 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
122 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
123 /* ensure WQE bcopy flushed before doorbell write */
124 wmb();
125
126 /* Update the host index before invoking device */
127 host_index = q->host_index;
128
129 q->host_index = idx;
130
131 /* Ring Doorbell */
132 doorbell.word0 = 0;
133 if (q->db_format == LPFC_DB_LIST_FORMAT) {
134 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
135 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
136 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
137 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
138 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
139 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
140 } else {
141 return -EINVAL;
142 }
143 writel(doorbell.word0, q->db_regaddr);
144
145 return 0;
146 }
147
148 /**
149 * lpfc_sli4_wq_release - Updates internal hba index for WQ
150 * @q: The Work Queue to operate on.
151 * @index: The index to advance the hba index to.
152 *
153 * This routine will update the HBA index of a queue to reflect consumption of
154 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
155 * an entry the host calls this function to update the queue's internal
156 * pointers. This routine returns the number of entries that were consumed by
157 * the HBA.
158 **/
159 static uint32_t
160 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
161 {
162 uint32_t released = 0;
163
164 /* sanity check on queue memory */
165 if (unlikely(!q))
166 return 0;
167
168 if (q->hba_index == index)
169 return 0;
170 do {
171 q->hba_index = ((q->hba_index + 1) % q->entry_count);
172 released++;
173 } while (q->hba_index != index);
174 return released;
175 }
176
177 /**
178 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
179 * @q: The Mailbox Queue to operate on.
180 * @wqe: The Mailbox Queue Entry to put on the Work queue.
181 *
182 * This routine will copy the contents of @mqe to the next available entry on
183 * the @q. This function will then ring the Work Queue Doorbell to signal the
184 * HBA to start processing the Work Queue Entry. This function returns 0 if
185 * successful. If no entries are available on @q then this function will return
186 * -ENOMEM.
187 * The caller is expected to hold the hbalock when calling this routine.
188 **/
189 static uint32_t
190 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
191 {
192 struct lpfc_mqe *temp_mqe;
193 struct lpfc_register doorbell;
194
195 /* sanity check on queue memory */
196 if (unlikely(!q))
197 return -ENOMEM;
198 temp_mqe = q->qe[q->host_index].mqe;
199
200 /* If the host has not yet processed the next entry then we are done */
201 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
202 return -ENOMEM;
203 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
204 /* Save off the mailbox pointer for completion */
205 q->phba->mbox = (MAILBOX_t *)temp_mqe;
206
207 /* Update the host index before invoking device */
208 q->host_index = ((q->host_index + 1) % q->entry_count);
209
210 /* Ring Doorbell */
211 doorbell.word0 = 0;
212 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
213 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
214 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
215 return 0;
216 }
217
218 /**
219 * lpfc_sli4_mq_release - Updates internal hba index for MQ
220 * @q: The Mailbox Queue to operate on.
221 *
222 * This routine will update the HBA index of a queue to reflect consumption of
223 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
224 * an entry the host calls this function to update the queue's internal
225 * pointers. This routine returns the number of entries that were consumed by
226 * the HBA.
227 **/
228 static uint32_t
229 lpfc_sli4_mq_release(struct lpfc_queue *q)
230 {
231 /* sanity check on queue memory */
232 if (unlikely(!q))
233 return 0;
234
235 /* Clear the mailbox pointer for completion */
236 q->phba->mbox = NULL;
237 q->hba_index = ((q->hba_index + 1) % q->entry_count);
238 return 1;
239 }
240
241 /**
242 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
243 * @q: The Event Queue to get the first valid EQE from
244 *
245 * This routine will get the first valid Event Queue Entry from @q, update
246 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
247 * the Queue (no more work to do), or the Queue is full of EQEs that have been
248 * processed, but not popped back to the HBA then this routine will return NULL.
249 **/
250 static struct lpfc_eqe *
251 lpfc_sli4_eq_get(struct lpfc_queue *q)
252 {
253 struct lpfc_eqe *eqe;
254 uint32_t idx;
255
256 /* sanity check on queue memory */
257 if (unlikely(!q))
258 return NULL;
259 eqe = q->qe[q->hba_index].eqe;
260
261 /* If the next EQE is not valid then we are done */
262 if (!bf_get_le32(lpfc_eqe_valid, eqe))
263 return NULL;
264 /* If the host has not yet processed the next entry then we are done */
265 idx = ((q->hba_index + 1) % q->entry_count);
266 if (idx == q->host_index)
267 return NULL;
268
269 q->hba_index = idx;
270
271 /*
272 * insert barrier for instruction interlock : data from the hardware
273 * must have the valid bit checked before it can be copied and acted
274 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
275 * instructions allowing action on content before valid bit checked,
276 * add barrier here as well. May not be needed as "content" is a
277 * single 32-bit entity here (vs multi word structure for cq's).
278 */
279 mb();
280 return eqe;
281 }
282
283 /**
284 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
285 * @q: The Event Queue to disable interrupts
286 *
287 **/
288 static inline void
289 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
290 {
291 struct lpfc_register doorbell;
292
293 doorbell.word0 = 0;
294 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
295 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
296 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
297 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
298 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
299 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
300 }
301
302 /**
303 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
304 * @q: The Event Queue that the host has completed processing for.
305 * @arm: Indicates whether the host wants to arms this CQ.
306 *
307 * This routine will mark all Event Queue Entries on @q, from the last
308 * known completed entry to the last entry that was processed, as completed
309 * by clearing the valid bit for each completion queue entry. Then it will
310 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
311 * The internal host index in the @q will be updated by this routine to indicate
312 * that the host has finished processing the entries. The @arm parameter
313 * indicates that the queue should be rearmed when ringing the doorbell.
314 *
315 * This function will return the number of EQEs that were popped.
316 **/
317 uint32_t
318 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
319 {
320 uint32_t released = 0;
321 struct lpfc_eqe *temp_eqe;
322 struct lpfc_register doorbell;
323
324 /* sanity check on queue memory */
325 if (unlikely(!q))
326 return 0;
327
328 /* while there are valid entries */
329 while (q->hba_index != q->host_index) {
330 temp_eqe = q->qe[q->host_index].eqe;
331 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
332 released++;
333 q->host_index = ((q->host_index + 1) % q->entry_count);
334 }
335 if (unlikely(released == 0 && !arm))
336 return 0;
337
338 /* ring doorbell for number popped */
339 doorbell.word0 = 0;
340 if (arm) {
341 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
342 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
343 }
344 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
345 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
346 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
347 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
348 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
349 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
350 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
351 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
352 readl(q->phba->sli4_hba.EQCQDBregaddr);
353 return released;
354 }
355
356 /**
357 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
358 * @q: The Completion Queue to get the first valid CQE from
359 *
360 * This routine will get the first valid Completion Queue Entry from @q, update
361 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
362 * the Queue (no more work to do), or the Queue is full of CQEs that have been
363 * processed, but not popped back to the HBA then this routine will return NULL.
364 **/
365 static struct lpfc_cqe *
366 lpfc_sli4_cq_get(struct lpfc_queue *q)
367 {
368 struct lpfc_cqe *cqe;
369 uint32_t idx;
370
371 /* sanity check on queue memory */
372 if (unlikely(!q))
373 return NULL;
374
375 /* If the next CQE is not valid then we are done */
376 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
377 return NULL;
378 /* If the host has not yet processed the next entry then we are done */
379 idx = ((q->hba_index + 1) % q->entry_count);
380 if (idx == q->host_index)
381 return NULL;
382
383 cqe = q->qe[q->hba_index].cqe;
384 q->hba_index = idx;
385
386 /*
387 * insert barrier for instruction interlock : data from the hardware
388 * must have the valid bit checked before it can be copied and acted
389 * upon. Speculative instructions were allowing a bcopy at the start
390 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
391 * after our return, to copy data before the valid bit check above
392 * was done. As such, some of the copied data was stale. The barrier
393 * ensures the check is before any data is copied.
394 */
395 mb();
396 return cqe;
397 }
398
399 /**
400 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
401 * @q: The Completion Queue that the host has completed processing for.
402 * @arm: Indicates whether the host wants to arms this CQ.
403 *
404 * This routine will mark all Completion queue entries on @q, from the last
405 * known completed entry to the last entry that was processed, as completed
406 * by clearing the valid bit for each completion queue entry. Then it will
407 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
408 * The internal host index in the @q will be updated by this routine to indicate
409 * that the host has finished processing the entries. The @arm parameter
410 * indicates that the queue should be rearmed when ringing the doorbell.
411 *
412 * This function will return the number of CQEs that were released.
413 **/
414 uint32_t
415 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
416 {
417 uint32_t released = 0;
418 struct lpfc_cqe *temp_qe;
419 struct lpfc_register doorbell;
420
421 /* sanity check on queue memory */
422 if (unlikely(!q))
423 return 0;
424 /* while there are valid entries */
425 while (q->hba_index != q->host_index) {
426 temp_qe = q->qe[q->host_index].cqe;
427 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
428 released++;
429 q->host_index = ((q->host_index + 1) % q->entry_count);
430 }
431 if (unlikely(released == 0 && !arm))
432 return 0;
433
434 /* ring doorbell for number popped */
435 doorbell.word0 = 0;
436 if (arm)
437 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
438 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
439 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
440 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
441 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
442 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
443 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
444 return released;
445 }
446
447 /**
448 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
449 * @q: The Header Receive Queue to operate on.
450 * @wqe: The Receive Queue Entry to put on the Receive queue.
451 *
452 * This routine will copy the contents of @wqe to the next available entry on
453 * the @q. This function will then ring the Receive Queue Doorbell to signal the
454 * HBA to start processing the Receive Queue Entry. This function returns the
455 * index that the rqe was copied to if successful. If no entries are available
456 * on @q then this function will return -ENOMEM.
457 * The caller is expected to hold the hbalock when calling this routine.
458 **/
459 static int
460 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
461 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
462 {
463 struct lpfc_rqe *temp_hrqe;
464 struct lpfc_rqe *temp_drqe;
465 struct lpfc_register doorbell;
466 int put_index;
467
468 /* sanity check on queue memory */
469 if (unlikely(!hq) || unlikely(!dq))
470 return -ENOMEM;
471 put_index = hq->host_index;
472 temp_hrqe = hq->qe[hq->host_index].rqe;
473 temp_drqe = dq->qe[dq->host_index].rqe;
474
475 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
476 return -EINVAL;
477 if (hq->host_index != dq->host_index)
478 return -EINVAL;
479 /* If the host has not yet processed the next entry then we are done */
480 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
481 return -EBUSY;
482 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
483 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
484
485 /* Update the host index to point to the next slot */
486 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
487 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
488
489 /* Ring The Header Receive Queue Doorbell */
490 if (!(hq->host_index % hq->entry_repost)) {
491 doorbell.word0 = 0;
492 if (hq->db_format == LPFC_DB_RING_FORMAT) {
493 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
494 hq->entry_repost);
495 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
496 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
497 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
498 hq->entry_repost);
499 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
500 hq->host_index);
501 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
502 } else {
503 return -EINVAL;
504 }
505 writel(doorbell.word0, hq->db_regaddr);
506 }
507 return put_index;
508 }
509
510 /**
511 * lpfc_sli4_rq_release - Updates internal hba index for RQ
512 * @q: The Header Receive Queue to operate on.
513 *
514 * This routine will update the HBA index of a queue to reflect consumption of
515 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
516 * consumed an entry the host calls this function to update the queue's
517 * internal pointers. This routine returns the number of entries that were
518 * consumed by the HBA.
519 **/
520 static uint32_t
521 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
522 {
523 /* sanity check on queue memory */
524 if (unlikely(!hq) || unlikely(!dq))
525 return 0;
526
527 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
528 return 0;
529 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
530 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
531 return 1;
532 }
533
534 /**
535 * lpfc_cmd_iocb - Get next command iocb entry in the ring
536 * @phba: Pointer to HBA context object.
537 * @pring: Pointer to driver SLI ring object.
538 *
539 * This function returns pointer to next command iocb entry
540 * in the command ring. The caller must hold hbalock to prevent
541 * other threads consume the next command iocb.
542 * SLI-2/SLI-3 provide different sized iocbs.
543 **/
544 static inline IOCB_t *
545 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
546 {
547 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
548 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
549 }
550
551 /**
552 * lpfc_resp_iocb - Get next response iocb entry in the ring
553 * @phba: Pointer to HBA context object.
554 * @pring: Pointer to driver SLI ring object.
555 *
556 * This function returns pointer to next response iocb entry
557 * in the response ring. The caller must hold hbalock to make sure
558 * that no other thread consume the next response iocb.
559 * SLI-2/SLI-3 provide different sized iocbs.
560 **/
561 static inline IOCB_t *
562 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
563 {
564 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
565 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
566 }
567
568 /**
569 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
570 * @phba: Pointer to HBA context object.
571 *
572 * This function is called with hbalock held. This function
573 * allocates a new driver iocb object from the iocb pool. If the
574 * allocation is successful, it returns pointer to the newly
575 * allocated iocb object else it returns NULL.
576 **/
577 struct lpfc_iocbq *
578 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
579 {
580 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
581 struct lpfc_iocbq * iocbq = NULL;
582
583 lockdep_assert_held(&phba->hbalock);
584
585 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
586 if (iocbq)
587 phba->iocb_cnt++;
588 if (phba->iocb_cnt > phba->iocb_max)
589 phba->iocb_max = phba->iocb_cnt;
590 return iocbq;
591 }
592
593 /**
594 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
595 * @phba: Pointer to HBA context object.
596 * @xritag: XRI value.
597 *
598 * This function clears the sglq pointer from the array of acive
599 * sglq's. The xritag that is passed in is used to index into the
600 * array. Before the xritag can be used it needs to be adjusted
601 * by subtracting the xribase.
602 *
603 * Returns sglq ponter = success, NULL = Failure.
604 **/
605 static struct lpfc_sglq *
606 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
607 {
608 struct lpfc_sglq *sglq;
609
610 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
611 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
612 return sglq;
613 }
614
615 /**
616 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
617 * @phba: Pointer to HBA context object.
618 * @xritag: XRI value.
619 *
620 * This function returns the sglq pointer from the array of acive
621 * sglq's. The xritag that is passed in is used to index into the
622 * array. Before the xritag can be used it needs to be adjusted
623 * by subtracting the xribase.
624 *
625 * Returns sglq ponter = success, NULL = Failure.
626 **/
627 struct lpfc_sglq *
628 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
629 {
630 struct lpfc_sglq *sglq;
631
632 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
633 return sglq;
634 }
635
636 /**
637 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
638 * @phba: Pointer to HBA context object.
639 * @xritag: xri used in this exchange.
640 * @rrq: The RRQ to be cleared.
641 *
642 **/
643 void
644 lpfc_clr_rrq_active(struct lpfc_hba *phba,
645 uint16_t xritag,
646 struct lpfc_node_rrq *rrq)
647 {
648 struct lpfc_nodelist *ndlp = NULL;
649
650 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
651 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
652
653 /* The target DID could have been swapped (cable swap)
654 * we should use the ndlp from the findnode if it is
655 * available.
656 */
657 if ((!ndlp) && rrq->ndlp)
658 ndlp = rrq->ndlp;
659
660 if (!ndlp)
661 goto out;
662
663 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
664 rrq->send_rrq = 0;
665 rrq->xritag = 0;
666 rrq->rrq_stop_time = 0;
667 }
668 out:
669 mempool_free(rrq, phba->rrq_pool);
670 }
671
672 /**
673 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
674 * @phba: Pointer to HBA context object.
675 *
676 * This function is called with hbalock held. This function
677 * Checks if stop_time (ratov from setting rrq active) has
678 * been reached, if it has and the send_rrq flag is set then
679 * it will call lpfc_send_rrq. If the send_rrq flag is not set
680 * then it will just call the routine to clear the rrq and
681 * free the rrq resource.
682 * The timer is set to the next rrq that is going to expire before
683 * leaving the routine.
684 *
685 **/
686 void
687 lpfc_handle_rrq_active(struct lpfc_hba *phba)
688 {
689 struct lpfc_node_rrq *rrq;
690 struct lpfc_node_rrq *nextrrq;
691 unsigned long next_time;
692 unsigned long iflags;
693 LIST_HEAD(send_rrq);
694
695 spin_lock_irqsave(&phba->hbalock, iflags);
696 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
697 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
698 list_for_each_entry_safe(rrq, nextrrq,
699 &phba->active_rrq_list, list) {
700 if (time_after(jiffies, rrq->rrq_stop_time))
701 list_move(&rrq->list, &send_rrq);
702 else if (time_before(rrq->rrq_stop_time, next_time))
703 next_time = rrq->rrq_stop_time;
704 }
705 spin_unlock_irqrestore(&phba->hbalock, iflags);
706 if ((!list_empty(&phba->active_rrq_list)) &&
707 (!(phba->pport->load_flag & FC_UNLOADING)))
708 mod_timer(&phba->rrq_tmr, next_time);
709 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
710 list_del(&rrq->list);
711 if (!rrq->send_rrq)
712 /* this call will free the rrq */
713 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
714 else if (lpfc_send_rrq(phba, rrq)) {
715 /* if we send the rrq then the completion handler
716 * will clear the bit in the xribitmap.
717 */
718 lpfc_clr_rrq_active(phba, rrq->xritag,
719 rrq);
720 }
721 }
722 }
723
724 /**
725 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
726 * @vport: Pointer to vport context object.
727 * @xri: The xri used in the exchange.
728 * @did: The targets DID for this exchange.
729 *
730 * returns NULL = rrq not found in the phba->active_rrq_list.
731 * rrq = rrq for this xri and target.
732 **/
733 struct lpfc_node_rrq *
734 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
735 {
736 struct lpfc_hba *phba = vport->phba;
737 struct lpfc_node_rrq *rrq;
738 struct lpfc_node_rrq *nextrrq;
739 unsigned long iflags;
740
741 if (phba->sli_rev != LPFC_SLI_REV4)
742 return NULL;
743 spin_lock_irqsave(&phba->hbalock, iflags);
744 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
745 if (rrq->vport == vport && rrq->xritag == xri &&
746 rrq->nlp_DID == did){
747 list_del(&rrq->list);
748 spin_unlock_irqrestore(&phba->hbalock, iflags);
749 return rrq;
750 }
751 }
752 spin_unlock_irqrestore(&phba->hbalock, iflags);
753 return NULL;
754 }
755
756 /**
757 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
758 * @vport: Pointer to vport context object.
759 * @ndlp: Pointer to the lpfc_node_list structure.
760 * If ndlp is NULL Remove all active RRQs for this vport from the
761 * phba->active_rrq_list and clear the rrq.
762 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
763 **/
764 void
765 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
766
767 {
768 struct lpfc_hba *phba = vport->phba;
769 struct lpfc_node_rrq *rrq;
770 struct lpfc_node_rrq *nextrrq;
771 unsigned long iflags;
772 LIST_HEAD(rrq_list);
773
774 if (phba->sli_rev != LPFC_SLI_REV4)
775 return;
776 if (!ndlp) {
777 lpfc_sli4_vport_delete_els_xri_aborted(vport);
778 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
779 }
780 spin_lock_irqsave(&phba->hbalock, iflags);
781 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
782 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
783 list_move(&rrq->list, &rrq_list);
784 spin_unlock_irqrestore(&phba->hbalock, iflags);
785
786 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
787 list_del(&rrq->list);
788 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
789 }
790 }
791
792 /**
793 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
794 * @phba: Pointer to HBA context object.
795 * @ndlp: Targets nodelist pointer for this exchange.
796 * @xritag the xri in the bitmap to test.
797 *
798 * This function is called with hbalock held. This function
799 * returns 0 = rrq not active for this xri
800 * 1 = rrq is valid for this xri.
801 **/
802 int
803 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
804 uint16_t xritag)
805 {
806 lockdep_assert_held(&phba->hbalock);
807 if (!ndlp)
808 return 0;
809 if (!ndlp->active_rrqs_xri_bitmap)
810 return 0;
811 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
812 return 1;
813 else
814 return 0;
815 }
816
817 /**
818 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
819 * @phba: Pointer to HBA context object.
820 * @ndlp: nodelist pointer for this target.
821 * @xritag: xri used in this exchange.
822 * @rxid: Remote Exchange ID.
823 * @send_rrq: Flag used to determine if we should send rrq els cmd.
824 *
825 * This function takes the hbalock.
826 * The active bit is always set in the active rrq xri_bitmap even
827 * if there is no slot avaiable for the other rrq information.
828 *
829 * returns 0 rrq actived for this xri
830 * < 0 No memory or invalid ndlp.
831 **/
832 int
833 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
834 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
835 {
836 unsigned long iflags;
837 struct lpfc_node_rrq *rrq;
838 int empty;
839
840 if (!ndlp)
841 return -EINVAL;
842
843 if (!phba->cfg_enable_rrq)
844 return -EINVAL;
845
846 spin_lock_irqsave(&phba->hbalock, iflags);
847 if (phba->pport->load_flag & FC_UNLOADING) {
848 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
849 goto out;
850 }
851
852 /*
853 * set the active bit even if there is no mem available.
854 */
855 if (NLP_CHK_FREE_REQ(ndlp))
856 goto out;
857
858 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
859 goto out;
860
861 if (!ndlp->active_rrqs_xri_bitmap)
862 goto out;
863
864 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
865 goto out;
866
867 spin_unlock_irqrestore(&phba->hbalock, iflags);
868 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
869 if (!rrq) {
870 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
871 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
872 " DID:0x%x Send:%d\n",
873 xritag, rxid, ndlp->nlp_DID, send_rrq);
874 return -EINVAL;
875 }
876 if (phba->cfg_enable_rrq == 1)
877 rrq->send_rrq = send_rrq;
878 else
879 rrq->send_rrq = 0;
880 rrq->xritag = xritag;
881 rrq->rrq_stop_time = jiffies +
882 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
883 rrq->ndlp = ndlp;
884 rrq->nlp_DID = ndlp->nlp_DID;
885 rrq->vport = ndlp->vport;
886 rrq->rxid = rxid;
887 spin_lock_irqsave(&phba->hbalock, iflags);
888 empty = list_empty(&phba->active_rrq_list);
889 list_add_tail(&rrq->list, &phba->active_rrq_list);
890 phba->hba_flag |= HBA_RRQ_ACTIVE;
891 if (empty)
892 lpfc_worker_wake_up(phba);
893 spin_unlock_irqrestore(&phba->hbalock, iflags);
894 return 0;
895 out:
896 spin_unlock_irqrestore(&phba->hbalock, iflags);
897 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
898 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
899 " DID:0x%x Send:%d\n",
900 xritag, rxid, ndlp->nlp_DID, send_rrq);
901 return -EINVAL;
902 }
903
904 /**
905 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
906 * @phba: Pointer to HBA context object.
907 * @piocb: Pointer to the iocbq.
908 *
909 * This function is called with the ring lock held. This function
910 * gets a new driver sglq object from the sglq list. If the
911 * list is not empty then it is successful, it returns pointer to the newly
912 * allocated sglq object else it returns NULL.
913 **/
914 static struct lpfc_sglq *
915 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
916 {
917 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
918 struct lpfc_sglq *sglq = NULL;
919 struct lpfc_sglq *start_sglq = NULL;
920 struct lpfc_scsi_buf *lpfc_cmd;
921 struct lpfc_nodelist *ndlp;
922 int found = 0;
923
924 lockdep_assert_held(&phba->hbalock);
925
926 if (piocbq->iocb_flag & LPFC_IO_FCP) {
927 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
928 ndlp = lpfc_cmd->rdata->pnode;
929 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
930 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
931 ndlp = piocbq->context_un.ndlp;
932 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
933 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
934 ndlp = NULL;
935 else
936 ndlp = piocbq->context_un.ndlp;
937 } else {
938 ndlp = piocbq->context1;
939 }
940
941 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
942 start_sglq = sglq;
943 while (!found) {
944 if (!sglq)
945 return NULL;
946 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
947 /* This xri has an rrq outstanding for this DID.
948 * put it back in the list and get another xri.
949 */
950 list_add_tail(&sglq->list, lpfc_sgl_list);
951 sglq = NULL;
952 list_remove_head(lpfc_sgl_list, sglq,
953 struct lpfc_sglq, list);
954 if (sglq == start_sglq) {
955 sglq = NULL;
956 break;
957 } else
958 continue;
959 }
960 sglq->ndlp = ndlp;
961 found = 1;
962 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
963 sglq->state = SGL_ALLOCATED;
964 }
965 return sglq;
966 }
967
968 /**
969 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
970 * @phba: Pointer to HBA context object.
971 *
972 * This function is called with no lock held. This function
973 * allocates a new driver iocb object from the iocb pool. If the
974 * allocation is successful, it returns pointer to the newly
975 * allocated iocb object else it returns NULL.
976 **/
977 struct lpfc_iocbq *
978 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
979 {
980 struct lpfc_iocbq * iocbq = NULL;
981 unsigned long iflags;
982
983 spin_lock_irqsave(&phba->hbalock, iflags);
984 iocbq = __lpfc_sli_get_iocbq(phba);
985 spin_unlock_irqrestore(&phba->hbalock, iflags);
986 return iocbq;
987 }
988
989 /**
990 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 * The sqlq structure that holds the xritag and phys and virtual
999 * mappings for the scatter gather list is retrieved from the
1000 * active array of sglq. The get of the sglq pointer also clears
1001 * the entry in the array. If the status of the IO indiactes that
1002 * this IO was aborted then the sglq entry it put on the
1003 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1004 * IO has good status or fails for any other reason then the sglq
1005 * entry is added to the free list (lpfc_sgl_list).
1006 **/
1007 static void
1008 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1009 {
1010 struct lpfc_sglq *sglq;
1011 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1012 unsigned long iflag = 0;
1013 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
1014
1015 lockdep_assert_held(&phba->hbalock);
1016
1017 if (iocbq->sli4_xritag == NO_XRI)
1018 sglq = NULL;
1019 else
1020 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1021
1022
1023 if (sglq) {
1024 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1025 (sglq->state != SGL_XRI_ABORTED)) {
1026 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1027 iflag);
1028 list_add(&sglq->list,
1029 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1030 spin_unlock_irqrestore(
1031 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1032 } else {
1033 spin_lock_irqsave(&pring->ring_lock, iflag);
1034 sglq->state = SGL_FREED;
1035 sglq->ndlp = NULL;
1036 list_add_tail(&sglq->list,
1037 &phba->sli4_hba.lpfc_sgl_list);
1038 spin_unlock_irqrestore(&pring->ring_lock, iflag);
1039
1040 /* Check if TXQ queue needs to be serviced */
1041 if (!list_empty(&pring->txq))
1042 lpfc_worker_wake_up(phba);
1043 }
1044 }
1045
1046
1047 /*
1048 * Clean all volatile data fields, preserve iotag and node struct.
1049 */
1050 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1051 iocbq->sli4_lxritag = NO_XRI;
1052 iocbq->sli4_xritag = NO_XRI;
1053 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1054 }
1055
1056
1057 /**
1058 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1059 * @phba: Pointer to HBA context object.
1060 * @iocbq: Pointer to driver iocb object.
1061 *
1062 * This function is called with hbalock held to release driver
1063 * iocb object to the iocb pool. The iotag in the iocb object
1064 * does not change for each use of the iocb object. This function
1065 * clears all other fields of the iocb object when it is freed.
1066 **/
1067 static void
1068 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1069 {
1070 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1071
1072 lockdep_assert_held(&phba->hbalock);
1073
1074 /*
1075 * Clean all volatile data fields, preserve iotag and node struct.
1076 */
1077 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1078 iocbq->sli4_xritag = NO_XRI;
1079 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1080 }
1081
1082 /**
1083 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1084 * @phba: Pointer to HBA context object.
1085 * @iocbq: Pointer to driver iocb object.
1086 *
1087 * This function is called with hbalock held to release driver
1088 * iocb object to the iocb pool. The iotag in the iocb object
1089 * does not change for each use of the iocb object. This function
1090 * clears all other fields of the iocb object when it is freed.
1091 **/
1092 static void
1093 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1094 {
1095 lockdep_assert_held(&phba->hbalock);
1096
1097 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1098 phba->iocb_cnt--;
1099 }
1100
1101 /**
1102 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1103 * @phba: Pointer to HBA context object.
1104 * @iocbq: Pointer to driver iocb object.
1105 *
1106 * This function is called with no lock held to release the iocb to
1107 * iocb pool.
1108 **/
1109 void
1110 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1111 {
1112 unsigned long iflags;
1113
1114 /*
1115 * Clean all volatile data fields, preserve iotag and node struct.
1116 */
1117 spin_lock_irqsave(&phba->hbalock, iflags);
1118 __lpfc_sli_release_iocbq(phba, iocbq);
1119 spin_unlock_irqrestore(&phba->hbalock, iflags);
1120 }
1121
1122 /**
1123 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1124 * @phba: Pointer to HBA context object.
1125 * @iocblist: List of IOCBs.
1126 * @ulpstatus: ULP status in IOCB command field.
1127 * @ulpWord4: ULP word-4 in IOCB command field.
1128 *
1129 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1130 * on the list by invoking the complete callback function associated with the
1131 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1132 * fields.
1133 **/
1134 void
1135 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1136 uint32_t ulpstatus, uint32_t ulpWord4)
1137 {
1138 struct lpfc_iocbq *piocb;
1139
1140 while (!list_empty(iocblist)) {
1141 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1142 if (!piocb->iocb_cmpl)
1143 lpfc_sli_release_iocbq(phba, piocb);
1144 else {
1145 piocb->iocb.ulpStatus = ulpstatus;
1146 piocb->iocb.un.ulpWord[4] = ulpWord4;
1147 (piocb->iocb_cmpl) (phba, piocb, piocb);
1148 }
1149 }
1150 return;
1151 }
1152
1153 /**
1154 * lpfc_sli_iocb_cmd_type - Get the iocb type
1155 * @iocb_cmnd: iocb command code.
1156 *
1157 * This function is called by ring event handler function to get the iocb type.
1158 * This function translates the iocb command to an iocb command type used to
1159 * decide the final disposition of each completed IOCB.
1160 * The function returns
1161 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1162 * LPFC_SOL_IOCB if it is a solicited iocb completion
1163 * LPFC_ABORT_IOCB if it is an abort iocb
1164 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1165 *
1166 * The caller is not required to hold any lock.
1167 **/
1168 static lpfc_iocb_type
1169 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1170 {
1171 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1172
1173 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1174 return 0;
1175
1176 switch (iocb_cmnd) {
1177 case CMD_XMIT_SEQUENCE_CR:
1178 case CMD_XMIT_SEQUENCE_CX:
1179 case CMD_XMIT_BCAST_CN:
1180 case CMD_XMIT_BCAST_CX:
1181 case CMD_ELS_REQUEST_CR:
1182 case CMD_ELS_REQUEST_CX:
1183 case CMD_CREATE_XRI_CR:
1184 case CMD_CREATE_XRI_CX:
1185 case CMD_GET_RPI_CN:
1186 case CMD_XMIT_ELS_RSP_CX:
1187 case CMD_GET_RPI_CR:
1188 case CMD_FCP_IWRITE_CR:
1189 case CMD_FCP_IWRITE_CX:
1190 case CMD_FCP_IREAD_CR:
1191 case CMD_FCP_IREAD_CX:
1192 case CMD_FCP_ICMND_CR:
1193 case CMD_FCP_ICMND_CX:
1194 case CMD_FCP_TSEND_CX:
1195 case CMD_FCP_TRSP_CX:
1196 case CMD_FCP_TRECEIVE_CX:
1197 case CMD_FCP_AUTO_TRSP_CX:
1198 case CMD_ADAPTER_MSG:
1199 case CMD_ADAPTER_DUMP:
1200 case CMD_XMIT_SEQUENCE64_CR:
1201 case CMD_XMIT_SEQUENCE64_CX:
1202 case CMD_XMIT_BCAST64_CN:
1203 case CMD_XMIT_BCAST64_CX:
1204 case CMD_ELS_REQUEST64_CR:
1205 case CMD_ELS_REQUEST64_CX:
1206 case CMD_FCP_IWRITE64_CR:
1207 case CMD_FCP_IWRITE64_CX:
1208 case CMD_FCP_IREAD64_CR:
1209 case CMD_FCP_IREAD64_CX:
1210 case CMD_FCP_ICMND64_CR:
1211 case CMD_FCP_ICMND64_CX:
1212 case CMD_FCP_TSEND64_CX:
1213 case CMD_FCP_TRSP64_CX:
1214 case CMD_FCP_TRECEIVE64_CX:
1215 case CMD_GEN_REQUEST64_CR:
1216 case CMD_GEN_REQUEST64_CX:
1217 case CMD_XMIT_ELS_RSP64_CX:
1218 case DSSCMD_IWRITE64_CR:
1219 case DSSCMD_IWRITE64_CX:
1220 case DSSCMD_IREAD64_CR:
1221 case DSSCMD_IREAD64_CX:
1222 type = LPFC_SOL_IOCB;
1223 break;
1224 case CMD_ABORT_XRI_CN:
1225 case CMD_ABORT_XRI_CX:
1226 case CMD_CLOSE_XRI_CN:
1227 case CMD_CLOSE_XRI_CX:
1228 case CMD_XRI_ABORTED_CX:
1229 case CMD_ABORT_MXRI64_CN:
1230 case CMD_XMIT_BLS_RSP64_CX:
1231 type = LPFC_ABORT_IOCB;
1232 break;
1233 case CMD_RCV_SEQUENCE_CX:
1234 case CMD_RCV_ELS_REQ_CX:
1235 case CMD_RCV_SEQUENCE64_CX:
1236 case CMD_RCV_ELS_REQ64_CX:
1237 case CMD_ASYNC_STATUS:
1238 case CMD_IOCB_RCV_SEQ64_CX:
1239 case CMD_IOCB_RCV_ELS64_CX:
1240 case CMD_IOCB_RCV_CONT64_CX:
1241 case CMD_IOCB_RET_XRI64_CX:
1242 type = LPFC_UNSOL_IOCB;
1243 break;
1244 case CMD_IOCB_XMIT_MSEQ64_CR:
1245 case CMD_IOCB_XMIT_MSEQ64_CX:
1246 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1247 case CMD_IOCB_RCV_ELS_LIST64_CX:
1248 case CMD_IOCB_CLOSE_EXTENDED_CN:
1249 case CMD_IOCB_ABORT_EXTENDED_CN:
1250 case CMD_IOCB_RET_HBQE64_CN:
1251 case CMD_IOCB_FCP_IBIDIR64_CR:
1252 case CMD_IOCB_FCP_IBIDIR64_CX:
1253 case CMD_IOCB_FCP_ITASKMGT64_CX:
1254 case CMD_IOCB_LOGENTRY_CN:
1255 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1256 printk("%s - Unhandled SLI-3 Command x%x\n",
1257 __func__, iocb_cmnd);
1258 type = LPFC_UNKNOWN_IOCB;
1259 break;
1260 default:
1261 type = LPFC_UNKNOWN_IOCB;
1262 break;
1263 }
1264
1265 return type;
1266 }
1267
1268 /**
1269 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1270 * @phba: Pointer to HBA context object.
1271 *
1272 * This function is called from SLI initialization code
1273 * to configure every ring of the HBA's SLI interface. The
1274 * caller is not required to hold any lock. This function issues
1275 * a config_ring mailbox command for each ring.
1276 * This function returns zero if successful else returns a negative
1277 * error code.
1278 **/
1279 static int
1280 lpfc_sli_ring_map(struct lpfc_hba *phba)
1281 {
1282 struct lpfc_sli *psli = &phba->sli;
1283 LPFC_MBOXQ_t *pmb;
1284 MAILBOX_t *pmbox;
1285 int i, rc, ret = 0;
1286
1287 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1288 if (!pmb)
1289 return -ENOMEM;
1290 pmbox = &pmb->u.mb;
1291 phba->link_state = LPFC_INIT_MBX_CMDS;
1292 for (i = 0; i < psli->num_rings; i++) {
1293 lpfc_config_ring(phba, i, pmb);
1294 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1295 if (rc != MBX_SUCCESS) {
1296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1297 "0446 Adapter failed to init (%d), "
1298 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1299 "ring %d\n",
1300 rc, pmbox->mbxCommand,
1301 pmbox->mbxStatus, i);
1302 phba->link_state = LPFC_HBA_ERROR;
1303 ret = -ENXIO;
1304 break;
1305 }
1306 }
1307 mempool_free(pmb, phba->mbox_mem_pool);
1308 return ret;
1309 }
1310
1311 /**
1312 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1313 * @phba: Pointer to HBA context object.
1314 * @pring: Pointer to driver SLI ring object.
1315 * @piocb: Pointer to the driver iocb object.
1316 *
1317 * This function is called with hbalock held. The function adds the
1318 * new iocb to txcmplq of the given ring. This function always returns
1319 * 0. If this function is called for ELS ring, this function checks if
1320 * there is a vport associated with the ELS command. This function also
1321 * starts els_tmofunc timer if this is an ELS command.
1322 **/
1323 static int
1324 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1325 struct lpfc_iocbq *piocb)
1326 {
1327 lockdep_assert_held(&phba->hbalock);
1328
1329 BUG_ON(!piocb);
1330
1331 list_add_tail(&piocb->list, &pring->txcmplq);
1332 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1333
1334 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1335 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1336 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1337 BUG_ON(!piocb->vport);
1338 if (!(piocb->vport->load_flag & FC_UNLOADING))
1339 mod_timer(&piocb->vport->els_tmofunc,
1340 jiffies +
1341 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1342 }
1343
1344 return 0;
1345 }
1346
1347 /**
1348 * lpfc_sli_ringtx_get - Get first element of the txq
1349 * @phba: Pointer to HBA context object.
1350 * @pring: Pointer to driver SLI ring object.
1351 *
1352 * This function is called with hbalock held to get next
1353 * iocb in txq of the given ring. If there is any iocb in
1354 * the txq, the function returns first iocb in the list after
1355 * removing the iocb from the list, else it returns NULL.
1356 **/
1357 struct lpfc_iocbq *
1358 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1359 {
1360 struct lpfc_iocbq *cmd_iocb;
1361
1362 lockdep_assert_held(&phba->hbalock);
1363
1364 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1365 return cmd_iocb;
1366 }
1367
1368 /**
1369 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1370 * @phba: Pointer to HBA context object.
1371 * @pring: Pointer to driver SLI ring object.
1372 *
1373 * This function is called with hbalock held and the caller must post the
1374 * iocb without releasing the lock. If the caller releases the lock,
1375 * iocb slot returned by the function is not guaranteed to be available.
1376 * The function returns pointer to the next available iocb slot if there
1377 * is available slot in the ring, else it returns NULL.
1378 * If the get index of the ring is ahead of the put index, the function
1379 * will post an error attention event to the worker thread to take the
1380 * HBA to offline state.
1381 **/
1382 static IOCB_t *
1383 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1384 {
1385 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1386 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1387
1388 lockdep_assert_held(&phba->hbalock);
1389
1390 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1391 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1392 pring->sli.sli3.next_cmdidx = 0;
1393
1394 if (unlikely(pring->sli.sli3.local_getidx ==
1395 pring->sli.sli3.next_cmdidx)) {
1396
1397 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1398
1399 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1401 "0315 Ring %d issue: portCmdGet %d "
1402 "is bigger than cmd ring %d\n",
1403 pring->ringno,
1404 pring->sli.sli3.local_getidx,
1405 max_cmd_idx);
1406
1407 phba->link_state = LPFC_HBA_ERROR;
1408 /*
1409 * All error attention handlers are posted to
1410 * worker thread
1411 */
1412 phba->work_ha |= HA_ERATT;
1413 phba->work_hs = HS_FFER3;
1414
1415 lpfc_worker_wake_up(phba);
1416
1417 return NULL;
1418 }
1419
1420 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1421 return NULL;
1422 }
1423
1424 return lpfc_cmd_iocb(phba, pring);
1425 }
1426
1427 /**
1428 * lpfc_sli_next_iotag - Get an iotag for the iocb
1429 * @phba: Pointer to HBA context object.
1430 * @iocbq: Pointer to driver iocb object.
1431 *
1432 * This function gets an iotag for the iocb. If there is no unused iotag and
1433 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1434 * array and assigns a new iotag.
1435 * The function returns the allocated iotag if successful, else returns zero.
1436 * Zero is not a valid iotag.
1437 * The caller is not required to hold any lock.
1438 **/
1439 uint16_t
1440 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1441 {
1442 struct lpfc_iocbq **new_arr;
1443 struct lpfc_iocbq **old_arr;
1444 size_t new_len;
1445 struct lpfc_sli *psli = &phba->sli;
1446 uint16_t iotag;
1447
1448 spin_lock_irq(&phba->hbalock);
1449 iotag = psli->last_iotag;
1450 if(++iotag < psli->iocbq_lookup_len) {
1451 psli->last_iotag = iotag;
1452 psli->iocbq_lookup[iotag] = iocbq;
1453 spin_unlock_irq(&phba->hbalock);
1454 iocbq->iotag = iotag;
1455 return iotag;
1456 } else if (psli->iocbq_lookup_len < (0xffff
1457 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1458 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1459 spin_unlock_irq(&phba->hbalock);
1460 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1461 GFP_KERNEL);
1462 if (new_arr) {
1463 spin_lock_irq(&phba->hbalock);
1464 old_arr = psli->iocbq_lookup;
1465 if (new_len <= psli->iocbq_lookup_len) {
1466 /* highly unprobable case */
1467 kfree(new_arr);
1468 iotag = psli->last_iotag;
1469 if(++iotag < psli->iocbq_lookup_len) {
1470 psli->last_iotag = iotag;
1471 psli->iocbq_lookup[iotag] = iocbq;
1472 spin_unlock_irq(&phba->hbalock);
1473 iocbq->iotag = iotag;
1474 return iotag;
1475 }
1476 spin_unlock_irq(&phba->hbalock);
1477 return 0;
1478 }
1479 if (psli->iocbq_lookup)
1480 memcpy(new_arr, old_arr,
1481 ((psli->last_iotag + 1) *
1482 sizeof (struct lpfc_iocbq *)));
1483 psli->iocbq_lookup = new_arr;
1484 psli->iocbq_lookup_len = new_len;
1485 psli->last_iotag = iotag;
1486 psli->iocbq_lookup[iotag] = iocbq;
1487 spin_unlock_irq(&phba->hbalock);
1488 iocbq->iotag = iotag;
1489 kfree(old_arr);
1490 return iotag;
1491 }
1492 } else
1493 spin_unlock_irq(&phba->hbalock);
1494
1495 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1496 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1497 psli->last_iotag);
1498
1499 return 0;
1500 }
1501
1502 /**
1503 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1504 * @phba: Pointer to HBA context object.
1505 * @pring: Pointer to driver SLI ring object.
1506 * @iocb: Pointer to iocb slot in the ring.
1507 * @nextiocb: Pointer to driver iocb object which need to be
1508 * posted to firmware.
1509 *
1510 * This function is called with hbalock held to post a new iocb to
1511 * the firmware. This function copies the new iocb to ring iocb slot and
1512 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1513 * a completion call back for this iocb else the function will free the
1514 * iocb object.
1515 **/
1516 static void
1517 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1518 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1519 {
1520 lockdep_assert_held(&phba->hbalock);
1521 /*
1522 * Set up an iotag
1523 */
1524 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1525
1526
1527 if (pring->ringno == LPFC_ELS_RING) {
1528 lpfc_debugfs_slow_ring_trc(phba,
1529 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1530 *(((uint32_t *) &nextiocb->iocb) + 4),
1531 *(((uint32_t *) &nextiocb->iocb) + 6),
1532 *(((uint32_t *) &nextiocb->iocb) + 7));
1533 }
1534
1535 /*
1536 * Issue iocb command to adapter
1537 */
1538 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1539 wmb();
1540 pring->stats.iocb_cmd++;
1541
1542 /*
1543 * If there is no completion routine to call, we can release the
1544 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1545 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1546 */
1547 if (nextiocb->iocb_cmpl)
1548 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1549 else
1550 __lpfc_sli_release_iocbq(phba, nextiocb);
1551
1552 /*
1553 * Let the HBA know what IOCB slot will be the next one the
1554 * driver will put a command into.
1555 */
1556 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1557 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1558 }
1559
1560 /**
1561 * lpfc_sli_update_full_ring - Update the chip attention register
1562 * @phba: Pointer to HBA context object.
1563 * @pring: Pointer to driver SLI ring object.
1564 *
1565 * The caller is not required to hold any lock for calling this function.
1566 * This function updates the chip attention bits for the ring to inform firmware
1567 * that there are pending work to be done for this ring and requests an
1568 * interrupt when there is space available in the ring. This function is
1569 * called when the driver is unable to post more iocbs to the ring due
1570 * to unavailability of space in the ring.
1571 **/
1572 static void
1573 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1574 {
1575 int ringno = pring->ringno;
1576
1577 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1578
1579 wmb();
1580
1581 /*
1582 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1583 * The HBA will tell us when an IOCB entry is available.
1584 */
1585 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1586 readl(phba->CAregaddr); /* flush */
1587
1588 pring->stats.iocb_cmd_full++;
1589 }
1590
1591 /**
1592 * lpfc_sli_update_ring - Update chip attention register
1593 * @phba: Pointer to HBA context object.
1594 * @pring: Pointer to driver SLI ring object.
1595 *
1596 * This function updates the chip attention register bit for the
1597 * given ring to inform HBA that there is more work to be done
1598 * in this ring. The caller is not required to hold any lock.
1599 **/
1600 static void
1601 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1602 {
1603 int ringno = pring->ringno;
1604
1605 /*
1606 * Tell the HBA that there is work to do in this ring.
1607 */
1608 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1609 wmb();
1610 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1611 readl(phba->CAregaddr); /* flush */
1612 }
1613 }
1614
1615 /**
1616 * lpfc_sli_resume_iocb - Process iocbs in the txq
1617 * @phba: Pointer to HBA context object.
1618 * @pring: Pointer to driver SLI ring object.
1619 *
1620 * This function is called with hbalock held to post pending iocbs
1621 * in the txq to the firmware. This function is called when driver
1622 * detects space available in the ring.
1623 **/
1624 static void
1625 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1626 {
1627 IOCB_t *iocb;
1628 struct lpfc_iocbq *nextiocb;
1629
1630 lockdep_assert_held(&phba->hbalock);
1631
1632 /*
1633 * Check to see if:
1634 * (a) there is anything on the txq to send
1635 * (b) link is up
1636 * (c) link attention events can be processed (fcp ring only)
1637 * (d) IOCB processing is not blocked by the outstanding mbox command.
1638 */
1639
1640 if (lpfc_is_link_up(phba) &&
1641 (!list_empty(&pring->txq)) &&
1642 (pring->ringno != phba->sli.fcp_ring ||
1643 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1644
1645 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1646 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1647 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1648
1649 if (iocb)
1650 lpfc_sli_update_ring(phba, pring);
1651 else
1652 lpfc_sli_update_full_ring(phba, pring);
1653 }
1654
1655 return;
1656 }
1657
1658 /**
1659 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1660 * @phba: Pointer to HBA context object.
1661 * @hbqno: HBQ number.
1662 *
1663 * This function is called with hbalock held to get the next
1664 * available slot for the given HBQ. If there is free slot
1665 * available for the HBQ it will return pointer to the next available
1666 * HBQ entry else it will return NULL.
1667 **/
1668 static struct lpfc_hbq_entry *
1669 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1670 {
1671 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1672
1673 lockdep_assert_held(&phba->hbalock);
1674
1675 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1676 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1677 hbqp->next_hbqPutIdx = 0;
1678
1679 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1680 uint32_t raw_index = phba->hbq_get[hbqno];
1681 uint32_t getidx = le32_to_cpu(raw_index);
1682
1683 hbqp->local_hbqGetIdx = getidx;
1684
1685 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1686 lpfc_printf_log(phba, KERN_ERR,
1687 LOG_SLI | LOG_VPORT,
1688 "1802 HBQ %d: local_hbqGetIdx "
1689 "%u is > than hbqp->entry_count %u\n",
1690 hbqno, hbqp->local_hbqGetIdx,
1691 hbqp->entry_count);
1692
1693 phba->link_state = LPFC_HBA_ERROR;
1694 return NULL;
1695 }
1696
1697 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1698 return NULL;
1699 }
1700
1701 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1702 hbqp->hbqPutIdx;
1703 }
1704
1705 /**
1706 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1707 * @phba: Pointer to HBA context object.
1708 *
1709 * This function is called with no lock held to free all the
1710 * hbq buffers while uninitializing the SLI interface. It also
1711 * frees the HBQ buffers returned by the firmware but not yet
1712 * processed by the upper layers.
1713 **/
1714 void
1715 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1716 {
1717 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1718 struct hbq_dmabuf *hbq_buf;
1719 unsigned long flags;
1720 int i, hbq_count;
1721 uint32_t hbqno;
1722
1723 hbq_count = lpfc_sli_hbq_count();
1724 /* Return all memory used by all HBQs */
1725 spin_lock_irqsave(&phba->hbalock, flags);
1726 for (i = 0; i < hbq_count; ++i) {
1727 list_for_each_entry_safe(dmabuf, next_dmabuf,
1728 &phba->hbqs[i].hbq_buffer_list, list) {
1729 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1730 list_del(&hbq_buf->dbuf.list);
1731 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1732 }
1733 phba->hbqs[i].buffer_count = 0;
1734 }
1735 /* Return all HBQ buffer that are in-fly */
1736 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1737 list) {
1738 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1739 list_del(&hbq_buf->dbuf.list);
1740 if (hbq_buf->tag == -1) {
1741 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1742 (phba, hbq_buf);
1743 } else {
1744 hbqno = hbq_buf->tag >> 16;
1745 if (hbqno >= LPFC_MAX_HBQS)
1746 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1747 (phba, hbq_buf);
1748 else
1749 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1750 hbq_buf);
1751 }
1752 }
1753
1754 /* Mark the HBQs not in use */
1755 phba->hbq_in_use = 0;
1756 spin_unlock_irqrestore(&phba->hbalock, flags);
1757 }
1758
1759 /**
1760 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1761 * @phba: Pointer to HBA context object.
1762 * @hbqno: HBQ number.
1763 * @hbq_buf: Pointer to HBQ buffer.
1764 *
1765 * This function is called with the hbalock held to post a
1766 * hbq buffer to the firmware. If the function finds an empty
1767 * slot in the HBQ, it will post the buffer. The function will return
1768 * pointer to the hbq entry if it successfully post the buffer
1769 * else it will return NULL.
1770 **/
1771 static int
1772 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1773 struct hbq_dmabuf *hbq_buf)
1774 {
1775 lockdep_assert_held(&phba->hbalock);
1776 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1777 }
1778
1779 /**
1780 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1781 * @phba: Pointer to HBA context object.
1782 * @hbqno: HBQ number.
1783 * @hbq_buf: Pointer to HBQ buffer.
1784 *
1785 * This function is called with the hbalock held to post a hbq buffer to the
1786 * firmware. If the function finds an empty slot in the HBQ, it will post the
1787 * buffer and place it on the hbq_buffer_list. The function will return zero if
1788 * it successfully post the buffer else it will return an error.
1789 **/
1790 static int
1791 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1792 struct hbq_dmabuf *hbq_buf)
1793 {
1794 struct lpfc_hbq_entry *hbqe;
1795 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1796
1797 lockdep_assert_held(&phba->hbalock);
1798 /* Get next HBQ entry slot to use */
1799 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1800 if (hbqe) {
1801 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1802
1803 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1804 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1805 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1806 hbqe->bde.tus.f.bdeFlags = 0;
1807 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1808 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1809 /* Sync SLIM */
1810 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1811 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1812 /* flush */
1813 readl(phba->hbq_put + hbqno);
1814 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1815 return 0;
1816 } else
1817 return -ENOMEM;
1818 }
1819
1820 /**
1821 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1822 * @phba: Pointer to HBA context object.
1823 * @hbqno: HBQ number.
1824 * @hbq_buf: Pointer to HBQ buffer.
1825 *
1826 * This function is called with the hbalock held to post an RQE to the SLI4
1827 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1828 * the hbq_buffer_list and return zero, otherwise it will return an error.
1829 **/
1830 static int
1831 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1832 struct hbq_dmabuf *hbq_buf)
1833 {
1834 int rc;
1835 struct lpfc_rqe hrqe;
1836 struct lpfc_rqe drqe;
1837
1838 lockdep_assert_held(&phba->hbalock);
1839 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1840 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1841 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1842 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1843 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1844 &hrqe, &drqe);
1845 if (rc < 0)
1846 return rc;
1847 hbq_buf->tag = rc;
1848 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1849 return 0;
1850 }
1851
1852 /* HBQ for ELS and CT traffic. */
1853 static struct lpfc_hbq_init lpfc_els_hbq = {
1854 .rn = 1,
1855 .entry_count = 256,
1856 .mask_count = 0,
1857 .profile = 0,
1858 .ring_mask = (1 << LPFC_ELS_RING),
1859 .buffer_count = 0,
1860 .init_count = 40,
1861 .add_count = 40,
1862 };
1863
1864 /* HBQ for the extra ring if needed */
1865 static struct lpfc_hbq_init lpfc_extra_hbq = {
1866 .rn = 1,
1867 .entry_count = 200,
1868 .mask_count = 0,
1869 .profile = 0,
1870 .ring_mask = (1 << LPFC_EXTRA_RING),
1871 .buffer_count = 0,
1872 .init_count = 0,
1873 .add_count = 5,
1874 };
1875
1876 /* Array of HBQs */
1877 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1878 &lpfc_els_hbq,
1879 &lpfc_extra_hbq,
1880 };
1881
1882 /**
1883 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1884 * @phba: Pointer to HBA context object.
1885 * @hbqno: HBQ number.
1886 * @count: Number of HBQ buffers to be posted.
1887 *
1888 * This function is called with no lock held to post more hbq buffers to the
1889 * given HBQ. The function returns the number of HBQ buffers successfully
1890 * posted.
1891 **/
1892 static int
1893 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1894 {
1895 uint32_t i, posted = 0;
1896 unsigned long flags;
1897 struct hbq_dmabuf *hbq_buffer;
1898 LIST_HEAD(hbq_buf_list);
1899 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1900 return 0;
1901
1902 if ((phba->hbqs[hbqno].buffer_count + count) >
1903 lpfc_hbq_defs[hbqno]->entry_count)
1904 count = lpfc_hbq_defs[hbqno]->entry_count -
1905 phba->hbqs[hbqno].buffer_count;
1906 if (!count)
1907 return 0;
1908 /* Allocate HBQ entries */
1909 for (i = 0; i < count; i++) {
1910 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1911 if (!hbq_buffer)
1912 break;
1913 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1914 }
1915 /* Check whether HBQ is still in use */
1916 spin_lock_irqsave(&phba->hbalock, flags);
1917 if (!phba->hbq_in_use)
1918 goto err;
1919 while (!list_empty(&hbq_buf_list)) {
1920 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1921 dbuf.list);
1922 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1923 (hbqno << 16));
1924 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1925 phba->hbqs[hbqno].buffer_count++;
1926 posted++;
1927 } else
1928 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1929 }
1930 spin_unlock_irqrestore(&phba->hbalock, flags);
1931 return posted;
1932 err:
1933 spin_unlock_irqrestore(&phba->hbalock, flags);
1934 while (!list_empty(&hbq_buf_list)) {
1935 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1936 dbuf.list);
1937 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1938 }
1939 return 0;
1940 }
1941
1942 /**
1943 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1944 * @phba: Pointer to HBA context object.
1945 * @qno: HBQ number.
1946 *
1947 * This function posts more buffers to the HBQ. This function
1948 * is called with no lock held. The function returns the number of HBQ entries
1949 * successfully allocated.
1950 **/
1951 int
1952 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1953 {
1954 if (phba->sli_rev == LPFC_SLI_REV4)
1955 return 0;
1956 else
1957 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1958 lpfc_hbq_defs[qno]->add_count);
1959 }
1960
1961 /**
1962 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1963 * @phba: Pointer to HBA context object.
1964 * @qno: HBQ queue number.
1965 *
1966 * This function is called from SLI initialization code path with
1967 * no lock held to post initial HBQ buffers to firmware. The
1968 * function returns the number of HBQ entries successfully allocated.
1969 **/
1970 static int
1971 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1972 {
1973 if (phba->sli_rev == LPFC_SLI_REV4)
1974 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1975 lpfc_hbq_defs[qno]->entry_count);
1976 else
1977 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1978 lpfc_hbq_defs[qno]->init_count);
1979 }
1980
1981 /**
1982 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1983 * @phba: Pointer to HBA context object.
1984 * @hbqno: HBQ number.
1985 *
1986 * This function removes the first hbq buffer on an hbq list and returns a
1987 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1988 **/
1989 static struct hbq_dmabuf *
1990 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1991 {
1992 struct lpfc_dmabuf *d_buf;
1993
1994 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1995 if (!d_buf)
1996 return NULL;
1997 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1998 }
1999
2000 /**
2001 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2002 * @phba: Pointer to HBA context object.
2003 * @tag: Tag of the hbq buffer.
2004 *
2005 * This function searches for the hbq buffer associated with the given tag in
2006 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2007 * otherwise it returns NULL.
2008 **/
2009 static struct hbq_dmabuf *
2010 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2011 {
2012 struct lpfc_dmabuf *d_buf;
2013 struct hbq_dmabuf *hbq_buf;
2014 uint32_t hbqno;
2015
2016 hbqno = tag >> 16;
2017 if (hbqno >= LPFC_MAX_HBQS)
2018 return NULL;
2019
2020 spin_lock_irq(&phba->hbalock);
2021 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2022 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2023 if (hbq_buf->tag == tag) {
2024 spin_unlock_irq(&phba->hbalock);
2025 return hbq_buf;
2026 }
2027 }
2028 spin_unlock_irq(&phba->hbalock);
2029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2030 "1803 Bad hbq tag. Data: x%x x%x\n",
2031 tag, phba->hbqs[tag >> 16].buffer_count);
2032 return NULL;
2033 }
2034
2035 /**
2036 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2037 * @phba: Pointer to HBA context object.
2038 * @hbq_buffer: Pointer to HBQ buffer.
2039 *
2040 * This function is called with hbalock. This function gives back
2041 * the hbq buffer to firmware. If the HBQ does not have space to
2042 * post the buffer, it will free the buffer.
2043 **/
2044 void
2045 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2046 {
2047 uint32_t hbqno;
2048
2049 if (hbq_buffer) {
2050 hbqno = hbq_buffer->tag >> 16;
2051 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2052 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2053 }
2054 }
2055
2056 /**
2057 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2058 * @mbxCommand: mailbox command code.
2059 *
2060 * This function is called by the mailbox event handler function to verify
2061 * that the completed mailbox command is a legitimate mailbox command. If the
2062 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2063 * and the mailbox event handler will take the HBA offline.
2064 **/
2065 static int
2066 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2067 {
2068 uint8_t ret;
2069
2070 switch (mbxCommand) {
2071 case MBX_LOAD_SM:
2072 case MBX_READ_NV:
2073 case MBX_WRITE_NV:
2074 case MBX_WRITE_VPARMS:
2075 case MBX_RUN_BIU_DIAG:
2076 case MBX_INIT_LINK:
2077 case MBX_DOWN_LINK:
2078 case MBX_CONFIG_LINK:
2079 case MBX_CONFIG_RING:
2080 case MBX_RESET_RING:
2081 case MBX_READ_CONFIG:
2082 case MBX_READ_RCONFIG:
2083 case MBX_READ_SPARM:
2084 case MBX_READ_STATUS:
2085 case MBX_READ_RPI:
2086 case MBX_READ_XRI:
2087 case MBX_READ_REV:
2088 case MBX_READ_LNK_STAT:
2089 case MBX_REG_LOGIN:
2090 case MBX_UNREG_LOGIN:
2091 case MBX_CLEAR_LA:
2092 case MBX_DUMP_MEMORY:
2093 case MBX_DUMP_CONTEXT:
2094 case MBX_RUN_DIAGS:
2095 case MBX_RESTART:
2096 case MBX_UPDATE_CFG:
2097 case MBX_DOWN_LOAD:
2098 case MBX_DEL_LD_ENTRY:
2099 case MBX_RUN_PROGRAM:
2100 case MBX_SET_MASK:
2101 case MBX_SET_VARIABLE:
2102 case MBX_UNREG_D_ID:
2103 case MBX_KILL_BOARD:
2104 case MBX_CONFIG_FARP:
2105 case MBX_BEACON:
2106 case MBX_LOAD_AREA:
2107 case MBX_RUN_BIU_DIAG64:
2108 case MBX_CONFIG_PORT:
2109 case MBX_READ_SPARM64:
2110 case MBX_READ_RPI64:
2111 case MBX_REG_LOGIN64:
2112 case MBX_READ_TOPOLOGY:
2113 case MBX_WRITE_WWN:
2114 case MBX_SET_DEBUG:
2115 case MBX_LOAD_EXP_ROM:
2116 case MBX_ASYNCEVT_ENABLE:
2117 case MBX_REG_VPI:
2118 case MBX_UNREG_VPI:
2119 case MBX_HEARTBEAT:
2120 case MBX_PORT_CAPABILITIES:
2121 case MBX_PORT_IOV_CONTROL:
2122 case MBX_SLI4_CONFIG:
2123 case MBX_SLI4_REQ_FTRS:
2124 case MBX_REG_FCFI:
2125 case MBX_UNREG_FCFI:
2126 case MBX_REG_VFI:
2127 case MBX_UNREG_VFI:
2128 case MBX_INIT_VPI:
2129 case MBX_INIT_VFI:
2130 case MBX_RESUME_RPI:
2131 case MBX_READ_EVENT_LOG_STATUS:
2132 case MBX_READ_EVENT_LOG:
2133 case MBX_SECURITY_MGMT:
2134 case MBX_AUTH_PORT:
2135 case MBX_ACCESS_VDATA:
2136 ret = mbxCommand;
2137 break;
2138 default:
2139 ret = MBX_SHUTDOWN;
2140 break;
2141 }
2142 return ret;
2143 }
2144
2145 /**
2146 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2147 * @phba: Pointer to HBA context object.
2148 * @pmboxq: Pointer to mailbox command.
2149 *
2150 * This is completion handler function for mailbox commands issued from
2151 * lpfc_sli_issue_mbox_wait function. This function is called by the
2152 * mailbox event handler function with no lock held. This function
2153 * will wake up thread waiting on the wait queue pointed by context1
2154 * of the mailbox.
2155 **/
2156 void
2157 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2158 {
2159 wait_queue_head_t *pdone_q;
2160 unsigned long drvr_flag;
2161
2162 /*
2163 * If pdone_q is empty, the driver thread gave up waiting and
2164 * continued running.
2165 */
2166 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2167 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2168 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2169 if (pdone_q)
2170 wake_up_interruptible(pdone_q);
2171 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2172 return;
2173 }
2174
2175
2176 /**
2177 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2178 * @phba: Pointer to HBA context object.
2179 * @pmb: Pointer to mailbox object.
2180 *
2181 * This function is the default mailbox completion handler. It
2182 * frees the memory resources associated with the completed mailbox
2183 * command. If the completed command is a REG_LOGIN mailbox command,
2184 * this function will issue a UREG_LOGIN to re-claim the RPI.
2185 **/
2186 void
2187 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2188 {
2189 struct lpfc_vport *vport = pmb->vport;
2190 struct lpfc_dmabuf *mp;
2191 struct lpfc_nodelist *ndlp;
2192 struct Scsi_Host *shost;
2193 uint16_t rpi, vpi;
2194 int rc;
2195
2196 mp = (struct lpfc_dmabuf *) (pmb->context1);
2197
2198 if (mp) {
2199 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2200 kfree(mp);
2201 }
2202
2203 /*
2204 * If a REG_LOGIN succeeded after node is destroyed or node
2205 * is in re-discovery driver need to cleanup the RPI.
2206 */
2207 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2208 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2209 !pmb->u.mb.mbxStatus) {
2210 rpi = pmb->u.mb.un.varWords[0];
2211 vpi = pmb->u.mb.un.varRegLogin.vpi;
2212 lpfc_unreg_login(phba, vpi, rpi, pmb);
2213 pmb->vport = vport;
2214 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2215 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2216 if (rc != MBX_NOT_FINISHED)
2217 return;
2218 }
2219
2220 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2221 !(phba->pport->load_flag & FC_UNLOADING) &&
2222 !pmb->u.mb.mbxStatus) {
2223 shost = lpfc_shost_from_vport(vport);
2224 spin_lock_irq(shost->host_lock);
2225 vport->vpi_state |= LPFC_VPI_REGISTERED;
2226 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2227 spin_unlock_irq(shost->host_lock);
2228 }
2229
2230 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2231 ndlp = (struct lpfc_nodelist *)pmb->context2;
2232 lpfc_nlp_put(ndlp);
2233 pmb->context2 = NULL;
2234 }
2235
2236 /* Check security permission status on INIT_LINK mailbox command */
2237 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2238 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2240 "2860 SLI authentication is required "
2241 "for INIT_LINK but has not done yet\n");
2242
2243 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2244 lpfc_sli4_mbox_cmd_free(phba, pmb);
2245 else
2246 mempool_free(pmb, phba->mbox_mem_pool);
2247 }
2248 /**
2249 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2250 * @phba: Pointer to HBA context object.
2251 * @pmb: Pointer to mailbox object.
2252 *
2253 * This function is the unreg rpi mailbox completion handler. It
2254 * frees the memory resources associated with the completed mailbox
2255 * command. An additional refrenece is put on the ndlp to prevent
2256 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2257 * the unreg mailbox command completes, this routine puts the
2258 * reference back.
2259 *
2260 **/
2261 void
2262 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2263 {
2264 struct lpfc_vport *vport = pmb->vport;
2265 struct lpfc_nodelist *ndlp;
2266
2267 ndlp = pmb->context1;
2268 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2269 if (phba->sli_rev == LPFC_SLI_REV4 &&
2270 (bf_get(lpfc_sli_intf_if_type,
2271 &phba->sli4_hba.sli_intf) ==
2272 LPFC_SLI_INTF_IF_TYPE_2)) {
2273 if (ndlp) {
2274 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2275 "0010 UNREG_LOGIN vpi:%x "
2276 "rpi:%x DID:%x map:%x %p\n",
2277 vport->vpi, ndlp->nlp_rpi,
2278 ndlp->nlp_DID,
2279 ndlp->nlp_usg_map, ndlp);
2280 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2281 lpfc_nlp_put(ndlp);
2282 }
2283 }
2284 }
2285
2286 mempool_free(pmb, phba->mbox_mem_pool);
2287 }
2288
2289 /**
2290 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2291 * @phba: Pointer to HBA context object.
2292 *
2293 * This function is called with no lock held. This function processes all
2294 * the completed mailbox commands and gives it to upper layers. The interrupt
2295 * service routine processes mailbox completion interrupt and adds completed
2296 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2297 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2298 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2299 * function returns the mailbox commands to the upper layer by calling the
2300 * completion handler function of each mailbox.
2301 **/
2302 int
2303 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2304 {
2305 MAILBOX_t *pmbox;
2306 LPFC_MBOXQ_t *pmb;
2307 int rc;
2308 LIST_HEAD(cmplq);
2309
2310 phba->sli.slistat.mbox_event++;
2311
2312 /* Get all completed mailboxe buffers into the cmplq */
2313 spin_lock_irq(&phba->hbalock);
2314 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2315 spin_unlock_irq(&phba->hbalock);
2316
2317 /* Get a Mailbox buffer to setup mailbox commands for callback */
2318 do {
2319 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2320 if (pmb == NULL)
2321 break;
2322
2323 pmbox = &pmb->u.mb;
2324
2325 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2326 if (pmb->vport) {
2327 lpfc_debugfs_disc_trc(pmb->vport,
2328 LPFC_DISC_TRC_MBOX_VPORT,
2329 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2330 (uint32_t)pmbox->mbxCommand,
2331 pmbox->un.varWords[0],
2332 pmbox->un.varWords[1]);
2333 }
2334 else {
2335 lpfc_debugfs_disc_trc(phba->pport,
2336 LPFC_DISC_TRC_MBOX,
2337 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2338 (uint32_t)pmbox->mbxCommand,
2339 pmbox->un.varWords[0],
2340 pmbox->un.varWords[1]);
2341 }
2342 }
2343
2344 /*
2345 * It is a fatal error if unknown mbox command completion.
2346 */
2347 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2348 MBX_SHUTDOWN) {
2349 /* Unknown mailbox command compl */
2350 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2351 "(%d):0323 Unknown Mailbox command "
2352 "x%x (x%x/x%x) Cmpl\n",
2353 pmb->vport ? pmb->vport->vpi : 0,
2354 pmbox->mbxCommand,
2355 lpfc_sli_config_mbox_subsys_get(phba,
2356 pmb),
2357 lpfc_sli_config_mbox_opcode_get(phba,
2358 pmb));
2359 phba->link_state = LPFC_HBA_ERROR;
2360 phba->work_hs = HS_FFER3;
2361 lpfc_handle_eratt(phba);
2362 continue;
2363 }
2364
2365 if (pmbox->mbxStatus) {
2366 phba->sli.slistat.mbox_stat_err++;
2367 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2368 /* Mbox cmd cmpl error - RETRYing */
2369 lpfc_printf_log(phba, KERN_INFO,
2370 LOG_MBOX | LOG_SLI,
2371 "(%d):0305 Mbox cmd cmpl "
2372 "error - RETRYing Data: x%x "
2373 "(x%x/x%x) x%x x%x x%x\n",
2374 pmb->vport ? pmb->vport->vpi : 0,
2375 pmbox->mbxCommand,
2376 lpfc_sli_config_mbox_subsys_get(phba,
2377 pmb),
2378 lpfc_sli_config_mbox_opcode_get(phba,
2379 pmb),
2380 pmbox->mbxStatus,
2381 pmbox->un.varWords[0],
2382 pmb->vport->port_state);
2383 pmbox->mbxStatus = 0;
2384 pmbox->mbxOwner = OWN_HOST;
2385 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2386 if (rc != MBX_NOT_FINISHED)
2387 continue;
2388 }
2389 }
2390
2391 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2392 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2393 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2394 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2395 "x%x x%x x%x\n",
2396 pmb->vport ? pmb->vport->vpi : 0,
2397 pmbox->mbxCommand,
2398 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2399 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2400 pmb->mbox_cmpl,
2401 *((uint32_t *) pmbox),
2402 pmbox->un.varWords[0],
2403 pmbox->un.varWords[1],
2404 pmbox->un.varWords[2],
2405 pmbox->un.varWords[3],
2406 pmbox->un.varWords[4],
2407 pmbox->un.varWords[5],
2408 pmbox->un.varWords[6],
2409 pmbox->un.varWords[7],
2410 pmbox->un.varWords[8],
2411 pmbox->un.varWords[9],
2412 pmbox->un.varWords[10]);
2413
2414 if (pmb->mbox_cmpl)
2415 pmb->mbox_cmpl(phba,pmb);
2416 } while (1);
2417 return 0;
2418 }
2419
2420 /**
2421 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2422 * @phba: Pointer to HBA context object.
2423 * @pring: Pointer to driver SLI ring object.
2424 * @tag: buffer tag.
2425 *
2426 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2427 * is set in the tag the buffer is posted for a particular exchange,
2428 * the function will return the buffer without replacing the buffer.
2429 * If the buffer is for unsolicited ELS or CT traffic, this function
2430 * returns the buffer and also posts another buffer to the firmware.
2431 **/
2432 static struct lpfc_dmabuf *
2433 lpfc_sli_get_buff(struct lpfc_hba *phba,
2434 struct lpfc_sli_ring *pring,
2435 uint32_t tag)
2436 {
2437 struct hbq_dmabuf *hbq_entry;
2438
2439 if (tag & QUE_BUFTAG_BIT)
2440 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2441 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2442 if (!hbq_entry)
2443 return NULL;
2444 return &hbq_entry->dbuf;
2445 }
2446
2447 /**
2448 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2449 * @phba: Pointer to HBA context object.
2450 * @pring: Pointer to driver SLI ring object.
2451 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2452 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2453 * @fch_type: the type for the first frame of the sequence.
2454 *
2455 * This function is called with no lock held. This function uses the r_ctl and
2456 * type of the received sequence to find the correct callback function to call
2457 * to process the sequence.
2458 **/
2459 static int
2460 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2461 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2462 uint32_t fch_type)
2463 {
2464 int i;
2465
2466 /* unSolicited Responses */
2467 if (pring->prt[0].profile) {
2468 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2469 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2470 saveq);
2471 return 1;
2472 }
2473 /* We must search, based on rctl / type
2474 for the right routine */
2475 for (i = 0; i < pring->num_mask; i++) {
2476 if ((pring->prt[i].rctl == fch_r_ctl) &&
2477 (pring->prt[i].type == fch_type)) {
2478 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2479 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2480 (phba, pring, saveq);
2481 return 1;
2482 }
2483 }
2484 return 0;
2485 }
2486
2487 /**
2488 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2489 * @phba: Pointer to HBA context object.
2490 * @pring: Pointer to driver SLI ring object.
2491 * @saveq: Pointer to the unsolicited iocb.
2492 *
2493 * This function is called with no lock held by the ring event handler
2494 * when there is an unsolicited iocb posted to the response ring by the
2495 * firmware. This function gets the buffer associated with the iocbs
2496 * and calls the event handler for the ring. This function handles both
2497 * qring buffers and hbq buffers.
2498 * When the function returns 1 the caller can free the iocb object otherwise
2499 * upper layer functions will free the iocb objects.
2500 **/
2501 static int
2502 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2503 struct lpfc_iocbq *saveq)
2504 {
2505 IOCB_t * irsp;
2506 WORD5 * w5p;
2507 uint32_t Rctl, Type;
2508 struct lpfc_iocbq *iocbq;
2509 struct lpfc_dmabuf *dmzbuf;
2510
2511 irsp = &(saveq->iocb);
2512
2513 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2514 if (pring->lpfc_sli_rcv_async_status)
2515 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2516 else
2517 lpfc_printf_log(phba,
2518 KERN_WARNING,
2519 LOG_SLI,
2520 "0316 Ring %d handler: unexpected "
2521 "ASYNC_STATUS iocb received evt_code "
2522 "0x%x\n",
2523 pring->ringno,
2524 irsp->un.asyncstat.evt_code);
2525 return 1;
2526 }
2527
2528 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2529 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2530 if (irsp->ulpBdeCount > 0) {
2531 dmzbuf = lpfc_sli_get_buff(phba, pring,
2532 irsp->un.ulpWord[3]);
2533 lpfc_in_buf_free(phba, dmzbuf);
2534 }
2535
2536 if (irsp->ulpBdeCount > 1) {
2537 dmzbuf = lpfc_sli_get_buff(phba, pring,
2538 irsp->unsli3.sli3Words[3]);
2539 lpfc_in_buf_free(phba, dmzbuf);
2540 }
2541
2542 if (irsp->ulpBdeCount > 2) {
2543 dmzbuf = lpfc_sli_get_buff(phba, pring,
2544 irsp->unsli3.sli3Words[7]);
2545 lpfc_in_buf_free(phba, dmzbuf);
2546 }
2547
2548 return 1;
2549 }
2550
2551 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2552 if (irsp->ulpBdeCount != 0) {
2553 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2554 irsp->un.ulpWord[3]);
2555 if (!saveq->context2)
2556 lpfc_printf_log(phba,
2557 KERN_ERR,
2558 LOG_SLI,
2559 "0341 Ring %d Cannot find buffer for "
2560 "an unsolicited iocb. tag 0x%x\n",
2561 pring->ringno,
2562 irsp->un.ulpWord[3]);
2563 }
2564 if (irsp->ulpBdeCount == 2) {
2565 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2566 irsp->unsli3.sli3Words[7]);
2567 if (!saveq->context3)
2568 lpfc_printf_log(phba,
2569 KERN_ERR,
2570 LOG_SLI,
2571 "0342 Ring %d Cannot find buffer for an"
2572 " unsolicited iocb. tag 0x%x\n",
2573 pring->ringno,
2574 irsp->unsli3.sli3Words[7]);
2575 }
2576 list_for_each_entry(iocbq, &saveq->list, list) {
2577 irsp = &(iocbq->iocb);
2578 if (irsp->ulpBdeCount != 0) {
2579 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2580 irsp->un.ulpWord[3]);
2581 if (!iocbq->context2)
2582 lpfc_printf_log(phba,
2583 KERN_ERR,
2584 LOG_SLI,
2585 "0343 Ring %d Cannot find "
2586 "buffer for an unsolicited iocb"
2587 ". tag 0x%x\n", pring->ringno,
2588 irsp->un.ulpWord[3]);
2589 }
2590 if (irsp->ulpBdeCount == 2) {
2591 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2592 irsp->unsli3.sli3Words[7]);
2593 if (!iocbq->context3)
2594 lpfc_printf_log(phba,
2595 KERN_ERR,
2596 LOG_SLI,
2597 "0344 Ring %d Cannot find "
2598 "buffer for an unsolicited "
2599 "iocb. tag 0x%x\n",
2600 pring->ringno,
2601 irsp->unsli3.sli3Words[7]);
2602 }
2603 }
2604 }
2605 if (irsp->ulpBdeCount != 0 &&
2606 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2607 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2608 int found = 0;
2609
2610 /* search continue save q for same XRI */
2611 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2612 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2613 saveq->iocb.unsli3.rcvsli3.ox_id) {
2614 list_add_tail(&saveq->list, &iocbq->list);
2615 found = 1;
2616 break;
2617 }
2618 }
2619 if (!found)
2620 list_add_tail(&saveq->clist,
2621 &pring->iocb_continue_saveq);
2622 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2623 list_del_init(&iocbq->clist);
2624 saveq = iocbq;
2625 irsp = &(saveq->iocb);
2626 } else
2627 return 0;
2628 }
2629 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2630 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2631 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2632 Rctl = FC_RCTL_ELS_REQ;
2633 Type = FC_TYPE_ELS;
2634 } else {
2635 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2636 Rctl = w5p->hcsw.Rctl;
2637 Type = w5p->hcsw.Type;
2638
2639 /* Firmware Workaround */
2640 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2641 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2642 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2643 Rctl = FC_RCTL_ELS_REQ;
2644 Type = FC_TYPE_ELS;
2645 w5p->hcsw.Rctl = Rctl;
2646 w5p->hcsw.Type = Type;
2647 }
2648 }
2649
2650 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2651 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2652 "0313 Ring %d handler: unexpected Rctl x%x "
2653 "Type x%x received\n",
2654 pring->ringno, Rctl, Type);
2655
2656 return 1;
2657 }
2658
2659 /**
2660 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2661 * @phba: Pointer to HBA context object.
2662 * @pring: Pointer to driver SLI ring object.
2663 * @prspiocb: Pointer to response iocb object.
2664 *
2665 * This function looks up the iocb_lookup table to get the command iocb
2666 * corresponding to the given response iocb using the iotag of the
2667 * response iocb. This function is called with the hbalock held.
2668 * This function returns the command iocb object if it finds the command
2669 * iocb else returns NULL.
2670 **/
2671 static struct lpfc_iocbq *
2672 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2673 struct lpfc_sli_ring *pring,
2674 struct lpfc_iocbq *prspiocb)
2675 {
2676 struct lpfc_iocbq *cmd_iocb = NULL;
2677 uint16_t iotag;
2678 lockdep_assert_held(&phba->hbalock);
2679
2680 iotag = prspiocb->iocb.ulpIoTag;
2681
2682 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2683 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2684 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2685 /* remove from txcmpl queue list */
2686 list_del_init(&cmd_iocb->list);
2687 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2688 return cmd_iocb;
2689 }
2690 }
2691
2692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2693 "0317 iotag x%x is out of "
2694 "range: max iotag x%x wd0 x%x\n",
2695 iotag, phba->sli.last_iotag,
2696 *(((uint32_t *) &prspiocb->iocb) + 7));
2697 return NULL;
2698 }
2699
2700 /**
2701 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2702 * @phba: Pointer to HBA context object.
2703 * @pring: Pointer to driver SLI ring object.
2704 * @iotag: IOCB tag.
2705 *
2706 * This function looks up the iocb_lookup table to get the command iocb
2707 * corresponding to the given iotag. This function is called with the
2708 * hbalock held.
2709 * This function returns the command iocb object if it finds the command
2710 * iocb else returns NULL.
2711 **/
2712 static struct lpfc_iocbq *
2713 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2714 struct lpfc_sli_ring *pring, uint16_t iotag)
2715 {
2716 struct lpfc_iocbq *cmd_iocb;
2717
2718 lockdep_assert_held(&phba->hbalock);
2719 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2720 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2721 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2722 /* remove from txcmpl queue list */
2723 list_del_init(&cmd_iocb->list);
2724 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2725 return cmd_iocb;
2726 }
2727 }
2728
2729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2730 "0372 iotag x%x is out of range: max iotag (x%x)\n",
2731 iotag, phba->sli.last_iotag);
2732 return NULL;
2733 }
2734
2735 /**
2736 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2737 * @phba: Pointer to HBA context object.
2738 * @pring: Pointer to driver SLI ring object.
2739 * @saveq: Pointer to the response iocb to be processed.
2740 *
2741 * This function is called by the ring event handler for non-fcp
2742 * rings when there is a new response iocb in the response ring.
2743 * The caller is not required to hold any locks. This function
2744 * gets the command iocb associated with the response iocb and
2745 * calls the completion handler for the command iocb. If there
2746 * is no completion handler, the function will free the resources
2747 * associated with command iocb. If the response iocb is for
2748 * an already aborted command iocb, the status of the completion
2749 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2750 * This function always returns 1.
2751 **/
2752 static int
2753 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2754 struct lpfc_iocbq *saveq)
2755 {
2756 struct lpfc_iocbq *cmdiocbp;
2757 int rc = 1;
2758 unsigned long iflag;
2759
2760 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2761 spin_lock_irqsave(&phba->hbalock, iflag);
2762 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2763 spin_unlock_irqrestore(&phba->hbalock, iflag);
2764
2765 if (cmdiocbp) {
2766 if (cmdiocbp->iocb_cmpl) {
2767 /*
2768 * If an ELS command failed send an event to mgmt
2769 * application.
2770 */
2771 if (saveq->iocb.ulpStatus &&
2772 (pring->ringno == LPFC_ELS_RING) &&
2773 (cmdiocbp->iocb.ulpCommand ==
2774 CMD_ELS_REQUEST64_CR))
2775 lpfc_send_els_failure_event(phba,
2776 cmdiocbp, saveq);
2777
2778 /*
2779 * Post all ELS completions to the worker thread.
2780 * All other are passed to the completion callback.
2781 */
2782 if (pring->ringno == LPFC_ELS_RING) {
2783 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2784 (cmdiocbp->iocb_flag &
2785 LPFC_DRIVER_ABORTED)) {
2786 spin_lock_irqsave(&phba->hbalock,
2787 iflag);
2788 cmdiocbp->iocb_flag &=
2789 ~LPFC_DRIVER_ABORTED;
2790 spin_unlock_irqrestore(&phba->hbalock,
2791 iflag);
2792 saveq->iocb.ulpStatus =
2793 IOSTAT_LOCAL_REJECT;
2794 saveq->iocb.un.ulpWord[4] =
2795 IOERR_SLI_ABORTED;
2796
2797 /* Firmware could still be in progress
2798 * of DMAing payload, so don't free data
2799 * buffer till after a hbeat.
2800 */
2801 spin_lock_irqsave(&phba->hbalock,
2802 iflag);
2803 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2804 spin_unlock_irqrestore(&phba->hbalock,
2805 iflag);
2806 }
2807 if (phba->sli_rev == LPFC_SLI_REV4) {
2808 if (saveq->iocb_flag &
2809 LPFC_EXCHANGE_BUSY) {
2810 /* Set cmdiocb flag for the
2811 * exchange busy so sgl (xri)
2812 * will not be released until
2813 * the abort xri is received
2814 * from hba.
2815 */
2816 spin_lock_irqsave(
2817 &phba->hbalock, iflag);
2818 cmdiocbp->iocb_flag |=
2819 LPFC_EXCHANGE_BUSY;
2820 spin_unlock_irqrestore(
2821 &phba->hbalock, iflag);
2822 }
2823 if (cmdiocbp->iocb_flag &
2824 LPFC_DRIVER_ABORTED) {
2825 /*
2826 * Clear LPFC_DRIVER_ABORTED
2827 * bit in case it was driver
2828 * initiated abort.
2829 */
2830 spin_lock_irqsave(
2831 &phba->hbalock, iflag);
2832 cmdiocbp->iocb_flag &=
2833 ~LPFC_DRIVER_ABORTED;
2834 spin_unlock_irqrestore(
2835 &phba->hbalock, iflag);
2836 cmdiocbp->iocb.ulpStatus =
2837 IOSTAT_LOCAL_REJECT;
2838 cmdiocbp->iocb.un.ulpWord[4] =
2839 IOERR_ABORT_REQUESTED;
2840 /*
2841 * For SLI4, irsiocb contains
2842 * NO_XRI in sli_xritag, it
2843 * shall not affect releasing
2844 * sgl (xri) process.
2845 */
2846 saveq->iocb.ulpStatus =
2847 IOSTAT_LOCAL_REJECT;
2848 saveq->iocb.un.ulpWord[4] =
2849 IOERR_SLI_ABORTED;
2850 spin_lock_irqsave(
2851 &phba->hbalock, iflag);
2852 saveq->iocb_flag |=
2853 LPFC_DELAY_MEM_FREE;
2854 spin_unlock_irqrestore(
2855 &phba->hbalock, iflag);
2856 }
2857 }
2858 }
2859 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2860 } else
2861 lpfc_sli_release_iocbq(phba, cmdiocbp);
2862 } else {
2863 /*
2864 * Unknown initiating command based on the response iotag.
2865 * This could be the case on the ELS ring because of
2866 * lpfc_els_abort().
2867 */
2868 if (pring->ringno != LPFC_ELS_RING) {
2869 /*
2870 * Ring <ringno> handler: unexpected completion IoTag
2871 * <IoTag>
2872 */
2873 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2874 "0322 Ring %d handler: "
2875 "unexpected completion IoTag x%x "
2876 "Data: x%x x%x x%x x%x\n",
2877 pring->ringno,
2878 saveq->iocb.ulpIoTag,
2879 saveq->iocb.ulpStatus,
2880 saveq->iocb.un.ulpWord[4],
2881 saveq->iocb.ulpCommand,
2882 saveq->iocb.ulpContext);
2883 }
2884 }
2885
2886 return rc;
2887 }
2888
2889 /**
2890 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2891 * @phba: Pointer to HBA context object.
2892 * @pring: Pointer to driver SLI ring object.
2893 *
2894 * This function is called from the iocb ring event handlers when
2895 * put pointer is ahead of the get pointer for a ring. This function signal
2896 * an error attention condition to the worker thread and the worker
2897 * thread will transition the HBA to offline state.
2898 **/
2899 static void
2900 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2901 {
2902 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2903 /*
2904 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2905 * rsp ring <portRspMax>
2906 */
2907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2908 "0312 Ring %d handler: portRspPut %d "
2909 "is bigger than rsp ring %d\n",
2910 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2911 pring->sli.sli3.numRiocb);
2912
2913 phba->link_state = LPFC_HBA_ERROR;
2914
2915 /*
2916 * All error attention handlers are posted to
2917 * worker thread
2918 */
2919 phba->work_ha |= HA_ERATT;
2920 phba->work_hs = HS_FFER3;
2921
2922 lpfc_worker_wake_up(phba);
2923
2924 return;
2925 }
2926
2927 /**
2928 * lpfc_poll_eratt - Error attention polling timer timeout handler
2929 * @ptr: Pointer to address of HBA context object.
2930 *
2931 * This function is invoked by the Error Attention polling timer when the
2932 * timer times out. It will check the SLI Error Attention register for
2933 * possible attention events. If so, it will post an Error Attention event
2934 * and wake up worker thread to process it. Otherwise, it will set up the
2935 * Error Attention polling timer for the next poll.
2936 **/
2937 void lpfc_poll_eratt(unsigned long ptr)
2938 {
2939 struct lpfc_hba *phba;
2940 uint32_t eratt = 0;
2941 uint64_t sli_intr, cnt;
2942
2943 phba = (struct lpfc_hba *)ptr;
2944
2945 /* Here we will also keep track of interrupts per sec of the hba */
2946 sli_intr = phba->sli.slistat.sli_intr;
2947
2948 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2949 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2950 sli_intr);
2951 else
2952 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2953
2954 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2955 do_div(cnt, phba->eratt_poll_interval);
2956 phba->sli.slistat.sli_ips = cnt;
2957
2958 phba->sli.slistat.sli_prev_intr = sli_intr;
2959
2960 /* Check chip HA register for error event */
2961 eratt = lpfc_sli_check_eratt(phba);
2962
2963 if (eratt)
2964 /* Tell the worker thread there is work to do */
2965 lpfc_worker_wake_up(phba);
2966 else
2967 /* Restart the timer for next eratt poll */
2968 mod_timer(&phba->eratt_poll,
2969 jiffies +
2970 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
2971 return;
2972 }
2973
2974
2975 /**
2976 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2977 * @phba: Pointer to HBA context object.
2978 * @pring: Pointer to driver SLI ring object.
2979 * @mask: Host attention register mask for this ring.
2980 *
2981 * This function is called from the interrupt context when there is a ring
2982 * event for the fcp ring. The caller does not hold any lock.
2983 * The function processes each response iocb in the response ring until it
2984 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2985 * LE bit set. The function will call the completion handler of the command iocb
2986 * if the response iocb indicates a completion for a command iocb or it is
2987 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2988 * function if this is an unsolicited iocb.
2989 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2990 * to check it explicitly.
2991 */
2992 int
2993 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2994 struct lpfc_sli_ring *pring, uint32_t mask)
2995 {
2996 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2997 IOCB_t *irsp = NULL;
2998 IOCB_t *entry = NULL;
2999 struct lpfc_iocbq *cmdiocbq = NULL;
3000 struct lpfc_iocbq rspiocbq;
3001 uint32_t status;
3002 uint32_t portRspPut, portRspMax;
3003 int rc = 1;
3004 lpfc_iocb_type type;
3005 unsigned long iflag;
3006 uint32_t rsp_cmpl = 0;
3007
3008 spin_lock_irqsave(&phba->hbalock, iflag);
3009 pring->stats.iocb_event++;
3010
3011 /*
3012 * The next available response entry should never exceed the maximum
3013 * entries. If it does, treat it as an adapter hardware error.
3014 */
3015 portRspMax = pring->sli.sli3.numRiocb;
3016 portRspPut = le32_to_cpu(pgp->rspPutInx);
3017 if (unlikely(portRspPut >= portRspMax)) {
3018 lpfc_sli_rsp_pointers_error(phba, pring);
3019 spin_unlock_irqrestore(&phba->hbalock, iflag);
3020 return 1;
3021 }
3022 if (phba->fcp_ring_in_use) {
3023 spin_unlock_irqrestore(&phba->hbalock, iflag);
3024 return 1;
3025 } else
3026 phba->fcp_ring_in_use = 1;
3027
3028 rmb();
3029 while (pring->sli.sli3.rspidx != portRspPut) {
3030 /*
3031 * Fetch an entry off the ring and copy it into a local data
3032 * structure. The copy involves a byte-swap since the
3033 * network byte order and pci byte orders are different.
3034 */
3035 entry = lpfc_resp_iocb(phba, pring);
3036 phba->last_completion_time = jiffies;
3037
3038 if (++pring->sli.sli3.rspidx >= portRspMax)
3039 pring->sli.sli3.rspidx = 0;
3040
3041 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3042 (uint32_t *) &rspiocbq.iocb,
3043 phba->iocb_rsp_size);
3044 INIT_LIST_HEAD(&(rspiocbq.list));
3045 irsp = &rspiocbq.iocb;
3046
3047 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3048 pring->stats.iocb_rsp++;
3049 rsp_cmpl++;
3050
3051 if (unlikely(irsp->ulpStatus)) {
3052 /*
3053 * If resource errors reported from HBA, reduce
3054 * queuedepths of the SCSI device.
3055 */
3056 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3057 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3058 IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
3064 /* Rsp ring <ringno> error: IOCB */
3065 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3066 "0336 Rsp Ring %d error: IOCB Data: "
3067 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3068 pring->ringno,
3069 irsp->un.ulpWord[0],
3070 irsp->un.ulpWord[1],
3071 irsp->un.ulpWord[2],
3072 irsp->un.ulpWord[3],
3073 irsp->un.ulpWord[4],
3074 irsp->un.ulpWord[5],
3075 *(uint32_t *)&irsp->un1,
3076 *((uint32_t *)&irsp->un1 + 1));
3077 }
3078
3079 switch (type) {
3080 case LPFC_ABORT_IOCB:
3081 case LPFC_SOL_IOCB:
3082 /*
3083 * Idle exchange closed via ABTS from port. No iocb
3084 * resources need to be recovered.
3085 */
3086 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3087 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3088 "0333 IOCB cmd 0x%x"
3089 " processed. Skipping"
3090 " completion\n",
3091 irsp->ulpCommand);
3092 break;
3093 }
3094
3095 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3096 &rspiocbq);
3097 if (unlikely(!cmdiocbq))
3098 break;
3099 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3100 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3101 if (cmdiocbq->iocb_cmpl) {
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3104 &rspiocbq);
3105 spin_lock_irqsave(&phba->hbalock, iflag);
3106 }
3107 break;
3108 case LPFC_UNSOL_IOCB:
3109 spin_unlock_irqrestore(&phba->hbalock, iflag);
3110 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3111 spin_lock_irqsave(&phba->hbalock, iflag);
3112 break;
3113 default:
3114 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3115 char adaptermsg[LPFC_MAX_ADPTMSG];
3116 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3117 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3118 MAX_MSG_DATA);
3119 dev_warn(&((phba->pcidev)->dev),
3120 "lpfc%d: %s\n",
3121 phba->brd_no, adaptermsg);
3122 } else {
3123 /* Unknown IOCB command */
3124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3125 "0334 Unknown IOCB command "
3126 "Data: x%x, x%x x%x x%x x%x\n",
3127 type, irsp->ulpCommand,
3128 irsp->ulpStatus,
3129 irsp->ulpIoTag,
3130 irsp->ulpContext);
3131 }
3132 break;
3133 }
3134
3135 /*
3136 * The response IOCB has been processed. Update the ring
3137 * pointer in SLIM. If the port response put pointer has not
3138 * been updated, sync the pgp->rspPutInx and fetch the new port
3139 * response put pointer.
3140 */
3141 writel(pring->sli.sli3.rspidx,
3142 &phba->host_gp[pring->ringno].rspGetInx);
3143
3144 if (pring->sli.sli3.rspidx == portRspPut)
3145 portRspPut = le32_to_cpu(pgp->rspPutInx);
3146 }
3147
3148 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3149 pring->stats.iocb_rsp_full++;
3150 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3151 writel(status, phba->CAregaddr);
3152 readl(phba->CAregaddr);
3153 }
3154 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3155 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3156 pring->stats.iocb_cmd_empty++;
3157
3158 /* Force update of the local copy of cmdGetInx */
3159 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3160 lpfc_sli_resume_iocb(phba, pring);
3161
3162 if ((pring->lpfc_sli_cmd_available))
3163 (pring->lpfc_sli_cmd_available) (phba, pring);
3164
3165 }
3166
3167 phba->fcp_ring_in_use = 0;
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rc;
3170 }
3171
3172 /**
3173 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @rspiocbp: Pointer to driver response IOCB object.
3177 *
3178 * This function is called from the worker thread when there is a slow-path
3179 * response IOCB to process. This function chains all the response iocbs until
3180 * seeing the iocb with the LE bit set. The function will call
3181 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3182 * completion of a command iocb. The function will call the
3183 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3184 * The function frees the resources or calls the completion handler if this
3185 * iocb is an abort completion. The function returns NULL when the response
3186 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3187 * this function shall chain the iocb on to the iocb_continueq and return the
3188 * response iocb passed in.
3189 **/
3190 static struct lpfc_iocbq *
3191 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3192 struct lpfc_iocbq *rspiocbp)
3193 {
3194 struct lpfc_iocbq *saveq;
3195 struct lpfc_iocbq *cmdiocbp;
3196 struct lpfc_iocbq *next_iocb;
3197 IOCB_t *irsp = NULL;
3198 uint32_t free_saveq;
3199 uint8_t iocb_cmd_type;
3200 lpfc_iocb_type type;
3201 unsigned long iflag;
3202 int rc;
3203
3204 spin_lock_irqsave(&phba->hbalock, iflag);
3205 /* First add the response iocb to the countinueq list */
3206 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3207 pring->iocb_continueq_cnt++;
3208
3209 /* Now, determine whether the list is completed for processing */
3210 irsp = &rspiocbp->iocb;
3211 if (irsp->ulpLe) {
3212 /*
3213 * By default, the driver expects to free all resources
3214 * associated with this iocb completion.
3215 */
3216 free_saveq = 1;
3217 saveq = list_get_first(&pring->iocb_continueq,
3218 struct lpfc_iocbq, list);
3219 irsp = &(saveq->iocb);
3220 list_del_init(&pring->iocb_continueq);
3221 pring->iocb_continueq_cnt = 0;
3222
3223 pring->stats.iocb_rsp++;
3224
3225 /*
3226 * If resource errors reported from HBA, reduce
3227 * queuedepths of the SCSI device.
3228 */
3229 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3230 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3231 IOERR_NO_RESOURCES)) {
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
3233 phba->lpfc_rampdown_queue_depth(phba);
3234 spin_lock_irqsave(&phba->hbalock, iflag);
3235 }
3236
3237 if (irsp->ulpStatus) {
3238 /* Rsp ring <ringno> error: IOCB */
3239 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3240 "0328 Rsp Ring %d error: "
3241 "IOCB Data: "
3242 "x%x x%x x%x x%x "
3243 "x%x x%x x%x x%x "
3244 "x%x x%x x%x x%x "
3245 "x%x x%x x%x x%x\n",
3246 pring->ringno,
3247 irsp->un.ulpWord[0],
3248 irsp->un.ulpWord[1],
3249 irsp->un.ulpWord[2],
3250 irsp->un.ulpWord[3],
3251 irsp->un.ulpWord[4],
3252 irsp->un.ulpWord[5],
3253 *(((uint32_t *) irsp) + 6),
3254 *(((uint32_t *) irsp) + 7),
3255 *(((uint32_t *) irsp) + 8),
3256 *(((uint32_t *) irsp) + 9),
3257 *(((uint32_t *) irsp) + 10),
3258 *(((uint32_t *) irsp) + 11),
3259 *(((uint32_t *) irsp) + 12),
3260 *(((uint32_t *) irsp) + 13),
3261 *(((uint32_t *) irsp) + 14),
3262 *(((uint32_t *) irsp) + 15));
3263 }
3264
3265 /*
3266 * Fetch the IOCB command type and call the correct completion
3267 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3268 * get freed back to the lpfc_iocb_list by the discovery
3269 * kernel thread.
3270 */
3271 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3272 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3273 switch (type) {
3274 case LPFC_SOL_IOCB:
3275 spin_unlock_irqrestore(&phba->hbalock, iflag);
3276 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3277 spin_lock_irqsave(&phba->hbalock, iflag);
3278 break;
3279
3280 case LPFC_UNSOL_IOCB:
3281 spin_unlock_irqrestore(&phba->hbalock, iflag);
3282 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3283 spin_lock_irqsave(&phba->hbalock, iflag);
3284 if (!rc)
3285 free_saveq = 0;
3286 break;
3287
3288 case LPFC_ABORT_IOCB:
3289 cmdiocbp = NULL;
3290 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3291 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3292 saveq);
3293 if (cmdiocbp) {
3294 /* Call the specified completion routine */
3295 if (cmdiocbp->iocb_cmpl) {
3296 spin_unlock_irqrestore(&phba->hbalock,
3297 iflag);
3298 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3299 saveq);
3300 spin_lock_irqsave(&phba->hbalock,
3301 iflag);
3302 } else
3303 __lpfc_sli_release_iocbq(phba,
3304 cmdiocbp);
3305 }
3306 break;
3307
3308 case LPFC_UNKNOWN_IOCB:
3309 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3310 char adaptermsg[LPFC_MAX_ADPTMSG];
3311 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3312 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3313 MAX_MSG_DATA);
3314 dev_warn(&((phba->pcidev)->dev),
3315 "lpfc%d: %s\n",
3316 phba->brd_no, adaptermsg);
3317 } else {
3318 /* Unknown IOCB command */
3319 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3320 "0335 Unknown IOCB "
3321 "command Data: x%x "
3322 "x%x x%x x%x\n",
3323 irsp->ulpCommand,
3324 irsp->ulpStatus,
3325 irsp->ulpIoTag,
3326 irsp->ulpContext);
3327 }
3328 break;
3329 }
3330
3331 if (free_saveq) {
3332 list_for_each_entry_safe(rspiocbp, next_iocb,
3333 &saveq->list, list) {
3334 list_del_init(&rspiocbp->list);
3335 __lpfc_sli_release_iocbq(phba, rspiocbp);
3336 }
3337 __lpfc_sli_release_iocbq(phba, saveq);
3338 }
3339 rspiocbp = NULL;
3340 }
3341 spin_unlock_irqrestore(&phba->hbalock, iflag);
3342 return rspiocbp;
3343 }
3344
3345 /**
3346 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3347 * @phba: Pointer to HBA context object.
3348 * @pring: Pointer to driver SLI ring object.
3349 * @mask: Host attention register mask for this ring.
3350 *
3351 * This routine wraps the actual slow_ring event process routine from the
3352 * API jump table function pointer from the lpfc_hba struct.
3353 **/
3354 void
3355 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3356 struct lpfc_sli_ring *pring, uint32_t mask)
3357 {
3358 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3359 }
3360
3361 /**
3362 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3363 * @phba: Pointer to HBA context object.
3364 * @pring: Pointer to driver SLI ring object.
3365 * @mask: Host attention register mask for this ring.
3366 *
3367 * This function is called from the worker thread when there is a ring event
3368 * for non-fcp rings. The caller does not hold any lock. The function will
3369 * remove each response iocb in the response ring and calls the handle
3370 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3371 **/
3372 static void
3373 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3374 struct lpfc_sli_ring *pring, uint32_t mask)
3375 {
3376 struct lpfc_pgp *pgp;
3377 IOCB_t *entry;
3378 IOCB_t *irsp = NULL;
3379 struct lpfc_iocbq *rspiocbp = NULL;
3380 uint32_t portRspPut, portRspMax;
3381 unsigned long iflag;
3382 uint32_t status;
3383
3384 pgp = &phba->port_gp[pring->ringno];
3385 spin_lock_irqsave(&phba->hbalock, iflag);
3386 pring->stats.iocb_event++;
3387
3388 /*
3389 * The next available response entry should never exceed the maximum
3390 * entries. If it does, treat it as an adapter hardware error.
3391 */
3392 portRspMax = pring->sli.sli3.numRiocb;
3393 portRspPut = le32_to_cpu(pgp->rspPutInx);
3394 if (portRspPut >= portRspMax) {
3395 /*
3396 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3397 * rsp ring <portRspMax>
3398 */
3399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3400 "0303 Ring %d handler: portRspPut %d "
3401 "is bigger than rsp ring %d\n",
3402 pring->ringno, portRspPut, portRspMax);
3403
3404 phba->link_state = LPFC_HBA_ERROR;
3405 spin_unlock_irqrestore(&phba->hbalock, iflag);
3406
3407 phba->work_hs = HS_FFER3;
3408 lpfc_handle_eratt(phba);
3409
3410 return;
3411 }
3412
3413 rmb();
3414 while (pring->sli.sli3.rspidx != portRspPut) {
3415 /*
3416 * Build a completion list and call the appropriate handler.
3417 * The process is to get the next available response iocb, get
3418 * a free iocb from the list, copy the response data into the
3419 * free iocb, insert to the continuation list, and update the
3420 * next response index to slim. This process makes response
3421 * iocb's in the ring available to DMA as fast as possible but
3422 * pays a penalty for a copy operation. Since the iocb is
3423 * only 32 bytes, this penalty is considered small relative to
3424 * the PCI reads for register values and a slim write. When
3425 * the ulpLe field is set, the entire Command has been
3426 * received.
3427 */
3428 entry = lpfc_resp_iocb(phba, pring);
3429
3430 phba->last_completion_time = jiffies;
3431 rspiocbp = __lpfc_sli_get_iocbq(phba);
3432 if (rspiocbp == NULL) {
3433 printk(KERN_ERR "%s: out of buffers! Failing "
3434 "completion.\n", __func__);
3435 break;
3436 }
3437
3438 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3439 phba->iocb_rsp_size);
3440 irsp = &rspiocbp->iocb;
3441
3442 if (++pring->sli.sli3.rspidx >= portRspMax)
3443 pring->sli.sli3.rspidx = 0;
3444
3445 if (pring->ringno == LPFC_ELS_RING) {
3446 lpfc_debugfs_slow_ring_trc(phba,
3447 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3448 *(((uint32_t *) irsp) + 4),
3449 *(((uint32_t *) irsp) + 6),
3450 *(((uint32_t *) irsp) + 7));
3451 }
3452
3453 writel(pring->sli.sli3.rspidx,
3454 &phba->host_gp[pring->ringno].rspGetInx);
3455
3456 spin_unlock_irqrestore(&phba->hbalock, iflag);
3457 /* Handle the response IOCB */
3458 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3459 spin_lock_irqsave(&phba->hbalock, iflag);
3460
3461 /*
3462 * If the port response put pointer has not been updated, sync
3463 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3464 * response put pointer.
3465 */
3466 if (pring->sli.sli3.rspidx == portRspPut) {
3467 portRspPut = le32_to_cpu(pgp->rspPutInx);
3468 }
3469 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3470
3471 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3472 /* At least one response entry has been freed */
3473 pring->stats.iocb_rsp_full++;
3474 /* SET RxRE_RSP in Chip Att register */
3475 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3476 writel(status, phba->CAregaddr);
3477 readl(phba->CAregaddr); /* flush */
3478 }
3479 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3480 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3481 pring->stats.iocb_cmd_empty++;
3482
3483 /* Force update of the local copy of cmdGetInx */
3484 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3485 lpfc_sli_resume_iocb(phba, pring);
3486
3487 if ((pring->lpfc_sli_cmd_available))
3488 (pring->lpfc_sli_cmd_available) (phba, pring);
3489
3490 }
3491
3492 spin_unlock_irqrestore(&phba->hbalock, iflag);
3493 return;
3494 }
3495
3496 /**
3497 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3498 * @phba: Pointer to HBA context object.
3499 * @pring: Pointer to driver SLI ring object.
3500 * @mask: Host attention register mask for this ring.
3501 *
3502 * This function is called from the worker thread when there is a pending
3503 * ELS response iocb on the driver internal slow-path response iocb worker
3504 * queue. The caller does not hold any lock. The function will remove each
3505 * response iocb from the response worker queue and calls the handle
3506 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3507 **/
3508 static void
3509 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3510 struct lpfc_sli_ring *pring, uint32_t mask)
3511 {
3512 struct lpfc_iocbq *irspiocbq;
3513 struct hbq_dmabuf *dmabuf;
3514 struct lpfc_cq_event *cq_event;
3515 unsigned long iflag;
3516
3517 spin_lock_irqsave(&phba->hbalock, iflag);
3518 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3519 spin_unlock_irqrestore(&phba->hbalock, iflag);
3520 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3521 /* Get the response iocb from the head of work queue */
3522 spin_lock_irqsave(&phba->hbalock, iflag);
3523 list_remove_head(&phba->sli4_hba.sp_queue_event,
3524 cq_event, struct lpfc_cq_event, list);
3525 spin_unlock_irqrestore(&phba->hbalock, iflag);
3526
3527 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3528 case CQE_CODE_COMPL_WQE:
3529 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3530 cq_event);
3531 /* Translate ELS WCQE to response IOCBQ */
3532 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3533 irspiocbq);
3534 if (irspiocbq)
3535 lpfc_sli_sp_handle_rspiocb(phba, pring,
3536 irspiocbq);
3537 break;
3538 case CQE_CODE_RECEIVE:
3539 case CQE_CODE_RECEIVE_V1:
3540 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3541 cq_event);
3542 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3543 break;
3544 default:
3545 break;
3546 }
3547 }
3548 }
3549
3550 /**
3551 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3552 * @phba: Pointer to HBA context object.
3553 * @pring: Pointer to driver SLI ring object.
3554 *
3555 * This function aborts all iocbs in the given ring and frees all the iocb
3556 * objects in txq. This function issues an abort iocb for all the iocb commands
3557 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3558 * the return of this function. The caller is not required to hold any locks.
3559 **/
3560 void
3561 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3562 {
3563 LIST_HEAD(completions);
3564 struct lpfc_iocbq *iocb, *next_iocb;
3565
3566 if (pring->ringno == LPFC_ELS_RING) {
3567 lpfc_fabric_abort_hba(phba);
3568 }
3569
3570 /* Error everything on txq and txcmplq
3571 * First do the txq.
3572 */
3573 if (phba->sli_rev >= LPFC_SLI_REV4) {
3574 spin_lock_irq(&pring->ring_lock);
3575 list_splice_init(&pring->txq, &completions);
3576 pring->txq_cnt = 0;
3577 spin_unlock_irq(&pring->ring_lock);
3578
3579 spin_lock_irq(&phba->hbalock);
3580 /* Next issue ABTS for everything on the txcmplq */
3581 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3582 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3583 spin_unlock_irq(&phba->hbalock);
3584 } else {
3585 spin_lock_irq(&phba->hbalock);
3586 list_splice_init(&pring->txq, &completions);
3587 pring->txq_cnt = 0;
3588
3589 /* Next issue ABTS for everything on the txcmplq */
3590 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3591 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3592 spin_unlock_irq(&phba->hbalock);
3593 }
3594
3595 /* Cancel all the IOCBs from the completions list */
3596 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3597 IOERR_SLI_ABORTED);
3598 }
3599
3600 /**
3601 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3602 * @phba: Pointer to HBA context object.
3603 * @pring: Pointer to driver SLI ring object.
3604 *
3605 * This function aborts all iocbs in FCP rings and frees all the iocb
3606 * objects in txq. This function issues an abort iocb for all the iocb commands
3607 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3608 * the return of this function. The caller is not required to hold any locks.
3609 **/
3610 void
3611 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3612 {
3613 struct lpfc_sli *psli = &phba->sli;
3614 struct lpfc_sli_ring *pring;
3615 uint32_t i;
3616
3617 /* Look on all the FCP Rings for the iotag */
3618 if (phba->sli_rev >= LPFC_SLI_REV4) {
3619 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3620 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3621 lpfc_sli_abort_iocb_ring(phba, pring);
3622 }
3623 } else {
3624 pring = &psli->ring[psli->fcp_ring];
3625 lpfc_sli_abort_iocb_ring(phba, pring);
3626 }
3627 }
3628
3629
3630 /**
3631 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3632 * @phba: Pointer to HBA context object.
3633 *
3634 * This function flushes all iocbs in the fcp ring and frees all the iocb
3635 * objects in txq and txcmplq. This function will not issue abort iocbs
3636 * for all the iocb commands in txcmplq, they will just be returned with
3637 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3638 * slot has been permanently disabled.
3639 **/
3640 void
3641 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3642 {
3643 LIST_HEAD(txq);
3644 LIST_HEAD(txcmplq);
3645 struct lpfc_sli *psli = &phba->sli;
3646 struct lpfc_sli_ring *pring;
3647 uint32_t i;
3648
3649 spin_lock_irq(&phba->hbalock);
3650 /* Indicate the I/O queues are flushed */
3651 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3652 spin_unlock_irq(&phba->hbalock);
3653
3654 /* Look on all the FCP Rings for the iotag */
3655 if (phba->sli_rev >= LPFC_SLI_REV4) {
3656 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3657 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3658
3659 spin_lock_irq(&pring->ring_lock);
3660 /* Retrieve everything on txq */
3661 list_splice_init(&pring->txq, &txq);
3662 /* Retrieve everything on the txcmplq */
3663 list_splice_init(&pring->txcmplq, &txcmplq);
3664 pring->txq_cnt = 0;
3665 pring->txcmplq_cnt = 0;
3666 spin_unlock_irq(&pring->ring_lock);
3667
3668 /* Flush the txq */
3669 lpfc_sli_cancel_iocbs(phba, &txq,
3670 IOSTAT_LOCAL_REJECT,
3671 IOERR_SLI_DOWN);
3672 /* Flush the txcmpq */
3673 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3674 IOSTAT_LOCAL_REJECT,
3675 IOERR_SLI_DOWN);
3676 }
3677 } else {
3678 pring = &psli->ring[psli->fcp_ring];
3679
3680 spin_lock_irq(&phba->hbalock);
3681 /* Retrieve everything on txq */
3682 list_splice_init(&pring->txq, &txq);
3683 /* Retrieve everything on the txcmplq */
3684 list_splice_init(&pring->txcmplq, &txcmplq);
3685 pring->txq_cnt = 0;
3686 pring->txcmplq_cnt = 0;
3687 spin_unlock_irq(&phba->hbalock);
3688
3689 /* Flush the txq */
3690 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3691 IOERR_SLI_DOWN);
3692 /* Flush the txcmpq */
3693 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3694 IOERR_SLI_DOWN);
3695 }
3696 }
3697
3698 /**
3699 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3700 * @phba: Pointer to HBA context object.
3701 * @mask: Bit mask to be checked.
3702 *
3703 * This function reads the host status register and compares
3704 * with the provided bit mask to check if HBA completed
3705 * the restart. This function will wait in a loop for the
3706 * HBA to complete restart. If the HBA does not restart within
3707 * 15 iterations, the function will reset the HBA again. The
3708 * function returns 1 when HBA fail to restart otherwise returns
3709 * zero.
3710 **/
3711 static int
3712 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3713 {
3714 uint32_t status;
3715 int i = 0;
3716 int retval = 0;
3717
3718 /* Read the HBA Host Status Register */
3719 if (lpfc_readl(phba->HSregaddr, &status))
3720 return 1;
3721
3722 /*
3723 * Check status register every 100ms for 5 retries, then every
3724 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3725 * every 2.5 sec for 4.
3726 * Break our of the loop if errors occurred during init.
3727 */
3728 while (((status & mask) != mask) &&
3729 !(status & HS_FFERM) &&
3730 i++ < 20) {
3731
3732 if (i <= 5)
3733 msleep(10);
3734 else if (i <= 10)
3735 msleep(500);
3736 else
3737 msleep(2500);
3738
3739 if (i == 15) {
3740 /* Do post */
3741 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3742 lpfc_sli_brdrestart(phba);
3743 }
3744 /* Read the HBA Host Status Register */
3745 if (lpfc_readl(phba->HSregaddr, &status)) {
3746 retval = 1;
3747 break;
3748 }
3749 }
3750
3751 /* Check to see if any errors occurred during init */
3752 if ((status & HS_FFERM) || (i >= 20)) {
3753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3754 "2751 Adapter failed to restart, "
3755 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3756 status,
3757 readl(phba->MBslimaddr + 0xa8),
3758 readl(phba->MBslimaddr + 0xac));
3759 phba->link_state = LPFC_HBA_ERROR;
3760 retval = 1;
3761 }
3762
3763 return retval;
3764 }
3765
3766 /**
3767 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3768 * @phba: Pointer to HBA context object.
3769 * @mask: Bit mask to be checked.
3770 *
3771 * This function checks the host status register to check if HBA is
3772 * ready. This function will wait in a loop for the HBA to be ready
3773 * If the HBA is not ready , the function will will reset the HBA PCI
3774 * function again. The function returns 1 when HBA fail to be ready
3775 * otherwise returns zero.
3776 **/
3777 static int
3778 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3779 {
3780 uint32_t status;
3781 int retval = 0;
3782
3783 /* Read the HBA Host Status Register */
3784 status = lpfc_sli4_post_status_check(phba);
3785
3786 if (status) {
3787 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3788 lpfc_sli_brdrestart(phba);
3789 status = lpfc_sli4_post_status_check(phba);
3790 }
3791
3792 /* Check to see if any errors occurred during init */
3793 if (status) {
3794 phba->link_state = LPFC_HBA_ERROR;
3795 retval = 1;
3796 } else
3797 phba->sli4_hba.intr_enable = 0;
3798
3799 return retval;
3800 }
3801
3802 /**
3803 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3804 * @phba: Pointer to HBA context object.
3805 * @mask: Bit mask to be checked.
3806 *
3807 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3808 * from the API jump table function pointer from the lpfc_hba struct.
3809 **/
3810 int
3811 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3812 {
3813 return phba->lpfc_sli_brdready(phba, mask);
3814 }
3815
3816 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3817
3818 /**
3819 * lpfc_reset_barrier - Make HBA ready for HBA reset
3820 * @phba: Pointer to HBA context object.
3821 *
3822 * This function is called before resetting an HBA. This function is called
3823 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3824 **/
3825 void lpfc_reset_barrier(struct lpfc_hba *phba)
3826 {
3827 uint32_t __iomem *resp_buf;
3828 uint32_t __iomem *mbox_buf;
3829 volatile uint32_t mbox;
3830 uint32_t hc_copy, ha_copy, resp_data;
3831 int i;
3832 uint8_t hdrtype;
3833
3834 lockdep_assert_held(&phba->hbalock);
3835
3836 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3837 if (hdrtype != 0x80 ||
3838 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3839 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3840 return;
3841
3842 /*
3843 * Tell the other part of the chip to suspend temporarily all
3844 * its DMA activity.
3845 */
3846 resp_buf = phba->MBslimaddr;
3847
3848 /* Disable the error attention */
3849 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3850 return;
3851 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3852 readl(phba->HCregaddr); /* flush */
3853 phba->link_flag |= LS_IGNORE_ERATT;
3854
3855 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3856 return;
3857 if (ha_copy & HA_ERATT) {
3858 /* Clear Chip error bit */
3859 writel(HA_ERATT, phba->HAregaddr);
3860 phba->pport->stopped = 1;
3861 }
3862
3863 mbox = 0;
3864 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3865 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3866
3867 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3868 mbox_buf = phba->MBslimaddr;
3869 writel(mbox, mbox_buf);
3870
3871 for (i = 0; i < 50; i++) {
3872 if (lpfc_readl((resp_buf + 1), &resp_data))
3873 return;
3874 if (resp_data != ~(BARRIER_TEST_PATTERN))
3875 mdelay(1);
3876 else
3877 break;
3878 }
3879 resp_data = 0;
3880 if (lpfc_readl((resp_buf + 1), &resp_data))
3881 return;
3882 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3883 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3884 phba->pport->stopped)
3885 goto restore_hc;
3886 else
3887 goto clear_errat;
3888 }
3889
3890 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3891 resp_data = 0;
3892 for (i = 0; i < 500; i++) {
3893 if (lpfc_readl(resp_buf, &resp_data))
3894 return;
3895 if (resp_data != mbox)
3896 mdelay(1);
3897 else
3898 break;
3899 }
3900
3901 clear_errat:
3902
3903 while (++i < 500) {
3904 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3905 return;
3906 if (!(ha_copy & HA_ERATT))
3907 mdelay(1);
3908 else
3909 break;
3910 }
3911
3912 if (readl(phba->HAregaddr) & HA_ERATT) {
3913 writel(HA_ERATT, phba->HAregaddr);
3914 phba->pport->stopped = 1;
3915 }
3916
3917 restore_hc:
3918 phba->link_flag &= ~LS_IGNORE_ERATT;
3919 writel(hc_copy, phba->HCregaddr);
3920 readl(phba->HCregaddr); /* flush */
3921 }
3922
3923 /**
3924 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3925 * @phba: Pointer to HBA context object.
3926 *
3927 * This function issues a kill_board mailbox command and waits for
3928 * the error attention interrupt. This function is called for stopping
3929 * the firmware processing. The caller is not required to hold any
3930 * locks. This function calls lpfc_hba_down_post function to free
3931 * any pending commands after the kill. The function will return 1 when it
3932 * fails to kill the board else will return 0.
3933 **/
3934 int
3935 lpfc_sli_brdkill(struct lpfc_hba *phba)
3936 {
3937 struct lpfc_sli *psli;
3938 LPFC_MBOXQ_t *pmb;
3939 uint32_t status;
3940 uint32_t ha_copy;
3941 int retval;
3942 int i = 0;
3943
3944 psli = &phba->sli;
3945
3946 /* Kill HBA */
3947 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3948 "0329 Kill HBA Data: x%x x%x\n",
3949 phba->pport->port_state, psli->sli_flag);
3950
3951 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3952 if (!pmb)
3953 return 1;
3954
3955 /* Disable the error attention */
3956 spin_lock_irq(&phba->hbalock);
3957 if (lpfc_readl(phba->HCregaddr, &status)) {
3958 spin_unlock_irq(&phba->hbalock);
3959 mempool_free(pmb, phba->mbox_mem_pool);
3960 return 1;
3961 }
3962 status &= ~HC_ERINT_ENA;
3963 writel(status, phba->HCregaddr);
3964 readl(phba->HCregaddr); /* flush */
3965 phba->link_flag |= LS_IGNORE_ERATT;
3966 spin_unlock_irq(&phba->hbalock);
3967
3968 lpfc_kill_board(phba, pmb);
3969 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3970 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3971
3972 if (retval != MBX_SUCCESS) {
3973 if (retval != MBX_BUSY)
3974 mempool_free(pmb, phba->mbox_mem_pool);
3975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3976 "2752 KILL_BOARD command failed retval %d\n",
3977 retval);
3978 spin_lock_irq(&phba->hbalock);
3979 phba->link_flag &= ~LS_IGNORE_ERATT;
3980 spin_unlock_irq(&phba->hbalock);
3981 return 1;
3982 }
3983
3984 spin_lock_irq(&phba->hbalock);
3985 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3986 spin_unlock_irq(&phba->hbalock);
3987
3988 mempool_free(pmb, phba->mbox_mem_pool);
3989
3990 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3991 * attention every 100ms for 3 seconds. If we don't get ERATT after
3992 * 3 seconds we still set HBA_ERROR state because the status of the
3993 * board is now undefined.
3994 */
3995 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3996 return 1;
3997 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3998 mdelay(100);
3999 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4000 return 1;
4001 }
4002
4003 del_timer_sync(&psli->mbox_tmo);
4004 if (ha_copy & HA_ERATT) {
4005 writel(HA_ERATT, phba->HAregaddr);
4006 phba->pport->stopped = 1;
4007 }
4008 spin_lock_irq(&phba->hbalock);
4009 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4010 psli->mbox_active = NULL;
4011 phba->link_flag &= ~LS_IGNORE_ERATT;
4012 spin_unlock_irq(&phba->hbalock);
4013
4014 lpfc_hba_down_post(phba);
4015 phba->link_state = LPFC_HBA_ERROR;
4016
4017 return ha_copy & HA_ERATT ? 0 : 1;
4018 }
4019
4020 /**
4021 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4022 * @phba: Pointer to HBA context object.
4023 *
4024 * This function resets the HBA by writing HC_INITFF to the control
4025 * register. After the HBA resets, this function resets all the iocb ring
4026 * indices. This function disables PCI layer parity checking during
4027 * the reset.
4028 * This function returns 0 always.
4029 * The caller is not required to hold any locks.
4030 **/
4031 int
4032 lpfc_sli_brdreset(struct lpfc_hba *phba)
4033 {
4034 struct lpfc_sli *psli;
4035 struct lpfc_sli_ring *pring;
4036 uint16_t cfg_value;
4037 int i;
4038
4039 psli = &phba->sli;
4040
4041 /* Reset HBA */
4042 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4043 "0325 Reset HBA Data: x%x x%x\n",
4044 phba->pport->port_state, psli->sli_flag);
4045
4046 /* perform board reset */
4047 phba->fc_eventTag = 0;
4048 phba->link_events = 0;
4049 phba->pport->fc_myDID = 0;
4050 phba->pport->fc_prevDID = 0;
4051
4052 /* Turn off parity checking and serr during the physical reset */
4053 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4054 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4055 (cfg_value &
4056 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4057
4058 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4059
4060 /* Now toggle INITFF bit in the Host Control Register */
4061 writel(HC_INITFF, phba->HCregaddr);
4062 mdelay(1);
4063 readl(phba->HCregaddr); /* flush */
4064 writel(0, phba->HCregaddr);
4065 readl(phba->HCregaddr); /* flush */
4066
4067 /* Restore PCI cmd register */
4068 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4069
4070 /* Initialize relevant SLI info */
4071 for (i = 0; i < psli->num_rings; i++) {
4072 pring = &psli->ring[i];
4073 pring->flag = 0;
4074 pring->sli.sli3.rspidx = 0;
4075 pring->sli.sli3.next_cmdidx = 0;
4076 pring->sli.sli3.local_getidx = 0;
4077 pring->sli.sli3.cmdidx = 0;
4078 pring->missbufcnt = 0;
4079 }
4080
4081 phba->link_state = LPFC_WARM_START;
4082 return 0;
4083 }
4084
4085 /**
4086 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4087 * @phba: Pointer to HBA context object.
4088 *
4089 * This function resets a SLI4 HBA. This function disables PCI layer parity
4090 * checking during resets the device. The caller is not required to hold
4091 * any locks.
4092 *
4093 * This function returns 0 always.
4094 **/
4095 int
4096 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4097 {
4098 struct lpfc_sli *psli = &phba->sli;
4099 uint16_t cfg_value;
4100 int rc = 0;
4101
4102 /* Reset HBA */
4103 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4104 "0295 Reset HBA Data: x%x x%x x%x\n",
4105 phba->pport->port_state, psli->sli_flag,
4106 phba->hba_flag);
4107
4108 /* perform board reset */
4109 phba->fc_eventTag = 0;
4110 phba->link_events = 0;
4111 phba->pport->fc_myDID = 0;
4112 phba->pport->fc_prevDID = 0;
4113
4114 spin_lock_irq(&phba->hbalock);
4115 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4116 phba->fcf.fcf_flag = 0;
4117 spin_unlock_irq(&phba->hbalock);
4118
4119 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4120 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4121 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4122 return rc;
4123 }
4124
4125 /* Now physically reset the device */
4126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4127 "0389 Performing PCI function reset!\n");
4128
4129 /* Turn off parity checking and serr during the physical reset */
4130 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4131 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4132 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4133
4134 /* Perform FCoE PCI function reset before freeing queue memory */
4135 rc = lpfc_pci_function_reset(phba);
4136 lpfc_sli4_queue_destroy(phba);
4137
4138 /* Restore PCI cmd register */
4139 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4140
4141 return rc;
4142 }
4143
4144 /**
4145 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4146 * @phba: Pointer to HBA context object.
4147 *
4148 * This function is called in the SLI initialization code path to
4149 * restart the HBA. The caller is not required to hold any lock.
4150 * This function writes MBX_RESTART mailbox command to the SLIM and
4151 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4152 * function to free any pending commands. The function enables
4153 * POST only during the first initialization. The function returns zero.
4154 * The function does not guarantee completion of MBX_RESTART mailbox
4155 * command before the return of this function.
4156 **/
4157 static int
4158 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4159 {
4160 MAILBOX_t *mb;
4161 struct lpfc_sli *psli;
4162 volatile uint32_t word0;
4163 void __iomem *to_slim;
4164 uint32_t hba_aer_enabled;
4165
4166 spin_lock_irq(&phba->hbalock);
4167
4168 /* Take PCIe device Advanced Error Reporting (AER) state */
4169 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4170
4171 psli = &phba->sli;
4172
4173 /* Restart HBA */
4174 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4175 "0337 Restart HBA Data: x%x x%x\n",
4176 phba->pport->port_state, psli->sli_flag);
4177
4178 word0 = 0;
4179 mb = (MAILBOX_t *) &word0;
4180 mb->mbxCommand = MBX_RESTART;
4181 mb->mbxHc = 1;
4182
4183 lpfc_reset_barrier(phba);
4184
4185 to_slim = phba->MBslimaddr;
4186 writel(*(uint32_t *) mb, to_slim);
4187 readl(to_slim); /* flush */
4188
4189 /* Only skip post after fc_ffinit is completed */
4190 if (phba->pport->port_state)
4191 word0 = 1; /* This is really setting up word1 */
4192 else
4193 word0 = 0; /* This is really setting up word1 */
4194 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4195 writel(*(uint32_t *) mb, to_slim);
4196 readl(to_slim); /* flush */
4197
4198 lpfc_sli_brdreset(phba);
4199 phba->pport->stopped = 0;
4200 phba->link_state = LPFC_INIT_START;
4201 phba->hba_flag = 0;
4202 spin_unlock_irq(&phba->hbalock);
4203
4204 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4205 psli->stats_start = get_seconds();
4206
4207 /* Give the INITFF and Post time to settle. */
4208 mdelay(100);
4209
4210 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4211 if (hba_aer_enabled)
4212 pci_disable_pcie_error_reporting(phba->pcidev);
4213
4214 lpfc_hba_down_post(phba);
4215
4216 return 0;
4217 }
4218
4219 /**
4220 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4221 * @phba: Pointer to HBA context object.
4222 *
4223 * This function is called in the SLI initialization code path to restart
4224 * a SLI4 HBA. The caller is not required to hold any lock.
4225 * At the end of the function, it calls lpfc_hba_down_post function to
4226 * free any pending commands.
4227 **/
4228 static int
4229 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4230 {
4231 struct lpfc_sli *psli = &phba->sli;
4232 uint32_t hba_aer_enabled;
4233 int rc;
4234
4235 /* Restart HBA */
4236 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4237 "0296 Restart HBA Data: x%x x%x\n",
4238 phba->pport->port_state, psli->sli_flag);
4239
4240 /* Take PCIe device Advanced Error Reporting (AER) state */
4241 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4242
4243 rc = lpfc_sli4_brdreset(phba);
4244
4245 spin_lock_irq(&phba->hbalock);
4246 phba->pport->stopped = 0;
4247 phba->link_state = LPFC_INIT_START;
4248 phba->hba_flag = 0;
4249 spin_unlock_irq(&phba->hbalock);
4250
4251 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4252 psli->stats_start = get_seconds();
4253
4254 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4255 if (hba_aer_enabled)
4256 pci_disable_pcie_error_reporting(phba->pcidev);
4257
4258 lpfc_hba_down_post(phba);
4259
4260 return rc;
4261 }
4262
4263 /**
4264 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4265 * @phba: Pointer to HBA context object.
4266 *
4267 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4268 * API jump table function pointer from the lpfc_hba struct.
4269 **/
4270 int
4271 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4272 {
4273 return phba->lpfc_sli_brdrestart(phba);
4274 }
4275
4276 /**
4277 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4278 * @phba: Pointer to HBA context object.
4279 *
4280 * This function is called after a HBA restart to wait for successful
4281 * restart of the HBA. Successful restart of the HBA is indicated by
4282 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4283 * iteration, the function will restart the HBA again. The function returns
4284 * zero if HBA successfully restarted else returns negative error code.
4285 **/
4286 static int
4287 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4288 {
4289 uint32_t status, i = 0;
4290
4291 /* Read the HBA Host Status Register */
4292 if (lpfc_readl(phba->HSregaddr, &status))
4293 return -EIO;
4294
4295 /* Check status register to see what current state is */
4296 i = 0;
4297 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4298
4299 /* Check every 10ms for 10 retries, then every 100ms for 90
4300 * retries, then every 1 sec for 50 retires for a total of
4301 * ~60 seconds before reset the board again and check every
4302 * 1 sec for 50 retries. The up to 60 seconds before the
4303 * board ready is required by the Falcon FIPS zeroization
4304 * complete, and any reset the board in between shall cause
4305 * restart of zeroization, further delay the board ready.
4306 */
4307 if (i++ >= 200) {
4308 /* Adapter failed to init, timeout, status reg
4309 <status> */
4310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4311 "0436 Adapter failed to init, "
4312 "timeout, status reg x%x, "
4313 "FW Data: A8 x%x AC x%x\n", status,
4314 readl(phba->MBslimaddr + 0xa8),
4315 readl(phba->MBslimaddr + 0xac));
4316 phba->link_state = LPFC_HBA_ERROR;
4317 return -ETIMEDOUT;
4318 }
4319
4320 /* Check to see if any errors occurred during init */
4321 if (status & HS_FFERM) {
4322 /* ERROR: During chipset initialization */
4323 /* Adapter failed to init, chipset, status reg
4324 <status> */
4325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4326 "0437 Adapter failed to init, "
4327 "chipset, status reg x%x, "
4328 "FW Data: A8 x%x AC x%x\n", status,
4329 readl(phba->MBslimaddr + 0xa8),
4330 readl(phba->MBslimaddr + 0xac));
4331 phba->link_state = LPFC_HBA_ERROR;
4332 return -EIO;
4333 }
4334
4335 if (i <= 10)
4336 msleep(10);
4337 else if (i <= 100)
4338 msleep(100);
4339 else
4340 msleep(1000);
4341
4342 if (i == 150) {
4343 /* Do post */
4344 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4345 lpfc_sli_brdrestart(phba);
4346 }
4347 /* Read the HBA Host Status Register */
4348 if (lpfc_readl(phba->HSregaddr, &status))
4349 return -EIO;
4350 }
4351
4352 /* Check to see if any errors occurred during init */
4353 if (status & HS_FFERM) {
4354 /* ERROR: During chipset initialization */
4355 /* Adapter failed to init, chipset, status reg <status> */
4356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4357 "0438 Adapter failed to init, chipset, "
4358 "status reg x%x, "
4359 "FW Data: A8 x%x AC x%x\n", status,
4360 readl(phba->MBslimaddr + 0xa8),
4361 readl(phba->MBslimaddr + 0xac));
4362 phba->link_state = LPFC_HBA_ERROR;
4363 return -EIO;
4364 }
4365
4366 /* Clear all interrupt enable conditions */
4367 writel(0, phba->HCregaddr);
4368 readl(phba->HCregaddr); /* flush */
4369
4370 /* setup host attn register */
4371 writel(0xffffffff, phba->HAregaddr);
4372 readl(phba->HAregaddr); /* flush */
4373 return 0;
4374 }
4375
4376 /**
4377 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4378 *
4379 * This function calculates and returns the number of HBQs required to be
4380 * configured.
4381 **/
4382 int
4383 lpfc_sli_hbq_count(void)
4384 {
4385 return ARRAY_SIZE(lpfc_hbq_defs);
4386 }
4387
4388 /**
4389 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4390 *
4391 * This function adds the number of hbq entries in every HBQ to get
4392 * the total number of hbq entries required for the HBA and returns
4393 * the total count.
4394 **/
4395 static int
4396 lpfc_sli_hbq_entry_count(void)
4397 {
4398 int hbq_count = lpfc_sli_hbq_count();
4399 int count = 0;
4400 int i;
4401
4402 for (i = 0; i < hbq_count; ++i)
4403 count += lpfc_hbq_defs[i]->entry_count;
4404 return count;
4405 }
4406
4407 /**
4408 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4409 *
4410 * This function calculates amount of memory required for all hbq entries
4411 * to be configured and returns the total memory required.
4412 **/
4413 int
4414 lpfc_sli_hbq_size(void)
4415 {
4416 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4417 }
4418
4419 /**
4420 * lpfc_sli_hbq_setup - configure and initialize HBQs
4421 * @phba: Pointer to HBA context object.
4422 *
4423 * This function is called during the SLI initialization to configure
4424 * all the HBQs and post buffers to the HBQ. The caller is not
4425 * required to hold any locks. This function will return zero if successful
4426 * else it will return negative error code.
4427 **/
4428 static int
4429 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4430 {
4431 int hbq_count = lpfc_sli_hbq_count();
4432 LPFC_MBOXQ_t *pmb;
4433 MAILBOX_t *pmbox;
4434 uint32_t hbqno;
4435 uint32_t hbq_entry_index;
4436
4437 /* Get a Mailbox buffer to setup mailbox
4438 * commands for HBA initialization
4439 */
4440 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4441
4442 if (!pmb)
4443 return -ENOMEM;
4444
4445 pmbox = &pmb->u.mb;
4446
4447 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4448 phba->link_state = LPFC_INIT_MBX_CMDS;
4449 phba->hbq_in_use = 1;
4450
4451 hbq_entry_index = 0;
4452 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4453 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4454 phba->hbqs[hbqno].hbqPutIdx = 0;
4455 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4456 phba->hbqs[hbqno].entry_count =
4457 lpfc_hbq_defs[hbqno]->entry_count;
4458 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4459 hbq_entry_index, pmb);
4460 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4461
4462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4463 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4464 mbxStatus <status>, ring <num> */
4465
4466 lpfc_printf_log(phba, KERN_ERR,
4467 LOG_SLI | LOG_VPORT,
4468 "1805 Adapter failed to init. "
4469 "Data: x%x x%x x%x\n",
4470 pmbox->mbxCommand,
4471 pmbox->mbxStatus, hbqno);
4472
4473 phba->link_state = LPFC_HBA_ERROR;
4474 mempool_free(pmb, phba->mbox_mem_pool);
4475 return -ENXIO;
4476 }
4477 }
4478 phba->hbq_count = hbq_count;
4479
4480 mempool_free(pmb, phba->mbox_mem_pool);
4481
4482 /* Initially populate or replenish the HBQs */
4483 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4484 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4485 return 0;
4486 }
4487
4488 /**
4489 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4490 * @phba: Pointer to HBA context object.
4491 *
4492 * This function is called during the SLI initialization to configure
4493 * all the HBQs and post buffers to the HBQ. The caller is not
4494 * required to hold any locks. This function will return zero if successful
4495 * else it will return negative error code.
4496 **/
4497 static int
4498 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4499 {
4500 phba->hbq_in_use = 1;
4501 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4502 phba->hbq_count = 1;
4503 /* Initially populate or replenish the HBQs */
4504 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4505 return 0;
4506 }
4507
4508 /**
4509 * lpfc_sli_config_port - Issue config port mailbox command
4510 * @phba: Pointer to HBA context object.
4511 * @sli_mode: sli mode - 2/3
4512 *
4513 * This function is called by the sli intialization code path
4514 * to issue config_port mailbox command. This function restarts the
4515 * HBA firmware and issues a config_port mailbox command to configure
4516 * the SLI interface in the sli mode specified by sli_mode
4517 * variable. The caller is not required to hold any locks.
4518 * The function returns 0 if successful, else returns negative error
4519 * code.
4520 **/
4521 int
4522 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4523 {
4524 LPFC_MBOXQ_t *pmb;
4525 uint32_t resetcount = 0, rc = 0, done = 0;
4526
4527 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4528 if (!pmb) {
4529 phba->link_state = LPFC_HBA_ERROR;
4530 return -ENOMEM;
4531 }
4532
4533 phba->sli_rev = sli_mode;
4534 while (resetcount < 2 && !done) {
4535 spin_lock_irq(&phba->hbalock);
4536 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4537 spin_unlock_irq(&phba->hbalock);
4538 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4539 lpfc_sli_brdrestart(phba);
4540 rc = lpfc_sli_chipset_init(phba);
4541 if (rc)
4542 break;
4543
4544 spin_lock_irq(&phba->hbalock);
4545 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4546 spin_unlock_irq(&phba->hbalock);
4547 resetcount++;
4548
4549 /* Call pre CONFIG_PORT mailbox command initialization. A
4550 * value of 0 means the call was successful. Any other
4551 * nonzero value is a failure, but if ERESTART is returned,
4552 * the driver may reset the HBA and try again.
4553 */
4554 rc = lpfc_config_port_prep(phba);
4555 if (rc == -ERESTART) {
4556 phba->link_state = LPFC_LINK_UNKNOWN;
4557 continue;
4558 } else if (rc)
4559 break;
4560
4561 phba->link_state = LPFC_INIT_MBX_CMDS;
4562 lpfc_config_port(phba, pmb);
4563 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4564 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4565 LPFC_SLI3_HBQ_ENABLED |
4566 LPFC_SLI3_CRP_ENABLED |
4567 LPFC_SLI3_BG_ENABLED |
4568 LPFC_SLI3_DSS_ENABLED);
4569 if (rc != MBX_SUCCESS) {
4570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4571 "0442 Adapter failed to init, mbxCmd x%x "
4572 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4573 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4574 spin_lock_irq(&phba->hbalock);
4575 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4576 spin_unlock_irq(&phba->hbalock);
4577 rc = -ENXIO;
4578 } else {
4579 /* Allow asynchronous mailbox command to go through */
4580 spin_lock_irq(&phba->hbalock);
4581 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4582 spin_unlock_irq(&phba->hbalock);
4583 done = 1;
4584
4585 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4586 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4587 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4588 "3110 Port did not grant ASABT\n");
4589 }
4590 }
4591 if (!done) {
4592 rc = -EINVAL;
4593 goto do_prep_failed;
4594 }
4595 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4596 if (!pmb->u.mb.un.varCfgPort.cMA) {
4597 rc = -ENXIO;
4598 goto do_prep_failed;
4599 }
4600 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4601 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4602 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4603 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4604 phba->max_vpi : phba->max_vports;
4605
4606 } else
4607 phba->max_vpi = 0;
4608 phba->fips_level = 0;
4609 phba->fips_spec_rev = 0;
4610 if (pmb->u.mb.un.varCfgPort.gdss) {
4611 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4612 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4613 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4615 "2850 Security Crypto Active. FIPS x%d "
4616 "(Spec Rev: x%d)",
4617 phba->fips_level, phba->fips_spec_rev);
4618 }
4619 if (pmb->u.mb.un.varCfgPort.sec_err) {
4620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4621 "2856 Config Port Security Crypto "
4622 "Error: x%x ",
4623 pmb->u.mb.un.varCfgPort.sec_err);
4624 }
4625 if (pmb->u.mb.un.varCfgPort.gerbm)
4626 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4627 if (pmb->u.mb.un.varCfgPort.gcrp)
4628 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4629
4630 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4631 phba->port_gp = phba->mbox->us.s3_pgp.port;
4632
4633 if (phba->cfg_enable_bg) {
4634 if (pmb->u.mb.un.varCfgPort.gbg)
4635 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4636 else
4637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4638 "0443 Adapter did not grant "
4639 "BlockGuard\n");
4640 }
4641 } else {
4642 phba->hbq_get = NULL;
4643 phba->port_gp = phba->mbox->us.s2.port;
4644 phba->max_vpi = 0;
4645 }
4646 do_prep_failed:
4647 mempool_free(pmb, phba->mbox_mem_pool);
4648 return rc;
4649 }
4650
4651
4652 /**
4653 * lpfc_sli_hba_setup - SLI intialization function
4654 * @phba: Pointer to HBA context object.
4655 *
4656 * This function is the main SLI intialization function. This function
4657 * is called by the HBA intialization code, HBA reset code and HBA
4658 * error attention handler code. Caller is not required to hold any
4659 * locks. This function issues config_port mailbox command to configure
4660 * the SLI, setup iocb rings and HBQ rings. In the end the function
4661 * calls the config_port_post function to issue init_link mailbox
4662 * command and to start the discovery. The function will return zero
4663 * if successful, else it will return negative error code.
4664 **/
4665 int
4666 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4667 {
4668 uint32_t rc;
4669 int mode = 3, i;
4670 int longs;
4671
4672 switch (phba->cfg_sli_mode) {
4673 case 2:
4674 if (phba->cfg_enable_npiv) {
4675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4676 "1824 NPIV enabled: Override sli_mode "
4677 "parameter (%d) to auto (0).\n",
4678 phba->cfg_sli_mode);
4679 break;
4680 }
4681 mode = 2;
4682 break;
4683 case 0:
4684 case 3:
4685 break;
4686 default:
4687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4688 "1819 Unrecognized sli_mode parameter: %d.\n",
4689 phba->cfg_sli_mode);
4690
4691 break;
4692 }
4693 phba->fcp_embed_io = 0; /* SLI4 FC support only */
4694
4695 rc = lpfc_sli_config_port(phba, mode);
4696
4697 if (rc && phba->cfg_sli_mode == 3)
4698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4699 "1820 Unable to select SLI-3. "
4700 "Not supported by adapter.\n");
4701 if (rc && mode != 2)
4702 rc = lpfc_sli_config_port(phba, 2);
4703 else if (rc && mode == 2)
4704 rc = lpfc_sli_config_port(phba, 3);
4705 if (rc)
4706 goto lpfc_sli_hba_setup_error;
4707
4708 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4709 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4710 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4711 if (!rc) {
4712 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4713 "2709 This device supports "
4714 "Advanced Error Reporting (AER)\n");
4715 spin_lock_irq(&phba->hbalock);
4716 phba->hba_flag |= HBA_AER_ENABLED;
4717 spin_unlock_irq(&phba->hbalock);
4718 } else {
4719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4720 "2708 This device does not support "
4721 "Advanced Error Reporting (AER): %d\n",
4722 rc);
4723 phba->cfg_aer_support = 0;
4724 }
4725 }
4726
4727 if (phba->sli_rev == 3) {
4728 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4729 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4730 } else {
4731 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4732 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4733 phba->sli3_options = 0;
4734 }
4735
4736 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4737 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4738 phba->sli_rev, phba->max_vpi);
4739 rc = lpfc_sli_ring_map(phba);
4740
4741 if (rc)
4742 goto lpfc_sli_hba_setup_error;
4743
4744 /* Initialize VPIs. */
4745 if (phba->sli_rev == LPFC_SLI_REV3) {
4746 /*
4747 * The VPI bitmask and physical ID array are allocated
4748 * and initialized once only - at driver load. A port
4749 * reset doesn't need to reinitialize this memory.
4750 */
4751 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4752 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4753 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4754 GFP_KERNEL);
4755 if (!phba->vpi_bmask) {
4756 rc = -ENOMEM;
4757 goto lpfc_sli_hba_setup_error;
4758 }
4759
4760 phba->vpi_ids = kzalloc(
4761 (phba->max_vpi+1) * sizeof(uint16_t),
4762 GFP_KERNEL);
4763 if (!phba->vpi_ids) {
4764 kfree(phba->vpi_bmask);
4765 rc = -ENOMEM;
4766 goto lpfc_sli_hba_setup_error;
4767 }
4768 for (i = 0; i < phba->max_vpi; i++)
4769 phba->vpi_ids[i] = i;
4770 }
4771 }
4772
4773 /* Init HBQs */
4774 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4775 rc = lpfc_sli_hbq_setup(phba);
4776 if (rc)
4777 goto lpfc_sli_hba_setup_error;
4778 }
4779 spin_lock_irq(&phba->hbalock);
4780 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4781 spin_unlock_irq(&phba->hbalock);
4782
4783 rc = lpfc_config_port_post(phba);
4784 if (rc)
4785 goto lpfc_sli_hba_setup_error;
4786
4787 return rc;
4788
4789 lpfc_sli_hba_setup_error:
4790 phba->link_state = LPFC_HBA_ERROR;
4791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4792 "0445 Firmware initialization failed\n");
4793 return rc;
4794 }
4795
4796 /**
4797 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4798 * @phba: Pointer to HBA context object.
4799 * @mboxq: mailbox pointer.
4800 * This function issue a dump mailbox command to read config region
4801 * 23 and parse the records in the region and populate driver
4802 * data structure.
4803 **/
4804 static int
4805 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4806 {
4807 LPFC_MBOXQ_t *mboxq;
4808 struct lpfc_dmabuf *mp;
4809 struct lpfc_mqe *mqe;
4810 uint32_t data_length;
4811 int rc;
4812
4813 /* Program the default value of vlan_id and fc_map */
4814 phba->valid_vlan = 0;
4815 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4816 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4817 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4818
4819 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4820 if (!mboxq)
4821 return -ENOMEM;
4822
4823 mqe = &mboxq->u.mqe;
4824 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4825 rc = -ENOMEM;
4826 goto out_free_mboxq;
4827 }
4828
4829 mp = (struct lpfc_dmabuf *) mboxq->context1;
4830 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4831
4832 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4833 "(%d):2571 Mailbox cmd x%x Status x%x "
4834 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4835 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4836 "CQ: x%x x%x x%x x%x\n",
4837 mboxq->vport ? mboxq->vport->vpi : 0,
4838 bf_get(lpfc_mqe_command, mqe),
4839 bf_get(lpfc_mqe_status, mqe),
4840 mqe->un.mb_words[0], mqe->un.mb_words[1],
4841 mqe->un.mb_words[2], mqe->un.mb_words[3],
4842 mqe->un.mb_words[4], mqe->un.mb_words[5],
4843 mqe->un.mb_words[6], mqe->un.mb_words[7],
4844 mqe->un.mb_words[8], mqe->un.mb_words[9],
4845 mqe->un.mb_words[10], mqe->un.mb_words[11],
4846 mqe->un.mb_words[12], mqe->un.mb_words[13],
4847 mqe->un.mb_words[14], mqe->un.mb_words[15],
4848 mqe->un.mb_words[16], mqe->un.mb_words[50],
4849 mboxq->mcqe.word0,
4850 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4851 mboxq->mcqe.trailer);
4852
4853 if (rc) {
4854 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4855 kfree(mp);
4856 rc = -EIO;
4857 goto out_free_mboxq;
4858 }
4859 data_length = mqe->un.mb_words[5];
4860 if (data_length > DMP_RGN23_SIZE) {
4861 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4862 kfree(mp);
4863 rc = -EIO;
4864 goto out_free_mboxq;
4865 }
4866
4867 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4868 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4869 kfree(mp);
4870 rc = 0;
4871
4872 out_free_mboxq:
4873 mempool_free(mboxq, phba->mbox_mem_pool);
4874 return rc;
4875 }
4876
4877 /**
4878 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4879 * @phba: pointer to lpfc hba data structure.
4880 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4881 * @vpd: pointer to the memory to hold resulting port vpd data.
4882 * @vpd_size: On input, the number of bytes allocated to @vpd.
4883 * On output, the number of data bytes in @vpd.
4884 *
4885 * This routine executes a READ_REV SLI4 mailbox command. In
4886 * addition, this routine gets the port vpd data.
4887 *
4888 * Return codes
4889 * 0 - successful
4890 * -ENOMEM - could not allocated memory.
4891 **/
4892 static int
4893 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4894 uint8_t *vpd, uint32_t *vpd_size)
4895 {
4896 int rc = 0;
4897 uint32_t dma_size;
4898 struct lpfc_dmabuf *dmabuf;
4899 struct lpfc_mqe *mqe;
4900
4901 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4902 if (!dmabuf)
4903 return -ENOMEM;
4904
4905 /*
4906 * Get a DMA buffer for the vpd data resulting from the READ_REV
4907 * mailbox command.
4908 */
4909 dma_size = *vpd_size;
4910 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4911 &dmabuf->phys, GFP_KERNEL);
4912 if (!dmabuf->virt) {
4913 kfree(dmabuf);
4914 return -ENOMEM;
4915 }
4916
4917 /*
4918 * The SLI4 implementation of READ_REV conflicts at word1,
4919 * bits 31:16 and SLI4 adds vpd functionality not present
4920 * in SLI3. This code corrects the conflicts.
4921 */
4922 lpfc_read_rev(phba, mboxq);
4923 mqe = &mboxq->u.mqe;
4924 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4925 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4926 mqe->un.read_rev.word1 &= 0x0000FFFF;
4927 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4928 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4929
4930 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4931 if (rc) {
4932 dma_free_coherent(&phba->pcidev->dev, dma_size,
4933 dmabuf->virt, dmabuf->phys);
4934 kfree(dmabuf);
4935 return -EIO;
4936 }
4937
4938 /*
4939 * The available vpd length cannot be bigger than the
4940 * DMA buffer passed to the port. Catch the less than
4941 * case and update the caller's size.
4942 */
4943 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4944 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4945
4946 memcpy(vpd, dmabuf->virt, *vpd_size);
4947
4948 dma_free_coherent(&phba->pcidev->dev, dma_size,
4949 dmabuf->virt, dmabuf->phys);
4950 kfree(dmabuf);
4951 return 0;
4952 }
4953
4954 /**
4955 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4956 * @phba: pointer to lpfc hba data structure.
4957 *
4958 * This routine retrieves SLI4 device physical port name this PCI function
4959 * is attached to.
4960 *
4961 * Return codes
4962 * 0 - successful
4963 * otherwise - failed to retrieve physical port name
4964 **/
4965 static int
4966 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4967 {
4968 LPFC_MBOXQ_t *mboxq;
4969 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4970 struct lpfc_controller_attribute *cntl_attr;
4971 struct lpfc_mbx_get_port_name *get_port_name;
4972 void *virtaddr = NULL;
4973 uint32_t alloclen, reqlen;
4974 uint32_t shdr_status, shdr_add_status;
4975 union lpfc_sli4_cfg_shdr *shdr;
4976 char cport_name = 0;
4977 int rc;
4978
4979 /* We assume nothing at this point */
4980 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4981 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4982
4983 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984 if (!mboxq)
4985 return -ENOMEM;
4986 /* obtain link type and link number via READ_CONFIG */
4987 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4988 lpfc_sli4_read_config(phba);
4989 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4990 goto retrieve_ppname;
4991
4992 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4993 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4994 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4995 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4996 LPFC_SLI4_MBX_NEMBED);
4997 if (alloclen < reqlen) {
4998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4999 "3084 Allocated DMA memory size (%d) is "
5000 "less than the requested DMA memory size "
5001 "(%d)\n", alloclen, reqlen);
5002 rc = -ENOMEM;
5003 goto out_free_mboxq;
5004 }
5005 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5006 virtaddr = mboxq->sge_array->addr[0];
5007 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5008 shdr = &mbx_cntl_attr->cfg_shdr;
5009 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5010 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5011 if (shdr_status || shdr_add_status || rc) {
5012 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5013 "3085 Mailbox x%x (x%x/x%x) failed, "
5014 "rc:x%x, status:x%x, add_status:x%x\n",
5015 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5016 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5017 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5018 rc, shdr_status, shdr_add_status);
5019 rc = -ENXIO;
5020 goto out_free_mboxq;
5021 }
5022 cntl_attr = &mbx_cntl_attr->cntl_attr;
5023 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5024 phba->sli4_hba.lnk_info.lnk_tp =
5025 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5026 phba->sli4_hba.lnk_info.lnk_no =
5027 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5028 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5029 "3086 lnk_type:%d, lnk_numb:%d\n",
5030 phba->sli4_hba.lnk_info.lnk_tp,
5031 phba->sli4_hba.lnk_info.lnk_no);
5032
5033 retrieve_ppname:
5034 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5035 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5036 sizeof(struct lpfc_mbx_get_port_name) -
5037 sizeof(struct lpfc_sli4_cfg_mhdr),
5038 LPFC_SLI4_MBX_EMBED);
5039 get_port_name = &mboxq->u.mqe.un.get_port_name;
5040 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5041 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5042 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5043 phba->sli4_hba.lnk_info.lnk_tp);
5044 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5045 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5046 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5047 if (shdr_status || shdr_add_status || rc) {
5048 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5049 "3087 Mailbox x%x (x%x/x%x) failed: "
5050 "rc:x%x, status:x%x, add_status:x%x\n",
5051 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5052 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5053 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5054 rc, shdr_status, shdr_add_status);
5055 rc = -ENXIO;
5056 goto out_free_mboxq;
5057 }
5058 switch (phba->sli4_hba.lnk_info.lnk_no) {
5059 case LPFC_LINK_NUMBER_0:
5060 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5061 &get_port_name->u.response);
5062 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5063 break;
5064 case LPFC_LINK_NUMBER_1:
5065 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5066 &get_port_name->u.response);
5067 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5068 break;
5069 case LPFC_LINK_NUMBER_2:
5070 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5071 &get_port_name->u.response);
5072 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5073 break;
5074 case LPFC_LINK_NUMBER_3:
5075 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5076 &get_port_name->u.response);
5077 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5078 break;
5079 default:
5080 break;
5081 }
5082
5083 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5084 phba->Port[0] = cport_name;
5085 phba->Port[1] = '\0';
5086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5087 "3091 SLI get port name: %s\n", phba->Port);
5088 }
5089
5090 out_free_mboxq:
5091 if (rc != MBX_TIMEOUT) {
5092 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5093 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5094 else
5095 mempool_free(mboxq, phba->mbox_mem_pool);
5096 }
5097 return rc;
5098 }
5099
5100 /**
5101 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5102 * @phba: pointer to lpfc hba data structure.
5103 *
5104 * This routine is called to explicitly arm the SLI4 device's completion and
5105 * event queues
5106 **/
5107 static void
5108 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5109 {
5110 int fcp_eqidx;
5111
5112 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5113 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5114 fcp_eqidx = 0;
5115 if (phba->sli4_hba.fcp_cq) {
5116 do {
5117 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5118 LPFC_QUEUE_REARM);
5119 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
5120 }
5121
5122 if (phba->cfg_fof)
5123 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5124
5125 if (phba->sli4_hba.hba_eq) {
5126 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
5127 fcp_eqidx++)
5128 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
5129 LPFC_QUEUE_REARM);
5130 }
5131
5132 if (phba->cfg_fof)
5133 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5134 }
5135
5136 /**
5137 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5138 * @phba: Pointer to HBA context object.
5139 * @type: The resource extent type.
5140 * @extnt_count: buffer to hold port available extent count.
5141 * @extnt_size: buffer to hold element count per extent.
5142 *
5143 * This function calls the port and retrievs the number of available
5144 * extents and their size for a particular extent type.
5145 *
5146 * Returns: 0 if successful. Nonzero otherwise.
5147 **/
5148 int
5149 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5150 uint16_t *extnt_count, uint16_t *extnt_size)
5151 {
5152 int rc = 0;
5153 uint32_t length;
5154 uint32_t mbox_tmo;
5155 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5156 LPFC_MBOXQ_t *mbox;
5157
5158 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5159 if (!mbox)
5160 return -ENOMEM;
5161
5162 /* Find out how many extents are available for this resource type */
5163 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5164 sizeof(struct lpfc_sli4_cfg_mhdr));
5165 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5166 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5167 length, LPFC_SLI4_MBX_EMBED);
5168
5169 /* Send an extents count of 0 - the GET doesn't use it. */
5170 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5171 LPFC_SLI4_MBX_EMBED);
5172 if (unlikely(rc)) {
5173 rc = -EIO;
5174 goto err_exit;
5175 }
5176
5177 if (!phba->sli4_hba.intr_enable)
5178 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5179 else {
5180 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5181 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5182 }
5183 if (unlikely(rc)) {
5184 rc = -EIO;
5185 goto err_exit;
5186 }
5187
5188 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5189 if (bf_get(lpfc_mbox_hdr_status,
5190 &rsrc_info->header.cfg_shdr.response)) {
5191 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5192 "2930 Failed to get resource extents "
5193 "Status 0x%x Add'l Status 0x%x\n",
5194 bf_get(lpfc_mbox_hdr_status,
5195 &rsrc_info->header.cfg_shdr.response),
5196 bf_get(lpfc_mbox_hdr_add_status,
5197 &rsrc_info->header.cfg_shdr.response));
5198 rc = -EIO;
5199 goto err_exit;
5200 }
5201
5202 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5203 &rsrc_info->u.rsp);
5204 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5205 &rsrc_info->u.rsp);
5206
5207 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5208 "3162 Retrieved extents type-%d from port: count:%d, "
5209 "size:%d\n", type, *extnt_count, *extnt_size);
5210
5211 err_exit:
5212 mempool_free(mbox, phba->mbox_mem_pool);
5213 return rc;
5214 }
5215
5216 /**
5217 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5218 * @phba: Pointer to HBA context object.
5219 * @type: The extent type to check.
5220 *
5221 * This function reads the current available extents from the port and checks
5222 * if the extent count or extent size has changed since the last access.
5223 * Callers use this routine post port reset to understand if there is a
5224 * extent reprovisioning requirement.
5225 *
5226 * Returns:
5227 * -Error: error indicates problem.
5228 * 1: Extent count or size has changed.
5229 * 0: No changes.
5230 **/
5231 static int
5232 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5233 {
5234 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5235 uint16_t size_diff, rsrc_ext_size;
5236 int rc = 0;
5237 struct lpfc_rsrc_blks *rsrc_entry;
5238 struct list_head *rsrc_blk_list = NULL;
5239
5240 size_diff = 0;
5241 curr_ext_cnt = 0;
5242 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5243 &rsrc_ext_cnt,
5244 &rsrc_ext_size);
5245 if (unlikely(rc))
5246 return -EIO;
5247
5248 switch (type) {
5249 case LPFC_RSC_TYPE_FCOE_RPI:
5250 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5251 break;
5252 case LPFC_RSC_TYPE_FCOE_VPI:
5253 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_XRI:
5256 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5257 break;
5258 case LPFC_RSC_TYPE_FCOE_VFI:
5259 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5260 break;
5261 default:
5262 break;
5263 }
5264
5265 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5266 curr_ext_cnt++;
5267 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5268 size_diff++;
5269 }
5270
5271 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5272 rc = 1;
5273
5274 return rc;
5275 }
5276
5277 /**
5278 * lpfc_sli4_cfg_post_extnts -
5279 * @phba: Pointer to HBA context object.
5280 * @extnt_cnt - number of available extents.
5281 * @type - the extent type (rpi, xri, vfi, vpi).
5282 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5283 * @mbox - pointer to the caller's allocated mailbox structure.
5284 *
5285 * This function executes the extents allocation request. It also
5286 * takes care of the amount of memory needed to allocate or get the
5287 * allocated extents. It is the caller's responsibility to evaluate
5288 * the response.
5289 *
5290 * Returns:
5291 * -Error: Error value describes the condition found.
5292 * 0: if successful
5293 **/
5294 static int
5295 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5296 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5297 {
5298 int rc = 0;
5299 uint32_t req_len;
5300 uint32_t emb_len;
5301 uint32_t alloc_len, mbox_tmo;
5302
5303 /* Calculate the total requested length of the dma memory */
5304 req_len = extnt_cnt * sizeof(uint16_t);
5305
5306 /*
5307 * Calculate the size of an embedded mailbox. The uint32_t
5308 * accounts for extents-specific word.
5309 */
5310 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5311 sizeof(uint32_t);
5312
5313 /*
5314 * Presume the allocation and response will fit into an embedded
5315 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5316 */
5317 *emb = LPFC_SLI4_MBX_EMBED;
5318 if (req_len > emb_len) {
5319 req_len = extnt_cnt * sizeof(uint16_t) +
5320 sizeof(union lpfc_sli4_cfg_shdr) +
5321 sizeof(uint32_t);
5322 *emb = LPFC_SLI4_MBX_NEMBED;
5323 }
5324
5325 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5326 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5327 req_len, *emb);
5328 if (alloc_len < req_len) {
5329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5330 "2982 Allocated DMA memory size (x%x) is "
5331 "less than the requested DMA memory "
5332 "size (x%x)\n", alloc_len, req_len);
5333 return -ENOMEM;
5334 }
5335 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5336 if (unlikely(rc))
5337 return -EIO;
5338
5339 if (!phba->sli4_hba.intr_enable)
5340 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5341 else {
5342 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5343 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5344 }
5345
5346 if (unlikely(rc))
5347 rc = -EIO;
5348 return rc;
5349 }
5350
5351 /**
5352 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5353 * @phba: Pointer to HBA context object.
5354 * @type: The resource extent type to allocate.
5355 *
5356 * This function allocates the number of elements for the specified
5357 * resource type.
5358 **/
5359 static int
5360 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5361 {
5362 bool emb = false;
5363 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5364 uint16_t rsrc_id, rsrc_start, j, k;
5365 uint16_t *ids;
5366 int i, rc;
5367 unsigned long longs;
5368 unsigned long *bmask;
5369 struct lpfc_rsrc_blks *rsrc_blks;
5370 LPFC_MBOXQ_t *mbox;
5371 uint32_t length;
5372 struct lpfc_id_range *id_array = NULL;
5373 void *virtaddr = NULL;
5374 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5375 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5376 struct list_head *ext_blk_list;
5377
5378 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5379 &rsrc_cnt,
5380 &rsrc_size);
5381 if (unlikely(rc))
5382 return -EIO;
5383
5384 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5385 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5386 "3009 No available Resource Extents "
5387 "for resource type 0x%x: Count: 0x%x, "
5388 "Size 0x%x\n", type, rsrc_cnt,
5389 rsrc_size);
5390 return -ENOMEM;
5391 }
5392
5393 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5394 "2903 Post resource extents type-0x%x: "
5395 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5396
5397 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5398 if (!mbox)
5399 return -ENOMEM;
5400
5401 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5402 if (unlikely(rc)) {
5403 rc = -EIO;
5404 goto err_exit;
5405 }
5406
5407 /*
5408 * Figure out where the response is located. Then get local pointers
5409 * to the response data. The port does not guarantee to respond to
5410 * all extents counts request so update the local variable with the
5411 * allocated count from the port.
5412 */
5413 if (emb == LPFC_SLI4_MBX_EMBED) {
5414 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5415 id_array = &rsrc_ext->u.rsp.id[0];
5416 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5417 } else {
5418 virtaddr = mbox->sge_array->addr[0];
5419 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5420 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5421 id_array = &n_rsrc->id;
5422 }
5423
5424 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5425 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5426
5427 /*
5428 * Based on the resource size and count, correct the base and max
5429 * resource values.
5430 */
5431 length = sizeof(struct lpfc_rsrc_blks);
5432 switch (type) {
5433 case LPFC_RSC_TYPE_FCOE_RPI:
5434 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5435 sizeof(unsigned long),
5436 GFP_KERNEL);
5437 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5438 rc = -ENOMEM;
5439 goto err_exit;
5440 }
5441 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5442 sizeof(uint16_t),
5443 GFP_KERNEL);
5444 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5445 kfree(phba->sli4_hba.rpi_bmask);
5446 rc = -ENOMEM;
5447 goto err_exit;
5448 }
5449
5450 /*
5451 * The next_rpi was initialized with the maximum available
5452 * count but the port may allocate a smaller number. Catch
5453 * that case and update the next_rpi.
5454 */
5455 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5456
5457 /* Initialize local ptrs for common extent processing later. */
5458 bmask = phba->sli4_hba.rpi_bmask;
5459 ids = phba->sli4_hba.rpi_ids;
5460 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5461 break;
5462 case LPFC_RSC_TYPE_FCOE_VPI:
5463 phba->vpi_bmask = kzalloc(longs *
5464 sizeof(unsigned long),
5465 GFP_KERNEL);
5466 if (unlikely(!phba->vpi_bmask)) {
5467 rc = -ENOMEM;
5468 goto err_exit;
5469 }
5470 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5471 sizeof(uint16_t),
5472 GFP_KERNEL);
5473 if (unlikely(!phba->vpi_ids)) {
5474 kfree(phba->vpi_bmask);
5475 rc = -ENOMEM;
5476 goto err_exit;
5477 }
5478
5479 /* Initialize local ptrs for common extent processing later. */
5480 bmask = phba->vpi_bmask;
5481 ids = phba->vpi_ids;
5482 ext_blk_list = &phba->lpfc_vpi_blk_list;
5483 break;
5484 case LPFC_RSC_TYPE_FCOE_XRI:
5485 phba->sli4_hba.xri_bmask = kzalloc(longs *
5486 sizeof(unsigned long),
5487 GFP_KERNEL);
5488 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5489 rc = -ENOMEM;
5490 goto err_exit;
5491 }
5492 phba->sli4_hba.max_cfg_param.xri_used = 0;
5493 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5494 sizeof(uint16_t),
5495 GFP_KERNEL);
5496 if (unlikely(!phba->sli4_hba.xri_ids)) {
5497 kfree(phba->sli4_hba.xri_bmask);
5498 rc = -ENOMEM;
5499 goto err_exit;
5500 }
5501
5502 /* Initialize local ptrs for common extent processing later. */
5503 bmask = phba->sli4_hba.xri_bmask;
5504 ids = phba->sli4_hba.xri_ids;
5505 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5506 break;
5507 case LPFC_RSC_TYPE_FCOE_VFI:
5508 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5509 sizeof(unsigned long),
5510 GFP_KERNEL);
5511 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5512 rc = -ENOMEM;
5513 goto err_exit;
5514 }
5515 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5516 sizeof(uint16_t),
5517 GFP_KERNEL);
5518 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5519 kfree(phba->sli4_hba.vfi_bmask);
5520 rc = -ENOMEM;
5521 goto err_exit;
5522 }
5523
5524 /* Initialize local ptrs for common extent processing later. */
5525 bmask = phba->sli4_hba.vfi_bmask;
5526 ids = phba->sli4_hba.vfi_ids;
5527 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5528 break;
5529 default:
5530 /* Unsupported Opcode. Fail call. */
5531 id_array = NULL;
5532 bmask = NULL;
5533 ids = NULL;
5534 ext_blk_list = NULL;
5535 goto err_exit;
5536 }
5537
5538 /*
5539 * Complete initializing the extent configuration with the
5540 * allocated ids assigned to this function. The bitmask serves
5541 * as an index into the array and manages the available ids. The
5542 * array just stores the ids communicated to the port via the wqes.
5543 */
5544 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5545 if ((i % 2) == 0)
5546 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5547 &id_array[k]);
5548 else
5549 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5550 &id_array[k]);
5551
5552 rsrc_blks = kzalloc(length, GFP_KERNEL);
5553 if (unlikely(!rsrc_blks)) {
5554 rc = -ENOMEM;
5555 kfree(bmask);
5556 kfree(ids);
5557 goto err_exit;
5558 }
5559 rsrc_blks->rsrc_start = rsrc_id;
5560 rsrc_blks->rsrc_size = rsrc_size;
5561 list_add_tail(&rsrc_blks->list, ext_blk_list);
5562 rsrc_start = rsrc_id;
5563 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5564 phba->sli4_hba.scsi_xri_start = rsrc_start +
5565 lpfc_sli4_get_els_iocb_cnt(phba);
5566
5567 while (rsrc_id < (rsrc_start + rsrc_size)) {
5568 ids[j] = rsrc_id;
5569 rsrc_id++;
5570 j++;
5571 }
5572 /* Entire word processed. Get next word.*/
5573 if ((i % 2) == 1)
5574 k++;
5575 }
5576 err_exit:
5577 lpfc_sli4_mbox_cmd_free(phba, mbox);
5578 return rc;
5579 }
5580
5581 /**
5582 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5583 * @phba: Pointer to HBA context object.
5584 * @type: the extent's type.
5585 *
5586 * This function deallocates all extents of a particular resource type.
5587 * SLI4 does not allow for deallocating a particular extent range. It
5588 * is the caller's responsibility to release all kernel memory resources.
5589 **/
5590 static int
5591 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5592 {
5593 int rc;
5594 uint32_t length, mbox_tmo = 0;
5595 LPFC_MBOXQ_t *mbox;
5596 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5597 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5598
5599 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5600 if (!mbox)
5601 return -ENOMEM;
5602
5603 /*
5604 * This function sends an embedded mailbox because it only sends the
5605 * the resource type. All extents of this type are released by the
5606 * port.
5607 */
5608 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5609 sizeof(struct lpfc_sli4_cfg_mhdr));
5610 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5611 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5612 length, LPFC_SLI4_MBX_EMBED);
5613
5614 /* Send an extents count of 0 - the dealloc doesn't use it. */
5615 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5616 LPFC_SLI4_MBX_EMBED);
5617 if (unlikely(rc)) {
5618 rc = -EIO;
5619 goto out_free_mbox;
5620 }
5621 if (!phba->sli4_hba.intr_enable)
5622 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5623 else {
5624 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5625 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5626 }
5627 if (unlikely(rc)) {
5628 rc = -EIO;
5629 goto out_free_mbox;
5630 }
5631
5632 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5633 if (bf_get(lpfc_mbox_hdr_status,
5634 &dealloc_rsrc->header.cfg_shdr.response)) {
5635 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5636 "2919 Failed to release resource extents "
5637 "for type %d - Status 0x%x Add'l Status 0x%x. "
5638 "Resource memory not released.\n",
5639 type,
5640 bf_get(lpfc_mbox_hdr_status,
5641 &dealloc_rsrc->header.cfg_shdr.response),
5642 bf_get(lpfc_mbox_hdr_add_status,
5643 &dealloc_rsrc->header.cfg_shdr.response));
5644 rc = -EIO;
5645 goto out_free_mbox;
5646 }
5647
5648 /* Release kernel memory resources for the specific type. */
5649 switch (type) {
5650 case LPFC_RSC_TYPE_FCOE_VPI:
5651 kfree(phba->vpi_bmask);
5652 kfree(phba->vpi_ids);
5653 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5654 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5655 &phba->lpfc_vpi_blk_list, list) {
5656 list_del_init(&rsrc_blk->list);
5657 kfree(rsrc_blk);
5658 }
5659 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5660 break;
5661 case LPFC_RSC_TYPE_FCOE_XRI:
5662 kfree(phba->sli4_hba.xri_bmask);
5663 kfree(phba->sli4_hba.xri_ids);
5664 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5665 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5666 list_del_init(&rsrc_blk->list);
5667 kfree(rsrc_blk);
5668 }
5669 break;
5670 case LPFC_RSC_TYPE_FCOE_VFI:
5671 kfree(phba->sli4_hba.vfi_bmask);
5672 kfree(phba->sli4_hba.vfi_ids);
5673 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5674 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5675 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5676 list_del_init(&rsrc_blk->list);
5677 kfree(rsrc_blk);
5678 }
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_RPI:
5681 /* RPI bitmask and physical id array are cleaned up earlier. */
5682 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5683 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5684 list_del_init(&rsrc_blk->list);
5685 kfree(rsrc_blk);
5686 }
5687 break;
5688 default:
5689 break;
5690 }
5691
5692 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5693
5694 out_free_mbox:
5695 mempool_free(mbox, phba->mbox_mem_pool);
5696 return rc;
5697 }
5698
5699 static void
5700 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5701 uint32_t feature)
5702 {
5703 uint32_t len;
5704
5705 len = sizeof(struct lpfc_mbx_set_feature) -
5706 sizeof(struct lpfc_sli4_cfg_mhdr);
5707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5708 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5709 LPFC_SLI4_MBX_EMBED);
5710
5711 switch (feature) {
5712 case LPFC_SET_UE_RECOVERY:
5713 bf_set(lpfc_mbx_set_feature_UER,
5714 &mbox->u.mqe.un.set_feature, 1);
5715 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5716 mbox->u.mqe.un.set_feature.param_len = 8;
5717 break;
5718 case LPFC_SET_MDS_DIAGS:
5719 bf_set(lpfc_mbx_set_feature_mds,
5720 &mbox->u.mqe.un.set_feature, 1);
5721 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5722 &mbox->u.mqe.un.set_feature, 0);
5723 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5724 mbox->u.mqe.un.set_feature.param_len = 8;
5725 break;
5726 }
5727
5728 return;
5729 }
5730
5731 /**
5732 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5733 * @phba: Pointer to HBA context object.
5734 *
5735 * This function allocates all SLI4 resource identifiers.
5736 **/
5737 int
5738 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5739 {
5740 int i, rc, error = 0;
5741 uint16_t count, base;
5742 unsigned long longs;
5743
5744 if (!phba->sli4_hba.rpi_hdrs_in_use)
5745 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5746 if (phba->sli4_hba.extents_in_use) {
5747 /*
5748 * The port supports resource extents. The XRI, VPI, VFI, RPI
5749 * resource extent count must be read and allocated before
5750 * provisioning the resource id arrays.
5751 */
5752 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5753 LPFC_IDX_RSRC_RDY) {
5754 /*
5755 * Extent-based resources are set - the driver could
5756 * be in a port reset. Figure out if any corrective
5757 * actions need to be taken.
5758 */
5759 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5760 LPFC_RSC_TYPE_FCOE_VFI);
5761 if (rc != 0)
5762 error++;
5763 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5764 LPFC_RSC_TYPE_FCOE_VPI);
5765 if (rc != 0)
5766 error++;
5767 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5768 LPFC_RSC_TYPE_FCOE_XRI);
5769 if (rc != 0)
5770 error++;
5771 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5772 LPFC_RSC_TYPE_FCOE_RPI);
5773 if (rc != 0)
5774 error++;
5775
5776 /*
5777 * It's possible that the number of resources
5778 * provided to this port instance changed between
5779 * resets. Detect this condition and reallocate
5780 * resources. Otherwise, there is no action.
5781 */
5782 if (error) {
5783 lpfc_printf_log(phba, KERN_INFO,
5784 LOG_MBOX | LOG_INIT,
5785 "2931 Detected extent resource "
5786 "change. Reallocating all "
5787 "extents.\n");
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_VFI);
5790 rc = lpfc_sli4_dealloc_extent(phba,
5791 LPFC_RSC_TYPE_FCOE_VPI);
5792 rc = lpfc_sli4_dealloc_extent(phba,
5793 LPFC_RSC_TYPE_FCOE_XRI);
5794 rc = lpfc_sli4_dealloc_extent(phba,
5795 LPFC_RSC_TYPE_FCOE_RPI);
5796 } else
5797 return 0;
5798 }
5799
5800 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5801 if (unlikely(rc))
5802 goto err_exit;
5803
5804 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5805 if (unlikely(rc))
5806 goto err_exit;
5807
5808 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5809 if (unlikely(rc))
5810 goto err_exit;
5811
5812 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5813 if (unlikely(rc))
5814 goto err_exit;
5815 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5816 LPFC_IDX_RSRC_RDY);
5817 return rc;
5818 } else {
5819 /*
5820 * The port does not support resource extents. The XRI, VPI,
5821 * VFI, RPI resource ids were determined from READ_CONFIG.
5822 * Just allocate the bitmasks and provision the resource id
5823 * arrays. If a port reset is active, the resources don't
5824 * need any action - just exit.
5825 */
5826 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5827 LPFC_IDX_RSRC_RDY) {
5828 lpfc_sli4_dealloc_resource_identifiers(phba);
5829 lpfc_sli4_remove_rpis(phba);
5830 }
5831 /* RPIs. */
5832 count = phba->sli4_hba.max_cfg_param.max_rpi;
5833 if (count <= 0) {
5834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5835 "3279 Invalid provisioning of "
5836 "rpi:%d\n", count);
5837 rc = -EINVAL;
5838 goto err_exit;
5839 }
5840 base = phba->sli4_hba.max_cfg_param.rpi_base;
5841 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5842 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5843 sizeof(unsigned long),
5844 GFP_KERNEL);
5845 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5846 rc = -ENOMEM;
5847 goto err_exit;
5848 }
5849 phba->sli4_hba.rpi_ids = kzalloc(count *
5850 sizeof(uint16_t),
5851 GFP_KERNEL);
5852 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5853 rc = -ENOMEM;
5854 goto free_rpi_bmask;
5855 }
5856
5857 for (i = 0; i < count; i++)
5858 phba->sli4_hba.rpi_ids[i] = base + i;
5859
5860 /* VPIs. */
5861 count = phba->sli4_hba.max_cfg_param.max_vpi;
5862 if (count <= 0) {
5863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5864 "3280 Invalid provisioning of "
5865 "vpi:%d\n", count);
5866 rc = -EINVAL;
5867 goto free_rpi_ids;
5868 }
5869 base = phba->sli4_hba.max_cfg_param.vpi_base;
5870 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5871 phba->vpi_bmask = kzalloc(longs *
5872 sizeof(unsigned long),
5873 GFP_KERNEL);
5874 if (unlikely(!phba->vpi_bmask)) {
5875 rc = -ENOMEM;
5876 goto free_rpi_ids;
5877 }
5878 phba->vpi_ids = kzalloc(count *
5879 sizeof(uint16_t),
5880 GFP_KERNEL);
5881 if (unlikely(!phba->vpi_ids)) {
5882 rc = -ENOMEM;
5883 goto free_vpi_bmask;
5884 }
5885
5886 for (i = 0; i < count; i++)
5887 phba->vpi_ids[i] = base + i;
5888
5889 /* XRIs. */
5890 count = phba->sli4_hba.max_cfg_param.max_xri;
5891 if (count <= 0) {
5892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5893 "3281 Invalid provisioning of "
5894 "xri:%d\n", count);
5895 rc = -EINVAL;
5896 goto free_vpi_ids;
5897 }
5898 base = phba->sli4_hba.max_cfg_param.xri_base;
5899 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5900 phba->sli4_hba.xri_bmask = kzalloc(longs *
5901 sizeof(unsigned long),
5902 GFP_KERNEL);
5903 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5904 rc = -ENOMEM;
5905 goto free_vpi_ids;
5906 }
5907 phba->sli4_hba.max_cfg_param.xri_used = 0;
5908 phba->sli4_hba.xri_ids = kzalloc(count *
5909 sizeof(uint16_t),
5910 GFP_KERNEL);
5911 if (unlikely(!phba->sli4_hba.xri_ids)) {
5912 rc = -ENOMEM;
5913 goto free_xri_bmask;
5914 }
5915
5916 for (i = 0; i < count; i++)
5917 phba->sli4_hba.xri_ids[i] = base + i;
5918
5919 /* VFIs. */
5920 count = phba->sli4_hba.max_cfg_param.max_vfi;
5921 if (count <= 0) {
5922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5923 "3282 Invalid provisioning of "
5924 "vfi:%d\n", count);
5925 rc = -EINVAL;
5926 goto free_xri_ids;
5927 }
5928 base = phba->sli4_hba.max_cfg_param.vfi_base;
5929 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5930 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5931 sizeof(unsigned long),
5932 GFP_KERNEL);
5933 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5934 rc = -ENOMEM;
5935 goto free_xri_ids;
5936 }
5937 phba->sli4_hba.vfi_ids = kzalloc(count *
5938 sizeof(uint16_t),
5939 GFP_KERNEL);
5940 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5941 rc = -ENOMEM;
5942 goto free_vfi_bmask;
5943 }
5944
5945 for (i = 0; i < count; i++)
5946 phba->sli4_hba.vfi_ids[i] = base + i;
5947
5948 /*
5949 * Mark all resources ready. An HBA reset doesn't need
5950 * to reset the initialization.
5951 */
5952 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5953 LPFC_IDX_RSRC_RDY);
5954 return 0;
5955 }
5956
5957 free_vfi_bmask:
5958 kfree(phba->sli4_hba.vfi_bmask);
5959 phba->sli4_hba.vfi_bmask = NULL;
5960 free_xri_ids:
5961 kfree(phba->sli4_hba.xri_ids);
5962 phba->sli4_hba.xri_ids = NULL;
5963 free_xri_bmask:
5964 kfree(phba->sli4_hba.xri_bmask);
5965 phba->sli4_hba.xri_bmask = NULL;
5966 free_vpi_ids:
5967 kfree(phba->vpi_ids);
5968 phba->vpi_ids = NULL;
5969 free_vpi_bmask:
5970 kfree(phba->vpi_bmask);
5971 phba->vpi_bmask = NULL;
5972 free_rpi_ids:
5973 kfree(phba->sli4_hba.rpi_ids);
5974 phba->sli4_hba.rpi_ids = NULL;
5975 free_rpi_bmask:
5976 kfree(phba->sli4_hba.rpi_bmask);
5977 phba->sli4_hba.rpi_bmask = NULL;
5978 err_exit:
5979 return rc;
5980 }
5981
5982 /**
5983 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5984 * @phba: Pointer to HBA context object.
5985 *
5986 * This function allocates the number of elements for the specified
5987 * resource type.
5988 **/
5989 int
5990 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5991 {
5992 if (phba->sli4_hba.extents_in_use) {
5993 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5994 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5995 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5996 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5997 } else {
5998 kfree(phba->vpi_bmask);
5999 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6000 kfree(phba->vpi_ids);
6001 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6002 kfree(phba->sli4_hba.xri_bmask);
6003 kfree(phba->sli4_hba.xri_ids);
6004 kfree(phba->sli4_hba.vfi_bmask);
6005 kfree(phba->sli4_hba.vfi_ids);
6006 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6007 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6008 }
6009
6010 return 0;
6011 }
6012
6013 /**
6014 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6015 * @phba: Pointer to HBA context object.
6016 * @type: The resource extent type.
6017 * @extnt_count: buffer to hold port extent count response
6018 * @extnt_size: buffer to hold port extent size response.
6019 *
6020 * This function calls the port to read the host allocated extents
6021 * for a particular type.
6022 **/
6023 int
6024 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6025 uint16_t *extnt_cnt, uint16_t *extnt_size)
6026 {
6027 bool emb;
6028 int rc = 0;
6029 uint16_t curr_blks = 0;
6030 uint32_t req_len, emb_len;
6031 uint32_t alloc_len, mbox_tmo;
6032 struct list_head *blk_list_head;
6033 struct lpfc_rsrc_blks *rsrc_blk;
6034 LPFC_MBOXQ_t *mbox;
6035 void *virtaddr = NULL;
6036 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6037 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6038 union lpfc_sli4_cfg_shdr *shdr;
6039
6040 switch (type) {
6041 case LPFC_RSC_TYPE_FCOE_VPI:
6042 blk_list_head = &phba->lpfc_vpi_blk_list;
6043 break;
6044 case LPFC_RSC_TYPE_FCOE_XRI:
6045 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6046 break;
6047 case LPFC_RSC_TYPE_FCOE_VFI:
6048 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6049 break;
6050 case LPFC_RSC_TYPE_FCOE_RPI:
6051 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6052 break;
6053 default:
6054 return -EIO;
6055 }
6056
6057 /* Count the number of extents currently allocatd for this type. */
6058 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6059 if (curr_blks == 0) {
6060 /*
6061 * The GET_ALLOCATED mailbox does not return the size,
6062 * just the count. The size should be just the size
6063 * stored in the current allocated block and all sizes
6064 * for an extent type are the same so set the return
6065 * value now.
6066 */
6067 *extnt_size = rsrc_blk->rsrc_size;
6068 }
6069 curr_blks++;
6070 }
6071
6072 /*
6073 * Calculate the size of an embedded mailbox. The uint32_t
6074 * accounts for extents-specific word.
6075 */
6076 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6077 sizeof(uint32_t);
6078
6079 /*
6080 * Presume the allocation and response will fit into an embedded
6081 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6082 */
6083 emb = LPFC_SLI4_MBX_EMBED;
6084 req_len = emb_len;
6085 if (req_len > emb_len) {
6086 req_len = curr_blks * sizeof(uint16_t) +
6087 sizeof(union lpfc_sli4_cfg_shdr) +
6088 sizeof(uint32_t);
6089 emb = LPFC_SLI4_MBX_NEMBED;
6090 }
6091
6092 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6093 if (!mbox)
6094 return -ENOMEM;
6095 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6096
6097 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6098 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6099 req_len, emb);
6100 if (alloc_len < req_len) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102 "2983 Allocated DMA memory size (x%x) is "
6103 "less than the requested DMA memory "
6104 "size (x%x)\n", alloc_len, req_len);
6105 rc = -ENOMEM;
6106 goto err_exit;
6107 }
6108 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6109 if (unlikely(rc)) {
6110 rc = -EIO;
6111 goto err_exit;
6112 }
6113
6114 if (!phba->sli4_hba.intr_enable)
6115 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6116 else {
6117 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6118 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6119 }
6120
6121 if (unlikely(rc)) {
6122 rc = -EIO;
6123 goto err_exit;
6124 }
6125
6126 /*
6127 * Figure out where the response is located. Then get local pointers
6128 * to the response data. The port does not guarantee to respond to
6129 * all extents counts request so update the local variable with the
6130 * allocated count from the port.
6131 */
6132 if (emb == LPFC_SLI4_MBX_EMBED) {
6133 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6134 shdr = &rsrc_ext->header.cfg_shdr;
6135 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6136 } else {
6137 virtaddr = mbox->sge_array->addr[0];
6138 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6139 shdr = &n_rsrc->cfg_shdr;
6140 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6141 }
6142
6143 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6145 "2984 Failed to read allocated resources "
6146 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6147 type,
6148 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6149 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6150 rc = -EIO;
6151 goto err_exit;
6152 }
6153 err_exit:
6154 lpfc_sli4_mbox_cmd_free(phba, mbox);
6155 return rc;
6156 }
6157
6158 /**
6159 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6160 * @phba: pointer to lpfc hba data structure.
6161 *
6162 * This routine walks the list of els buffers that have been allocated and
6163 * repost them to the port by using SGL block post. This is needed after a
6164 * pci_function_reset/warm_start or start. It attempts to construct blocks
6165 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6166 * SGL block post mailbox commands to post them to the port. For single els
6167 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6168 * mailbox command for posting.
6169 *
6170 * Returns: 0 = success, non-zero failure.
6171 **/
6172 static int
6173 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6174 {
6175 struct lpfc_sglq *sglq_entry = NULL;
6176 struct lpfc_sglq *sglq_entry_next = NULL;
6177 struct lpfc_sglq *sglq_entry_first = NULL;
6178 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
6179 int last_xritag = NO_XRI;
6180 struct lpfc_sli_ring *pring;
6181 LIST_HEAD(prep_sgl_list);
6182 LIST_HEAD(blck_sgl_list);
6183 LIST_HEAD(allc_sgl_list);
6184 LIST_HEAD(post_sgl_list);
6185 LIST_HEAD(free_sgl_list);
6186
6187 pring = &phba->sli.ring[LPFC_ELS_RING];
6188 spin_lock_irq(&phba->hbalock);
6189 spin_lock(&pring->ring_lock);
6190 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6191 spin_unlock(&pring->ring_lock);
6192 spin_unlock_irq(&phba->hbalock);
6193
6194 total_cnt = phba->sli4_hba.els_xri_cnt;
6195 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6196 &allc_sgl_list, list) {
6197 list_del_init(&sglq_entry->list);
6198 block_cnt++;
6199 if ((last_xritag != NO_XRI) &&
6200 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6201 /* a hole in xri block, form a sgl posting block */
6202 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6203 post_cnt = block_cnt - 1;
6204 /* prepare list for next posting block */
6205 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6206 block_cnt = 1;
6207 } else {
6208 /* prepare list for next posting block */
6209 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6210 /* enough sgls for non-embed sgl mbox command */
6211 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6212 list_splice_init(&prep_sgl_list,
6213 &blck_sgl_list);
6214 post_cnt = block_cnt;
6215 block_cnt = 0;
6216 }
6217 }
6218 num_posted++;
6219
6220 /* keep track of last sgl's xritag */
6221 last_xritag = sglq_entry->sli4_xritag;
6222
6223 /* end of repost sgl list condition for els buffers */
6224 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6225 if (post_cnt == 0) {
6226 list_splice_init(&prep_sgl_list,
6227 &blck_sgl_list);
6228 post_cnt = block_cnt;
6229 } else if (block_cnt == 1) {
6230 status = lpfc_sli4_post_sgl(phba,
6231 sglq_entry->phys, 0,
6232 sglq_entry->sli4_xritag);
6233 if (!status) {
6234 /* successful, put sgl to posted list */
6235 list_add_tail(&sglq_entry->list,
6236 &post_sgl_list);
6237 } else {
6238 /* Failure, put sgl to free list */
6239 lpfc_printf_log(phba, KERN_WARNING,
6240 LOG_SLI,
6241 "3159 Failed to post els "
6242 "sgl, xritag:x%x\n",
6243 sglq_entry->sli4_xritag);
6244 list_add_tail(&sglq_entry->list,
6245 &free_sgl_list);
6246 total_cnt--;
6247 }
6248 }
6249 }
6250
6251 /* continue until a nembed page worth of sgls */
6252 if (post_cnt == 0)
6253 continue;
6254
6255 /* post the els buffer list sgls as a block */
6256 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6257 post_cnt);
6258
6259 if (!status) {
6260 /* success, put sgl list to posted sgl list */
6261 list_splice_init(&blck_sgl_list, &post_sgl_list);
6262 } else {
6263 /* Failure, put sgl list to free sgl list */
6264 sglq_entry_first = list_first_entry(&blck_sgl_list,
6265 struct lpfc_sglq,
6266 list);
6267 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6268 "3160 Failed to post els sgl-list, "
6269 "xritag:x%x-x%x\n",
6270 sglq_entry_first->sli4_xritag,
6271 (sglq_entry_first->sli4_xritag +
6272 post_cnt - 1));
6273 list_splice_init(&blck_sgl_list, &free_sgl_list);
6274 total_cnt -= post_cnt;
6275 }
6276
6277 /* don't reset xirtag due to hole in xri block */
6278 if (block_cnt == 0)
6279 last_xritag = NO_XRI;
6280
6281 /* reset els sgl post count for next round of posting */
6282 post_cnt = 0;
6283 }
6284 /* update the number of XRIs posted for ELS */
6285 phba->sli4_hba.els_xri_cnt = total_cnt;
6286
6287 /* free the els sgls failed to post */
6288 lpfc_free_sgl_list(phba, &free_sgl_list);
6289
6290 /* push els sgls posted to the availble list */
6291 if (!list_empty(&post_sgl_list)) {
6292 spin_lock_irq(&phba->hbalock);
6293 spin_lock(&pring->ring_lock);
6294 list_splice_init(&post_sgl_list,
6295 &phba->sli4_hba.lpfc_sgl_list);
6296 spin_unlock(&pring->ring_lock);
6297 spin_unlock_irq(&phba->hbalock);
6298 } else {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6300 "3161 Failure to post els sgl to port.\n");
6301 return -EIO;
6302 }
6303 return 0;
6304 }
6305
6306 void
6307 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6308 {
6309 uint32_t len;
6310
6311 len = sizeof(struct lpfc_mbx_set_host_data) -
6312 sizeof(struct lpfc_sli4_cfg_mhdr);
6313 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6314 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6315 LPFC_SLI4_MBX_EMBED);
6316
6317 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6318 mbox->u.mqe.un.set_host_data.param_len = 8;
6319 snprintf(mbox->u.mqe.un.set_host_data.data,
6320 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6321 "Linux %s v"LPFC_DRIVER_VERSION,
6322 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6323 }
6324
6325 /**
6326 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6327 * @phba: Pointer to HBA context object.
6328 *
6329 * This function is the main SLI4 device intialization PCI function. This
6330 * function is called by the HBA intialization code, HBA reset code and
6331 * HBA error attention handler code. Caller is not required to hold any
6332 * locks.
6333 **/
6334 int
6335 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6336 {
6337 int rc;
6338 LPFC_MBOXQ_t *mboxq;
6339 struct lpfc_mqe *mqe;
6340 uint8_t *vpd;
6341 uint32_t vpd_size;
6342 uint32_t ftr_rsp = 0;
6343 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6344 struct lpfc_vport *vport = phba->pport;
6345 struct lpfc_dmabuf *mp;
6346
6347 /* Perform a PCI function reset to start from clean */
6348 rc = lpfc_pci_function_reset(phba);
6349 if (unlikely(rc))
6350 return -ENODEV;
6351
6352 /* Check the HBA Host Status Register for readyness */
6353 rc = lpfc_sli4_post_status_check(phba);
6354 if (unlikely(rc))
6355 return -ENODEV;
6356 else {
6357 spin_lock_irq(&phba->hbalock);
6358 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6359 spin_unlock_irq(&phba->hbalock);
6360 }
6361
6362 /*
6363 * Allocate a single mailbox container for initializing the
6364 * port.
6365 */
6366 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6367 if (!mboxq)
6368 return -ENOMEM;
6369
6370 /* Issue READ_REV to collect vpd and FW information. */
6371 vpd_size = SLI4_PAGE_SIZE;
6372 vpd = kzalloc(vpd_size, GFP_KERNEL);
6373 if (!vpd) {
6374 rc = -ENOMEM;
6375 goto out_free_mbox;
6376 }
6377
6378 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6379 if (unlikely(rc)) {
6380 kfree(vpd);
6381 goto out_free_mbox;
6382 }
6383
6384 mqe = &mboxq->u.mqe;
6385 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6386 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6387 phba->hba_flag |= HBA_FCOE_MODE;
6388 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6389 } else {
6390 phba->hba_flag &= ~HBA_FCOE_MODE;
6391 }
6392
6393 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6394 LPFC_DCBX_CEE_MODE)
6395 phba->hba_flag |= HBA_FIP_SUPPORT;
6396 else
6397 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6398
6399 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6400
6401 if (phba->sli_rev != LPFC_SLI_REV4) {
6402 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6403 "0376 READ_REV Error. SLI Level %d "
6404 "FCoE enabled %d\n",
6405 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6406 rc = -EIO;
6407 kfree(vpd);
6408 goto out_free_mbox;
6409 }
6410
6411 /*
6412 * Continue initialization with default values even if driver failed
6413 * to read FCoE param config regions, only read parameters if the
6414 * board is FCoE
6415 */
6416 if (phba->hba_flag & HBA_FCOE_MODE &&
6417 lpfc_sli4_read_fcoe_params(phba))
6418 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6419 "2570 Failed to read FCoE parameters\n");
6420
6421 /*
6422 * Retrieve sli4 device physical port name, failure of doing it
6423 * is considered as non-fatal.
6424 */
6425 rc = lpfc_sli4_retrieve_pport_name(phba);
6426 if (!rc)
6427 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6428 "3080 Successful retrieving SLI4 device "
6429 "physical port name: %s.\n", phba->Port);
6430
6431 /*
6432 * Evaluate the read rev and vpd data. Populate the driver
6433 * state with the results. If this routine fails, the failure
6434 * is not fatal as the driver will use generic values.
6435 */
6436 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6437 if (unlikely(!rc)) {
6438 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6439 "0377 Error %d parsing vpd. "
6440 "Using defaults.\n", rc);
6441 rc = 0;
6442 }
6443 kfree(vpd);
6444
6445 /* Save information as VPD data */
6446 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6447 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6448 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6449 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6450 &mqe->un.read_rev);
6451 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6452 &mqe->un.read_rev);
6453 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6454 &mqe->un.read_rev);
6455 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6456 &mqe->un.read_rev);
6457 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6458 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6459 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6460 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6461 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6462 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6463 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6464 "(%d):0380 READ_REV Status x%x "
6465 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6466 mboxq->vport ? mboxq->vport->vpi : 0,
6467 bf_get(lpfc_mqe_status, mqe),
6468 phba->vpd.rev.opFwName,
6469 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6470 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6471
6472 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6473 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6474 if (phba->pport->cfg_lun_queue_depth > rc) {
6475 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6476 "3362 LUN queue depth changed from %d to %d\n",
6477 phba->pport->cfg_lun_queue_depth, rc);
6478 phba->pport->cfg_lun_queue_depth = rc;
6479 }
6480
6481 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6482 LPFC_SLI_INTF_IF_TYPE_0) {
6483 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6484 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6485 if (rc == MBX_SUCCESS) {
6486 phba->hba_flag |= HBA_RECOVERABLE_UE;
6487 /* Set 1Sec interval to detect UE */
6488 phba->eratt_poll_interval = 1;
6489 phba->sli4_hba.ue_to_sr = bf_get(
6490 lpfc_mbx_set_feature_UESR,
6491 &mboxq->u.mqe.un.set_feature);
6492 phba->sli4_hba.ue_to_rp = bf_get(
6493 lpfc_mbx_set_feature_UERP,
6494 &mboxq->u.mqe.un.set_feature);
6495 }
6496 }
6497
6498 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6499 /* Enable MDS Diagnostics only if the SLI Port supports it */
6500 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6501 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6502 if (rc != MBX_SUCCESS)
6503 phba->mds_diags_support = 0;
6504 }
6505
6506 /*
6507 * Discover the port's supported feature set and match it against the
6508 * hosts requests.
6509 */
6510 lpfc_request_features(phba, mboxq);
6511 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6512 if (unlikely(rc)) {
6513 rc = -EIO;
6514 goto out_free_mbox;
6515 }
6516
6517 /*
6518 * The port must support FCP initiator mode as this is the
6519 * only mode running in the host.
6520 */
6521 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6522 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6523 "0378 No support for fcpi mode.\n");
6524 ftr_rsp++;
6525 }
6526 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6527 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6528 else
6529 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6530 /*
6531 * If the port cannot support the host's requested features
6532 * then turn off the global config parameters to disable the
6533 * feature in the driver. This is not a fatal error.
6534 */
6535 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6536 if (phba->cfg_enable_bg) {
6537 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6538 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6539 else
6540 ftr_rsp++;
6541 }
6542
6543 if (phba->max_vpi && phba->cfg_enable_npiv &&
6544 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6545 ftr_rsp++;
6546
6547 if (ftr_rsp) {
6548 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6549 "0379 Feature Mismatch Data: x%08x %08x "
6550 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6551 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6552 phba->cfg_enable_npiv, phba->max_vpi);
6553 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6554 phba->cfg_enable_bg = 0;
6555 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6556 phba->cfg_enable_npiv = 0;
6557 }
6558
6559 /* These SLI3 features are assumed in SLI4 */
6560 spin_lock_irq(&phba->hbalock);
6561 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6562 spin_unlock_irq(&phba->hbalock);
6563
6564 /*
6565 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6566 * calls depends on these resources to complete port setup.
6567 */
6568 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6569 if (rc) {
6570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6571 "2920 Failed to alloc Resource IDs "
6572 "rc = x%x\n", rc);
6573 goto out_free_mbox;
6574 }
6575
6576 lpfc_set_host_data(phba, mboxq);
6577
6578 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6579 if (rc) {
6580 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6581 "2134 Failed to set host os driver version %x",
6582 rc);
6583 }
6584
6585 /* Read the port's service parameters. */
6586 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6587 if (rc) {
6588 phba->link_state = LPFC_HBA_ERROR;
6589 rc = -ENOMEM;
6590 goto out_free_mbox;
6591 }
6592
6593 mboxq->vport = vport;
6594 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6595 mp = (struct lpfc_dmabuf *) mboxq->context1;
6596 if (rc == MBX_SUCCESS) {
6597 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6598 rc = 0;
6599 }
6600
6601 /*
6602 * This memory was allocated by the lpfc_read_sparam routine. Release
6603 * it to the mbuf pool.
6604 */
6605 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6606 kfree(mp);
6607 mboxq->context1 = NULL;
6608 if (unlikely(rc)) {
6609 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6610 "0382 READ_SPARAM command failed "
6611 "status %d, mbxStatus x%x\n",
6612 rc, bf_get(lpfc_mqe_status, mqe));
6613 phba->link_state = LPFC_HBA_ERROR;
6614 rc = -EIO;
6615 goto out_free_mbox;
6616 }
6617
6618 lpfc_update_vport_wwn(vport);
6619
6620 /* Update the fc_host data structures with new wwn. */
6621 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6622 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6623
6624 /* update host els and scsi xri-sgl sizes and mappings */
6625 rc = lpfc_sli4_xri_sgl_update(phba);
6626 if (unlikely(rc)) {
6627 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6628 "1400 Failed to update xri-sgl size and "
6629 "mapping: %d\n", rc);
6630 goto out_free_mbox;
6631 }
6632
6633 /* register the els sgl pool to the port */
6634 rc = lpfc_sli4_repost_els_sgl_list(phba);
6635 if (unlikely(rc)) {
6636 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6637 "0582 Error %d during els sgl post "
6638 "operation\n", rc);
6639 rc = -ENODEV;
6640 goto out_free_mbox;
6641 }
6642
6643 /* register the allocated scsi sgl pool to the port */
6644 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6645 if (unlikely(rc)) {
6646 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6647 "0383 Error %d during scsi sgl post "
6648 "operation\n", rc);
6649 /* Some Scsi buffers were moved to the abort scsi list */
6650 /* A pci function reset will repost them */
6651 rc = -ENODEV;
6652 goto out_free_mbox;
6653 }
6654
6655 /* Post the rpi header region to the device. */
6656 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6657 if (unlikely(rc)) {
6658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6659 "0393 Error %d during rpi post operation\n",
6660 rc);
6661 rc = -ENODEV;
6662 goto out_free_mbox;
6663 }
6664 lpfc_sli4_node_prep(phba);
6665
6666 /* Create all the SLI4 queues */
6667 rc = lpfc_sli4_queue_create(phba);
6668 if (rc) {
6669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6670 "3089 Failed to allocate queues\n");
6671 rc = -ENODEV;
6672 goto out_stop_timers;
6673 }
6674 /* Set up all the queues to the device */
6675 rc = lpfc_sli4_queue_setup(phba);
6676 if (unlikely(rc)) {
6677 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6678 "0381 Error %d during queue setup.\n ", rc);
6679 goto out_destroy_queue;
6680 }
6681
6682 /* Arm the CQs and then EQs on device */
6683 lpfc_sli4_arm_cqeq_intr(phba);
6684
6685 /* Indicate device interrupt mode */
6686 phba->sli4_hba.intr_enable = 1;
6687
6688 /* Allow asynchronous mailbox command to go through */
6689 spin_lock_irq(&phba->hbalock);
6690 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6691 spin_unlock_irq(&phba->hbalock);
6692
6693 /* Post receive buffers to the device */
6694 lpfc_sli4_rb_setup(phba);
6695
6696 /* Reset HBA FCF states after HBA reset */
6697 phba->fcf.fcf_flag = 0;
6698 phba->fcf.current_rec.flag = 0;
6699
6700 /* Start the ELS watchdog timer */
6701 mod_timer(&vport->els_tmofunc,
6702 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
6703
6704 /* Start heart beat timer */
6705 mod_timer(&phba->hb_tmofunc,
6706 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
6707 phba->hb_outstanding = 0;
6708 phba->last_completion_time = jiffies;
6709
6710 /* Start error attention (ERATT) polling timer */
6711 mod_timer(&phba->eratt_poll,
6712 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
6713
6714 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6715 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6716 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6717 if (!rc) {
6718 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6719 "2829 This device supports "
6720 "Advanced Error Reporting (AER)\n");
6721 spin_lock_irq(&phba->hbalock);
6722 phba->hba_flag |= HBA_AER_ENABLED;
6723 spin_unlock_irq(&phba->hbalock);
6724 } else {
6725 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6726 "2830 This device does not support "
6727 "Advanced Error Reporting (AER)\n");
6728 phba->cfg_aer_support = 0;
6729 }
6730 rc = 0;
6731 }
6732
6733 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6734 /*
6735 * The FC Port needs to register FCFI (index 0)
6736 */
6737 lpfc_reg_fcfi(phba, mboxq);
6738 mboxq->vport = phba->pport;
6739 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6740 if (rc != MBX_SUCCESS)
6741 goto out_unset_queue;
6742 rc = 0;
6743 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6744 &mboxq->u.mqe.un.reg_fcfi);
6745
6746 /* Check if the port is configured to be disabled */
6747 lpfc_sli_read_link_ste(phba);
6748 }
6749
6750 /*
6751 * The port is ready, set the host's link state to LINK_DOWN
6752 * in preparation for link interrupts.
6753 */
6754 spin_lock_irq(&phba->hbalock);
6755 phba->link_state = LPFC_LINK_DOWN;
6756 spin_unlock_irq(&phba->hbalock);
6757 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6758 (phba->hba_flag & LINK_DISABLED)) {
6759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6760 "3103 Adapter Link is disabled.\n");
6761 lpfc_down_link(phba, mboxq);
6762 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6763 if (rc != MBX_SUCCESS) {
6764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6765 "3104 Adapter failed to issue "
6766 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6767 goto out_unset_queue;
6768 }
6769 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6770 /* don't perform init_link on SLI4 FC port loopback test */
6771 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6772 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6773 if (rc)
6774 goto out_unset_queue;
6775 }
6776 }
6777 mempool_free(mboxq, phba->mbox_mem_pool);
6778 return rc;
6779 out_unset_queue:
6780 /* Unset all the queues set up in this routine when error out */
6781 lpfc_sli4_queue_unset(phba);
6782 out_destroy_queue:
6783 lpfc_sli4_queue_destroy(phba);
6784 out_stop_timers:
6785 lpfc_stop_hba_timers(phba);
6786 out_free_mbox:
6787 mempool_free(mboxq, phba->mbox_mem_pool);
6788 return rc;
6789 }
6790
6791 /**
6792 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6793 * @ptr: context object - pointer to hba structure.
6794 *
6795 * This is the callback function for mailbox timer. The mailbox
6796 * timer is armed when a new mailbox command is issued and the timer
6797 * is deleted when the mailbox complete. The function is called by
6798 * the kernel timer code when a mailbox does not complete within
6799 * expected time. This function wakes up the worker thread to
6800 * process the mailbox timeout and returns. All the processing is
6801 * done by the worker thread function lpfc_mbox_timeout_handler.
6802 **/
6803 void
6804 lpfc_mbox_timeout(unsigned long ptr)
6805 {
6806 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6807 unsigned long iflag;
6808 uint32_t tmo_posted;
6809
6810 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6811 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6812 if (!tmo_posted)
6813 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6814 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6815
6816 if (!tmo_posted)
6817 lpfc_worker_wake_up(phba);
6818 return;
6819 }
6820
6821 /**
6822 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6823 * are pending
6824 * @phba: Pointer to HBA context object.
6825 *
6826 * This function checks if any mailbox completions are present on the mailbox
6827 * completion queue.
6828 **/
6829 static bool
6830 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6831 {
6832
6833 uint32_t idx;
6834 struct lpfc_queue *mcq;
6835 struct lpfc_mcqe *mcqe;
6836 bool pending_completions = false;
6837
6838 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6839 return false;
6840
6841 /* Check for completions on mailbox completion queue */
6842
6843 mcq = phba->sli4_hba.mbx_cq;
6844 idx = mcq->hba_index;
6845 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6846 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6847 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6848 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6849 pending_completions = true;
6850 break;
6851 }
6852 idx = (idx + 1) % mcq->entry_count;
6853 if (mcq->hba_index == idx)
6854 break;
6855 }
6856 return pending_completions;
6857
6858 }
6859
6860 /**
6861 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6862 * that were missed.
6863 * @phba: Pointer to HBA context object.
6864 *
6865 * For sli4, it is possible to miss an interrupt. As such mbox completions
6866 * maybe missed causing erroneous mailbox timeouts to occur. This function
6867 * checks to see if mbox completions are on the mailbox completion queue
6868 * and will process all the completions associated with the eq for the
6869 * mailbox completion queue.
6870 **/
6871 bool
6872 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6873 {
6874
6875 uint32_t eqidx;
6876 struct lpfc_queue *fpeq = NULL;
6877 struct lpfc_eqe *eqe;
6878 bool mbox_pending;
6879
6880 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6881 return false;
6882
6883 /* Find the eq associated with the mcq */
6884
6885 if (phba->sli4_hba.hba_eq)
6886 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6887 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6888 phba->sli4_hba.mbx_cq->assoc_qid) {
6889 fpeq = phba->sli4_hba.hba_eq[eqidx];
6890 break;
6891 }
6892 if (!fpeq)
6893 return false;
6894
6895 /* Turn off interrupts from this EQ */
6896
6897 lpfc_sli4_eq_clr_intr(fpeq);
6898
6899 /* Check to see if a mbox completion is pending */
6900
6901 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6902
6903 /*
6904 * If a mbox completion is pending, process all the events on EQ
6905 * associated with the mbox completion queue (this could include
6906 * mailbox commands, async events, els commands, receive queue data
6907 * and fcp commands)
6908 */
6909
6910 if (mbox_pending)
6911 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6912 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6913 fpeq->EQ_processed++;
6914 }
6915
6916 /* Always clear and re-arm the EQ */
6917
6918 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6919
6920 return mbox_pending;
6921
6922 }
6923
6924 /**
6925 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6926 * @phba: Pointer to HBA context object.
6927 *
6928 * This function is called from worker thread when a mailbox command times out.
6929 * The caller is not required to hold any locks. This function will reset the
6930 * HBA and recover all the pending commands.
6931 **/
6932 void
6933 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6934 {
6935 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6936 MAILBOX_t *mb = NULL;
6937
6938 struct lpfc_sli *psli = &phba->sli;
6939
6940 /* If the mailbox completed, process the completion and return */
6941 if (lpfc_sli4_process_missed_mbox_completions(phba))
6942 return;
6943
6944 if (pmbox != NULL)
6945 mb = &pmbox->u.mb;
6946 /* Check the pmbox pointer first. There is a race condition
6947 * between the mbox timeout handler getting executed in the
6948 * worklist and the mailbox actually completing. When this
6949 * race condition occurs, the mbox_active will be NULL.
6950 */
6951 spin_lock_irq(&phba->hbalock);
6952 if (pmbox == NULL) {
6953 lpfc_printf_log(phba, KERN_WARNING,
6954 LOG_MBOX | LOG_SLI,
6955 "0353 Active Mailbox cleared - mailbox timeout "
6956 "exiting\n");
6957 spin_unlock_irq(&phba->hbalock);
6958 return;
6959 }
6960
6961 /* Mbox cmd <mbxCommand> timeout */
6962 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6963 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6964 mb->mbxCommand,
6965 phba->pport->port_state,
6966 phba->sli.sli_flag,
6967 phba->sli.mbox_active);
6968 spin_unlock_irq(&phba->hbalock);
6969
6970 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6971 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6972 * it to fail all outstanding SCSI IO.
6973 */
6974 spin_lock_irq(&phba->pport->work_port_lock);
6975 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6976 spin_unlock_irq(&phba->pport->work_port_lock);
6977 spin_lock_irq(&phba->hbalock);
6978 phba->link_state = LPFC_LINK_UNKNOWN;
6979 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6980 spin_unlock_irq(&phba->hbalock);
6981
6982 lpfc_sli_abort_fcp_rings(phba);
6983
6984 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6985 "0345 Resetting board due to mailbox timeout\n");
6986
6987 /* Reset the HBA device */
6988 lpfc_reset_hba(phba);
6989 }
6990
6991 /**
6992 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6993 * @phba: Pointer to HBA context object.
6994 * @pmbox: Pointer to mailbox object.
6995 * @flag: Flag indicating how the mailbox need to be processed.
6996 *
6997 * This function is called by discovery code and HBA management code
6998 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6999 * function gets the hbalock to protect the data structures.
7000 * The mailbox command can be submitted in polling mode, in which case
7001 * this function will wait in a polling loop for the completion of the
7002 * mailbox.
7003 * If the mailbox is submitted in no_wait mode (not polling) the
7004 * function will submit the command and returns immediately without waiting
7005 * for the mailbox completion. The no_wait is supported only when HBA
7006 * is in SLI2/SLI3 mode - interrupts are enabled.
7007 * The SLI interface allows only one mailbox pending at a time. If the
7008 * mailbox is issued in polling mode and there is already a mailbox
7009 * pending, then the function will return an error. If the mailbox is issued
7010 * in NO_WAIT mode and there is a mailbox pending already, the function
7011 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7012 * The sli layer owns the mailbox object until the completion of mailbox
7013 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7014 * return codes the caller owns the mailbox command after the return of
7015 * the function.
7016 **/
7017 static int
7018 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7019 uint32_t flag)
7020 {
7021 MAILBOX_t *mbx;
7022 struct lpfc_sli *psli = &phba->sli;
7023 uint32_t status, evtctr;
7024 uint32_t ha_copy, hc_copy;
7025 int i;
7026 unsigned long timeout;
7027 unsigned long drvr_flag = 0;
7028 uint32_t word0, ldata;
7029 void __iomem *to_slim;
7030 int processing_queue = 0;
7031
7032 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7033 if (!pmbox) {
7034 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7035 /* processing mbox queue from intr_handler */
7036 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7037 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7038 return MBX_SUCCESS;
7039 }
7040 processing_queue = 1;
7041 pmbox = lpfc_mbox_get(phba);
7042 if (!pmbox) {
7043 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7044 return MBX_SUCCESS;
7045 }
7046 }
7047
7048 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7049 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7050 if(!pmbox->vport) {
7051 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7052 lpfc_printf_log(phba, KERN_ERR,
7053 LOG_MBOX | LOG_VPORT,
7054 "1806 Mbox x%x failed. No vport\n",
7055 pmbox->u.mb.mbxCommand);
7056 dump_stack();
7057 goto out_not_finished;
7058 }
7059 }
7060
7061 /* If the PCI channel is in offline state, do not post mbox. */
7062 if (unlikely(pci_channel_offline(phba->pcidev))) {
7063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7064 goto out_not_finished;
7065 }
7066
7067 /* If HBA has a deferred error attention, fail the iocb. */
7068 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7069 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7070 goto out_not_finished;
7071 }
7072
7073 psli = &phba->sli;
7074
7075 mbx = &pmbox->u.mb;
7076 status = MBX_SUCCESS;
7077
7078 if (phba->link_state == LPFC_HBA_ERROR) {
7079 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7080
7081 /* Mbox command <mbxCommand> cannot issue */
7082 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7083 "(%d):0311 Mailbox command x%x cannot "
7084 "issue Data: x%x x%x\n",
7085 pmbox->vport ? pmbox->vport->vpi : 0,
7086 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7087 goto out_not_finished;
7088 }
7089
7090 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7091 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7092 !(hc_copy & HC_MBINT_ENA)) {
7093 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7094 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7095 "(%d):2528 Mailbox command x%x cannot "
7096 "issue Data: x%x x%x\n",
7097 pmbox->vport ? pmbox->vport->vpi : 0,
7098 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7099 goto out_not_finished;
7100 }
7101 }
7102
7103 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7104 /* Polling for a mbox command when another one is already active
7105 * is not allowed in SLI. Also, the driver must have established
7106 * SLI2 mode to queue and process multiple mbox commands.
7107 */
7108
7109 if (flag & MBX_POLL) {
7110 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7111
7112 /* Mbox command <mbxCommand> cannot issue */
7113 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7114 "(%d):2529 Mailbox command x%x "
7115 "cannot issue Data: x%x x%x\n",
7116 pmbox->vport ? pmbox->vport->vpi : 0,
7117 pmbox->u.mb.mbxCommand,
7118 psli->sli_flag, flag);
7119 goto out_not_finished;
7120 }
7121
7122 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7123 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7124 /* Mbox command <mbxCommand> cannot issue */
7125 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7126 "(%d):2530 Mailbox command x%x "
7127 "cannot issue Data: x%x x%x\n",
7128 pmbox->vport ? pmbox->vport->vpi : 0,
7129 pmbox->u.mb.mbxCommand,
7130 psli->sli_flag, flag);
7131 goto out_not_finished;
7132 }
7133
7134 /* Another mailbox command is still being processed, queue this
7135 * command to be processed later.
7136 */
7137 lpfc_mbox_put(phba, pmbox);
7138
7139 /* Mbox cmd issue - BUSY */
7140 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7141 "(%d):0308 Mbox cmd issue - BUSY Data: "
7142 "x%x x%x x%x x%x\n",
7143 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7144 mbx->mbxCommand, phba->pport->port_state,
7145 psli->sli_flag, flag);
7146
7147 psli->slistat.mbox_busy++;
7148 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7149
7150 if (pmbox->vport) {
7151 lpfc_debugfs_disc_trc(pmbox->vport,
7152 LPFC_DISC_TRC_MBOX_VPORT,
7153 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
7154 (uint32_t)mbx->mbxCommand,
7155 mbx->un.varWords[0], mbx->un.varWords[1]);
7156 }
7157 else {
7158 lpfc_debugfs_disc_trc(phba->pport,
7159 LPFC_DISC_TRC_MBOX,
7160 "MBOX Bsy: cmd:x%x mb:x%x x%x",
7161 (uint32_t)mbx->mbxCommand,
7162 mbx->un.varWords[0], mbx->un.varWords[1]);
7163 }
7164
7165 return MBX_BUSY;
7166 }
7167
7168 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7169
7170 /* If we are not polling, we MUST be in SLI2 mode */
7171 if (flag != MBX_POLL) {
7172 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7173 (mbx->mbxCommand != MBX_KILL_BOARD)) {
7174 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7175 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7176 /* Mbox command <mbxCommand> cannot issue */
7177 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7178 "(%d):2531 Mailbox command x%x "
7179 "cannot issue Data: x%x x%x\n",
7180 pmbox->vport ? pmbox->vport->vpi : 0,
7181 pmbox->u.mb.mbxCommand,
7182 psli->sli_flag, flag);
7183 goto out_not_finished;
7184 }
7185 /* timeout active mbox command */
7186 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7187 1000);
7188 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7189 }
7190
7191 /* Mailbox cmd <cmd> issue */
7192 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7193 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7194 "x%x\n",
7195 pmbox->vport ? pmbox->vport->vpi : 0,
7196 mbx->mbxCommand, phba->pport->port_state,
7197 psli->sli_flag, flag);
7198
7199 if (mbx->mbxCommand != MBX_HEARTBEAT) {
7200 if (pmbox->vport) {
7201 lpfc_debugfs_disc_trc(pmbox->vport,
7202 LPFC_DISC_TRC_MBOX_VPORT,
7203 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7204 (uint32_t)mbx->mbxCommand,
7205 mbx->un.varWords[0], mbx->un.varWords[1]);
7206 }
7207 else {
7208 lpfc_debugfs_disc_trc(phba->pport,
7209 LPFC_DISC_TRC_MBOX,
7210 "MBOX Send: cmd:x%x mb:x%x x%x",
7211 (uint32_t)mbx->mbxCommand,
7212 mbx->un.varWords[0], mbx->un.varWords[1]);
7213 }
7214 }
7215
7216 psli->slistat.mbox_cmd++;
7217 evtctr = psli->slistat.mbox_event;
7218
7219 /* next set own bit for the adapter and copy over command word */
7220 mbx->mbxOwner = OWN_CHIP;
7221
7222 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7223 /* Populate mbox extension offset word. */
7224 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7225 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7226 = (uint8_t *)phba->mbox_ext
7227 - (uint8_t *)phba->mbox;
7228 }
7229
7230 /* Copy the mailbox extension data */
7231 if (pmbox->in_ext_byte_len && pmbox->context2) {
7232 lpfc_sli_pcimem_bcopy(pmbox->context2,
7233 (uint8_t *)phba->mbox_ext,
7234 pmbox->in_ext_byte_len);
7235 }
7236 /* Copy command data to host SLIM area */
7237 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7238 } else {
7239 /* Populate mbox extension offset word. */
7240 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7241 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7242 = MAILBOX_HBA_EXT_OFFSET;
7243
7244 /* Copy the mailbox extension data */
7245 if (pmbox->in_ext_byte_len && pmbox->context2) {
7246 lpfc_memcpy_to_slim(phba->MBslimaddr +
7247 MAILBOX_HBA_EXT_OFFSET,
7248 pmbox->context2, pmbox->in_ext_byte_len);
7249
7250 }
7251 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7252 /* copy command data into host mbox for cmpl */
7253 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7254 }
7255
7256 /* First copy mbox command data to HBA SLIM, skip past first
7257 word */
7258 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7259 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7260 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7261
7262 /* Next copy over first word, with mbxOwner set */
7263 ldata = *((uint32_t *)mbx);
7264 to_slim = phba->MBslimaddr;
7265 writel(ldata, to_slim);
7266 readl(to_slim); /* flush */
7267
7268 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7269 /* switch over to host mailbox */
7270 psli->sli_flag |= LPFC_SLI_ACTIVE;
7271 }
7272 }
7273
7274 wmb();
7275
7276 switch (flag) {
7277 case MBX_NOWAIT:
7278 /* Set up reference to mailbox command */
7279 psli->mbox_active = pmbox;
7280 /* Interrupt board to do it */
7281 writel(CA_MBATT, phba->CAregaddr);
7282 readl(phba->CAregaddr); /* flush */
7283 /* Don't wait for it to finish, just return */
7284 break;
7285
7286 case MBX_POLL:
7287 /* Set up null reference to mailbox command */
7288 psli->mbox_active = NULL;
7289 /* Interrupt board to do it */
7290 writel(CA_MBATT, phba->CAregaddr);
7291 readl(phba->CAregaddr); /* flush */
7292
7293 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7294 /* First read mbox status word */
7295 word0 = *((uint32_t *)phba->mbox);
7296 word0 = le32_to_cpu(word0);
7297 } else {
7298 /* First read mbox status word */
7299 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7300 spin_unlock_irqrestore(&phba->hbalock,
7301 drvr_flag);
7302 goto out_not_finished;
7303 }
7304 }
7305
7306 /* Read the HBA Host Attention Register */
7307 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7308 spin_unlock_irqrestore(&phba->hbalock,
7309 drvr_flag);
7310 goto out_not_finished;
7311 }
7312 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7313 1000) + jiffies;
7314 i = 0;
7315 /* Wait for command to complete */
7316 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7317 (!(ha_copy & HA_MBATT) &&
7318 (phba->link_state > LPFC_WARM_START))) {
7319 if (time_after(jiffies, timeout)) {
7320 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7321 spin_unlock_irqrestore(&phba->hbalock,
7322 drvr_flag);
7323 goto out_not_finished;
7324 }
7325
7326 /* Check if we took a mbox interrupt while we were
7327 polling */
7328 if (((word0 & OWN_CHIP) != OWN_CHIP)
7329 && (evtctr != psli->slistat.mbox_event))
7330 break;
7331
7332 if (i++ > 10) {
7333 spin_unlock_irqrestore(&phba->hbalock,
7334 drvr_flag);
7335 msleep(1);
7336 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7337 }
7338
7339 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7340 /* First copy command data */
7341 word0 = *((uint32_t *)phba->mbox);
7342 word0 = le32_to_cpu(word0);
7343 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7344 MAILBOX_t *slimmb;
7345 uint32_t slimword0;
7346 /* Check real SLIM for any errors */
7347 slimword0 = readl(phba->MBslimaddr);
7348 slimmb = (MAILBOX_t *) & slimword0;
7349 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7350 && slimmb->mbxStatus) {
7351 psli->sli_flag &=
7352 ~LPFC_SLI_ACTIVE;
7353 word0 = slimword0;
7354 }
7355 }
7356 } else {
7357 /* First copy command data */
7358 word0 = readl(phba->MBslimaddr);
7359 }
7360 /* Read the HBA Host Attention Register */
7361 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7362 spin_unlock_irqrestore(&phba->hbalock,
7363 drvr_flag);
7364 goto out_not_finished;
7365 }
7366 }
7367
7368 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7369 /* copy results back to user */
7370 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7371 /* Copy the mailbox extension data */
7372 if (pmbox->out_ext_byte_len && pmbox->context2) {
7373 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7374 pmbox->context2,
7375 pmbox->out_ext_byte_len);
7376 }
7377 } else {
7378 /* First copy command data */
7379 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7380 MAILBOX_CMD_SIZE);
7381 /* Copy the mailbox extension data */
7382 if (pmbox->out_ext_byte_len && pmbox->context2) {
7383 lpfc_memcpy_from_slim(pmbox->context2,
7384 phba->MBslimaddr +
7385 MAILBOX_HBA_EXT_OFFSET,
7386 pmbox->out_ext_byte_len);
7387 }
7388 }
7389
7390 writel(HA_MBATT, phba->HAregaddr);
7391 readl(phba->HAregaddr); /* flush */
7392
7393 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7394 status = mbx->mbxStatus;
7395 }
7396
7397 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7398 return status;
7399
7400 out_not_finished:
7401 if (processing_queue) {
7402 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7403 lpfc_mbox_cmpl_put(phba, pmbox);
7404 }
7405 return MBX_NOT_FINISHED;
7406 }
7407
7408 /**
7409 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7410 * @phba: Pointer to HBA context object.
7411 *
7412 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7413 * the driver internal pending mailbox queue. It will then try to wait out the
7414 * possible outstanding mailbox command before return.
7415 *
7416 * Returns:
7417 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7418 * the outstanding mailbox command timed out.
7419 **/
7420 static int
7421 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7422 {
7423 struct lpfc_sli *psli = &phba->sli;
7424 int rc = 0;
7425 unsigned long timeout = 0;
7426
7427 /* Mark the asynchronous mailbox command posting as blocked */
7428 spin_lock_irq(&phba->hbalock);
7429 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7430 /* Determine how long we might wait for the active mailbox
7431 * command to be gracefully completed by firmware.
7432 */
7433 if (phba->sli.mbox_active)
7434 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7435 phba->sli.mbox_active) *
7436 1000) + jiffies;
7437 spin_unlock_irq(&phba->hbalock);
7438
7439 /* Make sure the mailbox is really active */
7440 if (timeout)
7441 lpfc_sli4_process_missed_mbox_completions(phba);
7442
7443 /* Wait for the outstnading mailbox command to complete */
7444 while (phba->sli.mbox_active) {
7445 /* Check active mailbox complete status every 2ms */
7446 msleep(2);
7447 if (time_after(jiffies, timeout)) {
7448 /* Timeout, marked the outstanding cmd not complete */
7449 rc = 1;
7450 break;
7451 }
7452 }
7453
7454 /* Can not cleanly block async mailbox command, fails it */
7455 if (rc) {
7456 spin_lock_irq(&phba->hbalock);
7457 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7458 spin_unlock_irq(&phba->hbalock);
7459 }
7460 return rc;
7461 }
7462
7463 /**
7464 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7465 * @phba: Pointer to HBA context object.
7466 *
7467 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7468 * commands from the driver internal pending mailbox queue. It makes sure
7469 * that there is no outstanding mailbox command before resuming posting
7470 * asynchronous mailbox commands. If, for any reason, there is outstanding
7471 * mailbox command, it will try to wait it out before resuming asynchronous
7472 * mailbox command posting.
7473 **/
7474 static void
7475 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7476 {
7477 struct lpfc_sli *psli = &phba->sli;
7478
7479 spin_lock_irq(&phba->hbalock);
7480 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7481 /* Asynchronous mailbox posting is not blocked, do nothing */
7482 spin_unlock_irq(&phba->hbalock);
7483 return;
7484 }
7485
7486 /* Outstanding synchronous mailbox command is guaranteed to be done,
7487 * successful or timeout, after timing-out the outstanding mailbox
7488 * command shall always be removed, so just unblock posting async
7489 * mailbox command and resume
7490 */
7491 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7492 spin_unlock_irq(&phba->hbalock);
7493
7494 /* wake up worker thread to post asynchronlous mailbox command */
7495 lpfc_worker_wake_up(phba);
7496 }
7497
7498 /**
7499 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7500 * @phba: Pointer to HBA context object.
7501 * @mboxq: Pointer to mailbox object.
7502 *
7503 * The function waits for the bootstrap mailbox register ready bit from
7504 * port for twice the regular mailbox command timeout value.
7505 *
7506 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7507 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7508 **/
7509 static int
7510 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7511 {
7512 uint32_t db_ready;
7513 unsigned long timeout;
7514 struct lpfc_register bmbx_reg;
7515
7516 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7517 * 1000) + jiffies;
7518
7519 do {
7520 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7521 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7522 if (!db_ready)
7523 msleep(2);
7524
7525 if (time_after(jiffies, timeout))
7526 return MBXERR_ERROR;
7527 } while (!db_ready);
7528
7529 return 0;
7530 }
7531
7532 /**
7533 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7534 * @phba: Pointer to HBA context object.
7535 * @mboxq: Pointer to mailbox object.
7536 *
7537 * The function posts a mailbox to the port. The mailbox is expected
7538 * to be comletely filled in and ready for the port to operate on it.
7539 * This routine executes a synchronous completion operation on the
7540 * mailbox by polling for its completion.
7541 *
7542 * The caller must not be holding any locks when calling this routine.
7543 *
7544 * Returns:
7545 * MBX_SUCCESS - mailbox posted successfully
7546 * Any of the MBX error values.
7547 **/
7548 static int
7549 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7550 {
7551 int rc = MBX_SUCCESS;
7552 unsigned long iflag;
7553 uint32_t mcqe_status;
7554 uint32_t mbx_cmnd;
7555 struct lpfc_sli *psli = &phba->sli;
7556 struct lpfc_mqe *mb = &mboxq->u.mqe;
7557 struct lpfc_bmbx_create *mbox_rgn;
7558 struct dma_address *dma_address;
7559
7560 /*
7561 * Only one mailbox can be active to the bootstrap mailbox region
7562 * at a time and there is no queueing provided.
7563 */
7564 spin_lock_irqsave(&phba->hbalock, iflag);
7565 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7566 spin_unlock_irqrestore(&phba->hbalock, iflag);
7567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7568 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7569 "cannot issue Data: x%x x%x\n",
7570 mboxq->vport ? mboxq->vport->vpi : 0,
7571 mboxq->u.mb.mbxCommand,
7572 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7573 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7574 psli->sli_flag, MBX_POLL);
7575 return MBXERR_ERROR;
7576 }
7577 /* The server grabs the token and owns it until release */
7578 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7579 phba->sli.mbox_active = mboxq;
7580 spin_unlock_irqrestore(&phba->hbalock, iflag);
7581
7582 /* wait for bootstrap mbox register for readyness */
7583 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7584 if (rc)
7585 goto exit;
7586
7587 /*
7588 * Initialize the bootstrap memory region to avoid stale data areas
7589 * in the mailbox post. Then copy the caller's mailbox contents to
7590 * the bmbx mailbox region.
7591 */
7592 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7593 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7594 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7595 sizeof(struct lpfc_mqe));
7596
7597 /* Post the high mailbox dma address to the port and wait for ready. */
7598 dma_address = &phba->sli4_hba.bmbx.dma_address;
7599 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7600
7601 /* wait for bootstrap mbox register for hi-address write done */
7602 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7603 if (rc)
7604 goto exit;
7605
7606 /* Post the low mailbox dma address to the port. */
7607 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7608
7609 /* wait for bootstrap mbox register for low address write done */
7610 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7611 if (rc)
7612 goto exit;
7613
7614 /*
7615 * Read the CQ to ensure the mailbox has completed.
7616 * If so, update the mailbox status so that the upper layers
7617 * can complete the request normally.
7618 */
7619 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7620 sizeof(struct lpfc_mqe));
7621 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7622 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7623 sizeof(struct lpfc_mcqe));
7624 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7625 /*
7626 * When the CQE status indicates a failure and the mailbox status
7627 * indicates success then copy the CQE status into the mailbox status
7628 * (and prefix it with x4000).
7629 */
7630 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7631 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7632 bf_set(lpfc_mqe_status, mb,
7633 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7634 rc = MBXERR_ERROR;
7635 } else
7636 lpfc_sli4_swap_str(phba, mboxq);
7637
7638 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7639 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7640 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7641 " x%x x%x CQ: x%x x%x x%x x%x\n",
7642 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7643 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7644 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7645 bf_get(lpfc_mqe_status, mb),
7646 mb->un.mb_words[0], mb->un.mb_words[1],
7647 mb->un.mb_words[2], mb->un.mb_words[3],
7648 mb->un.mb_words[4], mb->un.mb_words[5],
7649 mb->un.mb_words[6], mb->un.mb_words[7],
7650 mb->un.mb_words[8], mb->un.mb_words[9],
7651 mb->un.mb_words[10], mb->un.mb_words[11],
7652 mb->un.mb_words[12], mboxq->mcqe.word0,
7653 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7654 mboxq->mcqe.trailer);
7655 exit:
7656 /* We are holding the token, no needed for lock when release */
7657 spin_lock_irqsave(&phba->hbalock, iflag);
7658 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7659 phba->sli.mbox_active = NULL;
7660 spin_unlock_irqrestore(&phba->hbalock, iflag);
7661 return rc;
7662 }
7663
7664 /**
7665 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7666 * @phba: Pointer to HBA context object.
7667 * @pmbox: Pointer to mailbox object.
7668 * @flag: Flag indicating how the mailbox need to be processed.
7669 *
7670 * This function is called by discovery code and HBA management code to submit
7671 * a mailbox command to firmware with SLI-4 interface spec.
7672 *
7673 * Return codes the caller owns the mailbox command after the return of the
7674 * function.
7675 **/
7676 static int
7677 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7678 uint32_t flag)
7679 {
7680 struct lpfc_sli *psli = &phba->sli;
7681 unsigned long iflags;
7682 int rc;
7683
7684 /* dump from issue mailbox command if setup */
7685 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7686
7687 rc = lpfc_mbox_dev_check(phba);
7688 if (unlikely(rc)) {
7689 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7690 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7691 "cannot issue Data: x%x x%x\n",
7692 mboxq->vport ? mboxq->vport->vpi : 0,
7693 mboxq->u.mb.mbxCommand,
7694 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7695 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7696 psli->sli_flag, flag);
7697 goto out_not_finished;
7698 }
7699
7700 /* Detect polling mode and jump to a handler */
7701 if (!phba->sli4_hba.intr_enable) {
7702 if (flag == MBX_POLL)
7703 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7704 else
7705 rc = -EIO;
7706 if (rc != MBX_SUCCESS)
7707 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7708 "(%d):2541 Mailbox command x%x "
7709 "(x%x/x%x) failure: "
7710 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7711 "Data: x%x x%x\n,",
7712 mboxq->vport ? mboxq->vport->vpi : 0,
7713 mboxq->u.mb.mbxCommand,
7714 lpfc_sli_config_mbox_subsys_get(phba,
7715 mboxq),
7716 lpfc_sli_config_mbox_opcode_get(phba,
7717 mboxq),
7718 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7719 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7720 bf_get(lpfc_mcqe_ext_status,
7721 &mboxq->mcqe),
7722 psli->sli_flag, flag);
7723 return rc;
7724 } else if (flag == MBX_POLL) {
7725 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7726 "(%d):2542 Try to issue mailbox command "
7727 "x%x (x%x/x%x) synchronously ahead of async"
7728 "mailbox command queue: x%x x%x\n",
7729 mboxq->vport ? mboxq->vport->vpi : 0,
7730 mboxq->u.mb.mbxCommand,
7731 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7732 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7733 psli->sli_flag, flag);
7734 /* Try to block the asynchronous mailbox posting */
7735 rc = lpfc_sli4_async_mbox_block(phba);
7736 if (!rc) {
7737 /* Successfully blocked, now issue sync mbox cmd */
7738 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7739 if (rc != MBX_SUCCESS)
7740 lpfc_printf_log(phba, KERN_WARNING,
7741 LOG_MBOX | LOG_SLI,
7742 "(%d):2597 Sync Mailbox command "
7743 "x%x (x%x/x%x) failure: "
7744 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7745 "Data: x%x x%x\n,",
7746 mboxq->vport ? mboxq->vport->vpi : 0,
7747 mboxq->u.mb.mbxCommand,
7748 lpfc_sli_config_mbox_subsys_get(phba,
7749 mboxq),
7750 lpfc_sli_config_mbox_opcode_get(phba,
7751 mboxq),
7752 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7753 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7754 bf_get(lpfc_mcqe_ext_status,
7755 &mboxq->mcqe),
7756 psli->sli_flag, flag);
7757 /* Unblock the async mailbox posting afterward */
7758 lpfc_sli4_async_mbox_unblock(phba);
7759 }
7760 return rc;
7761 }
7762
7763 /* Now, interrupt mode asynchrous mailbox command */
7764 rc = lpfc_mbox_cmd_check(phba, mboxq);
7765 if (rc) {
7766 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7767 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7768 "cannot issue Data: x%x x%x\n",
7769 mboxq->vport ? mboxq->vport->vpi : 0,
7770 mboxq->u.mb.mbxCommand,
7771 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7772 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7773 psli->sli_flag, flag);
7774 goto out_not_finished;
7775 }
7776
7777 /* Put the mailbox command to the driver internal FIFO */
7778 psli->slistat.mbox_busy++;
7779 spin_lock_irqsave(&phba->hbalock, iflags);
7780 lpfc_mbox_put(phba, mboxq);
7781 spin_unlock_irqrestore(&phba->hbalock, iflags);
7782 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7783 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7784 "x%x (x%x/x%x) x%x x%x x%x\n",
7785 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7786 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7787 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7788 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7789 phba->pport->port_state,
7790 psli->sli_flag, MBX_NOWAIT);
7791 /* Wake up worker thread to transport mailbox command from head */
7792 lpfc_worker_wake_up(phba);
7793
7794 return MBX_BUSY;
7795
7796 out_not_finished:
7797 return MBX_NOT_FINISHED;
7798 }
7799
7800 /**
7801 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7802 * @phba: Pointer to HBA context object.
7803 *
7804 * This function is called by worker thread to send a mailbox command to
7805 * SLI4 HBA firmware.
7806 *
7807 **/
7808 int
7809 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7810 {
7811 struct lpfc_sli *psli = &phba->sli;
7812 LPFC_MBOXQ_t *mboxq;
7813 int rc = MBX_SUCCESS;
7814 unsigned long iflags;
7815 struct lpfc_mqe *mqe;
7816 uint32_t mbx_cmnd;
7817
7818 /* Check interrupt mode before post async mailbox command */
7819 if (unlikely(!phba->sli4_hba.intr_enable))
7820 return MBX_NOT_FINISHED;
7821
7822 /* Check for mailbox command service token */
7823 spin_lock_irqsave(&phba->hbalock, iflags);
7824 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7825 spin_unlock_irqrestore(&phba->hbalock, iflags);
7826 return MBX_NOT_FINISHED;
7827 }
7828 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7829 spin_unlock_irqrestore(&phba->hbalock, iflags);
7830 return MBX_NOT_FINISHED;
7831 }
7832 if (unlikely(phba->sli.mbox_active)) {
7833 spin_unlock_irqrestore(&phba->hbalock, iflags);
7834 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7835 "0384 There is pending active mailbox cmd\n");
7836 return MBX_NOT_FINISHED;
7837 }
7838 /* Take the mailbox command service token */
7839 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7840
7841 /* Get the next mailbox command from head of queue */
7842 mboxq = lpfc_mbox_get(phba);
7843
7844 /* If no more mailbox command waiting for post, we're done */
7845 if (!mboxq) {
7846 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7847 spin_unlock_irqrestore(&phba->hbalock, iflags);
7848 return MBX_SUCCESS;
7849 }
7850 phba->sli.mbox_active = mboxq;
7851 spin_unlock_irqrestore(&phba->hbalock, iflags);
7852
7853 /* Check device readiness for posting mailbox command */
7854 rc = lpfc_mbox_dev_check(phba);
7855 if (unlikely(rc))
7856 /* Driver clean routine will clean up pending mailbox */
7857 goto out_not_finished;
7858
7859 /* Prepare the mbox command to be posted */
7860 mqe = &mboxq->u.mqe;
7861 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7862
7863 /* Start timer for the mbox_tmo and log some mailbox post messages */
7864 mod_timer(&psli->mbox_tmo, (jiffies +
7865 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
7866
7867 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7868 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7869 "x%x x%x\n",
7870 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7871 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7872 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7873 phba->pport->port_state, psli->sli_flag);
7874
7875 if (mbx_cmnd != MBX_HEARTBEAT) {
7876 if (mboxq->vport) {
7877 lpfc_debugfs_disc_trc(mboxq->vport,
7878 LPFC_DISC_TRC_MBOX_VPORT,
7879 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7880 mbx_cmnd, mqe->un.mb_words[0],
7881 mqe->un.mb_words[1]);
7882 } else {
7883 lpfc_debugfs_disc_trc(phba->pport,
7884 LPFC_DISC_TRC_MBOX,
7885 "MBOX Send: cmd:x%x mb:x%x x%x",
7886 mbx_cmnd, mqe->un.mb_words[0],
7887 mqe->un.mb_words[1]);
7888 }
7889 }
7890 psli->slistat.mbox_cmd++;
7891
7892 /* Post the mailbox command to the port */
7893 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7894 if (rc != MBX_SUCCESS) {
7895 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7896 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7897 "cannot issue Data: x%x x%x\n",
7898 mboxq->vport ? mboxq->vport->vpi : 0,
7899 mboxq->u.mb.mbxCommand,
7900 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7901 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7902 psli->sli_flag, MBX_NOWAIT);
7903 goto out_not_finished;
7904 }
7905
7906 return rc;
7907
7908 out_not_finished:
7909 spin_lock_irqsave(&phba->hbalock, iflags);
7910 if (phba->sli.mbox_active) {
7911 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7912 __lpfc_mbox_cmpl_put(phba, mboxq);
7913 /* Release the token */
7914 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7915 phba->sli.mbox_active = NULL;
7916 }
7917 spin_unlock_irqrestore(&phba->hbalock, iflags);
7918
7919 return MBX_NOT_FINISHED;
7920 }
7921
7922 /**
7923 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7924 * @phba: Pointer to HBA context object.
7925 * @pmbox: Pointer to mailbox object.
7926 * @flag: Flag indicating how the mailbox need to be processed.
7927 *
7928 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7929 * the API jump table function pointer from the lpfc_hba struct.
7930 *
7931 * Return codes the caller owns the mailbox command after the return of the
7932 * function.
7933 **/
7934 int
7935 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7936 {
7937 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7938 }
7939
7940 /**
7941 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7942 * @phba: The hba struct for which this call is being executed.
7943 * @dev_grp: The HBA PCI-Device group number.
7944 *
7945 * This routine sets up the mbox interface API function jump table in @phba
7946 * struct.
7947 * Returns: 0 - success, -ENODEV - failure.
7948 **/
7949 int
7950 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7951 {
7952
7953 switch (dev_grp) {
7954 case LPFC_PCI_DEV_LP:
7955 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7956 phba->lpfc_sli_handle_slow_ring_event =
7957 lpfc_sli_handle_slow_ring_event_s3;
7958 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7959 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7960 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7961 break;
7962 case LPFC_PCI_DEV_OC:
7963 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7964 phba->lpfc_sli_handle_slow_ring_event =
7965 lpfc_sli_handle_slow_ring_event_s4;
7966 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7967 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7968 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7969 break;
7970 default:
7971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7972 "1420 Invalid HBA PCI-device group: 0x%x\n",
7973 dev_grp);
7974 return -ENODEV;
7975 break;
7976 }
7977 return 0;
7978 }
7979
7980 /**
7981 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7982 * @phba: Pointer to HBA context object.
7983 * @pring: Pointer to driver SLI ring object.
7984 * @piocb: Pointer to address of newly added command iocb.
7985 *
7986 * This function is called with hbalock held to add a command
7987 * iocb to the txq when SLI layer cannot submit the command iocb
7988 * to the ring.
7989 **/
7990 void
7991 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7992 struct lpfc_iocbq *piocb)
7993 {
7994 lockdep_assert_held(&phba->hbalock);
7995 /* Insert the caller's iocb in the txq tail for later processing. */
7996 list_add_tail(&piocb->list, &pring->txq);
7997 }
7998
7999 /**
8000 * lpfc_sli_next_iocb - Get the next iocb in the txq
8001 * @phba: Pointer to HBA context object.
8002 * @pring: Pointer to driver SLI ring object.
8003 * @piocb: Pointer to address of newly added command iocb.
8004 *
8005 * This function is called with hbalock held before a new
8006 * iocb is submitted to the firmware. This function checks
8007 * txq to flush the iocbs in txq to Firmware before
8008 * submitting new iocbs to the Firmware.
8009 * If there are iocbs in the txq which need to be submitted
8010 * to firmware, lpfc_sli_next_iocb returns the first element
8011 * of the txq after dequeuing it from txq.
8012 * If there is no iocb in the txq then the function will return
8013 * *piocb and *piocb is set to NULL. Caller needs to check
8014 * *piocb to find if there are more commands in the txq.
8015 **/
8016 static struct lpfc_iocbq *
8017 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8018 struct lpfc_iocbq **piocb)
8019 {
8020 struct lpfc_iocbq * nextiocb;
8021
8022 lockdep_assert_held(&phba->hbalock);
8023
8024 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8025 if (!nextiocb) {
8026 nextiocb = *piocb;
8027 *piocb = NULL;
8028 }
8029
8030 return nextiocb;
8031 }
8032
8033 /**
8034 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8035 * @phba: Pointer to HBA context object.
8036 * @ring_number: SLI ring number to issue iocb on.
8037 * @piocb: Pointer to command iocb.
8038 * @flag: Flag indicating if this command can be put into txq.
8039 *
8040 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8041 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8042 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8043 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8044 * this function allows only iocbs for posting buffers. This function finds
8045 * next available slot in the command ring and posts the command to the
8046 * available slot and writes the port attention register to request HBA start
8047 * processing new iocb. If there is no slot available in the ring and
8048 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8049 * the function returns IOCB_BUSY.
8050 *
8051 * This function is called with hbalock held. The function will return success
8052 * after it successfully submit the iocb to firmware or after adding to the
8053 * txq.
8054 **/
8055 static int
8056 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8057 struct lpfc_iocbq *piocb, uint32_t flag)
8058 {
8059 struct lpfc_iocbq *nextiocb;
8060 IOCB_t *iocb;
8061 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8062
8063 lockdep_assert_held(&phba->hbalock);
8064
8065 if (piocb->iocb_cmpl && (!piocb->vport) &&
8066 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8067 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8068 lpfc_printf_log(phba, KERN_ERR,
8069 LOG_SLI | LOG_VPORT,
8070 "1807 IOCB x%x failed. No vport\n",
8071 piocb->iocb.ulpCommand);
8072 dump_stack();
8073 return IOCB_ERROR;
8074 }
8075
8076
8077 /* If the PCI channel is in offline state, do not post iocbs. */
8078 if (unlikely(pci_channel_offline(phba->pcidev)))
8079 return IOCB_ERROR;
8080
8081 /* If HBA has a deferred error attention, fail the iocb. */
8082 if (unlikely(phba->hba_flag & DEFER_ERATT))
8083 return IOCB_ERROR;
8084
8085 /*
8086 * We should never get an IOCB if we are in a < LINK_DOWN state
8087 */
8088 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8089 return IOCB_ERROR;
8090
8091 /*
8092 * Check to see if we are blocking IOCB processing because of a
8093 * outstanding event.
8094 */
8095 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8096 goto iocb_busy;
8097
8098 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8099 /*
8100 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8101 * can be issued if the link is not up.
8102 */
8103 switch (piocb->iocb.ulpCommand) {
8104 case CMD_GEN_REQUEST64_CR:
8105 case CMD_GEN_REQUEST64_CX:
8106 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8107 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8108 FC_RCTL_DD_UNSOL_CMD) ||
8109 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8110 MENLO_TRANSPORT_TYPE))
8111
8112 goto iocb_busy;
8113 break;
8114 case CMD_QUE_RING_BUF_CN:
8115 case CMD_QUE_RING_BUF64_CN:
8116 /*
8117 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8118 * completion, iocb_cmpl MUST be 0.
8119 */
8120 if (piocb->iocb_cmpl)
8121 piocb->iocb_cmpl = NULL;
8122 /*FALLTHROUGH*/
8123 case CMD_CREATE_XRI_CR:
8124 case CMD_CLOSE_XRI_CN:
8125 case CMD_CLOSE_XRI_CX:
8126 break;
8127 default:
8128 goto iocb_busy;
8129 }
8130
8131 /*
8132 * For FCP commands, we must be in a state where we can process link
8133 * attention events.
8134 */
8135 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
8136 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8137 goto iocb_busy;
8138 }
8139
8140 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8141 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8142 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8143
8144 if (iocb)
8145 lpfc_sli_update_ring(phba, pring);
8146 else
8147 lpfc_sli_update_full_ring(phba, pring);
8148
8149 if (!piocb)
8150 return IOCB_SUCCESS;
8151
8152 goto out_busy;
8153
8154 iocb_busy:
8155 pring->stats.iocb_cmd_delay++;
8156
8157 out_busy:
8158
8159 if (!(flag & SLI_IOCB_RET_IOCB)) {
8160 __lpfc_sli_ringtx_put(phba, pring, piocb);
8161 return IOCB_SUCCESS;
8162 }
8163
8164 return IOCB_BUSY;
8165 }
8166
8167 /**
8168 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8169 * @phba: Pointer to HBA context object.
8170 * @piocb: Pointer to command iocb.
8171 * @sglq: Pointer to the scatter gather queue object.
8172 *
8173 * This routine converts the bpl or bde that is in the IOCB
8174 * to a sgl list for the sli4 hardware. The physical address
8175 * of the bpl/bde is converted back to a virtual address.
8176 * If the IOCB contains a BPL then the list of BDE's is
8177 * converted to sli4_sge's. If the IOCB contains a single
8178 * BDE then it is converted to a single sli_sge.
8179 * The IOCB is still in cpu endianess so the contents of
8180 * the bpl can be used without byte swapping.
8181 *
8182 * Returns valid XRI = Success, NO_XRI = Failure.
8183 **/
8184 static uint16_t
8185 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8186 struct lpfc_sglq *sglq)
8187 {
8188 uint16_t xritag = NO_XRI;
8189 struct ulp_bde64 *bpl = NULL;
8190 struct ulp_bde64 bde;
8191 struct sli4_sge *sgl = NULL;
8192 struct lpfc_dmabuf *dmabuf;
8193 IOCB_t *icmd;
8194 int numBdes = 0;
8195 int i = 0;
8196 uint32_t offset = 0; /* accumulated offset in the sg request list */
8197 int inbound = 0; /* number of sg reply entries inbound from firmware */
8198
8199 if (!piocbq || !sglq)
8200 return xritag;
8201
8202 sgl = (struct sli4_sge *)sglq->sgl;
8203 icmd = &piocbq->iocb;
8204 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8205 return sglq->sli4_xritag;
8206 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8207 numBdes = icmd->un.genreq64.bdl.bdeSize /
8208 sizeof(struct ulp_bde64);
8209 /* The addrHigh and addrLow fields within the IOCB
8210 * have not been byteswapped yet so there is no
8211 * need to swap them back.
8212 */
8213 if (piocbq->context3)
8214 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8215 else
8216 return xritag;
8217
8218 bpl = (struct ulp_bde64 *)dmabuf->virt;
8219 if (!bpl)
8220 return xritag;
8221
8222 for (i = 0; i < numBdes; i++) {
8223 /* Should already be byte swapped. */
8224 sgl->addr_hi = bpl->addrHigh;
8225 sgl->addr_lo = bpl->addrLow;
8226
8227 sgl->word2 = le32_to_cpu(sgl->word2);
8228 if ((i+1) == numBdes)
8229 bf_set(lpfc_sli4_sge_last, sgl, 1);
8230 else
8231 bf_set(lpfc_sli4_sge_last, sgl, 0);
8232 /* swap the size field back to the cpu so we
8233 * can assign it to the sgl.
8234 */
8235 bde.tus.w = le32_to_cpu(bpl->tus.w);
8236 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8237 /* The offsets in the sgl need to be accumulated
8238 * separately for the request and reply lists.
8239 * The request is always first, the reply follows.
8240 */
8241 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8242 /* add up the reply sg entries */
8243 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8244 inbound++;
8245 /* first inbound? reset the offset */
8246 if (inbound == 1)
8247 offset = 0;
8248 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8249 bf_set(lpfc_sli4_sge_type, sgl,
8250 LPFC_SGE_TYPE_DATA);
8251 offset += bde.tus.f.bdeSize;
8252 }
8253 sgl->word2 = cpu_to_le32(sgl->word2);
8254 bpl++;
8255 sgl++;
8256 }
8257 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8258 /* The addrHigh and addrLow fields of the BDE have not
8259 * been byteswapped yet so they need to be swapped
8260 * before putting them in the sgl.
8261 */
8262 sgl->addr_hi =
8263 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8264 sgl->addr_lo =
8265 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8266 sgl->word2 = le32_to_cpu(sgl->word2);
8267 bf_set(lpfc_sli4_sge_last, sgl, 1);
8268 sgl->word2 = cpu_to_le32(sgl->word2);
8269 sgl->sge_len =
8270 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8271 }
8272 return sglq->sli4_xritag;
8273 }
8274
8275 /**
8276 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8277 * @phba: Pointer to HBA context object.
8278 * @piocb: Pointer to command iocb.
8279 * @wqe: Pointer to the work queue entry.
8280 *
8281 * This routine converts the iocb command to its Work Queue Entry
8282 * equivalent. The wqe pointer should not have any fields set when
8283 * this routine is called because it will memcpy over them.
8284 * This routine does not set the CQ_ID or the WQEC bits in the
8285 * wqe.
8286 *
8287 * Returns: 0 = Success, IOCB_ERROR = Failure.
8288 **/
8289 static int
8290 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8291 union lpfc_wqe *wqe)
8292 {
8293 uint32_t xmit_len = 0, total_len = 0;
8294 uint8_t ct = 0;
8295 uint32_t fip;
8296 uint32_t abort_tag;
8297 uint8_t command_type = ELS_COMMAND_NON_FIP;
8298 uint8_t cmnd;
8299 uint16_t xritag;
8300 uint16_t abrt_iotag;
8301 struct lpfc_iocbq *abrtiocbq;
8302 struct ulp_bde64 *bpl = NULL;
8303 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8304 int numBdes, i;
8305 struct ulp_bde64 bde;
8306 struct lpfc_nodelist *ndlp;
8307 uint32_t *pcmd;
8308 uint32_t if_type;
8309
8310 fip = phba->hba_flag & HBA_FIP_SUPPORT;
8311 /* The fcp commands will set command type */
8312 if (iocbq->iocb_flag & LPFC_IO_FCP)
8313 command_type = FCP_COMMAND;
8314 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8315 command_type = ELS_COMMAND_FIP;
8316 else
8317 command_type = ELS_COMMAND_NON_FIP;
8318
8319 if (phba->fcp_embed_io)
8320 memset(wqe, 0, sizeof(union lpfc_wqe128));
8321 /* Some of the fields are in the right position already */
8322 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8323 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8324 wqe->generic.wqe_com.word10 = 0;
8325
8326 abort_tag = (uint32_t) iocbq->iotag;
8327 xritag = iocbq->sli4_xritag;
8328 /* words0-2 bpl convert bde */
8329 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8330 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8331 sizeof(struct ulp_bde64);
8332 bpl = (struct ulp_bde64 *)
8333 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8334 if (!bpl)
8335 return IOCB_ERROR;
8336
8337 /* Should already be byte swapped. */
8338 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8339 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8340 /* swap the size field back to the cpu so we
8341 * can assign it to the sgl.
8342 */
8343 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
8344 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8345 total_len = 0;
8346 for (i = 0; i < numBdes; i++) {
8347 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8348 total_len += bde.tus.f.bdeSize;
8349 }
8350 } else
8351 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8352
8353 iocbq->iocb.ulpIoTag = iocbq->iotag;
8354 cmnd = iocbq->iocb.ulpCommand;
8355
8356 switch (iocbq->iocb.ulpCommand) {
8357 case CMD_ELS_REQUEST64_CR:
8358 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8359 ndlp = iocbq->context_un.ndlp;
8360 else
8361 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8362 if (!iocbq->iocb.ulpLe) {
8363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8364 "2007 Only Limited Edition cmd Format"
8365 " supported 0x%x\n",
8366 iocbq->iocb.ulpCommand);
8367 return IOCB_ERROR;
8368 }
8369
8370 wqe->els_req.payload_len = xmit_len;
8371 /* Els_reguest64 has a TMO */
8372 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8373 iocbq->iocb.ulpTimeout);
8374 /* Need a VF for word 4 set the vf bit*/
8375 bf_set(els_req64_vf, &wqe->els_req, 0);
8376 /* And a VFID for word 12 */
8377 bf_set(els_req64_vfid, &wqe->els_req, 0);
8378 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8379 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8380 iocbq->iocb.ulpContext);
8381 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8382 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8383 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8384 if (command_type == ELS_COMMAND_FIP)
8385 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8386 >> LPFC_FIP_ELS_ID_SHIFT);
8387 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8388 iocbq->context2)->virt);
8389 if_type = bf_get(lpfc_sli_intf_if_type,
8390 &phba->sli4_hba.sli_intf);
8391 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8392 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8393 *pcmd == ELS_CMD_SCR ||
8394 *pcmd == ELS_CMD_FDISC ||
8395 *pcmd == ELS_CMD_LOGO ||
8396 *pcmd == ELS_CMD_PLOGI)) {
8397 bf_set(els_req64_sp, &wqe->els_req, 1);
8398 bf_set(els_req64_sid, &wqe->els_req,
8399 iocbq->vport->fc_myDID);
8400 if ((*pcmd == ELS_CMD_FLOGI) &&
8401 !(phba->fc_topology ==
8402 LPFC_TOPOLOGY_LOOP))
8403 bf_set(els_req64_sid, &wqe->els_req, 0);
8404 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8405 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8406 phba->vpi_ids[iocbq->vport->vpi]);
8407 } else if (pcmd && iocbq->context1) {
8408 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8409 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8410 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8411 }
8412 }
8413 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8414 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8415 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8416 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8417 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8418 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8419 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8420 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8421 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8422 break;
8423 case CMD_XMIT_SEQUENCE64_CX:
8424 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8425 iocbq->iocb.un.ulpWord[3]);
8426 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8427 iocbq->iocb.unsli3.rcvsli3.ox_id);
8428 /* The entire sequence is transmitted for this IOCB */
8429 xmit_len = total_len;
8430 cmnd = CMD_XMIT_SEQUENCE64_CR;
8431 if (phba->link_flag & LS_LOOPBACK_MODE)
8432 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8433 case CMD_XMIT_SEQUENCE64_CR:
8434 /* word3 iocb=io_tag32 wqe=reserved */
8435 wqe->xmit_sequence.rsvd3 = 0;
8436 /* word4 relative_offset memcpy */
8437 /* word5 r_ctl/df_ctl memcpy */
8438 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8439 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8440 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8441 LPFC_WQE_IOD_WRITE);
8442 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8443 LPFC_WQE_LENLOC_WORD12);
8444 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8445 wqe->xmit_sequence.xmit_len = xmit_len;
8446 command_type = OTHER_COMMAND;
8447 break;
8448 case CMD_XMIT_BCAST64_CN:
8449 /* word3 iocb=iotag32 wqe=seq_payload_len */
8450 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8451 /* word4 iocb=rsvd wqe=rsvd */
8452 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8453 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8454 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8455 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8456 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8457 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8458 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8459 LPFC_WQE_LENLOC_WORD3);
8460 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8461 break;
8462 case CMD_FCP_IWRITE64_CR:
8463 command_type = FCP_COMMAND_DATA_OUT;
8464 /* word3 iocb=iotag wqe=payload_offset_len */
8465 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8466 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8467 xmit_len + sizeof(struct fcp_rsp));
8468 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8469 0);
8470 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8471 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8472 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8473 iocbq->iocb.ulpFCP2Rcvy);
8474 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8475 /* Always open the exchange */
8476 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8477 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8478 LPFC_WQE_LENLOC_WORD4);
8479 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8480 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8481 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8482 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8483 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8484 if (iocbq->priority) {
8485 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8486 (iocbq->priority << 1));
8487 } else {
8488 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8489 (phba->cfg_XLanePriority << 1));
8490 }
8491 }
8492 /* Note, word 10 is already initialized to 0 */
8493
8494 if (phba->fcp_embed_io) {
8495 struct lpfc_scsi_buf *lpfc_cmd;
8496 struct sli4_sge *sgl;
8497 union lpfc_wqe128 *wqe128;
8498 struct fcp_cmnd *fcp_cmnd;
8499 uint32_t *ptr;
8500
8501 /* 128 byte wqe support here */
8502 wqe128 = (union lpfc_wqe128 *)wqe;
8503
8504 lpfc_cmd = iocbq->context1;
8505 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8506 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8507
8508 /* Word 0-2 - FCP_CMND */
8509 wqe128->generic.bde.tus.f.bdeFlags =
8510 BUFF_TYPE_BDE_IMMED;
8511 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8512 wqe128->generic.bde.addrHigh = 0;
8513 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8514
8515 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8516
8517 /* Word 22-29 FCP CMND Payload */
8518 ptr = &wqe128->words[22];
8519 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8520 }
8521 break;
8522 case CMD_FCP_IREAD64_CR:
8523 /* word3 iocb=iotag wqe=payload_offset_len */
8524 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8525 bf_set(payload_offset_len, &wqe->fcp_iread,
8526 xmit_len + sizeof(struct fcp_rsp));
8527 bf_set(cmd_buff_len, &wqe->fcp_iread,
8528 0);
8529 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8530 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8531 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8532 iocbq->iocb.ulpFCP2Rcvy);
8533 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8534 /* Always open the exchange */
8535 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8536 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8537 LPFC_WQE_LENLOC_WORD4);
8538 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8539 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8540 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8541 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8542 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8543 if (iocbq->priority) {
8544 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8545 (iocbq->priority << 1));
8546 } else {
8547 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8548 (phba->cfg_XLanePriority << 1));
8549 }
8550 }
8551 /* Note, word 10 is already initialized to 0 */
8552
8553 if (phba->fcp_embed_io) {
8554 struct lpfc_scsi_buf *lpfc_cmd;
8555 struct sli4_sge *sgl;
8556 union lpfc_wqe128 *wqe128;
8557 struct fcp_cmnd *fcp_cmnd;
8558 uint32_t *ptr;
8559
8560 /* 128 byte wqe support here */
8561 wqe128 = (union lpfc_wqe128 *)wqe;
8562
8563 lpfc_cmd = iocbq->context1;
8564 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8565 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8566
8567 /* Word 0-2 - FCP_CMND */
8568 wqe128->generic.bde.tus.f.bdeFlags =
8569 BUFF_TYPE_BDE_IMMED;
8570 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8571 wqe128->generic.bde.addrHigh = 0;
8572 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8573
8574 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8575
8576 /* Word 22-29 FCP CMND Payload */
8577 ptr = &wqe128->words[22];
8578 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8579 }
8580 break;
8581 case CMD_FCP_ICMND64_CR:
8582 /* word3 iocb=iotag wqe=payload_offset_len */
8583 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8584 bf_set(payload_offset_len, &wqe->fcp_icmd,
8585 xmit_len + sizeof(struct fcp_rsp));
8586 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8587 0);
8588 /* word3 iocb=IO_TAG wqe=reserved */
8589 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8590 /* Always open the exchange */
8591 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8592 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8593 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8594 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8595 LPFC_WQE_LENLOC_NONE);
8596 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8597 iocbq->iocb.ulpFCP2Rcvy);
8598 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8599 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8600 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8601 if (iocbq->priority) {
8602 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8603 (iocbq->priority << 1));
8604 } else {
8605 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8606 (phba->cfg_XLanePriority << 1));
8607 }
8608 }
8609 /* Note, word 10 is already initialized to 0 */
8610
8611 if (phba->fcp_embed_io) {
8612 struct lpfc_scsi_buf *lpfc_cmd;
8613 struct sli4_sge *sgl;
8614 union lpfc_wqe128 *wqe128;
8615 struct fcp_cmnd *fcp_cmnd;
8616 uint32_t *ptr;
8617
8618 /* 128 byte wqe support here */
8619 wqe128 = (union lpfc_wqe128 *)wqe;
8620
8621 lpfc_cmd = iocbq->context1;
8622 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8623 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8624
8625 /* Word 0-2 - FCP_CMND */
8626 wqe128->generic.bde.tus.f.bdeFlags =
8627 BUFF_TYPE_BDE_IMMED;
8628 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8629 wqe128->generic.bde.addrHigh = 0;
8630 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8631
8632 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8633
8634 /* Word 22-29 FCP CMND Payload */
8635 ptr = &wqe128->words[22];
8636 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8637 }
8638 break;
8639 case CMD_GEN_REQUEST64_CR:
8640 /* For this command calculate the xmit length of the
8641 * request bde.
8642 */
8643 xmit_len = 0;
8644 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8645 sizeof(struct ulp_bde64);
8646 for (i = 0; i < numBdes; i++) {
8647 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8648 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8649 break;
8650 xmit_len += bde.tus.f.bdeSize;
8651 }
8652 /* word3 iocb=IO_TAG wqe=request_payload_len */
8653 wqe->gen_req.request_payload_len = xmit_len;
8654 /* word4 iocb=parameter wqe=relative_offset memcpy */
8655 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8656 /* word6 context tag copied in memcpy */
8657 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8658 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8660 "2015 Invalid CT %x command 0x%x\n",
8661 ct, iocbq->iocb.ulpCommand);
8662 return IOCB_ERROR;
8663 }
8664 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8665 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8666 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8667 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8668 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8669 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8670 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8671 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8672 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8673 command_type = OTHER_COMMAND;
8674 break;
8675 case CMD_XMIT_ELS_RSP64_CX:
8676 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8677 /* words0-2 BDE memcpy */
8678 /* word3 iocb=iotag32 wqe=response_payload_len */
8679 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8680 /* word4 */
8681 wqe->xmit_els_rsp.word4 = 0;
8682 /* word5 iocb=rsvd wge=did */
8683 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8684 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8685
8686 if_type = bf_get(lpfc_sli_intf_if_type,
8687 &phba->sli4_hba.sli_intf);
8688 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8689 if (iocbq->vport->fc_flag & FC_PT2PT) {
8690 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8691 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8692 iocbq->vport->fc_myDID);
8693 if (iocbq->vport->fc_myDID == Fabric_DID) {
8694 bf_set(wqe_els_did,
8695 &wqe->xmit_els_rsp.wqe_dest, 0);
8696 }
8697 }
8698 }
8699 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8700 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8701 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8702 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8703 iocbq->iocb.unsli3.rcvsli3.ox_id);
8704 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8705 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8706 phba->vpi_ids[iocbq->vport->vpi]);
8707 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8708 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8709 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8710 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8711 LPFC_WQE_LENLOC_WORD3);
8712 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8713 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8714 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8715 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8716 iocbq->context2)->virt);
8717 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8718 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8719 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8720 iocbq->vport->fc_myDID);
8721 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8722 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8723 phba->vpi_ids[phba->pport->vpi]);
8724 }
8725 command_type = OTHER_COMMAND;
8726 break;
8727 case CMD_CLOSE_XRI_CN:
8728 case CMD_ABORT_XRI_CN:
8729 case CMD_ABORT_XRI_CX:
8730 /* words 0-2 memcpy should be 0 rserved */
8731 /* port will send abts */
8732 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8733 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8734 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8735 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8736 } else
8737 fip = 0;
8738
8739 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8740 /*
8741 * The link is down, or the command was ELS_FIP
8742 * so the fw does not need to send abts
8743 * on the wire.
8744 */
8745 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8746 else
8747 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8748 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8749 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8750 wqe->abort_cmd.rsrvd5 = 0;
8751 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8752 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8753 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8754 /*
8755 * The abort handler will send us CMD_ABORT_XRI_CN or
8756 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8757 */
8758 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8759 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8760 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8761 LPFC_WQE_LENLOC_NONE);
8762 cmnd = CMD_ABORT_XRI_CX;
8763 command_type = OTHER_COMMAND;
8764 xritag = 0;
8765 break;
8766 case CMD_XMIT_BLS_RSP64_CX:
8767 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8768 /* As BLS ABTS RSP WQE is very different from other WQEs,
8769 * we re-construct this WQE here based on information in
8770 * iocbq from scratch.
8771 */
8772 memset(wqe, 0, sizeof(union lpfc_wqe));
8773 /* OX_ID is invariable to who sent ABTS to CT exchange */
8774 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8775 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8776 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8777 LPFC_ABTS_UNSOL_INT) {
8778 /* ABTS sent by initiator to CT exchange, the
8779 * RX_ID field will be filled with the newly
8780 * allocated responder XRI.
8781 */
8782 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8783 iocbq->sli4_xritag);
8784 } else {
8785 /* ABTS sent by responder to CT exchange, the
8786 * RX_ID field will be filled with the responder
8787 * RX_ID from ABTS.
8788 */
8789 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8790 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8791 }
8792 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8793 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8794
8795 /* Use CT=VPI */
8796 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8797 ndlp->nlp_DID);
8798 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8799 iocbq->iocb.ulpContext);
8800 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8801 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8802 phba->vpi_ids[phba->pport->vpi]);
8803 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8804 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8805 LPFC_WQE_LENLOC_NONE);
8806 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8807 command_type = OTHER_COMMAND;
8808 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8809 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8810 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8811 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8812 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8813 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8814 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8815 }
8816
8817 break;
8818 case CMD_XRI_ABORTED_CX:
8819 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8820 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8821 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8822 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8823 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8824 default:
8825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8826 "2014 Invalid command 0x%x\n",
8827 iocbq->iocb.ulpCommand);
8828 return IOCB_ERROR;
8829 break;
8830 }
8831
8832 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8833 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8834 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8835 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8836 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8837 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8838 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8839 LPFC_IO_DIF_INSERT);
8840 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8841 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8842 wqe->generic.wqe_com.abort_tag = abort_tag;
8843 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8844 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8845 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8846 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8847 return 0;
8848 }
8849
8850 /**
8851 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8852 * @phba: Pointer to HBA context object.
8853 * @ring_number: SLI ring number to issue iocb on.
8854 * @piocb: Pointer to command iocb.
8855 * @flag: Flag indicating if this command can be put into txq.
8856 *
8857 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8858 * an iocb command to an HBA with SLI-4 interface spec.
8859 *
8860 * This function is called with hbalock held. The function will return success
8861 * after it successfully submit the iocb to firmware or after adding to the
8862 * txq.
8863 **/
8864 static int
8865 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8866 struct lpfc_iocbq *piocb, uint32_t flag)
8867 {
8868 struct lpfc_sglq *sglq;
8869 union lpfc_wqe *wqe;
8870 union lpfc_wqe128 wqe128;
8871 struct lpfc_queue *wq;
8872 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8873
8874 lockdep_assert_held(&phba->hbalock);
8875
8876 /*
8877 * The WQE can be either 64 or 128 bytes,
8878 * so allocate space on the stack assuming the largest.
8879 */
8880 wqe = (union lpfc_wqe *)&wqe128;
8881
8882 if (piocb->sli4_xritag == NO_XRI) {
8883 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8884 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8885 sglq = NULL;
8886 else {
8887 if (!list_empty(&pring->txq)) {
8888 if (!(flag & SLI_IOCB_RET_IOCB)) {
8889 __lpfc_sli_ringtx_put(phba,
8890 pring, piocb);
8891 return IOCB_SUCCESS;
8892 } else {
8893 return IOCB_BUSY;
8894 }
8895 } else {
8896 sglq = __lpfc_sli_get_sglq(phba, piocb);
8897 if (!sglq) {
8898 if (!(flag & SLI_IOCB_RET_IOCB)) {
8899 __lpfc_sli_ringtx_put(phba,
8900 pring,
8901 piocb);
8902 return IOCB_SUCCESS;
8903 } else
8904 return IOCB_BUSY;
8905 }
8906 }
8907 }
8908 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8909 /* These IO's already have an XRI and a mapped sgl. */
8910 sglq = NULL;
8911 } else {
8912 /*
8913 * This is a continuation of a commandi,(CX) so this
8914 * sglq is on the active list
8915 */
8916 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
8917 if (!sglq)
8918 return IOCB_ERROR;
8919 }
8920
8921 if (sglq) {
8922 piocb->sli4_lxritag = sglq->sli4_lxritag;
8923 piocb->sli4_xritag = sglq->sli4_xritag;
8924 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8925 return IOCB_ERROR;
8926 }
8927
8928 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
8929 return IOCB_ERROR;
8930
8931 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8932 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8933 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
8934 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8935 } else {
8936 wq = phba->sli4_hba.oas_wq;
8937 }
8938 if (lpfc_sli4_wq_put(wq, wqe))
8939 return IOCB_ERROR;
8940 } else {
8941 if (unlikely(!phba->sli4_hba.els_wq))
8942 return IOCB_ERROR;
8943 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
8944 return IOCB_ERROR;
8945 }
8946 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8947
8948 return 0;
8949 }
8950
8951 /**
8952 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8953 *
8954 * This routine wraps the actual lockless version for issusing IOCB function
8955 * pointer from the lpfc_hba struct.
8956 *
8957 * Return codes:
8958 * IOCB_ERROR - Error
8959 * IOCB_SUCCESS - Success
8960 * IOCB_BUSY - Busy
8961 **/
8962 int
8963 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8964 struct lpfc_iocbq *piocb, uint32_t flag)
8965 {
8966 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8967 }
8968
8969 /**
8970 * lpfc_sli_api_table_setup - Set up sli api function jump table
8971 * @phba: The hba struct for which this call is being executed.
8972 * @dev_grp: The HBA PCI-Device group number.
8973 *
8974 * This routine sets up the SLI interface API function jump table in @phba
8975 * struct.
8976 * Returns: 0 - success, -ENODEV - failure.
8977 **/
8978 int
8979 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8980 {
8981
8982 switch (dev_grp) {
8983 case LPFC_PCI_DEV_LP:
8984 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8985 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8986 break;
8987 case LPFC_PCI_DEV_OC:
8988 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8989 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8990 break;
8991 default:
8992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8993 "1419 Invalid HBA PCI-device group: 0x%x\n",
8994 dev_grp);
8995 return -ENODEV;
8996 break;
8997 }
8998 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8999 return 0;
9000 }
9001
9002 /**
9003 * lpfc_sli_calc_ring - Calculates which ring to use
9004 * @phba: Pointer to HBA context object.
9005 * @ring_number: Initial ring
9006 * @piocb: Pointer to command iocb.
9007 *
9008 * For SLI4, FCP IO can deferred to one fo many WQs, based on
9009 * fcp_wqidx, thus we need to calculate the corresponding ring.
9010 * Since ABORTS must go on the same WQ of the command they are
9011 * aborting, we use command's fcp_wqidx.
9012 */
9013 static int
9014 lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
9015 struct lpfc_iocbq *piocb)
9016 {
9017 if (phba->sli_rev < LPFC_SLI_REV4)
9018 return ring_number;
9019
9020 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9021 if (!(phba->cfg_fof) ||
9022 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9023 if (unlikely(!phba->sli4_hba.fcp_wq))
9024 return LPFC_HBA_ERROR;
9025 /*
9026 * for abort iocb fcp_wqidx should already
9027 * be setup based on what work queue we used.
9028 */
9029 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9030 piocb->fcp_wqidx =
9031 lpfc_sli4_scmd_to_wqidx_distr(phba,
9032 piocb->context1);
9033 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9034 piocb->fcp_wqidx;
9035 } else {
9036 if (unlikely(!phba->sli4_hba.oas_wq))
9037 return LPFC_HBA_ERROR;
9038 piocb->fcp_wqidx = 0;
9039 ring_number = LPFC_FCP_OAS_RING;
9040 }
9041 }
9042 return ring_number;
9043 }
9044
9045 /**
9046 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9047 * @phba: Pointer to HBA context object.
9048 * @pring: Pointer to driver SLI ring object.
9049 * @piocb: Pointer to command iocb.
9050 * @flag: Flag indicating if this command can be put into txq.
9051 *
9052 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9053 * function. This function gets the hbalock and calls
9054 * __lpfc_sli_issue_iocb function and will return the error returned
9055 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9056 * functions which do not hold hbalock.
9057 **/
9058 int
9059 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9060 struct lpfc_iocbq *piocb, uint32_t flag)
9061 {
9062 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9063 struct lpfc_sli_ring *pring;
9064 struct lpfc_queue *fpeq;
9065 struct lpfc_eqe *eqe;
9066 unsigned long iflags;
9067 int rc, idx;
9068
9069 if (phba->sli_rev == LPFC_SLI_REV4) {
9070 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9071 if (unlikely(ring_number == LPFC_HBA_ERROR))
9072 return IOCB_ERROR;
9073 idx = piocb->fcp_wqidx;
9074
9075 pring = &phba->sli.ring[ring_number];
9076 spin_lock_irqsave(&pring->ring_lock, iflags);
9077 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9078 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9079
9080 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9081 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
9082
9083 if (atomic_dec_and_test(&fcp_eq_hdl->
9084 fcp_eq_in_use)) {
9085
9086 /* Get associated EQ with this index */
9087 fpeq = phba->sli4_hba.hba_eq[idx];
9088
9089 /* Turn off interrupts from this EQ */
9090 lpfc_sli4_eq_clr_intr(fpeq);
9091
9092 /*
9093 * Process all the events on FCP EQ
9094 */
9095 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9096 lpfc_sli4_hba_handle_eqe(phba,
9097 eqe, idx);
9098 fpeq->EQ_processed++;
9099 }
9100
9101 /* Always clear and re-arm the EQ */
9102 lpfc_sli4_eq_release(fpeq,
9103 LPFC_QUEUE_REARM);
9104 }
9105 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
9106 }
9107 } else {
9108 /* For now, SLI2/3 will still use hbalock */
9109 spin_lock_irqsave(&phba->hbalock, iflags);
9110 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9111 spin_unlock_irqrestore(&phba->hbalock, iflags);
9112 }
9113 return rc;
9114 }
9115
9116 /**
9117 * lpfc_extra_ring_setup - Extra ring setup function
9118 * @phba: Pointer to HBA context object.
9119 *
9120 * This function is called while driver attaches with the
9121 * HBA to setup the extra ring. The extra ring is used
9122 * only when driver needs to support target mode functionality
9123 * or IP over FC functionalities.
9124 *
9125 * This function is called with no lock held.
9126 **/
9127 static int
9128 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9129 {
9130 struct lpfc_sli *psli;
9131 struct lpfc_sli_ring *pring;
9132
9133 psli = &phba->sli;
9134
9135 /* Adjust cmd/rsp ring iocb entries more evenly */
9136
9137 /* Take some away from the FCP ring */
9138 pring = &psli->ring[psli->fcp_ring];
9139 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9140 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9141 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9142 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9143
9144 /* and give them to the extra ring */
9145 pring = &psli->ring[psli->extra_ring];
9146
9147 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9148 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9149 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9150 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9151
9152 /* Setup default profile for this ring */
9153 pring->iotag_max = 4096;
9154 pring->num_mask = 1;
9155 pring->prt[0].profile = 0; /* Mask 0 */
9156 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9157 pring->prt[0].type = phba->cfg_multi_ring_type;
9158 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9159 return 0;
9160 }
9161
9162 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9163 * @phba: Pointer to HBA context object.
9164 * @iocbq: Pointer to iocb object.
9165 *
9166 * The async_event handler calls this routine when it receives
9167 * an ASYNC_STATUS_CN event from the port. The port generates
9168 * this event when an Abort Sequence request to an rport fails
9169 * twice in succession. The abort could be originated by the
9170 * driver or by the port. The ABTS could have been for an ELS
9171 * or FCP IO. The port only generates this event when an ABTS
9172 * fails to complete after one retry.
9173 */
9174 static void
9175 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9176 struct lpfc_iocbq *iocbq)
9177 {
9178 struct lpfc_nodelist *ndlp = NULL;
9179 uint16_t rpi = 0, vpi = 0;
9180 struct lpfc_vport *vport = NULL;
9181
9182 /* The rpi in the ulpContext is vport-sensitive. */
9183 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9184 rpi = iocbq->iocb.ulpContext;
9185
9186 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9187 "3092 Port generated ABTS async event "
9188 "on vpi %d rpi %d status 0x%x\n",
9189 vpi, rpi, iocbq->iocb.ulpStatus);
9190
9191 vport = lpfc_find_vport_by_vpid(phba, vpi);
9192 if (!vport)
9193 goto err_exit;
9194 ndlp = lpfc_findnode_rpi(vport, rpi);
9195 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9196 goto err_exit;
9197
9198 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9199 lpfc_sli_abts_recover_port(vport, ndlp);
9200 return;
9201
9202 err_exit:
9203 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9204 "3095 Event Context not found, no "
9205 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9206 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9207 vpi, rpi);
9208 }
9209
9210 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9211 * @phba: pointer to HBA context object.
9212 * @ndlp: nodelist pointer for the impacted rport.
9213 * @axri: pointer to the wcqe containing the failed exchange.
9214 *
9215 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9216 * port. The port generates this event when an abort exchange request to an
9217 * rport fails twice in succession with no reply. The abort could be originated
9218 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9219 */
9220 void
9221 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9222 struct lpfc_nodelist *ndlp,
9223 struct sli4_wcqe_xri_aborted *axri)
9224 {
9225 struct lpfc_vport *vport;
9226 uint32_t ext_status = 0;
9227
9228 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9229 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9230 "3115 Node Context not found, driver "
9231 "ignoring abts err event\n");
9232 return;
9233 }
9234
9235 vport = ndlp->vport;
9236 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9237 "3116 Port generated FCP XRI ABORT event on "
9238 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9239 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9240 bf_get(lpfc_wcqe_xa_xri, axri),
9241 bf_get(lpfc_wcqe_xa_status, axri),
9242 axri->parameter);
9243
9244 /*
9245 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9246 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9247 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9248 */
9249 ext_status = axri->parameter & IOERR_PARAM_MASK;
9250 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9251 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9252 lpfc_sli_abts_recover_port(vport, ndlp);
9253 }
9254
9255 /**
9256 * lpfc_sli_async_event_handler - ASYNC iocb handler function
9257 * @phba: Pointer to HBA context object.
9258 * @pring: Pointer to driver SLI ring object.
9259 * @iocbq: Pointer to iocb object.
9260 *
9261 * This function is called by the slow ring event handler
9262 * function when there is an ASYNC event iocb in the ring.
9263 * This function is called with no lock held.
9264 * Currently this function handles only temperature related
9265 * ASYNC events. The function decodes the temperature sensor
9266 * event message and posts events for the management applications.
9267 **/
9268 static void
9269 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9270 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9271 {
9272 IOCB_t *icmd;
9273 uint16_t evt_code;
9274 struct temp_event temp_event_data;
9275 struct Scsi_Host *shost;
9276 uint32_t *iocb_w;
9277
9278 icmd = &iocbq->iocb;
9279 evt_code = icmd->un.asyncstat.evt_code;
9280
9281 switch (evt_code) {
9282 case ASYNC_TEMP_WARN:
9283 case ASYNC_TEMP_SAFE:
9284 temp_event_data.data = (uint32_t) icmd->ulpContext;
9285 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9286 if (evt_code == ASYNC_TEMP_WARN) {
9287 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9288 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9289 "0347 Adapter is very hot, please take "
9290 "corrective action. temperature : %d Celsius\n",
9291 (uint32_t) icmd->ulpContext);
9292 } else {
9293 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9294 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9295 "0340 Adapter temperature is OK now. "
9296 "temperature : %d Celsius\n",
9297 (uint32_t) icmd->ulpContext);
9298 }
9299
9300 /* Send temperature change event to applications */
9301 shost = lpfc_shost_from_vport(phba->pport);
9302 fc_host_post_vendor_event(shost, fc_get_event_number(),
9303 sizeof(temp_event_data), (char *) &temp_event_data,
9304 LPFC_NL_VENDOR_ID);
9305 break;
9306 case ASYNC_STATUS_CN:
9307 lpfc_sli_abts_err_handler(phba, iocbq);
9308 break;
9309 default:
9310 iocb_w = (uint32_t *) icmd;
9311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9312 "0346 Ring %d handler: unexpected ASYNC_STATUS"
9313 " evt_code 0x%x\n"
9314 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9315 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9316 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9317 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9318 pring->ringno, icmd->un.asyncstat.evt_code,
9319 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9320 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9321 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9322 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9323
9324 break;
9325 }
9326 }
9327
9328
9329 /**
9330 * lpfc_sli_setup - SLI ring setup function
9331 * @phba: Pointer to HBA context object.
9332 *
9333 * lpfc_sli_setup sets up rings of the SLI interface with
9334 * number of iocbs per ring and iotags. This function is
9335 * called while driver attach to the HBA and before the
9336 * interrupts are enabled. So there is no need for locking.
9337 *
9338 * This function always returns 0.
9339 **/
9340 int
9341 lpfc_sli_setup(struct lpfc_hba *phba)
9342 {
9343 int i, totiocbsize = 0;
9344 struct lpfc_sli *psli = &phba->sli;
9345 struct lpfc_sli_ring *pring;
9346
9347 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9348 if (phba->sli_rev == LPFC_SLI_REV4)
9349 psli->num_rings += phba->cfg_fcp_io_channel;
9350 psli->sli_flag = 0;
9351 psli->fcp_ring = LPFC_FCP_RING;
9352 psli->next_ring = LPFC_FCP_NEXT_RING;
9353 psli->extra_ring = LPFC_EXTRA_RING;
9354
9355 psli->iocbq_lookup = NULL;
9356 psli->iocbq_lookup_len = 0;
9357 psli->last_iotag = 0;
9358
9359 for (i = 0; i < psli->num_rings; i++) {
9360 pring = &psli->ring[i];
9361 switch (i) {
9362 case LPFC_FCP_RING: /* ring 0 - FCP */
9363 /* numCiocb and numRiocb are used in config_port */
9364 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9365 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9366 pring->sli.sli3.numCiocb +=
9367 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9368 pring->sli.sli3.numRiocb +=
9369 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9370 pring->sli.sli3.numCiocb +=
9371 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9372 pring->sli.sli3.numRiocb +=
9373 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9374 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9375 SLI3_IOCB_CMD_SIZE :
9376 SLI2_IOCB_CMD_SIZE;
9377 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9378 SLI3_IOCB_RSP_SIZE :
9379 SLI2_IOCB_RSP_SIZE;
9380 pring->iotag_ctr = 0;
9381 pring->iotag_max =
9382 (phba->cfg_hba_queue_depth * 2);
9383 pring->fast_iotag = pring->iotag_max;
9384 pring->num_mask = 0;
9385 break;
9386 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
9387 /* numCiocb and numRiocb are used in config_port */
9388 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9389 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9390 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9391 SLI3_IOCB_CMD_SIZE :
9392 SLI2_IOCB_CMD_SIZE;
9393 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9394 SLI3_IOCB_RSP_SIZE :
9395 SLI2_IOCB_RSP_SIZE;
9396 pring->iotag_max = phba->cfg_hba_queue_depth;
9397 pring->num_mask = 0;
9398 break;
9399 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9400 /* numCiocb and numRiocb are used in config_port */
9401 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9402 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9403 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9404 SLI3_IOCB_CMD_SIZE :
9405 SLI2_IOCB_CMD_SIZE;
9406 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9407 SLI3_IOCB_RSP_SIZE :
9408 SLI2_IOCB_RSP_SIZE;
9409 pring->fast_iotag = 0;
9410 pring->iotag_ctr = 0;
9411 pring->iotag_max = 4096;
9412 pring->lpfc_sli_rcv_async_status =
9413 lpfc_sli_async_event_handler;
9414 pring->num_mask = LPFC_MAX_RING_MASK;
9415 pring->prt[0].profile = 0; /* Mask 0 */
9416 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9417 pring->prt[0].type = FC_TYPE_ELS;
9418 pring->prt[0].lpfc_sli_rcv_unsol_event =
9419 lpfc_els_unsol_event;
9420 pring->prt[1].profile = 0; /* Mask 1 */
9421 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9422 pring->prt[1].type = FC_TYPE_ELS;
9423 pring->prt[1].lpfc_sli_rcv_unsol_event =
9424 lpfc_els_unsol_event;
9425 pring->prt[2].profile = 0; /* Mask 2 */
9426 /* NameServer Inquiry */
9427 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9428 /* NameServer */
9429 pring->prt[2].type = FC_TYPE_CT;
9430 pring->prt[2].lpfc_sli_rcv_unsol_event =
9431 lpfc_ct_unsol_event;
9432 pring->prt[3].profile = 0; /* Mask 3 */
9433 /* NameServer response */
9434 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9435 /* NameServer */
9436 pring->prt[3].type = FC_TYPE_CT;
9437 pring->prt[3].lpfc_sli_rcv_unsol_event =
9438 lpfc_ct_unsol_event;
9439 break;
9440 }
9441 totiocbsize += (pring->sli.sli3.numCiocb *
9442 pring->sli.sli3.sizeCiocb) +
9443 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9444 }
9445 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9446 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9447 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9448 "SLI2 SLIM Data: x%x x%lx\n",
9449 phba->brd_no, totiocbsize,
9450 (unsigned long) MAX_SLIM_IOCB_SIZE);
9451 }
9452 if (phba->cfg_multi_ring_support == 2)
9453 lpfc_extra_ring_setup(phba);
9454
9455 return 0;
9456 }
9457
9458 /**
9459 * lpfc_sli_queue_setup - Queue initialization function
9460 * @phba: Pointer to HBA context object.
9461 *
9462 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9463 * ring. This function also initializes ring indices of each ring.
9464 * This function is called during the initialization of the SLI
9465 * interface of an HBA.
9466 * This function is called with no lock held and always returns
9467 * 1.
9468 **/
9469 int
9470 lpfc_sli_queue_setup(struct lpfc_hba *phba)
9471 {
9472 struct lpfc_sli *psli;
9473 struct lpfc_sli_ring *pring;
9474 int i;
9475
9476 psli = &phba->sli;
9477 spin_lock_irq(&phba->hbalock);
9478 INIT_LIST_HEAD(&psli->mboxq);
9479 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9480 /* Initialize list headers for txq and txcmplq as double linked lists */
9481 for (i = 0; i < psli->num_rings; i++) {
9482 pring = &psli->ring[i];
9483 pring->ringno = i;
9484 pring->sli.sli3.next_cmdidx = 0;
9485 pring->sli.sli3.local_getidx = 0;
9486 pring->sli.sli3.cmdidx = 0;
9487 pring->flag = 0;
9488 INIT_LIST_HEAD(&pring->txq);
9489 INIT_LIST_HEAD(&pring->txcmplq);
9490 INIT_LIST_HEAD(&pring->iocb_continueq);
9491 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9492 INIT_LIST_HEAD(&pring->postbufq);
9493 spin_lock_init(&pring->ring_lock);
9494 }
9495 spin_unlock_irq(&phba->hbalock);
9496 return 1;
9497 }
9498
9499 /**
9500 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9501 * @phba: Pointer to HBA context object.
9502 *
9503 * This routine flushes the mailbox command subsystem. It will unconditionally
9504 * flush all the mailbox commands in the three possible stages in the mailbox
9505 * command sub-system: pending mailbox command queue; the outstanding mailbox
9506 * command; and completed mailbox command queue. It is caller's responsibility
9507 * to make sure that the driver is in the proper state to flush the mailbox
9508 * command sub-system. Namely, the posting of mailbox commands into the
9509 * pending mailbox command queue from the various clients must be stopped;
9510 * either the HBA is in a state that it will never works on the outstanding
9511 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9512 * mailbox command has been completed.
9513 **/
9514 static void
9515 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9516 {
9517 LIST_HEAD(completions);
9518 struct lpfc_sli *psli = &phba->sli;
9519 LPFC_MBOXQ_t *pmb;
9520 unsigned long iflag;
9521
9522 /* Flush all the mailbox commands in the mbox system */
9523 spin_lock_irqsave(&phba->hbalock, iflag);
9524 /* The pending mailbox command queue */
9525 list_splice_init(&phba->sli.mboxq, &completions);
9526 /* The outstanding active mailbox command */
9527 if (psli->mbox_active) {
9528 list_add_tail(&psli->mbox_active->list, &completions);
9529 psli->mbox_active = NULL;
9530 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9531 }
9532 /* The completed mailbox command queue */
9533 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9534 spin_unlock_irqrestore(&phba->hbalock, iflag);
9535
9536 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9537 while (!list_empty(&completions)) {
9538 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9539 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9540 if (pmb->mbox_cmpl)
9541 pmb->mbox_cmpl(phba, pmb);
9542 }
9543 }
9544
9545 /**
9546 * lpfc_sli_host_down - Vport cleanup function
9547 * @vport: Pointer to virtual port object.
9548 *
9549 * lpfc_sli_host_down is called to clean up the resources
9550 * associated with a vport before destroying virtual
9551 * port data structures.
9552 * This function does following operations:
9553 * - Free discovery resources associated with this virtual
9554 * port.
9555 * - Free iocbs associated with this virtual port in
9556 * the txq.
9557 * - Send abort for all iocb commands associated with this
9558 * vport in txcmplq.
9559 *
9560 * This function is called with no lock held and always returns 1.
9561 **/
9562 int
9563 lpfc_sli_host_down(struct lpfc_vport *vport)
9564 {
9565 LIST_HEAD(completions);
9566 struct lpfc_hba *phba = vport->phba;
9567 struct lpfc_sli *psli = &phba->sli;
9568 struct lpfc_sli_ring *pring;
9569 struct lpfc_iocbq *iocb, *next_iocb;
9570 int i;
9571 unsigned long flags = 0;
9572 uint16_t prev_pring_flag;
9573
9574 lpfc_cleanup_discovery_resources(vport);
9575
9576 spin_lock_irqsave(&phba->hbalock, flags);
9577 for (i = 0; i < psli->num_rings; i++) {
9578 pring = &psli->ring[i];
9579 prev_pring_flag = pring->flag;
9580 /* Only slow rings */
9581 if (pring->ringno == LPFC_ELS_RING) {
9582 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9583 /* Set the lpfc data pending flag */
9584 set_bit(LPFC_DATA_READY, &phba->data_flags);
9585 }
9586 /*
9587 * Error everything on the txq since these iocbs have not been
9588 * given to the FW yet.
9589 */
9590 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9591 if (iocb->vport != vport)
9592 continue;
9593 list_move_tail(&iocb->list, &completions);
9594 }
9595
9596 /* Next issue ABTS for everything on the txcmplq */
9597 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9598 list) {
9599 if (iocb->vport != vport)
9600 continue;
9601 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9602 }
9603
9604 pring->flag = prev_pring_flag;
9605 }
9606
9607 spin_unlock_irqrestore(&phba->hbalock, flags);
9608
9609 /* Cancel all the IOCBs from the completions list */
9610 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9611 IOERR_SLI_DOWN);
9612 return 1;
9613 }
9614
9615 /**
9616 * lpfc_sli_hba_down - Resource cleanup function for the HBA
9617 * @phba: Pointer to HBA context object.
9618 *
9619 * This function cleans up all iocb, buffers, mailbox commands
9620 * while shutting down the HBA. This function is called with no
9621 * lock held and always returns 1.
9622 * This function does the following to cleanup driver resources:
9623 * - Free discovery resources for each virtual port
9624 * - Cleanup any pending fabric iocbs
9625 * - Iterate through the iocb txq and free each entry
9626 * in the list.
9627 * - Free up any buffer posted to the HBA
9628 * - Free mailbox commands in the mailbox queue.
9629 **/
9630 int
9631 lpfc_sli_hba_down(struct lpfc_hba *phba)
9632 {
9633 LIST_HEAD(completions);
9634 struct lpfc_sli *psli = &phba->sli;
9635 struct lpfc_sli_ring *pring;
9636 struct lpfc_dmabuf *buf_ptr;
9637 unsigned long flags = 0;
9638 int i;
9639
9640 /* Shutdown the mailbox command sub-system */
9641 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9642
9643 lpfc_hba_down_prep(phba);
9644
9645 lpfc_fabric_abort_hba(phba);
9646
9647 spin_lock_irqsave(&phba->hbalock, flags);
9648 for (i = 0; i < psli->num_rings; i++) {
9649 pring = &psli->ring[i];
9650 /* Only slow rings */
9651 if (pring->ringno == LPFC_ELS_RING) {
9652 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9653 /* Set the lpfc data pending flag */
9654 set_bit(LPFC_DATA_READY, &phba->data_flags);
9655 }
9656
9657 /*
9658 * Error everything on the txq since these iocbs have not been
9659 * given to the FW yet.
9660 */
9661 list_splice_init(&pring->txq, &completions);
9662 }
9663 spin_unlock_irqrestore(&phba->hbalock, flags);
9664
9665 /* Cancel all the IOCBs from the completions list */
9666 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9667 IOERR_SLI_DOWN);
9668
9669 spin_lock_irqsave(&phba->hbalock, flags);
9670 list_splice_init(&phba->elsbuf, &completions);
9671 phba->elsbuf_cnt = 0;
9672 phba->elsbuf_prev_cnt = 0;
9673 spin_unlock_irqrestore(&phba->hbalock, flags);
9674
9675 while (!list_empty(&completions)) {
9676 list_remove_head(&completions, buf_ptr,
9677 struct lpfc_dmabuf, list);
9678 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9679 kfree(buf_ptr);
9680 }
9681
9682 /* Return any active mbox cmds */
9683 del_timer_sync(&psli->mbox_tmo);
9684
9685 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9686 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9687 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9688
9689 return 1;
9690 }
9691
9692 /**
9693 * lpfc_sli_pcimem_bcopy - SLI memory copy function
9694 * @srcp: Source memory pointer.
9695 * @destp: Destination memory pointer.
9696 * @cnt: Number of words required to be copied.
9697 *
9698 * This function is used for copying data between driver memory
9699 * and the SLI memory. This function also changes the endianness
9700 * of each word if native endianness is different from SLI
9701 * endianness. This function can be called with or without
9702 * lock.
9703 **/
9704 void
9705 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9706 {
9707 uint32_t *src = srcp;
9708 uint32_t *dest = destp;
9709 uint32_t ldata;
9710 int i;
9711
9712 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9713 ldata = *src;
9714 ldata = le32_to_cpu(ldata);
9715 *dest = ldata;
9716 src++;
9717 dest++;
9718 }
9719 }
9720
9721
9722 /**
9723 * lpfc_sli_bemem_bcopy - SLI memory copy function
9724 * @srcp: Source memory pointer.
9725 * @destp: Destination memory pointer.
9726 * @cnt: Number of words required to be copied.
9727 *
9728 * This function is used for copying data between a data structure
9729 * with big endian representation to local endianness.
9730 * This function can be called with or without lock.
9731 **/
9732 void
9733 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9734 {
9735 uint32_t *src = srcp;
9736 uint32_t *dest = destp;
9737 uint32_t ldata;
9738 int i;
9739
9740 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9741 ldata = *src;
9742 ldata = be32_to_cpu(ldata);
9743 *dest = ldata;
9744 src++;
9745 dest++;
9746 }
9747 }
9748
9749 /**
9750 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9751 * @phba: Pointer to HBA context object.
9752 * @pring: Pointer to driver SLI ring object.
9753 * @mp: Pointer to driver buffer object.
9754 *
9755 * This function is called with no lock held.
9756 * It always return zero after adding the buffer to the postbufq
9757 * buffer list.
9758 **/
9759 int
9760 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9761 struct lpfc_dmabuf *mp)
9762 {
9763 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9764 later */
9765 spin_lock_irq(&phba->hbalock);
9766 list_add_tail(&mp->list, &pring->postbufq);
9767 pring->postbufq_cnt++;
9768 spin_unlock_irq(&phba->hbalock);
9769 return 0;
9770 }
9771
9772 /**
9773 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9774 * @phba: Pointer to HBA context object.
9775 *
9776 * When HBQ is enabled, buffers are searched based on tags. This function
9777 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9778 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9779 * does not conflict with tags of buffer posted for unsolicited events.
9780 * The function returns the allocated tag. The function is called with
9781 * no locks held.
9782 **/
9783 uint32_t
9784 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9785 {
9786 spin_lock_irq(&phba->hbalock);
9787 phba->buffer_tag_count++;
9788 /*
9789 * Always set the QUE_BUFTAG_BIT to distiguish between
9790 * a tag assigned by HBQ.
9791 */
9792 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9793 spin_unlock_irq(&phba->hbalock);
9794 return phba->buffer_tag_count;
9795 }
9796
9797 /**
9798 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9799 * @phba: Pointer to HBA context object.
9800 * @pring: Pointer to driver SLI ring object.
9801 * @tag: Buffer tag.
9802 *
9803 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9804 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9805 * iocb is posted to the response ring with the tag of the buffer.
9806 * This function searches the pring->postbufq list using the tag
9807 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9808 * iocb. If the buffer is found then lpfc_dmabuf object of the
9809 * buffer is returned to the caller else NULL is returned.
9810 * This function is called with no lock held.
9811 **/
9812 struct lpfc_dmabuf *
9813 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9814 uint32_t tag)
9815 {
9816 struct lpfc_dmabuf *mp, *next_mp;
9817 struct list_head *slp = &pring->postbufq;
9818
9819 /* Search postbufq, from the beginning, looking for a match on tag */
9820 spin_lock_irq(&phba->hbalock);
9821 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9822 if (mp->buffer_tag == tag) {
9823 list_del_init(&mp->list);
9824 pring->postbufq_cnt--;
9825 spin_unlock_irq(&phba->hbalock);
9826 return mp;
9827 }
9828 }
9829
9830 spin_unlock_irq(&phba->hbalock);
9831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9832 "0402 Cannot find virtual addr for buffer tag on "
9833 "ring %d Data x%lx x%p x%p x%x\n",
9834 pring->ringno, (unsigned long) tag,
9835 slp->next, slp->prev, pring->postbufq_cnt);
9836
9837 return NULL;
9838 }
9839
9840 /**
9841 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9842 * @phba: Pointer to HBA context object.
9843 * @pring: Pointer to driver SLI ring object.
9844 * @phys: DMA address of the buffer.
9845 *
9846 * This function searches the buffer list using the dma_address
9847 * of unsolicited event to find the driver's lpfc_dmabuf object
9848 * corresponding to the dma_address. The function returns the
9849 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9850 * This function is called by the ct and els unsolicited event
9851 * handlers to get the buffer associated with the unsolicited
9852 * event.
9853 *
9854 * This function is called with no lock held.
9855 **/
9856 struct lpfc_dmabuf *
9857 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9858 dma_addr_t phys)
9859 {
9860 struct lpfc_dmabuf *mp, *next_mp;
9861 struct list_head *slp = &pring->postbufq;
9862
9863 /* Search postbufq, from the beginning, looking for a match on phys */
9864 spin_lock_irq(&phba->hbalock);
9865 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9866 if (mp->phys == phys) {
9867 list_del_init(&mp->list);
9868 pring->postbufq_cnt--;
9869 spin_unlock_irq(&phba->hbalock);
9870 return mp;
9871 }
9872 }
9873
9874 spin_unlock_irq(&phba->hbalock);
9875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9876 "0410 Cannot find virtual addr for mapped buf on "
9877 "ring %d Data x%llx x%p x%p x%x\n",
9878 pring->ringno, (unsigned long long)phys,
9879 slp->next, slp->prev, pring->postbufq_cnt);
9880 return NULL;
9881 }
9882
9883 /**
9884 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9885 * @phba: Pointer to HBA context object.
9886 * @cmdiocb: Pointer to driver command iocb object.
9887 * @rspiocb: Pointer to driver response iocb object.
9888 *
9889 * This function is the completion handler for the abort iocbs for
9890 * ELS commands. This function is called from the ELS ring event
9891 * handler with no lock held. This function frees memory resources
9892 * associated with the abort iocb.
9893 **/
9894 static void
9895 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9896 struct lpfc_iocbq *rspiocb)
9897 {
9898 IOCB_t *irsp = &rspiocb->iocb;
9899 uint16_t abort_iotag, abort_context;
9900 struct lpfc_iocbq *abort_iocb = NULL;
9901
9902 if (irsp->ulpStatus) {
9903
9904 /*
9905 * Assume that the port already completed and returned, or
9906 * will return the iocb. Just Log the message.
9907 */
9908 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9909 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9910
9911 spin_lock_irq(&phba->hbalock);
9912 if (phba->sli_rev < LPFC_SLI_REV4) {
9913 if (abort_iotag != 0 &&
9914 abort_iotag <= phba->sli.last_iotag)
9915 abort_iocb =
9916 phba->sli.iocbq_lookup[abort_iotag];
9917 } else
9918 /* For sli4 the abort_tag is the XRI,
9919 * so the abort routine puts the iotag of the iocb
9920 * being aborted in the context field of the abort
9921 * IOCB.
9922 */
9923 abort_iocb = phba->sli.iocbq_lookup[abort_context];
9924
9925 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9926 "0327 Cannot abort els iocb %p "
9927 "with tag %x context %x, abort status %x, "
9928 "abort code %x\n",
9929 abort_iocb, abort_iotag, abort_context,
9930 irsp->ulpStatus, irsp->un.ulpWord[4]);
9931
9932 spin_unlock_irq(&phba->hbalock);
9933 }
9934 lpfc_sli_release_iocbq(phba, cmdiocb);
9935 return;
9936 }
9937
9938 /**
9939 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9940 * @phba: Pointer to HBA context object.
9941 * @cmdiocb: Pointer to driver command iocb object.
9942 * @rspiocb: Pointer to driver response iocb object.
9943 *
9944 * The function is called from SLI ring event handler with no
9945 * lock held. This function is the completion handler for ELS commands
9946 * which are aborted. The function frees memory resources used for
9947 * the aborted ELS commands.
9948 **/
9949 static void
9950 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9951 struct lpfc_iocbq *rspiocb)
9952 {
9953 IOCB_t *irsp = &rspiocb->iocb;
9954
9955 /* ELS cmd tag <ulpIoTag> completes */
9956 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9957 "0139 Ignoring ELS cmd tag x%x completion Data: "
9958 "x%x x%x x%x\n",
9959 irsp->ulpIoTag, irsp->ulpStatus,
9960 irsp->un.ulpWord[4], irsp->ulpTimeout);
9961 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9962 lpfc_ct_free_iocb(phba, cmdiocb);
9963 else
9964 lpfc_els_free_iocb(phba, cmdiocb);
9965 return;
9966 }
9967
9968 /**
9969 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9970 * @phba: Pointer to HBA context object.
9971 * @pring: Pointer to driver SLI ring object.
9972 * @cmdiocb: Pointer to driver command iocb object.
9973 *
9974 * This function issues an abort iocb for the provided command iocb down to
9975 * the port. Other than the case the outstanding command iocb is an abort
9976 * request, this function issues abort out unconditionally. This function is
9977 * called with hbalock held. The function returns 0 when it fails due to
9978 * memory allocation failure or when the command iocb is an abort request.
9979 **/
9980 static int
9981 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9982 struct lpfc_iocbq *cmdiocb)
9983 {
9984 struct lpfc_vport *vport = cmdiocb->vport;
9985 struct lpfc_iocbq *abtsiocbp;
9986 IOCB_t *icmd = NULL;
9987 IOCB_t *iabt = NULL;
9988 int ring_number;
9989 int retval;
9990 unsigned long iflags;
9991
9992 lockdep_assert_held(&phba->hbalock);
9993
9994 /*
9995 * There are certain command types we don't want to abort. And we
9996 * don't want to abort commands that are already in the process of
9997 * being aborted.
9998 */
9999 icmd = &cmdiocb->iocb;
10000 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10001 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10002 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10003 return 0;
10004
10005 /* issue ABTS for this IOCB based on iotag */
10006 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10007 if (abtsiocbp == NULL)
10008 return 0;
10009
10010 /* This signals the response to set the correct status
10011 * before calling the completion handler
10012 */
10013 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10014
10015 iabt = &abtsiocbp->iocb;
10016 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10017 iabt->un.acxri.abortContextTag = icmd->ulpContext;
10018 if (phba->sli_rev == LPFC_SLI_REV4) {
10019 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10020 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10021 }
10022 else
10023 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10024 iabt->ulpLe = 1;
10025 iabt->ulpClass = icmd->ulpClass;
10026
10027 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10028 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
10029 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10030 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10031 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10032 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10033
10034 if (phba->link_state >= LPFC_LINK_UP)
10035 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10036 else
10037 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10038
10039 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10040
10041 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10042 "0339 Abort xri x%x, original iotag x%x, "
10043 "abort cmd iotag x%x\n",
10044 iabt->un.acxri.abortIoTag,
10045 iabt->un.acxri.abortContextTag,
10046 abtsiocbp->iotag);
10047
10048 if (phba->sli_rev == LPFC_SLI_REV4) {
10049 ring_number =
10050 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10051 if (unlikely(ring_number == LPFC_HBA_ERROR))
10052 return 0;
10053 pring = &phba->sli.ring[ring_number];
10054 /* Note: both hbalock and ring_lock need to be set here */
10055 spin_lock_irqsave(&pring->ring_lock, iflags);
10056 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10057 abtsiocbp, 0);
10058 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10059 } else {
10060 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10061 abtsiocbp, 0);
10062 }
10063
10064 if (retval)
10065 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10066
10067 /*
10068 * Caller to this routine should check for IOCB_ERROR
10069 * and handle it properly. This routine no longer removes
10070 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10071 */
10072 return retval;
10073 }
10074
10075 /**
10076 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10077 * @phba: Pointer to HBA context object.
10078 * @pring: Pointer to driver SLI ring object.
10079 * @cmdiocb: Pointer to driver command iocb object.
10080 *
10081 * This function issues an abort iocb for the provided command iocb. In case
10082 * of unloading, the abort iocb will not be issued to commands on the ELS
10083 * ring. Instead, the callback function shall be changed to those commands
10084 * so that nothing happens when them finishes. This function is called with
10085 * hbalock held. The function returns 0 when the command iocb is an abort
10086 * request.
10087 **/
10088 int
10089 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10090 struct lpfc_iocbq *cmdiocb)
10091 {
10092 struct lpfc_vport *vport = cmdiocb->vport;
10093 int retval = IOCB_ERROR;
10094 IOCB_t *icmd = NULL;
10095
10096 lockdep_assert_held(&phba->hbalock);
10097
10098 /*
10099 * There are certain command types we don't want to abort. And we
10100 * don't want to abort commands that are already in the process of
10101 * being aborted.
10102 */
10103 icmd = &cmdiocb->iocb;
10104 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10105 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10106 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10107 return 0;
10108
10109 /*
10110 * If we're unloading, don't abort iocb on the ELS ring, but change
10111 * the callback so that nothing happens when it finishes.
10112 */
10113 if ((vport->load_flag & FC_UNLOADING) &&
10114 (pring->ringno == LPFC_ELS_RING)) {
10115 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10116 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10117 else
10118 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10119 goto abort_iotag_exit;
10120 }
10121
10122 /* Now, we try to issue the abort to the cmdiocb out */
10123 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10124
10125 abort_iotag_exit:
10126 /*
10127 * Caller to this routine should check for IOCB_ERROR
10128 * and handle it properly. This routine no longer removes
10129 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10130 */
10131 return retval;
10132 }
10133
10134 /**
10135 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10136 * @phba: pointer to lpfc HBA data structure.
10137 *
10138 * This routine will abort all pending and outstanding iocbs to an HBA.
10139 **/
10140 void
10141 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10142 {
10143 struct lpfc_sli *psli = &phba->sli;
10144 struct lpfc_sli_ring *pring;
10145 int i;
10146
10147 for (i = 0; i < psli->num_rings; i++) {
10148 pring = &psli->ring[i];
10149 lpfc_sli_abort_iocb_ring(phba, pring);
10150 }
10151 }
10152
10153 /**
10154 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
10155 * @iocbq: Pointer to driver iocb object.
10156 * @vport: Pointer to driver virtual port object.
10157 * @tgt_id: SCSI ID of the target.
10158 * @lun_id: LUN ID of the scsi device.
10159 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10160 *
10161 * This function acts as an iocb filter for functions which abort or count
10162 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10163 * 0 if the filtering criteria is met for the given iocb and will return
10164 * 1 if the filtering criteria is not met.
10165 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10166 * given iocb is for the SCSI device specified by vport, tgt_id and
10167 * lun_id parameter.
10168 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10169 * given iocb is for the SCSI target specified by vport and tgt_id
10170 * parameters.
10171 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10172 * given iocb is for the SCSI host associated with the given vport.
10173 * This function is called with no locks held.
10174 **/
10175 static int
10176 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10177 uint16_t tgt_id, uint64_t lun_id,
10178 lpfc_ctx_cmd ctx_cmd)
10179 {
10180 struct lpfc_scsi_buf *lpfc_cmd;
10181 int rc = 1;
10182
10183 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10184 return rc;
10185
10186 if (iocbq->vport != vport)
10187 return rc;
10188
10189 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10190
10191 if (lpfc_cmd->pCmd == NULL)
10192 return rc;
10193
10194 switch (ctx_cmd) {
10195 case LPFC_CTX_LUN:
10196 if ((lpfc_cmd->rdata->pnode) &&
10197 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10198 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
10199 rc = 0;
10200 break;
10201 case LPFC_CTX_TGT:
10202 if ((lpfc_cmd->rdata->pnode) &&
10203 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
10204 rc = 0;
10205 break;
10206 case LPFC_CTX_HOST:
10207 rc = 0;
10208 break;
10209 default:
10210 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
10211 __func__, ctx_cmd);
10212 break;
10213 }
10214
10215 return rc;
10216 }
10217
10218 /**
10219 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10220 * @vport: Pointer to virtual port.
10221 * @tgt_id: SCSI ID of the target.
10222 * @lun_id: LUN ID of the scsi device.
10223 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10224 *
10225 * This function returns number of FCP commands pending for the vport.
10226 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10227 * commands pending on the vport associated with SCSI device specified
10228 * by tgt_id and lun_id parameters.
10229 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10230 * commands pending on the vport associated with SCSI target specified
10231 * by tgt_id parameter.
10232 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10233 * commands pending on the vport.
10234 * This function returns the number of iocbs which satisfy the filter.
10235 * This function is called without any lock held.
10236 **/
10237 int
10238 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10239 lpfc_ctx_cmd ctx_cmd)
10240 {
10241 struct lpfc_hba *phba = vport->phba;
10242 struct lpfc_iocbq *iocbq;
10243 int sum, i;
10244
10245 spin_lock_irq(&phba->hbalock);
10246 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10247 iocbq = phba->sli.iocbq_lookup[i];
10248
10249 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10250 ctx_cmd) == 0)
10251 sum++;
10252 }
10253 spin_unlock_irq(&phba->hbalock);
10254
10255 return sum;
10256 }
10257
10258 /**
10259 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10260 * @phba: Pointer to HBA context object
10261 * @cmdiocb: Pointer to command iocb object.
10262 * @rspiocb: Pointer to response iocb object.
10263 *
10264 * This function is called when an aborted FCP iocb completes. This
10265 * function is called by the ring event handler with no lock held.
10266 * This function frees the iocb.
10267 **/
10268 void
10269 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10270 struct lpfc_iocbq *rspiocb)
10271 {
10272 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10273 "3096 ABORT_XRI_CN completing on rpi x%x "
10274 "original iotag x%x, abort cmd iotag x%x "
10275 "status 0x%x, reason 0x%x\n",
10276 cmdiocb->iocb.un.acxri.abortContextTag,
10277 cmdiocb->iocb.un.acxri.abortIoTag,
10278 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10279 rspiocb->iocb.un.ulpWord[4]);
10280 lpfc_sli_release_iocbq(phba, cmdiocb);
10281 return;
10282 }
10283
10284 /**
10285 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10286 * @vport: Pointer to virtual port.
10287 * @pring: Pointer to driver SLI ring object.
10288 * @tgt_id: SCSI ID of the target.
10289 * @lun_id: LUN ID of the scsi device.
10290 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10291 *
10292 * This function sends an abort command for every SCSI command
10293 * associated with the given virtual port pending on the ring
10294 * filtered by lpfc_sli_validate_fcp_iocb function.
10295 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10296 * FCP iocbs associated with lun specified by tgt_id and lun_id
10297 * parameters
10298 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10299 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10300 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10301 * FCP iocbs associated with virtual port.
10302 * This function returns number of iocbs it failed to abort.
10303 * This function is called with no locks held.
10304 **/
10305 int
10306 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10307 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10308 {
10309 struct lpfc_hba *phba = vport->phba;
10310 struct lpfc_iocbq *iocbq;
10311 struct lpfc_iocbq *abtsiocb;
10312 IOCB_t *cmd = NULL;
10313 int errcnt = 0, ret_val = 0;
10314 int i;
10315
10316 for (i = 1; i <= phba->sli.last_iotag; i++) {
10317 iocbq = phba->sli.iocbq_lookup[i];
10318
10319 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10320 abort_cmd) != 0)
10321 continue;
10322
10323 /*
10324 * If the iocbq is already being aborted, don't take a second
10325 * action, but do count it.
10326 */
10327 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10328 continue;
10329
10330 /* issue ABTS for this IOCB based on iotag */
10331 abtsiocb = lpfc_sli_get_iocbq(phba);
10332 if (abtsiocb == NULL) {
10333 errcnt++;
10334 continue;
10335 }
10336
10337 /* indicate the IO is being aborted by the driver. */
10338 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10339
10340 cmd = &iocbq->iocb;
10341 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10342 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10343 if (phba->sli_rev == LPFC_SLI_REV4)
10344 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10345 else
10346 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10347 abtsiocb->iocb.ulpLe = 1;
10348 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10349 abtsiocb->vport = vport;
10350
10351 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10352 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
10353 if (iocbq->iocb_flag & LPFC_IO_FCP)
10354 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10355 if (iocbq->iocb_flag & LPFC_IO_FOF)
10356 abtsiocb->iocb_flag |= LPFC_IO_FOF;
10357
10358 if (lpfc_is_link_up(phba))
10359 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10360 else
10361 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10362
10363 /* Setup callback routine and issue the command. */
10364 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10365 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10366 abtsiocb, 0);
10367 if (ret_val == IOCB_ERROR) {
10368 lpfc_sli_release_iocbq(phba, abtsiocb);
10369 errcnt++;
10370 continue;
10371 }
10372 }
10373
10374 return errcnt;
10375 }
10376
10377 /**
10378 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10379 * @vport: Pointer to virtual port.
10380 * @pring: Pointer to driver SLI ring object.
10381 * @tgt_id: SCSI ID of the target.
10382 * @lun_id: LUN ID of the scsi device.
10383 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10384 *
10385 * This function sends an abort command for every SCSI command
10386 * associated with the given virtual port pending on the ring
10387 * filtered by lpfc_sli_validate_fcp_iocb function.
10388 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10389 * FCP iocbs associated with lun specified by tgt_id and lun_id
10390 * parameters
10391 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10392 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10393 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10394 * FCP iocbs associated with virtual port.
10395 * This function returns number of iocbs it aborted .
10396 * This function is called with no locks held right after a taskmgmt
10397 * command is sent.
10398 **/
10399 int
10400 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10401 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10402 {
10403 struct lpfc_hba *phba = vport->phba;
10404 struct lpfc_scsi_buf *lpfc_cmd;
10405 struct lpfc_iocbq *abtsiocbq;
10406 struct lpfc_nodelist *ndlp;
10407 struct lpfc_iocbq *iocbq;
10408 IOCB_t *icmd;
10409 int sum, i, ret_val;
10410 unsigned long iflags;
10411 struct lpfc_sli_ring *pring_s4;
10412 uint32_t ring_number;
10413
10414 spin_lock_irq(&phba->hbalock);
10415
10416 /* all I/Os are in process of being flushed */
10417 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10418 spin_unlock_irq(&phba->hbalock);
10419 return 0;
10420 }
10421 sum = 0;
10422
10423 for (i = 1; i <= phba->sli.last_iotag; i++) {
10424 iocbq = phba->sli.iocbq_lookup[i];
10425
10426 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10427 cmd) != 0)
10428 continue;
10429
10430 /*
10431 * If the iocbq is already being aborted, don't take a second
10432 * action, but do count it.
10433 */
10434 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10435 continue;
10436
10437 /* issue ABTS for this IOCB based on iotag */
10438 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10439 if (abtsiocbq == NULL)
10440 continue;
10441
10442 icmd = &iocbq->iocb;
10443 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10444 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10445 if (phba->sli_rev == LPFC_SLI_REV4)
10446 abtsiocbq->iocb.un.acxri.abortIoTag =
10447 iocbq->sli4_xritag;
10448 else
10449 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10450 abtsiocbq->iocb.ulpLe = 1;
10451 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10452 abtsiocbq->vport = vport;
10453
10454 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10455 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10456 if (iocbq->iocb_flag & LPFC_IO_FCP)
10457 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
10458 if (iocbq->iocb_flag & LPFC_IO_FOF)
10459 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
10460
10461 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10462 ndlp = lpfc_cmd->rdata->pnode;
10463
10464 if (lpfc_is_link_up(phba) &&
10465 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
10466 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10467 else
10468 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10469
10470 /* Setup callback routine and issue the command. */
10471 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10472
10473 /*
10474 * Indicate the IO is being aborted by the driver and set
10475 * the caller's flag into the aborted IO.
10476 */
10477 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10478
10479 if (phba->sli_rev == LPFC_SLI_REV4) {
10480 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10481 iocbq->fcp_wqidx;
10482 pring_s4 = &phba->sli.ring[ring_number];
10483 /* Note: both hbalock and ring_lock must be set here */
10484 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10485 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10486 abtsiocbq, 0);
10487 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10488 } else {
10489 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10490 abtsiocbq, 0);
10491 }
10492
10493
10494 if (ret_val == IOCB_ERROR)
10495 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10496 else
10497 sum++;
10498 }
10499 spin_unlock_irq(&phba->hbalock);
10500 return sum;
10501 }
10502
10503 /**
10504 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
10505 * @phba: Pointer to HBA context object.
10506 * @cmdiocbq: Pointer to command iocb.
10507 * @rspiocbq: Pointer to response iocb.
10508 *
10509 * This function is the completion handler for iocbs issued using
10510 * lpfc_sli_issue_iocb_wait function. This function is called by the
10511 * ring event handler function without any lock held. This function
10512 * can be called from both worker thread context and interrupt
10513 * context. This function also can be called from other thread which
10514 * cleans up the SLI layer objects.
10515 * This function copy the contents of the response iocb to the
10516 * response iocb memory object provided by the caller of
10517 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10518 * sleeps for the iocb completion.
10519 **/
10520 static void
10521 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10522 struct lpfc_iocbq *cmdiocbq,
10523 struct lpfc_iocbq *rspiocbq)
10524 {
10525 wait_queue_head_t *pdone_q;
10526 unsigned long iflags;
10527 struct lpfc_scsi_buf *lpfc_cmd;
10528
10529 spin_lock_irqsave(&phba->hbalock, iflags);
10530 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10531
10532 /*
10533 * A time out has occurred for the iocb. If a time out
10534 * completion handler has been supplied, call it. Otherwise,
10535 * just free the iocbq.
10536 */
10537
10538 spin_unlock_irqrestore(&phba->hbalock, iflags);
10539 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10540 cmdiocbq->wait_iocb_cmpl = NULL;
10541 if (cmdiocbq->iocb_cmpl)
10542 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10543 else
10544 lpfc_sli_release_iocbq(phba, cmdiocbq);
10545 return;
10546 }
10547
10548 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10549 if (cmdiocbq->context2 && rspiocbq)
10550 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10551 &rspiocbq->iocb, sizeof(IOCB_t));
10552
10553 /* Set the exchange busy flag for task management commands */
10554 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10555 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10556 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10557 cur_iocbq);
10558 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10559 }
10560
10561 pdone_q = cmdiocbq->context_un.wait_queue;
10562 if (pdone_q)
10563 wake_up(pdone_q);
10564 spin_unlock_irqrestore(&phba->hbalock, iflags);
10565 return;
10566 }
10567
10568 /**
10569 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10570 * @phba: Pointer to HBA context object..
10571 * @piocbq: Pointer to command iocb.
10572 * @flag: Flag to test.
10573 *
10574 * This routine grabs the hbalock and then test the iocb_flag to
10575 * see if the passed in flag is set.
10576 * Returns:
10577 * 1 if flag is set.
10578 * 0 if flag is not set.
10579 **/
10580 static int
10581 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10582 struct lpfc_iocbq *piocbq, uint32_t flag)
10583 {
10584 unsigned long iflags;
10585 int ret;
10586
10587 spin_lock_irqsave(&phba->hbalock, iflags);
10588 ret = piocbq->iocb_flag & flag;
10589 spin_unlock_irqrestore(&phba->hbalock, iflags);
10590 return ret;
10591
10592 }
10593
10594 /**
10595 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
10596 * @phba: Pointer to HBA context object..
10597 * @pring: Pointer to sli ring.
10598 * @piocb: Pointer to command iocb.
10599 * @prspiocbq: Pointer to response iocb.
10600 * @timeout: Timeout in number of seconds.
10601 *
10602 * This function issues the iocb to firmware and waits for the
10603 * iocb to complete. The iocb_cmpl field of the shall be used
10604 * to handle iocbs which time out. If the field is NULL, the
10605 * function shall free the iocbq structure. If more clean up is
10606 * needed, the caller is expected to provide a completion function
10607 * that will provide the needed clean up. If the iocb command is
10608 * not completed within timeout seconds, the function will either
10609 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10610 * completion function set in the iocb_cmpl field and then return
10611 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10612 * resources if this function returns IOCB_TIMEDOUT.
10613 * The function waits for the iocb completion using an
10614 * non-interruptible wait.
10615 * This function will sleep while waiting for iocb completion.
10616 * So, this function should not be called from any context which
10617 * does not allow sleeping. Due to the same reason, this function
10618 * cannot be called with interrupt disabled.
10619 * This function assumes that the iocb completions occur while
10620 * this function sleep. So, this function cannot be called from
10621 * the thread which process iocb completion for this ring.
10622 * This function clears the iocb_flag of the iocb object before
10623 * issuing the iocb and the iocb completion handler sets this
10624 * flag and wakes this thread when the iocb completes.
10625 * The contents of the response iocb will be copied to prspiocbq
10626 * by the completion handler when the command completes.
10627 * This function returns IOCB_SUCCESS when success.
10628 * This function is called with no lock held.
10629 **/
10630 int
10631 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
10632 uint32_t ring_number,
10633 struct lpfc_iocbq *piocb,
10634 struct lpfc_iocbq *prspiocbq,
10635 uint32_t timeout)
10636 {
10637 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10638 long timeleft, timeout_req = 0;
10639 int retval = IOCB_SUCCESS;
10640 uint32_t creg_val;
10641 struct lpfc_iocbq *iocb;
10642 int txq_cnt = 0;
10643 int txcmplq_cnt = 0;
10644 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10645 unsigned long iflags;
10646 bool iocb_completed = true;
10647
10648 /*
10649 * If the caller has provided a response iocbq buffer, then context2
10650 * is NULL or its an error.
10651 */
10652 if (prspiocbq) {
10653 if (piocb->context2)
10654 return IOCB_ERROR;
10655 piocb->context2 = prspiocbq;
10656 }
10657
10658 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
10659 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10660 piocb->context_un.wait_queue = &done_q;
10661 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
10662
10663 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10664 if (lpfc_readl(phba->HCregaddr, &creg_val))
10665 return IOCB_ERROR;
10666 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10667 writel(creg_val, phba->HCregaddr);
10668 readl(phba->HCregaddr); /* flush */
10669 }
10670
10671 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10672 SLI_IOCB_RET_IOCB);
10673 if (retval == IOCB_SUCCESS) {
10674 timeout_req = msecs_to_jiffies(timeout * 1000);
10675 timeleft = wait_event_timeout(done_q,
10676 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
10677 timeout_req);
10678 spin_lock_irqsave(&phba->hbalock, iflags);
10679 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10680
10681 /*
10682 * IOCB timed out. Inform the wake iocb wait
10683 * completion function and set local status
10684 */
10685
10686 iocb_completed = false;
10687 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10688 }
10689 spin_unlock_irqrestore(&phba->hbalock, iflags);
10690 if (iocb_completed) {
10691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10692 "0331 IOCB wake signaled\n");
10693 /* Note: we are not indicating if the IOCB has a success
10694 * status or not - that's for the caller to check.
10695 * IOCB_SUCCESS means just that the command was sent and
10696 * completed. Not that it completed successfully.
10697 * */
10698 } else if (timeleft == 0) {
10699 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10700 "0338 IOCB wait timeout error - no "
10701 "wake response Data x%x\n", timeout);
10702 retval = IOCB_TIMEDOUT;
10703 } else {
10704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10705 "0330 IOCB wake NOT set, "
10706 "Data x%x x%lx\n",
10707 timeout, (timeleft / jiffies));
10708 retval = IOCB_TIMEDOUT;
10709 }
10710 } else if (retval == IOCB_BUSY) {
10711 if (phba->cfg_log_verbose & LOG_SLI) {
10712 list_for_each_entry(iocb, &pring->txq, list) {
10713 txq_cnt++;
10714 }
10715 list_for_each_entry(iocb, &pring->txcmplq, list) {
10716 txcmplq_cnt++;
10717 }
10718 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10719 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10720 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10721 }
10722 return retval;
10723 } else {
10724 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10725 "0332 IOCB wait issue failed, Data x%x\n",
10726 retval);
10727 retval = IOCB_ERROR;
10728 }
10729
10730 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10731 if (lpfc_readl(phba->HCregaddr, &creg_val))
10732 return IOCB_ERROR;
10733 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10734 writel(creg_val, phba->HCregaddr);
10735 readl(phba->HCregaddr); /* flush */
10736 }
10737
10738 if (prspiocbq)
10739 piocb->context2 = NULL;
10740
10741 piocb->context_un.wait_queue = NULL;
10742 piocb->iocb_cmpl = NULL;
10743 return retval;
10744 }
10745
10746 /**
10747 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10748 * @phba: Pointer to HBA context object.
10749 * @pmboxq: Pointer to driver mailbox object.
10750 * @timeout: Timeout in number of seconds.
10751 *
10752 * This function issues the mailbox to firmware and waits for the
10753 * mailbox command to complete. If the mailbox command is not
10754 * completed within timeout seconds, it returns MBX_TIMEOUT.
10755 * The function waits for the mailbox completion using an
10756 * interruptible wait. If the thread is woken up due to a
10757 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10758 * should not free the mailbox resources, if this function returns
10759 * MBX_TIMEOUT.
10760 * This function will sleep while waiting for mailbox completion.
10761 * So, this function should not be called from any context which
10762 * does not allow sleeping. Due to the same reason, this function
10763 * cannot be called with interrupt disabled.
10764 * This function assumes that the mailbox completion occurs while
10765 * this function sleep. So, this function cannot be called from
10766 * the worker thread which processes mailbox completion.
10767 * This function is called in the context of HBA management
10768 * applications.
10769 * This function returns MBX_SUCCESS when successful.
10770 * This function is called with no lock held.
10771 **/
10772 int
10773 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10774 uint32_t timeout)
10775 {
10776 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10777 MAILBOX_t *mb = NULL;
10778 int retval;
10779 unsigned long flag;
10780
10781 /* The caller might set context1 for extended buffer */
10782 if (pmboxq->context1)
10783 mb = (MAILBOX_t *)pmboxq->context1;
10784
10785 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10786 /* setup wake call as IOCB callback */
10787 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10788 /* setup context field to pass wait_queue pointer to wake function */
10789 pmboxq->context1 = &done_q;
10790
10791 /* now issue the command */
10792 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10793 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10794 wait_event_interruptible_timeout(done_q,
10795 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10796 msecs_to_jiffies(timeout * 1000));
10797
10798 spin_lock_irqsave(&phba->hbalock, flag);
10799 /* restore the possible extended buffer for free resource */
10800 pmboxq->context1 = (uint8_t *)mb;
10801 /*
10802 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10803 * else do not free the resources.
10804 */
10805 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10806 retval = MBX_SUCCESS;
10807 } else {
10808 retval = MBX_TIMEOUT;
10809 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10810 }
10811 spin_unlock_irqrestore(&phba->hbalock, flag);
10812 } else {
10813 /* restore the possible extended buffer for free resource */
10814 pmboxq->context1 = (uint8_t *)mb;
10815 }
10816
10817 return retval;
10818 }
10819
10820 /**
10821 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10822 * @phba: Pointer to HBA context.
10823 *
10824 * This function is called to shutdown the driver's mailbox sub-system.
10825 * It first marks the mailbox sub-system is in a block state to prevent
10826 * the asynchronous mailbox command from issued off the pending mailbox
10827 * command queue. If the mailbox command sub-system shutdown is due to
10828 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10829 * the mailbox sub-system flush routine to forcefully bring down the
10830 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10831 * as with offline or HBA function reset), this routine will wait for the
10832 * outstanding mailbox command to complete before invoking the mailbox
10833 * sub-system flush routine to gracefully bring down mailbox sub-system.
10834 **/
10835 void
10836 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10837 {
10838 struct lpfc_sli *psli = &phba->sli;
10839 unsigned long timeout;
10840
10841 if (mbx_action == LPFC_MBX_NO_WAIT) {
10842 /* delay 100ms for port state */
10843 msleep(100);
10844 lpfc_sli_mbox_sys_flush(phba);
10845 return;
10846 }
10847 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10848
10849 spin_lock_irq(&phba->hbalock);
10850 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10851
10852 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10853 /* Determine how long we might wait for the active mailbox
10854 * command to be gracefully completed by firmware.
10855 */
10856 if (phba->sli.mbox_active)
10857 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10858 phba->sli.mbox_active) *
10859 1000) + jiffies;
10860 spin_unlock_irq(&phba->hbalock);
10861
10862 while (phba->sli.mbox_active) {
10863 /* Check active mailbox complete status every 2ms */
10864 msleep(2);
10865 if (time_after(jiffies, timeout))
10866 /* Timeout, let the mailbox flush routine to
10867 * forcefully release active mailbox command
10868 */
10869 break;
10870 }
10871 } else
10872 spin_unlock_irq(&phba->hbalock);
10873
10874 lpfc_sli_mbox_sys_flush(phba);
10875 }
10876
10877 /**
10878 * lpfc_sli_eratt_read - read sli-3 error attention events
10879 * @phba: Pointer to HBA context.
10880 *
10881 * This function is called to read the SLI3 device error attention registers
10882 * for possible error attention events. The caller must hold the hostlock
10883 * with spin_lock_irq().
10884 *
10885 * This function returns 1 when there is Error Attention in the Host Attention
10886 * Register and returns 0 otherwise.
10887 **/
10888 static int
10889 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10890 {
10891 uint32_t ha_copy;
10892
10893 /* Read chip Host Attention (HA) register */
10894 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10895 goto unplug_err;
10896
10897 if (ha_copy & HA_ERATT) {
10898 /* Read host status register to retrieve error event */
10899 if (lpfc_sli_read_hs(phba))
10900 goto unplug_err;
10901
10902 /* Check if there is a deferred error condition is active */
10903 if ((HS_FFER1 & phba->work_hs) &&
10904 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10905 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10906 phba->hba_flag |= DEFER_ERATT;
10907 /* Clear all interrupt enable conditions */
10908 writel(0, phba->HCregaddr);
10909 readl(phba->HCregaddr);
10910 }
10911
10912 /* Set the driver HA work bitmap */
10913 phba->work_ha |= HA_ERATT;
10914 /* Indicate polling handles this ERATT */
10915 phba->hba_flag |= HBA_ERATT_HANDLED;
10916 return 1;
10917 }
10918 return 0;
10919
10920 unplug_err:
10921 /* Set the driver HS work bitmap */
10922 phba->work_hs |= UNPLUG_ERR;
10923 /* Set the driver HA work bitmap */
10924 phba->work_ha |= HA_ERATT;
10925 /* Indicate polling handles this ERATT */
10926 phba->hba_flag |= HBA_ERATT_HANDLED;
10927 return 1;
10928 }
10929
10930 /**
10931 * lpfc_sli4_eratt_read - read sli-4 error attention events
10932 * @phba: Pointer to HBA context.
10933 *
10934 * This function is called to read the SLI4 device error attention registers
10935 * for possible error attention events. The caller must hold the hostlock
10936 * with spin_lock_irq().
10937 *
10938 * This function returns 1 when there is Error Attention in the Host Attention
10939 * Register and returns 0 otherwise.
10940 **/
10941 static int
10942 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10943 {
10944 uint32_t uerr_sta_hi, uerr_sta_lo;
10945 uint32_t if_type, portsmphr;
10946 struct lpfc_register portstat_reg;
10947
10948 /*
10949 * For now, use the SLI4 device internal unrecoverable error
10950 * registers for error attention. This can be changed later.
10951 */
10952 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10953 switch (if_type) {
10954 case LPFC_SLI_INTF_IF_TYPE_0:
10955 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10956 &uerr_sta_lo) ||
10957 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10958 &uerr_sta_hi)) {
10959 phba->work_hs |= UNPLUG_ERR;
10960 phba->work_ha |= HA_ERATT;
10961 phba->hba_flag |= HBA_ERATT_HANDLED;
10962 return 1;
10963 }
10964 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10965 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10967 "1423 HBA Unrecoverable error: "
10968 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10969 "ue_mask_lo_reg=0x%x, "
10970 "ue_mask_hi_reg=0x%x\n",
10971 uerr_sta_lo, uerr_sta_hi,
10972 phba->sli4_hba.ue_mask_lo,
10973 phba->sli4_hba.ue_mask_hi);
10974 phba->work_status[0] = uerr_sta_lo;
10975 phba->work_status[1] = uerr_sta_hi;
10976 phba->work_ha |= HA_ERATT;
10977 phba->hba_flag |= HBA_ERATT_HANDLED;
10978 return 1;
10979 }
10980 break;
10981 case LPFC_SLI_INTF_IF_TYPE_2:
10982 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10983 &portstat_reg.word0) ||
10984 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10985 &portsmphr)){
10986 phba->work_hs |= UNPLUG_ERR;
10987 phba->work_ha |= HA_ERATT;
10988 phba->hba_flag |= HBA_ERATT_HANDLED;
10989 return 1;
10990 }
10991 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10992 phba->work_status[0] =
10993 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10994 phba->work_status[1] =
10995 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10997 "2885 Port Status Event: "
10998 "port status reg 0x%x, "
10999 "port smphr reg 0x%x, "
11000 "error 1=0x%x, error 2=0x%x\n",
11001 portstat_reg.word0,
11002 portsmphr,
11003 phba->work_status[0],
11004 phba->work_status[1]);
11005 phba->work_ha |= HA_ERATT;
11006 phba->hba_flag |= HBA_ERATT_HANDLED;
11007 return 1;
11008 }
11009 break;
11010 case LPFC_SLI_INTF_IF_TYPE_1:
11011 default:
11012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11013 "2886 HBA Error Attention on unsupported "
11014 "if type %d.", if_type);
11015 return 1;
11016 }
11017
11018 return 0;
11019 }
11020
11021 /**
11022 * lpfc_sli_check_eratt - check error attention events
11023 * @phba: Pointer to HBA context.
11024 *
11025 * This function is called from timer soft interrupt context to check HBA's
11026 * error attention register bit for error attention events.
11027 *
11028 * This function returns 1 when there is Error Attention in the Host Attention
11029 * Register and returns 0 otherwise.
11030 **/
11031 int
11032 lpfc_sli_check_eratt(struct lpfc_hba *phba)
11033 {
11034 uint32_t ha_copy;
11035
11036 /* If somebody is waiting to handle an eratt, don't process it
11037 * here. The brdkill function will do this.
11038 */
11039 if (phba->link_flag & LS_IGNORE_ERATT)
11040 return 0;
11041
11042 /* Check if interrupt handler handles this ERATT */
11043 spin_lock_irq(&phba->hbalock);
11044 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11045 /* Interrupt handler has handled ERATT */
11046 spin_unlock_irq(&phba->hbalock);
11047 return 0;
11048 }
11049
11050 /*
11051 * If there is deferred error attention, do not check for error
11052 * attention
11053 */
11054 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11055 spin_unlock_irq(&phba->hbalock);
11056 return 0;
11057 }
11058
11059 /* If PCI channel is offline, don't process it */
11060 if (unlikely(pci_channel_offline(phba->pcidev))) {
11061 spin_unlock_irq(&phba->hbalock);
11062 return 0;
11063 }
11064
11065 switch (phba->sli_rev) {
11066 case LPFC_SLI_REV2:
11067 case LPFC_SLI_REV3:
11068 /* Read chip Host Attention (HA) register */
11069 ha_copy = lpfc_sli_eratt_read(phba);
11070 break;
11071 case LPFC_SLI_REV4:
11072 /* Read device Uncoverable Error (UERR) registers */
11073 ha_copy = lpfc_sli4_eratt_read(phba);
11074 break;
11075 default:
11076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11077 "0299 Invalid SLI revision (%d)\n",
11078 phba->sli_rev);
11079 ha_copy = 0;
11080 break;
11081 }
11082 spin_unlock_irq(&phba->hbalock);
11083
11084 return ha_copy;
11085 }
11086
11087 /**
11088 * lpfc_intr_state_check - Check device state for interrupt handling
11089 * @phba: Pointer to HBA context.
11090 *
11091 * This inline routine checks whether a device or its PCI slot is in a state
11092 * that the interrupt should be handled.
11093 *
11094 * This function returns 0 if the device or the PCI slot is in a state that
11095 * interrupt should be handled, otherwise -EIO.
11096 */
11097 static inline int
11098 lpfc_intr_state_check(struct lpfc_hba *phba)
11099 {
11100 /* If the pci channel is offline, ignore all the interrupts */
11101 if (unlikely(pci_channel_offline(phba->pcidev)))
11102 return -EIO;
11103
11104 /* Update device level interrupt statistics */
11105 phba->sli.slistat.sli_intr++;
11106
11107 /* Ignore all interrupts during initialization. */
11108 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11109 return -EIO;
11110
11111 return 0;
11112 }
11113
11114 /**
11115 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
11116 * @irq: Interrupt number.
11117 * @dev_id: The device context pointer.
11118 *
11119 * This function is directly called from the PCI layer as an interrupt
11120 * service routine when device with SLI-3 interface spec is enabled with
11121 * MSI-X multi-message interrupt mode and there are slow-path events in
11122 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11123 * interrupt mode, this function is called as part of the device-level
11124 * interrupt handler. When the PCI slot is in error recovery or the HBA
11125 * is undergoing initialization, the interrupt handler will not process
11126 * the interrupt. The link attention and ELS ring attention events are
11127 * handled by the worker thread. The interrupt handler signals the worker
11128 * thread and returns for these events. This function is called without
11129 * any lock held. It gets the hbalock to access and update SLI data
11130 * structures.
11131 *
11132 * This function returns IRQ_HANDLED when interrupt is handled else it
11133 * returns IRQ_NONE.
11134 **/
11135 irqreturn_t
11136 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
11137 {
11138 struct lpfc_hba *phba;
11139 uint32_t ha_copy, hc_copy;
11140 uint32_t work_ha_copy;
11141 unsigned long status;
11142 unsigned long iflag;
11143 uint32_t control;
11144
11145 MAILBOX_t *mbox, *pmbox;
11146 struct lpfc_vport *vport;
11147 struct lpfc_nodelist *ndlp;
11148 struct lpfc_dmabuf *mp;
11149 LPFC_MBOXQ_t *pmb;
11150 int rc;
11151
11152 /*
11153 * Get the driver's phba structure from the dev_id and
11154 * assume the HBA is not interrupting.
11155 */
11156 phba = (struct lpfc_hba *)dev_id;
11157
11158 if (unlikely(!phba))
11159 return IRQ_NONE;
11160
11161 /*
11162 * Stuff needs to be attented to when this function is invoked as an
11163 * individual interrupt handler in MSI-X multi-message interrupt mode
11164 */
11165 if (phba->intr_type == MSIX) {
11166 /* Check device state for handling interrupt */
11167 if (lpfc_intr_state_check(phba))
11168 return IRQ_NONE;
11169 /* Need to read HA REG for slow-path events */
11170 spin_lock_irqsave(&phba->hbalock, iflag);
11171 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11172 goto unplug_error;
11173 /* If somebody is waiting to handle an eratt don't process it
11174 * here. The brdkill function will do this.
11175 */
11176 if (phba->link_flag & LS_IGNORE_ERATT)
11177 ha_copy &= ~HA_ERATT;
11178 /* Check the need for handling ERATT in interrupt handler */
11179 if (ha_copy & HA_ERATT) {
11180 if (phba->hba_flag & HBA_ERATT_HANDLED)
11181 /* ERATT polling has handled ERATT */
11182 ha_copy &= ~HA_ERATT;
11183 else
11184 /* Indicate interrupt handler handles ERATT */
11185 phba->hba_flag |= HBA_ERATT_HANDLED;
11186 }
11187
11188 /*
11189 * If there is deferred error attention, do not check for any
11190 * interrupt.
11191 */
11192 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11193 spin_unlock_irqrestore(&phba->hbalock, iflag);
11194 return IRQ_NONE;
11195 }
11196
11197 /* Clear up only attention source related to slow-path */
11198 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11199 goto unplug_error;
11200
11201 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11202 HC_LAINT_ENA | HC_ERINT_ENA),
11203 phba->HCregaddr);
11204 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11205 phba->HAregaddr);
11206 writel(hc_copy, phba->HCregaddr);
11207 readl(phba->HAregaddr); /* flush */
11208 spin_unlock_irqrestore(&phba->hbalock, iflag);
11209 } else
11210 ha_copy = phba->ha_copy;
11211
11212 work_ha_copy = ha_copy & phba->work_ha_mask;
11213
11214 if (work_ha_copy) {
11215 if (work_ha_copy & HA_LATT) {
11216 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11217 /*
11218 * Turn off Link Attention interrupts
11219 * until CLEAR_LA done
11220 */
11221 spin_lock_irqsave(&phba->hbalock, iflag);
11222 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11223 if (lpfc_readl(phba->HCregaddr, &control))
11224 goto unplug_error;
11225 control &= ~HC_LAINT_ENA;
11226 writel(control, phba->HCregaddr);
11227 readl(phba->HCregaddr); /* flush */
11228 spin_unlock_irqrestore(&phba->hbalock, iflag);
11229 }
11230 else
11231 work_ha_copy &= ~HA_LATT;
11232 }
11233
11234 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11235 /*
11236 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11237 * the only slow ring.
11238 */
11239 status = (work_ha_copy &
11240 (HA_RXMASK << (4*LPFC_ELS_RING)));
11241 status >>= (4*LPFC_ELS_RING);
11242 if (status & HA_RXMASK) {
11243 spin_lock_irqsave(&phba->hbalock, iflag);
11244 if (lpfc_readl(phba->HCregaddr, &control))
11245 goto unplug_error;
11246
11247 lpfc_debugfs_slow_ring_trc(phba,
11248 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11249 control, status,
11250 (uint32_t)phba->sli.slistat.sli_intr);
11251
11252 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11253 lpfc_debugfs_slow_ring_trc(phba,
11254 "ISR Disable ring:"
11255 "pwork:x%x hawork:x%x wait:x%x",
11256 phba->work_ha, work_ha_copy,
11257 (uint32_t)((unsigned long)
11258 &phba->work_waitq));
11259
11260 control &=
11261 ~(HC_R0INT_ENA << LPFC_ELS_RING);
11262 writel(control, phba->HCregaddr);
11263 readl(phba->HCregaddr); /* flush */
11264 }
11265 else {
11266 lpfc_debugfs_slow_ring_trc(phba,
11267 "ISR slow ring: pwork:"
11268 "x%x hawork:x%x wait:x%x",
11269 phba->work_ha, work_ha_copy,
11270 (uint32_t)((unsigned long)
11271 &phba->work_waitq));
11272 }
11273 spin_unlock_irqrestore(&phba->hbalock, iflag);
11274 }
11275 }
11276 spin_lock_irqsave(&phba->hbalock, iflag);
11277 if (work_ha_copy & HA_ERATT) {
11278 if (lpfc_sli_read_hs(phba))
11279 goto unplug_error;
11280 /*
11281 * Check if there is a deferred error condition
11282 * is active
11283 */
11284 if ((HS_FFER1 & phba->work_hs) &&
11285 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11286 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11287 phba->work_hs)) {
11288 phba->hba_flag |= DEFER_ERATT;
11289 /* Clear all interrupt enable conditions */
11290 writel(0, phba->HCregaddr);
11291 readl(phba->HCregaddr);
11292 }
11293 }
11294
11295 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11296 pmb = phba->sli.mbox_active;
11297 pmbox = &pmb->u.mb;
11298 mbox = phba->mbox;
11299 vport = pmb->vport;
11300
11301 /* First check out the status word */
11302 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11303 if (pmbox->mbxOwner != OWN_HOST) {
11304 spin_unlock_irqrestore(&phba->hbalock, iflag);
11305 /*
11306 * Stray Mailbox Interrupt, mbxCommand <cmd>
11307 * mbxStatus <status>
11308 */
11309 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11310 LOG_SLI,
11311 "(%d):0304 Stray Mailbox "
11312 "Interrupt mbxCommand x%x "
11313 "mbxStatus x%x\n",
11314 (vport ? vport->vpi : 0),
11315 pmbox->mbxCommand,
11316 pmbox->mbxStatus);
11317 /* clear mailbox attention bit */
11318 work_ha_copy &= ~HA_MBATT;
11319 } else {
11320 phba->sli.mbox_active = NULL;
11321 spin_unlock_irqrestore(&phba->hbalock, iflag);
11322 phba->last_completion_time = jiffies;
11323 del_timer(&phba->sli.mbox_tmo);
11324 if (pmb->mbox_cmpl) {
11325 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11326 MAILBOX_CMD_SIZE);
11327 if (pmb->out_ext_byte_len &&
11328 pmb->context2)
11329 lpfc_sli_pcimem_bcopy(
11330 phba->mbox_ext,
11331 pmb->context2,
11332 pmb->out_ext_byte_len);
11333 }
11334 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11335 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11336
11337 lpfc_debugfs_disc_trc(vport,
11338 LPFC_DISC_TRC_MBOX_VPORT,
11339 "MBOX dflt rpi: : "
11340 "status:x%x rpi:x%x",
11341 (uint32_t)pmbox->mbxStatus,
11342 pmbox->un.varWords[0], 0);
11343
11344 if (!pmbox->mbxStatus) {
11345 mp = (struct lpfc_dmabuf *)
11346 (pmb->context1);
11347 ndlp = (struct lpfc_nodelist *)
11348 pmb->context2;
11349
11350 /* Reg_LOGIN of dflt RPI was
11351 * successful. new lets get
11352 * rid of the RPI using the
11353 * same mbox buffer.
11354 */
11355 lpfc_unreg_login(phba,
11356 vport->vpi,
11357 pmbox->un.varWords[0],
11358 pmb);
11359 pmb->mbox_cmpl =
11360 lpfc_mbx_cmpl_dflt_rpi;
11361 pmb->context1 = mp;
11362 pmb->context2 = ndlp;
11363 pmb->vport = vport;
11364 rc = lpfc_sli_issue_mbox(phba,
11365 pmb,
11366 MBX_NOWAIT);
11367 if (rc != MBX_BUSY)
11368 lpfc_printf_log(phba,
11369 KERN_ERR,
11370 LOG_MBOX | LOG_SLI,
11371 "0350 rc should have"
11372 "been MBX_BUSY\n");
11373 if (rc != MBX_NOT_FINISHED)
11374 goto send_current_mbox;
11375 }
11376 }
11377 spin_lock_irqsave(
11378 &phba->pport->work_port_lock,
11379 iflag);
11380 phba->pport->work_port_events &=
11381 ~WORKER_MBOX_TMO;
11382 spin_unlock_irqrestore(
11383 &phba->pport->work_port_lock,
11384 iflag);
11385 lpfc_mbox_cmpl_put(phba, pmb);
11386 }
11387 } else
11388 spin_unlock_irqrestore(&phba->hbalock, iflag);
11389
11390 if ((work_ha_copy & HA_MBATT) &&
11391 (phba->sli.mbox_active == NULL)) {
11392 send_current_mbox:
11393 /* Process next mailbox command if there is one */
11394 do {
11395 rc = lpfc_sli_issue_mbox(phba, NULL,
11396 MBX_NOWAIT);
11397 } while (rc == MBX_NOT_FINISHED);
11398 if (rc != MBX_SUCCESS)
11399 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11400 LOG_SLI, "0349 rc should be "
11401 "MBX_SUCCESS\n");
11402 }
11403
11404 spin_lock_irqsave(&phba->hbalock, iflag);
11405 phba->work_ha |= work_ha_copy;
11406 spin_unlock_irqrestore(&phba->hbalock, iflag);
11407 lpfc_worker_wake_up(phba);
11408 }
11409 return IRQ_HANDLED;
11410 unplug_error:
11411 spin_unlock_irqrestore(&phba->hbalock, iflag);
11412 return IRQ_HANDLED;
11413
11414 } /* lpfc_sli_sp_intr_handler */
11415
11416 /**
11417 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11418 * @irq: Interrupt number.
11419 * @dev_id: The device context pointer.
11420 *
11421 * This function is directly called from the PCI layer as an interrupt
11422 * service routine when device with SLI-3 interface spec is enabled with
11423 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11424 * ring event in the HBA. However, when the device is enabled with either
11425 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11426 * device-level interrupt handler. When the PCI slot is in error recovery
11427 * or the HBA is undergoing initialization, the interrupt handler will not
11428 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11429 * the intrrupt context. This function is called without any lock held.
11430 * It gets the hbalock to access and update SLI data structures.
11431 *
11432 * This function returns IRQ_HANDLED when interrupt is handled else it
11433 * returns IRQ_NONE.
11434 **/
11435 irqreturn_t
11436 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
11437 {
11438 struct lpfc_hba *phba;
11439 uint32_t ha_copy;
11440 unsigned long status;
11441 unsigned long iflag;
11442
11443 /* Get the driver's phba structure from the dev_id and
11444 * assume the HBA is not interrupting.
11445 */
11446 phba = (struct lpfc_hba *) dev_id;
11447
11448 if (unlikely(!phba))
11449 return IRQ_NONE;
11450
11451 /*
11452 * Stuff needs to be attented to when this function is invoked as an
11453 * individual interrupt handler in MSI-X multi-message interrupt mode
11454 */
11455 if (phba->intr_type == MSIX) {
11456 /* Check device state for handling interrupt */
11457 if (lpfc_intr_state_check(phba))
11458 return IRQ_NONE;
11459 /* Need to read HA REG for FCP ring and other ring events */
11460 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11461 return IRQ_HANDLED;
11462 /* Clear up only attention source related to fast-path */
11463 spin_lock_irqsave(&phba->hbalock, iflag);
11464 /*
11465 * If there is deferred error attention, do not check for
11466 * any interrupt.
11467 */
11468 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11469 spin_unlock_irqrestore(&phba->hbalock, iflag);
11470 return IRQ_NONE;
11471 }
11472 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11473 phba->HAregaddr);
11474 readl(phba->HAregaddr); /* flush */
11475 spin_unlock_irqrestore(&phba->hbalock, iflag);
11476 } else
11477 ha_copy = phba->ha_copy;
11478
11479 /*
11480 * Process all events on FCP ring. Take the optimized path for FCP IO.
11481 */
11482 ha_copy &= ~(phba->work_ha_mask);
11483
11484 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11485 status >>= (4*LPFC_FCP_RING);
11486 if (status & HA_RXMASK)
11487 lpfc_sli_handle_fast_ring_event(phba,
11488 &phba->sli.ring[LPFC_FCP_RING],
11489 status);
11490
11491 if (phba->cfg_multi_ring_support == 2) {
11492 /*
11493 * Process all events on extra ring. Take the optimized path
11494 * for extra ring IO.
11495 */
11496 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11497 status >>= (4*LPFC_EXTRA_RING);
11498 if (status & HA_RXMASK) {
11499 lpfc_sli_handle_fast_ring_event(phba,
11500 &phba->sli.ring[LPFC_EXTRA_RING],
11501 status);
11502 }
11503 }
11504 return IRQ_HANDLED;
11505 } /* lpfc_sli_fp_intr_handler */
11506
11507 /**
11508 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
11509 * @irq: Interrupt number.
11510 * @dev_id: The device context pointer.
11511 *
11512 * This function is the HBA device-level interrupt handler to device with
11513 * SLI-3 interface spec, called from the PCI layer when either MSI or
11514 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11515 * requires driver attention. This function invokes the slow-path interrupt
11516 * attention handling function and fast-path interrupt attention handling
11517 * function in turn to process the relevant HBA attention events. This
11518 * function is called without any lock held. It gets the hbalock to access
11519 * and update SLI data structures.
11520 *
11521 * This function returns IRQ_HANDLED when interrupt is handled, else it
11522 * returns IRQ_NONE.
11523 **/
11524 irqreturn_t
11525 lpfc_sli_intr_handler(int irq, void *dev_id)
11526 {
11527 struct lpfc_hba *phba;
11528 irqreturn_t sp_irq_rc, fp_irq_rc;
11529 unsigned long status1, status2;
11530 uint32_t hc_copy;
11531
11532 /*
11533 * Get the driver's phba structure from the dev_id and
11534 * assume the HBA is not interrupting.
11535 */
11536 phba = (struct lpfc_hba *) dev_id;
11537
11538 if (unlikely(!phba))
11539 return IRQ_NONE;
11540
11541 /* Check device state for handling interrupt */
11542 if (lpfc_intr_state_check(phba))
11543 return IRQ_NONE;
11544
11545 spin_lock(&phba->hbalock);
11546 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11547 spin_unlock(&phba->hbalock);
11548 return IRQ_HANDLED;
11549 }
11550
11551 if (unlikely(!phba->ha_copy)) {
11552 spin_unlock(&phba->hbalock);
11553 return IRQ_NONE;
11554 } else if (phba->ha_copy & HA_ERATT) {
11555 if (phba->hba_flag & HBA_ERATT_HANDLED)
11556 /* ERATT polling has handled ERATT */
11557 phba->ha_copy &= ~HA_ERATT;
11558 else
11559 /* Indicate interrupt handler handles ERATT */
11560 phba->hba_flag |= HBA_ERATT_HANDLED;
11561 }
11562
11563 /*
11564 * If there is deferred error attention, do not check for any interrupt.
11565 */
11566 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11567 spin_unlock(&phba->hbalock);
11568 return IRQ_NONE;
11569 }
11570
11571 /* Clear attention sources except link and error attentions */
11572 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11573 spin_unlock(&phba->hbalock);
11574 return IRQ_HANDLED;
11575 }
11576 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11577 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11578 phba->HCregaddr);
11579 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
11580 writel(hc_copy, phba->HCregaddr);
11581 readl(phba->HAregaddr); /* flush */
11582 spin_unlock(&phba->hbalock);
11583
11584 /*
11585 * Invokes slow-path host attention interrupt handling as appropriate.
11586 */
11587
11588 /* status of events with mailbox and link attention */
11589 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11590
11591 /* status of events with ELS ring */
11592 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11593 status2 >>= (4*LPFC_ELS_RING);
11594
11595 if (status1 || (status2 & HA_RXMASK))
11596 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
11597 else
11598 sp_irq_rc = IRQ_NONE;
11599
11600 /*
11601 * Invoke fast-path host attention interrupt handling as appropriate.
11602 */
11603
11604 /* status of events with FCP ring */
11605 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11606 status1 >>= (4*LPFC_FCP_RING);
11607
11608 /* status of events with extra ring */
11609 if (phba->cfg_multi_ring_support == 2) {
11610 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11611 status2 >>= (4*LPFC_EXTRA_RING);
11612 } else
11613 status2 = 0;
11614
11615 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
11616 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
11617 else
11618 fp_irq_rc = IRQ_NONE;
11619
11620 /* Return device-level interrupt handling status */
11621 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
11622 } /* lpfc_sli_intr_handler */
11623
11624 /**
11625 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11626 * @phba: pointer to lpfc hba data structure.
11627 *
11628 * This routine is invoked by the worker thread to process all the pending
11629 * SLI4 FCP abort XRI events.
11630 **/
11631 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11632 {
11633 struct lpfc_cq_event *cq_event;
11634
11635 /* First, declare the fcp xri abort event has been handled */
11636 spin_lock_irq(&phba->hbalock);
11637 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11638 spin_unlock_irq(&phba->hbalock);
11639 /* Now, handle all the fcp xri abort events */
11640 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11641 /* Get the first event from the head of the event queue */
11642 spin_lock_irq(&phba->hbalock);
11643 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11644 cq_event, struct lpfc_cq_event, list);
11645 spin_unlock_irq(&phba->hbalock);
11646 /* Notify aborted XRI for FCP work queue */
11647 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11648 /* Free the event processed back to the free pool */
11649 lpfc_sli4_cq_event_release(phba, cq_event);
11650 }
11651 }
11652
11653 /**
11654 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11655 * @phba: pointer to lpfc hba data structure.
11656 *
11657 * This routine is invoked by the worker thread to process all the pending
11658 * SLI4 els abort xri events.
11659 **/
11660 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11661 {
11662 struct lpfc_cq_event *cq_event;
11663
11664 /* First, declare the els xri abort event has been handled */
11665 spin_lock_irq(&phba->hbalock);
11666 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11667 spin_unlock_irq(&phba->hbalock);
11668 /* Now, handle all the els xri abort events */
11669 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11670 /* Get the first event from the head of the event queue */
11671 spin_lock_irq(&phba->hbalock);
11672 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11673 cq_event, struct lpfc_cq_event, list);
11674 spin_unlock_irq(&phba->hbalock);
11675 /* Notify aborted XRI for ELS work queue */
11676 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11677 /* Free the event processed back to the free pool */
11678 lpfc_sli4_cq_event_release(phba, cq_event);
11679 }
11680 }
11681
11682 /**
11683 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11684 * @phba: pointer to lpfc hba data structure
11685 * @pIocbIn: pointer to the rspiocbq
11686 * @pIocbOut: pointer to the cmdiocbq
11687 * @wcqe: pointer to the complete wcqe
11688 *
11689 * This routine transfers the fields of a command iocbq to a response iocbq
11690 * by copying all the IOCB fields from command iocbq and transferring the
11691 * completion status information from the complete wcqe.
11692 **/
11693 static void
11694 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11695 struct lpfc_iocbq *pIocbIn,
11696 struct lpfc_iocbq *pIocbOut,
11697 struct lpfc_wcqe_complete *wcqe)
11698 {
11699 int numBdes, i;
11700 unsigned long iflags;
11701 uint32_t status, max_response;
11702 struct lpfc_dmabuf *dmabuf;
11703 struct ulp_bde64 *bpl, bde;
11704 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11705
11706 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11707 sizeof(struct lpfc_iocbq) - offset);
11708 /* Map WCQE parameters into irspiocb parameters */
11709 status = bf_get(lpfc_wcqe_c_status, wcqe);
11710 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
11711 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11712 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11713 pIocbIn->iocb.un.fcpi.fcpi_parm =
11714 pIocbOut->iocb.un.fcpi.fcpi_parm -
11715 wcqe->total_data_placed;
11716 else
11717 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11718 else {
11719 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11720 switch (pIocbOut->iocb.ulpCommand) {
11721 case CMD_ELS_REQUEST64_CR:
11722 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11723 bpl = (struct ulp_bde64 *)dmabuf->virt;
11724 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11725 max_response = bde.tus.f.bdeSize;
11726 break;
11727 case CMD_GEN_REQUEST64_CR:
11728 max_response = 0;
11729 if (!pIocbOut->context3)
11730 break;
11731 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11732 sizeof(struct ulp_bde64);
11733 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11734 bpl = (struct ulp_bde64 *)dmabuf->virt;
11735 for (i = 0; i < numBdes; i++) {
11736 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11737 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11738 max_response += bde.tus.f.bdeSize;
11739 }
11740 break;
11741 default:
11742 max_response = wcqe->total_data_placed;
11743 break;
11744 }
11745 if (max_response < wcqe->total_data_placed)
11746 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11747 else
11748 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11749 wcqe->total_data_placed;
11750 }
11751
11752 /* Convert BG errors for completion status */
11753 if (status == CQE_STATUS_DI_ERROR) {
11754 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11755
11756 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11757 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11758 else
11759 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11760
11761 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11762 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11763 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11764 BGS_GUARD_ERR_MASK;
11765 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11766 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11767 BGS_APPTAG_ERR_MASK;
11768 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11769 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11770 BGS_REFTAG_ERR_MASK;
11771
11772 /* Check to see if there was any good data before the error */
11773 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11774 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11775 BGS_HI_WATER_MARK_PRESENT_MASK;
11776 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11777 wcqe->total_data_placed;
11778 }
11779
11780 /*
11781 * Set ALL the error bits to indicate we don't know what
11782 * type of error it is.
11783 */
11784 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11785 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11786 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11787 BGS_GUARD_ERR_MASK);
11788 }
11789
11790 /* Pick up HBA exchange busy condition */
11791 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11792 spin_lock_irqsave(&phba->hbalock, iflags);
11793 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11794 spin_unlock_irqrestore(&phba->hbalock, iflags);
11795 }
11796 }
11797
11798 /**
11799 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11800 * @phba: Pointer to HBA context object.
11801 * @wcqe: Pointer to work-queue completion queue entry.
11802 *
11803 * This routine handles an ELS work-queue completion event and construct
11804 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11805 * discovery engine to handle.
11806 *
11807 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11808 **/
11809 static struct lpfc_iocbq *
11810 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11811 struct lpfc_iocbq *irspiocbq)
11812 {
11813 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11814 struct lpfc_iocbq *cmdiocbq;
11815 struct lpfc_wcqe_complete *wcqe;
11816 unsigned long iflags;
11817
11818 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11819 spin_lock_irqsave(&pring->ring_lock, iflags);
11820 pring->stats.iocb_event++;
11821 /* Look up the ELS command IOCB and create pseudo response IOCB */
11822 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11823 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11824 /* Put the iocb back on the txcmplq */
11825 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
11826 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11827
11828 if (unlikely(!cmdiocbq)) {
11829 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11830 "0386 ELS complete with no corresponding "
11831 "cmdiocb: iotag (%d)\n",
11832 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11833 lpfc_sli_release_iocbq(phba, irspiocbq);
11834 return NULL;
11835 }
11836
11837 /* Fake the irspiocbq and copy necessary response information */
11838 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11839
11840 return irspiocbq;
11841 }
11842
11843 /**
11844 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11845 * @phba: Pointer to HBA context object.
11846 * @cqe: Pointer to mailbox completion queue entry.
11847 *
11848 * This routine process a mailbox completion queue entry with asynchrous
11849 * event.
11850 *
11851 * Return: true if work posted to worker thread, otherwise false.
11852 **/
11853 static bool
11854 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11855 {
11856 struct lpfc_cq_event *cq_event;
11857 unsigned long iflags;
11858
11859 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11860 "0392 Async Event: word0:x%x, word1:x%x, "
11861 "word2:x%x, word3:x%x\n", mcqe->word0,
11862 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11863
11864 /* Allocate a new internal CQ_EVENT entry */
11865 cq_event = lpfc_sli4_cq_event_alloc(phba);
11866 if (!cq_event) {
11867 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11868 "0394 Failed to allocate CQ_EVENT entry\n");
11869 return false;
11870 }
11871
11872 /* Move the CQE into an asynchronous event entry */
11873 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11874 spin_lock_irqsave(&phba->hbalock, iflags);
11875 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11876 /* Set the async event flag */
11877 phba->hba_flag |= ASYNC_EVENT;
11878 spin_unlock_irqrestore(&phba->hbalock, iflags);
11879
11880 return true;
11881 }
11882
11883 /**
11884 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11885 * @phba: Pointer to HBA context object.
11886 * @cqe: Pointer to mailbox completion queue entry.
11887 *
11888 * This routine process a mailbox completion queue entry with mailbox
11889 * completion event.
11890 *
11891 * Return: true if work posted to worker thread, otherwise false.
11892 **/
11893 static bool
11894 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11895 {
11896 uint32_t mcqe_status;
11897 MAILBOX_t *mbox, *pmbox;
11898 struct lpfc_mqe *mqe;
11899 struct lpfc_vport *vport;
11900 struct lpfc_nodelist *ndlp;
11901 struct lpfc_dmabuf *mp;
11902 unsigned long iflags;
11903 LPFC_MBOXQ_t *pmb;
11904 bool workposted = false;
11905 int rc;
11906
11907 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11908 if (!bf_get(lpfc_trailer_completed, mcqe))
11909 goto out_no_mqe_complete;
11910
11911 /* Get the reference to the active mbox command */
11912 spin_lock_irqsave(&phba->hbalock, iflags);
11913 pmb = phba->sli.mbox_active;
11914 if (unlikely(!pmb)) {
11915 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11916 "1832 No pending MBOX command to handle\n");
11917 spin_unlock_irqrestore(&phba->hbalock, iflags);
11918 goto out_no_mqe_complete;
11919 }
11920 spin_unlock_irqrestore(&phba->hbalock, iflags);
11921 mqe = &pmb->u.mqe;
11922 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11923 mbox = phba->mbox;
11924 vport = pmb->vport;
11925
11926 /* Reset heartbeat timer */
11927 phba->last_completion_time = jiffies;
11928 del_timer(&phba->sli.mbox_tmo);
11929
11930 /* Move mbox data to caller's mailbox region, do endian swapping */
11931 if (pmb->mbox_cmpl && mbox)
11932 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11933
11934 /*
11935 * For mcqe errors, conditionally move a modified error code to
11936 * the mbox so that the error will not be missed.
11937 */
11938 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11939 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11940 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11941 bf_set(lpfc_mqe_status, mqe,
11942 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11943 }
11944 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11945 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11946 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11947 "MBOX dflt rpi: status:x%x rpi:x%x",
11948 mcqe_status,
11949 pmbox->un.varWords[0], 0);
11950 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11951 mp = (struct lpfc_dmabuf *)(pmb->context1);
11952 ndlp = (struct lpfc_nodelist *)pmb->context2;
11953 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11954 * RID of the PPI using the same mbox buffer.
11955 */
11956 lpfc_unreg_login(phba, vport->vpi,
11957 pmbox->un.varWords[0], pmb);
11958 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11959 pmb->context1 = mp;
11960 pmb->context2 = ndlp;
11961 pmb->vport = vport;
11962 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11963 if (rc != MBX_BUSY)
11964 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11965 LOG_SLI, "0385 rc should "
11966 "have been MBX_BUSY\n");
11967 if (rc != MBX_NOT_FINISHED)
11968 goto send_current_mbox;
11969 }
11970 }
11971 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11972 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11973 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11974
11975 /* There is mailbox completion work to do */
11976 spin_lock_irqsave(&phba->hbalock, iflags);
11977 __lpfc_mbox_cmpl_put(phba, pmb);
11978 phba->work_ha |= HA_MBATT;
11979 spin_unlock_irqrestore(&phba->hbalock, iflags);
11980 workposted = true;
11981
11982 send_current_mbox:
11983 spin_lock_irqsave(&phba->hbalock, iflags);
11984 /* Release the mailbox command posting token */
11985 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11986 /* Setting active mailbox pointer need to be in sync to flag clear */
11987 phba->sli.mbox_active = NULL;
11988 spin_unlock_irqrestore(&phba->hbalock, iflags);
11989 /* Wake up worker thread to post the next pending mailbox command */
11990 lpfc_worker_wake_up(phba);
11991 out_no_mqe_complete:
11992 if (bf_get(lpfc_trailer_consumed, mcqe))
11993 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11994 return workposted;
11995 }
11996
11997 /**
11998 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11999 * @phba: Pointer to HBA context object.
12000 * @cqe: Pointer to mailbox completion queue entry.
12001 *
12002 * This routine process a mailbox completion queue entry, it invokes the
12003 * proper mailbox complete handling or asynchrous event handling routine
12004 * according to the MCQE's async bit.
12005 *
12006 * Return: true if work posted to worker thread, otherwise false.
12007 **/
12008 static bool
12009 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12010 {
12011 struct lpfc_mcqe mcqe;
12012 bool workposted;
12013
12014 /* Copy the mailbox MCQE and convert endian order as needed */
12015 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12016
12017 /* Invoke the proper event handling routine */
12018 if (!bf_get(lpfc_trailer_async, &mcqe))
12019 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12020 else
12021 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12022 return workposted;
12023 }
12024
12025 /**
12026 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12027 * @phba: Pointer to HBA context object.
12028 * @cq: Pointer to associated CQ
12029 * @wcqe: Pointer to work-queue completion queue entry.
12030 *
12031 * This routine handles an ELS work-queue completion event.
12032 *
12033 * Return: true if work posted to worker thread, otherwise false.
12034 **/
12035 static bool
12036 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12037 struct lpfc_wcqe_complete *wcqe)
12038 {
12039 struct lpfc_iocbq *irspiocbq;
12040 unsigned long iflags;
12041 struct lpfc_sli_ring *pring = cq->pring;
12042 int txq_cnt = 0;
12043 int txcmplq_cnt = 0;
12044 int fcp_txcmplq_cnt = 0;
12045
12046 /* Get an irspiocbq for later ELS response processing use */
12047 irspiocbq = lpfc_sli_get_iocbq(phba);
12048 if (!irspiocbq) {
12049 if (!list_empty(&pring->txq))
12050 txq_cnt++;
12051 if (!list_empty(&pring->txcmplq))
12052 txcmplq_cnt++;
12053 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12054 fcp_txcmplq_cnt++;
12055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12056 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12057 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12058 txq_cnt, phba->iocb_cnt,
12059 fcp_txcmplq_cnt,
12060 txcmplq_cnt);
12061 return false;
12062 }
12063
12064 /* Save off the slow-path queue event for work thread to process */
12065 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12066 spin_lock_irqsave(&phba->hbalock, iflags);
12067 list_add_tail(&irspiocbq->cq_event.list,
12068 &phba->sli4_hba.sp_queue_event);
12069 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12070 spin_unlock_irqrestore(&phba->hbalock, iflags);
12071
12072 return true;
12073 }
12074
12075 /**
12076 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12077 * @phba: Pointer to HBA context object.
12078 * @wcqe: Pointer to work-queue completion queue entry.
12079 *
12080 * This routine handles slow-path WQ entry comsumed event by invoking the
12081 * proper WQ release routine to the slow-path WQ.
12082 **/
12083 static void
12084 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12085 struct lpfc_wcqe_release *wcqe)
12086 {
12087 /* sanity check on queue memory */
12088 if (unlikely(!phba->sli4_hba.els_wq))
12089 return;
12090 /* Check for the slow-path ELS work queue */
12091 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12092 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12093 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12094 else
12095 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12096 "2579 Slow-path wqe consume event carries "
12097 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12098 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12099 phba->sli4_hba.els_wq->queue_id);
12100 }
12101
12102 /**
12103 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12104 * @phba: Pointer to HBA context object.
12105 * @cq: Pointer to a WQ completion queue.
12106 * @wcqe: Pointer to work-queue completion queue entry.
12107 *
12108 * This routine handles an XRI abort event.
12109 *
12110 * Return: true if work posted to worker thread, otherwise false.
12111 **/
12112 static bool
12113 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12114 struct lpfc_queue *cq,
12115 struct sli4_wcqe_xri_aborted *wcqe)
12116 {
12117 bool workposted = false;
12118 struct lpfc_cq_event *cq_event;
12119 unsigned long iflags;
12120
12121 /* Allocate a new internal CQ_EVENT entry */
12122 cq_event = lpfc_sli4_cq_event_alloc(phba);
12123 if (!cq_event) {
12124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12125 "0602 Failed to allocate CQ_EVENT entry\n");
12126 return false;
12127 }
12128
12129 /* Move the CQE into the proper xri abort event list */
12130 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12131 switch (cq->subtype) {
12132 case LPFC_FCP:
12133 spin_lock_irqsave(&phba->hbalock, iflags);
12134 list_add_tail(&cq_event->list,
12135 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12136 /* Set the fcp xri abort event flag */
12137 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12138 spin_unlock_irqrestore(&phba->hbalock, iflags);
12139 workposted = true;
12140 break;
12141 case LPFC_ELS:
12142 spin_lock_irqsave(&phba->hbalock, iflags);
12143 list_add_tail(&cq_event->list,
12144 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12145 /* Set the els xri abort event flag */
12146 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12147 spin_unlock_irqrestore(&phba->hbalock, iflags);
12148 workposted = true;
12149 break;
12150 default:
12151 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12152 "0603 Invalid work queue CQE subtype (x%x)\n",
12153 cq->subtype);
12154 workposted = false;
12155 break;
12156 }
12157 return workposted;
12158 }
12159
12160 /**
12161 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12162 * @phba: Pointer to HBA context object.
12163 * @rcqe: Pointer to receive-queue completion queue entry.
12164 *
12165 * This routine process a receive-queue completion queue entry.
12166 *
12167 * Return: true if work posted to worker thread, otherwise false.
12168 **/
12169 static bool
12170 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12171 {
12172 bool workposted = false;
12173 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12174 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12175 struct hbq_dmabuf *dma_buf;
12176 uint32_t status, rq_id;
12177 unsigned long iflags;
12178
12179 /* sanity check on queue memory */
12180 if (unlikely(!hrq) || unlikely(!drq))
12181 return workposted;
12182
12183 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12184 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12185 else
12186 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12187 if (rq_id != hrq->queue_id)
12188 goto out;
12189
12190 status = bf_get(lpfc_rcqe_status, rcqe);
12191 switch (status) {
12192 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12194 "2537 Receive Frame Truncated!!\n");
12195 hrq->RQ_buf_trunc++;
12196 case FC_STATUS_RQ_SUCCESS:
12197 lpfc_sli4_rq_release(hrq, drq);
12198 spin_lock_irqsave(&phba->hbalock, iflags);
12199 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12200 if (!dma_buf) {
12201 hrq->RQ_no_buf_found++;
12202 spin_unlock_irqrestore(&phba->hbalock, iflags);
12203 goto out;
12204 }
12205 hrq->RQ_rcv_buf++;
12206 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12207 /* save off the frame for the word thread to process */
12208 list_add_tail(&dma_buf->cq_event.list,
12209 &phba->sli4_hba.sp_queue_event);
12210 /* Frame received */
12211 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12212 spin_unlock_irqrestore(&phba->hbalock, iflags);
12213 workposted = true;
12214 break;
12215 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12216 case FC_STATUS_INSUFF_BUF_FRM_DISC:
12217 hrq->RQ_no_posted_buf++;
12218 /* Post more buffers if possible */
12219 spin_lock_irqsave(&phba->hbalock, iflags);
12220 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12221 spin_unlock_irqrestore(&phba->hbalock, iflags);
12222 workposted = true;
12223 break;
12224 }
12225 out:
12226 return workposted;
12227 }
12228
12229 /**
12230 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12231 * @phba: Pointer to HBA context object.
12232 * @cq: Pointer to the completion queue.
12233 * @wcqe: Pointer to a completion queue entry.
12234 *
12235 * This routine process a slow-path work-queue or receive queue completion queue
12236 * entry.
12237 *
12238 * Return: true if work posted to worker thread, otherwise false.
12239 **/
12240 static bool
12241 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12242 struct lpfc_cqe *cqe)
12243 {
12244 struct lpfc_cqe cqevt;
12245 bool workposted = false;
12246
12247 /* Copy the work queue CQE and convert endian order if needed */
12248 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12249
12250 /* Check and process for different type of WCQE and dispatch */
12251 switch (bf_get(lpfc_cqe_code, &cqevt)) {
12252 case CQE_CODE_COMPL_WQE:
12253 /* Process the WQ/RQ complete event */
12254 phba->last_completion_time = jiffies;
12255 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12256 (struct lpfc_wcqe_complete *)&cqevt);
12257 break;
12258 case CQE_CODE_RELEASE_WQE:
12259 /* Process the WQ release event */
12260 lpfc_sli4_sp_handle_rel_wcqe(phba,
12261 (struct lpfc_wcqe_release *)&cqevt);
12262 break;
12263 case CQE_CODE_XRI_ABORTED:
12264 /* Process the WQ XRI abort event */
12265 phba->last_completion_time = jiffies;
12266 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12267 (struct sli4_wcqe_xri_aborted *)&cqevt);
12268 break;
12269 case CQE_CODE_RECEIVE:
12270 case CQE_CODE_RECEIVE_V1:
12271 /* Process the RQ event */
12272 phba->last_completion_time = jiffies;
12273 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12274 (struct lpfc_rcqe *)&cqevt);
12275 break;
12276 default:
12277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12278 "0388 Not a valid WCQE code: x%x\n",
12279 bf_get(lpfc_cqe_code, &cqevt));
12280 break;
12281 }
12282 return workposted;
12283 }
12284
12285 /**
12286 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12287 * @phba: Pointer to HBA context object.
12288 * @eqe: Pointer to fast-path event queue entry.
12289 *
12290 * This routine process a event queue entry from the slow-path event queue.
12291 * It will check the MajorCode and MinorCode to determine this is for a
12292 * completion event on a completion queue, if not, an error shall be logged
12293 * and just return. Otherwise, it will get to the corresponding completion
12294 * queue and process all the entries on that completion queue, rearm the
12295 * completion queue, and then return.
12296 *
12297 **/
12298 static void
12299 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12300 struct lpfc_queue *speq)
12301 {
12302 struct lpfc_queue *cq = NULL, *childq;
12303 struct lpfc_cqe *cqe;
12304 bool workposted = false;
12305 int ecount = 0;
12306 uint16_t cqid;
12307
12308 /* Get the reference to the corresponding CQ */
12309 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12310
12311 list_for_each_entry(childq, &speq->child_list, list) {
12312 if (childq->queue_id == cqid) {
12313 cq = childq;
12314 break;
12315 }
12316 }
12317 if (unlikely(!cq)) {
12318 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12319 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12320 "0365 Slow-path CQ identifier "
12321 "(%d) does not exist\n", cqid);
12322 return;
12323 }
12324
12325 /* Process all the entries to the CQ */
12326 switch (cq->type) {
12327 case LPFC_MCQ:
12328 while ((cqe = lpfc_sli4_cq_get(cq))) {
12329 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12330 if (!(++ecount % cq->entry_repost))
12331 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12332 cq->CQ_mbox++;
12333 }
12334 break;
12335 case LPFC_WCQ:
12336 while ((cqe = lpfc_sli4_cq_get(cq))) {
12337 if (cq->subtype == LPFC_FCP)
12338 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12339 cqe);
12340 else
12341 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12342 cqe);
12343 if (!(++ecount % cq->entry_repost))
12344 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12345 }
12346
12347 /* Track the max number of CQEs processed in 1 EQ */
12348 if (ecount > cq->CQ_max_cqe)
12349 cq->CQ_max_cqe = ecount;
12350 break;
12351 default:
12352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12353 "0370 Invalid completion queue type (%d)\n",
12354 cq->type);
12355 return;
12356 }
12357
12358 /* Catch the no cq entry condition, log an error */
12359 if (unlikely(ecount == 0))
12360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12361 "0371 No entry from the CQ: identifier "
12362 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12363
12364 /* In any case, flash and re-arm the RCQ */
12365 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12366
12367 /* wake up worker thread if there are works to be done */
12368 if (workposted)
12369 lpfc_worker_wake_up(phba);
12370 }
12371
12372 /**
12373 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12374 * @phba: Pointer to HBA context object.
12375 * @cq: Pointer to associated CQ
12376 * @wcqe: Pointer to work-queue completion queue entry.
12377 *
12378 * This routine process a fast-path work queue completion entry from fast-path
12379 * event queue for FCP command response completion.
12380 **/
12381 static void
12382 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12383 struct lpfc_wcqe_complete *wcqe)
12384 {
12385 struct lpfc_sli_ring *pring = cq->pring;
12386 struct lpfc_iocbq *cmdiocbq;
12387 struct lpfc_iocbq irspiocbq;
12388 unsigned long iflags;
12389
12390 /* Check for response status */
12391 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12392 /* If resource errors reported from HBA, reduce queue
12393 * depth of the SCSI device.
12394 */
12395 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12396 IOSTAT_LOCAL_REJECT)) &&
12397 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12398 IOERR_NO_RESOURCES))
12399 phba->lpfc_rampdown_queue_depth(phba);
12400
12401 /* Log the error status */
12402 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12403 "0373 FCP complete error: status=x%x, "
12404 "hw_status=x%x, total_data_specified=%d, "
12405 "parameter=x%x, word3=x%x\n",
12406 bf_get(lpfc_wcqe_c_status, wcqe),
12407 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12408 wcqe->total_data_placed, wcqe->parameter,
12409 wcqe->word3);
12410 }
12411
12412 /* Look up the FCP command IOCB and create pseudo response IOCB */
12413 spin_lock_irqsave(&pring->ring_lock, iflags);
12414 pring->stats.iocb_event++;
12415 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12416 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12417 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12418 if (unlikely(!cmdiocbq)) {
12419 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12420 "0374 FCP complete with no corresponding "
12421 "cmdiocb: iotag (%d)\n",
12422 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12423 return;
12424 }
12425 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12426 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12427 "0375 FCP cmdiocb not callback function "
12428 "iotag: (%d)\n",
12429 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12430 return;
12431 }
12432
12433 /* Fake the irspiocb and copy necessary response information */
12434 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
12435
12436 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12437 spin_lock_irqsave(&phba->hbalock, iflags);
12438 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12439 spin_unlock_irqrestore(&phba->hbalock, iflags);
12440 }
12441
12442 /* Pass the cmd_iocb and the rsp state to the upper layer */
12443 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12444 }
12445
12446 /**
12447 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12448 * @phba: Pointer to HBA context object.
12449 * @cq: Pointer to completion queue.
12450 * @wcqe: Pointer to work-queue completion queue entry.
12451 *
12452 * This routine handles an fast-path WQ entry comsumed event by invoking the
12453 * proper WQ release routine to the slow-path WQ.
12454 **/
12455 static void
12456 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12457 struct lpfc_wcqe_release *wcqe)
12458 {
12459 struct lpfc_queue *childwq;
12460 bool wqid_matched = false;
12461 uint16_t fcp_wqid;
12462
12463 /* Check for fast-path FCP work queue release */
12464 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12465 list_for_each_entry(childwq, &cq->child_list, list) {
12466 if (childwq->queue_id == fcp_wqid) {
12467 lpfc_sli4_wq_release(childwq,
12468 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12469 wqid_matched = true;
12470 break;
12471 }
12472 }
12473 /* Report warning log message if no match found */
12474 if (wqid_matched != true)
12475 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12476 "2580 Fast-path wqe consume event carries "
12477 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12478 }
12479
12480 /**
12481 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12482 * @cq: Pointer to the completion queue.
12483 * @eqe: Pointer to fast-path completion queue entry.
12484 *
12485 * This routine process a fast-path work queue completion entry from fast-path
12486 * event queue for FCP command response completion.
12487 **/
12488 static int
12489 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12490 struct lpfc_cqe *cqe)
12491 {
12492 struct lpfc_wcqe_release wcqe;
12493 bool workposted = false;
12494
12495 /* Copy the work queue CQE and convert endian order if needed */
12496 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12497
12498 /* Check and process for different type of WCQE and dispatch */
12499 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12500 case CQE_CODE_COMPL_WQE:
12501 cq->CQ_wq++;
12502 /* Process the WQ complete event */
12503 phba->last_completion_time = jiffies;
12504 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
12505 (struct lpfc_wcqe_complete *)&wcqe);
12506 break;
12507 case CQE_CODE_RELEASE_WQE:
12508 cq->CQ_release_wqe++;
12509 /* Process the WQ release event */
12510 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12511 (struct lpfc_wcqe_release *)&wcqe);
12512 break;
12513 case CQE_CODE_XRI_ABORTED:
12514 cq->CQ_xri_aborted++;
12515 /* Process the WQ XRI abort event */
12516 phba->last_completion_time = jiffies;
12517 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12518 (struct sli4_wcqe_xri_aborted *)&wcqe);
12519 break;
12520 default:
12521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12522 "0144 Not a valid WCQE code: x%x\n",
12523 bf_get(lpfc_wcqe_c_code, &wcqe));
12524 break;
12525 }
12526 return workposted;
12527 }
12528
12529 /**
12530 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
12531 * @phba: Pointer to HBA context object.
12532 * @eqe: Pointer to fast-path event queue entry.
12533 *
12534 * This routine process a event queue entry from the fast-path event queue.
12535 * It will check the MajorCode and MinorCode to determine this is for a
12536 * completion event on a completion queue, if not, an error shall be logged
12537 * and just return. Otherwise, it will get to the corresponding completion
12538 * queue and process all the entries on the completion queue, rearm the
12539 * completion queue, and then return.
12540 **/
12541 static void
12542 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12543 uint32_t qidx)
12544 {
12545 struct lpfc_queue *cq;
12546 struct lpfc_cqe *cqe;
12547 bool workposted = false;
12548 uint16_t cqid;
12549 int ecount = 0;
12550
12551 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12553 "0366 Not a valid completion "
12554 "event: majorcode=x%x, minorcode=x%x\n",
12555 bf_get_le32(lpfc_eqe_major_code, eqe),
12556 bf_get_le32(lpfc_eqe_minor_code, eqe));
12557 return;
12558 }
12559
12560 /* Get the reference to the corresponding CQ */
12561 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12562
12563 /* Check if this is a Slow path event */
12564 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12565 lpfc_sli4_sp_handle_eqe(phba, eqe,
12566 phba->sli4_hba.hba_eq[qidx]);
12567 return;
12568 }
12569
12570 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12571 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12572 "3146 Fast-path completion queues "
12573 "does not exist\n");
12574 return;
12575 }
12576 cq = phba->sli4_hba.fcp_cq[qidx];
12577 if (unlikely(!cq)) {
12578 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12580 "0367 Fast-path completion queue "
12581 "(%d) does not exist\n", qidx);
12582 return;
12583 }
12584
12585 if (unlikely(cqid != cq->queue_id)) {
12586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12587 "0368 Miss-matched fast-path completion "
12588 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12589 cqid, cq->queue_id);
12590 return;
12591 }
12592
12593 /* Process all the entries to the CQ */
12594 while ((cqe = lpfc_sli4_cq_get(cq))) {
12595 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12596 if (!(++ecount % cq->entry_repost))
12597 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12598 }
12599
12600 /* Track the max number of CQEs processed in 1 EQ */
12601 if (ecount > cq->CQ_max_cqe)
12602 cq->CQ_max_cqe = ecount;
12603
12604 /* Catch the no cq entry condition */
12605 if (unlikely(ecount == 0))
12606 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12607 "0369 No entry from fast-path completion "
12608 "queue fcpcqid=%d\n", cq->queue_id);
12609
12610 /* In any case, flash and re-arm the CQ */
12611 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12612
12613 /* wake up worker thread if there are works to be done */
12614 if (workposted)
12615 lpfc_worker_wake_up(phba);
12616 }
12617
12618 static void
12619 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12620 {
12621 struct lpfc_eqe *eqe;
12622
12623 /* walk all the EQ entries and drop on the floor */
12624 while ((eqe = lpfc_sli4_eq_get(eq)))
12625 ;
12626
12627 /* Clear and re-arm the EQ */
12628 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12629 }
12630
12631
12632 /**
12633 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12634 * entry
12635 * @phba: Pointer to HBA context object.
12636 * @eqe: Pointer to fast-path event queue entry.
12637 *
12638 * This routine process a event queue entry from the Flash Optimized Fabric
12639 * event queue. It will check the MajorCode and MinorCode to determine this
12640 * is for a completion event on a completion queue, if not, an error shall be
12641 * logged and just return. Otherwise, it will get to the corresponding
12642 * completion queue and process all the entries on the completion queue, rearm
12643 * the completion queue, and then return.
12644 **/
12645 static void
12646 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12647 {
12648 struct lpfc_queue *cq;
12649 struct lpfc_cqe *cqe;
12650 bool workposted = false;
12651 uint16_t cqid;
12652 int ecount = 0;
12653
12654 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12656 "9147 Not a valid completion "
12657 "event: majorcode=x%x, minorcode=x%x\n",
12658 bf_get_le32(lpfc_eqe_major_code, eqe),
12659 bf_get_le32(lpfc_eqe_minor_code, eqe));
12660 return;
12661 }
12662
12663 /* Get the reference to the corresponding CQ */
12664 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12665
12666 /* Next check for OAS */
12667 cq = phba->sli4_hba.oas_cq;
12668 if (unlikely(!cq)) {
12669 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12671 "9148 OAS completion queue "
12672 "does not exist\n");
12673 return;
12674 }
12675
12676 if (unlikely(cqid != cq->queue_id)) {
12677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12678 "9149 Miss-matched fast-path compl "
12679 "queue id: eqcqid=%d, fcpcqid=%d\n",
12680 cqid, cq->queue_id);
12681 return;
12682 }
12683
12684 /* Process all the entries to the OAS CQ */
12685 while ((cqe = lpfc_sli4_cq_get(cq))) {
12686 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12687 if (!(++ecount % cq->entry_repost))
12688 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12689 }
12690
12691 /* Track the max number of CQEs processed in 1 EQ */
12692 if (ecount > cq->CQ_max_cqe)
12693 cq->CQ_max_cqe = ecount;
12694
12695 /* Catch the no cq entry condition */
12696 if (unlikely(ecount == 0))
12697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12698 "9153 No entry from fast-path completion "
12699 "queue fcpcqid=%d\n", cq->queue_id);
12700
12701 /* In any case, flash and re-arm the CQ */
12702 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12703
12704 /* wake up worker thread if there are works to be done */
12705 if (workposted)
12706 lpfc_worker_wake_up(phba);
12707 }
12708
12709 /**
12710 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12711 * @irq: Interrupt number.
12712 * @dev_id: The device context pointer.
12713 *
12714 * This function is directly called from the PCI layer as an interrupt
12715 * service routine when device with SLI-4 interface spec is enabled with
12716 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12717 * IOCB ring event in the HBA. However, when the device is enabled with either
12718 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12719 * device-level interrupt handler. When the PCI slot is in error recovery
12720 * or the HBA is undergoing initialization, the interrupt handler will not
12721 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12722 * the intrrupt context. This function is called without any lock held.
12723 * It gets the hbalock to access and update SLI data structures. Note that,
12724 * the EQ to CQ are one-to-one map such that the EQ index is
12725 * equal to that of CQ index.
12726 *
12727 * This function returns IRQ_HANDLED when interrupt is handled else it
12728 * returns IRQ_NONE.
12729 **/
12730 irqreturn_t
12731 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12732 {
12733 struct lpfc_hba *phba;
12734 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12735 struct lpfc_queue *eq;
12736 struct lpfc_eqe *eqe;
12737 unsigned long iflag;
12738 int ecount = 0;
12739
12740 /* Get the driver's phba structure from the dev_id */
12741 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12742 phba = fcp_eq_hdl->phba;
12743
12744 if (unlikely(!phba))
12745 return IRQ_NONE;
12746
12747 /* Get to the EQ struct associated with this vector */
12748 eq = phba->sli4_hba.fof_eq;
12749 if (unlikely(!eq))
12750 return IRQ_NONE;
12751
12752 /* Check device state for handling interrupt */
12753 if (unlikely(lpfc_intr_state_check(phba))) {
12754 eq->EQ_badstate++;
12755 /* Check again for link_state with lock held */
12756 spin_lock_irqsave(&phba->hbalock, iflag);
12757 if (phba->link_state < LPFC_LINK_DOWN)
12758 /* Flush, clear interrupt, and rearm the EQ */
12759 lpfc_sli4_eq_flush(phba, eq);
12760 spin_unlock_irqrestore(&phba->hbalock, iflag);
12761 return IRQ_NONE;
12762 }
12763
12764 /*
12765 * Process all the event on FCP fast-path EQ
12766 */
12767 while ((eqe = lpfc_sli4_eq_get(eq))) {
12768 lpfc_sli4_fof_handle_eqe(phba, eqe);
12769 if (!(++ecount % eq->entry_repost))
12770 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12771 eq->EQ_processed++;
12772 }
12773
12774 /* Track the max number of EQEs processed in 1 intr */
12775 if (ecount > eq->EQ_max_eqe)
12776 eq->EQ_max_eqe = ecount;
12777
12778
12779 if (unlikely(ecount == 0)) {
12780 eq->EQ_no_entry++;
12781
12782 if (phba->intr_type == MSIX)
12783 /* MSI-X treated interrupt served as no EQ share INT */
12784 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12785 "9145 MSI-X interrupt with no EQE\n");
12786 else {
12787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12788 "9146 ISR interrupt with no EQE\n");
12789 /* Non MSI-X treated on interrupt as EQ share INT */
12790 return IRQ_NONE;
12791 }
12792 }
12793 /* Always clear and re-arm the fast-path EQ */
12794 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12795 return IRQ_HANDLED;
12796 }
12797
12798 /**
12799 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
12800 * @irq: Interrupt number.
12801 * @dev_id: The device context pointer.
12802 *
12803 * This function is directly called from the PCI layer as an interrupt
12804 * service routine when device with SLI-4 interface spec is enabled with
12805 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12806 * ring event in the HBA. However, when the device is enabled with either
12807 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12808 * device-level interrupt handler. When the PCI slot is in error recovery
12809 * or the HBA is undergoing initialization, the interrupt handler will not
12810 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12811 * the intrrupt context. This function is called without any lock held.
12812 * It gets the hbalock to access and update SLI data structures. Note that,
12813 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12814 * equal to that of FCP CQ index.
12815 *
12816 * The link attention and ELS ring attention events are handled
12817 * by the worker thread. The interrupt handler signals the worker thread
12818 * and returns for these events. This function is called without any lock
12819 * held. It gets the hbalock to access and update SLI data structures.
12820 *
12821 * This function returns IRQ_HANDLED when interrupt is handled else it
12822 * returns IRQ_NONE.
12823 **/
12824 irqreturn_t
12825 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
12826 {
12827 struct lpfc_hba *phba;
12828 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12829 struct lpfc_queue *fpeq;
12830 struct lpfc_eqe *eqe;
12831 unsigned long iflag;
12832 int ecount = 0;
12833 int fcp_eqidx;
12834
12835 /* Get the driver's phba structure from the dev_id */
12836 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12837 phba = fcp_eq_hdl->phba;
12838 fcp_eqidx = fcp_eq_hdl->idx;
12839
12840 if (unlikely(!phba))
12841 return IRQ_NONE;
12842 if (unlikely(!phba->sli4_hba.hba_eq))
12843 return IRQ_NONE;
12844
12845 /* Get to the EQ struct associated with this vector */
12846 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
12847 if (unlikely(!fpeq))
12848 return IRQ_NONE;
12849
12850 if (lpfc_fcp_look_ahead) {
12851 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12852 lpfc_sli4_eq_clr_intr(fpeq);
12853 else {
12854 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12855 return IRQ_NONE;
12856 }
12857 }
12858
12859 /* Check device state for handling interrupt */
12860 if (unlikely(lpfc_intr_state_check(phba))) {
12861 fpeq->EQ_badstate++;
12862 /* Check again for link_state with lock held */
12863 spin_lock_irqsave(&phba->hbalock, iflag);
12864 if (phba->link_state < LPFC_LINK_DOWN)
12865 /* Flush, clear interrupt, and rearm the EQ */
12866 lpfc_sli4_eq_flush(phba, fpeq);
12867 spin_unlock_irqrestore(&phba->hbalock, iflag);
12868 if (lpfc_fcp_look_ahead)
12869 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12870 return IRQ_NONE;
12871 }
12872
12873 /*
12874 * Process all the event on FCP fast-path EQ
12875 */
12876 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
12877 if (eqe == NULL)
12878 break;
12879
12880 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
12881 if (!(++ecount % fpeq->entry_repost))
12882 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
12883 fpeq->EQ_processed++;
12884 }
12885
12886 /* Track the max number of EQEs processed in 1 intr */
12887 if (ecount > fpeq->EQ_max_eqe)
12888 fpeq->EQ_max_eqe = ecount;
12889
12890 /* Always clear and re-arm the fast-path EQ */
12891 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12892
12893 if (unlikely(ecount == 0)) {
12894 fpeq->EQ_no_entry++;
12895
12896 if (lpfc_fcp_look_ahead) {
12897 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12898 return IRQ_NONE;
12899 }
12900
12901 if (phba->intr_type == MSIX)
12902 /* MSI-X treated interrupt served as no EQ share INT */
12903 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12904 "0358 MSI-X interrupt with no EQE\n");
12905 else
12906 /* Non MSI-X treated on interrupt as EQ share INT */
12907 return IRQ_NONE;
12908 }
12909
12910 if (lpfc_fcp_look_ahead)
12911 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12912 return IRQ_HANDLED;
12913 } /* lpfc_sli4_fp_intr_handler */
12914
12915 /**
12916 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12917 * @irq: Interrupt number.
12918 * @dev_id: The device context pointer.
12919 *
12920 * This function is the device-level interrupt handler to device with SLI-4
12921 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12922 * interrupt mode is enabled and there is an event in the HBA which requires
12923 * driver attention. This function invokes the slow-path interrupt attention
12924 * handling function and fast-path interrupt attention handling function in
12925 * turn to process the relevant HBA attention events. This function is called
12926 * without any lock held. It gets the hbalock to access and update SLI data
12927 * structures.
12928 *
12929 * This function returns IRQ_HANDLED when interrupt is handled, else it
12930 * returns IRQ_NONE.
12931 **/
12932 irqreturn_t
12933 lpfc_sli4_intr_handler(int irq, void *dev_id)
12934 {
12935 struct lpfc_hba *phba;
12936 irqreturn_t hba_irq_rc;
12937 bool hba_handled = false;
12938 int fcp_eqidx;
12939
12940 /* Get the driver's phba structure from the dev_id */
12941 phba = (struct lpfc_hba *)dev_id;
12942
12943 if (unlikely(!phba))
12944 return IRQ_NONE;
12945
12946 /*
12947 * Invoke fast-path host attention interrupt handling as appropriate.
12948 */
12949 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12950 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12951 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12952 if (hba_irq_rc == IRQ_HANDLED)
12953 hba_handled |= true;
12954 }
12955
12956 if (phba->cfg_fof) {
12957 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12958 &phba->sli4_hba.fcp_eq_hdl[0]);
12959 if (hba_irq_rc == IRQ_HANDLED)
12960 hba_handled |= true;
12961 }
12962
12963 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12964 } /* lpfc_sli4_intr_handler */
12965
12966 /**
12967 * lpfc_sli4_queue_free - free a queue structure and associated memory
12968 * @queue: The queue structure to free.
12969 *
12970 * This function frees a queue structure and the DMAable memory used for
12971 * the host resident queue. This function must be called after destroying the
12972 * queue on the HBA.
12973 **/
12974 void
12975 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12976 {
12977 struct lpfc_dmabuf *dmabuf;
12978
12979 if (!queue)
12980 return;
12981
12982 while (!list_empty(&queue->page_list)) {
12983 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12984 list);
12985 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12986 dmabuf->virt, dmabuf->phys);
12987 kfree(dmabuf);
12988 }
12989 kfree(queue);
12990 return;
12991 }
12992
12993 /**
12994 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12995 * @phba: The HBA that this queue is being created on.
12996 * @entry_size: The size of each queue entry for this queue.
12997 * @entry count: The number of entries that this queue will handle.
12998 *
12999 * This function allocates a queue structure and the DMAable memory used for
13000 * the host resident queue. This function must be called before creating the
13001 * queue on the HBA.
13002 **/
13003 struct lpfc_queue *
13004 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13005 uint32_t entry_count)
13006 {
13007 struct lpfc_queue *queue;
13008 struct lpfc_dmabuf *dmabuf;
13009 int x, total_qe_count;
13010 void *dma_pointer;
13011 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13012
13013 if (!phba->sli4_hba.pc_sli4_params.supported)
13014 hw_page_size = SLI4_PAGE_SIZE;
13015
13016 queue = kzalloc(sizeof(struct lpfc_queue) +
13017 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13018 if (!queue)
13019 return NULL;
13020 queue->page_count = (ALIGN(entry_size * entry_count,
13021 hw_page_size))/hw_page_size;
13022 INIT_LIST_HEAD(&queue->list);
13023 INIT_LIST_HEAD(&queue->page_list);
13024 INIT_LIST_HEAD(&queue->child_list);
13025 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13026 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13027 if (!dmabuf)
13028 goto out_fail;
13029 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13030 hw_page_size, &dmabuf->phys,
13031 GFP_KERNEL);
13032 if (!dmabuf->virt) {
13033 kfree(dmabuf);
13034 goto out_fail;
13035 }
13036 dmabuf->buffer_tag = x;
13037 list_add_tail(&dmabuf->list, &queue->page_list);
13038 /* initialize queue's entry array */
13039 dma_pointer = dmabuf->virt;
13040 for (; total_qe_count < entry_count &&
13041 dma_pointer < (hw_page_size + dmabuf->virt);
13042 total_qe_count++, dma_pointer += entry_size) {
13043 queue->qe[total_qe_count].address = dma_pointer;
13044 }
13045 }
13046 queue->entry_size = entry_size;
13047 queue->entry_count = entry_count;
13048
13049 /*
13050 * entry_repost is calculated based on the number of entries in the
13051 * queue. This works out except for RQs. If buffers are NOT initially
13052 * posted for every RQE, entry_repost should be adjusted accordingly.
13053 */
13054 queue->entry_repost = (entry_count >> 3);
13055 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13056 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
13057 queue->phba = phba;
13058
13059 return queue;
13060 out_fail:
13061 lpfc_sli4_queue_free(queue);
13062 return NULL;
13063 }
13064
13065 /**
13066 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13067 * @phba: HBA structure that indicates port to create a queue on.
13068 * @pci_barset: PCI BAR set flag.
13069 *
13070 * This function shall perform iomap of the specified PCI BAR address to host
13071 * memory address if not already done so and return it. The returned host
13072 * memory address can be NULL.
13073 */
13074 static void __iomem *
13075 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13076 {
13077 if (!phba->pcidev)
13078 return NULL;
13079
13080 switch (pci_barset) {
13081 case WQ_PCI_BAR_0_AND_1:
13082 return phba->pci_bar0_memmap_p;
13083 case WQ_PCI_BAR_2_AND_3:
13084 return phba->pci_bar2_memmap_p;
13085 case WQ_PCI_BAR_4_AND_5:
13086 return phba->pci_bar4_memmap_p;
13087 default:
13088 break;
13089 }
13090 return NULL;
13091 }
13092
13093 /**
13094 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13095 * @phba: HBA structure that indicates port to create a queue on.
13096 * @startq: The starting FCP EQ to modify
13097 *
13098 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13099 *
13100 * The @phba struct is used to send mailbox command to HBA. The @startq
13101 * is used to get the starting FCP EQ to change.
13102 * This function is asynchronous and will wait for the mailbox
13103 * command to finish before continuing.
13104 *
13105 * On success this function will return a zero. If unable to allocate enough
13106 * memory this function will return -ENOMEM. If the queue create mailbox command
13107 * fails this function will return -ENXIO.
13108 **/
13109 int
13110 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
13111 {
13112 struct lpfc_mbx_modify_eq_delay *eq_delay;
13113 LPFC_MBOXQ_t *mbox;
13114 struct lpfc_queue *eq;
13115 int cnt, rc, length, status = 0;
13116 uint32_t shdr_status, shdr_add_status;
13117 uint32_t result;
13118 int fcp_eqidx;
13119 union lpfc_sli4_cfg_shdr *shdr;
13120 uint16_t dmult;
13121
13122 if (startq >= phba->cfg_fcp_io_channel)
13123 return 0;
13124
13125 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13126 if (!mbox)
13127 return -ENOMEM;
13128 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13129 sizeof(struct lpfc_sli4_cfg_mhdr));
13130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13131 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13132 length, LPFC_SLI4_MBX_EMBED);
13133 eq_delay = &mbox->u.mqe.un.eq_delay;
13134
13135 /* Calculate delay multiper from maximum interrupt per second */
13136 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13137 if (result > LPFC_DMULT_CONST)
13138 dmult = 0;
13139 else
13140 dmult = LPFC_DMULT_CONST/result - 1;
13141
13142 cnt = 0;
13143 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
13144 fcp_eqidx++) {
13145 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
13146 if (!eq)
13147 continue;
13148 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13149 eq_delay->u.request.eq[cnt].phase = 0;
13150 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13151 cnt++;
13152 if (cnt >= LPFC_MAX_EQ_DELAY)
13153 break;
13154 }
13155 eq_delay->u.request.num_eq = cnt;
13156
13157 mbox->vport = phba->pport;
13158 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13159 mbox->context1 = NULL;
13160 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13161 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13162 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13163 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13164 if (shdr_status || shdr_add_status || rc) {
13165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13166 "2512 MODIFY_EQ_DELAY mailbox failed with "
13167 "status x%x add_status x%x, mbx status x%x\n",
13168 shdr_status, shdr_add_status, rc);
13169 status = -ENXIO;
13170 }
13171 mempool_free(mbox, phba->mbox_mem_pool);
13172 return status;
13173 }
13174
13175 /**
13176 * lpfc_eq_create - Create an Event Queue on the HBA
13177 * @phba: HBA structure that indicates port to create a queue on.
13178 * @eq: The queue structure to use to create the event queue.
13179 * @imax: The maximum interrupt per second limit.
13180 *
13181 * This function creates an event queue, as detailed in @eq, on a port,
13182 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13183 *
13184 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13185 * is used to get the entry count and entry size that are necessary to
13186 * determine the number of pages to allocate and use for this queue. This
13187 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13188 * event queue. This function is asynchronous and will wait for the mailbox
13189 * command to finish before continuing.
13190 *
13191 * On success this function will return a zero. If unable to allocate enough
13192 * memory this function will return -ENOMEM. If the queue create mailbox command
13193 * fails this function will return -ENXIO.
13194 **/
13195 int
13196 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
13197 {
13198 struct lpfc_mbx_eq_create *eq_create;
13199 LPFC_MBOXQ_t *mbox;
13200 int rc, length, status = 0;
13201 struct lpfc_dmabuf *dmabuf;
13202 uint32_t shdr_status, shdr_add_status;
13203 union lpfc_sli4_cfg_shdr *shdr;
13204 uint16_t dmult;
13205 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13206
13207 /* sanity check on queue memory */
13208 if (!eq)
13209 return -ENODEV;
13210 if (!phba->sli4_hba.pc_sli4_params.supported)
13211 hw_page_size = SLI4_PAGE_SIZE;
13212
13213 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13214 if (!mbox)
13215 return -ENOMEM;
13216 length = (sizeof(struct lpfc_mbx_eq_create) -
13217 sizeof(struct lpfc_sli4_cfg_mhdr));
13218 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13219 LPFC_MBOX_OPCODE_EQ_CREATE,
13220 length, LPFC_SLI4_MBX_EMBED);
13221 eq_create = &mbox->u.mqe.un.eq_create;
13222 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13223 eq->page_count);
13224 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13225 LPFC_EQE_SIZE);
13226 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
13227 /* don't setup delay multiplier using EQ_CREATE */
13228 dmult = 0;
13229 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13230 dmult);
13231 switch (eq->entry_count) {
13232 default:
13233 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13234 "0360 Unsupported EQ count. (%d)\n",
13235 eq->entry_count);
13236 if (eq->entry_count < 256)
13237 return -EINVAL;
13238 /* otherwise default to smallest count (drop through) */
13239 case 256:
13240 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13241 LPFC_EQ_CNT_256);
13242 break;
13243 case 512:
13244 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13245 LPFC_EQ_CNT_512);
13246 break;
13247 case 1024:
13248 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13249 LPFC_EQ_CNT_1024);
13250 break;
13251 case 2048:
13252 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13253 LPFC_EQ_CNT_2048);
13254 break;
13255 case 4096:
13256 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13257 LPFC_EQ_CNT_4096);
13258 break;
13259 }
13260 list_for_each_entry(dmabuf, &eq->page_list, list) {
13261 memset(dmabuf->virt, 0, hw_page_size);
13262 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13263 putPaddrLow(dmabuf->phys);
13264 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13265 putPaddrHigh(dmabuf->phys);
13266 }
13267 mbox->vport = phba->pport;
13268 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13269 mbox->context1 = NULL;
13270 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13271 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13272 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13273 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13274 if (shdr_status || shdr_add_status || rc) {
13275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13276 "2500 EQ_CREATE mailbox failed with "
13277 "status x%x add_status x%x, mbx status x%x\n",
13278 shdr_status, shdr_add_status, rc);
13279 status = -ENXIO;
13280 }
13281 eq->type = LPFC_EQ;
13282 eq->subtype = LPFC_NONE;
13283 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13284 if (eq->queue_id == 0xFFFF)
13285 status = -ENXIO;
13286 eq->host_index = 0;
13287 eq->hba_index = 0;
13288
13289 mempool_free(mbox, phba->mbox_mem_pool);
13290 return status;
13291 }
13292
13293 /**
13294 * lpfc_cq_create - Create a Completion Queue on the HBA
13295 * @phba: HBA structure that indicates port to create a queue on.
13296 * @cq: The queue structure to use to create the completion queue.
13297 * @eq: The event queue to bind this completion queue to.
13298 *
13299 * This function creates a completion queue, as detailed in @wq, on a port,
13300 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13301 *
13302 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13303 * is used to get the entry count and entry size that are necessary to
13304 * determine the number of pages to allocate and use for this queue. The @eq
13305 * is used to indicate which event queue to bind this completion queue to. This
13306 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13307 * completion queue. This function is asynchronous and will wait for the mailbox
13308 * command to finish before continuing.
13309 *
13310 * On success this function will return a zero. If unable to allocate enough
13311 * memory this function will return -ENOMEM. If the queue create mailbox command
13312 * fails this function will return -ENXIO.
13313 **/
13314 int
13315 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13316 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13317 {
13318 struct lpfc_mbx_cq_create *cq_create;
13319 struct lpfc_dmabuf *dmabuf;
13320 LPFC_MBOXQ_t *mbox;
13321 int rc, length, status = 0;
13322 uint32_t shdr_status, shdr_add_status;
13323 union lpfc_sli4_cfg_shdr *shdr;
13324 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13325
13326 /* sanity check on queue memory */
13327 if (!cq || !eq)
13328 return -ENODEV;
13329 if (!phba->sli4_hba.pc_sli4_params.supported)
13330 hw_page_size = SLI4_PAGE_SIZE;
13331
13332 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13333 if (!mbox)
13334 return -ENOMEM;
13335 length = (sizeof(struct lpfc_mbx_cq_create) -
13336 sizeof(struct lpfc_sli4_cfg_mhdr));
13337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13338 LPFC_MBOX_OPCODE_CQ_CREATE,
13339 length, LPFC_SLI4_MBX_EMBED);
13340 cq_create = &mbox->u.mqe.un.cq_create;
13341 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
13342 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13343 cq->page_count);
13344 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13345 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
13346 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13347 phba->sli4_hba.pc_sli4_params.cqv);
13348 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
13349 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13350 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
13351 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13352 eq->queue_id);
13353 } else {
13354 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13355 eq->queue_id);
13356 }
13357 switch (cq->entry_count) {
13358 default:
13359 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13360 "0361 Unsupported CQ count. (%d)\n",
13361 cq->entry_count);
13362 if (cq->entry_count < 256) {
13363 status = -EINVAL;
13364 goto out;
13365 }
13366 /* otherwise default to smallest count (drop through) */
13367 case 256:
13368 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13369 LPFC_CQ_CNT_256);
13370 break;
13371 case 512:
13372 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13373 LPFC_CQ_CNT_512);
13374 break;
13375 case 1024:
13376 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13377 LPFC_CQ_CNT_1024);
13378 break;
13379 }
13380 list_for_each_entry(dmabuf, &cq->page_list, list) {
13381 memset(dmabuf->virt, 0, hw_page_size);
13382 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13383 putPaddrLow(dmabuf->phys);
13384 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13385 putPaddrHigh(dmabuf->phys);
13386 }
13387 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13388
13389 /* The IOCTL status is embedded in the mailbox subheader. */
13390 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13391 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13392 if (shdr_status || shdr_add_status || rc) {
13393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13394 "2501 CQ_CREATE mailbox failed with "
13395 "status x%x add_status x%x, mbx status x%x\n",
13396 shdr_status, shdr_add_status, rc);
13397 status = -ENXIO;
13398 goto out;
13399 }
13400 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13401 if (cq->queue_id == 0xFFFF) {
13402 status = -ENXIO;
13403 goto out;
13404 }
13405 /* link the cq onto the parent eq child list */
13406 list_add_tail(&cq->list, &eq->child_list);
13407 /* Set up completion queue's type and subtype */
13408 cq->type = type;
13409 cq->subtype = subtype;
13410 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13411 cq->assoc_qid = eq->queue_id;
13412 cq->host_index = 0;
13413 cq->hba_index = 0;
13414
13415 out:
13416 mempool_free(mbox, phba->mbox_mem_pool);
13417 return status;
13418 }
13419
13420 /**
13421 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
13422 * @phba: HBA structure that indicates port to create a queue on.
13423 * @mq: The queue structure to use to create the mailbox queue.
13424 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13425 * @cq: The completion queue to associate with this cq.
13426 *
13427 * This function provides failback (fb) functionality when the
13428 * mq_create_ext fails on older FW generations. It's purpose is identical
13429 * to mq_create_ext otherwise.
13430 *
13431 * This routine cannot fail as all attributes were previously accessed and
13432 * initialized in mq_create_ext.
13433 **/
13434 static void
13435 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13436 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
13437 {
13438 struct lpfc_mbx_mq_create *mq_create;
13439 struct lpfc_dmabuf *dmabuf;
13440 int length;
13441
13442 length = (sizeof(struct lpfc_mbx_mq_create) -
13443 sizeof(struct lpfc_sli4_cfg_mhdr));
13444 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13445 LPFC_MBOX_OPCODE_MQ_CREATE,
13446 length, LPFC_SLI4_MBX_EMBED);
13447 mq_create = &mbox->u.mqe.un.mq_create;
13448 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
13449 mq->page_count);
13450 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
13451 cq->queue_id);
13452 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13453 switch (mq->entry_count) {
13454 case 16:
13455 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13456 LPFC_MQ_RING_SIZE_16);
13457 break;
13458 case 32:
13459 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13460 LPFC_MQ_RING_SIZE_32);
13461 break;
13462 case 64:
13463 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13464 LPFC_MQ_RING_SIZE_64);
13465 break;
13466 case 128:
13467 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13468 LPFC_MQ_RING_SIZE_128);
13469 break;
13470 }
13471 list_for_each_entry(dmabuf, &mq->page_list, list) {
13472 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13473 putPaddrLow(dmabuf->phys);
13474 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13475 putPaddrHigh(dmabuf->phys);
13476 }
13477 }
13478
13479 /**
13480 * lpfc_mq_create - Create a mailbox Queue on the HBA
13481 * @phba: HBA structure that indicates port to create a queue on.
13482 * @mq: The queue structure to use to create the mailbox queue.
13483 * @cq: The completion queue to associate with this cq.
13484 * @subtype: The queue's subtype.
13485 *
13486 * This function creates a mailbox queue, as detailed in @mq, on a port,
13487 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13488 *
13489 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13490 * is used to get the entry count and entry size that are necessary to
13491 * determine the number of pages to allocate and use for this queue. This
13492 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13493 * mailbox queue. This function is asynchronous and will wait for the mailbox
13494 * command to finish before continuing.
13495 *
13496 * On success this function will return a zero. If unable to allocate enough
13497 * memory this function will return -ENOMEM. If the queue create mailbox command
13498 * fails this function will return -ENXIO.
13499 **/
13500 int32_t
13501 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13502 struct lpfc_queue *cq, uint32_t subtype)
13503 {
13504 struct lpfc_mbx_mq_create *mq_create;
13505 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13506 struct lpfc_dmabuf *dmabuf;
13507 LPFC_MBOXQ_t *mbox;
13508 int rc, length, status = 0;
13509 uint32_t shdr_status, shdr_add_status;
13510 union lpfc_sli4_cfg_shdr *shdr;
13511 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13512
13513 /* sanity check on queue memory */
13514 if (!mq || !cq)
13515 return -ENODEV;
13516 if (!phba->sli4_hba.pc_sli4_params.supported)
13517 hw_page_size = SLI4_PAGE_SIZE;
13518
13519 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13520 if (!mbox)
13521 return -ENOMEM;
13522 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13523 sizeof(struct lpfc_sli4_cfg_mhdr));
13524 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13525 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13526 length, LPFC_SLI4_MBX_EMBED);
13527
13528 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
13529 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
13530 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13531 &mq_create_ext->u.request, mq->page_count);
13532 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13533 &mq_create_ext->u.request, 1);
13534 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
13535 &mq_create_ext->u.request, 1);
13536 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13537 &mq_create_ext->u.request, 1);
13538 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13539 &mq_create_ext->u.request, 1);
13540 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13541 &mq_create_ext->u.request, 1);
13542 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
13543 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13544 phba->sli4_hba.pc_sli4_params.mqv);
13545 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13546 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13547 cq->queue_id);
13548 else
13549 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13550 cq->queue_id);
13551 switch (mq->entry_count) {
13552 default:
13553 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13554 "0362 Unsupported MQ count. (%d)\n",
13555 mq->entry_count);
13556 if (mq->entry_count < 16) {
13557 status = -EINVAL;
13558 goto out;
13559 }
13560 /* otherwise default to smallest count (drop through) */
13561 case 16:
13562 bf_set(lpfc_mq_context_ring_size,
13563 &mq_create_ext->u.request.context,
13564 LPFC_MQ_RING_SIZE_16);
13565 break;
13566 case 32:
13567 bf_set(lpfc_mq_context_ring_size,
13568 &mq_create_ext->u.request.context,
13569 LPFC_MQ_RING_SIZE_32);
13570 break;
13571 case 64:
13572 bf_set(lpfc_mq_context_ring_size,
13573 &mq_create_ext->u.request.context,
13574 LPFC_MQ_RING_SIZE_64);
13575 break;
13576 case 128:
13577 bf_set(lpfc_mq_context_ring_size,
13578 &mq_create_ext->u.request.context,
13579 LPFC_MQ_RING_SIZE_128);
13580 break;
13581 }
13582 list_for_each_entry(dmabuf, &mq->page_list, list) {
13583 memset(dmabuf->virt, 0, hw_page_size);
13584 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13585 putPaddrLow(dmabuf->phys);
13586 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
13587 putPaddrHigh(dmabuf->phys);
13588 }
13589 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13590 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13591 &mq_create_ext->u.response);
13592 if (rc != MBX_SUCCESS) {
13593 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13594 "2795 MQ_CREATE_EXT failed with "
13595 "status x%x. Failback to MQ_CREATE.\n",
13596 rc);
13597 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13598 mq_create = &mbox->u.mqe.un.mq_create;
13599 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13600 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13601 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13602 &mq_create->u.response);
13603 }
13604
13605 /* The IOCTL status is embedded in the mailbox subheader. */
13606 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13607 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13608 if (shdr_status || shdr_add_status || rc) {
13609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13610 "2502 MQ_CREATE mailbox failed with "
13611 "status x%x add_status x%x, mbx status x%x\n",
13612 shdr_status, shdr_add_status, rc);
13613 status = -ENXIO;
13614 goto out;
13615 }
13616 if (mq->queue_id == 0xFFFF) {
13617 status = -ENXIO;
13618 goto out;
13619 }
13620 mq->type = LPFC_MQ;
13621 mq->assoc_qid = cq->queue_id;
13622 mq->subtype = subtype;
13623 mq->host_index = 0;
13624 mq->hba_index = 0;
13625
13626 /* link the mq onto the parent cq child list */
13627 list_add_tail(&mq->list, &cq->child_list);
13628 out:
13629 mempool_free(mbox, phba->mbox_mem_pool);
13630 return status;
13631 }
13632
13633 /**
13634 * lpfc_wq_create - Create a Work Queue on the HBA
13635 * @phba: HBA structure that indicates port to create a queue on.
13636 * @wq: The queue structure to use to create the work queue.
13637 * @cq: The completion queue to bind this work queue to.
13638 * @subtype: The subtype of the work queue indicating its functionality.
13639 *
13640 * This function creates a work queue, as detailed in @wq, on a port, described
13641 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13642 *
13643 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13644 * is used to get the entry count and entry size that are necessary to
13645 * determine the number of pages to allocate and use for this queue. The @cq
13646 * is used to indicate which completion queue to bind this work queue to. This
13647 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13648 * work queue. This function is asynchronous and will wait for the mailbox
13649 * command to finish before continuing.
13650 *
13651 * On success this function will return a zero. If unable to allocate enough
13652 * memory this function will return -ENOMEM. If the queue create mailbox command
13653 * fails this function will return -ENXIO.
13654 **/
13655 int
13656 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13657 struct lpfc_queue *cq, uint32_t subtype)
13658 {
13659 struct lpfc_mbx_wq_create *wq_create;
13660 struct lpfc_dmabuf *dmabuf;
13661 LPFC_MBOXQ_t *mbox;
13662 int rc, length, status = 0;
13663 uint32_t shdr_status, shdr_add_status;
13664 union lpfc_sli4_cfg_shdr *shdr;
13665 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13666 struct dma_address *page;
13667 void __iomem *bar_memmap_p;
13668 uint32_t db_offset;
13669 uint16_t pci_barset;
13670
13671 /* sanity check on queue memory */
13672 if (!wq || !cq)
13673 return -ENODEV;
13674 if (!phba->sli4_hba.pc_sli4_params.supported)
13675 hw_page_size = SLI4_PAGE_SIZE;
13676
13677 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13678 if (!mbox)
13679 return -ENOMEM;
13680 length = (sizeof(struct lpfc_mbx_wq_create) -
13681 sizeof(struct lpfc_sli4_cfg_mhdr));
13682 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13683 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13684 length, LPFC_SLI4_MBX_EMBED);
13685 wq_create = &mbox->u.mqe.un.wq_create;
13686 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
13687 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13688 wq->page_count);
13689 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13690 cq->queue_id);
13691
13692 /* wqv is the earliest version supported, NOT the latest */
13693 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13694 phba->sli4_hba.pc_sli4_params.wqv);
13695
13696 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13697 case LPFC_Q_CREATE_VERSION_0:
13698 switch (wq->entry_size) {
13699 default:
13700 case 64:
13701 /* Nothing to do, version 0 ONLY supports 64 byte */
13702 page = wq_create->u.request.page;
13703 break;
13704 case 128:
13705 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13706 LPFC_WQ_SZ128_SUPPORT)) {
13707 status = -ERANGE;
13708 goto out;
13709 }
13710 /* If we get here the HBA MUST also support V1 and
13711 * we MUST use it
13712 */
13713 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13714 LPFC_Q_CREATE_VERSION_1);
13715
13716 bf_set(lpfc_mbx_wq_create_wqe_count,
13717 &wq_create->u.request_1, wq->entry_count);
13718 bf_set(lpfc_mbx_wq_create_wqe_size,
13719 &wq_create->u.request_1,
13720 LPFC_WQ_WQE_SIZE_128);
13721 bf_set(lpfc_mbx_wq_create_page_size,
13722 &wq_create->u.request_1,
13723 LPFC_WQ_PAGE_SIZE_4096);
13724 page = wq_create->u.request_1.page;
13725 break;
13726 }
13727 break;
13728 case LPFC_Q_CREATE_VERSION_1:
13729 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13730 wq->entry_count);
13731 switch (wq->entry_size) {
13732 default:
13733 case 64:
13734 bf_set(lpfc_mbx_wq_create_wqe_size,
13735 &wq_create->u.request_1,
13736 LPFC_WQ_WQE_SIZE_64);
13737 break;
13738 case 128:
13739 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13740 LPFC_WQ_SZ128_SUPPORT)) {
13741 status = -ERANGE;
13742 goto out;
13743 }
13744 bf_set(lpfc_mbx_wq_create_wqe_size,
13745 &wq_create->u.request_1,
13746 LPFC_WQ_WQE_SIZE_128);
13747 break;
13748 }
13749 bf_set(lpfc_mbx_wq_create_page_size,
13750 &wq_create->u.request_1,
13751 LPFC_WQ_PAGE_SIZE_4096);
13752 page = wq_create->u.request_1.page;
13753 break;
13754 default:
13755 status = -ERANGE;
13756 goto out;
13757 }
13758
13759 list_for_each_entry(dmabuf, &wq->page_list, list) {
13760 memset(dmabuf->virt, 0, hw_page_size);
13761 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13762 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
13763 }
13764
13765 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13766 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13767
13768 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13769 /* The IOCTL status is embedded in the mailbox subheader. */
13770 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13771 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13772 if (shdr_status || shdr_add_status || rc) {
13773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13774 "2503 WQ_CREATE mailbox failed with "
13775 "status x%x add_status x%x, mbx status x%x\n",
13776 shdr_status, shdr_add_status, rc);
13777 status = -ENXIO;
13778 goto out;
13779 }
13780 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13781 if (wq->queue_id == 0xFFFF) {
13782 status = -ENXIO;
13783 goto out;
13784 }
13785 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13786 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13787 &wq_create->u.response);
13788 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13789 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13791 "3265 WQ[%d] doorbell format not "
13792 "supported: x%x\n", wq->queue_id,
13793 wq->db_format);
13794 status = -EINVAL;
13795 goto out;
13796 }
13797 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13798 &wq_create->u.response);
13799 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13800 if (!bar_memmap_p) {
13801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13802 "3263 WQ[%d] failed to memmap pci "
13803 "barset:x%x\n", wq->queue_id,
13804 pci_barset);
13805 status = -ENOMEM;
13806 goto out;
13807 }
13808 db_offset = wq_create->u.response.doorbell_offset;
13809 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13810 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13812 "3252 WQ[%d] doorbell offset not "
13813 "supported: x%x\n", wq->queue_id,
13814 db_offset);
13815 status = -EINVAL;
13816 goto out;
13817 }
13818 wq->db_regaddr = bar_memmap_p + db_offset;
13819 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13820 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13821 "format:x%x\n", wq->queue_id, pci_barset,
13822 db_offset, wq->db_format);
13823 } else {
13824 wq->db_format = LPFC_DB_LIST_FORMAT;
13825 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13826 }
13827 wq->type = LPFC_WQ;
13828 wq->assoc_qid = cq->queue_id;
13829 wq->subtype = subtype;
13830 wq->host_index = 0;
13831 wq->hba_index = 0;
13832 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
13833
13834 /* link the wq onto the parent cq child list */
13835 list_add_tail(&wq->list, &cq->child_list);
13836 out:
13837 mempool_free(mbox, phba->mbox_mem_pool);
13838 return status;
13839 }
13840
13841 /**
13842 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13843 * @phba: HBA structure that indicates port to create a queue on.
13844 * @rq: The queue structure to use for the receive queue.
13845 * @qno: The associated HBQ number
13846 *
13847 *
13848 * For SLI4 we need to adjust the RQ repost value based on
13849 * the number of buffers that are initially posted to the RQ.
13850 */
13851 void
13852 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13853 {
13854 uint32_t cnt;
13855
13856 /* sanity check on queue memory */
13857 if (!rq)
13858 return;
13859 cnt = lpfc_hbq_defs[qno]->entry_count;
13860
13861 /* Recalc repost for RQs based on buffers initially posted */
13862 cnt = (cnt >> 3);
13863 if (cnt < LPFC_QUEUE_MIN_REPOST)
13864 cnt = LPFC_QUEUE_MIN_REPOST;
13865
13866 rq->entry_repost = cnt;
13867 }
13868
13869 /**
13870 * lpfc_rq_create - Create a Receive Queue on the HBA
13871 * @phba: HBA structure that indicates port to create a queue on.
13872 * @hrq: The queue structure to use to create the header receive queue.
13873 * @drq: The queue structure to use to create the data receive queue.
13874 * @cq: The completion queue to bind this work queue to.
13875 *
13876 * This function creates a receive buffer queue pair , as detailed in @hrq and
13877 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13878 * to the HBA.
13879 *
13880 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13881 * struct is used to get the entry count that is necessary to determine the
13882 * number of pages to use for this queue. The @cq is used to indicate which
13883 * completion queue to bind received buffers that are posted to these queues to.
13884 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13885 * receive queue pair. This function is asynchronous and will wait for the
13886 * mailbox command to finish before continuing.
13887 *
13888 * On success this function will return a zero. If unable to allocate enough
13889 * memory this function will return -ENOMEM. If the queue create mailbox command
13890 * fails this function will return -ENXIO.
13891 **/
13892 int
13893 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13894 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13895 {
13896 struct lpfc_mbx_rq_create *rq_create;
13897 struct lpfc_dmabuf *dmabuf;
13898 LPFC_MBOXQ_t *mbox;
13899 int rc, length, status = 0;
13900 uint32_t shdr_status, shdr_add_status;
13901 union lpfc_sli4_cfg_shdr *shdr;
13902 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13903 void __iomem *bar_memmap_p;
13904 uint32_t db_offset;
13905 uint16_t pci_barset;
13906
13907 /* sanity check on queue memory */
13908 if (!hrq || !drq || !cq)
13909 return -ENODEV;
13910 if (!phba->sli4_hba.pc_sli4_params.supported)
13911 hw_page_size = SLI4_PAGE_SIZE;
13912
13913 if (hrq->entry_count != drq->entry_count)
13914 return -EINVAL;
13915 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13916 if (!mbox)
13917 return -ENOMEM;
13918 length = (sizeof(struct lpfc_mbx_rq_create) -
13919 sizeof(struct lpfc_sli4_cfg_mhdr));
13920 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13921 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13922 length, LPFC_SLI4_MBX_EMBED);
13923 rq_create = &mbox->u.mqe.un.rq_create;
13924 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13925 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13926 phba->sli4_hba.pc_sli4_params.rqv);
13927 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13928 bf_set(lpfc_rq_context_rqe_count_1,
13929 &rq_create->u.request.context,
13930 hrq->entry_count);
13931 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
13932 bf_set(lpfc_rq_context_rqe_size,
13933 &rq_create->u.request.context,
13934 LPFC_RQE_SIZE_8);
13935 bf_set(lpfc_rq_context_page_size,
13936 &rq_create->u.request.context,
13937 LPFC_RQ_PAGE_SIZE_4096);
13938 } else {
13939 switch (hrq->entry_count) {
13940 default:
13941 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13942 "2535 Unsupported RQ count. (%d)\n",
13943 hrq->entry_count);
13944 if (hrq->entry_count < 512) {
13945 status = -EINVAL;
13946 goto out;
13947 }
13948 /* otherwise default to smallest count (drop through) */
13949 case 512:
13950 bf_set(lpfc_rq_context_rqe_count,
13951 &rq_create->u.request.context,
13952 LPFC_RQ_RING_SIZE_512);
13953 break;
13954 case 1024:
13955 bf_set(lpfc_rq_context_rqe_count,
13956 &rq_create->u.request.context,
13957 LPFC_RQ_RING_SIZE_1024);
13958 break;
13959 case 2048:
13960 bf_set(lpfc_rq_context_rqe_count,
13961 &rq_create->u.request.context,
13962 LPFC_RQ_RING_SIZE_2048);
13963 break;
13964 case 4096:
13965 bf_set(lpfc_rq_context_rqe_count,
13966 &rq_create->u.request.context,
13967 LPFC_RQ_RING_SIZE_4096);
13968 break;
13969 }
13970 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13971 LPFC_HDR_BUF_SIZE);
13972 }
13973 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13974 cq->queue_id);
13975 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13976 hrq->page_count);
13977 list_for_each_entry(dmabuf, &hrq->page_list, list) {
13978 memset(dmabuf->virt, 0, hw_page_size);
13979 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13980 putPaddrLow(dmabuf->phys);
13981 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13982 putPaddrHigh(dmabuf->phys);
13983 }
13984 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13985 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13986
13987 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13988 /* The IOCTL status is embedded in the mailbox subheader. */
13989 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13990 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13991 if (shdr_status || shdr_add_status || rc) {
13992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13993 "2504 RQ_CREATE mailbox failed with "
13994 "status x%x add_status x%x, mbx status x%x\n",
13995 shdr_status, shdr_add_status, rc);
13996 status = -ENXIO;
13997 goto out;
13998 }
13999 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14000 if (hrq->queue_id == 0xFFFF) {
14001 status = -ENXIO;
14002 goto out;
14003 }
14004
14005 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14006 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14007 &rq_create->u.response);
14008 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14009 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14011 "3262 RQ [%d] doorbell format not "
14012 "supported: x%x\n", hrq->queue_id,
14013 hrq->db_format);
14014 status = -EINVAL;
14015 goto out;
14016 }
14017
14018 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14019 &rq_create->u.response);
14020 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14021 if (!bar_memmap_p) {
14022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14023 "3269 RQ[%d] failed to memmap pci "
14024 "barset:x%x\n", hrq->queue_id,
14025 pci_barset);
14026 status = -ENOMEM;
14027 goto out;
14028 }
14029
14030 db_offset = rq_create->u.response.doorbell_offset;
14031 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14032 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14034 "3270 RQ[%d] doorbell offset not "
14035 "supported: x%x\n", hrq->queue_id,
14036 db_offset);
14037 status = -EINVAL;
14038 goto out;
14039 }
14040 hrq->db_regaddr = bar_memmap_p + db_offset;
14041 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14042 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14043 "format:x%x\n", hrq->queue_id, pci_barset,
14044 db_offset, hrq->db_format);
14045 } else {
14046 hrq->db_format = LPFC_DB_RING_FORMAT;
14047 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14048 }
14049 hrq->type = LPFC_HRQ;
14050 hrq->assoc_qid = cq->queue_id;
14051 hrq->subtype = subtype;
14052 hrq->host_index = 0;
14053 hrq->hba_index = 0;
14054
14055 /* now create the data queue */
14056 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14057 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14058 length, LPFC_SLI4_MBX_EMBED);
14059 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14060 phba->sli4_hba.pc_sli4_params.rqv);
14061 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14062 bf_set(lpfc_rq_context_rqe_count_1,
14063 &rq_create->u.request.context, hrq->entry_count);
14064 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
14065 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14066 LPFC_RQE_SIZE_8);
14067 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14068 (PAGE_SIZE/SLI4_PAGE_SIZE));
14069 } else {
14070 switch (drq->entry_count) {
14071 default:
14072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14073 "2536 Unsupported RQ count. (%d)\n",
14074 drq->entry_count);
14075 if (drq->entry_count < 512) {
14076 status = -EINVAL;
14077 goto out;
14078 }
14079 /* otherwise default to smallest count (drop through) */
14080 case 512:
14081 bf_set(lpfc_rq_context_rqe_count,
14082 &rq_create->u.request.context,
14083 LPFC_RQ_RING_SIZE_512);
14084 break;
14085 case 1024:
14086 bf_set(lpfc_rq_context_rqe_count,
14087 &rq_create->u.request.context,
14088 LPFC_RQ_RING_SIZE_1024);
14089 break;
14090 case 2048:
14091 bf_set(lpfc_rq_context_rqe_count,
14092 &rq_create->u.request.context,
14093 LPFC_RQ_RING_SIZE_2048);
14094 break;
14095 case 4096:
14096 bf_set(lpfc_rq_context_rqe_count,
14097 &rq_create->u.request.context,
14098 LPFC_RQ_RING_SIZE_4096);
14099 break;
14100 }
14101 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14102 LPFC_DATA_BUF_SIZE);
14103 }
14104 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14105 cq->queue_id);
14106 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14107 drq->page_count);
14108 list_for_each_entry(dmabuf, &drq->page_list, list) {
14109 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14110 putPaddrLow(dmabuf->phys);
14111 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14112 putPaddrHigh(dmabuf->phys);
14113 }
14114 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14115 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14116 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14117 /* The IOCTL status is embedded in the mailbox subheader. */
14118 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14119 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14120 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14121 if (shdr_status || shdr_add_status || rc) {
14122 status = -ENXIO;
14123 goto out;
14124 }
14125 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14126 if (drq->queue_id == 0xFFFF) {
14127 status = -ENXIO;
14128 goto out;
14129 }
14130 drq->type = LPFC_DRQ;
14131 drq->assoc_qid = cq->queue_id;
14132 drq->subtype = subtype;
14133 drq->host_index = 0;
14134 drq->hba_index = 0;
14135
14136 /* link the header and data RQs onto the parent cq child list */
14137 list_add_tail(&hrq->list, &cq->child_list);
14138 list_add_tail(&drq->list, &cq->child_list);
14139
14140 out:
14141 mempool_free(mbox, phba->mbox_mem_pool);
14142 return status;
14143 }
14144
14145 /**
14146 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14147 * @eq: The queue structure associated with the queue to destroy.
14148 *
14149 * This function destroys a queue, as detailed in @eq by sending an mailbox
14150 * command, specific to the type of queue, to the HBA.
14151 *
14152 * The @eq struct is used to get the queue ID of the queue to destroy.
14153 *
14154 * On success this function will return a zero. If the queue destroy mailbox
14155 * command fails this function will return -ENXIO.
14156 **/
14157 int
14158 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14159 {
14160 LPFC_MBOXQ_t *mbox;
14161 int rc, length, status = 0;
14162 uint32_t shdr_status, shdr_add_status;
14163 union lpfc_sli4_cfg_shdr *shdr;
14164
14165 /* sanity check on queue memory */
14166 if (!eq)
14167 return -ENODEV;
14168 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14169 if (!mbox)
14170 return -ENOMEM;
14171 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14172 sizeof(struct lpfc_sli4_cfg_mhdr));
14173 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14174 LPFC_MBOX_OPCODE_EQ_DESTROY,
14175 length, LPFC_SLI4_MBX_EMBED);
14176 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14177 eq->queue_id);
14178 mbox->vport = eq->phba->pport;
14179 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14180
14181 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14182 /* The IOCTL status is embedded in the mailbox subheader. */
14183 shdr = (union lpfc_sli4_cfg_shdr *)
14184 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14185 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14186 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14187 if (shdr_status || shdr_add_status || rc) {
14188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14189 "2505 EQ_DESTROY mailbox failed with "
14190 "status x%x add_status x%x, mbx status x%x\n",
14191 shdr_status, shdr_add_status, rc);
14192 status = -ENXIO;
14193 }
14194
14195 /* Remove eq from any list */
14196 list_del_init(&eq->list);
14197 mempool_free(mbox, eq->phba->mbox_mem_pool);
14198 return status;
14199 }
14200
14201 /**
14202 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14203 * @cq: The queue structure associated with the queue to destroy.
14204 *
14205 * This function destroys a queue, as detailed in @cq by sending an mailbox
14206 * command, specific to the type of queue, to the HBA.
14207 *
14208 * The @cq struct is used to get the queue ID of the queue to destroy.
14209 *
14210 * On success this function will return a zero. If the queue destroy mailbox
14211 * command fails this function will return -ENXIO.
14212 **/
14213 int
14214 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14215 {
14216 LPFC_MBOXQ_t *mbox;
14217 int rc, length, status = 0;
14218 uint32_t shdr_status, shdr_add_status;
14219 union lpfc_sli4_cfg_shdr *shdr;
14220
14221 /* sanity check on queue memory */
14222 if (!cq)
14223 return -ENODEV;
14224 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14225 if (!mbox)
14226 return -ENOMEM;
14227 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14228 sizeof(struct lpfc_sli4_cfg_mhdr));
14229 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14230 LPFC_MBOX_OPCODE_CQ_DESTROY,
14231 length, LPFC_SLI4_MBX_EMBED);
14232 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14233 cq->queue_id);
14234 mbox->vport = cq->phba->pport;
14235 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14236 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14237 /* The IOCTL status is embedded in the mailbox subheader. */
14238 shdr = (union lpfc_sli4_cfg_shdr *)
14239 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14240 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14241 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14242 if (shdr_status || shdr_add_status || rc) {
14243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14244 "2506 CQ_DESTROY mailbox failed with "
14245 "status x%x add_status x%x, mbx status x%x\n",
14246 shdr_status, shdr_add_status, rc);
14247 status = -ENXIO;
14248 }
14249 /* Remove cq from any list */
14250 list_del_init(&cq->list);
14251 mempool_free(mbox, cq->phba->mbox_mem_pool);
14252 return status;
14253 }
14254
14255 /**
14256 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14257 * @qm: The queue structure associated with the queue to destroy.
14258 *
14259 * This function destroys a queue, as detailed in @mq by sending an mailbox
14260 * command, specific to the type of queue, to the HBA.
14261 *
14262 * The @mq struct is used to get the queue ID of the queue to destroy.
14263 *
14264 * On success this function will return a zero. If the queue destroy mailbox
14265 * command fails this function will return -ENXIO.
14266 **/
14267 int
14268 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14269 {
14270 LPFC_MBOXQ_t *mbox;
14271 int rc, length, status = 0;
14272 uint32_t shdr_status, shdr_add_status;
14273 union lpfc_sli4_cfg_shdr *shdr;
14274
14275 /* sanity check on queue memory */
14276 if (!mq)
14277 return -ENODEV;
14278 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14279 if (!mbox)
14280 return -ENOMEM;
14281 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14282 sizeof(struct lpfc_sli4_cfg_mhdr));
14283 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14284 LPFC_MBOX_OPCODE_MQ_DESTROY,
14285 length, LPFC_SLI4_MBX_EMBED);
14286 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14287 mq->queue_id);
14288 mbox->vport = mq->phba->pport;
14289 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14290 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14291 /* The IOCTL status is embedded in the mailbox subheader. */
14292 shdr = (union lpfc_sli4_cfg_shdr *)
14293 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14296 if (shdr_status || shdr_add_status || rc) {
14297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14298 "2507 MQ_DESTROY mailbox failed with "
14299 "status x%x add_status x%x, mbx status x%x\n",
14300 shdr_status, shdr_add_status, rc);
14301 status = -ENXIO;
14302 }
14303 /* Remove mq from any list */
14304 list_del_init(&mq->list);
14305 mempool_free(mbox, mq->phba->mbox_mem_pool);
14306 return status;
14307 }
14308
14309 /**
14310 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14311 * @wq: The queue structure associated with the queue to destroy.
14312 *
14313 * This function destroys a queue, as detailed in @wq by sending an mailbox
14314 * command, specific to the type of queue, to the HBA.
14315 *
14316 * The @wq struct is used to get the queue ID of the queue to destroy.
14317 *
14318 * On success this function will return a zero. If the queue destroy mailbox
14319 * command fails this function will return -ENXIO.
14320 **/
14321 int
14322 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14323 {
14324 LPFC_MBOXQ_t *mbox;
14325 int rc, length, status = 0;
14326 uint32_t shdr_status, shdr_add_status;
14327 union lpfc_sli4_cfg_shdr *shdr;
14328
14329 /* sanity check on queue memory */
14330 if (!wq)
14331 return -ENODEV;
14332 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14333 if (!mbox)
14334 return -ENOMEM;
14335 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14336 sizeof(struct lpfc_sli4_cfg_mhdr));
14337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14338 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14339 length, LPFC_SLI4_MBX_EMBED);
14340 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14341 wq->queue_id);
14342 mbox->vport = wq->phba->pport;
14343 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14344 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14345 shdr = (union lpfc_sli4_cfg_shdr *)
14346 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14347 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14348 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14349 if (shdr_status || shdr_add_status || rc) {
14350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14351 "2508 WQ_DESTROY mailbox failed with "
14352 "status x%x add_status x%x, mbx status x%x\n",
14353 shdr_status, shdr_add_status, rc);
14354 status = -ENXIO;
14355 }
14356 /* Remove wq from any list */
14357 list_del_init(&wq->list);
14358 mempool_free(mbox, wq->phba->mbox_mem_pool);
14359 return status;
14360 }
14361
14362 /**
14363 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14364 * @rq: The queue structure associated with the queue to destroy.
14365 *
14366 * This function destroys a queue, as detailed in @rq by sending an mailbox
14367 * command, specific to the type of queue, to the HBA.
14368 *
14369 * The @rq struct is used to get the queue ID of the queue to destroy.
14370 *
14371 * On success this function will return a zero. If the queue destroy mailbox
14372 * command fails this function will return -ENXIO.
14373 **/
14374 int
14375 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14376 struct lpfc_queue *drq)
14377 {
14378 LPFC_MBOXQ_t *mbox;
14379 int rc, length, status = 0;
14380 uint32_t shdr_status, shdr_add_status;
14381 union lpfc_sli4_cfg_shdr *shdr;
14382
14383 /* sanity check on queue memory */
14384 if (!hrq || !drq)
14385 return -ENODEV;
14386 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14387 if (!mbox)
14388 return -ENOMEM;
14389 length = (sizeof(struct lpfc_mbx_rq_destroy) -
14390 sizeof(struct lpfc_sli4_cfg_mhdr));
14391 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14392 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14393 length, LPFC_SLI4_MBX_EMBED);
14394 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14395 hrq->queue_id);
14396 mbox->vport = hrq->phba->pport;
14397 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14398 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14399 /* The IOCTL status is embedded in the mailbox subheader. */
14400 shdr = (union lpfc_sli4_cfg_shdr *)
14401 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14402 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14403 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14404 if (shdr_status || shdr_add_status || rc) {
14405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14406 "2509 RQ_DESTROY mailbox failed with "
14407 "status x%x add_status x%x, mbx status x%x\n",
14408 shdr_status, shdr_add_status, rc);
14409 if (rc != MBX_TIMEOUT)
14410 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14411 return -ENXIO;
14412 }
14413 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14414 drq->queue_id);
14415 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14416 shdr = (union lpfc_sli4_cfg_shdr *)
14417 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14418 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14419 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14420 if (shdr_status || shdr_add_status || rc) {
14421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14422 "2510 RQ_DESTROY mailbox failed with "
14423 "status x%x add_status x%x, mbx status x%x\n",
14424 shdr_status, shdr_add_status, rc);
14425 status = -ENXIO;
14426 }
14427 list_del_init(&hrq->list);
14428 list_del_init(&drq->list);
14429 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14430 return status;
14431 }
14432
14433 /**
14434 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14435 * @phba: The virtual port for which this call being executed.
14436 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14437 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14438 * @xritag: the xritag that ties this io to the SGL pages.
14439 *
14440 * This routine will post the sgl pages for the IO that has the xritag
14441 * that is in the iocbq structure. The xritag is assigned during iocbq
14442 * creation and persists for as long as the driver is loaded.
14443 * if the caller has fewer than 256 scatter gather segments to map then
14444 * pdma_phys_addr1 should be 0.
14445 * If the caller needs to map more than 256 scatter gather segment then
14446 * pdma_phys_addr1 should be a valid physical address.
14447 * physical address for SGLs must be 64 byte aligned.
14448 * If you are going to map 2 SGL's then the first one must have 256 entries
14449 * the second sgl can have between 1 and 256 entries.
14450 *
14451 * Return codes:
14452 * 0 - Success
14453 * -ENXIO, -ENOMEM - Failure
14454 **/
14455 int
14456 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14457 dma_addr_t pdma_phys_addr0,
14458 dma_addr_t pdma_phys_addr1,
14459 uint16_t xritag)
14460 {
14461 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14462 LPFC_MBOXQ_t *mbox;
14463 int rc;
14464 uint32_t shdr_status, shdr_add_status;
14465 uint32_t mbox_tmo;
14466 union lpfc_sli4_cfg_shdr *shdr;
14467
14468 if (xritag == NO_XRI) {
14469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14470 "0364 Invalid param:\n");
14471 return -EINVAL;
14472 }
14473
14474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14475 if (!mbox)
14476 return -ENOMEM;
14477
14478 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14479 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14480 sizeof(struct lpfc_mbx_post_sgl_pages) -
14481 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
14482
14483 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14484 &mbox->u.mqe.un.post_sgl_pages;
14485 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14486 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14487
14488 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14489 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14490 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14491 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14492
14493 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14494 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14495 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14496 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14497 if (!phba->sli4_hba.intr_enable)
14498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14499 else {
14500 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14501 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14502 }
14503 /* The IOCTL status is embedded in the mailbox subheader. */
14504 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14505 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14506 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14507 if (rc != MBX_TIMEOUT)
14508 mempool_free(mbox, phba->mbox_mem_pool);
14509 if (shdr_status || shdr_add_status || rc) {
14510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14511 "2511 POST_SGL mailbox failed with "
14512 "status x%x add_status x%x, mbx status x%x\n",
14513 shdr_status, shdr_add_status, rc);
14514 }
14515 return 0;
14516 }
14517
14518 /**
14519 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
14520 * @phba: pointer to lpfc hba data structure.
14521 *
14522 * This routine is invoked to post rpi header templates to the
14523 * HBA consistent with the SLI-4 interface spec. This routine
14524 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14525 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14526 *
14527 * Returns
14528 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14529 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14530 **/
14531 static uint16_t
14532 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14533 {
14534 unsigned long xri;
14535
14536 /*
14537 * Fetch the next logical xri. Because this index is logical,
14538 * the driver starts at 0 each time.
14539 */
14540 spin_lock_irq(&phba->hbalock);
14541 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14542 phba->sli4_hba.max_cfg_param.max_xri, 0);
14543 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14544 spin_unlock_irq(&phba->hbalock);
14545 return NO_XRI;
14546 } else {
14547 set_bit(xri, phba->sli4_hba.xri_bmask);
14548 phba->sli4_hba.max_cfg_param.xri_used++;
14549 }
14550 spin_unlock_irq(&phba->hbalock);
14551 return xri;
14552 }
14553
14554 /**
14555 * lpfc_sli4_free_xri - Release an xri for reuse.
14556 * @phba: pointer to lpfc hba data structure.
14557 *
14558 * This routine is invoked to release an xri to the pool of
14559 * available rpis maintained by the driver.
14560 **/
14561 static void
14562 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14563 {
14564 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
14565 phba->sli4_hba.max_cfg_param.xri_used--;
14566 }
14567 }
14568
14569 /**
14570 * lpfc_sli4_free_xri - Release an xri for reuse.
14571 * @phba: pointer to lpfc hba data structure.
14572 *
14573 * This routine is invoked to release an xri to the pool of
14574 * available rpis maintained by the driver.
14575 **/
14576 void
14577 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14578 {
14579 spin_lock_irq(&phba->hbalock);
14580 __lpfc_sli4_free_xri(phba, xri);
14581 spin_unlock_irq(&phba->hbalock);
14582 }
14583
14584 /**
14585 * lpfc_sli4_next_xritag - Get an xritag for the io
14586 * @phba: Pointer to HBA context object.
14587 *
14588 * This function gets an xritag for the iocb. If there is no unused xritag
14589 * it will return 0xffff.
14590 * The function returns the allocated xritag if successful, else returns zero.
14591 * Zero is not a valid xritag.
14592 * The caller is not required to hold any lock.
14593 **/
14594 uint16_t
14595 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14596 {
14597 uint16_t xri_index;
14598
14599 xri_index = lpfc_sli4_alloc_xri(phba);
14600 if (xri_index == NO_XRI)
14601 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14602 "2004 Failed to allocate XRI.last XRITAG is %d"
14603 " Max XRI is %d, Used XRI is %d\n",
14604 xri_index,
14605 phba->sli4_hba.max_cfg_param.max_xri,
14606 phba->sli4_hba.max_cfg_param.xri_used);
14607 return xri_index;
14608 }
14609
14610 /**
14611 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
14612 * @phba: pointer to lpfc hba data structure.
14613 * @post_sgl_list: pointer to els sgl entry list.
14614 * @count: number of els sgl entries on the list.
14615 *
14616 * This routine is invoked to post a block of driver's sgl pages to the
14617 * HBA using non-embedded mailbox command. No Lock is held. This routine
14618 * is only called when the driver is loading and after all IO has been
14619 * stopped.
14620 **/
14621 static int
14622 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14623 struct list_head *post_sgl_list,
14624 int post_cnt)
14625 {
14626 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
14627 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14628 struct sgl_page_pairs *sgl_pg_pairs;
14629 void *viraddr;
14630 LPFC_MBOXQ_t *mbox;
14631 uint32_t reqlen, alloclen, pg_pairs;
14632 uint32_t mbox_tmo;
14633 uint16_t xritag_start = 0;
14634 int rc = 0;
14635 uint32_t shdr_status, shdr_add_status;
14636 union lpfc_sli4_cfg_shdr *shdr;
14637
14638 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
14639 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14640 if (reqlen > SLI4_PAGE_SIZE) {
14641 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14642 "2559 Block sgl registration required DMA "
14643 "size (%d) great than a page\n", reqlen);
14644 return -ENOMEM;
14645 }
14646 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14647 if (!mbox)
14648 return -ENOMEM;
14649
14650 /* Allocate DMA memory and set up the non-embedded mailbox command */
14651 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14652 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14653 LPFC_SLI4_MBX_NEMBED);
14654
14655 if (alloclen < reqlen) {
14656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14657 "0285 Allocated DMA memory size (%d) is "
14658 "less than the requested DMA memory "
14659 "size (%d)\n", alloclen, reqlen);
14660 lpfc_sli4_mbox_cmd_free(phba, mbox);
14661 return -ENOMEM;
14662 }
14663 /* Set up the SGL pages in the non-embedded DMA pages */
14664 viraddr = mbox->sge_array->addr[0];
14665 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14666 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14667
14668 pg_pairs = 0;
14669 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
14670 /* Set up the sge entry */
14671 sgl_pg_pairs->sgl_pg0_addr_lo =
14672 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14673 sgl_pg_pairs->sgl_pg0_addr_hi =
14674 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14675 sgl_pg_pairs->sgl_pg1_addr_lo =
14676 cpu_to_le32(putPaddrLow(0));
14677 sgl_pg_pairs->sgl_pg1_addr_hi =
14678 cpu_to_le32(putPaddrHigh(0));
14679
14680 /* Keep the first xritag on the list */
14681 if (pg_pairs == 0)
14682 xritag_start = sglq_entry->sli4_xritag;
14683 sgl_pg_pairs++;
14684 pg_pairs++;
14685 }
14686
14687 /* Complete initialization and perform endian conversion. */
14688 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14689 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
14690 sgl->word0 = cpu_to_le32(sgl->word0);
14691 if (!phba->sli4_hba.intr_enable)
14692 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14693 else {
14694 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14695 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14696 }
14697 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14698 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14699 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14700 if (rc != MBX_TIMEOUT)
14701 lpfc_sli4_mbox_cmd_free(phba, mbox);
14702 if (shdr_status || shdr_add_status || rc) {
14703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14704 "2513 POST_SGL_BLOCK mailbox command failed "
14705 "status x%x add_status x%x mbx status x%x\n",
14706 shdr_status, shdr_add_status, rc);
14707 rc = -ENXIO;
14708 }
14709 return rc;
14710 }
14711
14712 /**
14713 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14714 * @phba: pointer to lpfc hba data structure.
14715 * @sblist: pointer to scsi buffer list.
14716 * @count: number of scsi buffers on the list.
14717 *
14718 * This routine is invoked to post a block of @count scsi sgl pages from a
14719 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14720 * No Lock is held.
14721 *
14722 **/
14723 int
14724 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14725 struct list_head *sblist,
14726 int count)
14727 {
14728 struct lpfc_scsi_buf *psb;
14729 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14730 struct sgl_page_pairs *sgl_pg_pairs;
14731 void *viraddr;
14732 LPFC_MBOXQ_t *mbox;
14733 uint32_t reqlen, alloclen, pg_pairs;
14734 uint32_t mbox_tmo;
14735 uint16_t xritag_start = 0;
14736 int rc = 0;
14737 uint32_t shdr_status, shdr_add_status;
14738 dma_addr_t pdma_phys_bpl1;
14739 union lpfc_sli4_cfg_shdr *shdr;
14740
14741 /* Calculate the requested length of the dma memory */
14742 reqlen = count * sizeof(struct sgl_page_pairs) +
14743 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14744 if (reqlen > SLI4_PAGE_SIZE) {
14745 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14746 "0217 Block sgl registration required DMA "
14747 "size (%d) great than a page\n", reqlen);
14748 return -ENOMEM;
14749 }
14750 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14751 if (!mbox) {
14752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14753 "0283 Failed to allocate mbox cmd memory\n");
14754 return -ENOMEM;
14755 }
14756
14757 /* Allocate DMA memory and set up the non-embedded mailbox command */
14758 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14759 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14760 LPFC_SLI4_MBX_NEMBED);
14761
14762 if (alloclen < reqlen) {
14763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14764 "2561 Allocated DMA memory size (%d) is "
14765 "less than the requested DMA memory "
14766 "size (%d)\n", alloclen, reqlen);
14767 lpfc_sli4_mbox_cmd_free(phba, mbox);
14768 return -ENOMEM;
14769 }
14770
14771 /* Get the first SGE entry from the non-embedded DMA memory */
14772 viraddr = mbox->sge_array->addr[0];
14773
14774 /* Set up the SGL pages in the non-embedded DMA pages */
14775 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14776 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14777
14778 pg_pairs = 0;
14779 list_for_each_entry(psb, sblist, list) {
14780 /* Set up the sge entry */
14781 sgl_pg_pairs->sgl_pg0_addr_lo =
14782 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14783 sgl_pg_pairs->sgl_pg0_addr_hi =
14784 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14785 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14786 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14787 else
14788 pdma_phys_bpl1 = 0;
14789 sgl_pg_pairs->sgl_pg1_addr_lo =
14790 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14791 sgl_pg_pairs->sgl_pg1_addr_hi =
14792 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14793 /* Keep the first xritag on the list */
14794 if (pg_pairs == 0)
14795 xritag_start = psb->cur_iocbq.sli4_xritag;
14796 sgl_pg_pairs++;
14797 pg_pairs++;
14798 }
14799 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14800 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14801 /* Perform endian conversion if necessary */
14802 sgl->word0 = cpu_to_le32(sgl->word0);
14803
14804 if (!phba->sli4_hba.intr_enable)
14805 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14806 else {
14807 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14808 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14809 }
14810 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14811 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14812 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14813 if (rc != MBX_TIMEOUT)
14814 lpfc_sli4_mbox_cmd_free(phba, mbox);
14815 if (shdr_status || shdr_add_status || rc) {
14816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14817 "2564 POST_SGL_BLOCK mailbox command failed "
14818 "status x%x add_status x%x mbx status x%x\n",
14819 shdr_status, shdr_add_status, rc);
14820 rc = -ENXIO;
14821 }
14822 return rc;
14823 }
14824
14825 /**
14826 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14827 * @phba: pointer to lpfc_hba struct that the frame was received on
14828 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14829 *
14830 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14831 * valid type of frame that the LPFC driver will handle. This function will
14832 * return a zero if the frame is a valid frame or a non zero value when the
14833 * frame does not pass the check.
14834 **/
14835 static int
14836 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14837 {
14838 /* make rctl_names static to save stack space */
14839 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
14840 char *type_names[] = FC_TYPE_NAMES_INIT;
14841 struct fc_vft_header *fc_vft_hdr;
14842 uint32_t *header = (uint32_t *) fc_hdr;
14843
14844 switch (fc_hdr->fh_r_ctl) {
14845 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14846 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14847 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14848 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14849 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14850 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14851 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14852 case FC_RCTL_DD_CMD_STATUS: /* command status */
14853 case FC_RCTL_ELS_REQ: /* extended link services request */
14854 case FC_RCTL_ELS_REP: /* extended link services reply */
14855 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14856 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14857 case FC_RCTL_BA_NOP: /* basic link service NOP */
14858 case FC_RCTL_BA_ABTS: /* basic link service abort */
14859 case FC_RCTL_BA_RMC: /* remove connection */
14860 case FC_RCTL_BA_ACC: /* basic accept */
14861 case FC_RCTL_BA_RJT: /* basic reject */
14862 case FC_RCTL_BA_PRMT:
14863 case FC_RCTL_ACK_1: /* acknowledge_1 */
14864 case FC_RCTL_ACK_0: /* acknowledge_0 */
14865 case FC_RCTL_P_RJT: /* port reject */
14866 case FC_RCTL_F_RJT: /* fabric reject */
14867 case FC_RCTL_P_BSY: /* port busy */
14868 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14869 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14870 case FC_RCTL_LCR: /* link credit reset */
14871 case FC_RCTL_END: /* end */
14872 break;
14873 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14874 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14875 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14876 return lpfc_fc_frame_check(phba, fc_hdr);
14877 default:
14878 goto drop;
14879 }
14880 switch (fc_hdr->fh_type) {
14881 case FC_TYPE_BLS:
14882 case FC_TYPE_ELS:
14883 case FC_TYPE_FCP:
14884 case FC_TYPE_CT:
14885 break;
14886 case FC_TYPE_IP:
14887 case FC_TYPE_ILS:
14888 default:
14889 goto drop;
14890 }
14891
14892 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14893 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14894 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14895 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14896 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
14897 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14898 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
14899 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14900 be32_to_cpu(header[6]));
14901 return 0;
14902 drop:
14903 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14904 "2539 Dropped frame rctl:%s type:%s\n",
14905 rctl_names[fc_hdr->fh_r_ctl],
14906 type_names[fc_hdr->fh_type]);
14907 return 1;
14908 }
14909
14910 /**
14911 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14912 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14913 *
14914 * This function processes the FC header to retrieve the VFI from the VF
14915 * header, if one exists. This function will return the VFI if one exists
14916 * or 0 if no VSAN Header exists.
14917 **/
14918 static uint32_t
14919 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14920 {
14921 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14922
14923 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14924 return 0;
14925 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14926 }
14927
14928 /**
14929 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14930 * @phba: Pointer to the HBA structure to search for the vport on
14931 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14932 * @fcfi: The FC Fabric ID that the frame came from
14933 *
14934 * This function searches the @phba for a vport that matches the content of the
14935 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14936 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14937 * returns the matching vport pointer or NULL if unable to match frame to a
14938 * vport.
14939 **/
14940 static struct lpfc_vport *
14941 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14942 uint16_t fcfi)
14943 {
14944 struct lpfc_vport **vports;
14945 struct lpfc_vport *vport = NULL;
14946 int i;
14947 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14948 fc_hdr->fh_d_id[1] << 8 |
14949 fc_hdr->fh_d_id[2]);
14950
14951 if (did == Fabric_DID)
14952 return phba->pport;
14953 if ((phba->pport->fc_flag & FC_PT2PT) &&
14954 !(phba->link_state == LPFC_HBA_READY))
14955 return phba->pport;
14956
14957 vports = lpfc_create_vport_work_array(phba);
14958 if (vports != NULL)
14959 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14960 if (phba->fcf.fcfi == fcfi &&
14961 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14962 vports[i]->fc_myDID == did) {
14963 vport = vports[i];
14964 break;
14965 }
14966 }
14967 lpfc_destroy_vport_work_array(phba, vports);
14968 return vport;
14969 }
14970
14971 /**
14972 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14973 * @vport: The vport to work on.
14974 *
14975 * This function updates the receive sequence time stamp for this vport. The
14976 * receive sequence time stamp indicates the time that the last frame of the
14977 * the sequence that has been idle for the longest amount of time was received.
14978 * the driver uses this time stamp to indicate if any received sequences have
14979 * timed out.
14980 **/
14981 static void
14982 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14983 {
14984 struct lpfc_dmabuf *h_buf;
14985 struct hbq_dmabuf *dmabuf = NULL;
14986
14987 /* get the oldest sequence on the rcv list */
14988 h_buf = list_get_first(&vport->rcv_buffer_list,
14989 struct lpfc_dmabuf, list);
14990 if (!h_buf)
14991 return;
14992 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14993 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14994 }
14995
14996 /**
14997 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14998 * @vport: The vport that the received sequences were sent to.
14999 *
15000 * This function cleans up all outstanding received sequences. This is called
15001 * by the driver when a link event or user action invalidates all the received
15002 * sequences.
15003 **/
15004 void
15005 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
15006 {
15007 struct lpfc_dmabuf *h_buf, *hnext;
15008 struct lpfc_dmabuf *d_buf, *dnext;
15009 struct hbq_dmabuf *dmabuf = NULL;
15010
15011 /* start with the oldest sequence on the rcv list */
15012 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15013 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15014 list_del_init(&dmabuf->hbuf.list);
15015 list_for_each_entry_safe(d_buf, dnext,
15016 &dmabuf->dbuf.list, list) {
15017 list_del_init(&d_buf->list);
15018 lpfc_in_buf_free(vport->phba, d_buf);
15019 }
15020 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15021 }
15022 }
15023
15024 /**
15025 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15026 * @vport: The vport that the received sequences were sent to.
15027 *
15028 * This function determines whether any received sequences have timed out by
15029 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15030 * indicates that there is at least one timed out sequence this routine will
15031 * go through the received sequences one at a time from most inactive to most
15032 * active to determine which ones need to be cleaned up. Once it has determined
15033 * that a sequence needs to be cleaned up it will simply free up the resources
15034 * without sending an abort.
15035 **/
15036 void
15037 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15038 {
15039 struct lpfc_dmabuf *h_buf, *hnext;
15040 struct lpfc_dmabuf *d_buf, *dnext;
15041 struct hbq_dmabuf *dmabuf = NULL;
15042 unsigned long timeout;
15043 int abort_count = 0;
15044
15045 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15046 vport->rcv_buffer_time_stamp);
15047 if (list_empty(&vport->rcv_buffer_list) ||
15048 time_before(jiffies, timeout))
15049 return;
15050 /* start with the oldest sequence on the rcv list */
15051 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15052 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15053 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15054 dmabuf->time_stamp);
15055 if (time_before(jiffies, timeout))
15056 break;
15057 abort_count++;
15058 list_del_init(&dmabuf->hbuf.list);
15059 list_for_each_entry_safe(d_buf, dnext,
15060 &dmabuf->dbuf.list, list) {
15061 list_del_init(&d_buf->list);
15062 lpfc_in_buf_free(vport->phba, d_buf);
15063 }
15064 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15065 }
15066 if (abort_count)
15067 lpfc_update_rcv_time_stamp(vport);
15068 }
15069
15070 /**
15071 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15072 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15073 *
15074 * This function searches through the existing incomplete sequences that have
15075 * been sent to this @vport. If the frame matches one of the incomplete
15076 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15077 * make up that sequence. If no sequence is found that matches this frame then
15078 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15079 * This function returns a pointer to the first dmabuf in the sequence list that
15080 * the frame was linked to.
15081 **/
15082 static struct hbq_dmabuf *
15083 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15084 {
15085 struct fc_frame_header *new_hdr;
15086 struct fc_frame_header *temp_hdr;
15087 struct lpfc_dmabuf *d_buf;
15088 struct lpfc_dmabuf *h_buf;
15089 struct hbq_dmabuf *seq_dmabuf = NULL;
15090 struct hbq_dmabuf *temp_dmabuf = NULL;
15091 uint8_t found = 0;
15092
15093 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15094 dmabuf->time_stamp = jiffies;
15095 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15096
15097 /* Use the hdr_buf to find the sequence that this frame belongs to */
15098 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15099 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15100 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15101 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15102 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15103 continue;
15104 /* found a pending sequence that matches this frame */
15105 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15106 break;
15107 }
15108 if (!seq_dmabuf) {
15109 /*
15110 * This indicates first frame received for this sequence.
15111 * Queue the buffer on the vport's rcv_buffer_list.
15112 */
15113 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15114 lpfc_update_rcv_time_stamp(vport);
15115 return dmabuf;
15116 }
15117 temp_hdr = seq_dmabuf->hbuf.virt;
15118 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15119 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
15120 list_del_init(&seq_dmabuf->hbuf.list);
15121 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15122 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15123 lpfc_update_rcv_time_stamp(vport);
15124 return dmabuf;
15125 }
15126 /* move this sequence to the tail to indicate a young sequence */
15127 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15128 seq_dmabuf->time_stamp = jiffies;
15129 lpfc_update_rcv_time_stamp(vport);
15130 if (list_empty(&seq_dmabuf->dbuf.list)) {
15131 temp_hdr = dmabuf->hbuf.virt;
15132 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15133 return seq_dmabuf;
15134 }
15135 /* find the correct place in the sequence to insert this frame */
15136 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15137 while (!found) {
15138 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15139 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15140 /*
15141 * If the frame's sequence count is greater than the frame on
15142 * the list then insert the frame right after this frame
15143 */
15144 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15145 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
15146 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
15147 found = 1;
15148 break;
15149 }
15150
15151 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15152 break;
15153 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
15154 }
15155
15156 if (found)
15157 return seq_dmabuf;
15158 return NULL;
15159 }
15160
15161 /**
15162 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15163 * @vport: pointer to a vitural port
15164 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15165 *
15166 * This function tries to abort from the partially assembed sequence, described
15167 * by the information from basic abbort @dmabuf. It checks to see whether such
15168 * partially assembled sequence held by the driver. If so, it shall free up all
15169 * the frames from the partially assembled sequence.
15170 *
15171 * Return
15172 * true -- if there is matching partially assembled sequence present and all
15173 * the frames freed with the sequence;
15174 * false -- if there is no matching partially assembled sequence present so
15175 * nothing got aborted in the lower layer driver
15176 **/
15177 static bool
15178 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15179 struct hbq_dmabuf *dmabuf)
15180 {
15181 struct fc_frame_header *new_hdr;
15182 struct fc_frame_header *temp_hdr;
15183 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15184 struct hbq_dmabuf *seq_dmabuf = NULL;
15185
15186 /* Use the hdr_buf to find the sequence that matches this frame */
15187 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15188 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15189 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15190 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15191 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15192 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15193 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15194 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15195 continue;
15196 /* found a pending sequence that matches this frame */
15197 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15198 break;
15199 }
15200
15201 /* Free up all the frames from the partially assembled sequence */
15202 if (seq_dmabuf) {
15203 list_for_each_entry_safe(d_buf, n_buf,
15204 &seq_dmabuf->dbuf.list, list) {
15205 list_del_init(&d_buf->list);
15206 lpfc_in_buf_free(vport->phba, d_buf);
15207 }
15208 return true;
15209 }
15210 return false;
15211 }
15212
15213 /**
15214 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15215 * @vport: pointer to a vitural port
15216 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15217 *
15218 * This function tries to abort from the assembed sequence from upper level
15219 * protocol, described by the information from basic abbort @dmabuf. It
15220 * checks to see whether such pending context exists at upper level protocol.
15221 * If so, it shall clean up the pending context.
15222 *
15223 * Return
15224 * true -- if there is matching pending context of the sequence cleaned
15225 * at ulp;
15226 * false -- if there is no matching pending context of the sequence present
15227 * at ulp.
15228 **/
15229 static bool
15230 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15231 {
15232 struct lpfc_hba *phba = vport->phba;
15233 int handled;
15234
15235 /* Accepting abort at ulp with SLI4 only */
15236 if (phba->sli_rev < LPFC_SLI_REV4)
15237 return false;
15238
15239 /* Register all caring upper level protocols to attend abort */
15240 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15241 if (handled)
15242 return true;
15243
15244 return false;
15245 }
15246
15247 /**
15248 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
15249 * @phba: Pointer to HBA context object.
15250 * @cmd_iocbq: pointer to the command iocbq structure.
15251 * @rsp_iocbq: pointer to the response iocbq structure.
15252 *
15253 * This function handles the sequence abort response iocb command complete
15254 * event. It properly releases the memory allocated to the sequence abort
15255 * accept iocb.
15256 **/
15257 static void
15258 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
15259 struct lpfc_iocbq *cmd_iocbq,
15260 struct lpfc_iocbq *rsp_iocbq)
15261 {
15262 struct lpfc_nodelist *ndlp;
15263
15264 if (cmd_iocbq) {
15265 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15266 lpfc_nlp_put(ndlp);
15267 lpfc_nlp_not_used(ndlp);
15268 lpfc_sli_release_iocbq(phba, cmd_iocbq);
15269 }
15270
15271 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15272 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15274 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15275 rsp_iocbq->iocb.ulpStatus,
15276 rsp_iocbq->iocb.un.ulpWord[4]);
15277 }
15278
15279 /**
15280 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15281 * @phba: Pointer to HBA context object.
15282 * @xri: xri id in transaction.
15283 *
15284 * This function validates the xri maps to the known range of XRIs allocated an
15285 * used by the driver.
15286 **/
15287 uint16_t
15288 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15289 uint16_t xri)
15290 {
15291 uint16_t i;
15292
15293 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15294 if (xri == phba->sli4_hba.xri_ids[i])
15295 return i;
15296 }
15297 return NO_XRI;
15298 }
15299
15300 /**
15301 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
15302 * @phba: Pointer to HBA context object.
15303 * @fc_hdr: pointer to a FC frame header.
15304 *
15305 * This function sends a basic response to a previous unsol sequence abort
15306 * event after aborting the sequence handling.
15307 **/
15308 static void
15309 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15310 struct fc_frame_header *fc_hdr, bool aborted)
15311 {
15312 struct lpfc_hba *phba = vport->phba;
15313 struct lpfc_iocbq *ctiocb = NULL;
15314 struct lpfc_nodelist *ndlp;
15315 uint16_t oxid, rxid, xri, lxri;
15316 uint32_t sid, fctl;
15317 IOCB_t *icmd;
15318 int rc;
15319
15320 if (!lpfc_is_link_up(phba))
15321 return;
15322
15323 sid = sli4_sid_from_fc_hdr(fc_hdr);
15324 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
15325 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
15326
15327 ndlp = lpfc_findnode_did(vport, sid);
15328 if (!ndlp) {
15329 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15330 if (!ndlp) {
15331 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15332 "1268 Failed to allocate ndlp for "
15333 "oxid:x%x SID:x%x\n", oxid, sid);
15334 return;
15335 }
15336 lpfc_nlp_init(vport, ndlp, sid);
15337 /* Put ndlp onto pport node list */
15338 lpfc_enqueue_node(vport, ndlp);
15339 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15340 /* re-setup ndlp without removing from node list */
15341 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15342 if (!ndlp) {
15343 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15344 "3275 Failed to active ndlp found "
15345 "for oxid:x%x SID:x%x\n", oxid, sid);
15346 return;
15347 }
15348 }
15349
15350 /* Allocate buffer for rsp iocb */
15351 ctiocb = lpfc_sli_get_iocbq(phba);
15352 if (!ctiocb)
15353 return;
15354
15355 /* Extract the F_CTL field from FC_HDR */
15356 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15357
15358 icmd = &ctiocb->iocb;
15359 icmd->un.xseq64.bdl.bdeSize = 0;
15360 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
15361 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15362 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15363 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15364
15365 /* Fill in the rest of iocb fields */
15366 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15367 icmd->ulpBdeCount = 0;
15368 icmd->ulpLe = 1;
15369 icmd->ulpClass = CLASS3;
15370 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15371 ctiocb->context1 = lpfc_nlp_get(ndlp);
15372
15373 ctiocb->iocb_cmpl = NULL;
15374 ctiocb->vport = phba->pport;
15375 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
15376 ctiocb->sli4_lxritag = NO_XRI;
15377 ctiocb->sli4_xritag = NO_XRI;
15378
15379 if (fctl & FC_FC_EX_CTX)
15380 /* Exchange responder sent the abort so we
15381 * own the oxid.
15382 */
15383 xri = oxid;
15384 else
15385 xri = rxid;
15386 lxri = lpfc_sli4_xri_inrange(phba, xri);
15387 if (lxri != NO_XRI)
15388 lpfc_set_rrq_active(phba, ndlp, lxri,
15389 (xri == oxid) ? rxid : oxid, 0);
15390 /* For BA_ABTS from exchange responder, if the logical xri with
15391 * the oxid maps to the FCP XRI range, the port no longer has
15392 * that exchange context, send a BLS_RJT. Override the IOCB for
15393 * a BA_RJT.
15394 */
15395 if ((fctl & FC_FC_EX_CTX) &&
15396 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15397 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15398 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15399 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15400 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15401 }
15402
15403 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15404 * the driver no longer has that exchange, send a BLS_RJT. Override
15405 * the IOCB for a BA_RJT.
15406 */
15407 if (aborted == false) {
15408 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15409 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15410 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15411 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15412 }
15413
15414 if (fctl & FC_FC_EX_CTX) {
15415 /* ABTS sent by responder to CT exchange, construction
15416 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15417 * field and RX_ID from ABTS for RX_ID field.
15418 */
15419 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
15420 } else {
15421 /* ABTS sent by initiator to CT exchange, construction
15422 * of BA_ACC will need to allocate a new XRI as for the
15423 * XRI_TAG field.
15424 */
15425 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
15426 }
15427 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
15428 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
15429
15430 /* Xmit CT abts response on exchange <xid> */
15431 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15432 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15433 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
15434
15435 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15436 if (rc == IOCB_ERROR) {
15437 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15438 "2925 Failed to issue CT ABTS RSP x%x on "
15439 "xri x%x, Data x%x\n",
15440 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15441 phba->link_state);
15442 lpfc_nlp_put(ndlp);
15443 ctiocb->context1 = NULL;
15444 lpfc_sli_release_iocbq(phba, ctiocb);
15445 }
15446 }
15447
15448 /**
15449 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15450 * @vport: Pointer to the vport on which this sequence was received
15451 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15452 *
15453 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15454 * receive sequence is only partially assembed by the driver, it shall abort
15455 * the partially assembled frames for the sequence. Otherwise, if the
15456 * unsolicited receive sequence has been completely assembled and passed to
15457 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15458 * unsolicited sequence has been aborted. After that, it will issue a basic
15459 * accept to accept the abort.
15460 **/
15461 static void
15462 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15463 struct hbq_dmabuf *dmabuf)
15464 {
15465 struct lpfc_hba *phba = vport->phba;
15466 struct fc_frame_header fc_hdr;
15467 uint32_t fctl;
15468 bool aborted;
15469
15470 /* Make a copy of fc_hdr before the dmabuf being released */
15471 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
15472 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
15473
15474 if (fctl & FC_FC_EX_CTX) {
15475 /* ABTS by responder to exchange, no cleanup needed */
15476 aborted = true;
15477 } else {
15478 /* ABTS by initiator to exchange, need to do cleanup */
15479 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15480 if (aborted == false)
15481 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
15482 }
15483 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15484
15485 /* Respond with BA_ACC or BA_RJT accordingly */
15486 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
15487 }
15488
15489 /**
15490 * lpfc_seq_complete - Indicates if a sequence is complete
15491 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15492 *
15493 * This function checks the sequence, starting with the frame described by
15494 * @dmabuf, to see if all the frames associated with this sequence are present.
15495 * the frames associated with this sequence are linked to the @dmabuf using the
15496 * dbuf list. This function looks for two major things. 1) That the first frame
15497 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15498 * set. 3) That there are no holes in the sequence count. The function will
15499 * return 1 when the sequence is complete, otherwise it will return 0.
15500 **/
15501 static int
15502 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15503 {
15504 struct fc_frame_header *hdr;
15505 struct lpfc_dmabuf *d_buf;
15506 struct hbq_dmabuf *seq_dmabuf;
15507 uint32_t fctl;
15508 int seq_count = 0;
15509
15510 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15511 /* make sure first fame of sequence has a sequence count of zero */
15512 if (hdr->fh_seq_cnt != seq_count)
15513 return 0;
15514 fctl = (hdr->fh_f_ctl[0] << 16 |
15515 hdr->fh_f_ctl[1] << 8 |
15516 hdr->fh_f_ctl[2]);
15517 /* If last frame of sequence we can return success. */
15518 if (fctl & FC_FC_END_SEQ)
15519 return 1;
15520 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15521 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15522 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15523 /* If there is a hole in the sequence count then fail. */
15524 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
15525 return 0;
15526 fctl = (hdr->fh_f_ctl[0] << 16 |
15527 hdr->fh_f_ctl[1] << 8 |
15528 hdr->fh_f_ctl[2]);
15529 /* If last frame of sequence we can return success. */
15530 if (fctl & FC_FC_END_SEQ)
15531 return 1;
15532 }
15533 return 0;
15534 }
15535
15536 /**
15537 * lpfc_prep_seq - Prep sequence for ULP processing
15538 * @vport: Pointer to the vport on which this sequence was received
15539 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15540 *
15541 * This function takes a sequence, described by a list of frames, and creates
15542 * a list of iocbq structures to describe the sequence. This iocbq list will be
15543 * used to issue to the generic unsolicited sequence handler. This routine
15544 * returns a pointer to the first iocbq in the list. If the function is unable
15545 * to allocate an iocbq then it throw out the received frames that were not
15546 * able to be described and return a pointer to the first iocbq. If unable to
15547 * allocate any iocbqs (including the first) this function will return NULL.
15548 **/
15549 static struct lpfc_iocbq *
15550 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15551 {
15552 struct hbq_dmabuf *hbq_buf;
15553 struct lpfc_dmabuf *d_buf, *n_buf;
15554 struct lpfc_iocbq *first_iocbq, *iocbq;
15555 struct fc_frame_header *fc_hdr;
15556 uint32_t sid;
15557 uint32_t len, tot_len;
15558 struct ulp_bde64 *pbde;
15559
15560 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15561 /* remove from receive buffer list */
15562 list_del_init(&seq_dmabuf->hbuf.list);
15563 lpfc_update_rcv_time_stamp(vport);
15564 /* get the Remote Port's SID */
15565 sid = sli4_sid_from_fc_hdr(fc_hdr);
15566 tot_len = 0;
15567 /* Get an iocbq struct to fill in. */
15568 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15569 if (first_iocbq) {
15570 /* Initialize the first IOCB. */
15571 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
15572 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
15573
15574 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15575 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15576 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15577 first_iocbq->iocb.un.rcvels.parmRo =
15578 sli4_did_from_fc_hdr(fc_hdr);
15579 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15580 } else
15581 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
15582 first_iocbq->iocb.ulpContext = NO_XRI;
15583 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15584 be16_to_cpu(fc_hdr->fh_ox_id);
15585 /* iocbq is prepped for internal consumption. Physical vpi. */
15586 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15587 vport->phba->vpi_ids[vport->vpi];
15588 /* put the first buffer into the first IOCBq */
15589 tot_len = bf_get(lpfc_rcqe_length,
15590 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15591
15592 first_iocbq->context2 = &seq_dmabuf->dbuf;
15593 first_iocbq->context3 = NULL;
15594 first_iocbq->iocb.ulpBdeCount = 1;
15595 if (tot_len > LPFC_DATA_BUF_SIZE)
15596 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15597 LPFC_DATA_BUF_SIZE;
15598 else
15599 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15600
15601 first_iocbq->iocb.un.rcvels.remoteID = sid;
15602
15603 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15604 }
15605 iocbq = first_iocbq;
15606 /*
15607 * Each IOCBq can have two Buffers assigned, so go through the list
15608 * of buffers for this sequence and save two buffers in each IOCBq
15609 */
15610 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15611 if (!iocbq) {
15612 lpfc_in_buf_free(vport->phba, d_buf);
15613 continue;
15614 }
15615 if (!iocbq->context3) {
15616 iocbq->context3 = d_buf;
15617 iocbq->iocb.ulpBdeCount++;
15618 /* We need to get the size out of the right CQE */
15619 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15620 len = bf_get(lpfc_rcqe_length,
15621 &hbq_buf->cq_event.cqe.rcqe_cmpl);
15622 pbde = (struct ulp_bde64 *)
15623 &iocbq->iocb.unsli3.sli3Words[4];
15624 if (len > LPFC_DATA_BUF_SIZE)
15625 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15626 else
15627 pbde->tus.f.bdeSize = len;
15628
15629 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15630 tot_len += len;
15631 } else {
15632 iocbq = lpfc_sli_get_iocbq(vport->phba);
15633 if (!iocbq) {
15634 if (first_iocbq) {
15635 first_iocbq->iocb.ulpStatus =
15636 IOSTAT_FCP_RSP_ERROR;
15637 first_iocbq->iocb.un.ulpWord[4] =
15638 IOERR_NO_RESOURCES;
15639 }
15640 lpfc_in_buf_free(vport->phba, d_buf);
15641 continue;
15642 }
15643 /* We need to get the size out of the right CQE */
15644 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15645 len = bf_get(lpfc_rcqe_length,
15646 &hbq_buf->cq_event.cqe.rcqe_cmpl);
15647 iocbq->context2 = d_buf;
15648 iocbq->context3 = NULL;
15649 iocbq->iocb.ulpBdeCount = 1;
15650 if (len > LPFC_DATA_BUF_SIZE)
15651 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15652 LPFC_DATA_BUF_SIZE;
15653 else
15654 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15655
15656 tot_len += len;
15657 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15658
15659 iocbq->iocb.un.rcvels.remoteID = sid;
15660 list_add_tail(&iocbq->list, &first_iocbq->list);
15661 }
15662 }
15663 return first_iocbq;
15664 }
15665
15666 static void
15667 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15668 struct hbq_dmabuf *seq_dmabuf)
15669 {
15670 struct fc_frame_header *fc_hdr;
15671 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15672 struct lpfc_hba *phba = vport->phba;
15673
15674 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15675 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15676 if (!iocbq) {
15677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15678 "2707 Ring %d handler: Failed to allocate "
15679 "iocb Rctl x%x Type x%x received\n",
15680 LPFC_ELS_RING,
15681 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15682 return;
15683 }
15684 if (!lpfc_complete_unsol_iocb(phba,
15685 &phba->sli.ring[LPFC_ELS_RING],
15686 iocbq, fc_hdr->fh_r_ctl,
15687 fc_hdr->fh_type))
15688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15689 "2540 Ring %d handler: unexpected Rctl "
15690 "x%x Type x%x received\n",
15691 LPFC_ELS_RING,
15692 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15693
15694 /* Free iocb created in lpfc_prep_seq */
15695 list_for_each_entry_safe(curr_iocb, next_iocb,
15696 &iocbq->list, list) {
15697 list_del_init(&curr_iocb->list);
15698 lpfc_sli_release_iocbq(phba, curr_iocb);
15699 }
15700 lpfc_sli_release_iocbq(phba, iocbq);
15701 }
15702
15703 /**
15704 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15705 * @phba: Pointer to HBA context object.
15706 *
15707 * This function is called with no lock held. This function processes all
15708 * the received buffers and gives it to upper layers when a received buffer
15709 * indicates that it is the final frame in the sequence. The interrupt
15710 * service routine processes received buffers at interrupt contexts and adds
15711 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15712 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15713 * appropriate receive function when the final frame in a sequence is received.
15714 **/
15715 void
15716 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15717 struct hbq_dmabuf *dmabuf)
15718 {
15719 struct hbq_dmabuf *seq_dmabuf;
15720 struct fc_frame_header *fc_hdr;
15721 struct lpfc_vport *vport;
15722 uint32_t fcfi;
15723 uint32_t did;
15724
15725 /* Process each received buffer */
15726 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15727 /* check to see if this a valid type of frame */
15728 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15729 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15730 return;
15731 }
15732 if ((bf_get(lpfc_cqe_code,
15733 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15734 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15735 &dmabuf->cq_event.cqe.rcqe_cmpl);
15736 else
15737 fcfi = bf_get(lpfc_rcqe_fcf_id,
15738 &dmabuf->cq_event.cqe.rcqe_cmpl);
15739
15740 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
15741 if (!vport) {
15742 /* throw out the frame */
15743 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15744 return;
15745 }
15746
15747 /* d_id this frame is directed to */
15748 did = sli4_did_from_fc_hdr(fc_hdr);
15749
15750 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15751 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15752 (did != Fabric_DID)) {
15753 /*
15754 * Throw out the frame if we are not pt2pt.
15755 * The pt2pt protocol allows for discovery frames
15756 * to be received without a registered VPI.
15757 */
15758 if (!(vport->fc_flag & FC_PT2PT) ||
15759 (phba->link_state == LPFC_HBA_READY)) {
15760 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15761 return;
15762 }
15763 }
15764
15765 /* Handle the basic abort sequence (BA_ABTS) event */
15766 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15767 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15768 return;
15769 }
15770
15771 /* Link this frame */
15772 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15773 if (!seq_dmabuf) {
15774 /* unable to add frame to vport - throw it out */
15775 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15776 return;
15777 }
15778 /* If not last frame in sequence continue processing frames. */
15779 if (!lpfc_seq_complete(seq_dmabuf))
15780 return;
15781
15782 /* Send the complete sequence to the upper layer protocol */
15783 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
15784 }
15785
15786 /**
15787 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15788 * @phba: pointer to lpfc hba data structure.
15789 *
15790 * This routine is invoked to post rpi header templates to the
15791 * HBA consistent with the SLI-4 interface spec. This routine
15792 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15793 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15794 *
15795 * This routine does not require any locks. It's usage is expected
15796 * to be driver load or reset recovery when the driver is
15797 * sequential.
15798 *
15799 * Return codes
15800 * 0 - successful
15801 * -EIO - The mailbox failed to complete successfully.
15802 * When this error occurs, the driver is not guaranteed
15803 * to have any rpi regions posted to the device and
15804 * must either attempt to repost the regions or take a
15805 * fatal error.
15806 **/
15807 int
15808 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15809 {
15810 struct lpfc_rpi_hdr *rpi_page;
15811 uint32_t rc = 0;
15812 uint16_t lrpi = 0;
15813
15814 /* SLI4 ports that support extents do not require RPI headers. */
15815 if (!phba->sli4_hba.rpi_hdrs_in_use)
15816 goto exit;
15817 if (phba->sli4_hba.extents_in_use)
15818 return -EIO;
15819
15820 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
15821 /*
15822 * Assign the rpi headers a physical rpi only if the driver
15823 * has not initialized those resources. A port reset only
15824 * needs the headers posted.
15825 */
15826 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15827 LPFC_RPI_RSRC_RDY)
15828 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15829
15830 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15831 if (rc != MBX_SUCCESS) {
15832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15833 "2008 Error %d posting all rpi "
15834 "headers\n", rc);
15835 rc = -EIO;
15836 break;
15837 }
15838 }
15839
15840 exit:
15841 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15842 LPFC_RPI_RSRC_RDY);
15843 return rc;
15844 }
15845
15846 /**
15847 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15848 * @phba: pointer to lpfc hba data structure.
15849 * @rpi_page: pointer to the rpi memory region.
15850 *
15851 * This routine is invoked to post a single rpi header to the
15852 * HBA consistent with the SLI-4 interface spec. This memory region
15853 * maps up to 64 rpi context regions.
15854 *
15855 * Return codes
15856 * 0 - successful
15857 * -ENOMEM - No available memory
15858 * -EIO - The mailbox failed to complete successfully.
15859 **/
15860 int
15861 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15862 {
15863 LPFC_MBOXQ_t *mboxq;
15864 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15865 uint32_t rc = 0;
15866 uint32_t shdr_status, shdr_add_status;
15867 union lpfc_sli4_cfg_shdr *shdr;
15868
15869 /* SLI4 ports that support extents do not require RPI headers. */
15870 if (!phba->sli4_hba.rpi_hdrs_in_use)
15871 return rc;
15872 if (phba->sli4_hba.extents_in_use)
15873 return -EIO;
15874
15875 /* The port is notified of the header region via a mailbox command. */
15876 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15877 if (!mboxq) {
15878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15879 "2001 Unable to allocate memory for issuing "
15880 "SLI_CONFIG_SPECIAL mailbox command\n");
15881 return -ENOMEM;
15882 }
15883
15884 /* Post all rpi memory regions to the port. */
15885 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
15886 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15887 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15888 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
15889 sizeof(struct lpfc_sli4_cfg_mhdr),
15890 LPFC_SLI4_MBX_EMBED);
15891
15892
15893 /* Post the physical rpi to the port for this rpi header. */
15894 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15895 rpi_page->start_rpi);
15896 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15897 hdr_tmpl, rpi_page->page_count);
15898
15899 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15900 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
15901 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15902 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15905 if (rc != MBX_TIMEOUT)
15906 mempool_free(mboxq, phba->mbox_mem_pool);
15907 if (shdr_status || shdr_add_status || rc) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15909 "2514 POST_RPI_HDR mailbox failed with "
15910 "status x%x add_status x%x, mbx status x%x\n",
15911 shdr_status, shdr_add_status, rc);
15912 rc = -ENXIO;
15913 }
15914 return rc;
15915 }
15916
15917 /**
15918 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15919 * @phba: pointer to lpfc hba data structure.
15920 *
15921 * This routine is invoked to post rpi header templates to the
15922 * HBA consistent with the SLI-4 interface spec. This routine
15923 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15924 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15925 *
15926 * Returns
15927 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15928 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15929 **/
15930 int
15931 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15932 {
15933 unsigned long rpi;
15934 uint16_t max_rpi, rpi_limit;
15935 uint16_t rpi_remaining, lrpi = 0;
15936 struct lpfc_rpi_hdr *rpi_hdr;
15937 unsigned long iflag;
15938
15939 /*
15940 * Fetch the next logical rpi. Because this index is logical,
15941 * the driver starts at 0 each time.
15942 */
15943 spin_lock_irqsave(&phba->hbalock, iflag);
15944 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15945 rpi_limit = phba->sli4_hba.next_rpi;
15946
15947 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15948 if (rpi >= rpi_limit)
15949 rpi = LPFC_RPI_ALLOC_ERROR;
15950 else {
15951 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15952 phba->sli4_hba.max_cfg_param.rpi_used++;
15953 phba->sli4_hba.rpi_count++;
15954 }
15955 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15956 "0001 rpi:%x max:%x lim:%x\n",
15957 (int) rpi, max_rpi, rpi_limit);
15958
15959 /*
15960 * Don't try to allocate more rpi header regions if the device limit
15961 * has been exhausted.
15962 */
15963 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15964 (phba->sli4_hba.rpi_count >= max_rpi)) {
15965 spin_unlock_irqrestore(&phba->hbalock, iflag);
15966 return rpi;
15967 }
15968
15969 /*
15970 * RPI header postings are not required for SLI4 ports capable of
15971 * extents.
15972 */
15973 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15974 spin_unlock_irqrestore(&phba->hbalock, iflag);
15975 return rpi;
15976 }
15977
15978 /*
15979 * If the driver is running low on rpi resources, allocate another
15980 * page now. Note that the next_rpi value is used because
15981 * it represents how many are actually in use whereas max_rpi notes
15982 * how many are supported max by the device.
15983 */
15984 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
15985 spin_unlock_irqrestore(&phba->hbalock, iflag);
15986 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15987 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15988 if (!rpi_hdr) {
15989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15990 "2002 Error Could not grow rpi "
15991 "count\n");
15992 } else {
15993 lrpi = rpi_hdr->start_rpi;
15994 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15995 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15996 }
15997 }
15998
15999 return rpi;
16000 }
16001
16002 /**
16003 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16004 * @phba: pointer to lpfc hba data structure.
16005 *
16006 * This routine is invoked to release an rpi to the pool of
16007 * available rpis maintained by the driver.
16008 **/
16009 static void
16010 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16011 {
16012 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16013 phba->sli4_hba.rpi_count--;
16014 phba->sli4_hba.max_cfg_param.rpi_used--;
16015 }
16016 }
16017
16018 /**
16019 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16020 * @phba: pointer to lpfc hba data structure.
16021 *
16022 * This routine is invoked to release an rpi to the pool of
16023 * available rpis maintained by the driver.
16024 **/
16025 void
16026 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16027 {
16028 spin_lock_irq(&phba->hbalock);
16029 __lpfc_sli4_free_rpi(phba, rpi);
16030 spin_unlock_irq(&phba->hbalock);
16031 }
16032
16033 /**
16034 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16035 * @phba: pointer to lpfc hba data structure.
16036 *
16037 * This routine is invoked to remove the memory region that
16038 * provided rpi via a bitmask.
16039 **/
16040 void
16041 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16042 {
16043 kfree(phba->sli4_hba.rpi_bmask);
16044 kfree(phba->sli4_hba.rpi_ids);
16045 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
16046 }
16047
16048 /**
16049 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16050 * @phba: pointer to lpfc hba data structure.
16051 *
16052 * This routine is invoked to remove the memory region that
16053 * provided rpi via a bitmask.
16054 **/
16055 int
16056 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16057 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
16058 {
16059 LPFC_MBOXQ_t *mboxq;
16060 struct lpfc_hba *phba = ndlp->phba;
16061 int rc;
16062
16063 /* The port is notified of the header region via a mailbox command. */
16064 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16065 if (!mboxq)
16066 return -ENOMEM;
16067
16068 /* Post all rpi memory regions to the port. */
16069 lpfc_resume_rpi(mboxq, ndlp);
16070 if (cmpl) {
16071 mboxq->mbox_cmpl = cmpl;
16072 mboxq->context1 = arg;
16073 mboxq->context2 = ndlp;
16074 } else
16075 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16076 mboxq->vport = ndlp->vport;
16077 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16078 if (rc == MBX_NOT_FINISHED) {
16079 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16080 "2010 Resume RPI Mailbox failed "
16081 "status %d, mbxStatus x%x\n", rc,
16082 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16083 mempool_free(mboxq, phba->mbox_mem_pool);
16084 return -EIO;
16085 }
16086 return 0;
16087 }
16088
16089 /**
16090 * lpfc_sli4_init_vpi - Initialize a vpi with the port
16091 * @vport: Pointer to the vport for which the vpi is being initialized
16092 *
16093 * This routine is invoked to activate a vpi with the port.
16094 *
16095 * Returns:
16096 * 0 success
16097 * -Evalue otherwise
16098 **/
16099 int
16100 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
16101 {
16102 LPFC_MBOXQ_t *mboxq;
16103 int rc = 0;
16104 int retval = MBX_SUCCESS;
16105 uint32_t mbox_tmo;
16106 struct lpfc_hba *phba = vport->phba;
16107 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16108 if (!mboxq)
16109 return -ENOMEM;
16110 lpfc_init_vpi(phba, mboxq, vport->vpi);
16111 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
16112 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
16113 if (rc != MBX_SUCCESS) {
16114 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
16115 "2022 INIT VPI Mailbox failed "
16116 "status %d, mbxStatus x%x\n", rc,
16117 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16118 retval = -EIO;
16119 }
16120 if (rc != MBX_TIMEOUT)
16121 mempool_free(mboxq, vport->phba->mbox_mem_pool);
16122
16123 return retval;
16124 }
16125
16126 /**
16127 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16128 * @phba: pointer to lpfc hba data structure.
16129 * @mboxq: Pointer to mailbox object.
16130 *
16131 * This routine is invoked to manually add a single FCF record. The caller
16132 * must pass a completely initialized FCF_Record. This routine takes
16133 * care of the nonembedded mailbox operations.
16134 **/
16135 static void
16136 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16137 {
16138 void *virt_addr;
16139 union lpfc_sli4_cfg_shdr *shdr;
16140 uint32_t shdr_status, shdr_add_status;
16141
16142 virt_addr = mboxq->sge_array->addr[0];
16143 /* The IOCTL status is embedded in the mailbox subheader. */
16144 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16145 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16146 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16147
16148 if ((shdr_status || shdr_add_status) &&
16149 (shdr_status != STATUS_FCF_IN_USE))
16150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16151 "2558 ADD_FCF_RECORD mailbox failed with "
16152 "status x%x add_status x%x\n",
16153 shdr_status, shdr_add_status);
16154
16155 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16156 }
16157
16158 /**
16159 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16160 * @phba: pointer to lpfc hba data structure.
16161 * @fcf_record: pointer to the initialized fcf record to add.
16162 *
16163 * This routine is invoked to manually add a single FCF record. The caller
16164 * must pass a completely initialized FCF_Record. This routine takes
16165 * care of the nonembedded mailbox operations.
16166 **/
16167 int
16168 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16169 {
16170 int rc = 0;
16171 LPFC_MBOXQ_t *mboxq;
16172 uint8_t *bytep;
16173 void *virt_addr;
16174 struct lpfc_mbx_sge sge;
16175 uint32_t alloc_len, req_len;
16176 uint32_t fcfindex;
16177
16178 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16179 if (!mboxq) {
16180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16181 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16182 return -ENOMEM;
16183 }
16184
16185 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16186 sizeof(uint32_t);
16187
16188 /* Allocate DMA memory and set up the non-embedded mailbox command */
16189 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16190 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16191 req_len, LPFC_SLI4_MBX_NEMBED);
16192 if (alloc_len < req_len) {
16193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16194 "2523 Allocated DMA memory size (x%x) is "
16195 "less than the requested DMA memory "
16196 "size (x%x)\n", alloc_len, req_len);
16197 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16198 return -ENOMEM;
16199 }
16200
16201 /*
16202 * Get the first SGE entry from the non-embedded DMA memory. This
16203 * routine only uses a single SGE.
16204 */
16205 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
16206 virt_addr = mboxq->sge_array->addr[0];
16207 /*
16208 * Configure the FCF record for FCFI 0. This is the driver's
16209 * hardcoded default and gets used in nonFIP mode.
16210 */
16211 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16212 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16213 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16214
16215 /*
16216 * Copy the fcf_index and the FCF Record Data. The data starts after
16217 * the FCoE header plus word10. The data copy needs to be endian
16218 * correct.
16219 */
16220 bytep += sizeof(uint32_t);
16221 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16222 mboxq->vport = phba->pport;
16223 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16224 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16225 if (rc == MBX_NOT_FINISHED) {
16226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16227 "2515 ADD_FCF_RECORD mailbox failed with "
16228 "status 0x%x\n", rc);
16229 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16230 rc = -EIO;
16231 } else
16232 rc = 0;
16233
16234 return rc;
16235 }
16236
16237 /**
16238 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16239 * @phba: pointer to lpfc hba data structure.
16240 * @fcf_record: pointer to the fcf record to write the default data.
16241 * @fcf_index: FCF table entry index.
16242 *
16243 * This routine is invoked to build the driver's default FCF record. The
16244 * values used are hardcoded. This routine handles memory initialization.
16245 *
16246 **/
16247 void
16248 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16249 struct fcf_record *fcf_record,
16250 uint16_t fcf_index)
16251 {
16252 memset(fcf_record, 0, sizeof(struct fcf_record));
16253 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16254 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16255 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16256 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16257 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16258 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16259 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16260 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16261 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16262 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16263 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16264 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16265 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
16266 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
16267 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16268 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16269 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16270 /* Set the VLAN bit map */
16271 if (phba->valid_vlan) {
16272 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16273 = 1 << (phba->vlan_id % 8);
16274 }
16275 }
16276
16277 /**
16278 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
16279 * @phba: pointer to lpfc hba data structure.
16280 * @fcf_index: FCF table entry offset.
16281 *
16282 * This routine is invoked to scan the entire FCF table by reading FCF
16283 * record and processing it one at a time starting from the @fcf_index
16284 * for initial FCF discovery or fast FCF failover rediscovery.
16285 *
16286 * Return 0 if the mailbox command is submitted successfully, none 0
16287 * otherwise.
16288 **/
16289 int
16290 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16291 {
16292 int rc = 0, error;
16293 LPFC_MBOXQ_t *mboxq;
16294
16295 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
16296 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
16297 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16298 if (!mboxq) {
16299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16300 "2000 Failed to allocate mbox for "
16301 "READ_FCF cmd\n");
16302 error = -ENOMEM;
16303 goto fail_fcf_scan;
16304 }
16305 /* Construct the read FCF record mailbox command */
16306 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16307 if (rc) {
16308 error = -EINVAL;
16309 goto fail_fcf_scan;
16310 }
16311 /* Issue the mailbox command asynchronously */
16312 mboxq->vport = phba->pport;
16313 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
16314
16315 spin_lock_irq(&phba->hbalock);
16316 phba->hba_flag |= FCF_TS_INPROG;
16317 spin_unlock_irq(&phba->hbalock);
16318
16319 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16320 if (rc == MBX_NOT_FINISHED)
16321 error = -EIO;
16322 else {
16323 /* Reset eligible FCF count for new scan */
16324 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
16325 phba->fcf.eligible_fcf_cnt = 0;
16326 error = 0;
16327 }
16328 fail_fcf_scan:
16329 if (error) {
16330 if (mboxq)
16331 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16332 /* FCF scan failed, clear FCF_TS_INPROG flag */
16333 spin_lock_irq(&phba->hbalock);
16334 phba->hba_flag &= ~FCF_TS_INPROG;
16335 spin_unlock_irq(&phba->hbalock);
16336 }
16337 return error;
16338 }
16339
16340 /**
16341 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
16342 * @phba: pointer to lpfc hba data structure.
16343 * @fcf_index: FCF table entry offset.
16344 *
16345 * This routine is invoked to read an FCF record indicated by @fcf_index
16346 * and to use it for FLOGI roundrobin FCF failover.
16347 *
16348 * Return 0 if the mailbox command is submitted successfully, none 0
16349 * otherwise.
16350 **/
16351 int
16352 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16353 {
16354 int rc = 0, error;
16355 LPFC_MBOXQ_t *mboxq;
16356
16357 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16358 if (!mboxq) {
16359 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16360 "2763 Failed to allocate mbox for "
16361 "READ_FCF cmd\n");
16362 error = -ENOMEM;
16363 goto fail_fcf_read;
16364 }
16365 /* Construct the read FCF record mailbox command */
16366 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16367 if (rc) {
16368 error = -EINVAL;
16369 goto fail_fcf_read;
16370 }
16371 /* Issue the mailbox command asynchronously */
16372 mboxq->vport = phba->pport;
16373 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16375 if (rc == MBX_NOT_FINISHED)
16376 error = -EIO;
16377 else
16378 error = 0;
16379
16380 fail_fcf_read:
16381 if (error && mboxq)
16382 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16383 return error;
16384 }
16385
16386 /**
16387 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16388 * @phba: pointer to lpfc hba data structure.
16389 * @fcf_index: FCF table entry offset.
16390 *
16391 * This routine is invoked to read an FCF record indicated by @fcf_index to
16392 * determine whether it's eligible for FLOGI roundrobin failover list.
16393 *
16394 * Return 0 if the mailbox command is submitted successfully, none 0
16395 * otherwise.
16396 **/
16397 int
16398 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16399 {
16400 int rc = 0, error;
16401 LPFC_MBOXQ_t *mboxq;
16402
16403 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16404 if (!mboxq) {
16405 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16406 "2758 Failed to allocate mbox for "
16407 "READ_FCF cmd\n");
16408 error = -ENOMEM;
16409 goto fail_fcf_read;
16410 }
16411 /* Construct the read FCF record mailbox command */
16412 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16413 if (rc) {
16414 error = -EINVAL;
16415 goto fail_fcf_read;
16416 }
16417 /* Issue the mailbox command asynchronously */
16418 mboxq->vport = phba->pport;
16419 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16420 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16421 if (rc == MBX_NOT_FINISHED)
16422 error = -EIO;
16423 else
16424 error = 0;
16425
16426 fail_fcf_read:
16427 if (error && mboxq)
16428 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16429 return error;
16430 }
16431
16432 /**
16433 * lpfc_check_next_fcf_pri_level
16434 * phba pointer to the lpfc_hba struct for this port.
16435 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16436 * routine when the rr_bmask is empty. The FCF indecies are put into the
16437 * rr_bmask based on their priority level. Starting from the highest priority
16438 * to the lowest. The most likely FCF candidate will be in the highest
16439 * priority group. When this routine is called it searches the fcf_pri list for
16440 * next lowest priority group and repopulates the rr_bmask with only those
16441 * fcf_indexes.
16442 * returns:
16443 * 1=success 0=failure
16444 **/
16445 static int
16446 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16447 {
16448 uint16_t next_fcf_pri;
16449 uint16_t last_index;
16450 struct lpfc_fcf_pri *fcf_pri;
16451 int rc;
16452 int ret = 0;
16453
16454 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16455 LPFC_SLI4_FCF_TBL_INDX_MAX);
16456 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16457 "3060 Last IDX %d\n", last_index);
16458
16459 /* Verify the priority list has 2 or more entries */
16460 spin_lock_irq(&phba->hbalock);
16461 if (list_empty(&phba->fcf.fcf_pri_list) ||
16462 list_is_singular(&phba->fcf.fcf_pri_list)) {
16463 spin_unlock_irq(&phba->hbalock);
16464 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16465 "3061 Last IDX %d\n", last_index);
16466 return 0; /* Empty rr list */
16467 }
16468 spin_unlock_irq(&phba->hbalock);
16469
16470 next_fcf_pri = 0;
16471 /*
16472 * Clear the rr_bmask and set all of the bits that are at this
16473 * priority.
16474 */
16475 memset(phba->fcf.fcf_rr_bmask, 0,
16476 sizeof(*phba->fcf.fcf_rr_bmask));
16477 spin_lock_irq(&phba->hbalock);
16478 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16479 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16480 continue;
16481 /*
16482 * the 1st priority that has not FLOGI failed
16483 * will be the highest.
16484 */
16485 if (!next_fcf_pri)
16486 next_fcf_pri = fcf_pri->fcf_rec.priority;
16487 spin_unlock_irq(&phba->hbalock);
16488 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16489 rc = lpfc_sli4_fcf_rr_index_set(phba,
16490 fcf_pri->fcf_rec.fcf_index);
16491 if (rc)
16492 return 0;
16493 }
16494 spin_lock_irq(&phba->hbalock);
16495 }
16496 /*
16497 * if next_fcf_pri was not set above and the list is not empty then
16498 * we have failed flogis on all of them. So reset flogi failed
16499 * and start at the beginning.
16500 */
16501 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16502 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16503 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16504 /*
16505 * the 1st priority that has not FLOGI failed
16506 * will be the highest.
16507 */
16508 if (!next_fcf_pri)
16509 next_fcf_pri = fcf_pri->fcf_rec.priority;
16510 spin_unlock_irq(&phba->hbalock);
16511 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16512 rc = lpfc_sli4_fcf_rr_index_set(phba,
16513 fcf_pri->fcf_rec.fcf_index);
16514 if (rc)
16515 return 0;
16516 }
16517 spin_lock_irq(&phba->hbalock);
16518 }
16519 } else
16520 ret = 1;
16521 spin_unlock_irq(&phba->hbalock);
16522
16523 return ret;
16524 }
16525 /**
16526 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16527 * @phba: pointer to lpfc hba data structure.
16528 *
16529 * This routine is to get the next eligible FCF record index in a round
16530 * robin fashion. If the next eligible FCF record index equals to the
16531 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
16532 * shall be returned, otherwise, the next eligible FCF record's index
16533 * shall be returned.
16534 **/
16535 uint16_t
16536 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16537 {
16538 uint16_t next_fcf_index;
16539
16540 initial_priority:
16541 /* Search start from next bit of currently registered FCF index */
16542 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16543
16544 next_priority:
16545 /* Determine the next fcf index to check */
16546 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
16547 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16548 LPFC_SLI4_FCF_TBL_INDX_MAX,
16549 next_fcf_index);
16550
16551 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
16552 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16553 /*
16554 * If we have wrapped then we need to clear the bits that
16555 * have been tested so that we can detect when we should
16556 * change the priority level.
16557 */
16558 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16559 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
16560 }
16561
16562
16563 /* Check roundrobin failover list empty condition */
16564 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16565 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16566 /*
16567 * If next fcf index is not found check if there are lower
16568 * Priority level fcf's in the fcf_priority list.
16569 * Set up the rr_bmask with all of the avaiable fcf bits
16570 * at that level and continue the selection process.
16571 */
16572 if (lpfc_check_next_fcf_pri_level(phba))
16573 goto initial_priority;
16574 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16575 "2844 No roundrobin failover FCF available\n");
16576 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16577 return LPFC_FCOE_FCF_NEXT_NONE;
16578 else {
16579 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16580 "3063 Only FCF available idx %d, flag %x\n",
16581 next_fcf_index,
16582 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16583 return next_fcf_index;
16584 }
16585 }
16586
16587 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16588 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
16589 LPFC_FCF_FLOGI_FAILED) {
16590 if (list_is_singular(&phba->fcf.fcf_pri_list))
16591 return LPFC_FCOE_FCF_NEXT_NONE;
16592
16593 goto next_priority;
16594 }
16595
16596 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16597 "2845 Get next roundrobin failover FCF (x%x)\n",
16598 next_fcf_index);
16599
16600 return next_fcf_index;
16601 }
16602
16603 /**
16604 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16605 * @phba: pointer to lpfc hba data structure.
16606 *
16607 * This routine sets the FCF record index in to the eligible bmask for
16608 * roundrobin failover search. It checks to make sure that the index
16609 * does not go beyond the range of the driver allocated bmask dimension
16610 * before setting the bit.
16611 *
16612 * Returns 0 if the index bit successfully set, otherwise, it returns
16613 * -EINVAL.
16614 **/
16615 int
16616 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16617 {
16618 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16619 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16620 "2610 FCF (x%x) reached driver's book "
16621 "keeping dimension:x%x\n",
16622 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16623 return -EINVAL;
16624 }
16625 /* Set the eligible FCF record index bmask */
16626 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16627
16628 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16629 "2790 Set FCF (x%x) to roundrobin FCF failover "
16630 "bmask\n", fcf_index);
16631
16632 return 0;
16633 }
16634
16635 /**
16636 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
16637 * @phba: pointer to lpfc hba data structure.
16638 *
16639 * This routine clears the FCF record index from the eligible bmask for
16640 * roundrobin failover search. It checks to make sure that the index
16641 * does not go beyond the range of the driver allocated bmask dimension
16642 * before clearing the bit.
16643 **/
16644 void
16645 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16646 {
16647 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
16648 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16649 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16650 "2762 FCF (x%x) reached driver's book "
16651 "keeping dimension:x%x\n",
16652 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16653 return;
16654 }
16655 /* Clear the eligible FCF record index bmask */
16656 spin_lock_irq(&phba->hbalock);
16657 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16658 list) {
16659 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16660 list_del_init(&fcf_pri->list);
16661 break;
16662 }
16663 }
16664 spin_unlock_irq(&phba->hbalock);
16665 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16666
16667 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16668 "2791 Clear FCF (x%x) from roundrobin failover "
16669 "bmask\n", fcf_index);
16670 }
16671
16672 /**
16673 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16674 * @phba: pointer to lpfc hba data structure.
16675 *
16676 * This routine is the completion routine for the rediscover FCF table mailbox
16677 * command. If the mailbox command returned failure, it will try to stop the
16678 * FCF rediscover wait timer.
16679 **/
16680 static void
16681 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16682 {
16683 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16684 uint32_t shdr_status, shdr_add_status;
16685
16686 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16687
16688 shdr_status = bf_get(lpfc_mbox_hdr_status,
16689 &redisc_fcf->header.cfg_shdr.response);
16690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16691 &redisc_fcf->header.cfg_shdr.response);
16692 if (shdr_status || shdr_add_status) {
16693 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16694 "2746 Requesting for FCF rediscovery failed "
16695 "status x%x add_status x%x\n",
16696 shdr_status, shdr_add_status);
16697 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
16698 spin_lock_irq(&phba->hbalock);
16699 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
16700 spin_unlock_irq(&phba->hbalock);
16701 /*
16702 * CVL event triggered FCF rediscover request failed,
16703 * last resort to re-try current registered FCF entry.
16704 */
16705 lpfc_retry_pport_discovery(phba);
16706 } else {
16707 spin_lock_irq(&phba->hbalock);
16708 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
16709 spin_unlock_irq(&phba->hbalock);
16710 /*
16711 * DEAD FCF event triggered FCF rediscover request
16712 * failed, last resort to fail over as a link down
16713 * to FCF registration.
16714 */
16715 lpfc_sli4_fcf_dead_failthrough(phba);
16716 }
16717 } else {
16718 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16719 "2775 Start FCF rediscover quiescent timer\n");
16720 /*
16721 * Start FCF rediscovery wait timer for pending FCF
16722 * before rescan FCF record table.
16723 */
16724 lpfc_fcf_redisc_wait_start_timer(phba);
16725 }
16726
16727 mempool_free(mbox, phba->mbox_mem_pool);
16728 }
16729
16730 /**
16731 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
16732 * @phba: pointer to lpfc hba data structure.
16733 *
16734 * This routine is invoked to request for rediscovery of the entire FCF table
16735 * by the port.
16736 **/
16737 int
16738 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16739 {
16740 LPFC_MBOXQ_t *mbox;
16741 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16742 int rc, length;
16743
16744 /* Cancel retry delay timers to all vports before FCF rediscover */
16745 lpfc_cancel_all_vport_retry_delay_timer(phba);
16746
16747 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16748 if (!mbox) {
16749 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16750 "2745 Failed to allocate mbox for "
16751 "requesting FCF rediscover.\n");
16752 return -ENOMEM;
16753 }
16754
16755 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16756 sizeof(struct lpfc_sli4_cfg_mhdr));
16757 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16758 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16759 length, LPFC_SLI4_MBX_EMBED);
16760
16761 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16762 /* Set count to 0 for invalidating the entire FCF database */
16763 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16764
16765 /* Issue the mailbox command asynchronously */
16766 mbox->vport = phba->pport;
16767 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16768 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16769
16770 if (rc == MBX_NOT_FINISHED) {
16771 mempool_free(mbox, phba->mbox_mem_pool);
16772 return -EIO;
16773 }
16774 return 0;
16775 }
16776
16777 /**
16778 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16779 * @phba: pointer to lpfc hba data structure.
16780 *
16781 * This function is the failover routine as a last resort to the FCF DEAD
16782 * event when driver failed to perform fast FCF failover.
16783 **/
16784 void
16785 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16786 {
16787 uint32_t link_state;
16788
16789 /*
16790 * Last resort as FCF DEAD event failover will treat this as
16791 * a link down, but save the link state because we don't want
16792 * it to be changed to Link Down unless it is already down.
16793 */
16794 link_state = phba->link_state;
16795 lpfc_linkdown(phba);
16796 phba->link_state = link_state;
16797
16798 /* Unregister FCF if no devices connected to it */
16799 lpfc_unregister_unused_fcf(phba);
16800 }
16801
16802 /**
16803 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
16804 * @phba: pointer to lpfc hba data structure.
16805 * @rgn23_data: pointer to configure region 23 data.
16806 *
16807 * This function gets SLI3 port configure region 23 data through memory dump
16808 * mailbox command. When it successfully retrieves data, the size of the data
16809 * will be returned, otherwise, 0 will be returned.
16810 **/
16811 static uint32_t
16812 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16813 {
16814 LPFC_MBOXQ_t *pmb = NULL;
16815 MAILBOX_t *mb;
16816 uint32_t offset = 0;
16817 int rc;
16818
16819 if (!rgn23_data)
16820 return 0;
16821
16822 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16823 if (!pmb) {
16824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16825 "2600 failed to allocate mailbox memory\n");
16826 return 0;
16827 }
16828 mb = &pmb->u.mb;
16829
16830 do {
16831 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16832 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16833
16834 if (rc != MBX_SUCCESS) {
16835 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16836 "2601 failed to read config "
16837 "region 23, rc 0x%x Status 0x%x\n",
16838 rc, mb->mbxStatus);
16839 mb->un.varDmp.word_cnt = 0;
16840 }
16841 /*
16842 * dump mem may return a zero when finished or we got a
16843 * mailbox error, either way we are done.
16844 */
16845 if (mb->un.varDmp.word_cnt == 0)
16846 break;
16847 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16848 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16849
16850 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
16851 rgn23_data + offset,
16852 mb->un.varDmp.word_cnt);
16853 offset += mb->un.varDmp.word_cnt;
16854 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16855
16856 mempool_free(pmb, phba->mbox_mem_pool);
16857 return offset;
16858 }
16859
16860 /**
16861 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16862 * @phba: pointer to lpfc hba data structure.
16863 * @rgn23_data: pointer to configure region 23 data.
16864 *
16865 * This function gets SLI4 port configure region 23 data through memory dump
16866 * mailbox command. When it successfully retrieves data, the size of the data
16867 * will be returned, otherwise, 0 will be returned.
16868 **/
16869 static uint32_t
16870 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16871 {
16872 LPFC_MBOXQ_t *mboxq = NULL;
16873 struct lpfc_dmabuf *mp = NULL;
16874 struct lpfc_mqe *mqe;
16875 uint32_t data_length = 0;
16876 int rc;
16877
16878 if (!rgn23_data)
16879 return 0;
16880
16881 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16882 if (!mboxq) {
16883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16884 "3105 failed to allocate mailbox memory\n");
16885 return 0;
16886 }
16887
16888 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16889 goto out;
16890 mqe = &mboxq->u.mqe;
16891 mp = (struct lpfc_dmabuf *) mboxq->context1;
16892 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16893 if (rc)
16894 goto out;
16895 data_length = mqe->un.mb_words[5];
16896 if (data_length == 0)
16897 goto out;
16898 if (data_length > DMP_RGN23_SIZE) {
16899 data_length = 0;
16900 goto out;
16901 }
16902 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16903 out:
16904 mempool_free(mboxq, phba->mbox_mem_pool);
16905 if (mp) {
16906 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16907 kfree(mp);
16908 }
16909 return data_length;
16910 }
16911
16912 /**
16913 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16914 * @phba: pointer to lpfc hba data structure.
16915 *
16916 * This function read region 23 and parse TLV for port status to
16917 * decide if the user disaled the port. If the TLV indicates the
16918 * port is disabled, the hba_flag is set accordingly.
16919 **/
16920 void
16921 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16922 {
16923 uint8_t *rgn23_data = NULL;
16924 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16925 uint32_t offset = 0;
16926
16927 /* Get adapter Region 23 data */
16928 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16929 if (!rgn23_data)
16930 goto out;
16931
16932 if (phba->sli_rev < LPFC_SLI_REV4)
16933 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16934 else {
16935 if_type = bf_get(lpfc_sli_intf_if_type,
16936 &phba->sli4_hba.sli_intf);
16937 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16938 goto out;
16939 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16940 }
16941
16942 if (!data_size)
16943 goto out;
16944
16945 /* Check the region signature first */
16946 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16948 "2619 Config region 23 has bad signature\n");
16949 goto out;
16950 }
16951 offset += 4;
16952
16953 /* Check the data structure version */
16954 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16956 "2620 Config region 23 has bad version\n");
16957 goto out;
16958 }
16959 offset += 4;
16960
16961 /* Parse TLV entries in the region */
16962 while (offset < data_size) {
16963 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16964 break;
16965 /*
16966 * If the TLV is not driver specific TLV or driver id is
16967 * not linux driver id, skip the record.
16968 */
16969 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16970 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16971 (rgn23_data[offset + 3] != 0)) {
16972 offset += rgn23_data[offset + 1] * 4 + 4;
16973 continue;
16974 }
16975
16976 /* Driver found a driver specific TLV in the config region */
16977 sub_tlv_len = rgn23_data[offset + 1] * 4;
16978 offset += 4;
16979 tlv_offset = 0;
16980
16981 /*
16982 * Search for configured port state sub-TLV.
16983 */
16984 while ((offset < data_size) &&
16985 (tlv_offset < sub_tlv_len)) {
16986 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16987 offset += 4;
16988 tlv_offset += 4;
16989 break;
16990 }
16991 if (rgn23_data[offset] != PORT_STE_TYPE) {
16992 offset += rgn23_data[offset + 1] * 4 + 4;
16993 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16994 continue;
16995 }
16996
16997 /* This HBA contains PORT_STE configured */
16998 if (!rgn23_data[offset + 2])
16999 phba->hba_flag |= LINK_DISABLED;
17000
17001 goto out;
17002 }
17003 }
17004
17005 out:
17006 kfree(rgn23_data);
17007 return;
17008 }
17009
17010 /**
17011 * lpfc_wr_object - write an object to the firmware
17012 * @phba: HBA structure that indicates port to create a queue on.
17013 * @dmabuf_list: list of dmabufs to write to the port.
17014 * @size: the total byte value of the objects to write to the port.
17015 * @offset: the current offset to be used to start the transfer.
17016 *
17017 * This routine will create a wr_object mailbox command to send to the port.
17018 * the mailbox command will be constructed using the dma buffers described in
17019 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17020 * BDEs that the imbedded mailbox can support. The @offset variable will be
17021 * used to indicate the starting offset of the transfer and will also return
17022 * the offset after the write object mailbox has completed. @size is used to
17023 * determine the end of the object and whether the eof bit should be set.
17024 *
17025 * Return 0 is successful and offset will contain the the new offset to use
17026 * for the next write.
17027 * Return negative value for error cases.
17028 **/
17029 int
17030 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17031 uint32_t size, uint32_t *offset)
17032 {
17033 struct lpfc_mbx_wr_object *wr_object;
17034 LPFC_MBOXQ_t *mbox;
17035 int rc = 0, i = 0;
17036 uint32_t shdr_status, shdr_add_status;
17037 uint32_t mbox_tmo;
17038 union lpfc_sli4_cfg_shdr *shdr;
17039 struct lpfc_dmabuf *dmabuf;
17040 uint32_t written = 0;
17041
17042 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17043 if (!mbox)
17044 return -ENOMEM;
17045
17046 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17047 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17048 sizeof(struct lpfc_mbx_wr_object) -
17049 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17050
17051 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17052 wr_object->u.request.write_offset = *offset;
17053 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17054 wr_object->u.request.object_name[0] =
17055 cpu_to_le32(wr_object->u.request.object_name[0]);
17056 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17057 list_for_each_entry(dmabuf, dmabuf_list, list) {
17058 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17059 break;
17060 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17061 wr_object->u.request.bde[i].addrHigh =
17062 putPaddrHigh(dmabuf->phys);
17063 if (written + SLI4_PAGE_SIZE >= size) {
17064 wr_object->u.request.bde[i].tus.f.bdeSize =
17065 (size - written);
17066 written += (size - written);
17067 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17068 } else {
17069 wr_object->u.request.bde[i].tus.f.bdeSize =
17070 SLI4_PAGE_SIZE;
17071 written += SLI4_PAGE_SIZE;
17072 }
17073 i++;
17074 }
17075 wr_object->u.request.bde_count = i;
17076 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17077 if (!phba->sli4_hba.intr_enable)
17078 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17079 else {
17080 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17081 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17082 }
17083 /* The IOCTL status is embedded in the mailbox subheader. */
17084 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17085 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17086 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17087 if (rc != MBX_TIMEOUT)
17088 mempool_free(mbox, phba->mbox_mem_pool);
17089 if (shdr_status || shdr_add_status || rc) {
17090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17091 "3025 Write Object mailbox failed with "
17092 "status x%x add_status x%x, mbx status x%x\n",
17093 shdr_status, shdr_add_status, rc);
17094 rc = -ENXIO;
17095 } else
17096 *offset += wr_object->u.response.actual_write_length;
17097 return rc;
17098 }
17099
17100 /**
17101 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17102 * @vport: pointer to vport data structure.
17103 *
17104 * This function iterate through the mailboxq and clean up all REG_LOGIN
17105 * and REG_VPI mailbox commands associated with the vport. This function
17106 * is called when driver want to restart discovery of the vport due to
17107 * a Clear Virtual Link event.
17108 **/
17109 void
17110 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17111 {
17112 struct lpfc_hba *phba = vport->phba;
17113 LPFC_MBOXQ_t *mb, *nextmb;
17114 struct lpfc_dmabuf *mp;
17115 struct lpfc_nodelist *ndlp;
17116 struct lpfc_nodelist *act_mbx_ndlp = NULL;
17117 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
17118 LIST_HEAD(mbox_cmd_list);
17119 uint8_t restart_loop;
17120
17121 /* Clean up internally queued mailbox commands with the vport */
17122 spin_lock_irq(&phba->hbalock);
17123 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17124 if (mb->vport != vport)
17125 continue;
17126
17127 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17128 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17129 continue;
17130
17131 list_del(&mb->list);
17132 list_add_tail(&mb->list, &mbox_cmd_list);
17133 }
17134 /* Clean up active mailbox command with the vport */
17135 mb = phba->sli.mbox_active;
17136 if (mb && (mb->vport == vport)) {
17137 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17138 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17139 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17140 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17141 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17142 /* Put reference count for delayed processing */
17143 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17144 /* Unregister the RPI when mailbox complete */
17145 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17146 }
17147 }
17148 /* Cleanup any mailbox completions which are not yet processed */
17149 do {
17150 restart_loop = 0;
17151 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17152 /*
17153 * If this mailox is already processed or it is
17154 * for another vport ignore it.
17155 */
17156 if ((mb->vport != vport) ||
17157 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17158 continue;
17159
17160 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17161 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17162 continue;
17163
17164 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17165 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17166 ndlp = (struct lpfc_nodelist *)mb->context2;
17167 /* Unregister the RPI when mailbox complete */
17168 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17169 restart_loop = 1;
17170 spin_unlock_irq(&phba->hbalock);
17171 spin_lock(shost->host_lock);
17172 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17173 spin_unlock(shost->host_lock);
17174 spin_lock_irq(&phba->hbalock);
17175 break;
17176 }
17177 }
17178 } while (restart_loop);
17179
17180 spin_unlock_irq(&phba->hbalock);
17181
17182 /* Release the cleaned-up mailbox commands */
17183 while (!list_empty(&mbox_cmd_list)) {
17184 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
17185 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17186 mp = (struct lpfc_dmabuf *) (mb->context1);
17187 if (mp) {
17188 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17189 kfree(mp);
17190 }
17191 ndlp = (struct lpfc_nodelist *) mb->context2;
17192 mb->context2 = NULL;
17193 if (ndlp) {
17194 spin_lock(shost->host_lock);
17195 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17196 spin_unlock(shost->host_lock);
17197 lpfc_nlp_put(ndlp);
17198 }
17199 }
17200 mempool_free(mb, phba->mbox_mem_pool);
17201 }
17202
17203 /* Release the ndlp with the cleaned-up active mailbox command */
17204 if (act_mbx_ndlp) {
17205 spin_lock(shost->host_lock);
17206 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17207 spin_unlock(shost->host_lock);
17208 lpfc_nlp_put(act_mbx_ndlp);
17209 }
17210 }
17211
17212 /**
17213 * lpfc_drain_txq - Drain the txq
17214 * @phba: Pointer to HBA context object.
17215 *
17216 * This function attempt to submit IOCBs on the txq
17217 * to the adapter. For SLI4 adapters, the txq contains
17218 * ELS IOCBs that have been deferred because the there
17219 * are no SGLs. This congestion can occur with large
17220 * vport counts during node discovery.
17221 **/
17222
17223 uint32_t
17224 lpfc_drain_txq(struct lpfc_hba *phba)
17225 {
17226 LIST_HEAD(completions);
17227 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
17228 struct lpfc_iocbq *piocbq = NULL;
17229 unsigned long iflags = 0;
17230 char *fail_msg = NULL;
17231 struct lpfc_sglq *sglq;
17232 union lpfc_wqe wqe;
17233 uint32_t txq_cnt = 0;
17234
17235 spin_lock_irqsave(&pring->ring_lock, iflags);
17236 list_for_each_entry(piocbq, &pring->txq, list) {
17237 txq_cnt++;
17238 }
17239
17240 if (txq_cnt > pring->txq_max)
17241 pring->txq_max = txq_cnt;
17242
17243 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17244
17245 while (!list_empty(&pring->txq)) {
17246 spin_lock_irqsave(&pring->ring_lock, iflags);
17247
17248 piocbq = lpfc_sli_ringtx_get(phba, pring);
17249 if (!piocbq) {
17250 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17252 "2823 txq empty and txq_cnt is %d\n ",
17253 txq_cnt);
17254 break;
17255 }
17256 sglq = __lpfc_sli_get_sglq(phba, piocbq);
17257 if (!sglq) {
17258 __lpfc_sli_ringtx_put(phba, pring, piocbq);
17259 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17260 break;
17261 }
17262 txq_cnt--;
17263
17264 /* The xri and iocb resources secured,
17265 * attempt to issue request
17266 */
17267 piocbq->sli4_lxritag = sglq->sli4_lxritag;
17268 piocbq->sli4_xritag = sglq->sli4_xritag;
17269 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17270 fail_msg = "to convert bpl to sgl";
17271 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17272 fail_msg = "to convert iocb to wqe";
17273 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17274 fail_msg = " - Wq is full";
17275 else
17276 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17277
17278 if (fail_msg) {
17279 /* Failed means we can't issue and need to cancel */
17280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17281 "2822 IOCB failed %s iotag 0x%x "
17282 "xri 0x%x\n",
17283 fail_msg,
17284 piocbq->iotag, piocbq->sli4_xritag);
17285 list_add_tail(&piocbq->list, &completions);
17286 }
17287 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17288 }
17289
17290 /* Cancel all the IOCBs that cannot be issued */
17291 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17292 IOERR_SLI_ABORTED);
17293
17294 return txq_cnt;
17295 }