]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.3.13: Initialization code clean up and fixes.
[mirror_ubuntu-bionic-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-2009 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71 return &iocbq->iocb;
72 }
73
74 /**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115 }
116
117 /**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128 static uint32_t
129 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130 {
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140 }
141
142 /**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154 static uint32_t
155 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156 {
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179 }
180
181 /**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191 static uint32_t
192 lpfc_sli4_mq_release(struct lpfc_queue *q)
193 {
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198 }
199
200 /**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209 static struct lpfc_eqe *
210 lpfc_sli4_eq_get(struct lpfc_queue *q)
211 {
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223 }
224
225 /**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240 uint32_t
241 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242 {
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
270 return released;
271 }
272
273 /**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282 static struct lpfc_cqe *
283 lpfc_sli4_cq_get(struct lpfc_queue *q)
284 {
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297 }
298
299 /**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314 uint32_t
315 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316 {
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340 }
341
342 /**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354 static int
355 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357 {
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386 }
387
388 /**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398 static uint32_t
399 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400 {
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406 }
407
408 /**
409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
418 static inline IOCB_t *
419 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420 {
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423 }
424
425 /**
426 * lpfc_resp_iocb - Get next response iocb entry in the ring
427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
435 static inline IOCB_t *
436 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437 {
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440 }
441
442 /**
443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
451 static struct lpfc_iocbq *
452 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
453 {
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458 return iocbq;
459 }
460
461 /**
462 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
463 * @phba: Pointer to HBA context object.
464 * @xritag: XRI value.
465 *
466 * This function clears the sglq pointer from the array of acive
467 * sglq's. The xritag that is passed in is used to index into the
468 * array. Before the xritag can be used it needs to be adjusted
469 * by subtracting the xribase.
470 *
471 * Returns sglq ponter = success, NULL = Failure.
472 **/
473 static struct lpfc_sglq *
474 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
475 {
476 uint16_t adj_xri;
477 struct lpfc_sglq *sglq;
478 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
479 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
480 return NULL;
481 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
482 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
483 return sglq;
484 }
485
486 /**
487 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
488 * @phba: Pointer to HBA context object.
489 * @xritag: XRI value.
490 *
491 * This function returns the sglq pointer from the array of acive
492 * sglq's. The xritag that is passed in is used to index into the
493 * array. Before the xritag can be used it needs to be adjusted
494 * by subtracting the xribase.
495 *
496 * Returns sglq ponter = success, NULL = Failure.
497 **/
498 struct lpfc_sglq *
499 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
500 {
501 uint16_t adj_xri;
502 struct lpfc_sglq *sglq;
503 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
504 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
505 return NULL;
506 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
507 return sglq;
508 }
509
510 /**
511 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
512 * @phba: Pointer to HBA context object.
513 *
514 * This function is called with hbalock held. This function
515 * Gets a new driver sglq object from the sglq list. If the
516 * list is not empty then it is successful, it returns pointer to the newly
517 * allocated sglq object else it returns NULL.
518 **/
519 static struct lpfc_sglq *
520 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
521 {
522 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
523 struct lpfc_sglq *sglq = NULL;
524 uint16_t adj_xri;
525 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
526 if (!sglq)
527 return NULL;
528 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
529 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
530 sglq->state = SGL_ALLOCATED;
531 return sglq;
532 }
533
534 /**
535 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
536 * @phba: Pointer to HBA context object.
537 *
538 * This function is called with no lock held. This function
539 * allocates a new driver iocb object from the iocb pool. If the
540 * allocation is successful, it returns pointer to the newly
541 * allocated iocb object else it returns NULL.
542 **/
543 struct lpfc_iocbq *
544 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
545 {
546 struct lpfc_iocbq * iocbq = NULL;
547 unsigned long iflags;
548
549 spin_lock_irqsave(&phba->hbalock, iflags);
550 iocbq = __lpfc_sli_get_iocbq(phba);
551 spin_unlock_irqrestore(&phba->hbalock, iflags);
552 return iocbq;
553 }
554
555 /**
556 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
557 * @phba: Pointer to HBA context object.
558 * @iocbq: Pointer to driver iocb object.
559 *
560 * This function is called with hbalock held to release driver
561 * iocb object to the iocb pool. The iotag in the iocb object
562 * does not change for each use of the iocb object. This function
563 * clears all other fields of the iocb object when it is freed.
564 * The sqlq structure that holds the xritag and phys and virtual
565 * mappings for the scatter gather list is retrieved from the
566 * active array of sglq. The get of the sglq pointer also clears
567 * the entry in the array. If the status of the IO indiactes that
568 * this IO was aborted then the sglq entry it put on the
569 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
570 * IO has good status or fails for any other reason then the sglq
571 * entry is added to the free list (lpfc_sgl_list).
572 **/
573 static void
574 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
575 {
576 struct lpfc_sglq *sglq;
577 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
578 unsigned long iflag;
579
580 if (iocbq->sli4_xritag == NO_XRI)
581 sglq = NULL;
582 else
583 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
584 if (sglq) {
585 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
586 (sglq->state != SGL_XRI_ABORTED)) {
587 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588 iflag);
589 list_add(&sglq->list,
590 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591 spin_unlock_irqrestore(
592 &phba->sli4_hba.abts_sgl_list_lock, iflag);
593 } else {
594 sglq->state = SGL_FREED;
595 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
596 }
597 }
598
599
600 /*
601 * Clean all volatile data fields, preserve iotag and node struct.
602 */
603 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
604 iocbq->sli4_xritag = NO_XRI;
605 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
606 }
607
608 /**
609 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
610 * @phba: Pointer to HBA context object.
611 * @iocbq: Pointer to driver iocb object.
612 *
613 * This function is called with hbalock held to release driver
614 * iocb object to the iocb pool. The iotag in the iocb object
615 * does not change for each use of the iocb object. This function
616 * clears all other fields of the iocb object when it is freed.
617 **/
618 static void
619 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
620 {
621 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
622
623 /*
624 * Clean all volatile data fields, preserve iotag and node struct.
625 */
626 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
627 iocbq->sli4_xritag = NO_XRI;
628 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
629 }
630
631 /**
632 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
633 * @phba: Pointer to HBA context object.
634 * @iocbq: Pointer to driver iocb object.
635 *
636 * This function is called with hbalock held to release driver
637 * iocb object to the iocb pool. The iotag in the iocb object
638 * does not change for each use of the iocb object. This function
639 * clears all other fields of the iocb object when it is freed.
640 **/
641 static void
642 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
643 {
644 phba->__lpfc_sli_release_iocbq(phba, iocbq);
645 }
646
647 /**
648 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
649 * @phba: Pointer to HBA context object.
650 * @iocbq: Pointer to driver iocb object.
651 *
652 * This function is called with no lock held to release the iocb to
653 * iocb pool.
654 **/
655 void
656 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
657 {
658 unsigned long iflags;
659
660 /*
661 * Clean all volatile data fields, preserve iotag and node struct.
662 */
663 spin_lock_irqsave(&phba->hbalock, iflags);
664 __lpfc_sli_release_iocbq(phba, iocbq);
665 spin_unlock_irqrestore(&phba->hbalock, iflags);
666 }
667
668 /**
669 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
670 * @phba: Pointer to HBA context object.
671 * @iocblist: List of IOCBs.
672 * @ulpstatus: ULP status in IOCB command field.
673 * @ulpWord4: ULP word-4 in IOCB command field.
674 *
675 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
676 * on the list by invoking the complete callback function associated with the
677 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
678 * fields.
679 **/
680 void
681 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
682 uint32_t ulpstatus, uint32_t ulpWord4)
683 {
684 struct lpfc_iocbq *piocb;
685
686 while (!list_empty(iocblist)) {
687 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
688
689 if (!piocb->iocb_cmpl)
690 lpfc_sli_release_iocbq(phba, piocb);
691 else {
692 piocb->iocb.ulpStatus = ulpstatus;
693 piocb->iocb.un.ulpWord[4] = ulpWord4;
694 (piocb->iocb_cmpl) (phba, piocb, piocb);
695 }
696 }
697 return;
698 }
699
700 /**
701 * lpfc_sli_iocb_cmd_type - Get the iocb type
702 * @iocb_cmnd: iocb command code.
703 *
704 * This function is called by ring event handler function to get the iocb type.
705 * This function translates the iocb command to an iocb command type used to
706 * decide the final disposition of each completed IOCB.
707 * The function returns
708 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
709 * LPFC_SOL_IOCB if it is a solicited iocb completion
710 * LPFC_ABORT_IOCB if it is an abort iocb
711 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
712 *
713 * The caller is not required to hold any lock.
714 **/
715 static lpfc_iocb_type
716 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
717 {
718 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
719
720 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
721 return 0;
722
723 switch (iocb_cmnd) {
724 case CMD_XMIT_SEQUENCE_CR:
725 case CMD_XMIT_SEQUENCE_CX:
726 case CMD_XMIT_BCAST_CN:
727 case CMD_XMIT_BCAST_CX:
728 case CMD_ELS_REQUEST_CR:
729 case CMD_ELS_REQUEST_CX:
730 case CMD_CREATE_XRI_CR:
731 case CMD_CREATE_XRI_CX:
732 case CMD_GET_RPI_CN:
733 case CMD_XMIT_ELS_RSP_CX:
734 case CMD_GET_RPI_CR:
735 case CMD_FCP_IWRITE_CR:
736 case CMD_FCP_IWRITE_CX:
737 case CMD_FCP_IREAD_CR:
738 case CMD_FCP_IREAD_CX:
739 case CMD_FCP_ICMND_CR:
740 case CMD_FCP_ICMND_CX:
741 case CMD_FCP_TSEND_CX:
742 case CMD_FCP_TRSP_CX:
743 case CMD_FCP_TRECEIVE_CX:
744 case CMD_FCP_AUTO_TRSP_CX:
745 case CMD_ADAPTER_MSG:
746 case CMD_ADAPTER_DUMP:
747 case CMD_XMIT_SEQUENCE64_CR:
748 case CMD_XMIT_SEQUENCE64_CX:
749 case CMD_XMIT_BCAST64_CN:
750 case CMD_XMIT_BCAST64_CX:
751 case CMD_ELS_REQUEST64_CR:
752 case CMD_ELS_REQUEST64_CX:
753 case CMD_FCP_IWRITE64_CR:
754 case CMD_FCP_IWRITE64_CX:
755 case CMD_FCP_IREAD64_CR:
756 case CMD_FCP_IREAD64_CX:
757 case CMD_FCP_ICMND64_CR:
758 case CMD_FCP_ICMND64_CX:
759 case CMD_FCP_TSEND64_CX:
760 case CMD_FCP_TRSP64_CX:
761 case CMD_FCP_TRECEIVE64_CX:
762 case CMD_GEN_REQUEST64_CR:
763 case CMD_GEN_REQUEST64_CX:
764 case CMD_XMIT_ELS_RSP64_CX:
765 case DSSCMD_IWRITE64_CR:
766 case DSSCMD_IWRITE64_CX:
767 case DSSCMD_IREAD64_CR:
768 case DSSCMD_IREAD64_CX:
769 type = LPFC_SOL_IOCB;
770 break;
771 case CMD_ABORT_XRI_CN:
772 case CMD_ABORT_XRI_CX:
773 case CMD_CLOSE_XRI_CN:
774 case CMD_CLOSE_XRI_CX:
775 case CMD_XRI_ABORTED_CX:
776 case CMD_ABORT_MXRI64_CN:
777 case CMD_XMIT_BLS_RSP64_CX:
778 type = LPFC_ABORT_IOCB;
779 break;
780 case CMD_RCV_SEQUENCE_CX:
781 case CMD_RCV_ELS_REQ_CX:
782 case CMD_RCV_SEQUENCE64_CX:
783 case CMD_RCV_ELS_REQ64_CX:
784 case CMD_ASYNC_STATUS:
785 case CMD_IOCB_RCV_SEQ64_CX:
786 case CMD_IOCB_RCV_ELS64_CX:
787 case CMD_IOCB_RCV_CONT64_CX:
788 case CMD_IOCB_RET_XRI64_CX:
789 type = LPFC_UNSOL_IOCB;
790 break;
791 case CMD_IOCB_XMIT_MSEQ64_CR:
792 case CMD_IOCB_XMIT_MSEQ64_CX:
793 case CMD_IOCB_RCV_SEQ_LIST64_CX:
794 case CMD_IOCB_RCV_ELS_LIST64_CX:
795 case CMD_IOCB_CLOSE_EXTENDED_CN:
796 case CMD_IOCB_ABORT_EXTENDED_CN:
797 case CMD_IOCB_RET_HBQE64_CN:
798 case CMD_IOCB_FCP_IBIDIR64_CR:
799 case CMD_IOCB_FCP_IBIDIR64_CX:
800 case CMD_IOCB_FCP_ITASKMGT64_CX:
801 case CMD_IOCB_LOGENTRY_CN:
802 case CMD_IOCB_LOGENTRY_ASYNC_CN:
803 printk("%s - Unhandled SLI-3 Command x%x\n",
804 __func__, iocb_cmnd);
805 type = LPFC_UNKNOWN_IOCB;
806 break;
807 default:
808 type = LPFC_UNKNOWN_IOCB;
809 break;
810 }
811
812 return type;
813 }
814
815 /**
816 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
817 * @phba: Pointer to HBA context object.
818 *
819 * This function is called from SLI initialization code
820 * to configure every ring of the HBA's SLI interface. The
821 * caller is not required to hold any lock. This function issues
822 * a config_ring mailbox command for each ring.
823 * This function returns zero if successful else returns a negative
824 * error code.
825 **/
826 static int
827 lpfc_sli_ring_map(struct lpfc_hba *phba)
828 {
829 struct lpfc_sli *psli = &phba->sli;
830 LPFC_MBOXQ_t *pmb;
831 MAILBOX_t *pmbox;
832 int i, rc, ret = 0;
833
834 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835 if (!pmb)
836 return -ENOMEM;
837 pmbox = &pmb->u.mb;
838 phba->link_state = LPFC_INIT_MBX_CMDS;
839 for (i = 0; i < psli->num_rings; i++) {
840 lpfc_config_ring(phba, i, pmb);
841 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
842 if (rc != MBX_SUCCESS) {
843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
844 "0446 Adapter failed to init (%d), "
845 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
846 "ring %d\n",
847 rc, pmbox->mbxCommand,
848 pmbox->mbxStatus, i);
849 phba->link_state = LPFC_HBA_ERROR;
850 ret = -ENXIO;
851 break;
852 }
853 }
854 mempool_free(pmb, phba->mbox_mem_pool);
855 return ret;
856 }
857
858 /**
859 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
860 * @phba: Pointer to HBA context object.
861 * @pring: Pointer to driver SLI ring object.
862 * @piocb: Pointer to the driver iocb object.
863 *
864 * This function is called with hbalock held. The function adds the
865 * new iocb to txcmplq of the given ring. This function always returns
866 * 0. If this function is called for ELS ring, this function checks if
867 * there is a vport associated with the ELS command. This function also
868 * starts els_tmofunc timer if this is an ELS command.
869 **/
870 static int
871 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
872 struct lpfc_iocbq *piocb)
873 {
874 list_add_tail(&piocb->list, &pring->txcmplq);
875 pring->txcmplq_cnt++;
876 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
877 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
878 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
879 if (!piocb->vport)
880 BUG();
881 else
882 mod_timer(&piocb->vport->els_tmofunc,
883 jiffies + HZ * (phba->fc_ratov << 1));
884 }
885
886
887 return 0;
888 }
889
890 /**
891 * lpfc_sli_ringtx_get - Get first element of the txq
892 * @phba: Pointer to HBA context object.
893 * @pring: Pointer to driver SLI ring object.
894 *
895 * This function is called with hbalock held to get next
896 * iocb in txq of the given ring. If there is any iocb in
897 * the txq, the function returns first iocb in the list after
898 * removing the iocb from the list, else it returns NULL.
899 **/
900 static struct lpfc_iocbq *
901 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
902 {
903 struct lpfc_iocbq *cmd_iocb;
904
905 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
906 if (cmd_iocb != NULL)
907 pring->txq_cnt--;
908 return cmd_iocb;
909 }
910
911 /**
912 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
913 * @phba: Pointer to HBA context object.
914 * @pring: Pointer to driver SLI ring object.
915 *
916 * This function is called with hbalock held and the caller must post the
917 * iocb without releasing the lock. If the caller releases the lock,
918 * iocb slot returned by the function is not guaranteed to be available.
919 * The function returns pointer to the next available iocb slot if there
920 * is available slot in the ring, else it returns NULL.
921 * If the get index of the ring is ahead of the put index, the function
922 * will post an error attention event to the worker thread to take the
923 * HBA to offline state.
924 **/
925 static IOCB_t *
926 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
927 {
928 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
929 uint32_t max_cmd_idx = pring->numCiocb;
930 if ((pring->next_cmdidx == pring->cmdidx) &&
931 (++pring->next_cmdidx >= max_cmd_idx))
932 pring->next_cmdidx = 0;
933
934 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
935
936 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
937
938 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
940 "0315 Ring %d issue: portCmdGet %d "
941 "is bigger than cmd ring %d\n",
942 pring->ringno,
943 pring->local_getidx, max_cmd_idx);
944
945 phba->link_state = LPFC_HBA_ERROR;
946 /*
947 * All error attention handlers are posted to
948 * worker thread
949 */
950 phba->work_ha |= HA_ERATT;
951 phba->work_hs = HS_FFER3;
952
953 lpfc_worker_wake_up(phba);
954
955 return NULL;
956 }
957
958 if (pring->local_getidx == pring->next_cmdidx)
959 return NULL;
960 }
961
962 return lpfc_cmd_iocb(phba, pring);
963 }
964
965 /**
966 * lpfc_sli_next_iotag - Get an iotag for the iocb
967 * @phba: Pointer to HBA context object.
968 * @iocbq: Pointer to driver iocb object.
969 *
970 * This function gets an iotag for the iocb. If there is no unused iotag and
971 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
972 * array and assigns a new iotag.
973 * The function returns the allocated iotag if successful, else returns zero.
974 * Zero is not a valid iotag.
975 * The caller is not required to hold any lock.
976 **/
977 uint16_t
978 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
979 {
980 struct lpfc_iocbq **new_arr;
981 struct lpfc_iocbq **old_arr;
982 size_t new_len;
983 struct lpfc_sli *psli = &phba->sli;
984 uint16_t iotag;
985
986 spin_lock_irq(&phba->hbalock);
987 iotag = psli->last_iotag;
988 if(++iotag < psli->iocbq_lookup_len) {
989 psli->last_iotag = iotag;
990 psli->iocbq_lookup[iotag] = iocbq;
991 spin_unlock_irq(&phba->hbalock);
992 iocbq->iotag = iotag;
993 return iotag;
994 } else if (psli->iocbq_lookup_len < (0xffff
995 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
996 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
997 spin_unlock_irq(&phba->hbalock);
998 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
999 GFP_KERNEL);
1000 if (new_arr) {
1001 spin_lock_irq(&phba->hbalock);
1002 old_arr = psli->iocbq_lookup;
1003 if (new_len <= psli->iocbq_lookup_len) {
1004 /* highly unprobable case */
1005 kfree(new_arr);
1006 iotag = psli->last_iotag;
1007 if(++iotag < psli->iocbq_lookup_len) {
1008 psli->last_iotag = iotag;
1009 psli->iocbq_lookup[iotag] = iocbq;
1010 spin_unlock_irq(&phba->hbalock);
1011 iocbq->iotag = iotag;
1012 return iotag;
1013 }
1014 spin_unlock_irq(&phba->hbalock);
1015 return 0;
1016 }
1017 if (psli->iocbq_lookup)
1018 memcpy(new_arr, old_arr,
1019 ((psli->last_iotag + 1) *
1020 sizeof (struct lpfc_iocbq *)));
1021 psli->iocbq_lookup = new_arr;
1022 psli->iocbq_lookup_len = new_len;
1023 psli->last_iotag = iotag;
1024 psli->iocbq_lookup[iotag] = iocbq;
1025 spin_unlock_irq(&phba->hbalock);
1026 iocbq->iotag = iotag;
1027 kfree(old_arr);
1028 return iotag;
1029 }
1030 } else
1031 spin_unlock_irq(&phba->hbalock);
1032
1033 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1034 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1035 psli->last_iotag);
1036
1037 return 0;
1038 }
1039
1040 /**
1041 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1042 * @phba: Pointer to HBA context object.
1043 * @pring: Pointer to driver SLI ring object.
1044 * @iocb: Pointer to iocb slot in the ring.
1045 * @nextiocb: Pointer to driver iocb object which need to be
1046 * posted to firmware.
1047 *
1048 * This function is called with hbalock held to post a new iocb to
1049 * the firmware. This function copies the new iocb to ring iocb slot and
1050 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1051 * a completion call back for this iocb else the function will free the
1052 * iocb object.
1053 **/
1054 static void
1055 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1056 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1057 {
1058 /*
1059 * Set up an iotag
1060 */
1061 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1062
1063
1064 if (pring->ringno == LPFC_ELS_RING) {
1065 lpfc_debugfs_slow_ring_trc(phba,
1066 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1067 *(((uint32_t *) &nextiocb->iocb) + 4),
1068 *(((uint32_t *) &nextiocb->iocb) + 6),
1069 *(((uint32_t *) &nextiocb->iocb) + 7));
1070 }
1071
1072 /*
1073 * Issue iocb command to adapter
1074 */
1075 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1076 wmb();
1077 pring->stats.iocb_cmd++;
1078
1079 /*
1080 * If there is no completion routine to call, we can release the
1081 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1082 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1083 */
1084 if (nextiocb->iocb_cmpl)
1085 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1086 else
1087 __lpfc_sli_release_iocbq(phba, nextiocb);
1088
1089 /*
1090 * Let the HBA know what IOCB slot will be the next one the
1091 * driver will put a command into.
1092 */
1093 pring->cmdidx = pring->next_cmdidx;
1094 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1095 }
1096
1097 /**
1098 * lpfc_sli_update_full_ring - Update the chip attention register
1099 * @phba: Pointer to HBA context object.
1100 * @pring: Pointer to driver SLI ring object.
1101 *
1102 * The caller is not required to hold any lock for calling this function.
1103 * This function updates the chip attention bits for the ring to inform firmware
1104 * that there are pending work to be done for this ring and requests an
1105 * interrupt when there is space available in the ring. This function is
1106 * called when the driver is unable to post more iocbs to the ring due
1107 * to unavailability of space in the ring.
1108 **/
1109 static void
1110 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1111 {
1112 int ringno = pring->ringno;
1113
1114 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1115
1116 wmb();
1117
1118 /*
1119 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1120 * The HBA will tell us when an IOCB entry is available.
1121 */
1122 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1123 readl(phba->CAregaddr); /* flush */
1124
1125 pring->stats.iocb_cmd_full++;
1126 }
1127
1128 /**
1129 * lpfc_sli_update_ring - Update chip attention register
1130 * @phba: Pointer to HBA context object.
1131 * @pring: Pointer to driver SLI ring object.
1132 *
1133 * This function updates the chip attention register bit for the
1134 * given ring to inform HBA that there is more work to be done
1135 * in this ring. The caller is not required to hold any lock.
1136 **/
1137 static void
1138 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1139 {
1140 int ringno = pring->ringno;
1141
1142 /*
1143 * Tell the HBA that there is work to do in this ring.
1144 */
1145 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1146 wmb();
1147 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1148 readl(phba->CAregaddr); /* flush */
1149 }
1150 }
1151
1152 /**
1153 * lpfc_sli_resume_iocb - Process iocbs in the txq
1154 * @phba: Pointer to HBA context object.
1155 * @pring: Pointer to driver SLI ring object.
1156 *
1157 * This function is called with hbalock held to post pending iocbs
1158 * in the txq to the firmware. This function is called when driver
1159 * detects space available in the ring.
1160 **/
1161 static void
1162 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1163 {
1164 IOCB_t *iocb;
1165 struct lpfc_iocbq *nextiocb;
1166
1167 /*
1168 * Check to see if:
1169 * (a) there is anything on the txq to send
1170 * (b) link is up
1171 * (c) link attention events can be processed (fcp ring only)
1172 * (d) IOCB processing is not blocked by the outstanding mbox command.
1173 */
1174 if (pring->txq_cnt &&
1175 lpfc_is_link_up(phba) &&
1176 (pring->ringno != phba->sli.fcp_ring ||
1177 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1178
1179 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1180 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1181 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1182
1183 if (iocb)
1184 lpfc_sli_update_ring(phba, pring);
1185 else
1186 lpfc_sli_update_full_ring(phba, pring);
1187 }
1188
1189 return;
1190 }
1191
1192 /**
1193 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1194 * @phba: Pointer to HBA context object.
1195 * @hbqno: HBQ number.
1196 *
1197 * This function is called with hbalock held to get the next
1198 * available slot for the given HBQ. If there is free slot
1199 * available for the HBQ it will return pointer to the next available
1200 * HBQ entry else it will return NULL.
1201 **/
1202 static struct lpfc_hbq_entry *
1203 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1204 {
1205 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1206
1207 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1208 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1209 hbqp->next_hbqPutIdx = 0;
1210
1211 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1212 uint32_t raw_index = phba->hbq_get[hbqno];
1213 uint32_t getidx = le32_to_cpu(raw_index);
1214
1215 hbqp->local_hbqGetIdx = getidx;
1216
1217 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1218 lpfc_printf_log(phba, KERN_ERR,
1219 LOG_SLI | LOG_VPORT,
1220 "1802 HBQ %d: local_hbqGetIdx "
1221 "%u is > than hbqp->entry_count %u\n",
1222 hbqno, hbqp->local_hbqGetIdx,
1223 hbqp->entry_count);
1224
1225 phba->link_state = LPFC_HBA_ERROR;
1226 return NULL;
1227 }
1228
1229 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1230 return NULL;
1231 }
1232
1233 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1234 hbqp->hbqPutIdx;
1235 }
1236
1237 /**
1238 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1239 * @phba: Pointer to HBA context object.
1240 *
1241 * This function is called with no lock held to free all the
1242 * hbq buffers while uninitializing the SLI interface. It also
1243 * frees the HBQ buffers returned by the firmware but not yet
1244 * processed by the upper layers.
1245 **/
1246 void
1247 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1248 {
1249 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1250 struct hbq_dmabuf *hbq_buf;
1251 unsigned long flags;
1252 int i, hbq_count;
1253 uint32_t hbqno;
1254
1255 hbq_count = lpfc_sli_hbq_count();
1256 /* Return all memory used by all HBQs */
1257 spin_lock_irqsave(&phba->hbalock, flags);
1258 for (i = 0; i < hbq_count; ++i) {
1259 list_for_each_entry_safe(dmabuf, next_dmabuf,
1260 &phba->hbqs[i].hbq_buffer_list, list) {
1261 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1262 list_del(&hbq_buf->dbuf.list);
1263 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1264 }
1265 phba->hbqs[i].buffer_count = 0;
1266 }
1267 /* Return all HBQ buffer that are in-fly */
1268 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1269 list) {
1270 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271 list_del(&hbq_buf->dbuf.list);
1272 if (hbq_buf->tag == -1) {
1273 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274 (phba, hbq_buf);
1275 } else {
1276 hbqno = hbq_buf->tag >> 16;
1277 if (hbqno >= LPFC_MAX_HBQS)
1278 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1279 (phba, hbq_buf);
1280 else
1281 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1282 hbq_buf);
1283 }
1284 }
1285
1286 /* Mark the HBQs not in use */
1287 phba->hbq_in_use = 0;
1288 spin_unlock_irqrestore(&phba->hbalock, flags);
1289 }
1290
1291 /**
1292 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1293 * @phba: Pointer to HBA context object.
1294 * @hbqno: HBQ number.
1295 * @hbq_buf: Pointer to HBQ buffer.
1296 *
1297 * This function is called with the hbalock held to post a
1298 * hbq buffer to the firmware. If the function finds an empty
1299 * slot in the HBQ, it will post the buffer. The function will return
1300 * pointer to the hbq entry if it successfully post the buffer
1301 * else it will return NULL.
1302 **/
1303 static int
1304 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1305 struct hbq_dmabuf *hbq_buf)
1306 {
1307 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1308 }
1309
1310 /**
1311 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1312 * @phba: Pointer to HBA context object.
1313 * @hbqno: HBQ number.
1314 * @hbq_buf: Pointer to HBQ buffer.
1315 *
1316 * This function is called with the hbalock held to post a hbq buffer to the
1317 * firmware. If the function finds an empty slot in the HBQ, it will post the
1318 * buffer and place it on the hbq_buffer_list. The function will return zero if
1319 * it successfully post the buffer else it will return an error.
1320 **/
1321 static int
1322 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1323 struct hbq_dmabuf *hbq_buf)
1324 {
1325 struct lpfc_hbq_entry *hbqe;
1326 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1327
1328 /* Get next HBQ entry slot to use */
1329 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1330 if (hbqe) {
1331 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1332
1333 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1334 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1335 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1336 hbqe->bde.tus.f.bdeFlags = 0;
1337 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1338 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1339 /* Sync SLIM */
1340 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1341 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1342 /* flush */
1343 readl(phba->hbq_put + hbqno);
1344 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1345 return 0;
1346 } else
1347 return -ENOMEM;
1348 }
1349
1350 /**
1351 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1352 * @phba: Pointer to HBA context object.
1353 * @hbqno: HBQ number.
1354 * @hbq_buf: Pointer to HBQ buffer.
1355 *
1356 * This function is called with the hbalock held to post an RQE to the SLI4
1357 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1358 * the hbq_buffer_list and return zero, otherwise it will return an error.
1359 **/
1360 static int
1361 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1362 struct hbq_dmabuf *hbq_buf)
1363 {
1364 int rc;
1365 struct lpfc_rqe hrqe;
1366 struct lpfc_rqe drqe;
1367
1368 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1369 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1370 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1371 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1372 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1373 &hrqe, &drqe);
1374 if (rc < 0)
1375 return rc;
1376 hbq_buf->tag = rc;
1377 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1378 return 0;
1379 }
1380
1381 /* HBQ for ELS and CT traffic. */
1382 static struct lpfc_hbq_init lpfc_els_hbq = {
1383 .rn = 1,
1384 .entry_count = 256,
1385 .mask_count = 0,
1386 .profile = 0,
1387 .ring_mask = (1 << LPFC_ELS_RING),
1388 .buffer_count = 0,
1389 .init_count = 40,
1390 .add_count = 40,
1391 };
1392
1393 /* HBQ for the extra ring if needed */
1394 static struct lpfc_hbq_init lpfc_extra_hbq = {
1395 .rn = 1,
1396 .entry_count = 200,
1397 .mask_count = 0,
1398 .profile = 0,
1399 .ring_mask = (1 << LPFC_EXTRA_RING),
1400 .buffer_count = 0,
1401 .init_count = 0,
1402 .add_count = 5,
1403 };
1404
1405 /* Array of HBQs */
1406 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1407 &lpfc_els_hbq,
1408 &lpfc_extra_hbq,
1409 };
1410
1411 /**
1412 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1413 * @phba: Pointer to HBA context object.
1414 * @hbqno: HBQ number.
1415 * @count: Number of HBQ buffers to be posted.
1416 *
1417 * This function is called with no lock held to post more hbq buffers to the
1418 * given HBQ. The function returns the number of HBQ buffers successfully
1419 * posted.
1420 **/
1421 static int
1422 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1423 {
1424 uint32_t i, posted = 0;
1425 unsigned long flags;
1426 struct hbq_dmabuf *hbq_buffer;
1427 LIST_HEAD(hbq_buf_list);
1428 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1429 return 0;
1430
1431 if ((phba->hbqs[hbqno].buffer_count + count) >
1432 lpfc_hbq_defs[hbqno]->entry_count)
1433 count = lpfc_hbq_defs[hbqno]->entry_count -
1434 phba->hbqs[hbqno].buffer_count;
1435 if (!count)
1436 return 0;
1437 /* Allocate HBQ entries */
1438 for (i = 0; i < count; i++) {
1439 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1440 if (!hbq_buffer)
1441 break;
1442 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1443 }
1444 /* Check whether HBQ is still in use */
1445 spin_lock_irqsave(&phba->hbalock, flags);
1446 if (!phba->hbq_in_use)
1447 goto err;
1448 while (!list_empty(&hbq_buf_list)) {
1449 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1450 dbuf.list);
1451 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1452 (hbqno << 16));
1453 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1454 phba->hbqs[hbqno].buffer_count++;
1455 posted++;
1456 } else
1457 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1458 }
1459 spin_unlock_irqrestore(&phba->hbalock, flags);
1460 return posted;
1461 err:
1462 spin_unlock_irqrestore(&phba->hbalock, flags);
1463 while (!list_empty(&hbq_buf_list)) {
1464 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1465 dbuf.list);
1466 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467 }
1468 return 0;
1469 }
1470
1471 /**
1472 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1473 * @phba: Pointer to HBA context object.
1474 * @qno: HBQ number.
1475 *
1476 * This function posts more buffers to the HBQ. This function
1477 * is called with no lock held. The function returns the number of HBQ entries
1478 * successfully allocated.
1479 **/
1480 int
1481 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1482 {
1483 if (phba->sli_rev == LPFC_SLI_REV4)
1484 return 0;
1485 else
1486 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1487 lpfc_hbq_defs[qno]->add_count);
1488 }
1489
1490 /**
1491 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1492 * @phba: Pointer to HBA context object.
1493 * @qno: HBQ queue number.
1494 *
1495 * This function is called from SLI initialization code path with
1496 * no lock held to post initial HBQ buffers to firmware. The
1497 * function returns the number of HBQ entries successfully allocated.
1498 **/
1499 static int
1500 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1501 {
1502 if (phba->sli_rev == LPFC_SLI_REV4)
1503 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1504 lpfc_hbq_defs[qno]->entry_count);
1505 else
1506 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1507 lpfc_hbq_defs[qno]->init_count);
1508 }
1509
1510 /**
1511 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1512 * @phba: Pointer to HBA context object.
1513 * @hbqno: HBQ number.
1514 *
1515 * This function removes the first hbq buffer on an hbq list and returns a
1516 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1517 **/
1518 static struct hbq_dmabuf *
1519 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1520 {
1521 struct lpfc_dmabuf *d_buf;
1522
1523 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1524 if (!d_buf)
1525 return NULL;
1526 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1527 }
1528
1529 /**
1530 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1531 * @phba: Pointer to HBA context object.
1532 * @tag: Tag of the hbq buffer.
1533 *
1534 * This function is called with hbalock held. This function searches
1535 * for the hbq buffer associated with the given tag in the hbq buffer
1536 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1537 * it returns NULL.
1538 **/
1539 static struct hbq_dmabuf *
1540 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1541 {
1542 struct lpfc_dmabuf *d_buf;
1543 struct hbq_dmabuf *hbq_buf;
1544 uint32_t hbqno;
1545
1546 hbqno = tag >> 16;
1547 if (hbqno >= LPFC_MAX_HBQS)
1548 return NULL;
1549
1550 spin_lock_irq(&phba->hbalock);
1551 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1552 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1553 if (hbq_buf->tag == tag) {
1554 spin_unlock_irq(&phba->hbalock);
1555 return hbq_buf;
1556 }
1557 }
1558 spin_unlock_irq(&phba->hbalock);
1559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1560 "1803 Bad hbq tag. Data: x%x x%x\n",
1561 tag, phba->hbqs[tag >> 16].buffer_count);
1562 return NULL;
1563 }
1564
1565 /**
1566 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1567 * @phba: Pointer to HBA context object.
1568 * @hbq_buffer: Pointer to HBQ buffer.
1569 *
1570 * This function is called with hbalock. This function gives back
1571 * the hbq buffer to firmware. If the HBQ does not have space to
1572 * post the buffer, it will free the buffer.
1573 **/
1574 void
1575 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1576 {
1577 uint32_t hbqno;
1578
1579 if (hbq_buffer) {
1580 hbqno = hbq_buffer->tag >> 16;
1581 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1582 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1583 }
1584 }
1585
1586 /**
1587 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1588 * @mbxCommand: mailbox command code.
1589 *
1590 * This function is called by the mailbox event handler function to verify
1591 * that the completed mailbox command is a legitimate mailbox command. If the
1592 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1593 * and the mailbox event handler will take the HBA offline.
1594 **/
1595 static int
1596 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1597 {
1598 uint8_t ret;
1599
1600 switch (mbxCommand) {
1601 case MBX_LOAD_SM:
1602 case MBX_READ_NV:
1603 case MBX_WRITE_NV:
1604 case MBX_WRITE_VPARMS:
1605 case MBX_RUN_BIU_DIAG:
1606 case MBX_INIT_LINK:
1607 case MBX_DOWN_LINK:
1608 case MBX_CONFIG_LINK:
1609 case MBX_CONFIG_RING:
1610 case MBX_RESET_RING:
1611 case MBX_READ_CONFIG:
1612 case MBX_READ_RCONFIG:
1613 case MBX_READ_SPARM:
1614 case MBX_READ_STATUS:
1615 case MBX_READ_RPI:
1616 case MBX_READ_XRI:
1617 case MBX_READ_REV:
1618 case MBX_READ_LNK_STAT:
1619 case MBX_REG_LOGIN:
1620 case MBX_UNREG_LOGIN:
1621 case MBX_READ_LA:
1622 case MBX_CLEAR_LA:
1623 case MBX_DUMP_MEMORY:
1624 case MBX_DUMP_CONTEXT:
1625 case MBX_RUN_DIAGS:
1626 case MBX_RESTART:
1627 case MBX_UPDATE_CFG:
1628 case MBX_DOWN_LOAD:
1629 case MBX_DEL_LD_ENTRY:
1630 case MBX_RUN_PROGRAM:
1631 case MBX_SET_MASK:
1632 case MBX_SET_VARIABLE:
1633 case MBX_UNREG_D_ID:
1634 case MBX_KILL_BOARD:
1635 case MBX_CONFIG_FARP:
1636 case MBX_BEACON:
1637 case MBX_LOAD_AREA:
1638 case MBX_RUN_BIU_DIAG64:
1639 case MBX_CONFIG_PORT:
1640 case MBX_READ_SPARM64:
1641 case MBX_READ_RPI64:
1642 case MBX_REG_LOGIN64:
1643 case MBX_READ_LA64:
1644 case MBX_WRITE_WWN:
1645 case MBX_SET_DEBUG:
1646 case MBX_LOAD_EXP_ROM:
1647 case MBX_ASYNCEVT_ENABLE:
1648 case MBX_REG_VPI:
1649 case MBX_UNREG_VPI:
1650 case MBX_HEARTBEAT:
1651 case MBX_PORT_CAPABILITIES:
1652 case MBX_PORT_IOV_CONTROL:
1653 case MBX_SLI4_CONFIG:
1654 case MBX_SLI4_REQ_FTRS:
1655 case MBX_REG_FCFI:
1656 case MBX_UNREG_FCFI:
1657 case MBX_REG_VFI:
1658 case MBX_UNREG_VFI:
1659 case MBX_INIT_VPI:
1660 case MBX_INIT_VFI:
1661 case MBX_RESUME_RPI:
1662 case MBX_READ_EVENT_LOG_STATUS:
1663 case MBX_READ_EVENT_LOG:
1664 ret = mbxCommand;
1665 break;
1666 default:
1667 ret = MBX_SHUTDOWN;
1668 break;
1669 }
1670 return ret;
1671 }
1672
1673 /**
1674 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1675 * @phba: Pointer to HBA context object.
1676 * @pmboxq: Pointer to mailbox command.
1677 *
1678 * This is completion handler function for mailbox commands issued from
1679 * lpfc_sli_issue_mbox_wait function. This function is called by the
1680 * mailbox event handler function with no lock held. This function
1681 * will wake up thread waiting on the wait queue pointed by context1
1682 * of the mailbox.
1683 **/
1684 void
1685 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1686 {
1687 wait_queue_head_t *pdone_q;
1688 unsigned long drvr_flag;
1689
1690 /*
1691 * If pdone_q is empty, the driver thread gave up waiting and
1692 * continued running.
1693 */
1694 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1695 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1696 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1697 if (pdone_q)
1698 wake_up_interruptible(pdone_q);
1699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1700 return;
1701 }
1702
1703
1704 /**
1705 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1706 * @phba: Pointer to HBA context object.
1707 * @pmb: Pointer to mailbox object.
1708 *
1709 * This function is the default mailbox completion handler. It
1710 * frees the memory resources associated with the completed mailbox
1711 * command. If the completed command is a REG_LOGIN mailbox command,
1712 * this function will issue a UREG_LOGIN to re-claim the RPI.
1713 **/
1714 void
1715 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1716 {
1717 struct lpfc_dmabuf *mp;
1718 uint16_t rpi, vpi;
1719 int rc;
1720 struct lpfc_vport *vport = pmb->vport;
1721
1722 mp = (struct lpfc_dmabuf *) (pmb->context1);
1723
1724 if (mp) {
1725 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1726 kfree(mp);
1727 }
1728
1729 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1730 (phba->sli_rev == LPFC_SLI_REV4))
1731 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1732
1733 /*
1734 * If a REG_LOGIN succeeded after node is destroyed or node
1735 * is in re-discovery driver need to cleanup the RPI.
1736 */
1737 if (!(phba->pport->load_flag & FC_UNLOADING) &&
1738 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1739 !pmb->u.mb.mbxStatus) {
1740 rpi = pmb->u.mb.un.varWords[0];
1741 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1742 lpfc_unreg_login(phba, vpi, rpi, pmb);
1743 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1744 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1745 if (rc != MBX_NOT_FINISHED)
1746 return;
1747 }
1748
1749 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1750 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1751 !(phba->pport->load_flag & FC_UNLOADING) &&
1752 !pmb->u.mb.mbxStatus) {
1753 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1754 pmb->vport = vport;
1755 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1756 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1757 if (rc != MBX_NOT_FINISHED)
1758 return;
1759 }
1760
1761 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1762 lpfc_sli4_mbox_cmd_free(phba, pmb);
1763 else
1764 mempool_free(pmb, phba->mbox_mem_pool);
1765 }
1766
1767 /**
1768 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1769 * @phba: Pointer to HBA context object.
1770 *
1771 * This function is called with no lock held. This function processes all
1772 * the completed mailbox commands and gives it to upper layers. The interrupt
1773 * service routine processes mailbox completion interrupt and adds completed
1774 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1775 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1776 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1777 * function returns the mailbox commands to the upper layer by calling the
1778 * completion handler function of each mailbox.
1779 **/
1780 int
1781 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1782 {
1783 MAILBOX_t *pmbox;
1784 LPFC_MBOXQ_t *pmb;
1785 int rc;
1786 LIST_HEAD(cmplq);
1787
1788 phba->sli.slistat.mbox_event++;
1789
1790 /* Get all completed mailboxe buffers into the cmplq */
1791 spin_lock_irq(&phba->hbalock);
1792 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1793 spin_unlock_irq(&phba->hbalock);
1794
1795 /* Get a Mailbox buffer to setup mailbox commands for callback */
1796 do {
1797 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1798 if (pmb == NULL)
1799 break;
1800
1801 pmbox = &pmb->u.mb;
1802
1803 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1804 if (pmb->vport) {
1805 lpfc_debugfs_disc_trc(pmb->vport,
1806 LPFC_DISC_TRC_MBOX_VPORT,
1807 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1808 (uint32_t)pmbox->mbxCommand,
1809 pmbox->un.varWords[0],
1810 pmbox->un.varWords[1]);
1811 }
1812 else {
1813 lpfc_debugfs_disc_trc(phba->pport,
1814 LPFC_DISC_TRC_MBOX,
1815 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1816 (uint32_t)pmbox->mbxCommand,
1817 pmbox->un.varWords[0],
1818 pmbox->un.varWords[1]);
1819 }
1820 }
1821
1822 /*
1823 * It is a fatal error if unknown mbox command completion.
1824 */
1825 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1826 MBX_SHUTDOWN) {
1827 /* Unknown mailbox command compl */
1828 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1829 "(%d):0323 Unknown Mailbox command "
1830 "x%x (x%x) Cmpl\n",
1831 pmb->vport ? pmb->vport->vpi : 0,
1832 pmbox->mbxCommand,
1833 lpfc_sli4_mbox_opcode_get(phba, pmb));
1834 phba->link_state = LPFC_HBA_ERROR;
1835 phba->work_hs = HS_FFER3;
1836 lpfc_handle_eratt(phba);
1837 continue;
1838 }
1839
1840 if (pmbox->mbxStatus) {
1841 phba->sli.slistat.mbox_stat_err++;
1842 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1843 /* Mbox cmd cmpl error - RETRYing */
1844 lpfc_printf_log(phba, KERN_INFO,
1845 LOG_MBOX | LOG_SLI,
1846 "(%d):0305 Mbox cmd cmpl "
1847 "error - RETRYing Data: x%x "
1848 "(x%x) x%x x%x x%x\n",
1849 pmb->vport ? pmb->vport->vpi :0,
1850 pmbox->mbxCommand,
1851 lpfc_sli4_mbox_opcode_get(phba,
1852 pmb),
1853 pmbox->mbxStatus,
1854 pmbox->un.varWords[0],
1855 pmb->vport->port_state);
1856 pmbox->mbxStatus = 0;
1857 pmbox->mbxOwner = OWN_HOST;
1858 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1859 if (rc != MBX_NOT_FINISHED)
1860 continue;
1861 }
1862 }
1863
1864 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1865 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1866 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1867 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1868 pmb->vport ? pmb->vport->vpi : 0,
1869 pmbox->mbxCommand,
1870 lpfc_sli4_mbox_opcode_get(phba, pmb),
1871 pmb->mbox_cmpl,
1872 *((uint32_t *) pmbox),
1873 pmbox->un.varWords[0],
1874 pmbox->un.varWords[1],
1875 pmbox->un.varWords[2],
1876 pmbox->un.varWords[3],
1877 pmbox->un.varWords[4],
1878 pmbox->un.varWords[5],
1879 pmbox->un.varWords[6],
1880 pmbox->un.varWords[7]);
1881
1882 if (pmb->mbox_cmpl)
1883 pmb->mbox_cmpl(phba,pmb);
1884 } while (1);
1885 return 0;
1886 }
1887
1888 /**
1889 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1890 * @phba: Pointer to HBA context object.
1891 * @pring: Pointer to driver SLI ring object.
1892 * @tag: buffer tag.
1893 *
1894 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1895 * is set in the tag the buffer is posted for a particular exchange,
1896 * the function will return the buffer without replacing the buffer.
1897 * If the buffer is for unsolicited ELS or CT traffic, this function
1898 * returns the buffer and also posts another buffer to the firmware.
1899 **/
1900 static struct lpfc_dmabuf *
1901 lpfc_sli_get_buff(struct lpfc_hba *phba,
1902 struct lpfc_sli_ring *pring,
1903 uint32_t tag)
1904 {
1905 struct hbq_dmabuf *hbq_entry;
1906
1907 if (tag & QUE_BUFTAG_BIT)
1908 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1909 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1910 if (!hbq_entry)
1911 return NULL;
1912 return &hbq_entry->dbuf;
1913 }
1914
1915 /**
1916 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1917 * @phba: Pointer to HBA context object.
1918 * @pring: Pointer to driver SLI ring object.
1919 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1920 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1921 * @fch_type: the type for the first frame of the sequence.
1922 *
1923 * This function is called with no lock held. This function uses the r_ctl and
1924 * type of the received sequence to find the correct callback function to call
1925 * to process the sequence.
1926 **/
1927 static int
1928 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1929 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1930 uint32_t fch_type)
1931 {
1932 int i;
1933
1934 /* unSolicited Responses */
1935 if (pring->prt[0].profile) {
1936 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1937 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1938 saveq);
1939 return 1;
1940 }
1941 /* We must search, based on rctl / type
1942 for the right routine */
1943 for (i = 0; i < pring->num_mask; i++) {
1944 if ((pring->prt[i].rctl == fch_r_ctl) &&
1945 (pring->prt[i].type == fch_type)) {
1946 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1947 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1948 (phba, pring, saveq);
1949 return 1;
1950 }
1951 }
1952 return 0;
1953 }
1954
1955 /**
1956 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1957 * @phba: Pointer to HBA context object.
1958 * @pring: Pointer to driver SLI ring object.
1959 * @saveq: Pointer to the unsolicited iocb.
1960 *
1961 * This function is called with no lock held by the ring event handler
1962 * when there is an unsolicited iocb posted to the response ring by the
1963 * firmware. This function gets the buffer associated with the iocbs
1964 * and calls the event handler for the ring. This function handles both
1965 * qring buffers and hbq buffers.
1966 * When the function returns 1 the caller can free the iocb object otherwise
1967 * upper layer functions will free the iocb objects.
1968 **/
1969 static int
1970 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1971 struct lpfc_iocbq *saveq)
1972 {
1973 IOCB_t * irsp;
1974 WORD5 * w5p;
1975 uint32_t Rctl, Type;
1976 uint32_t match;
1977 struct lpfc_iocbq *iocbq;
1978 struct lpfc_dmabuf *dmzbuf;
1979
1980 match = 0;
1981 irsp = &(saveq->iocb);
1982
1983 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1984 if (pring->lpfc_sli_rcv_async_status)
1985 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1986 else
1987 lpfc_printf_log(phba,
1988 KERN_WARNING,
1989 LOG_SLI,
1990 "0316 Ring %d handler: unexpected "
1991 "ASYNC_STATUS iocb received evt_code "
1992 "0x%x\n",
1993 pring->ringno,
1994 irsp->un.asyncstat.evt_code);
1995 return 1;
1996 }
1997
1998 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1999 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2000 if (irsp->ulpBdeCount > 0) {
2001 dmzbuf = lpfc_sli_get_buff(phba, pring,
2002 irsp->un.ulpWord[3]);
2003 lpfc_in_buf_free(phba, dmzbuf);
2004 }
2005
2006 if (irsp->ulpBdeCount > 1) {
2007 dmzbuf = lpfc_sli_get_buff(phba, pring,
2008 irsp->unsli3.sli3Words[3]);
2009 lpfc_in_buf_free(phba, dmzbuf);
2010 }
2011
2012 if (irsp->ulpBdeCount > 2) {
2013 dmzbuf = lpfc_sli_get_buff(phba, pring,
2014 irsp->unsli3.sli3Words[7]);
2015 lpfc_in_buf_free(phba, dmzbuf);
2016 }
2017
2018 return 1;
2019 }
2020
2021 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2022 if (irsp->ulpBdeCount != 0) {
2023 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2024 irsp->un.ulpWord[3]);
2025 if (!saveq->context2)
2026 lpfc_printf_log(phba,
2027 KERN_ERR,
2028 LOG_SLI,
2029 "0341 Ring %d Cannot find buffer for "
2030 "an unsolicited iocb. tag 0x%x\n",
2031 pring->ringno,
2032 irsp->un.ulpWord[3]);
2033 }
2034 if (irsp->ulpBdeCount == 2) {
2035 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2036 irsp->unsli3.sli3Words[7]);
2037 if (!saveq->context3)
2038 lpfc_printf_log(phba,
2039 KERN_ERR,
2040 LOG_SLI,
2041 "0342 Ring %d Cannot find buffer for an"
2042 " unsolicited iocb. tag 0x%x\n",
2043 pring->ringno,
2044 irsp->unsli3.sli3Words[7]);
2045 }
2046 list_for_each_entry(iocbq, &saveq->list, list) {
2047 irsp = &(iocbq->iocb);
2048 if (irsp->ulpBdeCount != 0) {
2049 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2050 irsp->un.ulpWord[3]);
2051 if (!iocbq->context2)
2052 lpfc_printf_log(phba,
2053 KERN_ERR,
2054 LOG_SLI,
2055 "0343 Ring %d Cannot find "
2056 "buffer for an unsolicited iocb"
2057 ". tag 0x%x\n", pring->ringno,
2058 irsp->un.ulpWord[3]);
2059 }
2060 if (irsp->ulpBdeCount == 2) {
2061 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2062 irsp->unsli3.sli3Words[7]);
2063 if (!iocbq->context3)
2064 lpfc_printf_log(phba,
2065 KERN_ERR,
2066 LOG_SLI,
2067 "0344 Ring %d Cannot find "
2068 "buffer for an unsolicited "
2069 "iocb. tag 0x%x\n",
2070 pring->ringno,
2071 irsp->unsli3.sli3Words[7]);
2072 }
2073 }
2074 }
2075 if (irsp->ulpBdeCount != 0 &&
2076 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2077 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2078 int found = 0;
2079
2080 /* search continue save q for same XRI */
2081 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2082 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2083 list_add_tail(&saveq->list, &iocbq->list);
2084 found = 1;
2085 break;
2086 }
2087 }
2088 if (!found)
2089 list_add_tail(&saveq->clist,
2090 &pring->iocb_continue_saveq);
2091 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2092 list_del_init(&iocbq->clist);
2093 saveq = iocbq;
2094 irsp = &(saveq->iocb);
2095 } else
2096 return 0;
2097 }
2098 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2099 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2100 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2101 Rctl = FC_RCTL_ELS_REQ;
2102 Type = FC_TYPE_ELS;
2103 } else {
2104 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2105 Rctl = w5p->hcsw.Rctl;
2106 Type = w5p->hcsw.Type;
2107
2108 /* Firmware Workaround */
2109 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2110 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2111 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2112 Rctl = FC_RCTL_ELS_REQ;
2113 Type = FC_TYPE_ELS;
2114 w5p->hcsw.Rctl = Rctl;
2115 w5p->hcsw.Type = Type;
2116 }
2117 }
2118
2119 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2120 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2121 "0313 Ring %d handler: unexpected Rctl x%x "
2122 "Type x%x received\n",
2123 pring->ringno, Rctl, Type);
2124
2125 return 1;
2126 }
2127
2128 /**
2129 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2130 * @phba: Pointer to HBA context object.
2131 * @pring: Pointer to driver SLI ring object.
2132 * @prspiocb: Pointer to response iocb object.
2133 *
2134 * This function looks up the iocb_lookup table to get the command iocb
2135 * corresponding to the given response iocb using the iotag of the
2136 * response iocb. This function is called with the hbalock held.
2137 * This function returns the command iocb object if it finds the command
2138 * iocb else returns NULL.
2139 **/
2140 static struct lpfc_iocbq *
2141 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2142 struct lpfc_sli_ring *pring,
2143 struct lpfc_iocbq *prspiocb)
2144 {
2145 struct lpfc_iocbq *cmd_iocb = NULL;
2146 uint16_t iotag;
2147
2148 iotag = prspiocb->iocb.ulpIoTag;
2149
2150 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2151 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2152 list_del_init(&cmd_iocb->list);
2153 pring->txcmplq_cnt--;
2154 return cmd_iocb;
2155 }
2156
2157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2158 "0317 iotag x%x is out off "
2159 "range: max iotag x%x wd0 x%x\n",
2160 iotag, phba->sli.last_iotag,
2161 *(((uint32_t *) &prspiocb->iocb) + 7));
2162 return NULL;
2163 }
2164
2165 /**
2166 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2167 * @phba: Pointer to HBA context object.
2168 * @pring: Pointer to driver SLI ring object.
2169 * @iotag: IOCB tag.
2170 *
2171 * This function looks up the iocb_lookup table to get the command iocb
2172 * corresponding to the given iotag. This function is called with the
2173 * hbalock held.
2174 * This function returns the command iocb object if it finds the command
2175 * iocb else returns NULL.
2176 **/
2177 static struct lpfc_iocbq *
2178 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2179 struct lpfc_sli_ring *pring, uint16_t iotag)
2180 {
2181 struct lpfc_iocbq *cmd_iocb;
2182
2183 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2184 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2185 list_del_init(&cmd_iocb->list);
2186 pring->txcmplq_cnt--;
2187 return cmd_iocb;
2188 }
2189
2190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2191 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2192 iotag, phba->sli.last_iotag);
2193 return NULL;
2194 }
2195
2196 /**
2197 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2198 * @phba: Pointer to HBA context object.
2199 * @pring: Pointer to driver SLI ring object.
2200 * @saveq: Pointer to the response iocb to be processed.
2201 *
2202 * This function is called by the ring event handler for non-fcp
2203 * rings when there is a new response iocb in the response ring.
2204 * The caller is not required to hold any locks. This function
2205 * gets the command iocb associated with the response iocb and
2206 * calls the completion handler for the command iocb. If there
2207 * is no completion handler, the function will free the resources
2208 * associated with command iocb. If the response iocb is for
2209 * an already aborted command iocb, the status of the completion
2210 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2211 * This function always returns 1.
2212 **/
2213 static int
2214 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2215 struct lpfc_iocbq *saveq)
2216 {
2217 struct lpfc_iocbq *cmdiocbp;
2218 int rc = 1;
2219 unsigned long iflag;
2220
2221 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2222 spin_lock_irqsave(&phba->hbalock, iflag);
2223 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2224 spin_unlock_irqrestore(&phba->hbalock, iflag);
2225
2226 if (cmdiocbp) {
2227 if (cmdiocbp->iocb_cmpl) {
2228 /*
2229 * If an ELS command failed send an event to mgmt
2230 * application.
2231 */
2232 if (saveq->iocb.ulpStatus &&
2233 (pring->ringno == LPFC_ELS_RING) &&
2234 (cmdiocbp->iocb.ulpCommand ==
2235 CMD_ELS_REQUEST64_CR))
2236 lpfc_send_els_failure_event(phba,
2237 cmdiocbp, saveq);
2238
2239 /*
2240 * Post all ELS completions to the worker thread.
2241 * All other are passed to the completion callback.
2242 */
2243 if (pring->ringno == LPFC_ELS_RING) {
2244 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2245 (cmdiocbp->iocb_flag &
2246 LPFC_DRIVER_ABORTED)) {
2247 spin_lock_irqsave(&phba->hbalock,
2248 iflag);
2249 cmdiocbp->iocb_flag &=
2250 ~LPFC_DRIVER_ABORTED;
2251 spin_unlock_irqrestore(&phba->hbalock,
2252 iflag);
2253 saveq->iocb.ulpStatus =
2254 IOSTAT_LOCAL_REJECT;
2255 saveq->iocb.un.ulpWord[4] =
2256 IOERR_SLI_ABORTED;
2257
2258 /* Firmware could still be in progress
2259 * of DMAing payload, so don't free data
2260 * buffer till after a hbeat.
2261 */
2262 spin_lock_irqsave(&phba->hbalock,
2263 iflag);
2264 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2265 spin_unlock_irqrestore(&phba->hbalock,
2266 iflag);
2267 }
2268 if (phba->sli_rev == LPFC_SLI_REV4) {
2269 if (saveq->iocb_flag &
2270 LPFC_EXCHANGE_BUSY) {
2271 /* Set cmdiocb flag for the
2272 * exchange busy so sgl (xri)
2273 * will not be released until
2274 * the abort xri is received
2275 * from hba.
2276 */
2277 spin_lock_irqsave(
2278 &phba->hbalock, iflag);
2279 cmdiocbp->iocb_flag |=
2280 LPFC_EXCHANGE_BUSY;
2281 spin_unlock_irqrestore(
2282 &phba->hbalock, iflag);
2283 }
2284 if (cmdiocbp->iocb_flag &
2285 LPFC_DRIVER_ABORTED) {
2286 /*
2287 * Clear LPFC_DRIVER_ABORTED
2288 * bit in case it was driver
2289 * initiated abort.
2290 */
2291 spin_lock_irqsave(
2292 &phba->hbalock, iflag);
2293 cmdiocbp->iocb_flag &=
2294 ~LPFC_DRIVER_ABORTED;
2295 spin_unlock_irqrestore(
2296 &phba->hbalock, iflag);
2297 cmdiocbp->iocb.ulpStatus =
2298 IOSTAT_LOCAL_REJECT;
2299 cmdiocbp->iocb.un.ulpWord[4] =
2300 IOERR_ABORT_REQUESTED;
2301 /*
2302 * For SLI4, irsiocb contains
2303 * NO_XRI in sli_xritag, it
2304 * shall not affect releasing
2305 * sgl (xri) process.
2306 */
2307 saveq->iocb.ulpStatus =
2308 IOSTAT_LOCAL_REJECT;
2309 saveq->iocb.un.ulpWord[4] =
2310 IOERR_SLI_ABORTED;
2311 spin_lock_irqsave(
2312 &phba->hbalock, iflag);
2313 saveq->iocb_flag |=
2314 LPFC_DELAY_MEM_FREE;
2315 spin_unlock_irqrestore(
2316 &phba->hbalock, iflag);
2317 }
2318 }
2319 }
2320 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2321 } else
2322 lpfc_sli_release_iocbq(phba, cmdiocbp);
2323 } else {
2324 /*
2325 * Unknown initiating command based on the response iotag.
2326 * This could be the case on the ELS ring because of
2327 * lpfc_els_abort().
2328 */
2329 if (pring->ringno != LPFC_ELS_RING) {
2330 /*
2331 * Ring <ringno> handler: unexpected completion IoTag
2332 * <IoTag>
2333 */
2334 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2335 "0322 Ring %d handler: "
2336 "unexpected completion IoTag x%x "
2337 "Data: x%x x%x x%x x%x\n",
2338 pring->ringno,
2339 saveq->iocb.ulpIoTag,
2340 saveq->iocb.ulpStatus,
2341 saveq->iocb.un.ulpWord[4],
2342 saveq->iocb.ulpCommand,
2343 saveq->iocb.ulpContext);
2344 }
2345 }
2346
2347 return rc;
2348 }
2349
2350 /**
2351 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2352 * @phba: Pointer to HBA context object.
2353 * @pring: Pointer to driver SLI ring object.
2354 *
2355 * This function is called from the iocb ring event handlers when
2356 * put pointer is ahead of the get pointer for a ring. This function signal
2357 * an error attention condition to the worker thread and the worker
2358 * thread will transition the HBA to offline state.
2359 **/
2360 static void
2361 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2362 {
2363 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2364 /*
2365 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2366 * rsp ring <portRspMax>
2367 */
2368 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2369 "0312 Ring %d handler: portRspPut %d "
2370 "is bigger than rsp ring %d\n",
2371 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2372 pring->numRiocb);
2373
2374 phba->link_state = LPFC_HBA_ERROR;
2375
2376 /*
2377 * All error attention handlers are posted to
2378 * worker thread
2379 */
2380 phba->work_ha |= HA_ERATT;
2381 phba->work_hs = HS_FFER3;
2382
2383 lpfc_worker_wake_up(phba);
2384
2385 return;
2386 }
2387
2388 /**
2389 * lpfc_poll_eratt - Error attention polling timer timeout handler
2390 * @ptr: Pointer to address of HBA context object.
2391 *
2392 * This function is invoked by the Error Attention polling timer when the
2393 * timer times out. It will check the SLI Error Attention register for
2394 * possible attention events. If so, it will post an Error Attention event
2395 * and wake up worker thread to process it. Otherwise, it will set up the
2396 * Error Attention polling timer for the next poll.
2397 **/
2398 void lpfc_poll_eratt(unsigned long ptr)
2399 {
2400 struct lpfc_hba *phba;
2401 uint32_t eratt = 0;
2402
2403 phba = (struct lpfc_hba *)ptr;
2404
2405 /* Check chip HA register for error event */
2406 eratt = lpfc_sli_check_eratt(phba);
2407
2408 if (eratt)
2409 /* Tell the worker thread there is work to do */
2410 lpfc_worker_wake_up(phba);
2411 else
2412 /* Restart the timer for next eratt poll */
2413 mod_timer(&phba->eratt_poll, jiffies +
2414 HZ * LPFC_ERATT_POLL_INTERVAL);
2415 return;
2416 }
2417
2418
2419 /**
2420 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2421 * @phba: Pointer to HBA context object.
2422 * @pring: Pointer to driver SLI ring object.
2423 * @mask: Host attention register mask for this ring.
2424 *
2425 * This function is called from the interrupt context when there is a ring
2426 * event for the fcp ring. The caller does not hold any lock.
2427 * The function processes each response iocb in the response ring until it
2428 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2429 * LE bit set. The function will call the completion handler of the command iocb
2430 * if the response iocb indicates a completion for a command iocb or it is
2431 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2432 * function if this is an unsolicited iocb.
2433 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2434 * to check it explicitly.
2435 */
2436 int
2437 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2438 struct lpfc_sli_ring *pring, uint32_t mask)
2439 {
2440 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2441 IOCB_t *irsp = NULL;
2442 IOCB_t *entry = NULL;
2443 struct lpfc_iocbq *cmdiocbq = NULL;
2444 struct lpfc_iocbq rspiocbq;
2445 uint32_t status;
2446 uint32_t portRspPut, portRspMax;
2447 int rc = 1;
2448 lpfc_iocb_type type;
2449 unsigned long iflag;
2450 uint32_t rsp_cmpl = 0;
2451
2452 spin_lock_irqsave(&phba->hbalock, iflag);
2453 pring->stats.iocb_event++;
2454
2455 /*
2456 * The next available response entry should never exceed the maximum
2457 * entries. If it does, treat it as an adapter hardware error.
2458 */
2459 portRspMax = pring->numRiocb;
2460 portRspPut = le32_to_cpu(pgp->rspPutInx);
2461 if (unlikely(portRspPut >= portRspMax)) {
2462 lpfc_sli_rsp_pointers_error(phba, pring);
2463 spin_unlock_irqrestore(&phba->hbalock, iflag);
2464 return 1;
2465 }
2466 if (phba->fcp_ring_in_use) {
2467 spin_unlock_irqrestore(&phba->hbalock, iflag);
2468 return 1;
2469 } else
2470 phba->fcp_ring_in_use = 1;
2471
2472 rmb();
2473 while (pring->rspidx != portRspPut) {
2474 /*
2475 * Fetch an entry off the ring and copy it into a local data
2476 * structure. The copy involves a byte-swap since the
2477 * network byte order and pci byte orders are different.
2478 */
2479 entry = lpfc_resp_iocb(phba, pring);
2480 phba->last_completion_time = jiffies;
2481
2482 if (++pring->rspidx >= portRspMax)
2483 pring->rspidx = 0;
2484
2485 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2486 (uint32_t *) &rspiocbq.iocb,
2487 phba->iocb_rsp_size);
2488 INIT_LIST_HEAD(&(rspiocbq.list));
2489 irsp = &rspiocbq.iocb;
2490
2491 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2492 pring->stats.iocb_rsp++;
2493 rsp_cmpl++;
2494
2495 if (unlikely(irsp->ulpStatus)) {
2496 /*
2497 * If resource errors reported from HBA, reduce
2498 * queuedepths of the SCSI device.
2499 */
2500 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2501 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2502 spin_unlock_irqrestore(&phba->hbalock, iflag);
2503 phba->lpfc_rampdown_queue_depth(phba);
2504 spin_lock_irqsave(&phba->hbalock, iflag);
2505 }
2506
2507 /* Rsp ring <ringno> error: IOCB */
2508 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2509 "0336 Rsp Ring %d error: IOCB Data: "
2510 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2511 pring->ringno,
2512 irsp->un.ulpWord[0],
2513 irsp->un.ulpWord[1],
2514 irsp->un.ulpWord[2],
2515 irsp->un.ulpWord[3],
2516 irsp->un.ulpWord[4],
2517 irsp->un.ulpWord[5],
2518 *(uint32_t *)&irsp->un1,
2519 *((uint32_t *)&irsp->un1 + 1));
2520 }
2521
2522 switch (type) {
2523 case LPFC_ABORT_IOCB:
2524 case LPFC_SOL_IOCB:
2525 /*
2526 * Idle exchange closed via ABTS from port. No iocb
2527 * resources need to be recovered.
2528 */
2529 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2530 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2531 "0333 IOCB cmd 0x%x"
2532 " processed. Skipping"
2533 " completion\n",
2534 irsp->ulpCommand);
2535 break;
2536 }
2537
2538 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2539 &rspiocbq);
2540 if (unlikely(!cmdiocbq))
2541 break;
2542 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2543 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2544 if (cmdiocbq->iocb_cmpl) {
2545 spin_unlock_irqrestore(&phba->hbalock, iflag);
2546 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2547 &rspiocbq);
2548 spin_lock_irqsave(&phba->hbalock, iflag);
2549 }
2550 break;
2551 case LPFC_UNSOL_IOCB:
2552 spin_unlock_irqrestore(&phba->hbalock, iflag);
2553 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2554 spin_lock_irqsave(&phba->hbalock, iflag);
2555 break;
2556 default:
2557 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2558 char adaptermsg[LPFC_MAX_ADPTMSG];
2559 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2560 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2561 MAX_MSG_DATA);
2562 dev_warn(&((phba->pcidev)->dev),
2563 "lpfc%d: %s\n",
2564 phba->brd_no, adaptermsg);
2565 } else {
2566 /* Unknown IOCB command */
2567 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2568 "0334 Unknown IOCB command "
2569 "Data: x%x, x%x x%x x%x x%x\n",
2570 type, irsp->ulpCommand,
2571 irsp->ulpStatus,
2572 irsp->ulpIoTag,
2573 irsp->ulpContext);
2574 }
2575 break;
2576 }
2577
2578 /*
2579 * The response IOCB has been processed. Update the ring
2580 * pointer in SLIM. If the port response put pointer has not
2581 * been updated, sync the pgp->rspPutInx and fetch the new port
2582 * response put pointer.
2583 */
2584 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2585
2586 if (pring->rspidx == portRspPut)
2587 portRspPut = le32_to_cpu(pgp->rspPutInx);
2588 }
2589
2590 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2591 pring->stats.iocb_rsp_full++;
2592 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2593 writel(status, phba->CAregaddr);
2594 readl(phba->CAregaddr);
2595 }
2596 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2597 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2598 pring->stats.iocb_cmd_empty++;
2599
2600 /* Force update of the local copy of cmdGetInx */
2601 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2602 lpfc_sli_resume_iocb(phba, pring);
2603
2604 if ((pring->lpfc_sli_cmd_available))
2605 (pring->lpfc_sli_cmd_available) (phba, pring);
2606
2607 }
2608
2609 phba->fcp_ring_in_use = 0;
2610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2611 return rc;
2612 }
2613
2614 /**
2615 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2616 * @phba: Pointer to HBA context object.
2617 * @pring: Pointer to driver SLI ring object.
2618 * @rspiocbp: Pointer to driver response IOCB object.
2619 *
2620 * This function is called from the worker thread when there is a slow-path
2621 * response IOCB to process. This function chains all the response iocbs until
2622 * seeing the iocb with the LE bit set. The function will call
2623 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2624 * completion of a command iocb. The function will call the
2625 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2626 * The function frees the resources or calls the completion handler if this
2627 * iocb is an abort completion. The function returns NULL when the response
2628 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2629 * this function shall chain the iocb on to the iocb_continueq and return the
2630 * response iocb passed in.
2631 **/
2632 static struct lpfc_iocbq *
2633 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2634 struct lpfc_iocbq *rspiocbp)
2635 {
2636 struct lpfc_iocbq *saveq;
2637 struct lpfc_iocbq *cmdiocbp;
2638 struct lpfc_iocbq *next_iocb;
2639 IOCB_t *irsp = NULL;
2640 uint32_t free_saveq;
2641 uint8_t iocb_cmd_type;
2642 lpfc_iocb_type type;
2643 unsigned long iflag;
2644 int rc;
2645
2646 spin_lock_irqsave(&phba->hbalock, iflag);
2647 /* First add the response iocb to the countinueq list */
2648 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2649 pring->iocb_continueq_cnt++;
2650
2651 /* Now, determine whetehr the list is completed for processing */
2652 irsp = &rspiocbp->iocb;
2653 if (irsp->ulpLe) {
2654 /*
2655 * By default, the driver expects to free all resources
2656 * associated with this iocb completion.
2657 */
2658 free_saveq = 1;
2659 saveq = list_get_first(&pring->iocb_continueq,
2660 struct lpfc_iocbq, list);
2661 irsp = &(saveq->iocb);
2662 list_del_init(&pring->iocb_continueq);
2663 pring->iocb_continueq_cnt = 0;
2664
2665 pring->stats.iocb_rsp++;
2666
2667 /*
2668 * If resource errors reported from HBA, reduce
2669 * queuedepths of the SCSI device.
2670 */
2671 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2672 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2673 spin_unlock_irqrestore(&phba->hbalock, iflag);
2674 phba->lpfc_rampdown_queue_depth(phba);
2675 spin_lock_irqsave(&phba->hbalock, iflag);
2676 }
2677
2678 if (irsp->ulpStatus) {
2679 /* Rsp ring <ringno> error: IOCB */
2680 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2681 "0328 Rsp Ring %d error: "
2682 "IOCB Data: "
2683 "x%x x%x x%x x%x "
2684 "x%x x%x x%x x%x "
2685 "x%x x%x x%x x%x "
2686 "x%x x%x x%x x%x\n",
2687 pring->ringno,
2688 irsp->un.ulpWord[0],
2689 irsp->un.ulpWord[1],
2690 irsp->un.ulpWord[2],
2691 irsp->un.ulpWord[3],
2692 irsp->un.ulpWord[4],
2693 irsp->un.ulpWord[5],
2694 *(((uint32_t *) irsp) + 6),
2695 *(((uint32_t *) irsp) + 7),
2696 *(((uint32_t *) irsp) + 8),
2697 *(((uint32_t *) irsp) + 9),
2698 *(((uint32_t *) irsp) + 10),
2699 *(((uint32_t *) irsp) + 11),
2700 *(((uint32_t *) irsp) + 12),
2701 *(((uint32_t *) irsp) + 13),
2702 *(((uint32_t *) irsp) + 14),
2703 *(((uint32_t *) irsp) + 15));
2704 }
2705
2706 /*
2707 * Fetch the IOCB command type and call the correct completion
2708 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2709 * get freed back to the lpfc_iocb_list by the discovery
2710 * kernel thread.
2711 */
2712 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2713 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2714 switch (type) {
2715 case LPFC_SOL_IOCB:
2716 spin_unlock_irqrestore(&phba->hbalock, iflag);
2717 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2718 spin_lock_irqsave(&phba->hbalock, iflag);
2719 break;
2720
2721 case LPFC_UNSOL_IOCB:
2722 spin_unlock_irqrestore(&phba->hbalock, iflag);
2723 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2724 spin_lock_irqsave(&phba->hbalock, iflag);
2725 if (!rc)
2726 free_saveq = 0;
2727 break;
2728
2729 case LPFC_ABORT_IOCB:
2730 cmdiocbp = NULL;
2731 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2732 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2733 saveq);
2734 if (cmdiocbp) {
2735 /* Call the specified completion routine */
2736 if (cmdiocbp->iocb_cmpl) {
2737 spin_unlock_irqrestore(&phba->hbalock,
2738 iflag);
2739 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2740 saveq);
2741 spin_lock_irqsave(&phba->hbalock,
2742 iflag);
2743 } else
2744 __lpfc_sli_release_iocbq(phba,
2745 cmdiocbp);
2746 }
2747 break;
2748
2749 case LPFC_UNKNOWN_IOCB:
2750 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2751 char adaptermsg[LPFC_MAX_ADPTMSG];
2752 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2753 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2754 MAX_MSG_DATA);
2755 dev_warn(&((phba->pcidev)->dev),
2756 "lpfc%d: %s\n",
2757 phba->brd_no, adaptermsg);
2758 } else {
2759 /* Unknown IOCB command */
2760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2761 "0335 Unknown IOCB "
2762 "command Data: x%x "
2763 "x%x x%x x%x\n",
2764 irsp->ulpCommand,
2765 irsp->ulpStatus,
2766 irsp->ulpIoTag,
2767 irsp->ulpContext);
2768 }
2769 break;
2770 }
2771
2772 if (free_saveq) {
2773 list_for_each_entry_safe(rspiocbp, next_iocb,
2774 &saveq->list, list) {
2775 list_del(&rspiocbp->list);
2776 __lpfc_sli_release_iocbq(phba, rspiocbp);
2777 }
2778 __lpfc_sli_release_iocbq(phba, saveq);
2779 }
2780 rspiocbp = NULL;
2781 }
2782 spin_unlock_irqrestore(&phba->hbalock, iflag);
2783 return rspiocbp;
2784 }
2785
2786 /**
2787 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2788 * @phba: Pointer to HBA context object.
2789 * @pring: Pointer to driver SLI ring object.
2790 * @mask: Host attention register mask for this ring.
2791 *
2792 * This routine wraps the actual slow_ring event process routine from the
2793 * API jump table function pointer from the lpfc_hba struct.
2794 **/
2795 void
2796 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2797 struct lpfc_sli_ring *pring, uint32_t mask)
2798 {
2799 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2800 }
2801
2802 /**
2803 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2804 * @phba: Pointer to HBA context object.
2805 * @pring: Pointer to driver SLI ring object.
2806 * @mask: Host attention register mask for this ring.
2807 *
2808 * This function is called from the worker thread when there is a ring event
2809 * for non-fcp rings. The caller does not hold any lock. The function will
2810 * remove each response iocb in the response ring and calls the handle
2811 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2812 **/
2813 static void
2814 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2815 struct lpfc_sli_ring *pring, uint32_t mask)
2816 {
2817 struct lpfc_pgp *pgp;
2818 IOCB_t *entry;
2819 IOCB_t *irsp = NULL;
2820 struct lpfc_iocbq *rspiocbp = NULL;
2821 uint32_t portRspPut, portRspMax;
2822 unsigned long iflag;
2823 uint32_t status;
2824
2825 pgp = &phba->port_gp[pring->ringno];
2826 spin_lock_irqsave(&phba->hbalock, iflag);
2827 pring->stats.iocb_event++;
2828
2829 /*
2830 * The next available response entry should never exceed the maximum
2831 * entries. If it does, treat it as an adapter hardware error.
2832 */
2833 portRspMax = pring->numRiocb;
2834 portRspPut = le32_to_cpu(pgp->rspPutInx);
2835 if (portRspPut >= portRspMax) {
2836 /*
2837 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2838 * rsp ring <portRspMax>
2839 */
2840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2841 "0303 Ring %d handler: portRspPut %d "
2842 "is bigger than rsp ring %d\n",
2843 pring->ringno, portRspPut, portRspMax);
2844
2845 phba->link_state = LPFC_HBA_ERROR;
2846 spin_unlock_irqrestore(&phba->hbalock, iflag);
2847
2848 phba->work_hs = HS_FFER3;
2849 lpfc_handle_eratt(phba);
2850
2851 return;
2852 }
2853
2854 rmb();
2855 while (pring->rspidx != portRspPut) {
2856 /*
2857 * Build a completion list and call the appropriate handler.
2858 * The process is to get the next available response iocb, get
2859 * a free iocb from the list, copy the response data into the
2860 * free iocb, insert to the continuation list, and update the
2861 * next response index to slim. This process makes response
2862 * iocb's in the ring available to DMA as fast as possible but
2863 * pays a penalty for a copy operation. Since the iocb is
2864 * only 32 bytes, this penalty is considered small relative to
2865 * the PCI reads for register values and a slim write. When
2866 * the ulpLe field is set, the entire Command has been
2867 * received.
2868 */
2869 entry = lpfc_resp_iocb(phba, pring);
2870
2871 phba->last_completion_time = jiffies;
2872 rspiocbp = __lpfc_sli_get_iocbq(phba);
2873 if (rspiocbp == NULL) {
2874 printk(KERN_ERR "%s: out of buffers! Failing "
2875 "completion.\n", __func__);
2876 break;
2877 }
2878
2879 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2880 phba->iocb_rsp_size);
2881 irsp = &rspiocbp->iocb;
2882
2883 if (++pring->rspidx >= portRspMax)
2884 pring->rspidx = 0;
2885
2886 if (pring->ringno == LPFC_ELS_RING) {
2887 lpfc_debugfs_slow_ring_trc(phba,
2888 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2889 *(((uint32_t *) irsp) + 4),
2890 *(((uint32_t *) irsp) + 6),
2891 *(((uint32_t *) irsp) + 7));
2892 }
2893
2894 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2895
2896 spin_unlock_irqrestore(&phba->hbalock, iflag);
2897 /* Handle the response IOCB */
2898 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2899 spin_lock_irqsave(&phba->hbalock, iflag);
2900
2901 /*
2902 * If the port response put pointer has not been updated, sync
2903 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2904 * response put pointer.
2905 */
2906 if (pring->rspidx == portRspPut) {
2907 portRspPut = le32_to_cpu(pgp->rspPutInx);
2908 }
2909 } /* while (pring->rspidx != portRspPut) */
2910
2911 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2912 /* At least one response entry has been freed */
2913 pring->stats.iocb_rsp_full++;
2914 /* SET RxRE_RSP in Chip Att register */
2915 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2916 writel(status, phba->CAregaddr);
2917 readl(phba->CAregaddr); /* flush */
2918 }
2919 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2920 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2921 pring->stats.iocb_cmd_empty++;
2922
2923 /* Force update of the local copy of cmdGetInx */
2924 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2925 lpfc_sli_resume_iocb(phba, pring);
2926
2927 if ((pring->lpfc_sli_cmd_available))
2928 (pring->lpfc_sli_cmd_available) (phba, pring);
2929
2930 }
2931
2932 spin_unlock_irqrestore(&phba->hbalock, iflag);
2933 return;
2934 }
2935
2936 /**
2937 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2938 * @phba: Pointer to HBA context object.
2939 * @pring: Pointer to driver SLI ring object.
2940 * @mask: Host attention register mask for this ring.
2941 *
2942 * This function is called from the worker thread when there is a pending
2943 * ELS response iocb on the driver internal slow-path response iocb worker
2944 * queue. The caller does not hold any lock. The function will remove each
2945 * response iocb from the response worker queue and calls the handle
2946 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2947 **/
2948 static void
2949 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2950 struct lpfc_sli_ring *pring, uint32_t mask)
2951 {
2952 struct lpfc_iocbq *irspiocbq;
2953 struct hbq_dmabuf *dmabuf;
2954 struct lpfc_cq_event *cq_event;
2955 unsigned long iflag;
2956
2957 spin_lock_irqsave(&phba->hbalock, iflag);
2958 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2959 spin_unlock_irqrestore(&phba->hbalock, iflag);
2960 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2961 /* Get the response iocb from the head of work queue */
2962 spin_lock_irqsave(&phba->hbalock, iflag);
2963 list_remove_head(&phba->sli4_hba.sp_queue_event,
2964 cq_event, struct lpfc_cq_event, list);
2965 spin_unlock_irqrestore(&phba->hbalock, iflag);
2966
2967 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2968 case CQE_CODE_COMPL_WQE:
2969 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2970 cq_event);
2971 /* Translate ELS WCQE to response IOCBQ */
2972 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2973 irspiocbq);
2974 if (irspiocbq)
2975 lpfc_sli_sp_handle_rspiocb(phba, pring,
2976 irspiocbq);
2977 break;
2978 case CQE_CODE_RECEIVE:
2979 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2980 cq_event);
2981 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2982 break;
2983 default:
2984 break;
2985 }
2986 }
2987 }
2988
2989 /**
2990 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2991 * @phba: Pointer to HBA context object.
2992 * @pring: Pointer to driver SLI ring object.
2993 *
2994 * This function aborts all iocbs in the given ring and frees all the iocb
2995 * objects in txq. This function issues an abort iocb for all the iocb commands
2996 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2997 * the return of this function. The caller is not required to hold any locks.
2998 **/
2999 void
3000 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3001 {
3002 LIST_HEAD(completions);
3003 struct lpfc_iocbq *iocb, *next_iocb;
3004
3005 if (pring->ringno == LPFC_ELS_RING) {
3006 lpfc_fabric_abort_hba(phba);
3007 }
3008
3009 /* Error everything on txq and txcmplq
3010 * First do the txq.
3011 */
3012 spin_lock_irq(&phba->hbalock);
3013 list_splice_init(&pring->txq, &completions);
3014 pring->txq_cnt = 0;
3015
3016 /* Next issue ABTS for everything on the txcmplq */
3017 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3018 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3019
3020 spin_unlock_irq(&phba->hbalock);
3021
3022 /* Cancel all the IOCBs from the completions list */
3023 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3024 IOERR_SLI_ABORTED);
3025 }
3026
3027 /**
3028 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3029 * @phba: Pointer to HBA context object.
3030 *
3031 * This function flushes all iocbs in the fcp ring and frees all the iocb
3032 * objects in txq and txcmplq. This function will not issue abort iocbs
3033 * for all the iocb commands in txcmplq, they will just be returned with
3034 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3035 * slot has been permanently disabled.
3036 **/
3037 void
3038 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3039 {
3040 LIST_HEAD(txq);
3041 LIST_HEAD(txcmplq);
3042 struct lpfc_sli *psli = &phba->sli;
3043 struct lpfc_sli_ring *pring;
3044
3045 /* Currently, only one fcp ring */
3046 pring = &psli->ring[psli->fcp_ring];
3047
3048 spin_lock_irq(&phba->hbalock);
3049 /* Retrieve everything on txq */
3050 list_splice_init(&pring->txq, &txq);
3051 pring->txq_cnt = 0;
3052
3053 /* Retrieve everything on the txcmplq */
3054 list_splice_init(&pring->txcmplq, &txcmplq);
3055 pring->txcmplq_cnt = 0;
3056 spin_unlock_irq(&phba->hbalock);
3057
3058 /* Flush the txq */
3059 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3060 IOERR_SLI_DOWN);
3061
3062 /* Flush the txcmpq */
3063 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3064 IOERR_SLI_DOWN);
3065 }
3066
3067 /**
3068 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3069 * @phba: Pointer to HBA context object.
3070 * @mask: Bit mask to be checked.
3071 *
3072 * This function reads the host status register and compares
3073 * with the provided bit mask to check if HBA completed
3074 * the restart. This function will wait in a loop for the
3075 * HBA to complete restart. If the HBA does not restart within
3076 * 15 iterations, the function will reset the HBA again. The
3077 * function returns 1 when HBA fail to restart otherwise returns
3078 * zero.
3079 **/
3080 static int
3081 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3082 {
3083 uint32_t status;
3084 int i = 0;
3085 int retval = 0;
3086
3087 /* Read the HBA Host Status Register */
3088 status = readl(phba->HSregaddr);
3089
3090 /*
3091 * Check status register every 100ms for 5 retries, then every
3092 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3093 * every 2.5 sec for 4.
3094 * Break our of the loop if errors occurred during init.
3095 */
3096 while (((status & mask) != mask) &&
3097 !(status & HS_FFERM) &&
3098 i++ < 20) {
3099
3100 if (i <= 5)
3101 msleep(10);
3102 else if (i <= 10)
3103 msleep(500);
3104 else
3105 msleep(2500);
3106
3107 if (i == 15) {
3108 /* Do post */
3109 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3110 lpfc_sli_brdrestart(phba);
3111 }
3112 /* Read the HBA Host Status Register */
3113 status = readl(phba->HSregaddr);
3114 }
3115
3116 /* Check to see if any errors occurred during init */
3117 if ((status & HS_FFERM) || (i >= 20)) {
3118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3119 "2751 Adapter failed to restart, "
3120 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3121 status,
3122 readl(phba->MBslimaddr + 0xa8),
3123 readl(phba->MBslimaddr + 0xac));
3124 phba->link_state = LPFC_HBA_ERROR;
3125 retval = 1;
3126 }
3127
3128 return retval;
3129 }
3130
3131 /**
3132 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3133 * @phba: Pointer to HBA context object.
3134 * @mask: Bit mask to be checked.
3135 *
3136 * This function checks the host status register to check if HBA is
3137 * ready. This function will wait in a loop for the HBA to be ready
3138 * If the HBA is not ready , the function will will reset the HBA PCI
3139 * function again. The function returns 1 when HBA fail to be ready
3140 * otherwise returns zero.
3141 **/
3142 static int
3143 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3144 {
3145 uint32_t status;
3146 int retval = 0;
3147
3148 /* Read the HBA Host Status Register */
3149 status = lpfc_sli4_post_status_check(phba);
3150
3151 if (status) {
3152 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3153 lpfc_sli_brdrestart(phba);
3154 status = lpfc_sli4_post_status_check(phba);
3155 }
3156
3157 /* Check to see if any errors occurred during init */
3158 if (status) {
3159 phba->link_state = LPFC_HBA_ERROR;
3160 retval = 1;
3161 } else
3162 phba->sli4_hba.intr_enable = 0;
3163
3164 return retval;
3165 }
3166
3167 /**
3168 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3169 * @phba: Pointer to HBA context object.
3170 * @mask: Bit mask to be checked.
3171 *
3172 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3173 * from the API jump table function pointer from the lpfc_hba struct.
3174 **/
3175 int
3176 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3177 {
3178 return phba->lpfc_sli_brdready(phba, mask);
3179 }
3180
3181 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3182
3183 /**
3184 * lpfc_reset_barrier - Make HBA ready for HBA reset
3185 * @phba: Pointer to HBA context object.
3186 *
3187 * This function is called before resetting an HBA. This
3188 * function requests HBA to quiesce DMAs before a reset.
3189 **/
3190 void lpfc_reset_barrier(struct lpfc_hba *phba)
3191 {
3192 uint32_t __iomem *resp_buf;
3193 uint32_t __iomem *mbox_buf;
3194 volatile uint32_t mbox;
3195 uint32_t hc_copy;
3196 int i;
3197 uint8_t hdrtype;
3198
3199 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3200 if (hdrtype != 0x80 ||
3201 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3202 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3203 return;
3204
3205 /*
3206 * Tell the other part of the chip to suspend temporarily all
3207 * its DMA activity.
3208 */
3209 resp_buf = phba->MBslimaddr;
3210
3211 /* Disable the error attention */
3212 hc_copy = readl(phba->HCregaddr);
3213 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3214 readl(phba->HCregaddr); /* flush */
3215 phba->link_flag |= LS_IGNORE_ERATT;
3216
3217 if (readl(phba->HAregaddr) & HA_ERATT) {
3218 /* Clear Chip error bit */
3219 writel(HA_ERATT, phba->HAregaddr);
3220 phba->pport->stopped = 1;
3221 }
3222
3223 mbox = 0;
3224 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3225 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3226
3227 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3228 mbox_buf = phba->MBslimaddr;
3229 writel(mbox, mbox_buf);
3230
3231 for (i = 0;
3232 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3233 mdelay(1);
3234
3235 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3236 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3237 phba->pport->stopped)
3238 goto restore_hc;
3239 else
3240 goto clear_errat;
3241 }
3242
3243 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3244 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3245 mdelay(1);
3246
3247 clear_errat:
3248
3249 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3250 mdelay(1);
3251
3252 if (readl(phba->HAregaddr) & HA_ERATT) {
3253 writel(HA_ERATT, phba->HAregaddr);
3254 phba->pport->stopped = 1;
3255 }
3256
3257 restore_hc:
3258 phba->link_flag &= ~LS_IGNORE_ERATT;
3259 writel(hc_copy, phba->HCregaddr);
3260 readl(phba->HCregaddr); /* flush */
3261 }
3262
3263 /**
3264 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3265 * @phba: Pointer to HBA context object.
3266 *
3267 * This function issues a kill_board mailbox command and waits for
3268 * the error attention interrupt. This function is called for stopping
3269 * the firmware processing. The caller is not required to hold any
3270 * locks. This function calls lpfc_hba_down_post function to free
3271 * any pending commands after the kill. The function will return 1 when it
3272 * fails to kill the board else will return 0.
3273 **/
3274 int
3275 lpfc_sli_brdkill(struct lpfc_hba *phba)
3276 {
3277 struct lpfc_sli *psli;
3278 LPFC_MBOXQ_t *pmb;
3279 uint32_t status;
3280 uint32_t ha_copy;
3281 int retval;
3282 int i = 0;
3283
3284 psli = &phba->sli;
3285
3286 /* Kill HBA */
3287 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3288 "0329 Kill HBA Data: x%x x%x\n",
3289 phba->pport->port_state, psli->sli_flag);
3290
3291 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3292 if (!pmb)
3293 return 1;
3294
3295 /* Disable the error attention */
3296 spin_lock_irq(&phba->hbalock);
3297 status = readl(phba->HCregaddr);
3298 status &= ~HC_ERINT_ENA;
3299 writel(status, phba->HCregaddr);
3300 readl(phba->HCregaddr); /* flush */
3301 phba->link_flag |= LS_IGNORE_ERATT;
3302 spin_unlock_irq(&phba->hbalock);
3303
3304 lpfc_kill_board(phba, pmb);
3305 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3306 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3307
3308 if (retval != MBX_SUCCESS) {
3309 if (retval != MBX_BUSY)
3310 mempool_free(pmb, phba->mbox_mem_pool);
3311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3312 "2752 KILL_BOARD command failed retval %d\n",
3313 retval);
3314 spin_lock_irq(&phba->hbalock);
3315 phba->link_flag &= ~LS_IGNORE_ERATT;
3316 spin_unlock_irq(&phba->hbalock);
3317 return 1;
3318 }
3319
3320 spin_lock_irq(&phba->hbalock);
3321 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3322 spin_unlock_irq(&phba->hbalock);
3323
3324 mempool_free(pmb, phba->mbox_mem_pool);
3325
3326 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3327 * attention every 100ms for 3 seconds. If we don't get ERATT after
3328 * 3 seconds we still set HBA_ERROR state because the status of the
3329 * board is now undefined.
3330 */
3331 ha_copy = readl(phba->HAregaddr);
3332
3333 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3334 mdelay(100);
3335 ha_copy = readl(phba->HAregaddr);
3336 }
3337
3338 del_timer_sync(&psli->mbox_tmo);
3339 if (ha_copy & HA_ERATT) {
3340 writel(HA_ERATT, phba->HAregaddr);
3341 phba->pport->stopped = 1;
3342 }
3343 spin_lock_irq(&phba->hbalock);
3344 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3345 psli->mbox_active = NULL;
3346 phba->link_flag &= ~LS_IGNORE_ERATT;
3347 spin_unlock_irq(&phba->hbalock);
3348
3349 lpfc_hba_down_post(phba);
3350 phba->link_state = LPFC_HBA_ERROR;
3351
3352 return ha_copy & HA_ERATT ? 0 : 1;
3353 }
3354
3355 /**
3356 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3357 * @phba: Pointer to HBA context object.
3358 *
3359 * This function resets the HBA by writing HC_INITFF to the control
3360 * register. After the HBA resets, this function resets all the iocb ring
3361 * indices. This function disables PCI layer parity checking during
3362 * the reset.
3363 * This function returns 0 always.
3364 * The caller is not required to hold any locks.
3365 **/
3366 int
3367 lpfc_sli_brdreset(struct lpfc_hba *phba)
3368 {
3369 struct lpfc_sli *psli;
3370 struct lpfc_sli_ring *pring;
3371 uint16_t cfg_value;
3372 int i;
3373
3374 psli = &phba->sli;
3375
3376 /* Reset HBA */
3377 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3378 "0325 Reset HBA Data: x%x x%x\n",
3379 phba->pport->port_state, psli->sli_flag);
3380
3381 /* perform board reset */
3382 phba->fc_eventTag = 0;
3383 phba->link_events = 0;
3384 phba->pport->fc_myDID = 0;
3385 phba->pport->fc_prevDID = 0;
3386
3387 /* Turn off parity checking and serr during the physical reset */
3388 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3389 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3390 (cfg_value &
3391 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3392
3393 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3394
3395 /* Now toggle INITFF bit in the Host Control Register */
3396 writel(HC_INITFF, phba->HCregaddr);
3397 mdelay(1);
3398 readl(phba->HCregaddr); /* flush */
3399 writel(0, phba->HCregaddr);
3400 readl(phba->HCregaddr); /* flush */
3401
3402 /* Restore PCI cmd register */
3403 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3404
3405 /* Initialize relevant SLI info */
3406 for (i = 0; i < psli->num_rings; i++) {
3407 pring = &psli->ring[i];
3408 pring->flag = 0;
3409 pring->rspidx = 0;
3410 pring->next_cmdidx = 0;
3411 pring->local_getidx = 0;
3412 pring->cmdidx = 0;
3413 pring->missbufcnt = 0;
3414 }
3415
3416 phba->link_state = LPFC_WARM_START;
3417 return 0;
3418 }
3419
3420 /**
3421 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3422 * @phba: Pointer to HBA context object.
3423 *
3424 * This function resets a SLI4 HBA. This function disables PCI layer parity
3425 * checking during resets the device. The caller is not required to hold
3426 * any locks.
3427 *
3428 * This function returns 0 always.
3429 **/
3430 int
3431 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3432 {
3433 struct lpfc_sli *psli = &phba->sli;
3434 uint16_t cfg_value;
3435 uint8_t qindx;
3436
3437 /* Reset HBA */
3438 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3439 "0295 Reset HBA Data: x%x x%x\n",
3440 phba->pport->port_state, psli->sli_flag);
3441
3442 /* perform board reset */
3443 phba->fc_eventTag = 0;
3444 phba->link_events = 0;
3445 phba->pport->fc_myDID = 0;
3446 phba->pport->fc_prevDID = 0;
3447
3448 /* Turn off parity checking and serr during the physical reset */
3449 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3450 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3451 (cfg_value &
3452 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3453
3454 spin_lock_irq(&phba->hbalock);
3455 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3456 phba->fcf.fcf_flag = 0;
3457 /* Clean up the child queue list for the CQs */
3458 list_del_init(&phba->sli4_hba.mbx_wq->list);
3459 list_del_init(&phba->sli4_hba.els_wq->list);
3460 list_del_init(&phba->sli4_hba.hdr_rq->list);
3461 list_del_init(&phba->sli4_hba.dat_rq->list);
3462 list_del_init(&phba->sli4_hba.mbx_cq->list);
3463 list_del_init(&phba->sli4_hba.els_cq->list);
3464 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3465 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3466 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3467 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3468 spin_unlock_irq(&phba->hbalock);
3469
3470 /* Now physically reset the device */
3471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3472 "0389 Performing PCI function reset!\n");
3473 /* Perform FCoE PCI function reset */
3474 lpfc_pci_function_reset(phba);
3475
3476 return 0;
3477 }
3478
3479 /**
3480 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3481 * @phba: Pointer to HBA context object.
3482 *
3483 * This function is called in the SLI initialization code path to
3484 * restart the HBA. The caller is not required to hold any lock.
3485 * This function writes MBX_RESTART mailbox command to the SLIM and
3486 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3487 * function to free any pending commands. The function enables
3488 * POST only during the first initialization. The function returns zero.
3489 * The function does not guarantee completion of MBX_RESTART mailbox
3490 * command before the return of this function.
3491 **/
3492 static int
3493 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3494 {
3495 MAILBOX_t *mb;
3496 struct lpfc_sli *psli;
3497 volatile uint32_t word0;
3498 void __iomem *to_slim;
3499 uint32_t hba_aer_enabled;
3500
3501 spin_lock_irq(&phba->hbalock);
3502
3503 /* Take PCIe device Advanced Error Reporting (AER) state */
3504 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3505
3506 psli = &phba->sli;
3507
3508 /* Restart HBA */
3509 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3510 "0337 Restart HBA Data: x%x x%x\n",
3511 phba->pport->port_state, psli->sli_flag);
3512
3513 word0 = 0;
3514 mb = (MAILBOX_t *) &word0;
3515 mb->mbxCommand = MBX_RESTART;
3516 mb->mbxHc = 1;
3517
3518 lpfc_reset_barrier(phba);
3519
3520 to_slim = phba->MBslimaddr;
3521 writel(*(uint32_t *) mb, to_slim);
3522 readl(to_slim); /* flush */
3523
3524 /* Only skip post after fc_ffinit is completed */
3525 if (phba->pport->port_state)
3526 word0 = 1; /* This is really setting up word1 */
3527 else
3528 word0 = 0; /* This is really setting up word1 */
3529 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3530 writel(*(uint32_t *) mb, to_slim);
3531 readl(to_slim); /* flush */
3532
3533 lpfc_sli_brdreset(phba);
3534 phba->pport->stopped = 0;
3535 phba->link_state = LPFC_INIT_START;
3536 phba->hba_flag = 0;
3537 spin_unlock_irq(&phba->hbalock);
3538
3539 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3540 psli->stats_start = get_seconds();
3541
3542 /* Give the INITFF and Post time to settle. */
3543 mdelay(100);
3544
3545 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3546 if (hba_aer_enabled)
3547 pci_disable_pcie_error_reporting(phba->pcidev);
3548
3549 lpfc_hba_down_post(phba);
3550
3551 return 0;
3552 }
3553
3554 /**
3555 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3556 * @phba: Pointer to HBA context object.
3557 *
3558 * This function is called in the SLI initialization code path to restart
3559 * a SLI4 HBA. The caller is not required to hold any lock.
3560 * At the end of the function, it calls lpfc_hba_down_post function to
3561 * free any pending commands.
3562 **/
3563 static int
3564 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3565 {
3566 struct lpfc_sli *psli = &phba->sli;
3567
3568
3569 /* Restart HBA */
3570 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3571 "0296 Restart HBA Data: x%x x%x\n",
3572 phba->pport->port_state, psli->sli_flag);
3573
3574 lpfc_sli4_brdreset(phba);
3575
3576 spin_lock_irq(&phba->hbalock);
3577 phba->pport->stopped = 0;
3578 phba->link_state = LPFC_INIT_START;
3579 phba->hba_flag = 0;
3580 spin_unlock_irq(&phba->hbalock);
3581
3582 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3583 psli->stats_start = get_seconds();
3584
3585 lpfc_hba_down_post(phba);
3586
3587 return 0;
3588 }
3589
3590 /**
3591 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3592 * @phba: Pointer to HBA context object.
3593 *
3594 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3595 * API jump table function pointer from the lpfc_hba struct.
3596 **/
3597 int
3598 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3599 {
3600 return phba->lpfc_sli_brdrestart(phba);
3601 }
3602
3603 /**
3604 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3605 * @phba: Pointer to HBA context object.
3606 *
3607 * This function is called after a HBA restart to wait for successful
3608 * restart of the HBA. Successful restart of the HBA is indicated by
3609 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3610 * iteration, the function will restart the HBA again. The function returns
3611 * zero if HBA successfully restarted else returns negative error code.
3612 **/
3613 static int
3614 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3615 {
3616 uint32_t status, i = 0;
3617
3618 /* Read the HBA Host Status Register */
3619 status = readl(phba->HSregaddr);
3620
3621 /* Check status register to see what current state is */
3622 i = 0;
3623 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3624
3625 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3626 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3627 * 4.
3628 */
3629 if (i++ >= 20) {
3630 /* Adapter failed to init, timeout, status reg
3631 <status> */
3632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3633 "0436 Adapter failed to init, "
3634 "timeout, status reg x%x, "
3635 "FW Data: A8 x%x AC x%x\n", status,
3636 readl(phba->MBslimaddr + 0xa8),
3637 readl(phba->MBslimaddr + 0xac));
3638 phba->link_state = LPFC_HBA_ERROR;
3639 return -ETIMEDOUT;
3640 }
3641
3642 /* Check to see if any errors occurred during init */
3643 if (status & HS_FFERM) {
3644 /* ERROR: During chipset initialization */
3645 /* Adapter failed to init, chipset, status reg
3646 <status> */
3647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3648 "0437 Adapter failed to init, "
3649 "chipset, status reg x%x, "
3650 "FW Data: A8 x%x AC x%x\n", status,
3651 readl(phba->MBslimaddr + 0xa8),
3652 readl(phba->MBslimaddr + 0xac));
3653 phba->link_state = LPFC_HBA_ERROR;
3654 return -EIO;
3655 }
3656
3657 if (i <= 5) {
3658 msleep(10);
3659 } else if (i <= 10) {
3660 msleep(500);
3661 } else {
3662 msleep(2500);
3663 }
3664
3665 if (i == 15) {
3666 /* Do post */
3667 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3668 lpfc_sli_brdrestart(phba);
3669 }
3670 /* Read the HBA Host Status Register */
3671 status = readl(phba->HSregaddr);
3672 }
3673
3674 /* Check to see if any errors occurred during init */
3675 if (status & HS_FFERM) {
3676 /* ERROR: During chipset initialization */
3677 /* Adapter failed to init, chipset, status reg <status> */
3678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3679 "0438 Adapter failed to init, chipset, "
3680 "status reg x%x, "
3681 "FW Data: A8 x%x AC x%x\n", status,
3682 readl(phba->MBslimaddr + 0xa8),
3683 readl(phba->MBslimaddr + 0xac));
3684 phba->link_state = LPFC_HBA_ERROR;
3685 return -EIO;
3686 }
3687
3688 /* Clear all interrupt enable conditions */
3689 writel(0, phba->HCregaddr);
3690 readl(phba->HCregaddr); /* flush */
3691
3692 /* setup host attn register */
3693 writel(0xffffffff, phba->HAregaddr);
3694 readl(phba->HAregaddr); /* flush */
3695 return 0;
3696 }
3697
3698 /**
3699 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3700 *
3701 * This function calculates and returns the number of HBQs required to be
3702 * configured.
3703 **/
3704 int
3705 lpfc_sli_hbq_count(void)
3706 {
3707 return ARRAY_SIZE(lpfc_hbq_defs);
3708 }
3709
3710 /**
3711 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3712 *
3713 * This function adds the number of hbq entries in every HBQ to get
3714 * the total number of hbq entries required for the HBA and returns
3715 * the total count.
3716 **/
3717 static int
3718 lpfc_sli_hbq_entry_count(void)
3719 {
3720 int hbq_count = lpfc_sli_hbq_count();
3721 int count = 0;
3722 int i;
3723
3724 for (i = 0; i < hbq_count; ++i)
3725 count += lpfc_hbq_defs[i]->entry_count;
3726 return count;
3727 }
3728
3729 /**
3730 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3731 *
3732 * This function calculates amount of memory required for all hbq entries
3733 * to be configured and returns the total memory required.
3734 **/
3735 int
3736 lpfc_sli_hbq_size(void)
3737 {
3738 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3739 }
3740
3741 /**
3742 * lpfc_sli_hbq_setup - configure and initialize HBQs
3743 * @phba: Pointer to HBA context object.
3744 *
3745 * This function is called during the SLI initialization to configure
3746 * all the HBQs and post buffers to the HBQ. The caller is not
3747 * required to hold any locks. This function will return zero if successful
3748 * else it will return negative error code.
3749 **/
3750 static int
3751 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3752 {
3753 int hbq_count = lpfc_sli_hbq_count();
3754 LPFC_MBOXQ_t *pmb;
3755 MAILBOX_t *pmbox;
3756 uint32_t hbqno;
3757 uint32_t hbq_entry_index;
3758
3759 /* Get a Mailbox buffer to setup mailbox
3760 * commands for HBA initialization
3761 */
3762 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3763
3764 if (!pmb)
3765 return -ENOMEM;
3766
3767 pmbox = &pmb->u.mb;
3768
3769 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3770 phba->link_state = LPFC_INIT_MBX_CMDS;
3771 phba->hbq_in_use = 1;
3772
3773 hbq_entry_index = 0;
3774 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3775 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3776 phba->hbqs[hbqno].hbqPutIdx = 0;
3777 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3778 phba->hbqs[hbqno].entry_count =
3779 lpfc_hbq_defs[hbqno]->entry_count;
3780 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3781 hbq_entry_index, pmb);
3782 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3783
3784 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3785 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3786 mbxStatus <status>, ring <num> */
3787
3788 lpfc_printf_log(phba, KERN_ERR,
3789 LOG_SLI | LOG_VPORT,
3790 "1805 Adapter failed to init. "
3791 "Data: x%x x%x x%x\n",
3792 pmbox->mbxCommand,
3793 pmbox->mbxStatus, hbqno);
3794
3795 phba->link_state = LPFC_HBA_ERROR;
3796 mempool_free(pmb, phba->mbox_mem_pool);
3797 return -ENXIO;
3798 }
3799 }
3800 phba->hbq_count = hbq_count;
3801
3802 mempool_free(pmb, phba->mbox_mem_pool);
3803
3804 /* Initially populate or replenish the HBQs */
3805 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3806 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3807 return 0;
3808 }
3809
3810 /**
3811 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3812 * @phba: Pointer to HBA context object.
3813 *
3814 * This function is called during the SLI initialization to configure
3815 * all the HBQs and post buffers to the HBQ. The caller is not
3816 * required to hold any locks. This function will return zero if successful
3817 * else it will return negative error code.
3818 **/
3819 static int
3820 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3821 {
3822 phba->hbq_in_use = 1;
3823 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3824 phba->hbq_count = 1;
3825 /* Initially populate or replenish the HBQs */
3826 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3827 return 0;
3828 }
3829
3830 /**
3831 * lpfc_sli_config_port - Issue config port mailbox command
3832 * @phba: Pointer to HBA context object.
3833 * @sli_mode: sli mode - 2/3
3834 *
3835 * This function is called by the sli intialization code path
3836 * to issue config_port mailbox command. This function restarts the
3837 * HBA firmware and issues a config_port mailbox command to configure
3838 * the SLI interface in the sli mode specified by sli_mode
3839 * variable. The caller is not required to hold any locks.
3840 * The function returns 0 if successful, else returns negative error
3841 * code.
3842 **/
3843 int
3844 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3845 {
3846 LPFC_MBOXQ_t *pmb;
3847 uint32_t resetcount = 0, rc = 0, done = 0;
3848
3849 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3850 if (!pmb) {
3851 phba->link_state = LPFC_HBA_ERROR;
3852 return -ENOMEM;
3853 }
3854
3855 phba->sli_rev = sli_mode;
3856 while (resetcount < 2 && !done) {
3857 spin_lock_irq(&phba->hbalock);
3858 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3859 spin_unlock_irq(&phba->hbalock);
3860 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3861 lpfc_sli_brdrestart(phba);
3862 rc = lpfc_sli_chipset_init(phba);
3863 if (rc)
3864 break;
3865
3866 spin_lock_irq(&phba->hbalock);
3867 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3868 spin_unlock_irq(&phba->hbalock);
3869 resetcount++;
3870
3871 /* Call pre CONFIG_PORT mailbox command initialization. A
3872 * value of 0 means the call was successful. Any other
3873 * nonzero value is a failure, but if ERESTART is returned,
3874 * the driver may reset the HBA and try again.
3875 */
3876 rc = lpfc_config_port_prep(phba);
3877 if (rc == -ERESTART) {
3878 phba->link_state = LPFC_LINK_UNKNOWN;
3879 continue;
3880 } else if (rc)
3881 break;
3882 phba->link_state = LPFC_INIT_MBX_CMDS;
3883 lpfc_config_port(phba, pmb);
3884 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3885 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3886 LPFC_SLI3_HBQ_ENABLED |
3887 LPFC_SLI3_CRP_ENABLED |
3888 LPFC_SLI3_BG_ENABLED);
3889 if (rc != MBX_SUCCESS) {
3890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3891 "0442 Adapter failed to init, mbxCmd x%x "
3892 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3893 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3894 spin_lock_irq(&phba->hbalock);
3895 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3896 spin_unlock_irq(&phba->hbalock);
3897 rc = -ENXIO;
3898 } else {
3899 /* Allow asynchronous mailbox command to go through */
3900 spin_lock_irq(&phba->hbalock);
3901 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3902 spin_unlock_irq(&phba->hbalock);
3903 done = 1;
3904 }
3905 }
3906 if (!done) {
3907 rc = -EINVAL;
3908 goto do_prep_failed;
3909 }
3910 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3911 if (!pmb->u.mb.un.varCfgPort.cMA) {
3912 rc = -ENXIO;
3913 goto do_prep_failed;
3914 }
3915 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3916 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3917 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3918 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3919 phba->max_vpi : phba->max_vports;
3920
3921 } else
3922 phba->max_vpi = 0;
3923 if (pmb->u.mb.un.varCfgPort.gdss)
3924 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3925 if (pmb->u.mb.un.varCfgPort.gerbm)
3926 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3927 if (pmb->u.mb.un.varCfgPort.gcrp)
3928 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3929
3930 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3931 phba->port_gp = phba->mbox->us.s3_pgp.port;
3932
3933 if (phba->cfg_enable_bg) {
3934 if (pmb->u.mb.un.varCfgPort.gbg)
3935 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3936 else
3937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3938 "0443 Adapter did not grant "
3939 "BlockGuard\n");
3940 }
3941 } else {
3942 phba->hbq_get = NULL;
3943 phba->port_gp = phba->mbox->us.s2.port;
3944 phba->max_vpi = 0;
3945 }
3946 do_prep_failed:
3947 mempool_free(pmb, phba->mbox_mem_pool);
3948 return rc;
3949 }
3950
3951
3952 /**
3953 * lpfc_sli_hba_setup - SLI intialization function
3954 * @phba: Pointer to HBA context object.
3955 *
3956 * This function is the main SLI intialization function. This function
3957 * is called by the HBA intialization code, HBA reset code and HBA
3958 * error attention handler code. Caller is not required to hold any
3959 * locks. This function issues config_port mailbox command to configure
3960 * the SLI, setup iocb rings and HBQ rings. In the end the function
3961 * calls the config_port_post function to issue init_link mailbox
3962 * command and to start the discovery. The function will return zero
3963 * if successful, else it will return negative error code.
3964 **/
3965 int
3966 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3967 {
3968 uint32_t rc;
3969 int mode = 3;
3970
3971 switch (lpfc_sli_mode) {
3972 case 2:
3973 if (phba->cfg_enable_npiv) {
3974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3975 "1824 NPIV enabled: Override lpfc_sli_mode "
3976 "parameter (%d) to auto (0).\n",
3977 lpfc_sli_mode);
3978 break;
3979 }
3980 mode = 2;
3981 break;
3982 case 0:
3983 case 3:
3984 break;
3985 default:
3986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3987 "1819 Unrecognized lpfc_sli_mode "
3988 "parameter: %d.\n", lpfc_sli_mode);
3989
3990 break;
3991 }
3992
3993 rc = lpfc_sli_config_port(phba, mode);
3994
3995 if (rc && lpfc_sli_mode == 3)
3996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3997 "1820 Unable to select SLI-3. "
3998 "Not supported by adapter.\n");
3999 if (rc && mode != 2)
4000 rc = lpfc_sli_config_port(phba, 2);
4001 if (rc)
4002 goto lpfc_sli_hba_setup_error;
4003
4004 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4005 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4006 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4007 if (!rc) {
4008 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4009 "2709 This device supports "
4010 "Advanced Error Reporting (AER)\n");
4011 spin_lock_irq(&phba->hbalock);
4012 phba->hba_flag |= HBA_AER_ENABLED;
4013 spin_unlock_irq(&phba->hbalock);
4014 } else {
4015 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4016 "2708 This device does not support "
4017 "Advanced Error Reporting (AER)\n");
4018 phba->cfg_aer_support = 0;
4019 }
4020 }
4021
4022 if (phba->sli_rev == 3) {
4023 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4024 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4025 } else {
4026 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4027 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4028 phba->sli3_options = 0;
4029 }
4030
4031 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4032 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4033 phba->sli_rev, phba->max_vpi);
4034 rc = lpfc_sli_ring_map(phba);
4035
4036 if (rc)
4037 goto lpfc_sli_hba_setup_error;
4038
4039 /* Init HBQs */
4040 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4041 rc = lpfc_sli_hbq_setup(phba);
4042 if (rc)
4043 goto lpfc_sli_hba_setup_error;
4044 }
4045 spin_lock_irq(&phba->hbalock);
4046 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4047 spin_unlock_irq(&phba->hbalock);
4048
4049 rc = lpfc_config_port_post(phba);
4050 if (rc)
4051 goto lpfc_sli_hba_setup_error;
4052
4053 return rc;
4054
4055 lpfc_sli_hba_setup_error:
4056 phba->link_state = LPFC_HBA_ERROR;
4057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4058 "0445 Firmware initialization failed\n");
4059 return rc;
4060 }
4061
4062 /**
4063 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4064 * @phba: Pointer to HBA context object.
4065 * @mboxq: mailbox pointer.
4066 * This function issue a dump mailbox command to read config region
4067 * 23 and parse the records in the region and populate driver
4068 * data structure.
4069 **/
4070 static int
4071 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4072 LPFC_MBOXQ_t *mboxq)
4073 {
4074 struct lpfc_dmabuf *mp;
4075 struct lpfc_mqe *mqe;
4076 uint32_t data_length;
4077 int rc;
4078
4079 /* Program the default value of vlan_id and fc_map */
4080 phba->valid_vlan = 0;
4081 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4082 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4083 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4084
4085 mqe = &mboxq->u.mqe;
4086 if (lpfc_dump_fcoe_param(phba, mboxq))
4087 return -ENOMEM;
4088
4089 mp = (struct lpfc_dmabuf *) mboxq->context1;
4090 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4091
4092 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4093 "(%d):2571 Mailbox cmd x%x Status x%x "
4094 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4095 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4096 "CQ: x%x x%x x%x x%x\n",
4097 mboxq->vport ? mboxq->vport->vpi : 0,
4098 bf_get(lpfc_mqe_command, mqe),
4099 bf_get(lpfc_mqe_status, mqe),
4100 mqe->un.mb_words[0], mqe->un.mb_words[1],
4101 mqe->un.mb_words[2], mqe->un.mb_words[3],
4102 mqe->un.mb_words[4], mqe->un.mb_words[5],
4103 mqe->un.mb_words[6], mqe->un.mb_words[7],
4104 mqe->un.mb_words[8], mqe->un.mb_words[9],
4105 mqe->un.mb_words[10], mqe->un.mb_words[11],
4106 mqe->un.mb_words[12], mqe->un.mb_words[13],
4107 mqe->un.mb_words[14], mqe->un.mb_words[15],
4108 mqe->un.mb_words[16], mqe->un.mb_words[50],
4109 mboxq->mcqe.word0,
4110 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4111 mboxq->mcqe.trailer);
4112
4113 if (rc) {
4114 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4115 kfree(mp);
4116 return -EIO;
4117 }
4118 data_length = mqe->un.mb_words[5];
4119 if (data_length > DMP_RGN23_SIZE) {
4120 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4121 kfree(mp);
4122 return -EIO;
4123 }
4124
4125 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4126 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4127 kfree(mp);
4128 return 0;
4129 }
4130
4131 /**
4132 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4133 * @phba: pointer to lpfc hba data structure.
4134 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4135 * @vpd: pointer to the memory to hold resulting port vpd data.
4136 * @vpd_size: On input, the number of bytes allocated to @vpd.
4137 * On output, the number of data bytes in @vpd.
4138 *
4139 * This routine executes a READ_REV SLI4 mailbox command. In
4140 * addition, this routine gets the port vpd data.
4141 *
4142 * Return codes
4143 * 0 - successful
4144 * ENOMEM - could not allocated memory.
4145 **/
4146 static int
4147 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4148 uint8_t *vpd, uint32_t *vpd_size)
4149 {
4150 int rc = 0;
4151 uint32_t dma_size;
4152 struct lpfc_dmabuf *dmabuf;
4153 struct lpfc_mqe *mqe;
4154
4155 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4156 if (!dmabuf)
4157 return -ENOMEM;
4158
4159 /*
4160 * Get a DMA buffer for the vpd data resulting from the READ_REV
4161 * mailbox command.
4162 */
4163 dma_size = *vpd_size;
4164 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4165 dma_size,
4166 &dmabuf->phys,
4167 GFP_KERNEL);
4168 if (!dmabuf->virt) {
4169 kfree(dmabuf);
4170 return -ENOMEM;
4171 }
4172 memset(dmabuf->virt, 0, dma_size);
4173
4174 /*
4175 * The SLI4 implementation of READ_REV conflicts at word1,
4176 * bits 31:16 and SLI4 adds vpd functionality not present
4177 * in SLI3. This code corrects the conflicts.
4178 */
4179 lpfc_read_rev(phba, mboxq);
4180 mqe = &mboxq->u.mqe;
4181 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4182 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4183 mqe->un.read_rev.word1 &= 0x0000FFFF;
4184 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4185 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4186
4187 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4188 if (rc) {
4189 dma_free_coherent(&phba->pcidev->dev, dma_size,
4190 dmabuf->virt, dmabuf->phys);
4191 kfree(dmabuf);
4192 return -EIO;
4193 }
4194
4195 /*
4196 * The available vpd length cannot be bigger than the
4197 * DMA buffer passed to the port. Catch the less than
4198 * case and update the caller's size.
4199 */
4200 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4201 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4202
4203 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4204 dma_free_coherent(&phba->pcidev->dev, dma_size,
4205 dmabuf->virt, dmabuf->phys);
4206 kfree(dmabuf);
4207 return 0;
4208 }
4209
4210 /**
4211 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4212 * @phba: pointer to lpfc hba data structure.
4213 *
4214 * This routine is called to explicitly arm the SLI4 device's completion and
4215 * event queues
4216 **/
4217 static void
4218 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4219 {
4220 uint8_t fcp_eqidx;
4221
4222 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4223 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4224 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4225 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4226 LPFC_QUEUE_REARM);
4227 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4228 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4229 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4230 LPFC_QUEUE_REARM);
4231 }
4232
4233 /**
4234 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4235 * @phba: Pointer to HBA context object.
4236 *
4237 * This function is the main SLI4 device intialization PCI function. This
4238 * function is called by the HBA intialization code, HBA reset code and
4239 * HBA error attention handler code. Caller is not required to hold any
4240 * locks.
4241 **/
4242 int
4243 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4244 {
4245 int rc;
4246 LPFC_MBOXQ_t *mboxq;
4247 struct lpfc_mqe *mqe;
4248 uint8_t *vpd;
4249 uint32_t vpd_size;
4250 uint32_t ftr_rsp = 0;
4251 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4252 struct lpfc_vport *vport = phba->pport;
4253 struct lpfc_dmabuf *mp;
4254
4255 /* Perform a PCI function reset to start from clean */
4256 rc = lpfc_pci_function_reset(phba);
4257 if (unlikely(rc))
4258 return -ENODEV;
4259
4260 /* Check the HBA Host Status Register for readyness */
4261 rc = lpfc_sli4_post_status_check(phba);
4262 if (unlikely(rc))
4263 return -ENODEV;
4264 else {
4265 spin_lock_irq(&phba->hbalock);
4266 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4267 spin_unlock_irq(&phba->hbalock);
4268 }
4269
4270 /*
4271 * Allocate a single mailbox container for initializing the
4272 * port.
4273 */
4274 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4275 if (!mboxq)
4276 return -ENOMEM;
4277
4278 /*
4279 * Continue initialization with default values even if driver failed
4280 * to read FCoE param config regions
4281 */
4282 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4283 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4284 "2570 Failed to read FCoE parameters\n");
4285
4286 /* Issue READ_REV to collect vpd and FW information. */
4287 vpd_size = SLI4_PAGE_SIZE;
4288 vpd = kzalloc(vpd_size, GFP_KERNEL);
4289 if (!vpd) {
4290 rc = -ENOMEM;
4291 goto out_free_mbox;
4292 }
4293
4294 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4295 if (unlikely(rc))
4296 goto out_free_vpd;
4297
4298 mqe = &mboxq->u.mqe;
4299 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4300 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4301 phba->hba_flag |= HBA_FCOE_SUPPORT;
4302
4303 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4304 LPFC_DCBX_CEE_MODE)
4305 phba->hba_flag |= HBA_FIP_SUPPORT;
4306 else
4307 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4308
4309 if (phba->sli_rev != LPFC_SLI_REV4 ||
4310 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4311 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4312 "0376 READ_REV Error. SLI Level %d "
4313 "FCoE enabled %d\n",
4314 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4315 rc = -EIO;
4316 goto out_free_vpd;
4317 }
4318 /*
4319 * Evaluate the read rev and vpd data. Populate the driver
4320 * state with the results. If this routine fails, the failure
4321 * is not fatal as the driver will use generic values.
4322 */
4323 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4324 if (unlikely(!rc)) {
4325 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4326 "0377 Error %d parsing vpd. "
4327 "Using defaults.\n", rc);
4328 rc = 0;
4329 }
4330
4331 /* Save information as VPD data */
4332 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4333 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4334 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4335 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4336 &mqe->un.read_rev);
4337 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4338 &mqe->un.read_rev);
4339 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4340 &mqe->un.read_rev);
4341 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4342 &mqe->un.read_rev);
4343 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4344 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4345 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4346 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4347 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4348 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4349 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4350 "(%d):0380 READ_REV Status x%x "
4351 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4352 mboxq->vport ? mboxq->vport->vpi : 0,
4353 bf_get(lpfc_mqe_status, mqe),
4354 phba->vpd.rev.opFwName,
4355 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4356 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4357
4358 /*
4359 * Discover the port's supported feature set and match it against the
4360 * hosts requests.
4361 */
4362 lpfc_request_features(phba, mboxq);
4363 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4364 if (unlikely(rc)) {
4365 rc = -EIO;
4366 goto out_free_vpd;
4367 }
4368
4369 /*
4370 * The port must support FCP initiator mode as this is the
4371 * only mode running in the host.
4372 */
4373 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4374 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4375 "0378 No support for fcpi mode.\n");
4376 ftr_rsp++;
4377 }
4378
4379 /*
4380 * If the port cannot support the host's requested features
4381 * then turn off the global config parameters to disable the
4382 * feature in the driver. This is not a fatal error.
4383 */
4384 if ((phba->cfg_enable_bg) &&
4385 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4386 ftr_rsp++;
4387
4388 if (phba->max_vpi && phba->cfg_enable_npiv &&
4389 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4390 ftr_rsp++;
4391
4392 if (ftr_rsp) {
4393 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4394 "0379 Feature Mismatch Data: x%08x %08x "
4395 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4396 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4397 phba->cfg_enable_npiv, phba->max_vpi);
4398 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4399 phba->cfg_enable_bg = 0;
4400 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4401 phba->cfg_enable_npiv = 0;
4402 }
4403
4404 /* These SLI3 features are assumed in SLI4 */
4405 spin_lock_irq(&phba->hbalock);
4406 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4407 spin_unlock_irq(&phba->hbalock);
4408
4409 /* Read the port's service parameters. */
4410 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4411 if (rc) {
4412 phba->link_state = LPFC_HBA_ERROR;
4413 rc = -ENOMEM;
4414 goto out_free_vpd;
4415 }
4416
4417 mboxq->vport = vport;
4418 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4419 mp = (struct lpfc_dmabuf *) mboxq->context1;
4420 if (rc == MBX_SUCCESS) {
4421 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4422 rc = 0;
4423 }
4424
4425 /*
4426 * This memory was allocated by the lpfc_read_sparam routine. Release
4427 * it to the mbuf pool.
4428 */
4429 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4430 kfree(mp);
4431 mboxq->context1 = NULL;
4432 if (unlikely(rc)) {
4433 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4434 "0382 READ_SPARAM command failed "
4435 "status %d, mbxStatus x%x\n",
4436 rc, bf_get(lpfc_mqe_status, mqe));
4437 phba->link_state = LPFC_HBA_ERROR;
4438 rc = -EIO;
4439 goto out_free_vpd;
4440 }
4441
4442 if (phba->cfg_soft_wwnn)
4443 u64_to_wwn(phba->cfg_soft_wwnn,
4444 vport->fc_sparam.nodeName.u.wwn);
4445 if (phba->cfg_soft_wwpn)
4446 u64_to_wwn(phba->cfg_soft_wwpn,
4447 vport->fc_sparam.portName.u.wwn);
4448 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4449 sizeof(struct lpfc_name));
4450 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4451 sizeof(struct lpfc_name));
4452
4453 /* Update the fc_host data structures with new wwn. */
4454 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4455 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4456
4457 /* Register SGL pool to the device using non-embedded mailbox command */
4458 rc = lpfc_sli4_post_sgl_list(phba);
4459 if (unlikely(rc)) {
4460 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4461 "0582 Error %d during sgl post operation\n",
4462 rc);
4463 rc = -ENODEV;
4464 goto out_free_vpd;
4465 }
4466
4467 /* Register SCSI SGL pool to the device */
4468 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4469 if (unlikely(rc)) {
4470 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4471 "0383 Error %d during scsi sgl post "
4472 "operation\n", rc);
4473 /* Some Scsi buffers were moved to the abort scsi list */
4474 /* A pci function reset will repost them */
4475 rc = -ENODEV;
4476 goto out_free_vpd;
4477 }
4478
4479 /* Post the rpi header region to the device. */
4480 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4481 if (unlikely(rc)) {
4482 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4483 "0393 Error %d during rpi post operation\n",
4484 rc);
4485 rc = -ENODEV;
4486 goto out_free_vpd;
4487 }
4488
4489 /* Set up all the queues to the device */
4490 rc = lpfc_sli4_queue_setup(phba);
4491 if (unlikely(rc)) {
4492 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4493 "0381 Error %d during queue setup.\n ", rc);
4494 goto out_stop_timers;
4495 }
4496
4497 /* Arm the CQs and then EQs on device */
4498 lpfc_sli4_arm_cqeq_intr(phba);
4499
4500 /* Indicate device interrupt mode */
4501 phba->sli4_hba.intr_enable = 1;
4502
4503 /* Allow asynchronous mailbox command to go through */
4504 spin_lock_irq(&phba->hbalock);
4505 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4506 spin_unlock_irq(&phba->hbalock);
4507
4508 /* Post receive buffers to the device */
4509 lpfc_sli4_rb_setup(phba);
4510
4511 /* Reset HBA FCF states after HBA reset */
4512 phba->fcf.fcf_flag = 0;
4513 phba->fcf.current_rec.flag = 0;
4514
4515 /* Start the ELS watchdog timer */
4516 mod_timer(&vport->els_tmofunc,
4517 jiffies + HZ * (phba->fc_ratov * 2));
4518
4519 /* Start heart beat timer */
4520 mod_timer(&phba->hb_tmofunc,
4521 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4522 phba->hb_outstanding = 0;
4523 phba->last_completion_time = jiffies;
4524
4525 /* Start error attention (ERATT) polling timer */
4526 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4527
4528 /*
4529 * The port is ready, set the host's link state to LINK_DOWN
4530 * in preparation for link interrupts.
4531 */
4532 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4533 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4534 lpfc_set_loopback_flag(phba);
4535 /* Change driver state to LPFC_LINK_DOWN right before init link */
4536 spin_lock_irq(&phba->hbalock);
4537 phba->link_state = LPFC_LINK_DOWN;
4538 spin_unlock_irq(&phba->hbalock);
4539 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4540 if (unlikely(rc != MBX_NOT_FINISHED)) {
4541 kfree(vpd);
4542 return 0;
4543 } else
4544 rc = -EIO;
4545
4546 /* Unset all the queues set up in this routine when error out */
4547 if (rc)
4548 lpfc_sli4_queue_unset(phba);
4549
4550 out_stop_timers:
4551 if (rc)
4552 lpfc_stop_hba_timers(phba);
4553 out_free_vpd:
4554 kfree(vpd);
4555 out_free_mbox:
4556 mempool_free(mboxq, phba->mbox_mem_pool);
4557 return rc;
4558 }
4559
4560 /**
4561 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4562 * @ptr: context object - pointer to hba structure.
4563 *
4564 * This is the callback function for mailbox timer. The mailbox
4565 * timer is armed when a new mailbox command is issued and the timer
4566 * is deleted when the mailbox complete. The function is called by
4567 * the kernel timer code when a mailbox does not complete within
4568 * expected time. This function wakes up the worker thread to
4569 * process the mailbox timeout and returns. All the processing is
4570 * done by the worker thread function lpfc_mbox_timeout_handler.
4571 **/
4572 void
4573 lpfc_mbox_timeout(unsigned long ptr)
4574 {
4575 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4576 unsigned long iflag;
4577 uint32_t tmo_posted;
4578
4579 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4580 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4581 if (!tmo_posted)
4582 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4583 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4584
4585 if (!tmo_posted)
4586 lpfc_worker_wake_up(phba);
4587 return;
4588 }
4589
4590
4591 /**
4592 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4593 * @phba: Pointer to HBA context object.
4594 *
4595 * This function is called from worker thread when a mailbox command times out.
4596 * The caller is not required to hold any locks. This function will reset the
4597 * HBA and recover all the pending commands.
4598 **/
4599 void
4600 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4601 {
4602 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4603 MAILBOX_t *mb = &pmbox->u.mb;
4604 struct lpfc_sli *psli = &phba->sli;
4605 struct lpfc_sli_ring *pring;
4606
4607 /* Check the pmbox pointer first. There is a race condition
4608 * between the mbox timeout handler getting executed in the
4609 * worklist and the mailbox actually completing. When this
4610 * race condition occurs, the mbox_active will be NULL.
4611 */
4612 spin_lock_irq(&phba->hbalock);
4613 if (pmbox == NULL) {
4614 lpfc_printf_log(phba, KERN_WARNING,
4615 LOG_MBOX | LOG_SLI,
4616 "0353 Active Mailbox cleared - mailbox timeout "
4617 "exiting\n");
4618 spin_unlock_irq(&phba->hbalock);
4619 return;
4620 }
4621
4622 /* Mbox cmd <mbxCommand> timeout */
4623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4624 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4625 mb->mbxCommand,
4626 phba->pport->port_state,
4627 phba->sli.sli_flag,
4628 phba->sli.mbox_active);
4629 spin_unlock_irq(&phba->hbalock);
4630
4631 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4632 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4633 * it to fail all oustanding SCSI IO.
4634 */
4635 spin_lock_irq(&phba->pport->work_port_lock);
4636 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4637 spin_unlock_irq(&phba->pport->work_port_lock);
4638 spin_lock_irq(&phba->hbalock);
4639 phba->link_state = LPFC_LINK_UNKNOWN;
4640 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4641 spin_unlock_irq(&phba->hbalock);
4642
4643 pring = &psli->ring[psli->fcp_ring];
4644 lpfc_sli_abort_iocb_ring(phba, pring);
4645
4646 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4647 "0345 Resetting board due to mailbox timeout\n");
4648
4649 /* Reset the HBA device */
4650 lpfc_reset_hba(phba);
4651 }
4652
4653 /**
4654 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4655 * @phba: Pointer to HBA context object.
4656 * @pmbox: Pointer to mailbox object.
4657 * @flag: Flag indicating how the mailbox need to be processed.
4658 *
4659 * This function is called by discovery code and HBA management code
4660 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4661 * function gets the hbalock to protect the data structures.
4662 * The mailbox command can be submitted in polling mode, in which case
4663 * this function will wait in a polling loop for the completion of the
4664 * mailbox.
4665 * If the mailbox is submitted in no_wait mode (not polling) the
4666 * function will submit the command and returns immediately without waiting
4667 * for the mailbox completion. The no_wait is supported only when HBA
4668 * is in SLI2/SLI3 mode - interrupts are enabled.
4669 * The SLI interface allows only one mailbox pending at a time. If the
4670 * mailbox is issued in polling mode and there is already a mailbox
4671 * pending, then the function will return an error. If the mailbox is issued
4672 * in NO_WAIT mode and there is a mailbox pending already, the function
4673 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4674 * The sli layer owns the mailbox object until the completion of mailbox
4675 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4676 * return codes the caller owns the mailbox command after the return of
4677 * the function.
4678 **/
4679 static int
4680 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4681 uint32_t flag)
4682 {
4683 MAILBOX_t *mb;
4684 struct lpfc_sli *psli = &phba->sli;
4685 uint32_t status, evtctr;
4686 uint32_t ha_copy;
4687 int i;
4688 unsigned long timeout;
4689 unsigned long drvr_flag = 0;
4690 uint32_t word0, ldata;
4691 void __iomem *to_slim;
4692 int processing_queue = 0;
4693
4694 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4695 if (!pmbox) {
4696 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4697 /* processing mbox queue from intr_handler */
4698 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4700 return MBX_SUCCESS;
4701 }
4702 processing_queue = 1;
4703 pmbox = lpfc_mbox_get(phba);
4704 if (!pmbox) {
4705 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4706 return MBX_SUCCESS;
4707 }
4708 }
4709
4710 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4711 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4712 if(!pmbox->vport) {
4713 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4714 lpfc_printf_log(phba, KERN_ERR,
4715 LOG_MBOX | LOG_VPORT,
4716 "1806 Mbox x%x failed. No vport\n",
4717 pmbox->u.mb.mbxCommand);
4718 dump_stack();
4719 goto out_not_finished;
4720 }
4721 }
4722
4723 /* If the PCI channel is in offline state, do not post mbox. */
4724 if (unlikely(pci_channel_offline(phba->pcidev))) {
4725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4726 goto out_not_finished;
4727 }
4728
4729 /* If HBA has a deferred error attention, fail the iocb. */
4730 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4732 goto out_not_finished;
4733 }
4734
4735 psli = &phba->sli;
4736
4737 mb = &pmbox->u.mb;
4738 status = MBX_SUCCESS;
4739
4740 if (phba->link_state == LPFC_HBA_ERROR) {
4741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4742
4743 /* Mbox command <mbxCommand> cannot issue */
4744 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4745 "(%d):0311 Mailbox command x%x cannot "
4746 "issue Data: x%x x%x\n",
4747 pmbox->vport ? pmbox->vport->vpi : 0,
4748 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4749 goto out_not_finished;
4750 }
4751
4752 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4753 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4754 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4756 "(%d):2528 Mailbox command x%x cannot "
4757 "issue Data: x%x x%x\n",
4758 pmbox->vport ? pmbox->vport->vpi : 0,
4759 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4760 goto out_not_finished;
4761 }
4762
4763 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4764 /* Polling for a mbox command when another one is already active
4765 * is not allowed in SLI. Also, the driver must have established
4766 * SLI2 mode to queue and process multiple mbox commands.
4767 */
4768
4769 if (flag & MBX_POLL) {
4770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4771
4772 /* Mbox command <mbxCommand> cannot issue */
4773 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4774 "(%d):2529 Mailbox command x%x "
4775 "cannot issue Data: x%x x%x\n",
4776 pmbox->vport ? pmbox->vport->vpi : 0,
4777 pmbox->u.mb.mbxCommand,
4778 psli->sli_flag, flag);
4779 goto out_not_finished;
4780 }
4781
4782 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4783 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4784 /* Mbox command <mbxCommand> cannot issue */
4785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4786 "(%d):2530 Mailbox command x%x "
4787 "cannot issue Data: x%x x%x\n",
4788 pmbox->vport ? pmbox->vport->vpi : 0,
4789 pmbox->u.mb.mbxCommand,
4790 psli->sli_flag, flag);
4791 goto out_not_finished;
4792 }
4793
4794 /* Another mailbox command is still being processed, queue this
4795 * command to be processed later.
4796 */
4797 lpfc_mbox_put(phba, pmbox);
4798
4799 /* Mbox cmd issue - BUSY */
4800 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4801 "(%d):0308 Mbox cmd issue - BUSY Data: "
4802 "x%x x%x x%x x%x\n",
4803 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4804 mb->mbxCommand, phba->pport->port_state,
4805 psli->sli_flag, flag);
4806
4807 psli->slistat.mbox_busy++;
4808 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4809
4810 if (pmbox->vport) {
4811 lpfc_debugfs_disc_trc(pmbox->vport,
4812 LPFC_DISC_TRC_MBOX_VPORT,
4813 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4814 (uint32_t)mb->mbxCommand,
4815 mb->un.varWords[0], mb->un.varWords[1]);
4816 }
4817 else {
4818 lpfc_debugfs_disc_trc(phba->pport,
4819 LPFC_DISC_TRC_MBOX,
4820 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4821 (uint32_t)mb->mbxCommand,
4822 mb->un.varWords[0], mb->un.varWords[1]);
4823 }
4824
4825 return MBX_BUSY;
4826 }
4827
4828 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4829
4830 /* If we are not polling, we MUST be in SLI2 mode */
4831 if (flag != MBX_POLL) {
4832 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4833 (mb->mbxCommand != MBX_KILL_BOARD)) {
4834 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4835 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4836 /* Mbox command <mbxCommand> cannot issue */
4837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4838 "(%d):2531 Mailbox command x%x "
4839 "cannot issue Data: x%x x%x\n",
4840 pmbox->vport ? pmbox->vport->vpi : 0,
4841 pmbox->u.mb.mbxCommand,
4842 psli->sli_flag, flag);
4843 goto out_not_finished;
4844 }
4845 /* timeout active mbox command */
4846 mod_timer(&psli->mbox_tmo, (jiffies +
4847 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4848 }
4849
4850 /* Mailbox cmd <cmd> issue */
4851 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4852 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4853 "x%x\n",
4854 pmbox->vport ? pmbox->vport->vpi : 0,
4855 mb->mbxCommand, phba->pport->port_state,
4856 psli->sli_flag, flag);
4857
4858 if (mb->mbxCommand != MBX_HEARTBEAT) {
4859 if (pmbox->vport) {
4860 lpfc_debugfs_disc_trc(pmbox->vport,
4861 LPFC_DISC_TRC_MBOX_VPORT,
4862 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4863 (uint32_t)mb->mbxCommand,
4864 mb->un.varWords[0], mb->un.varWords[1]);
4865 }
4866 else {
4867 lpfc_debugfs_disc_trc(phba->pport,
4868 LPFC_DISC_TRC_MBOX,
4869 "MBOX Send: cmd:x%x mb:x%x x%x",
4870 (uint32_t)mb->mbxCommand,
4871 mb->un.varWords[0], mb->un.varWords[1]);
4872 }
4873 }
4874
4875 psli->slistat.mbox_cmd++;
4876 evtctr = psli->slistat.mbox_event;
4877
4878 /* next set own bit for the adapter and copy over command word */
4879 mb->mbxOwner = OWN_CHIP;
4880
4881 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4882 /* Populate mbox extension offset word. */
4883 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4884 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4885 = (uint8_t *)phba->mbox_ext
4886 - (uint8_t *)phba->mbox;
4887 }
4888
4889 /* Copy the mailbox extension data */
4890 if (pmbox->in_ext_byte_len && pmbox->context2) {
4891 lpfc_sli_pcimem_bcopy(pmbox->context2,
4892 (uint8_t *)phba->mbox_ext,
4893 pmbox->in_ext_byte_len);
4894 }
4895 /* Copy command data to host SLIM area */
4896 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4897 } else {
4898 /* Populate mbox extension offset word. */
4899 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4900 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4901 = MAILBOX_HBA_EXT_OFFSET;
4902
4903 /* Copy the mailbox extension data */
4904 if (pmbox->in_ext_byte_len && pmbox->context2) {
4905 lpfc_memcpy_to_slim(phba->MBslimaddr +
4906 MAILBOX_HBA_EXT_OFFSET,
4907 pmbox->context2, pmbox->in_ext_byte_len);
4908
4909 }
4910 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4911 /* copy command data into host mbox for cmpl */
4912 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4913 }
4914
4915 /* First copy mbox command data to HBA SLIM, skip past first
4916 word */
4917 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4918 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4919 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4920
4921 /* Next copy over first word, with mbxOwner set */
4922 ldata = *((uint32_t *)mb);
4923 to_slim = phba->MBslimaddr;
4924 writel(ldata, to_slim);
4925 readl(to_slim); /* flush */
4926
4927 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4928 /* switch over to host mailbox */
4929 psli->sli_flag |= LPFC_SLI_ACTIVE;
4930 }
4931 }
4932
4933 wmb();
4934
4935 switch (flag) {
4936 case MBX_NOWAIT:
4937 /* Set up reference to mailbox command */
4938 psli->mbox_active = pmbox;
4939 /* Interrupt board to do it */
4940 writel(CA_MBATT, phba->CAregaddr);
4941 readl(phba->CAregaddr); /* flush */
4942 /* Don't wait for it to finish, just return */
4943 break;
4944
4945 case MBX_POLL:
4946 /* Set up null reference to mailbox command */
4947 psli->mbox_active = NULL;
4948 /* Interrupt board to do it */
4949 writel(CA_MBATT, phba->CAregaddr);
4950 readl(phba->CAregaddr); /* flush */
4951
4952 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4953 /* First read mbox status word */
4954 word0 = *((uint32_t *)phba->mbox);
4955 word0 = le32_to_cpu(word0);
4956 } else {
4957 /* First read mbox status word */
4958 word0 = readl(phba->MBslimaddr);
4959 }
4960
4961 /* Read the HBA Host Attention Register */
4962 ha_copy = readl(phba->HAregaddr);
4963 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4964 mb->mbxCommand) *
4965 1000) + jiffies;
4966 i = 0;
4967 /* Wait for command to complete */
4968 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4969 (!(ha_copy & HA_MBATT) &&
4970 (phba->link_state > LPFC_WARM_START))) {
4971 if (time_after(jiffies, timeout)) {
4972 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4973 spin_unlock_irqrestore(&phba->hbalock,
4974 drvr_flag);
4975 goto out_not_finished;
4976 }
4977
4978 /* Check if we took a mbox interrupt while we were
4979 polling */
4980 if (((word0 & OWN_CHIP) != OWN_CHIP)
4981 && (evtctr != psli->slistat.mbox_event))
4982 break;
4983
4984 if (i++ > 10) {
4985 spin_unlock_irqrestore(&phba->hbalock,
4986 drvr_flag);
4987 msleep(1);
4988 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4989 }
4990
4991 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4992 /* First copy command data */
4993 word0 = *((uint32_t *)phba->mbox);
4994 word0 = le32_to_cpu(word0);
4995 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4996 MAILBOX_t *slimmb;
4997 uint32_t slimword0;
4998 /* Check real SLIM for any errors */
4999 slimword0 = readl(phba->MBslimaddr);
5000 slimmb = (MAILBOX_t *) & slimword0;
5001 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5002 && slimmb->mbxStatus) {
5003 psli->sli_flag &=
5004 ~LPFC_SLI_ACTIVE;
5005 word0 = slimword0;
5006 }
5007 }
5008 } else {
5009 /* First copy command data */
5010 word0 = readl(phba->MBslimaddr);
5011 }
5012 /* Read the HBA Host Attention Register */
5013 ha_copy = readl(phba->HAregaddr);
5014 }
5015
5016 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5017 /* copy results back to user */
5018 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5019 /* Copy the mailbox extension data */
5020 if (pmbox->out_ext_byte_len && pmbox->context2) {
5021 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5022 pmbox->context2,
5023 pmbox->out_ext_byte_len);
5024 }
5025 } else {
5026 /* First copy command data */
5027 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5028 MAILBOX_CMD_SIZE);
5029 /* Copy the mailbox extension data */
5030 if (pmbox->out_ext_byte_len && pmbox->context2) {
5031 lpfc_memcpy_from_slim(pmbox->context2,
5032 phba->MBslimaddr +
5033 MAILBOX_HBA_EXT_OFFSET,
5034 pmbox->out_ext_byte_len);
5035 }
5036 }
5037
5038 writel(HA_MBATT, phba->HAregaddr);
5039 readl(phba->HAregaddr); /* flush */
5040
5041 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5042 status = mb->mbxStatus;
5043 }
5044
5045 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5046 return status;
5047
5048 out_not_finished:
5049 if (processing_queue) {
5050 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5051 lpfc_mbox_cmpl_put(phba, pmbox);
5052 }
5053 return MBX_NOT_FINISHED;
5054 }
5055
5056 /**
5057 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5058 * @phba: Pointer to HBA context object.
5059 *
5060 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5061 * the driver internal pending mailbox queue. It will then try to wait out the
5062 * possible outstanding mailbox command before return.
5063 *
5064 * Returns:
5065 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5066 * the outstanding mailbox command timed out.
5067 **/
5068 static int
5069 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5070 {
5071 struct lpfc_sli *psli = &phba->sli;
5072 uint8_t actcmd = MBX_HEARTBEAT;
5073 int rc = 0;
5074 unsigned long timeout;
5075
5076 /* Mark the asynchronous mailbox command posting as blocked */
5077 spin_lock_irq(&phba->hbalock);
5078 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5079 if (phba->sli.mbox_active)
5080 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5081 spin_unlock_irq(&phba->hbalock);
5082 /* Determine how long we might wait for the active mailbox
5083 * command to be gracefully completed by firmware.
5084 */
5085 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5086 jiffies;
5087 /* Wait for the outstnading mailbox command to complete */
5088 while (phba->sli.mbox_active) {
5089 /* Check active mailbox complete status every 2ms */
5090 msleep(2);
5091 if (time_after(jiffies, timeout)) {
5092 /* Timeout, marked the outstanding cmd not complete */
5093 rc = 1;
5094 break;
5095 }
5096 }
5097
5098 /* Can not cleanly block async mailbox command, fails it */
5099 if (rc) {
5100 spin_lock_irq(&phba->hbalock);
5101 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5102 spin_unlock_irq(&phba->hbalock);
5103 }
5104 return rc;
5105 }
5106
5107 /**
5108 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5109 * @phba: Pointer to HBA context object.
5110 *
5111 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5112 * commands from the driver internal pending mailbox queue. It makes sure
5113 * that there is no outstanding mailbox command before resuming posting
5114 * asynchronous mailbox commands. If, for any reason, there is outstanding
5115 * mailbox command, it will try to wait it out before resuming asynchronous
5116 * mailbox command posting.
5117 **/
5118 static void
5119 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5120 {
5121 struct lpfc_sli *psli = &phba->sli;
5122
5123 spin_lock_irq(&phba->hbalock);
5124 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5125 /* Asynchronous mailbox posting is not blocked, do nothing */
5126 spin_unlock_irq(&phba->hbalock);
5127 return;
5128 }
5129
5130 /* Outstanding synchronous mailbox command is guaranteed to be done,
5131 * successful or timeout, after timing-out the outstanding mailbox
5132 * command shall always be removed, so just unblock posting async
5133 * mailbox command and resume
5134 */
5135 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5136 spin_unlock_irq(&phba->hbalock);
5137
5138 /* wake up worker thread to post asynchronlous mailbox command */
5139 lpfc_worker_wake_up(phba);
5140 }
5141
5142 /**
5143 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5144 * @phba: Pointer to HBA context object.
5145 * @mboxq: Pointer to mailbox object.
5146 *
5147 * The function posts a mailbox to the port. The mailbox is expected
5148 * to be comletely filled in and ready for the port to operate on it.
5149 * This routine executes a synchronous completion operation on the
5150 * mailbox by polling for its completion.
5151 *
5152 * The caller must not be holding any locks when calling this routine.
5153 *
5154 * Returns:
5155 * MBX_SUCCESS - mailbox posted successfully
5156 * Any of the MBX error values.
5157 **/
5158 static int
5159 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5160 {
5161 int rc = MBX_SUCCESS;
5162 unsigned long iflag;
5163 uint32_t db_ready;
5164 uint32_t mcqe_status;
5165 uint32_t mbx_cmnd;
5166 unsigned long timeout;
5167 struct lpfc_sli *psli = &phba->sli;
5168 struct lpfc_mqe *mb = &mboxq->u.mqe;
5169 struct lpfc_bmbx_create *mbox_rgn;
5170 struct dma_address *dma_address;
5171 struct lpfc_register bmbx_reg;
5172
5173 /*
5174 * Only one mailbox can be active to the bootstrap mailbox region
5175 * at a time and there is no queueing provided.
5176 */
5177 spin_lock_irqsave(&phba->hbalock, iflag);
5178 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5179 spin_unlock_irqrestore(&phba->hbalock, iflag);
5180 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5181 "(%d):2532 Mailbox command x%x (x%x) "
5182 "cannot issue Data: x%x x%x\n",
5183 mboxq->vport ? mboxq->vport->vpi : 0,
5184 mboxq->u.mb.mbxCommand,
5185 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5186 psli->sli_flag, MBX_POLL);
5187 return MBXERR_ERROR;
5188 }
5189 /* The server grabs the token and owns it until release */
5190 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5191 phba->sli.mbox_active = mboxq;
5192 spin_unlock_irqrestore(&phba->hbalock, iflag);
5193
5194 /*
5195 * Initialize the bootstrap memory region to avoid stale data areas
5196 * in the mailbox post. Then copy the caller's mailbox contents to
5197 * the bmbx mailbox region.
5198 */
5199 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5200 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5201 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5202 sizeof(struct lpfc_mqe));
5203
5204 /* Post the high mailbox dma address to the port and wait for ready. */
5205 dma_address = &phba->sli4_hba.bmbx.dma_address;
5206 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5207
5208 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5209 * 1000) + jiffies;
5210 do {
5211 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5212 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5213 if (!db_ready)
5214 msleep(2);
5215
5216 if (time_after(jiffies, timeout)) {
5217 rc = MBXERR_ERROR;
5218 goto exit;
5219 }
5220 } while (!db_ready);
5221
5222 /* Post the low mailbox dma address to the port. */
5223 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5224 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5225 * 1000) + jiffies;
5226 do {
5227 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5228 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5229 if (!db_ready)
5230 msleep(2);
5231
5232 if (time_after(jiffies, timeout)) {
5233 rc = MBXERR_ERROR;
5234 goto exit;
5235 }
5236 } while (!db_ready);
5237
5238 /*
5239 * Read the CQ to ensure the mailbox has completed.
5240 * If so, update the mailbox status so that the upper layers
5241 * can complete the request normally.
5242 */
5243 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5244 sizeof(struct lpfc_mqe));
5245 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5246 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5247 sizeof(struct lpfc_mcqe));
5248 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5249
5250 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5251 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5252 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5253 rc = MBXERR_ERROR;
5254 }
5255
5256 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5257 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5258 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5259 " x%x x%x CQ: x%x x%x x%x x%x\n",
5260 mboxq->vport ? mboxq->vport->vpi : 0,
5261 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5262 bf_get(lpfc_mqe_status, mb),
5263 mb->un.mb_words[0], mb->un.mb_words[1],
5264 mb->un.mb_words[2], mb->un.mb_words[3],
5265 mb->un.mb_words[4], mb->un.mb_words[5],
5266 mb->un.mb_words[6], mb->un.mb_words[7],
5267 mb->un.mb_words[8], mb->un.mb_words[9],
5268 mb->un.mb_words[10], mb->un.mb_words[11],
5269 mb->un.mb_words[12], mboxq->mcqe.word0,
5270 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5271 mboxq->mcqe.trailer);
5272 exit:
5273 /* We are holding the token, no needed for lock when release */
5274 spin_lock_irqsave(&phba->hbalock, iflag);
5275 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5276 phba->sli.mbox_active = NULL;
5277 spin_unlock_irqrestore(&phba->hbalock, iflag);
5278 return rc;
5279 }
5280
5281 /**
5282 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5283 * @phba: Pointer to HBA context object.
5284 * @pmbox: Pointer to mailbox object.
5285 * @flag: Flag indicating how the mailbox need to be processed.
5286 *
5287 * This function is called by discovery code and HBA management code to submit
5288 * a mailbox command to firmware with SLI-4 interface spec.
5289 *
5290 * Return codes the caller owns the mailbox command after the return of the
5291 * function.
5292 **/
5293 static int
5294 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5295 uint32_t flag)
5296 {
5297 struct lpfc_sli *psli = &phba->sli;
5298 unsigned long iflags;
5299 int rc;
5300
5301 rc = lpfc_mbox_dev_check(phba);
5302 if (unlikely(rc)) {
5303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5304 "(%d):2544 Mailbox command x%x (x%x) "
5305 "cannot issue Data: x%x x%x\n",
5306 mboxq->vport ? mboxq->vport->vpi : 0,
5307 mboxq->u.mb.mbxCommand,
5308 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5309 psli->sli_flag, flag);
5310 goto out_not_finished;
5311 }
5312
5313 /* Detect polling mode and jump to a handler */
5314 if (!phba->sli4_hba.intr_enable) {
5315 if (flag == MBX_POLL)
5316 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5317 else
5318 rc = -EIO;
5319 if (rc != MBX_SUCCESS)
5320 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5321 "(%d):2541 Mailbox command x%x "
5322 "(x%x) cannot issue Data: x%x x%x\n",
5323 mboxq->vport ? mboxq->vport->vpi : 0,
5324 mboxq->u.mb.mbxCommand,
5325 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5326 psli->sli_flag, flag);
5327 return rc;
5328 } else if (flag == MBX_POLL) {
5329 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5330 "(%d):2542 Try to issue mailbox command "
5331 "x%x (x%x) synchronously ahead of async"
5332 "mailbox command queue: x%x x%x\n",
5333 mboxq->vport ? mboxq->vport->vpi : 0,
5334 mboxq->u.mb.mbxCommand,
5335 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5336 psli->sli_flag, flag);
5337 /* Try to block the asynchronous mailbox posting */
5338 rc = lpfc_sli4_async_mbox_block(phba);
5339 if (!rc) {
5340 /* Successfully blocked, now issue sync mbox cmd */
5341 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5342 if (rc != MBX_SUCCESS)
5343 lpfc_printf_log(phba, KERN_ERR,
5344 LOG_MBOX | LOG_SLI,
5345 "(%d):2597 Mailbox command "
5346 "x%x (x%x) cannot issue "
5347 "Data: x%x x%x\n",
5348 mboxq->vport ?
5349 mboxq->vport->vpi : 0,
5350 mboxq->u.mb.mbxCommand,
5351 lpfc_sli4_mbox_opcode_get(phba,
5352 mboxq),
5353 psli->sli_flag, flag);
5354 /* Unblock the async mailbox posting afterward */
5355 lpfc_sli4_async_mbox_unblock(phba);
5356 }
5357 return rc;
5358 }
5359
5360 /* Now, interrupt mode asynchrous mailbox command */
5361 rc = lpfc_mbox_cmd_check(phba, mboxq);
5362 if (rc) {
5363 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5364 "(%d):2543 Mailbox command x%x (x%x) "
5365 "cannot issue Data: x%x x%x\n",
5366 mboxq->vport ? mboxq->vport->vpi : 0,
5367 mboxq->u.mb.mbxCommand,
5368 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5369 psli->sli_flag, flag);
5370 goto out_not_finished;
5371 }
5372
5373 /* Put the mailbox command to the driver internal FIFO */
5374 psli->slistat.mbox_busy++;
5375 spin_lock_irqsave(&phba->hbalock, iflags);
5376 lpfc_mbox_put(phba, mboxq);
5377 spin_unlock_irqrestore(&phba->hbalock, iflags);
5378 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5379 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5380 "x%x (x%x) x%x x%x x%x\n",
5381 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5382 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5383 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5384 phba->pport->port_state,
5385 psli->sli_flag, MBX_NOWAIT);
5386 /* Wake up worker thread to transport mailbox command from head */
5387 lpfc_worker_wake_up(phba);
5388
5389 return MBX_BUSY;
5390
5391 out_not_finished:
5392 return MBX_NOT_FINISHED;
5393 }
5394
5395 /**
5396 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5397 * @phba: Pointer to HBA context object.
5398 *
5399 * This function is called by worker thread to send a mailbox command to
5400 * SLI4 HBA firmware.
5401 *
5402 **/
5403 int
5404 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5405 {
5406 struct lpfc_sli *psli = &phba->sli;
5407 LPFC_MBOXQ_t *mboxq;
5408 int rc = MBX_SUCCESS;
5409 unsigned long iflags;
5410 struct lpfc_mqe *mqe;
5411 uint32_t mbx_cmnd;
5412
5413 /* Check interrupt mode before post async mailbox command */
5414 if (unlikely(!phba->sli4_hba.intr_enable))
5415 return MBX_NOT_FINISHED;
5416
5417 /* Check for mailbox command service token */
5418 spin_lock_irqsave(&phba->hbalock, iflags);
5419 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5420 spin_unlock_irqrestore(&phba->hbalock, iflags);
5421 return MBX_NOT_FINISHED;
5422 }
5423 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5424 spin_unlock_irqrestore(&phba->hbalock, iflags);
5425 return MBX_NOT_FINISHED;
5426 }
5427 if (unlikely(phba->sli.mbox_active)) {
5428 spin_unlock_irqrestore(&phba->hbalock, iflags);
5429 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5430 "0384 There is pending active mailbox cmd\n");
5431 return MBX_NOT_FINISHED;
5432 }
5433 /* Take the mailbox command service token */
5434 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5435
5436 /* Get the next mailbox command from head of queue */
5437 mboxq = lpfc_mbox_get(phba);
5438
5439 /* If no more mailbox command waiting for post, we're done */
5440 if (!mboxq) {
5441 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5442 spin_unlock_irqrestore(&phba->hbalock, iflags);
5443 return MBX_SUCCESS;
5444 }
5445 phba->sli.mbox_active = mboxq;
5446 spin_unlock_irqrestore(&phba->hbalock, iflags);
5447
5448 /* Check device readiness for posting mailbox command */
5449 rc = lpfc_mbox_dev_check(phba);
5450 if (unlikely(rc))
5451 /* Driver clean routine will clean up pending mailbox */
5452 goto out_not_finished;
5453
5454 /* Prepare the mbox command to be posted */
5455 mqe = &mboxq->u.mqe;
5456 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5457
5458 /* Start timer for the mbox_tmo and log some mailbox post messages */
5459 mod_timer(&psli->mbox_tmo, (jiffies +
5460 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5461
5462 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5463 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5464 "x%x x%x\n",
5465 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5466 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5467 phba->pport->port_state, psli->sli_flag);
5468
5469 if (mbx_cmnd != MBX_HEARTBEAT) {
5470 if (mboxq->vport) {
5471 lpfc_debugfs_disc_trc(mboxq->vport,
5472 LPFC_DISC_TRC_MBOX_VPORT,
5473 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5474 mbx_cmnd, mqe->un.mb_words[0],
5475 mqe->un.mb_words[1]);
5476 } else {
5477 lpfc_debugfs_disc_trc(phba->pport,
5478 LPFC_DISC_TRC_MBOX,
5479 "MBOX Send: cmd:x%x mb:x%x x%x",
5480 mbx_cmnd, mqe->un.mb_words[0],
5481 mqe->un.mb_words[1]);
5482 }
5483 }
5484 psli->slistat.mbox_cmd++;
5485
5486 /* Post the mailbox command to the port */
5487 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5488 if (rc != MBX_SUCCESS) {
5489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5490 "(%d):2533 Mailbox command x%x (x%x) "
5491 "cannot issue Data: x%x x%x\n",
5492 mboxq->vport ? mboxq->vport->vpi : 0,
5493 mboxq->u.mb.mbxCommand,
5494 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5495 psli->sli_flag, MBX_NOWAIT);
5496 goto out_not_finished;
5497 }
5498
5499 return rc;
5500
5501 out_not_finished:
5502 spin_lock_irqsave(&phba->hbalock, iflags);
5503 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5504 __lpfc_mbox_cmpl_put(phba, mboxq);
5505 /* Release the token */
5506 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5507 phba->sli.mbox_active = NULL;
5508 spin_unlock_irqrestore(&phba->hbalock, iflags);
5509
5510 return MBX_NOT_FINISHED;
5511 }
5512
5513 /**
5514 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5515 * @phba: Pointer to HBA context object.
5516 * @pmbox: Pointer to mailbox object.
5517 * @flag: Flag indicating how the mailbox need to be processed.
5518 *
5519 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5520 * the API jump table function pointer from the lpfc_hba struct.
5521 *
5522 * Return codes the caller owns the mailbox command after the return of the
5523 * function.
5524 **/
5525 int
5526 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5527 {
5528 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5529 }
5530
5531 /**
5532 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5533 * @phba: The hba struct for which this call is being executed.
5534 * @dev_grp: The HBA PCI-Device group number.
5535 *
5536 * This routine sets up the mbox interface API function jump table in @phba
5537 * struct.
5538 * Returns: 0 - success, -ENODEV - failure.
5539 **/
5540 int
5541 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5542 {
5543
5544 switch (dev_grp) {
5545 case LPFC_PCI_DEV_LP:
5546 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5547 phba->lpfc_sli_handle_slow_ring_event =
5548 lpfc_sli_handle_slow_ring_event_s3;
5549 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5550 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5551 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5552 break;
5553 case LPFC_PCI_DEV_OC:
5554 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5555 phba->lpfc_sli_handle_slow_ring_event =
5556 lpfc_sli_handle_slow_ring_event_s4;
5557 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5558 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5559 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5560 break;
5561 default:
5562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5563 "1420 Invalid HBA PCI-device group: 0x%x\n",
5564 dev_grp);
5565 return -ENODEV;
5566 break;
5567 }
5568 return 0;
5569 }
5570
5571 /**
5572 * __lpfc_sli_ringtx_put - Add an iocb to the txq
5573 * @phba: Pointer to HBA context object.
5574 * @pring: Pointer to driver SLI ring object.
5575 * @piocb: Pointer to address of newly added command iocb.
5576 *
5577 * This function is called with hbalock held to add a command
5578 * iocb to the txq when SLI layer cannot submit the command iocb
5579 * to the ring.
5580 **/
5581 static void
5582 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5583 struct lpfc_iocbq *piocb)
5584 {
5585 /* Insert the caller's iocb in the txq tail for later processing. */
5586 list_add_tail(&piocb->list, &pring->txq);
5587 pring->txq_cnt++;
5588 }
5589
5590 /**
5591 * lpfc_sli_next_iocb - Get the next iocb in the txq
5592 * @phba: Pointer to HBA context object.
5593 * @pring: Pointer to driver SLI ring object.
5594 * @piocb: Pointer to address of newly added command iocb.
5595 *
5596 * This function is called with hbalock held before a new
5597 * iocb is submitted to the firmware. This function checks
5598 * txq to flush the iocbs in txq to Firmware before
5599 * submitting new iocbs to the Firmware.
5600 * If there are iocbs in the txq which need to be submitted
5601 * to firmware, lpfc_sli_next_iocb returns the first element
5602 * of the txq after dequeuing it from txq.
5603 * If there is no iocb in the txq then the function will return
5604 * *piocb and *piocb is set to NULL. Caller needs to check
5605 * *piocb to find if there are more commands in the txq.
5606 **/
5607 static struct lpfc_iocbq *
5608 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5609 struct lpfc_iocbq **piocb)
5610 {
5611 struct lpfc_iocbq * nextiocb;
5612
5613 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5614 if (!nextiocb) {
5615 nextiocb = *piocb;
5616 *piocb = NULL;
5617 }
5618
5619 return nextiocb;
5620 }
5621
5622 /**
5623 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5624 * @phba: Pointer to HBA context object.
5625 * @ring_number: SLI ring number to issue iocb on.
5626 * @piocb: Pointer to command iocb.
5627 * @flag: Flag indicating if this command can be put into txq.
5628 *
5629 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5630 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5631 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5632 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5633 * this function allows only iocbs for posting buffers. This function finds
5634 * next available slot in the command ring and posts the command to the
5635 * available slot and writes the port attention register to request HBA start
5636 * processing new iocb. If there is no slot available in the ring and
5637 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5638 * the function returns IOCB_BUSY.
5639 *
5640 * This function is called with hbalock held. The function will return success
5641 * after it successfully submit the iocb to firmware or after adding to the
5642 * txq.
5643 **/
5644 static int
5645 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5646 struct lpfc_iocbq *piocb, uint32_t flag)
5647 {
5648 struct lpfc_iocbq *nextiocb;
5649 IOCB_t *iocb;
5650 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5651
5652 if (piocb->iocb_cmpl && (!piocb->vport) &&
5653 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5654 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5655 lpfc_printf_log(phba, KERN_ERR,
5656 LOG_SLI | LOG_VPORT,
5657 "1807 IOCB x%x failed. No vport\n",
5658 piocb->iocb.ulpCommand);
5659 dump_stack();
5660 return IOCB_ERROR;
5661 }
5662
5663
5664 /* If the PCI channel is in offline state, do not post iocbs. */
5665 if (unlikely(pci_channel_offline(phba->pcidev)))
5666 return IOCB_ERROR;
5667
5668 /* If HBA has a deferred error attention, fail the iocb. */
5669 if (unlikely(phba->hba_flag & DEFER_ERATT))
5670 return IOCB_ERROR;
5671
5672 /*
5673 * We should never get an IOCB if we are in a < LINK_DOWN state
5674 */
5675 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5676 return IOCB_ERROR;
5677
5678 /*
5679 * Check to see if we are blocking IOCB processing because of a
5680 * outstanding event.
5681 */
5682 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5683 goto iocb_busy;
5684
5685 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5686 /*
5687 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5688 * can be issued if the link is not up.
5689 */
5690 switch (piocb->iocb.ulpCommand) {
5691 case CMD_GEN_REQUEST64_CR:
5692 case CMD_GEN_REQUEST64_CX:
5693 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5694 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5695 FC_RCTL_DD_UNSOL_CMD) ||
5696 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5697 MENLO_TRANSPORT_TYPE))
5698
5699 goto iocb_busy;
5700 break;
5701 case CMD_QUE_RING_BUF_CN:
5702 case CMD_QUE_RING_BUF64_CN:
5703 /*
5704 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5705 * completion, iocb_cmpl MUST be 0.
5706 */
5707 if (piocb->iocb_cmpl)
5708 piocb->iocb_cmpl = NULL;
5709 /*FALLTHROUGH*/
5710 case CMD_CREATE_XRI_CR:
5711 case CMD_CLOSE_XRI_CN:
5712 case CMD_CLOSE_XRI_CX:
5713 break;
5714 default:
5715 goto iocb_busy;
5716 }
5717
5718 /*
5719 * For FCP commands, we must be in a state where we can process link
5720 * attention events.
5721 */
5722 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5723 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5724 goto iocb_busy;
5725 }
5726
5727 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5728 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5729 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5730
5731 if (iocb)
5732 lpfc_sli_update_ring(phba, pring);
5733 else
5734 lpfc_sli_update_full_ring(phba, pring);
5735
5736 if (!piocb)
5737 return IOCB_SUCCESS;
5738
5739 goto out_busy;
5740
5741 iocb_busy:
5742 pring->stats.iocb_cmd_delay++;
5743
5744 out_busy:
5745
5746 if (!(flag & SLI_IOCB_RET_IOCB)) {
5747 __lpfc_sli_ringtx_put(phba, pring, piocb);
5748 return IOCB_SUCCESS;
5749 }
5750
5751 return IOCB_BUSY;
5752 }
5753
5754 /**
5755 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5756 * @phba: Pointer to HBA context object.
5757 * @piocb: Pointer to command iocb.
5758 * @sglq: Pointer to the scatter gather queue object.
5759 *
5760 * This routine converts the bpl or bde that is in the IOCB
5761 * to a sgl list for the sli4 hardware. The physical address
5762 * of the bpl/bde is converted back to a virtual address.
5763 * If the IOCB contains a BPL then the list of BDE's is
5764 * converted to sli4_sge's. If the IOCB contains a single
5765 * BDE then it is converted to a single sli_sge.
5766 * The IOCB is still in cpu endianess so the contents of
5767 * the bpl can be used without byte swapping.
5768 *
5769 * Returns valid XRI = Success, NO_XRI = Failure.
5770 **/
5771 static uint16_t
5772 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5773 struct lpfc_sglq *sglq)
5774 {
5775 uint16_t xritag = NO_XRI;
5776 struct ulp_bde64 *bpl = NULL;
5777 struct ulp_bde64 bde;
5778 struct sli4_sge *sgl = NULL;
5779 IOCB_t *icmd;
5780 int numBdes = 0;
5781 int i = 0;
5782
5783 if (!piocbq || !sglq)
5784 return xritag;
5785
5786 sgl = (struct sli4_sge *)sglq->sgl;
5787 icmd = &piocbq->iocb;
5788 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5789 numBdes = icmd->un.genreq64.bdl.bdeSize /
5790 sizeof(struct ulp_bde64);
5791 /* The addrHigh and addrLow fields within the IOCB
5792 * have not been byteswapped yet so there is no
5793 * need to swap them back.
5794 */
5795 bpl = (struct ulp_bde64 *)
5796 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5797
5798 if (!bpl)
5799 return xritag;
5800
5801 for (i = 0; i < numBdes; i++) {
5802 /* Should already be byte swapped. */
5803 sgl->addr_hi = bpl->addrHigh;
5804 sgl->addr_lo = bpl->addrLow;
5805
5806 if ((i+1) == numBdes)
5807 bf_set(lpfc_sli4_sge_last, sgl, 1);
5808 else
5809 bf_set(lpfc_sli4_sge_last, sgl, 0);
5810 sgl->word2 = cpu_to_le32(sgl->word2);
5811 /* swap the size field back to the cpu so we
5812 * can assign it to the sgl.
5813 */
5814 bde.tus.w = le32_to_cpu(bpl->tus.w);
5815 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5816 bpl++;
5817 sgl++;
5818 }
5819 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5820 /* The addrHigh and addrLow fields of the BDE have not
5821 * been byteswapped yet so they need to be swapped
5822 * before putting them in the sgl.
5823 */
5824 sgl->addr_hi =
5825 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5826 sgl->addr_lo =
5827 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5828 bf_set(lpfc_sli4_sge_last, sgl, 1);
5829 sgl->word2 = cpu_to_le32(sgl->word2);
5830 sgl->sge_len =
5831 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5832 }
5833 return sglq->sli4_xritag;
5834 }
5835
5836 /**
5837 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5838 * @phba: Pointer to HBA context object.
5839 *
5840 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5841 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5842 * held.
5843 *
5844 * Return: index into SLI4 fast-path FCP queue index.
5845 **/
5846 static uint32_t
5847 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5848 {
5849 ++phba->fcp_qidx;
5850 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5851 phba->fcp_qidx = 0;
5852
5853 return phba->fcp_qidx;
5854 }
5855
5856 /**
5857 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5858 * @phba: Pointer to HBA context object.
5859 * @piocb: Pointer to command iocb.
5860 * @wqe: Pointer to the work queue entry.
5861 *
5862 * This routine converts the iocb command to its Work Queue Entry
5863 * equivalent. The wqe pointer should not have any fields set when
5864 * this routine is called because it will memcpy over them.
5865 * This routine does not set the CQ_ID or the WQEC bits in the
5866 * wqe.
5867 *
5868 * Returns: 0 = Success, IOCB_ERROR = Failure.
5869 **/
5870 static int
5871 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5872 union lpfc_wqe *wqe)
5873 {
5874 uint32_t xmit_len = 0, total_len = 0;
5875 uint8_t ct = 0;
5876 uint32_t fip;
5877 uint32_t abort_tag;
5878 uint8_t command_type = ELS_COMMAND_NON_FIP;
5879 uint8_t cmnd;
5880 uint16_t xritag;
5881 struct ulp_bde64 *bpl = NULL;
5882 uint32_t els_id = ELS_ID_DEFAULT;
5883 int numBdes, i;
5884 struct ulp_bde64 bde;
5885
5886 fip = phba->hba_flag & HBA_FIP_SUPPORT;
5887 /* The fcp commands will set command type */
5888 if (iocbq->iocb_flag & LPFC_IO_FCP)
5889 command_type = FCP_COMMAND;
5890 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5891 command_type = ELS_COMMAND_FIP;
5892 else
5893 command_type = ELS_COMMAND_NON_FIP;
5894
5895 /* Some of the fields are in the right position already */
5896 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5897 abort_tag = (uint32_t) iocbq->iotag;
5898 xritag = iocbq->sli4_xritag;
5899 wqe->words[7] = 0; /* The ct field has moved so reset */
5900 /* words0-2 bpl convert bde */
5901 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5902 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5903 sizeof(struct ulp_bde64);
5904 bpl = (struct ulp_bde64 *)
5905 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5906 if (!bpl)
5907 return IOCB_ERROR;
5908
5909 /* Should already be byte swapped. */
5910 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5911 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5912 /* swap the size field back to the cpu so we
5913 * can assign it to the sgl.
5914 */
5915 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5916 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5917 total_len = 0;
5918 for (i = 0; i < numBdes; i++) {
5919 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5920 total_len += bde.tus.f.bdeSize;
5921 }
5922 } else
5923 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5924
5925 iocbq->iocb.ulpIoTag = iocbq->iotag;
5926 cmnd = iocbq->iocb.ulpCommand;
5927
5928 switch (iocbq->iocb.ulpCommand) {
5929 case CMD_ELS_REQUEST64_CR:
5930 if (!iocbq->iocb.ulpLe) {
5931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5932 "2007 Only Limited Edition cmd Format"
5933 " supported 0x%x\n",
5934 iocbq->iocb.ulpCommand);
5935 return IOCB_ERROR;
5936 }
5937 wqe->els_req.payload_len = xmit_len;
5938 /* Els_reguest64 has a TMO */
5939 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5940 iocbq->iocb.ulpTimeout);
5941 /* Need a VF for word 4 set the vf bit*/
5942 bf_set(els_req64_vf, &wqe->els_req, 0);
5943 /* And a VFID for word 12 */
5944 bf_set(els_req64_vfid, &wqe->els_req, 0);
5945 /*
5946 * Set ct field to 3, indicates that the context_tag field
5947 * contains the FCFI and remote N_Port_ID is
5948 * in word 5.
5949 */
5950
5951 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5952 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5953 iocbq->iocb.ulpContext);
5954
5955 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5956 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5957 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5958
5959 if (command_type == ELS_COMMAND_FIP) {
5960 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5961 >> LPFC_FIP_ELS_ID_SHIFT);
5962 }
5963 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5964
5965 break;
5966 case CMD_XMIT_SEQUENCE64_CX:
5967 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5968 iocbq->iocb.un.ulpWord[3]);
5969 wqe->generic.word3 = 0;
5970 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5971 /* The entire sequence is transmitted for this IOCB */
5972 xmit_len = total_len;
5973 cmnd = CMD_XMIT_SEQUENCE64_CR;
5974 case CMD_XMIT_SEQUENCE64_CR:
5975 /* word3 iocb=io_tag32 wqe=payload_offset */
5976 /* payload offset used for multilpe outstanding
5977 * sequences on the same exchange
5978 */
5979 wqe->words[3] = 0;
5980 /* word4 relative_offset memcpy */
5981 /* word5 r_ctl/df_ctl memcpy */
5982 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5983 wqe->xmit_sequence.xmit_len = xmit_len;
5984 command_type = OTHER_COMMAND;
5985 break;
5986 case CMD_XMIT_BCAST64_CN:
5987 /* word3 iocb=iotag32 wqe=payload_len */
5988 wqe->words[3] = 0; /* no definition for this in wqe */
5989 /* word4 iocb=rsvd wqe=rsvd */
5990 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5991 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5992 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5993 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5994 break;
5995 case CMD_FCP_IWRITE64_CR:
5996 command_type = FCP_COMMAND_DATA_OUT;
5997 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5998 * confusing.
5999 * word3 is payload_len: byte offset to the sgl entry for the
6000 * fcp_command.
6001 * word4 is total xfer len, same as the IOCB->ulpParameter.
6002 * word5 is initial xfer len 0 = wait for xfer-ready
6003 */
6004
6005 /* Always wait for xfer-ready before sending data */
6006 wqe->fcp_iwrite.initial_xfer_len = 0;
6007 /* word 4 (xfer length) should have been set on the memcpy */
6008
6009 /* allow write to fall through to read */
6010 case CMD_FCP_IREAD64_CR:
6011 /* FCP_CMD is always the 1st sgl entry */
6012 wqe->fcp_iread.payload_len =
6013 xmit_len + sizeof(struct fcp_rsp);
6014
6015 /* word 4 (xfer length) should have been set on the memcpy */
6016
6017 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6018 iocbq->iocb.ulpFCP2Rcvy);
6019 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6020 /* The XC bit and the XS bit are similar. The driver never
6021 * tracked whether or not the exchange was previouslly open.
6022 * XC = Exchange create, 0 is create. 1 is already open.
6023 * XS = link cmd: 1 do not close the exchange after command.
6024 * XS = 0 close exchange when command completes.
6025 * The only time we would not set the XC bit is when the XS bit
6026 * is set and we are sending our 2nd or greater command on
6027 * this exchange.
6028 */
6029 /* Always open the exchange */
6030 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6031
6032 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6033 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6034 break;
6035 case CMD_FCP_ICMND64_CR:
6036 /* Always open the exchange */
6037 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6038
6039 wqe->words[4] = 0;
6040 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6041 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6042 break;
6043 case CMD_GEN_REQUEST64_CR:
6044 /* word3 command length is described as byte offset to the
6045 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6046 * sgl[0] = cmnd
6047 * sgl[1] = rsp.
6048 *
6049 */
6050 wqe->gen_req.command_len = xmit_len;
6051 /* Word4 parameter copied in the memcpy */
6052 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6053 /* word6 context tag copied in memcpy */
6054 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6055 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6057 "2015 Invalid CT %x command 0x%x\n",
6058 ct, iocbq->iocb.ulpCommand);
6059 return IOCB_ERROR;
6060 }
6061 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6062 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6063 iocbq->iocb.ulpTimeout);
6064
6065 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6066 command_type = OTHER_COMMAND;
6067 break;
6068 case CMD_XMIT_ELS_RSP64_CX:
6069 /* words0-2 BDE memcpy */
6070 /* word3 iocb=iotag32 wqe=rsvd */
6071 wqe->words[3] = 0;
6072 /* word4 iocb=did wge=rsvd. */
6073 wqe->words[4] = 0;
6074 /* word5 iocb=rsvd wge=did */
6075 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6076 iocbq->iocb.un.elsreq64.remoteID);
6077
6078 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6079 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6080
6081 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6082 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6083 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6084 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6085 iocbq->vport->vpi + phba->vpi_base);
6086 command_type = OTHER_COMMAND;
6087 break;
6088 case CMD_CLOSE_XRI_CN:
6089 case CMD_ABORT_XRI_CN:
6090 case CMD_ABORT_XRI_CX:
6091 /* words 0-2 memcpy should be 0 rserved */
6092 /* port will send abts */
6093 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6094 /*
6095 * The link is down so the fw does not need to send abts
6096 * on the wire.
6097 */
6098 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6099 else
6100 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6101 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6102 wqe->words[5] = 0;
6103 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6104 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6105 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6106 /*
6107 * The abort handler will send us CMD_ABORT_XRI_CN or
6108 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6109 */
6110 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6111 cmnd = CMD_ABORT_XRI_CX;
6112 command_type = OTHER_COMMAND;
6113 xritag = 0;
6114 break;
6115 case CMD_XMIT_BLS_RSP64_CX:
6116 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6117 * we re-construct this WQE here based on information in
6118 * iocbq from scratch.
6119 */
6120 memset(wqe, 0, sizeof(union lpfc_wqe));
6121 /* OX_ID is invariable to who sent ABTS to CT exchange */
6122 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6123 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6124 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6125 LPFC_ABTS_UNSOL_INT) {
6126 /* ABTS sent by initiator to CT exchange, the
6127 * RX_ID field will be filled with the newly
6128 * allocated responder XRI.
6129 */
6130 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6131 iocbq->sli4_xritag);
6132 } else {
6133 /* ABTS sent by responder to CT exchange, the
6134 * RX_ID field will be filled with the responder
6135 * RX_ID from ABTS.
6136 */
6137 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6138 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6139 }
6140 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6141 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6142 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6143 iocbq->iocb.ulpContext);
6144 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6145 command_type = OTHER_COMMAND;
6146 break;
6147 case CMD_XRI_ABORTED_CX:
6148 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6149 /* words0-2 are all 0's no bde */
6150 /* word3 and word4 are rsvrd */
6151 wqe->words[3] = 0;
6152 wqe->words[4] = 0;
6153 /* word5 iocb=rsvd wge=did */
6154 /* There is no remote port id in the IOCB? */
6155 /* Let this fall through and fail */
6156 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6157 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6158 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6159 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6160 default:
6161 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6162 "2014 Invalid command 0x%x\n",
6163 iocbq->iocb.ulpCommand);
6164 return IOCB_ERROR;
6165 break;
6166
6167 }
6168 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6169 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6170 wqe->generic.abort_tag = abort_tag;
6171 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6172 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6173 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6174 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6175
6176 return 0;
6177 }
6178
6179 /**
6180 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6181 * @phba: Pointer to HBA context object.
6182 * @ring_number: SLI ring number to issue iocb on.
6183 * @piocb: Pointer to command iocb.
6184 * @flag: Flag indicating if this command can be put into txq.
6185 *
6186 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6187 * an iocb command to an HBA with SLI-4 interface spec.
6188 *
6189 * This function is called with hbalock held. The function will return success
6190 * after it successfully submit the iocb to firmware or after adding to the
6191 * txq.
6192 **/
6193 static int
6194 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6195 struct lpfc_iocbq *piocb, uint32_t flag)
6196 {
6197 struct lpfc_sglq *sglq;
6198 uint16_t xritag;
6199 union lpfc_wqe wqe;
6200 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6201
6202 if (piocb->sli4_xritag == NO_XRI) {
6203 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6204 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6205 sglq = NULL;
6206 else {
6207 sglq = __lpfc_sli_get_sglq(phba);
6208 if (!sglq)
6209 return IOCB_ERROR;
6210 piocb->sli4_xritag = sglq->sli4_xritag;
6211 }
6212 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6213 sglq = NULL; /* These IO's already have an XRI and
6214 * a mapped sgl.
6215 */
6216 } else {
6217 /* This is a continuation of a commandi,(CX) so this
6218 * sglq is on the active list
6219 */
6220 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6221 if (!sglq)
6222 return IOCB_ERROR;
6223 }
6224
6225 if (sglq) {
6226 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6227 if (xritag != sglq->sli4_xritag)
6228 return IOCB_ERROR;
6229 }
6230
6231 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6232 return IOCB_ERROR;
6233
6234 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6235 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6236 /*
6237 * For FCP command IOCB, get a new WQ index to distribute
6238 * WQE across the WQsr. On the other hand, for abort IOCB,
6239 * it carries the same WQ index to the original command
6240 * IOCB.
6241 */
6242 if (piocb->iocb_flag & LPFC_IO_FCP)
6243 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6244 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6245 &wqe))
6246 return IOCB_ERROR;
6247 } else {
6248 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6249 return IOCB_ERROR;
6250 }
6251 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6252
6253 return 0;
6254 }
6255
6256 /**
6257 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6258 *
6259 * This routine wraps the actual lockless version for issusing IOCB function
6260 * pointer from the lpfc_hba struct.
6261 *
6262 * Return codes:
6263 * IOCB_ERROR - Error
6264 * IOCB_SUCCESS - Success
6265 * IOCB_BUSY - Busy
6266 **/
6267 static inline int
6268 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6269 struct lpfc_iocbq *piocb, uint32_t flag)
6270 {
6271 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6272 }
6273
6274 /**
6275 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6276 * @phba: The hba struct for which this call is being executed.
6277 * @dev_grp: The HBA PCI-Device group number.
6278 *
6279 * This routine sets up the SLI interface API function jump table in @phba
6280 * struct.
6281 * Returns: 0 - success, -ENODEV - failure.
6282 **/
6283 int
6284 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6285 {
6286
6287 switch (dev_grp) {
6288 case LPFC_PCI_DEV_LP:
6289 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6290 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6291 break;
6292 case LPFC_PCI_DEV_OC:
6293 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6294 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6295 break;
6296 default:
6297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6298 "1419 Invalid HBA PCI-device group: 0x%x\n",
6299 dev_grp);
6300 return -ENODEV;
6301 break;
6302 }
6303 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6304 return 0;
6305 }
6306
6307 /**
6308 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6309 * @phba: Pointer to HBA context object.
6310 * @pring: Pointer to driver SLI ring object.
6311 * @piocb: Pointer to command iocb.
6312 * @flag: Flag indicating if this command can be put into txq.
6313 *
6314 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6315 * function. This function gets the hbalock and calls
6316 * __lpfc_sli_issue_iocb function and will return the error returned
6317 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6318 * functions which do not hold hbalock.
6319 **/
6320 int
6321 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6322 struct lpfc_iocbq *piocb, uint32_t flag)
6323 {
6324 unsigned long iflags;
6325 int rc;
6326
6327 spin_lock_irqsave(&phba->hbalock, iflags);
6328 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6329 spin_unlock_irqrestore(&phba->hbalock, iflags);
6330
6331 return rc;
6332 }
6333
6334 /**
6335 * lpfc_extra_ring_setup - Extra ring setup function
6336 * @phba: Pointer to HBA context object.
6337 *
6338 * This function is called while driver attaches with the
6339 * HBA to setup the extra ring. The extra ring is used
6340 * only when driver needs to support target mode functionality
6341 * or IP over FC functionalities.
6342 *
6343 * This function is called with no lock held.
6344 **/
6345 static int
6346 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6347 {
6348 struct lpfc_sli *psli;
6349 struct lpfc_sli_ring *pring;
6350
6351 psli = &phba->sli;
6352
6353 /* Adjust cmd/rsp ring iocb entries more evenly */
6354
6355 /* Take some away from the FCP ring */
6356 pring = &psli->ring[psli->fcp_ring];
6357 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6358 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6359 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6360 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6361
6362 /* and give them to the extra ring */
6363 pring = &psli->ring[psli->extra_ring];
6364
6365 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6366 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6367 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6368 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6369
6370 /* Setup default profile for this ring */
6371 pring->iotag_max = 4096;
6372 pring->num_mask = 1;
6373 pring->prt[0].profile = 0; /* Mask 0 */
6374 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6375 pring->prt[0].type = phba->cfg_multi_ring_type;
6376 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6377 return 0;
6378 }
6379
6380 /**
6381 * lpfc_sli_async_event_handler - ASYNC iocb handler function
6382 * @phba: Pointer to HBA context object.
6383 * @pring: Pointer to driver SLI ring object.
6384 * @iocbq: Pointer to iocb object.
6385 *
6386 * This function is called by the slow ring event handler
6387 * function when there is an ASYNC event iocb in the ring.
6388 * This function is called with no lock held.
6389 * Currently this function handles only temperature related
6390 * ASYNC events. The function decodes the temperature sensor
6391 * event message and posts events for the management applications.
6392 **/
6393 static void
6394 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6395 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6396 {
6397 IOCB_t *icmd;
6398 uint16_t evt_code;
6399 uint16_t temp;
6400 struct temp_event temp_event_data;
6401 struct Scsi_Host *shost;
6402 uint32_t *iocb_w;
6403
6404 icmd = &iocbq->iocb;
6405 evt_code = icmd->un.asyncstat.evt_code;
6406 temp = icmd->ulpContext;
6407
6408 if ((evt_code != ASYNC_TEMP_WARN) &&
6409 (evt_code != ASYNC_TEMP_SAFE)) {
6410 iocb_w = (uint32_t *) icmd;
6411 lpfc_printf_log(phba,
6412 KERN_ERR,
6413 LOG_SLI,
6414 "0346 Ring %d handler: unexpected ASYNC_STATUS"
6415 " evt_code 0x%x\n"
6416 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6417 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6418 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6419 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6420 pring->ringno,
6421 icmd->un.asyncstat.evt_code,
6422 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6423 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6424 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6425 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6426
6427 return;
6428 }
6429 temp_event_data.data = (uint32_t)temp;
6430 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6431 if (evt_code == ASYNC_TEMP_WARN) {
6432 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6433 lpfc_printf_log(phba,
6434 KERN_ERR,
6435 LOG_TEMP,
6436 "0347 Adapter is very hot, please take "
6437 "corrective action. temperature : %d Celsius\n",
6438 temp);
6439 }
6440 if (evt_code == ASYNC_TEMP_SAFE) {
6441 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6442 lpfc_printf_log(phba,
6443 KERN_ERR,
6444 LOG_TEMP,
6445 "0340 Adapter temperature is OK now. "
6446 "temperature : %d Celsius\n",
6447 temp);
6448 }
6449
6450 /* Send temperature change event to applications */
6451 shost = lpfc_shost_from_vport(phba->pport);
6452 fc_host_post_vendor_event(shost, fc_get_event_number(),
6453 sizeof(temp_event_data), (char *) &temp_event_data,
6454 LPFC_NL_VENDOR_ID);
6455
6456 }
6457
6458
6459 /**
6460 * lpfc_sli_setup - SLI ring setup function
6461 * @phba: Pointer to HBA context object.
6462 *
6463 * lpfc_sli_setup sets up rings of the SLI interface with
6464 * number of iocbs per ring and iotags. This function is
6465 * called while driver attach to the HBA and before the
6466 * interrupts are enabled. So there is no need for locking.
6467 *
6468 * This function always returns 0.
6469 **/
6470 int
6471 lpfc_sli_setup(struct lpfc_hba *phba)
6472 {
6473 int i, totiocbsize = 0;
6474 struct lpfc_sli *psli = &phba->sli;
6475 struct lpfc_sli_ring *pring;
6476
6477 psli->num_rings = MAX_CONFIGURED_RINGS;
6478 psli->sli_flag = 0;
6479 psli->fcp_ring = LPFC_FCP_RING;
6480 psli->next_ring = LPFC_FCP_NEXT_RING;
6481 psli->extra_ring = LPFC_EXTRA_RING;
6482
6483 psli->iocbq_lookup = NULL;
6484 psli->iocbq_lookup_len = 0;
6485 psli->last_iotag = 0;
6486
6487 for (i = 0; i < psli->num_rings; i++) {
6488 pring = &psli->ring[i];
6489 switch (i) {
6490 case LPFC_FCP_RING: /* ring 0 - FCP */
6491 /* numCiocb and numRiocb are used in config_port */
6492 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6493 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6494 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6495 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6496 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6497 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6498 pring->sizeCiocb = (phba->sli_rev == 3) ?
6499 SLI3_IOCB_CMD_SIZE :
6500 SLI2_IOCB_CMD_SIZE;
6501 pring->sizeRiocb = (phba->sli_rev == 3) ?
6502 SLI3_IOCB_RSP_SIZE :
6503 SLI2_IOCB_RSP_SIZE;
6504 pring->iotag_ctr = 0;
6505 pring->iotag_max =
6506 (phba->cfg_hba_queue_depth * 2);
6507 pring->fast_iotag = pring->iotag_max;
6508 pring->num_mask = 0;
6509 break;
6510 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
6511 /* numCiocb and numRiocb are used in config_port */
6512 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6513 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6514 pring->sizeCiocb = (phba->sli_rev == 3) ?
6515 SLI3_IOCB_CMD_SIZE :
6516 SLI2_IOCB_CMD_SIZE;
6517 pring->sizeRiocb = (phba->sli_rev == 3) ?
6518 SLI3_IOCB_RSP_SIZE :
6519 SLI2_IOCB_RSP_SIZE;
6520 pring->iotag_max = phba->cfg_hba_queue_depth;
6521 pring->num_mask = 0;
6522 break;
6523 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6524 /* numCiocb and numRiocb are used in config_port */
6525 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6526 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6527 pring->sizeCiocb = (phba->sli_rev == 3) ?
6528 SLI3_IOCB_CMD_SIZE :
6529 SLI2_IOCB_CMD_SIZE;
6530 pring->sizeRiocb = (phba->sli_rev == 3) ?
6531 SLI3_IOCB_RSP_SIZE :
6532 SLI2_IOCB_RSP_SIZE;
6533 pring->fast_iotag = 0;
6534 pring->iotag_ctr = 0;
6535 pring->iotag_max = 4096;
6536 pring->lpfc_sli_rcv_async_status =
6537 lpfc_sli_async_event_handler;
6538 pring->num_mask = LPFC_MAX_RING_MASK;
6539 pring->prt[0].profile = 0; /* Mask 0 */
6540 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6541 pring->prt[0].type = FC_TYPE_ELS;
6542 pring->prt[0].lpfc_sli_rcv_unsol_event =
6543 lpfc_els_unsol_event;
6544 pring->prt[1].profile = 0; /* Mask 1 */
6545 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6546 pring->prt[1].type = FC_TYPE_ELS;
6547 pring->prt[1].lpfc_sli_rcv_unsol_event =
6548 lpfc_els_unsol_event;
6549 pring->prt[2].profile = 0; /* Mask 2 */
6550 /* NameServer Inquiry */
6551 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6552 /* NameServer */
6553 pring->prt[2].type = FC_TYPE_CT;
6554 pring->prt[2].lpfc_sli_rcv_unsol_event =
6555 lpfc_ct_unsol_event;
6556 pring->prt[3].profile = 0; /* Mask 3 */
6557 /* NameServer response */
6558 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6559 /* NameServer */
6560 pring->prt[3].type = FC_TYPE_CT;
6561 pring->prt[3].lpfc_sli_rcv_unsol_event =
6562 lpfc_ct_unsol_event;
6563 /* abort unsolicited sequence */
6564 pring->prt[4].profile = 0; /* Mask 4 */
6565 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6566 pring->prt[4].type = FC_TYPE_BLS;
6567 pring->prt[4].lpfc_sli_rcv_unsol_event =
6568 lpfc_sli4_ct_abort_unsol_event;
6569 break;
6570 }
6571 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6572 (pring->numRiocb * pring->sizeRiocb);
6573 }
6574 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6575 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6576 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6577 "SLI2 SLIM Data: x%x x%lx\n",
6578 phba->brd_no, totiocbsize,
6579 (unsigned long) MAX_SLIM_IOCB_SIZE);
6580 }
6581 if (phba->cfg_multi_ring_support == 2)
6582 lpfc_extra_ring_setup(phba);
6583
6584 return 0;
6585 }
6586
6587 /**
6588 * lpfc_sli_queue_setup - Queue initialization function
6589 * @phba: Pointer to HBA context object.
6590 *
6591 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6592 * ring. This function also initializes ring indices of each ring.
6593 * This function is called during the initialization of the SLI
6594 * interface of an HBA.
6595 * This function is called with no lock held and always returns
6596 * 1.
6597 **/
6598 int
6599 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6600 {
6601 struct lpfc_sli *psli;
6602 struct lpfc_sli_ring *pring;
6603 int i;
6604
6605 psli = &phba->sli;
6606 spin_lock_irq(&phba->hbalock);
6607 INIT_LIST_HEAD(&psli->mboxq);
6608 INIT_LIST_HEAD(&psli->mboxq_cmpl);
6609 /* Initialize list headers for txq and txcmplq as double linked lists */
6610 for (i = 0; i < psli->num_rings; i++) {
6611 pring = &psli->ring[i];
6612 pring->ringno = i;
6613 pring->next_cmdidx = 0;
6614 pring->local_getidx = 0;
6615 pring->cmdidx = 0;
6616 INIT_LIST_HEAD(&pring->txq);
6617 INIT_LIST_HEAD(&pring->txcmplq);
6618 INIT_LIST_HEAD(&pring->iocb_continueq);
6619 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6620 INIT_LIST_HEAD(&pring->postbufq);
6621 }
6622 spin_unlock_irq(&phba->hbalock);
6623 return 1;
6624 }
6625
6626 /**
6627 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6628 * @phba: Pointer to HBA context object.
6629 *
6630 * This routine flushes the mailbox command subsystem. It will unconditionally
6631 * flush all the mailbox commands in the three possible stages in the mailbox
6632 * command sub-system: pending mailbox command queue; the outstanding mailbox
6633 * command; and completed mailbox command queue. It is caller's responsibility
6634 * to make sure that the driver is in the proper state to flush the mailbox
6635 * command sub-system. Namely, the posting of mailbox commands into the
6636 * pending mailbox command queue from the various clients must be stopped;
6637 * either the HBA is in a state that it will never works on the outstanding
6638 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6639 * mailbox command has been completed.
6640 **/
6641 static void
6642 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6643 {
6644 LIST_HEAD(completions);
6645 struct lpfc_sli *psli = &phba->sli;
6646 LPFC_MBOXQ_t *pmb;
6647 unsigned long iflag;
6648
6649 /* Flush all the mailbox commands in the mbox system */
6650 spin_lock_irqsave(&phba->hbalock, iflag);
6651 /* The pending mailbox command queue */
6652 list_splice_init(&phba->sli.mboxq, &completions);
6653 /* The outstanding active mailbox command */
6654 if (psli->mbox_active) {
6655 list_add_tail(&psli->mbox_active->list, &completions);
6656 psli->mbox_active = NULL;
6657 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6658 }
6659 /* The completed mailbox command queue */
6660 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6661 spin_unlock_irqrestore(&phba->hbalock, iflag);
6662
6663 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6664 while (!list_empty(&completions)) {
6665 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6666 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6667 if (pmb->mbox_cmpl)
6668 pmb->mbox_cmpl(phba, pmb);
6669 }
6670 }
6671
6672 /**
6673 * lpfc_sli_host_down - Vport cleanup function
6674 * @vport: Pointer to virtual port object.
6675 *
6676 * lpfc_sli_host_down is called to clean up the resources
6677 * associated with a vport before destroying virtual
6678 * port data structures.
6679 * This function does following operations:
6680 * - Free discovery resources associated with this virtual
6681 * port.
6682 * - Free iocbs associated with this virtual port in
6683 * the txq.
6684 * - Send abort for all iocb commands associated with this
6685 * vport in txcmplq.
6686 *
6687 * This function is called with no lock held and always returns 1.
6688 **/
6689 int
6690 lpfc_sli_host_down(struct lpfc_vport *vport)
6691 {
6692 LIST_HEAD(completions);
6693 struct lpfc_hba *phba = vport->phba;
6694 struct lpfc_sli *psli = &phba->sli;
6695 struct lpfc_sli_ring *pring;
6696 struct lpfc_iocbq *iocb, *next_iocb;
6697 int i;
6698 unsigned long flags = 0;
6699 uint16_t prev_pring_flag;
6700
6701 lpfc_cleanup_discovery_resources(vport);
6702
6703 spin_lock_irqsave(&phba->hbalock, flags);
6704 for (i = 0; i < psli->num_rings; i++) {
6705 pring = &psli->ring[i];
6706 prev_pring_flag = pring->flag;
6707 /* Only slow rings */
6708 if (pring->ringno == LPFC_ELS_RING) {
6709 pring->flag |= LPFC_DEFERRED_RING_EVENT;
6710 /* Set the lpfc data pending flag */
6711 set_bit(LPFC_DATA_READY, &phba->data_flags);
6712 }
6713 /*
6714 * Error everything on the txq since these iocbs have not been
6715 * given to the FW yet.
6716 */
6717 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6718 if (iocb->vport != vport)
6719 continue;
6720 list_move_tail(&iocb->list, &completions);
6721 pring->txq_cnt--;
6722 }
6723
6724 /* Next issue ABTS for everything on the txcmplq */
6725 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6726 list) {
6727 if (iocb->vport != vport)
6728 continue;
6729 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6730 }
6731
6732 pring->flag = prev_pring_flag;
6733 }
6734
6735 spin_unlock_irqrestore(&phba->hbalock, flags);
6736
6737 /* Cancel all the IOCBs from the completions list */
6738 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6739 IOERR_SLI_DOWN);
6740 return 1;
6741 }
6742
6743 /**
6744 * lpfc_sli_hba_down - Resource cleanup function for the HBA
6745 * @phba: Pointer to HBA context object.
6746 *
6747 * This function cleans up all iocb, buffers, mailbox commands
6748 * while shutting down the HBA. This function is called with no
6749 * lock held and always returns 1.
6750 * This function does the following to cleanup driver resources:
6751 * - Free discovery resources for each virtual port
6752 * - Cleanup any pending fabric iocbs
6753 * - Iterate through the iocb txq and free each entry
6754 * in the list.
6755 * - Free up any buffer posted to the HBA
6756 * - Free mailbox commands in the mailbox queue.
6757 **/
6758 int
6759 lpfc_sli_hba_down(struct lpfc_hba *phba)
6760 {
6761 LIST_HEAD(completions);
6762 struct lpfc_sli *psli = &phba->sli;
6763 struct lpfc_sli_ring *pring;
6764 struct lpfc_dmabuf *buf_ptr;
6765 unsigned long flags = 0;
6766 int i;
6767
6768 /* Shutdown the mailbox command sub-system */
6769 lpfc_sli_mbox_sys_shutdown(phba);
6770
6771 lpfc_hba_down_prep(phba);
6772
6773 lpfc_fabric_abort_hba(phba);
6774
6775 spin_lock_irqsave(&phba->hbalock, flags);
6776 for (i = 0; i < psli->num_rings; i++) {
6777 pring = &psli->ring[i];
6778 /* Only slow rings */
6779 if (pring->ringno == LPFC_ELS_RING) {
6780 pring->flag |= LPFC_DEFERRED_RING_EVENT;
6781 /* Set the lpfc data pending flag */
6782 set_bit(LPFC_DATA_READY, &phba->data_flags);
6783 }
6784
6785 /*
6786 * Error everything on the txq since these iocbs have not been
6787 * given to the FW yet.
6788 */
6789 list_splice_init(&pring->txq, &completions);
6790 pring->txq_cnt = 0;
6791
6792 }
6793 spin_unlock_irqrestore(&phba->hbalock, flags);
6794
6795 /* Cancel all the IOCBs from the completions list */
6796 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6797 IOERR_SLI_DOWN);
6798
6799 spin_lock_irqsave(&phba->hbalock, flags);
6800 list_splice_init(&phba->elsbuf, &completions);
6801 phba->elsbuf_cnt = 0;
6802 phba->elsbuf_prev_cnt = 0;
6803 spin_unlock_irqrestore(&phba->hbalock, flags);
6804
6805 while (!list_empty(&completions)) {
6806 list_remove_head(&completions, buf_ptr,
6807 struct lpfc_dmabuf, list);
6808 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6809 kfree(buf_ptr);
6810 }
6811
6812 /* Return any active mbox cmds */
6813 del_timer_sync(&psli->mbox_tmo);
6814
6815 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6816 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6817 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6818
6819 return 1;
6820 }
6821
6822 /**
6823 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6824 * @phba: Pointer to HBA context object.
6825 *
6826 * This function cleans up all queues, iocb, buffers, mailbox commands while
6827 * shutting down the SLI4 HBA FCoE function. This function is called with no
6828 * lock held and always returns 1.
6829 *
6830 * This function does the following to cleanup driver FCoE function resources:
6831 * - Free discovery resources for each virtual port
6832 * - Cleanup any pending fabric iocbs
6833 * - Iterate through the iocb txq and free each entry in the list.
6834 * - Free up any buffer posted to the HBA.
6835 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6836 * - Free mailbox commands in the mailbox queue.
6837 **/
6838 int
6839 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6840 {
6841 /* Stop the SLI4 device port */
6842 lpfc_stop_port(phba);
6843
6844 /* Tear down the queues in the HBA */
6845 lpfc_sli4_queue_unset(phba);
6846
6847 /* unregister default FCFI from the HBA */
6848 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6849
6850 return 1;
6851 }
6852
6853 /**
6854 * lpfc_sli_pcimem_bcopy - SLI memory copy function
6855 * @srcp: Source memory pointer.
6856 * @destp: Destination memory pointer.
6857 * @cnt: Number of words required to be copied.
6858 *
6859 * This function is used for copying data between driver memory
6860 * and the SLI memory. This function also changes the endianness
6861 * of each word if native endianness is different from SLI
6862 * endianness. This function can be called with or without
6863 * lock.
6864 **/
6865 void
6866 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6867 {
6868 uint32_t *src = srcp;
6869 uint32_t *dest = destp;
6870 uint32_t ldata;
6871 int i;
6872
6873 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6874 ldata = *src;
6875 ldata = le32_to_cpu(ldata);
6876 *dest = ldata;
6877 src++;
6878 dest++;
6879 }
6880 }
6881
6882
6883 /**
6884 * lpfc_sli_bemem_bcopy - SLI memory copy function
6885 * @srcp: Source memory pointer.
6886 * @destp: Destination memory pointer.
6887 * @cnt: Number of words required to be copied.
6888 *
6889 * This function is used for copying data between a data structure
6890 * with big endian representation to local endianness.
6891 * This function can be called with or without lock.
6892 **/
6893 void
6894 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6895 {
6896 uint32_t *src = srcp;
6897 uint32_t *dest = destp;
6898 uint32_t ldata;
6899 int i;
6900
6901 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6902 ldata = *src;
6903 ldata = be32_to_cpu(ldata);
6904 *dest = ldata;
6905 src++;
6906 dest++;
6907 }
6908 }
6909
6910 /**
6911 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6912 * @phba: Pointer to HBA context object.
6913 * @pring: Pointer to driver SLI ring object.
6914 * @mp: Pointer to driver buffer object.
6915 *
6916 * This function is called with no lock held.
6917 * It always return zero after adding the buffer to the postbufq
6918 * buffer list.
6919 **/
6920 int
6921 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6922 struct lpfc_dmabuf *mp)
6923 {
6924 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6925 later */
6926 spin_lock_irq(&phba->hbalock);
6927 list_add_tail(&mp->list, &pring->postbufq);
6928 pring->postbufq_cnt++;
6929 spin_unlock_irq(&phba->hbalock);
6930 return 0;
6931 }
6932
6933 /**
6934 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6935 * @phba: Pointer to HBA context object.
6936 *
6937 * When HBQ is enabled, buffers are searched based on tags. This function
6938 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6939 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6940 * does not conflict with tags of buffer posted for unsolicited events.
6941 * The function returns the allocated tag. The function is called with
6942 * no locks held.
6943 **/
6944 uint32_t
6945 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6946 {
6947 spin_lock_irq(&phba->hbalock);
6948 phba->buffer_tag_count++;
6949 /*
6950 * Always set the QUE_BUFTAG_BIT to distiguish between
6951 * a tag assigned by HBQ.
6952 */
6953 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6954 spin_unlock_irq(&phba->hbalock);
6955 return phba->buffer_tag_count;
6956 }
6957
6958 /**
6959 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6960 * @phba: Pointer to HBA context object.
6961 * @pring: Pointer to driver SLI ring object.
6962 * @tag: Buffer tag.
6963 *
6964 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6965 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6966 * iocb is posted to the response ring with the tag of the buffer.
6967 * This function searches the pring->postbufq list using the tag
6968 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6969 * iocb. If the buffer is found then lpfc_dmabuf object of the
6970 * buffer is returned to the caller else NULL is returned.
6971 * This function is called with no lock held.
6972 **/
6973 struct lpfc_dmabuf *
6974 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6975 uint32_t tag)
6976 {
6977 struct lpfc_dmabuf *mp, *next_mp;
6978 struct list_head *slp = &pring->postbufq;
6979
6980 /* Search postbufq, from the begining, looking for a match on tag */
6981 spin_lock_irq(&phba->hbalock);
6982 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6983 if (mp->buffer_tag == tag) {
6984 list_del_init(&mp->list);
6985 pring->postbufq_cnt--;
6986 spin_unlock_irq(&phba->hbalock);
6987 return mp;
6988 }
6989 }
6990
6991 spin_unlock_irq(&phba->hbalock);
6992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6993 "0402 Cannot find virtual addr for buffer tag on "
6994 "ring %d Data x%lx x%p x%p x%x\n",
6995 pring->ringno, (unsigned long) tag,
6996 slp->next, slp->prev, pring->postbufq_cnt);
6997
6998 return NULL;
6999 }
7000
7001 /**
7002 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7003 * @phba: Pointer to HBA context object.
7004 * @pring: Pointer to driver SLI ring object.
7005 * @phys: DMA address of the buffer.
7006 *
7007 * This function searches the buffer list using the dma_address
7008 * of unsolicited event to find the driver's lpfc_dmabuf object
7009 * corresponding to the dma_address. The function returns the
7010 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7011 * This function is called by the ct and els unsolicited event
7012 * handlers to get the buffer associated with the unsolicited
7013 * event.
7014 *
7015 * This function is called with no lock held.
7016 **/
7017 struct lpfc_dmabuf *
7018 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7019 dma_addr_t phys)
7020 {
7021 struct lpfc_dmabuf *mp, *next_mp;
7022 struct list_head *slp = &pring->postbufq;
7023
7024 /* Search postbufq, from the begining, looking for a match on phys */
7025 spin_lock_irq(&phba->hbalock);
7026 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7027 if (mp->phys == phys) {
7028 list_del_init(&mp->list);
7029 pring->postbufq_cnt--;
7030 spin_unlock_irq(&phba->hbalock);
7031 return mp;
7032 }
7033 }
7034
7035 spin_unlock_irq(&phba->hbalock);
7036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7037 "0410 Cannot find virtual addr for mapped buf on "
7038 "ring %d Data x%llx x%p x%p x%x\n",
7039 pring->ringno, (unsigned long long)phys,
7040 slp->next, slp->prev, pring->postbufq_cnt);
7041 return NULL;
7042 }
7043
7044 /**
7045 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7046 * @phba: Pointer to HBA context object.
7047 * @cmdiocb: Pointer to driver command iocb object.
7048 * @rspiocb: Pointer to driver response iocb object.
7049 *
7050 * This function is the completion handler for the abort iocbs for
7051 * ELS commands. This function is called from the ELS ring event
7052 * handler with no lock held. This function frees memory resources
7053 * associated with the abort iocb.
7054 **/
7055 static void
7056 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7057 struct lpfc_iocbq *rspiocb)
7058 {
7059 IOCB_t *irsp = &rspiocb->iocb;
7060 uint16_t abort_iotag, abort_context;
7061 struct lpfc_iocbq *abort_iocb;
7062 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7063
7064 abort_iocb = NULL;
7065
7066 if (irsp->ulpStatus) {
7067 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7068 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7069
7070 spin_lock_irq(&phba->hbalock);
7071 if (phba->sli_rev < LPFC_SLI_REV4) {
7072 if (abort_iotag != 0 &&
7073 abort_iotag <= phba->sli.last_iotag)
7074 abort_iocb =
7075 phba->sli.iocbq_lookup[abort_iotag];
7076 } else
7077 /* For sli4 the abort_tag is the XRI,
7078 * so the abort routine puts the iotag of the iocb
7079 * being aborted in the context field of the abort
7080 * IOCB.
7081 */
7082 abort_iocb = phba->sli.iocbq_lookup[abort_context];
7083
7084 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
7085 "0327 Cannot abort els iocb %p "
7086 "with tag %x context %x, abort status %x, "
7087 "abort code %x\n",
7088 abort_iocb, abort_iotag, abort_context,
7089 irsp->ulpStatus, irsp->un.ulpWord[4]);
7090
7091 /*
7092 * If the iocb is not found in Firmware queue the iocb
7093 * might have completed already. Do not free it again.
7094 */
7095 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7096 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7097 spin_unlock_irq(&phba->hbalock);
7098 lpfc_sli_release_iocbq(phba, cmdiocb);
7099 return;
7100 }
7101 /* For SLI4 the ulpContext field for abort IOCB
7102 * holds the iotag of the IOCB being aborted so
7103 * the local abort_context needs to be reset to
7104 * match the aborted IOCBs ulpContext.
7105 */
7106 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7107 abort_context = abort_iocb->iocb.ulpContext;
7108 }
7109 /*
7110 * make sure we have the right iocbq before taking it
7111 * off the txcmplq and try to call completion routine.
7112 */
7113 if (!abort_iocb ||
7114 abort_iocb->iocb.ulpContext != abort_context ||
7115 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7116 spin_unlock_irq(&phba->hbalock);
7117 else if (phba->sli_rev < LPFC_SLI_REV4) {
7118 /*
7119 * leave the SLI4 aborted command on the txcmplq
7120 * list and the command complete WCQE's XB bit
7121 * will tell whether the SGL (XRI) can be released
7122 * immediately or to the aborted SGL list for the
7123 * following abort XRI from the HBA.
7124 */
7125 list_del_init(&abort_iocb->list);
7126 pring->txcmplq_cnt--;
7127
7128 /* Firmware could still be in progress of DMAing
7129 * payload, so don't free data buffer till after
7130 * a hbeat.
7131 */
7132 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7133 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7134 spin_unlock_irq(&phba->hbalock);
7135
7136 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7137 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7138 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7139 } else
7140 spin_unlock_irq(&phba->hbalock);
7141 }
7142
7143 lpfc_sli_release_iocbq(phba, cmdiocb);
7144 return;
7145 }
7146
7147 /**
7148 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7149 * @phba: Pointer to HBA context object.
7150 * @cmdiocb: Pointer to driver command iocb object.
7151 * @rspiocb: Pointer to driver response iocb object.
7152 *
7153 * The function is called from SLI ring event handler with no
7154 * lock held. This function is the completion handler for ELS commands
7155 * which are aborted. The function frees memory resources used for
7156 * the aborted ELS commands.
7157 **/
7158 static void
7159 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7160 struct lpfc_iocbq *rspiocb)
7161 {
7162 IOCB_t *irsp = &rspiocb->iocb;
7163
7164 /* ELS cmd tag <ulpIoTag> completes */
7165 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7166 "0139 Ignoring ELS cmd tag x%x completion Data: "
7167 "x%x x%x x%x\n",
7168 irsp->ulpIoTag, irsp->ulpStatus,
7169 irsp->un.ulpWord[4], irsp->ulpTimeout);
7170 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7171 lpfc_ct_free_iocb(phba, cmdiocb);
7172 else
7173 lpfc_els_free_iocb(phba, cmdiocb);
7174 return;
7175 }
7176
7177 /**
7178 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7179 * @phba: Pointer to HBA context object.
7180 * @pring: Pointer to driver SLI ring object.
7181 * @cmdiocb: Pointer to driver command iocb object.
7182 *
7183 * This function issues an abort iocb for the provided command
7184 * iocb. This function is called with hbalock held.
7185 * The function returns 0 when it fails due to memory allocation
7186 * failure or when the command iocb is an abort request.
7187 **/
7188 int
7189 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7190 struct lpfc_iocbq *cmdiocb)
7191 {
7192 struct lpfc_vport *vport = cmdiocb->vport;
7193 struct lpfc_iocbq *abtsiocbp;
7194 IOCB_t *icmd = NULL;
7195 IOCB_t *iabt = NULL;
7196 int retval = IOCB_ERROR;
7197
7198 /*
7199 * There are certain command types we don't want to abort. And we
7200 * don't want to abort commands that are already in the process of
7201 * being aborted.
7202 */
7203 icmd = &cmdiocb->iocb;
7204 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7205 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7206 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7207 return 0;
7208
7209 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7210 * callback so that nothing happens when it finishes.
7211 */
7212 if ((vport->load_flag & FC_UNLOADING) &&
7213 (pring->ringno == LPFC_ELS_RING)) {
7214 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7215 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7216 else
7217 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7218 goto abort_iotag_exit;
7219 }
7220
7221 /* issue ABTS for this IOCB based on iotag */
7222 abtsiocbp = __lpfc_sli_get_iocbq(phba);
7223 if (abtsiocbp == NULL)
7224 return 0;
7225
7226 /* This signals the response to set the correct status
7227 * before calling the completion handler
7228 */
7229 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7230
7231 iabt = &abtsiocbp->iocb;
7232 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7233 iabt->un.acxri.abortContextTag = icmd->ulpContext;
7234 if (phba->sli_rev == LPFC_SLI_REV4) {
7235 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7236 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7237 }
7238 else
7239 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7240 iabt->ulpLe = 1;
7241 iabt->ulpClass = icmd->ulpClass;
7242
7243 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7244 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7245 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7246 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7247
7248 if (phba->link_state >= LPFC_LINK_UP)
7249 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7250 else
7251 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7252
7253 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7254
7255 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7256 "0339 Abort xri x%x, original iotag x%x, "
7257 "abort cmd iotag x%x\n",
7258 iabt->un.acxri.abortContextTag,
7259 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
7260 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7261
7262 if (retval)
7263 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7264 abort_iotag_exit:
7265 /*
7266 * Caller to this routine should check for IOCB_ERROR
7267 * and handle it properly. This routine no longer removes
7268 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7269 */
7270 return retval;
7271 }
7272
7273 /**
7274 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7275 * @iocbq: Pointer to driver iocb object.
7276 * @vport: Pointer to driver virtual port object.
7277 * @tgt_id: SCSI ID of the target.
7278 * @lun_id: LUN ID of the scsi device.
7279 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7280 *
7281 * This function acts as an iocb filter for functions which abort or count
7282 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7283 * 0 if the filtering criteria is met for the given iocb and will return
7284 * 1 if the filtering criteria is not met.
7285 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7286 * given iocb is for the SCSI device specified by vport, tgt_id and
7287 * lun_id parameter.
7288 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7289 * given iocb is for the SCSI target specified by vport and tgt_id
7290 * parameters.
7291 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7292 * given iocb is for the SCSI host associated with the given vport.
7293 * This function is called with no locks held.
7294 **/
7295 static int
7296 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7297 uint16_t tgt_id, uint64_t lun_id,
7298 lpfc_ctx_cmd ctx_cmd)
7299 {
7300 struct lpfc_scsi_buf *lpfc_cmd;
7301 int rc = 1;
7302
7303 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7304 return rc;
7305
7306 if (iocbq->vport != vport)
7307 return rc;
7308
7309 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7310
7311 if (lpfc_cmd->pCmd == NULL)
7312 return rc;
7313
7314 switch (ctx_cmd) {
7315 case LPFC_CTX_LUN:
7316 if ((lpfc_cmd->rdata->pnode) &&
7317 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7318 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7319 rc = 0;
7320 break;
7321 case LPFC_CTX_TGT:
7322 if ((lpfc_cmd->rdata->pnode) &&
7323 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7324 rc = 0;
7325 break;
7326 case LPFC_CTX_HOST:
7327 rc = 0;
7328 break;
7329 default:
7330 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7331 __func__, ctx_cmd);
7332 break;
7333 }
7334
7335 return rc;
7336 }
7337
7338 /**
7339 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7340 * @vport: Pointer to virtual port.
7341 * @tgt_id: SCSI ID of the target.
7342 * @lun_id: LUN ID of the scsi device.
7343 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7344 *
7345 * This function returns number of FCP commands pending for the vport.
7346 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7347 * commands pending on the vport associated with SCSI device specified
7348 * by tgt_id and lun_id parameters.
7349 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7350 * commands pending on the vport associated with SCSI target specified
7351 * by tgt_id parameter.
7352 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7353 * commands pending on the vport.
7354 * This function returns the number of iocbs which satisfy the filter.
7355 * This function is called without any lock held.
7356 **/
7357 int
7358 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7359 lpfc_ctx_cmd ctx_cmd)
7360 {
7361 struct lpfc_hba *phba = vport->phba;
7362 struct lpfc_iocbq *iocbq;
7363 int sum, i;
7364
7365 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7366 iocbq = phba->sli.iocbq_lookup[i];
7367
7368 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7369 ctx_cmd) == 0)
7370 sum++;
7371 }
7372
7373 return sum;
7374 }
7375
7376 /**
7377 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7378 * @phba: Pointer to HBA context object
7379 * @cmdiocb: Pointer to command iocb object.
7380 * @rspiocb: Pointer to response iocb object.
7381 *
7382 * This function is called when an aborted FCP iocb completes. This
7383 * function is called by the ring event handler with no lock held.
7384 * This function frees the iocb.
7385 **/
7386 void
7387 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7388 struct lpfc_iocbq *rspiocb)
7389 {
7390 lpfc_sli_release_iocbq(phba, cmdiocb);
7391 return;
7392 }
7393
7394 /**
7395 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7396 * @vport: Pointer to virtual port.
7397 * @pring: Pointer to driver SLI ring object.
7398 * @tgt_id: SCSI ID of the target.
7399 * @lun_id: LUN ID of the scsi device.
7400 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7401 *
7402 * This function sends an abort command for every SCSI command
7403 * associated with the given virtual port pending on the ring
7404 * filtered by lpfc_sli_validate_fcp_iocb function.
7405 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7406 * FCP iocbs associated with lun specified by tgt_id and lun_id
7407 * parameters
7408 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7409 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7410 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7411 * FCP iocbs associated with virtual port.
7412 * This function returns number of iocbs it failed to abort.
7413 * This function is called with no locks held.
7414 **/
7415 int
7416 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7417 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7418 {
7419 struct lpfc_hba *phba = vport->phba;
7420 struct lpfc_iocbq *iocbq;
7421 struct lpfc_iocbq *abtsiocb;
7422 IOCB_t *cmd = NULL;
7423 int errcnt = 0, ret_val = 0;
7424 int i;
7425
7426 for (i = 1; i <= phba->sli.last_iotag; i++) {
7427 iocbq = phba->sli.iocbq_lookup[i];
7428
7429 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7430 abort_cmd) != 0)
7431 continue;
7432
7433 /* issue ABTS for this IOCB based on iotag */
7434 abtsiocb = lpfc_sli_get_iocbq(phba);
7435 if (abtsiocb == NULL) {
7436 errcnt++;
7437 continue;
7438 }
7439
7440 cmd = &iocbq->iocb;
7441 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7442 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7443 if (phba->sli_rev == LPFC_SLI_REV4)
7444 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7445 else
7446 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7447 abtsiocb->iocb.ulpLe = 1;
7448 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7449 abtsiocb->vport = phba->pport;
7450
7451 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7452 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7453 if (iocbq->iocb_flag & LPFC_IO_FCP)
7454 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7455
7456 if (lpfc_is_link_up(phba))
7457 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7458 else
7459 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7460
7461 /* Setup callback routine and issue the command. */
7462 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7463 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7464 abtsiocb, 0);
7465 if (ret_val == IOCB_ERROR) {
7466 lpfc_sli_release_iocbq(phba, abtsiocb);
7467 errcnt++;
7468 continue;
7469 }
7470 }
7471
7472 return errcnt;
7473 }
7474
7475 /**
7476 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7477 * @phba: Pointer to HBA context object.
7478 * @cmdiocbq: Pointer to command iocb.
7479 * @rspiocbq: Pointer to response iocb.
7480 *
7481 * This function is the completion handler for iocbs issued using
7482 * lpfc_sli_issue_iocb_wait function. This function is called by the
7483 * ring event handler function without any lock held. This function
7484 * can be called from both worker thread context and interrupt
7485 * context. This function also can be called from other thread which
7486 * cleans up the SLI layer objects.
7487 * This function copy the contents of the response iocb to the
7488 * response iocb memory object provided by the caller of
7489 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7490 * sleeps for the iocb completion.
7491 **/
7492 static void
7493 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7494 struct lpfc_iocbq *cmdiocbq,
7495 struct lpfc_iocbq *rspiocbq)
7496 {
7497 wait_queue_head_t *pdone_q;
7498 unsigned long iflags;
7499 struct lpfc_scsi_buf *lpfc_cmd;
7500
7501 spin_lock_irqsave(&phba->hbalock, iflags);
7502 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7503 if (cmdiocbq->context2 && rspiocbq)
7504 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7505 &rspiocbq->iocb, sizeof(IOCB_t));
7506
7507 /* Set the exchange busy flag for task management commands */
7508 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7509 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7510 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7511 cur_iocbq);
7512 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7513 }
7514
7515 pdone_q = cmdiocbq->context_un.wait_queue;
7516 if (pdone_q)
7517 wake_up(pdone_q);
7518 spin_unlock_irqrestore(&phba->hbalock, iflags);
7519 return;
7520 }
7521
7522 /**
7523 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7524 * @phba: Pointer to HBA context object..
7525 * @piocbq: Pointer to command iocb.
7526 * @flag: Flag to test.
7527 *
7528 * This routine grabs the hbalock and then test the iocb_flag to
7529 * see if the passed in flag is set.
7530 * Returns:
7531 * 1 if flag is set.
7532 * 0 if flag is not set.
7533 **/
7534 static int
7535 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7536 struct lpfc_iocbq *piocbq, uint32_t flag)
7537 {
7538 unsigned long iflags;
7539 int ret;
7540
7541 spin_lock_irqsave(&phba->hbalock, iflags);
7542 ret = piocbq->iocb_flag & flag;
7543 spin_unlock_irqrestore(&phba->hbalock, iflags);
7544 return ret;
7545
7546 }
7547
7548 /**
7549 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7550 * @phba: Pointer to HBA context object..
7551 * @pring: Pointer to sli ring.
7552 * @piocb: Pointer to command iocb.
7553 * @prspiocbq: Pointer to response iocb.
7554 * @timeout: Timeout in number of seconds.
7555 *
7556 * This function issues the iocb to firmware and waits for the
7557 * iocb to complete. If the iocb command is not
7558 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7559 * Caller should not free the iocb resources if this function
7560 * returns IOCB_TIMEDOUT.
7561 * The function waits for the iocb completion using an
7562 * non-interruptible wait.
7563 * This function will sleep while waiting for iocb completion.
7564 * So, this function should not be called from any context which
7565 * does not allow sleeping. Due to the same reason, this function
7566 * cannot be called with interrupt disabled.
7567 * This function assumes that the iocb completions occur while
7568 * this function sleep. So, this function cannot be called from
7569 * the thread which process iocb completion for this ring.
7570 * This function clears the iocb_flag of the iocb object before
7571 * issuing the iocb and the iocb completion handler sets this
7572 * flag and wakes this thread when the iocb completes.
7573 * The contents of the response iocb will be copied to prspiocbq
7574 * by the completion handler when the command completes.
7575 * This function returns IOCB_SUCCESS when success.
7576 * This function is called with no lock held.
7577 **/
7578 int
7579 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7580 uint32_t ring_number,
7581 struct lpfc_iocbq *piocb,
7582 struct lpfc_iocbq *prspiocbq,
7583 uint32_t timeout)
7584 {
7585 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7586 long timeleft, timeout_req = 0;
7587 int retval = IOCB_SUCCESS;
7588 uint32_t creg_val;
7589
7590 /*
7591 * If the caller has provided a response iocbq buffer, then context2
7592 * is NULL or its an error.
7593 */
7594 if (prspiocbq) {
7595 if (piocb->context2)
7596 return IOCB_ERROR;
7597 piocb->context2 = prspiocbq;
7598 }
7599
7600 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7601 piocb->context_un.wait_queue = &done_q;
7602 piocb->iocb_flag &= ~LPFC_IO_WAKE;
7603
7604 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7605 creg_val = readl(phba->HCregaddr);
7606 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7607 writel(creg_val, phba->HCregaddr);
7608 readl(phba->HCregaddr); /* flush */
7609 }
7610
7611 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7612 if (retval == IOCB_SUCCESS) {
7613 timeout_req = timeout * HZ;
7614 timeleft = wait_event_timeout(done_q,
7615 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7616 timeout_req);
7617
7618 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7619 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7620 "0331 IOCB wake signaled\n");
7621 } else if (timeleft == 0) {
7622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7623 "0338 IOCB wait timeout error - no "
7624 "wake response Data x%x\n", timeout);
7625 retval = IOCB_TIMEDOUT;
7626 } else {
7627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7628 "0330 IOCB wake NOT set, "
7629 "Data x%x x%lx\n",
7630 timeout, (timeleft / jiffies));
7631 retval = IOCB_TIMEDOUT;
7632 }
7633 } else {
7634 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7635 "0332 IOCB wait issue failed, Data x%x\n",
7636 retval);
7637 retval = IOCB_ERROR;
7638 }
7639
7640 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7641 creg_val = readl(phba->HCregaddr);
7642 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7643 writel(creg_val, phba->HCregaddr);
7644 readl(phba->HCregaddr); /* flush */
7645 }
7646
7647 if (prspiocbq)
7648 piocb->context2 = NULL;
7649
7650 piocb->context_un.wait_queue = NULL;
7651 piocb->iocb_cmpl = NULL;
7652 return retval;
7653 }
7654
7655 /**
7656 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7657 * @phba: Pointer to HBA context object.
7658 * @pmboxq: Pointer to driver mailbox object.
7659 * @timeout: Timeout in number of seconds.
7660 *
7661 * This function issues the mailbox to firmware and waits for the
7662 * mailbox command to complete. If the mailbox command is not
7663 * completed within timeout seconds, it returns MBX_TIMEOUT.
7664 * The function waits for the mailbox completion using an
7665 * interruptible wait. If the thread is woken up due to a
7666 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7667 * should not free the mailbox resources, if this function returns
7668 * MBX_TIMEOUT.
7669 * This function will sleep while waiting for mailbox completion.
7670 * So, this function should not be called from any context which
7671 * does not allow sleeping. Due to the same reason, this function
7672 * cannot be called with interrupt disabled.
7673 * This function assumes that the mailbox completion occurs while
7674 * this function sleep. So, this function cannot be called from
7675 * the worker thread which processes mailbox completion.
7676 * This function is called in the context of HBA management
7677 * applications.
7678 * This function returns MBX_SUCCESS when successful.
7679 * This function is called with no lock held.
7680 **/
7681 int
7682 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7683 uint32_t timeout)
7684 {
7685 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7686 int retval;
7687 unsigned long flag;
7688
7689 /* The caller must leave context1 empty. */
7690 if (pmboxq->context1)
7691 return MBX_NOT_FINISHED;
7692
7693 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7694 /* setup wake call as IOCB callback */
7695 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7696 /* setup context field to pass wait_queue pointer to wake function */
7697 pmboxq->context1 = &done_q;
7698
7699 /* now issue the command */
7700 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7701
7702 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7703 wait_event_interruptible_timeout(done_q,
7704 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7705 timeout * HZ);
7706
7707 spin_lock_irqsave(&phba->hbalock, flag);
7708 pmboxq->context1 = NULL;
7709 /*
7710 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7711 * else do not free the resources.
7712 */
7713 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7714 retval = MBX_SUCCESS;
7715 else {
7716 retval = MBX_TIMEOUT;
7717 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7718 }
7719 spin_unlock_irqrestore(&phba->hbalock, flag);
7720 }
7721
7722 return retval;
7723 }
7724
7725 /**
7726 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7727 * @phba: Pointer to HBA context.
7728 *
7729 * This function is called to shutdown the driver's mailbox sub-system.
7730 * It first marks the mailbox sub-system is in a block state to prevent
7731 * the asynchronous mailbox command from issued off the pending mailbox
7732 * command queue. If the mailbox command sub-system shutdown is due to
7733 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7734 * the mailbox sub-system flush routine to forcefully bring down the
7735 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7736 * as with offline or HBA function reset), this routine will wait for the
7737 * outstanding mailbox command to complete before invoking the mailbox
7738 * sub-system flush routine to gracefully bring down mailbox sub-system.
7739 **/
7740 void
7741 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7742 {
7743 struct lpfc_sli *psli = &phba->sli;
7744 uint8_t actcmd = MBX_HEARTBEAT;
7745 unsigned long timeout;
7746
7747 spin_lock_irq(&phba->hbalock);
7748 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7749 spin_unlock_irq(&phba->hbalock);
7750
7751 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7752 spin_lock_irq(&phba->hbalock);
7753 if (phba->sli.mbox_active)
7754 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7755 spin_unlock_irq(&phba->hbalock);
7756 /* Determine how long we might wait for the active mailbox
7757 * command to be gracefully completed by firmware.
7758 */
7759 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7760 1000) + jiffies;
7761 while (phba->sli.mbox_active) {
7762 /* Check active mailbox complete status every 2ms */
7763 msleep(2);
7764 if (time_after(jiffies, timeout))
7765 /* Timeout, let the mailbox flush routine to
7766 * forcefully release active mailbox command
7767 */
7768 break;
7769 }
7770 }
7771 lpfc_sli_mbox_sys_flush(phba);
7772 }
7773
7774 /**
7775 * lpfc_sli_eratt_read - read sli-3 error attention events
7776 * @phba: Pointer to HBA context.
7777 *
7778 * This function is called to read the SLI3 device error attention registers
7779 * for possible error attention events. The caller must hold the hostlock
7780 * with spin_lock_irq().
7781 *
7782 * This fucntion returns 1 when there is Error Attention in the Host Attention
7783 * Register and returns 0 otherwise.
7784 **/
7785 static int
7786 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7787 {
7788 uint32_t ha_copy;
7789
7790 /* Read chip Host Attention (HA) register */
7791 ha_copy = readl(phba->HAregaddr);
7792 if (ha_copy & HA_ERATT) {
7793 /* Read host status register to retrieve error event */
7794 lpfc_sli_read_hs(phba);
7795
7796 /* Check if there is a deferred error condition is active */
7797 if ((HS_FFER1 & phba->work_hs) &&
7798 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7799 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7800 phba->hba_flag |= DEFER_ERATT;
7801 /* Clear all interrupt enable conditions */
7802 writel(0, phba->HCregaddr);
7803 readl(phba->HCregaddr);
7804 }
7805
7806 /* Set the driver HA work bitmap */
7807 phba->work_ha |= HA_ERATT;
7808 /* Indicate polling handles this ERATT */
7809 phba->hba_flag |= HBA_ERATT_HANDLED;
7810 return 1;
7811 }
7812 return 0;
7813 }
7814
7815 /**
7816 * lpfc_sli4_eratt_read - read sli-4 error attention events
7817 * @phba: Pointer to HBA context.
7818 *
7819 * This function is called to read the SLI4 device error attention registers
7820 * for possible error attention events. The caller must hold the hostlock
7821 * with spin_lock_irq().
7822 *
7823 * This fucntion returns 1 when there is Error Attention in the Host Attention
7824 * Register and returns 0 otherwise.
7825 **/
7826 static int
7827 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7828 {
7829 uint32_t uerr_sta_hi, uerr_sta_lo;
7830
7831 /* For now, use the SLI4 device internal unrecoverable error
7832 * registers for error attention. This can be changed later.
7833 */
7834 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7835 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7836 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7837 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7839 "1423 HBA Unrecoverable error: "
7840 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7841 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7842 uerr_sta_lo, uerr_sta_hi,
7843 phba->sli4_hba.ue_mask_lo,
7844 phba->sli4_hba.ue_mask_hi);
7845 phba->work_status[0] = uerr_sta_lo;
7846 phba->work_status[1] = uerr_sta_hi;
7847 /* Set the driver HA work bitmap */
7848 phba->work_ha |= HA_ERATT;
7849 /* Indicate polling handles this ERATT */
7850 phba->hba_flag |= HBA_ERATT_HANDLED;
7851 return 1;
7852 }
7853 return 0;
7854 }
7855
7856 /**
7857 * lpfc_sli_check_eratt - check error attention events
7858 * @phba: Pointer to HBA context.
7859 *
7860 * This function is called from timer soft interrupt context to check HBA's
7861 * error attention register bit for error attention events.
7862 *
7863 * This fucntion returns 1 when there is Error Attention in the Host Attention
7864 * Register and returns 0 otherwise.
7865 **/
7866 int
7867 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7868 {
7869 uint32_t ha_copy;
7870
7871 /* If somebody is waiting to handle an eratt, don't process it
7872 * here. The brdkill function will do this.
7873 */
7874 if (phba->link_flag & LS_IGNORE_ERATT)
7875 return 0;
7876
7877 /* Check if interrupt handler handles this ERATT */
7878 spin_lock_irq(&phba->hbalock);
7879 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7880 /* Interrupt handler has handled ERATT */
7881 spin_unlock_irq(&phba->hbalock);
7882 return 0;
7883 }
7884
7885 /*
7886 * If there is deferred error attention, do not check for error
7887 * attention
7888 */
7889 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7890 spin_unlock_irq(&phba->hbalock);
7891 return 0;
7892 }
7893
7894 /* If PCI channel is offline, don't process it */
7895 if (unlikely(pci_channel_offline(phba->pcidev))) {
7896 spin_unlock_irq(&phba->hbalock);
7897 return 0;
7898 }
7899
7900 switch (phba->sli_rev) {
7901 case LPFC_SLI_REV2:
7902 case LPFC_SLI_REV3:
7903 /* Read chip Host Attention (HA) register */
7904 ha_copy = lpfc_sli_eratt_read(phba);
7905 break;
7906 case LPFC_SLI_REV4:
7907 /* Read devcie Uncoverable Error (UERR) registers */
7908 ha_copy = lpfc_sli4_eratt_read(phba);
7909 break;
7910 default:
7911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7912 "0299 Invalid SLI revision (%d)\n",
7913 phba->sli_rev);
7914 ha_copy = 0;
7915 break;
7916 }
7917 spin_unlock_irq(&phba->hbalock);
7918
7919 return ha_copy;
7920 }
7921
7922 /**
7923 * lpfc_intr_state_check - Check device state for interrupt handling
7924 * @phba: Pointer to HBA context.
7925 *
7926 * This inline routine checks whether a device or its PCI slot is in a state
7927 * that the interrupt should be handled.
7928 *
7929 * This function returns 0 if the device or the PCI slot is in a state that
7930 * interrupt should be handled, otherwise -EIO.
7931 */
7932 static inline int
7933 lpfc_intr_state_check(struct lpfc_hba *phba)
7934 {
7935 /* If the pci channel is offline, ignore all the interrupts */
7936 if (unlikely(pci_channel_offline(phba->pcidev)))
7937 return -EIO;
7938
7939 /* Update device level interrupt statistics */
7940 phba->sli.slistat.sli_intr++;
7941
7942 /* Ignore all interrupts during initialization. */
7943 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7944 return -EIO;
7945
7946 return 0;
7947 }
7948
7949 /**
7950 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7951 * @irq: Interrupt number.
7952 * @dev_id: The device context pointer.
7953 *
7954 * This function is directly called from the PCI layer as an interrupt
7955 * service routine when device with SLI-3 interface spec is enabled with
7956 * MSI-X multi-message interrupt mode and there are slow-path events in
7957 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7958 * interrupt mode, this function is called as part of the device-level
7959 * interrupt handler. When the PCI slot is in error recovery or the HBA
7960 * is undergoing initialization, the interrupt handler will not process
7961 * the interrupt. The link attention and ELS ring attention events are
7962 * handled by the worker thread. The interrupt handler signals the worker
7963 * thread and returns for these events. This function is called without
7964 * any lock held. It gets the hbalock to access and update SLI data
7965 * structures.
7966 *
7967 * This function returns IRQ_HANDLED when interrupt is handled else it
7968 * returns IRQ_NONE.
7969 **/
7970 irqreturn_t
7971 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7972 {
7973 struct lpfc_hba *phba;
7974 uint32_t ha_copy, hc_copy;
7975 uint32_t work_ha_copy;
7976 unsigned long status;
7977 unsigned long iflag;
7978 uint32_t control;
7979
7980 MAILBOX_t *mbox, *pmbox;
7981 struct lpfc_vport *vport;
7982 struct lpfc_nodelist *ndlp;
7983 struct lpfc_dmabuf *mp;
7984 LPFC_MBOXQ_t *pmb;
7985 int rc;
7986
7987 /*
7988 * Get the driver's phba structure from the dev_id and
7989 * assume the HBA is not interrupting.
7990 */
7991 phba = (struct lpfc_hba *)dev_id;
7992
7993 if (unlikely(!phba))
7994 return IRQ_NONE;
7995
7996 /*
7997 * Stuff needs to be attented to when this function is invoked as an
7998 * individual interrupt handler in MSI-X multi-message interrupt mode
7999 */
8000 if (phba->intr_type == MSIX) {
8001 /* Check device state for handling interrupt */
8002 if (lpfc_intr_state_check(phba))
8003 return IRQ_NONE;
8004 /* Need to read HA REG for slow-path events */
8005 spin_lock_irqsave(&phba->hbalock, iflag);
8006 ha_copy = readl(phba->HAregaddr);
8007 /* If somebody is waiting to handle an eratt don't process it
8008 * here. The brdkill function will do this.
8009 */
8010 if (phba->link_flag & LS_IGNORE_ERATT)
8011 ha_copy &= ~HA_ERATT;
8012 /* Check the need for handling ERATT in interrupt handler */
8013 if (ha_copy & HA_ERATT) {
8014 if (phba->hba_flag & HBA_ERATT_HANDLED)
8015 /* ERATT polling has handled ERATT */
8016 ha_copy &= ~HA_ERATT;
8017 else
8018 /* Indicate interrupt handler handles ERATT */
8019 phba->hba_flag |= HBA_ERATT_HANDLED;
8020 }
8021
8022 /*
8023 * If there is deferred error attention, do not check for any
8024 * interrupt.
8025 */
8026 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8027 spin_unlock_irqrestore(&phba->hbalock, iflag);
8028 return IRQ_NONE;
8029 }
8030
8031 /* Clear up only attention source related to slow-path */
8032 hc_copy = readl(phba->HCregaddr);
8033 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8034 HC_LAINT_ENA | HC_ERINT_ENA),
8035 phba->HCregaddr);
8036 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8037 phba->HAregaddr);
8038 writel(hc_copy, phba->HCregaddr);
8039 readl(phba->HAregaddr); /* flush */
8040 spin_unlock_irqrestore(&phba->hbalock, iflag);
8041 } else
8042 ha_copy = phba->ha_copy;
8043
8044 work_ha_copy = ha_copy & phba->work_ha_mask;
8045
8046 if (work_ha_copy) {
8047 if (work_ha_copy & HA_LATT) {
8048 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8049 /*
8050 * Turn off Link Attention interrupts
8051 * until CLEAR_LA done
8052 */
8053 spin_lock_irqsave(&phba->hbalock, iflag);
8054 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8055 control = readl(phba->HCregaddr);
8056 control &= ~HC_LAINT_ENA;
8057 writel(control, phba->HCregaddr);
8058 readl(phba->HCregaddr); /* flush */
8059 spin_unlock_irqrestore(&phba->hbalock, iflag);
8060 }
8061 else
8062 work_ha_copy &= ~HA_LATT;
8063 }
8064
8065 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8066 /*
8067 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8068 * the only slow ring.
8069 */
8070 status = (work_ha_copy &
8071 (HA_RXMASK << (4*LPFC_ELS_RING)));
8072 status >>= (4*LPFC_ELS_RING);
8073 if (status & HA_RXMASK) {
8074 spin_lock_irqsave(&phba->hbalock, iflag);
8075 control = readl(phba->HCregaddr);
8076
8077 lpfc_debugfs_slow_ring_trc(phba,
8078 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8079 control, status,
8080 (uint32_t)phba->sli.slistat.sli_intr);
8081
8082 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8083 lpfc_debugfs_slow_ring_trc(phba,
8084 "ISR Disable ring:"
8085 "pwork:x%x hawork:x%x wait:x%x",
8086 phba->work_ha, work_ha_copy,
8087 (uint32_t)((unsigned long)
8088 &phba->work_waitq));
8089
8090 control &=
8091 ~(HC_R0INT_ENA << LPFC_ELS_RING);
8092 writel(control, phba->HCregaddr);
8093 readl(phba->HCregaddr); /* flush */
8094 }
8095 else {
8096 lpfc_debugfs_slow_ring_trc(phba,
8097 "ISR slow ring: pwork:"
8098 "x%x hawork:x%x wait:x%x",
8099 phba->work_ha, work_ha_copy,
8100 (uint32_t)((unsigned long)
8101 &phba->work_waitq));
8102 }
8103 spin_unlock_irqrestore(&phba->hbalock, iflag);
8104 }
8105 }
8106 spin_lock_irqsave(&phba->hbalock, iflag);
8107 if (work_ha_copy & HA_ERATT) {
8108 lpfc_sli_read_hs(phba);
8109 /*
8110 * Check if there is a deferred error condition
8111 * is active
8112 */
8113 if ((HS_FFER1 & phba->work_hs) &&
8114 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8115 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8116 phba->hba_flag |= DEFER_ERATT;
8117 /* Clear all interrupt enable conditions */
8118 writel(0, phba->HCregaddr);
8119 readl(phba->HCregaddr);
8120 }
8121 }
8122
8123 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8124 pmb = phba->sli.mbox_active;
8125 pmbox = &pmb->u.mb;
8126 mbox = phba->mbox;
8127 vport = pmb->vport;
8128
8129 /* First check out the status word */
8130 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8131 if (pmbox->mbxOwner != OWN_HOST) {
8132 spin_unlock_irqrestore(&phba->hbalock, iflag);
8133 /*
8134 * Stray Mailbox Interrupt, mbxCommand <cmd>
8135 * mbxStatus <status>
8136 */
8137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8138 LOG_SLI,
8139 "(%d):0304 Stray Mailbox "
8140 "Interrupt mbxCommand x%x "
8141 "mbxStatus x%x\n",
8142 (vport ? vport->vpi : 0),
8143 pmbox->mbxCommand,
8144 pmbox->mbxStatus);
8145 /* clear mailbox attention bit */
8146 work_ha_copy &= ~HA_MBATT;
8147 } else {
8148 phba->sli.mbox_active = NULL;
8149 spin_unlock_irqrestore(&phba->hbalock, iflag);
8150 phba->last_completion_time = jiffies;
8151 del_timer(&phba->sli.mbox_tmo);
8152 if (pmb->mbox_cmpl) {
8153 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8154 MAILBOX_CMD_SIZE);
8155 if (pmb->out_ext_byte_len &&
8156 pmb->context2)
8157 lpfc_sli_pcimem_bcopy(
8158 phba->mbox_ext,
8159 pmb->context2,
8160 pmb->out_ext_byte_len);
8161 }
8162 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8163 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8164
8165 lpfc_debugfs_disc_trc(vport,
8166 LPFC_DISC_TRC_MBOX_VPORT,
8167 "MBOX dflt rpi: : "
8168 "status:x%x rpi:x%x",
8169 (uint32_t)pmbox->mbxStatus,
8170 pmbox->un.varWords[0], 0);
8171
8172 if (!pmbox->mbxStatus) {
8173 mp = (struct lpfc_dmabuf *)
8174 (pmb->context1);
8175 ndlp = (struct lpfc_nodelist *)
8176 pmb->context2;
8177
8178 /* Reg_LOGIN of dflt RPI was
8179 * successful. new lets get
8180 * rid of the RPI using the
8181 * same mbox buffer.
8182 */
8183 lpfc_unreg_login(phba,
8184 vport->vpi,
8185 pmbox->un.varWords[0],
8186 pmb);
8187 pmb->mbox_cmpl =
8188 lpfc_mbx_cmpl_dflt_rpi;
8189 pmb->context1 = mp;
8190 pmb->context2 = ndlp;
8191 pmb->vport = vport;
8192 rc = lpfc_sli_issue_mbox(phba,
8193 pmb,
8194 MBX_NOWAIT);
8195 if (rc != MBX_BUSY)
8196 lpfc_printf_log(phba,
8197 KERN_ERR,
8198 LOG_MBOX | LOG_SLI,
8199 "0350 rc should have"
8200 "been MBX_BUSY\n");
8201 if (rc != MBX_NOT_FINISHED)
8202 goto send_current_mbox;
8203 }
8204 }
8205 spin_lock_irqsave(
8206 &phba->pport->work_port_lock,
8207 iflag);
8208 phba->pport->work_port_events &=
8209 ~WORKER_MBOX_TMO;
8210 spin_unlock_irqrestore(
8211 &phba->pport->work_port_lock,
8212 iflag);
8213 lpfc_mbox_cmpl_put(phba, pmb);
8214 }
8215 } else
8216 spin_unlock_irqrestore(&phba->hbalock, iflag);
8217
8218 if ((work_ha_copy & HA_MBATT) &&
8219 (phba->sli.mbox_active == NULL)) {
8220 send_current_mbox:
8221 /* Process next mailbox command if there is one */
8222 do {
8223 rc = lpfc_sli_issue_mbox(phba, NULL,
8224 MBX_NOWAIT);
8225 } while (rc == MBX_NOT_FINISHED);
8226 if (rc != MBX_SUCCESS)
8227 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8228 LOG_SLI, "0349 rc should be "
8229 "MBX_SUCCESS\n");
8230 }
8231
8232 spin_lock_irqsave(&phba->hbalock, iflag);
8233 phba->work_ha |= work_ha_copy;
8234 spin_unlock_irqrestore(&phba->hbalock, iflag);
8235 lpfc_worker_wake_up(phba);
8236 }
8237 return IRQ_HANDLED;
8238
8239 } /* lpfc_sli_sp_intr_handler */
8240
8241 /**
8242 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8243 * @irq: Interrupt number.
8244 * @dev_id: The device context pointer.
8245 *
8246 * This function is directly called from the PCI layer as an interrupt
8247 * service routine when device with SLI-3 interface spec is enabled with
8248 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8249 * ring event in the HBA. However, when the device is enabled with either
8250 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8251 * device-level interrupt handler. When the PCI slot is in error recovery
8252 * or the HBA is undergoing initialization, the interrupt handler will not
8253 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8254 * the intrrupt context. This function is called without any lock held.
8255 * It gets the hbalock to access and update SLI data structures.
8256 *
8257 * This function returns IRQ_HANDLED when interrupt is handled else it
8258 * returns IRQ_NONE.
8259 **/
8260 irqreturn_t
8261 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8262 {
8263 struct lpfc_hba *phba;
8264 uint32_t ha_copy;
8265 unsigned long status;
8266 unsigned long iflag;
8267
8268 /* Get the driver's phba structure from the dev_id and
8269 * assume the HBA is not interrupting.
8270 */
8271 phba = (struct lpfc_hba *) dev_id;
8272
8273 if (unlikely(!phba))
8274 return IRQ_NONE;
8275
8276 /*
8277 * Stuff needs to be attented to when this function is invoked as an
8278 * individual interrupt handler in MSI-X multi-message interrupt mode
8279 */
8280 if (phba->intr_type == MSIX) {
8281 /* Check device state for handling interrupt */
8282 if (lpfc_intr_state_check(phba))
8283 return IRQ_NONE;
8284 /* Need to read HA REG for FCP ring and other ring events */
8285 ha_copy = readl(phba->HAregaddr);
8286 /* Clear up only attention source related to fast-path */
8287 spin_lock_irqsave(&phba->hbalock, iflag);
8288 /*
8289 * If there is deferred error attention, do not check for
8290 * any interrupt.
8291 */
8292 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8293 spin_unlock_irqrestore(&phba->hbalock, iflag);
8294 return IRQ_NONE;
8295 }
8296 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8297 phba->HAregaddr);
8298 readl(phba->HAregaddr); /* flush */
8299 spin_unlock_irqrestore(&phba->hbalock, iflag);
8300 } else
8301 ha_copy = phba->ha_copy;
8302
8303 /*
8304 * Process all events on FCP ring. Take the optimized path for FCP IO.
8305 */
8306 ha_copy &= ~(phba->work_ha_mask);
8307
8308 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8309 status >>= (4*LPFC_FCP_RING);
8310 if (status & HA_RXMASK)
8311 lpfc_sli_handle_fast_ring_event(phba,
8312 &phba->sli.ring[LPFC_FCP_RING],
8313 status);
8314
8315 if (phba->cfg_multi_ring_support == 2) {
8316 /*
8317 * Process all events on extra ring. Take the optimized path
8318 * for extra ring IO.
8319 */
8320 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8321 status >>= (4*LPFC_EXTRA_RING);
8322 if (status & HA_RXMASK) {
8323 lpfc_sli_handle_fast_ring_event(phba,
8324 &phba->sli.ring[LPFC_EXTRA_RING],
8325 status);
8326 }
8327 }
8328 return IRQ_HANDLED;
8329 } /* lpfc_sli_fp_intr_handler */
8330
8331 /**
8332 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8333 * @irq: Interrupt number.
8334 * @dev_id: The device context pointer.
8335 *
8336 * This function is the HBA device-level interrupt handler to device with
8337 * SLI-3 interface spec, called from the PCI layer when either MSI or
8338 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8339 * requires driver attention. This function invokes the slow-path interrupt
8340 * attention handling function and fast-path interrupt attention handling
8341 * function in turn to process the relevant HBA attention events. This
8342 * function is called without any lock held. It gets the hbalock to access
8343 * and update SLI data structures.
8344 *
8345 * This function returns IRQ_HANDLED when interrupt is handled, else it
8346 * returns IRQ_NONE.
8347 **/
8348 irqreturn_t
8349 lpfc_sli_intr_handler(int irq, void *dev_id)
8350 {
8351 struct lpfc_hba *phba;
8352 irqreturn_t sp_irq_rc, fp_irq_rc;
8353 unsigned long status1, status2;
8354 uint32_t hc_copy;
8355
8356 /*
8357 * Get the driver's phba structure from the dev_id and
8358 * assume the HBA is not interrupting.
8359 */
8360 phba = (struct lpfc_hba *) dev_id;
8361
8362 if (unlikely(!phba))
8363 return IRQ_NONE;
8364
8365 /* Check device state for handling interrupt */
8366 if (lpfc_intr_state_check(phba))
8367 return IRQ_NONE;
8368
8369 spin_lock(&phba->hbalock);
8370 phba->ha_copy = readl(phba->HAregaddr);
8371 if (unlikely(!phba->ha_copy)) {
8372 spin_unlock(&phba->hbalock);
8373 return IRQ_NONE;
8374 } else if (phba->ha_copy & HA_ERATT) {
8375 if (phba->hba_flag & HBA_ERATT_HANDLED)
8376 /* ERATT polling has handled ERATT */
8377 phba->ha_copy &= ~HA_ERATT;
8378 else
8379 /* Indicate interrupt handler handles ERATT */
8380 phba->hba_flag |= HBA_ERATT_HANDLED;
8381 }
8382
8383 /*
8384 * If there is deferred error attention, do not check for any interrupt.
8385 */
8386 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8387 spin_unlock_irq(&phba->hbalock);
8388 return IRQ_NONE;
8389 }
8390
8391 /* Clear attention sources except link and error attentions */
8392 hc_copy = readl(phba->HCregaddr);
8393 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8394 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8395 phba->HCregaddr);
8396 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8397 writel(hc_copy, phba->HCregaddr);
8398 readl(phba->HAregaddr); /* flush */
8399 spin_unlock(&phba->hbalock);
8400
8401 /*
8402 * Invokes slow-path host attention interrupt handling as appropriate.
8403 */
8404
8405 /* status of events with mailbox and link attention */
8406 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8407
8408 /* status of events with ELS ring */
8409 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8410 status2 >>= (4*LPFC_ELS_RING);
8411
8412 if (status1 || (status2 & HA_RXMASK))
8413 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8414 else
8415 sp_irq_rc = IRQ_NONE;
8416
8417 /*
8418 * Invoke fast-path host attention interrupt handling as appropriate.
8419 */
8420
8421 /* status of events with FCP ring */
8422 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8423 status1 >>= (4*LPFC_FCP_RING);
8424
8425 /* status of events with extra ring */
8426 if (phba->cfg_multi_ring_support == 2) {
8427 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8428 status2 >>= (4*LPFC_EXTRA_RING);
8429 } else
8430 status2 = 0;
8431
8432 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8433 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8434 else
8435 fp_irq_rc = IRQ_NONE;
8436
8437 /* Return device-level interrupt handling status */
8438 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8439 } /* lpfc_sli_intr_handler */
8440
8441 /**
8442 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8443 * @phba: pointer to lpfc hba data structure.
8444 *
8445 * This routine is invoked by the worker thread to process all the pending
8446 * SLI4 FCP abort XRI events.
8447 **/
8448 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8449 {
8450 struct lpfc_cq_event *cq_event;
8451
8452 /* First, declare the fcp xri abort event has been handled */
8453 spin_lock_irq(&phba->hbalock);
8454 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8455 spin_unlock_irq(&phba->hbalock);
8456 /* Now, handle all the fcp xri abort events */
8457 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8458 /* Get the first event from the head of the event queue */
8459 spin_lock_irq(&phba->hbalock);
8460 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8461 cq_event, struct lpfc_cq_event, list);
8462 spin_unlock_irq(&phba->hbalock);
8463 /* Notify aborted XRI for FCP work queue */
8464 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8465 /* Free the event processed back to the free pool */
8466 lpfc_sli4_cq_event_release(phba, cq_event);
8467 }
8468 }
8469
8470 /**
8471 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8472 * @phba: pointer to lpfc hba data structure.
8473 *
8474 * This routine is invoked by the worker thread to process all the pending
8475 * SLI4 els abort xri events.
8476 **/
8477 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8478 {
8479 struct lpfc_cq_event *cq_event;
8480
8481 /* First, declare the els xri abort event has been handled */
8482 spin_lock_irq(&phba->hbalock);
8483 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8484 spin_unlock_irq(&phba->hbalock);
8485 /* Now, handle all the els xri abort events */
8486 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8487 /* Get the first event from the head of the event queue */
8488 spin_lock_irq(&phba->hbalock);
8489 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8490 cq_event, struct lpfc_cq_event, list);
8491 spin_unlock_irq(&phba->hbalock);
8492 /* Notify aborted XRI for ELS work queue */
8493 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8494 /* Free the event processed back to the free pool */
8495 lpfc_sli4_cq_event_release(phba, cq_event);
8496 }
8497 }
8498
8499 /**
8500 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8501 * @phba: pointer to lpfc hba data structure
8502 * @pIocbIn: pointer to the rspiocbq
8503 * @pIocbOut: pointer to the cmdiocbq
8504 * @wcqe: pointer to the complete wcqe
8505 *
8506 * This routine transfers the fields of a command iocbq to a response iocbq
8507 * by copying all the IOCB fields from command iocbq and transferring the
8508 * completion status information from the complete wcqe.
8509 **/
8510 static void
8511 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8512 struct lpfc_iocbq *pIocbIn,
8513 struct lpfc_iocbq *pIocbOut,
8514 struct lpfc_wcqe_complete *wcqe)
8515 {
8516 unsigned long iflags;
8517 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8518
8519 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8520 sizeof(struct lpfc_iocbq) - offset);
8521 /* Map WCQE parameters into irspiocb parameters */
8522 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8523 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8524 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8525 pIocbIn->iocb.un.fcpi.fcpi_parm =
8526 pIocbOut->iocb.un.fcpi.fcpi_parm -
8527 wcqe->total_data_placed;
8528 else
8529 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8530 else {
8531 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8532 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8533 }
8534
8535 /* Pick up HBA exchange busy condition */
8536 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8537 spin_lock_irqsave(&phba->hbalock, iflags);
8538 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8539 spin_unlock_irqrestore(&phba->hbalock, iflags);
8540 }
8541 }
8542
8543 /**
8544 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8545 * @phba: Pointer to HBA context object.
8546 * @wcqe: Pointer to work-queue completion queue entry.
8547 *
8548 * This routine handles an ELS work-queue completion event and construct
8549 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8550 * discovery engine to handle.
8551 *
8552 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8553 **/
8554 static struct lpfc_iocbq *
8555 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8556 struct lpfc_iocbq *irspiocbq)
8557 {
8558 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8559 struct lpfc_iocbq *cmdiocbq;
8560 struct lpfc_wcqe_complete *wcqe;
8561 unsigned long iflags;
8562
8563 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8564 spin_lock_irqsave(&phba->hbalock, iflags);
8565 pring->stats.iocb_event++;
8566 /* Look up the ELS command IOCB and create pseudo response IOCB */
8567 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8568 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8569 spin_unlock_irqrestore(&phba->hbalock, iflags);
8570
8571 if (unlikely(!cmdiocbq)) {
8572 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8573 "0386 ELS complete with no corresponding "
8574 "cmdiocb: iotag (%d)\n",
8575 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8576 lpfc_sli_release_iocbq(phba, irspiocbq);
8577 return NULL;
8578 }
8579
8580 /* Fake the irspiocbq and copy necessary response information */
8581 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8582
8583 return irspiocbq;
8584 }
8585
8586 /**
8587 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8588 * @phba: Pointer to HBA context object.
8589 * @cqe: Pointer to mailbox completion queue entry.
8590 *
8591 * This routine process a mailbox completion queue entry with asynchrous
8592 * event.
8593 *
8594 * Return: true if work posted to worker thread, otherwise false.
8595 **/
8596 static bool
8597 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8598 {
8599 struct lpfc_cq_event *cq_event;
8600 unsigned long iflags;
8601
8602 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8603 "0392 Async Event: word0:x%x, word1:x%x, "
8604 "word2:x%x, word3:x%x\n", mcqe->word0,
8605 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8606
8607 /* Allocate a new internal CQ_EVENT entry */
8608 cq_event = lpfc_sli4_cq_event_alloc(phba);
8609 if (!cq_event) {
8610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8611 "0394 Failed to allocate CQ_EVENT entry\n");
8612 return false;
8613 }
8614
8615 /* Move the CQE into an asynchronous event entry */
8616 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8617 spin_lock_irqsave(&phba->hbalock, iflags);
8618 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8619 /* Set the async event flag */
8620 phba->hba_flag |= ASYNC_EVENT;
8621 spin_unlock_irqrestore(&phba->hbalock, iflags);
8622
8623 return true;
8624 }
8625
8626 /**
8627 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8628 * @phba: Pointer to HBA context object.
8629 * @cqe: Pointer to mailbox completion queue entry.
8630 *
8631 * This routine process a mailbox completion queue entry with mailbox
8632 * completion event.
8633 *
8634 * Return: true if work posted to worker thread, otherwise false.
8635 **/
8636 static bool
8637 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8638 {
8639 uint32_t mcqe_status;
8640 MAILBOX_t *mbox, *pmbox;
8641 struct lpfc_mqe *mqe;
8642 struct lpfc_vport *vport;
8643 struct lpfc_nodelist *ndlp;
8644 struct lpfc_dmabuf *mp;
8645 unsigned long iflags;
8646 LPFC_MBOXQ_t *pmb;
8647 bool workposted = false;
8648 int rc;
8649
8650 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8651 if (!bf_get(lpfc_trailer_completed, mcqe))
8652 goto out_no_mqe_complete;
8653
8654 /* Get the reference to the active mbox command */
8655 spin_lock_irqsave(&phba->hbalock, iflags);
8656 pmb = phba->sli.mbox_active;
8657 if (unlikely(!pmb)) {
8658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8659 "1832 No pending MBOX command to handle\n");
8660 spin_unlock_irqrestore(&phba->hbalock, iflags);
8661 goto out_no_mqe_complete;
8662 }
8663 spin_unlock_irqrestore(&phba->hbalock, iflags);
8664 mqe = &pmb->u.mqe;
8665 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8666 mbox = phba->mbox;
8667 vport = pmb->vport;
8668
8669 /* Reset heartbeat timer */
8670 phba->last_completion_time = jiffies;
8671 del_timer(&phba->sli.mbox_tmo);
8672
8673 /* Move mbox data to caller's mailbox region, do endian swapping */
8674 if (pmb->mbox_cmpl && mbox)
8675 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8676 /* Set the mailbox status with SLI4 range 0x4000 */
8677 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8678 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8679 bf_set(lpfc_mqe_status, mqe,
8680 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8681
8682 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8683 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8684 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8685 "MBOX dflt rpi: status:x%x rpi:x%x",
8686 mcqe_status,
8687 pmbox->un.varWords[0], 0);
8688 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8689 mp = (struct lpfc_dmabuf *)(pmb->context1);
8690 ndlp = (struct lpfc_nodelist *)pmb->context2;
8691 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8692 * RID of the PPI using the same mbox buffer.
8693 */
8694 lpfc_unreg_login(phba, vport->vpi,
8695 pmbox->un.varWords[0], pmb);
8696 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8697 pmb->context1 = mp;
8698 pmb->context2 = ndlp;
8699 pmb->vport = vport;
8700 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8701 if (rc != MBX_BUSY)
8702 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8703 LOG_SLI, "0385 rc should "
8704 "have been MBX_BUSY\n");
8705 if (rc != MBX_NOT_FINISHED)
8706 goto send_current_mbox;
8707 }
8708 }
8709 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8710 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8711 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8712
8713 /* There is mailbox completion work to do */
8714 spin_lock_irqsave(&phba->hbalock, iflags);
8715 __lpfc_mbox_cmpl_put(phba, pmb);
8716 phba->work_ha |= HA_MBATT;
8717 spin_unlock_irqrestore(&phba->hbalock, iflags);
8718 workposted = true;
8719
8720 send_current_mbox:
8721 spin_lock_irqsave(&phba->hbalock, iflags);
8722 /* Release the mailbox command posting token */
8723 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8724 /* Setting active mailbox pointer need to be in sync to flag clear */
8725 phba->sli.mbox_active = NULL;
8726 spin_unlock_irqrestore(&phba->hbalock, iflags);
8727 /* Wake up worker thread to post the next pending mailbox command */
8728 lpfc_worker_wake_up(phba);
8729 out_no_mqe_complete:
8730 if (bf_get(lpfc_trailer_consumed, mcqe))
8731 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8732 return workposted;
8733 }
8734
8735 /**
8736 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8737 * @phba: Pointer to HBA context object.
8738 * @cqe: Pointer to mailbox completion queue entry.
8739 *
8740 * This routine process a mailbox completion queue entry, it invokes the
8741 * proper mailbox complete handling or asynchrous event handling routine
8742 * according to the MCQE's async bit.
8743 *
8744 * Return: true if work posted to worker thread, otherwise false.
8745 **/
8746 static bool
8747 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8748 {
8749 struct lpfc_mcqe mcqe;
8750 bool workposted;
8751
8752 /* Copy the mailbox MCQE and convert endian order as needed */
8753 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8754
8755 /* Invoke the proper event handling routine */
8756 if (!bf_get(lpfc_trailer_async, &mcqe))
8757 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8758 else
8759 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8760 return workposted;
8761 }
8762
8763 /**
8764 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8765 * @phba: Pointer to HBA context object.
8766 * @wcqe: Pointer to work-queue completion queue entry.
8767 *
8768 * This routine handles an ELS work-queue completion event.
8769 *
8770 * Return: true if work posted to worker thread, otherwise false.
8771 **/
8772 static bool
8773 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8774 struct lpfc_wcqe_complete *wcqe)
8775 {
8776 struct lpfc_iocbq *irspiocbq;
8777 unsigned long iflags;
8778
8779 /* Get an irspiocbq for later ELS response processing use */
8780 irspiocbq = lpfc_sli_get_iocbq(phba);
8781 if (!irspiocbq) {
8782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8783 "0387 Failed to allocate an iocbq\n");
8784 return false;
8785 }
8786
8787 /* Save off the slow-path queue event for work thread to process */
8788 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8789 spin_lock_irqsave(&phba->hbalock, iflags);
8790 list_add_tail(&irspiocbq->cq_event.list,
8791 &phba->sli4_hba.sp_queue_event);
8792 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8793 spin_unlock_irqrestore(&phba->hbalock, iflags);
8794
8795 return true;
8796 }
8797
8798 /**
8799 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8800 * @phba: Pointer to HBA context object.
8801 * @wcqe: Pointer to work-queue completion queue entry.
8802 *
8803 * This routine handles slow-path WQ entry comsumed event by invoking the
8804 * proper WQ release routine to the slow-path WQ.
8805 **/
8806 static void
8807 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8808 struct lpfc_wcqe_release *wcqe)
8809 {
8810 /* Check for the slow-path ELS work queue */
8811 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8812 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8813 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8814 else
8815 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8816 "2579 Slow-path wqe consume event carries "
8817 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8818 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8819 phba->sli4_hba.els_wq->queue_id);
8820 }
8821
8822 /**
8823 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8824 * @phba: Pointer to HBA context object.
8825 * @cq: Pointer to a WQ completion queue.
8826 * @wcqe: Pointer to work-queue completion queue entry.
8827 *
8828 * This routine handles an XRI abort event.
8829 *
8830 * Return: true if work posted to worker thread, otherwise false.
8831 **/
8832 static bool
8833 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8834 struct lpfc_queue *cq,
8835 struct sli4_wcqe_xri_aborted *wcqe)
8836 {
8837 bool workposted = false;
8838 struct lpfc_cq_event *cq_event;
8839 unsigned long iflags;
8840
8841 /* Allocate a new internal CQ_EVENT entry */
8842 cq_event = lpfc_sli4_cq_event_alloc(phba);
8843 if (!cq_event) {
8844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8845 "0602 Failed to allocate CQ_EVENT entry\n");
8846 return false;
8847 }
8848
8849 /* Move the CQE into the proper xri abort event list */
8850 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8851 switch (cq->subtype) {
8852 case LPFC_FCP:
8853 spin_lock_irqsave(&phba->hbalock, iflags);
8854 list_add_tail(&cq_event->list,
8855 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8856 /* Set the fcp xri abort event flag */
8857 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8858 spin_unlock_irqrestore(&phba->hbalock, iflags);
8859 workposted = true;
8860 break;
8861 case LPFC_ELS:
8862 spin_lock_irqsave(&phba->hbalock, iflags);
8863 list_add_tail(&cq_event->list,
8864 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8865 /* Set the els xri abort event flag */
8866 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8867 spin_unlock_irqrestore(&phba->hbalock, iflags);
8868 workposted = true;
8869 break;
8870 default:
8871 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8872 "0603 Invalid work queue CQE subtype (x%x)\n",
8873 cq->subtype);
8874 workposted = false;
8875 break;
8876 }
8877 return workposted;
8878 }
8879
8880 /**
8881 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8882 * @phba: Pointer to HBA context object.
8883 * @rcqe: Pointer to receive-queue completion queue entry.
8884 *
8885 * This routine process a receive-queue completion queue entry.
8886 *
8887 * Return: true if work posted to worker thread, otherwise false.
8888 **/
8889 static bool
8890 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8891 {
8892 bool workposted = false;
8893 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8894 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8895 struct hbq_dmabuf *dma_buf;
8896 uint32_t status;
8897 unsigned long iflags;
8898
8899 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8900 goto out;
8901
8902 status = bf_get(lpfc_rcqe_status, rcqe);
8903 switch (status) {
8904 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8905 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8906 "2537 Receive Frame Truncated!!\n");
8907 case FC_STATUS_RQ_SUCCESS:
8908 lpfc_sli4_rq_release(hrq, drq);
8909 spin_lock_irqsave(&phba->hbalock, iflags);
8910 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8911 if (!dma_buf) {
8912 spin_unlock_irqrestore(&phba->hbalock, iflags);
8913 goto out;
8914 }
8915 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8916 /* save off the frame for the word thread to process */
8917 list_add_tail(&dma_buf->cq_event.list,
8918 &phba->sli4_hba.sp_queue_event);
8919 /* Frame received */
8920 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8921 spin_unlock_irqrestore(&phba->hbalock, iflags);
8922 workposted = true;
8923 break;
8924 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8925 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8926 /* Post more buffers if possible */
8927 spin_lock_irqsave(&phba->hbalock, iflags);
8928 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8929 spin_unlock_irqrestore(&phba->hbalock, iflags);
8930 workposted = true;
8931 break;
8932 }
8933 out:
8934 return workposted;
8935 }
8936
8937 /**
8938 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8939 * @phba: Pointer to HBA context object.
8940 * @cq: Pointer to the completion queue.
8941 * @wcqe: Pointer to a completion queue entry.
8942 *
8943 * This routine process a slow-path work-queue or recieve queue completion queue
8944 * entry.
8945 *
8946 * Return: true if work posted to worker thread, otherwise false.
8947 **/
8948 static bool
8949 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8950 struct lpfc_cqe *cqe)
8951 {
8952 struct lpfc_cqe cqevt;
8953 bool workposted = false;
8954
8955 /* Copy the work queue CQE and convert endian order if needed */
8956 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
8957
8958 /* Check and process for different type of WCQE and dispatch */
8959 switch (bf_get(lpfc_cqe_code, &cqevt)) {
8960 case CQE_CODE_COMPL_WQE:
8961 /* Process the WQ/RQ complete event */
8962 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8963 (struct lpfc_wcqe_complete *)&cqevt);
8964 break;
8965 case CQE_CODE_RELEASE_WQE:
8966 /* Process the WQ release event */
8967 lpfc_sli4_sp_handle_rel_wcqe(phba,
8968 (struct lpfc_wcqe_release *)&cqevt);
8969 break;
8970 case CQE_CODE_XRI_ABORTED:
8971 /* Process the WQ XRI abort event */
8972 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8973 (struct sli4_wcqe_xri_aborted *)&cqevt);
8974 break;
8975 case CQE_CODE_RECEIVE:
8976 /* Process the RQ event */
8977 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8978 (struct lpfc_rcqe *)&cqevt);
8979 break;
8980 default:
8981 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8982 "0388 Not a valid WCQE code: x%x\n",
8983 bf_get(lpfc_cqe_code, &cqevt));
8984 break;
8985 }
8986 return workposted;
8987 }
8988
8989 /**
8990 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8991 * @phba: Pointer to HBA context object.
8992 * @eqe: Pointer to fast-path event queue entry.
8993 *
8994 * This routine process a event queue entry from the slow-path event queue.
8995 * It will check the MajorCode and MinorCode to determine this is for a
8996 * completion event on a completion queue, if not, an error shall be logged
8997 * and just return. Otherwise, it will get to the corresponding completion
8998 * queue and process all the entries on that completion queue, rearm the
8999 * completion queue, and then return.
9000 *
9001 **/
9002 static void
9003 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9004 {
9005 struct lpfc_queue *cq = NULL, *childq, *speq;
9006 struct lpfc_cqe *cqe;
9007 bool workposted = false;
9008 int ecount = 0;
9009 uint16_t cqid;
9010
9011 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9013 "0359 Not a valid slow-path completion "
9014 "event: majorcode=x%x, minorcode=x%x\n",
9015 bf_get_le32(lpfc_eqe_major_code, eqe),
9016 bf_get_le32(lpfc_eqe_minor_code, eqe));
9017 return;
9018 }
9019
9020 /* Get the reference to the corresponding CQ */
9021 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9022
9023 /* Search for completion queue pointer matching this cqid */
9024 speq = phba->sli4_hba.sp_eq;
9025 list_for_each_entry(childq, &speq->child_list, list) {
9026 if (childq->queue_id == cqid) {
9027 cq = childq;
9028 break;
9029 }
9030 }
9031 if (unlikely(!cq)) {
9032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9033 "0365 Slow-path CQ identifier (%d) does "
9034 "not exist\n", cqid);
9035 return;
9036 }
9037
9038 /* Process all the entries to the CQ */
9039 switch (cq->type) {
9040 case LPFC_MCQ:
9041 while ((cqe = lpfc_sli4_cq_get(cq))) {
9042 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9043 if (!(++ecount % LPFC_GET_QE_REL_INT))
9044 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9045 }
9046 break;
9047 case LPFC_WCQ:
9048 while ((cqe = lpfc_sli4_cq_get(cq))) {
9049 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9050 if (!(++ecount % LPFC_GET_QE_REL_INT))
9051 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9052 }
9053 break;
9054 default:
9055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9056 "0370 Invalid completion queue type (%d)\n",
9057 cq->type);
9058 return;
9059 }
9060
9061 /* Catch the no cq entry condition, log an error */
9062 if (unlikely(ecount == 0))
9063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9064 "0371 No entry from the CQ: identifier "
9065 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9066
9067 /* In any case, flash and re-arm the RCQ */
9068 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9069
9070 /* wake up worker thread if there are works to be done */
9071 if (workposted)
9072 lpfc_worker_wake_up(phba);
9073 }
9074
9075 /**
9076 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9077 * @eqe: Pointer to fast-path completion queue entry.
9078 *
9079 * This routine process a fast-path work queue completion entry from fast-path
9080 * event queue for FCP command response completion.
9081 **/
9082 static void
9083 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9084 struct lpfc_wcqe_complete *wcqe)
9085 {
9086 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9087 struct lpfc_iocbq *cmdiocbq;
9088 struct lpfc_iocbq irspiocbq;
9089 unsigned long iflags;
9090
9091 spin_lock_irqsave(&phba->hbalock, iflags);
9092 pring->stats.iocb_event++;
9093 spin_unlock_irqrestore(&phba->hbalock, iflags);
9094
9095 /* Check for response status */
9096 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9097 /* If resource errors reported from HBA, reduce queue
9098 * depth of the SCSI device.
9099 */
9100 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9101 IOSTAT_LOCAL_REJECT) &&
9102 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9103 phba->lpfc_rampdown_queue_depth(phba);
9104 }
9105 /* Log the error status */
9106 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9107 "0373 FCP complete error: status=x%x, "
9108 "hw_status=x%x, total_data_specified=%d, "
9109 "parameter=x%x, word3=x%x\n",
9110 bf_get(lpfc_wcqe_c_status, wcqe),
9111 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9112 wcqe->total_data_placed, wcqe->parameter,
9113 wcqe->word3);
9114 }
9115
9116 /* Look up the FCP command IOCB and create pseudo response IOCB */
9117 spin_lock_irqsave(&phba->hbalock, iflags);
9118 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9119 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9120 spin_unlock_irqrestore(&phba->hbalock, iflags);
9121 if (unlikely(!cmdiocbq)) {
9122 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9123 "0374 FCP complete with no corresponding "
9124 "cmdiocb: iotag (%d)\n",
9125 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9126 return;
9127 }
9128 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9129 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9130 "0375 FCP cmdiocb not callback function "
9131 "iotag: (%d)\n",
9132 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9133 return;
9134 }
9135
9136 /* Fake the irspiocb and copy necessary response information */
9137 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9138
9139 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9140 spin_lock_irqsave(&phba->hbalock, iflags);
9141 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9142 spin_unlock_irqrestore(&phba->hbalock, iflags);
9143 }
9144
9145 /* Pass the cmd_iocb and the rsp state to the upper layer */
9146 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9147 }
9148
9149 /**
9150 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9151 * @phba: Pointer to HBA context object.
9152 * @cq: Pointer to completion queue.
9153 * @wcqe: Pointer to work-queue completion queue entry.
9154 *
9155 * This routine handles an fast-path WQ entry comsumed event by invoking the
9156 * proper WQ release routine to the slow-path WQ.
9157 **/
9158 static void
9159 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9160 struct lpfc_wcqe_release *wcqe)
9161 {
9162 struct lpfc_queue *childwq;
9163 bool wqid_matched = false;
9164 uint16_t fcp_wqid;
9165
9166 /* Check for fast-path FCP work queue release */
9167 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9168 list_for_each_entry(childwq, &cq->child_list, list) {
9169 if (childwq->queue_id == fcp_wqid) {
9170 lpfc_sli4_wq_release(childwq,
9171 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9172 wqid_matched = true;
9173 break;
9174 }
9175 }
9176 /* Report warning log message if no match found */
9177 if (wqid_matched != true)
9178 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9179 "2580 Fast-path wqe consume event carries "
9180 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9181 }
9182
9183 /**
9184 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9185 * @cq: Pointer to the completion queue.
9186 * @eqe: Pointer to fast-path completion queue entry.
9187 *
9188 * This routine process a fast-path work queue completion entry from fast-path
9189 * event queue for FCP command response completion.
9190 **/
9191 static int
9192 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9193 struct lpfc_cqe *cqe)
9194 {
9195 struct lpfc_wcqe_release wcqe;
9196 bool workposted = false;
9197
9198 /* Copy the work queue CQE and convert endian order if needed */
9199 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9200
9201 /* Check and process for different type of WCQE and dispatch */
9202 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9203 case CQE_CODE_COMPL_WQE:
9204 /* Process the WQ complete event */
9205 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9206 (struct lpfc_wcqe_complete *)&wcqe);
9207 break;
9208 case CQE_CODE_RELEASE_WQE:
9209 /* Process the WQ release event */
9210 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9211 (struct lpfc_wcqe_release *)&wcqe);
9212 break;
9213 case CQE_CODE_XRI_ABORTED:
9214 /* Process the WQ XRI abort event */
9215 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9216 (struct sli4_wcqe_xri_aborted *)&wcqe);
9217 break;
9218 default:
9219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9220 "0144 Not a valid WCQE code: x%x\n",
9221 bf_get(lpfc_wcqe_c_code, &wcqe));
9222 break;
9223 }
9224 return workposted;
9225 }
9226
9227 /**
9228 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9229 * @phba: Pointer to HBA context object.
9230 * @eqe: Pointer to fast-path event queue entry.
9231 *
9232 * This routine process a event queue entry from the fast-path event queue.
9233 * It will check the MajorCode and MinorCode to determine this is for a
9234 * completion event on a completion queue, if not, an error shall be logged
9235 * and just return. Otherwise, it will get to the corresponding completion
9236 * queue and process all the entries on the completion queue, rearm the
9237 * completion queue, and then return.
9238 **/
9239 static void
9240 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9241 uint32_t fcp_cqidx)
9242 {
9243 struct lpfc_queue *cq;
9244 struct lpfc_cqe *cqe;
9245 bool workposted = false;
9246 uint16_t cqid;
9247 int ecount = 0;
9248
9249 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9250 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9251 "0366 Not a valid fast-path completion "
9252 "event: majorcode=x%x, minorcode=x%x\n",
9253 bf_get_le32(lpfc_eqe_major_code, eqe),
9254 bf_get_le32(lpfc_eqe_minor_code, eqe));
9255 return;
9256 }
9257
9258 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9259 if (unlikely(!cq)) {
9260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9261 "0367 Fast-path completion queue does not "
9262 "exist\n");
9263 return;
9264 }
9265
9266 /* Get the reference to the corresponding CQ */
9267 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9268 if (unlikely(cqid != cq->queue_id)) {
9269 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9270 "0368 Miss-matched fast-path completion "
9271 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9272 cqid, cq->queue_id);
9273 return;
9274 }
9275
9276 /* Process all the entries to the CQ */
9277 while ((cqe = lpfc_sli4_cq_get(cq))) {
9278 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9279 if (!(++ecount % LPFC_GET_QE_REL_INT))
9280 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9281 }
9282
9283 /* Catch the no cq entry condition */
9284 if (unlikely(ecount == 0))
9285 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9286 "0369 No entry from fast-path completion "
9287 "queue fcpcqid=%d\n", cq->queue_id);
9288
9289 /* In any case, flash and re-arm the CQ */
9290 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9291
9292 /* wake up worker thread if there are works to be done */
9293 if (workposted)
9294 lpfc_worker_wake_up(phba);
9295 }
9296
9297 static void
9298 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9299 {
9300 struct lpfc_eqe *eqe;
9301
9302 /* walk all the EQ entries and drop on the floor */
9303 while ((eqe = lpfc_sli4_eq_get(eq)))
9304 ;
9305
9306 /* Clear and re-arm the EQ */
9307 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9308 }
9309
9310 /**
9311 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9312 * @irq: Interrupt number.
9313 * @dev_id: The device context pointer.
9314 *
9315 * This function is directly called from the PCI layer as an interrupt
9316 * service routine when device with SLI-4 interface spec is enabled with
9317 * MSI-X multi-message interrupt mode and there are slow-path events in
9318 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9319 * interrupt mode, this function is called as part of the device-level
9320 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9321 * undergoing initialization, the interrupt handler will not process the
9322 * interrupt. The link attention and ELS ring attention events are handled
9323 * by the worker thread. The interrupt handler signals the worker thread
9324 * and returns for these events. This function is called without any lock
9325 * held. It gets the hbalock to access and update SLI data structures.
9326 *
9327 * This function returns IRQ_HANDLED when interrupt is handled else it
9328 * returns IRQ_NONE.
9329 **/
9330 irqreturn_t
9331 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9332 {
9333 struct lpfc_hba *phba;
9334 struct lpfc_queue *speq;
9335 struct lpfc_eqe *eqe;
9336 unsigned long iflag;
9337 int ecount = 0;
9338
9339 /*
9340 * Get the driver's phba structure from the dev_id
9341 */
9342 phba = (struct lpfc_hba *)dev_id;
9343
9344 if (unlikely(!phba))
9345 return IRQ_NONE;
9346
9347 /* Get to the EQ struct associated with this vector */
9348 speq = phba->sli4_hba.sp_eq;
9349
9350 /* Check device state for handling interrupt */
9351 if (unlikely(lpfc_intr_state_check(phba))) {
9352 /* Check again for link_state with lock held */
9353 spin_lock_irqsave(&phba->hbalock, iflag);
9354 if (phba->link_state < LPFC_LINK_DOWN)
9355 /* Flush, clear interrupt, and rearm the EQ */
9356 lpfc_sli4_eq_flush(phba, speq);
9357 spin_unlock_irqrestore(&phba->hbalock, iflag);
9358 return IRQ_NONE;
9359 }
9360
9361 /*
9362 * Process all the event on FCP slow-path EQ
9363 */
9364 while ((eqe = lpfc_sli4_eq_get(speq))) {
9365 lpfc_sli4_sp_handle_eqe(phba, eqe);
9366 if (!(++ecount % LPFC_GET_QE_REL_INT))
9367 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9368 }
9369
9370 /* Always clear and re-arm the slow-path EQ */
9371 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9372
9373 /* Catch the no cq entry condition */
9374 if (unlikely(ecount == 0)) {
9375 if (phba->intr_type == MSIX)
9376 /* MSI-X treated interrupt served as no EQ share INT */
9377 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9378 "0357 MSI-X interrupt with no EQE\n");
9379 else
9380 /* Non MSI-X treated on interrupt as EQ share INT */
9381 return IRQ_NONE;
9382 }
9383
9384 return IRQ_HANDLED;
9385 } /* lpfc_sli4_sp_intr_handler */
9386
9387 /**
9388 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9389 * @irq: Interrupt number.
9390 * @dev_id: The device context pointer.
9391 *
9392 * This function is directly called from the PCI layer as an interrupt
9393 * service routine when device with SLI-4 interface spec is enabled with
9394 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9395 * ring event in the HBA. However, when the device is enabled with either
9396 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9397 * device-level interrupt handler. When the PCI slot is in error recovery
9398 * or the HBA is undergoing initialization, the interrupt handler will not
9399 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9400 * the intrrupt context. This function is called without any lock held.
9401 * It gets the hbalock to access and update SLI data structures. Note that,
9402 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9403 * equal to that of FCP CQ index.
9404 *
9405 * This function returns IRQ_HANDLED when interrupt is handled else it
9406 * returns IRQ_NONE.
9407 **/
9408 irqreturn_t
9409 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9410 {
9411 struct lpfc_hba *phba;
9412 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9413 struct lpfc_queue *fpeq;
9414 struct lpfc_eqe *eqe;
9415 unsigned long iflag;
9416 int ecount = 0;
9417 uint32_t fcp_eqidx;
9418
9419 /* Get the driver's phba structure from the dev_id */
9420 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9421 phba = fcp_eq_hdl->phba;
9422 fcp_eqidx = fcp_eq_hdl->idx;
9423
9424 if (unlikely(!phba))
9425 return IRQ_NONE;
9426
9427 /* Get to the EQ struct associated with this vector */
9428 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9429
9430 /* Check device state for handling interrupt */
9431 if (unlikely(lpfc_intr_state_check(phba))) {
9432 /* Check again for link_state with lock held */
9433 spin_lock_irqsave(&phba->hbalock, iflag);
9434 if (phba->link_state < LPFC_LINK_DOWN)
9435 /* Flush, clear interrupt, and rearm the EQ */
9436 lpfc_sli4_eq_flush(phba, fpeq);
9437 spin_unlock_irqrestore(&phba->hbalock, iflag);
9438 return IRQ_NONE;
9439 }
9440
9441 /*
9442 * Process all the event on FCP fast-path EQ
9443 */
9444 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9445 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9446 if (!(++ecount % LPFC_GET_QE_REL_INT))
9447 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9448 }
9449
9450 /* Always clear and re-arm the fast-path EQ */
9451 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9452
9453 if (unlikely(ecount == 0)) {
9454 if (phba->intr_type == MSIX)
9455 /* MSI-X treated interrupt served as no EQ share INT */
9456 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9457 "0358 MSI-X interrupt with no EQE\n");
9458 else
9459 /* Non MSI-X treated on interrupt as EQ share INT */
9460 return IRQ_NONE;
9461 }
9462
9463 return IRQ_HANDLED;
9464 } /* lpfc_sli4_fp_intr_handler */
9465
9466 /**
9467 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9468 * @irq: Interrupt number.
9469 * @dev_id: The device context pointer.
9470 *
9471 * This function is the device-level interrupt handler to device with SLI-4
9472 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9473 * interrupt mode is enabled and there is an event in the HBA which requires
9474 * driver attention. This function invokes the slow-path interrupt attention
9475 * handling function and fast-path interrupt attention handling function in
9476 * turn to process the relevant HBA attention events. This function is called
9477 * without any lock held. It gets the hbalock to access and update SLI data
9478 * structures.
9479 *
9480 * This function returns IRQ_HANDLED when interrupt is handled, else it
9481 * returns IRQ_NONE.
9482 **/
9483 irqreturn_t
9484 lpfc_sli4_intr_handler(int irq, void *dev_id)
9485 {
9486 struct lpfc_hba *phba;
9487 irqreturn_t sp_irq_rc, fp_irq_rc;
9488 bool fp_handled = false;
9489 uint32_t fcp_eqidx;
9490
9491 /* Get the driver's phba structure from the dev_id */
9492 phba = (struct lpfc_hba *)dev_id;
9493
9494 if (unlikely(!phba))
9495 return IRQ_NONE;
9496
9497 /*
9498 * Invokes slow-path host attention interrupt handling as appropriate.
9499 */
9500 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9501
9502 /*
9503 * Invoke fast-path host attention interrupt handling as appropriate.
9504 */
9505 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9506 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9507 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9508 if (fp_irq_rc == IRQ_HANDLED)
9509 fp_handled |= true;
9510 }
9511
9512 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9513 } /* lpfc_sli4_intr_handler */
9514
9515 /**
9516 * lpfc_sli4_queue_free - free a queue structure and associated memory
9517 * @queue: The queue structure to free.
9518 *
9519 * This function frees a queue structure and the DMAable memeory used for
9520 * the host resident queue. This function must be called after destroying the
9521 * queue on the HBA.
9522 **/
9523 void
9524 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9525 {
9526 struct lpfc_dmabuf *dmabuf;
9527
9528 if (!queue)
9529 return;
9530
9531 while (!list_empty(&queue->page_list)) {
9532 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9533 list);
9534 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
9535 dmabuf->virt, dmabuf->phys);
9536 kfree(dmabuf);
9537 }
9538 kfree(queue);
9539 return;
9540 }
9541
9542 /**
9543 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9544 * @phba: The HBA that this queue is being created on.
9545 * @entry_size: The size of each queue entry for this queue.
9546 * @entry count: The number of entries that this queue will handle.
9547 *
9548 * This function allocates a queue structure and the DMAable memory used for
9549 * the host resident queue. This function must be called before creating the
9550 * queue on the HBA.
9551 **/
9552 struct lpfc_queue *
9553 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9554 uint32_t entry_count)
9555 {
9556 struct lpfc_queue *queue;
9557 struct lpfc_dmabuf *dmabuf;
9558 int x, total_qe_count;
9559 void *dma_pointer;
9560 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9561
9562 if (!phba->sli4_hba.pc_sli4_params.supported)
9563 hw_page_size = SLI4_PAGE_SIZE;
9564
9565 queue = kzalloc(sizeof(struct lpfc_queue) +
9566 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9567 if (!queue)
9568 return NULL;
9569 queue->page_count = (ALIGN(entry_size * entry_count,
9570 hw_page_size))/hw_page_size;
9571 INIT_LIST_HEAD(&queue->list);
9572 INIT_LIST_HEAD(&queue->page_list);
9573 INIT_LIST_HEAD(&queue->child_list);
9574 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9575 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9576 if (!dmabuf)
9577 goto out_fail;
9578 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9579 hw_page_size, &dmabuf->phys,
9580 GFP_KERNEL);
9581 if (!dmabuf->virt) {
9582 kfree(dmabuf);
9583 goto out_fail;
9584 }
9585 memset(dmabuf->virt, 0, hw_page_size);
9586 dmabuf->buffer_tag = x;
9587 list_add_tail(&dmabuf->list, &queue->page_list);
9588 /* initialize queue's entry array */
9589 dma_pointer = dmabuf->virt;
9590 for (; total_qe_count < entry_count &&
9591 dma_pointer < (hw_page_size + dmabuf->virt);
9592 total_qe_count++, dma_pointer += entry_size) {
9593 queue->qe[total_qe_count].address = dma_pointer;
9594 }
9595 }
9596 queue->entry_size = entry_size;
9597 queue->entry_count = entry_count;
9598 queue->phba = phba;
9599
9600 return queue;
9601 out_fail:
9602 lpfc_sli4_queue_free(queue);
9603 return NULL;
9604 }
9605
9606 /**
9607 * lpfc_eq_create - Create an Event Queue on the HBA
9608 * @phba: HBA structure that indicates port to create a queue on.
9609 * @eq: The queue structure to use to create the event queue.
9610 * @imax: The maximum interrupt per second limit.
9611 *
9612 * This function creates an event queue, as detailed in @eq, on a port,
9613 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9614 *
9615 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9616 * is used to get the entry count and entry size that are necessary to
9617 * determine the number of pages to allocate and use for this queue. This
9618 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9619 * event queue. This function is asynchronous and will wait for the mailbox
9620 * command to finish before continuing.
9621 *
9622 * On success this function will return a zero. If unable to allocate enough
9623 * memory this function will return ENOMEM. If the queue create mailbox command
9624 * fails this function will return ENXIO.
9625 **/
9626 uint32_t
9627 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9628 {
9629 struct lpfc_mbx_eq_create *eq_create;
9630 LPFC_MBOXQ_t *mbox;
9631 int rc, length, status = 0;
9632 struct lpfc_dmabuf *dmabuf;
9633 uint32_t shdr_status, shdr_add_status;
9634 union lpfc_sli4_cfg_shdr *shdr;
9635 uint16_t dmult;
9636 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9637
9638 if (!phba->sli4_hba.pc_sli4_params.supported)
9639 hw_page_size = SLI4_PAGE_SIZE;
9640
9641 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9642 if (!mbox)
9643 return -ENOMEM;
9644 length = (sizeof(struct lpfc_mbx_eq_create) -
9645 sizeof(struct lpfc_sli4_cfg_mhdr));
9646 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9647 LPFC_MBOX_OPCODE_EQ_CREATE,
9648 length, LPFC_SLI4_MBX_EMBED);
9649 eq_create = &mbox->u.mqe.un.eq_create;
9650 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9651 eq->page_count);
9652 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9653 LPFC_EQE_SIZE);
9654 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9655 /* Calculate delay multiper from maximum interrupt per second */
9656 dmult = LPFC_DMULT_CONST/imax - 1;
9657 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9658 dmult);
9659 switch (eq->entry_count) {
9660 default:
9661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9662 "0360 Unsupported EQ count. (%d)\n",
9663 eq->entry_count);
9664 if (eq->entry_count < 256)
9665 return -EINVAL;
9666 /* otherwise default to smallest count (drop through) */
9667 case 256:
9668 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9669 LPFC_EQ_CNT_256);
9670 break;
9671 case 512:
9672 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9673 LPFC_EQ_CNT_512);
9674 break;
9675 case 1024:
9676 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9677 LPFC_EQ_CNT_1024);
9678 break;
9679 case 2048:
9680 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9681 LPFC_EQ_CNT_2048);
9682 break;
9683 case 4096:
9684 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9685 LPFC_EQ_CNT_4096);
9686 break;
9687 }
9688 list_for_each_entry(dmabuf, &eq->page_list, list) {
9689 memset(dmabuf->virt, 0, hw_page_size);
9690 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9691 putPaddrLow(dmabuf->phys);
9692 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9693 putPaddrHigh(dmabuf->phys);
9694 }
9695 mbox->vport = phba->pport;
9696 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9697 mbox->context1 = NULL;
9698 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9699 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9702 if (shdr_status || shdr_add_status || rc) {
9703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9704 "2500 EQ_CREATE mailbox failed with "
9705 "status x%x add_status x%x, mbx status x%x\n",
9706 shdr_status, shdr_add_status, rc);
9707 status = -ENXIO;
9708 }
9709 eq->type = LPFC_EQ;
9710 eq->subtype = LPFC_NONE;
9711 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9712 if (eq->queue_id == 0xFFFF)
9713 status = -ENXIO;
9714 eq->host_index = 0;
9715 eq->hba_index = 0;
9716
9717 mempool_free(mbox, phba->mbox_mem_pool);
9718 return status;
9719 }
9720
9721 /**
9722 * lpfc_cq_create - Create a Completion Queue on the HBA
9723 * @phba: HBA structure that indicates port to create a queue on.
9724 * @cq: The queue structure to use to create the completion queue.
9725 * @eq: The event queue to bind this completion queue to.
9726 *
9727 * This function creates a completion queue, as detailed in @wq, on a port,
9728 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9729 *
9730 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9731 * is used to get the entry count and entry size that are necessary to
9732 * determine the number of pages to allocate and use for this queue. The @eq
9733 * is used to indicate which event queue to bind this completion queue to. This
9734 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9735 * completion queue. This function is asynchronous and will wait for the mailbox
9736 * command to finish before continuing.
9737 *
9738 * On success this function will return a zero. If unable to allocate enough
9739 * memory this function will return ENOMEM. If the queue create mailbox command
9740 * fails this function will return ENXIO.
9741 **/
9742 uint32_t
9743 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9744 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9745 {
9746 struct lpfc_mbx_cq_create *cq_create;
9747 struct lpfc_dmabuf *dmabuf;
9748 LPFC_MBOXQ_t *mbox;
9749 int rc, length, status = 0;
9750 uint32_t shdr_status, shdr_add_status;
9751 union lpfc_sli4_cfg_shdr *shdr;
9752 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9753
9754 if (!phba->sli4_hba.pc_sli4_params.supported)
9755 hw_page_size = SLI4_PAGE_SIZE;
9756
9757
9758 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9759 if (!mbox)
9760 return -ENOMEM;
9761 length = (sizeof(struct lpfc_mbx_cq_create) -
9762 sizeof(struct lpfc_sli4_cfg_mhdr));
9763 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9764 LPFC_MBOX_OPCODE_CQ_CREATE,
9765 length, LPFC_SLI4_MBX_EMBED);
9766 cq_create = &mbox->u.mqe.un.cq_create;
9767 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9768 cq->page_count);
9769 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9770 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9771 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9772 switch (cq->entry_count) {
9773 default:
9774 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9775 "0361 Unsupported CQ count. (%d)\n",
9776 cq->entry_count);
9777 if (cq->entry_count < 256)
9778 return -EINVAL;
9779 /* otherwise default to smallest count (drop through) */
9780 case 256:
9781 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9782 LPFC_CQ_CNT_256);
9783 break;
9784 case 512:
9785 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9786 LPFC_CQ_CNT_512);
9787 break;
9788 case 1024:
9789 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9790 LPFC_CQ_CNT_1024);
9791 break;
9792 }
9793 list_for_each_entry(dmabuf, &cq->page_list, list) {
9794 memset(dmabuf->virt, 0, hw_page_size);
9795 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9796 putPaddrLow(dmabuf->phys);
9797 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9798 putPaddrHigh(dmabuf->phys);
9799 }
9800 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9801
9802 /* The IOCTL status is embedded in the mailbox subheader. */
9803 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9804 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9805 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9806 if (shdr_status || shdr_add_status || rc) {
9807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9808 "2501 CQ_CREATE mailbox failed with "
9809 "status x%x add_status x%x, mbx status x%x\n",
9810 shdr_status, shdr_add_status, rc);
9811 status = -ENXIO;
9812 goto out;
9813 }
9814 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9815 if (cq->queue_id == 0xFFFF) {
9816 status = -ENXIO;
9817 goto out;
9818 }
9819 /* link the cq onto the parent eq child list */
9820 list_add_tail(&cq->list, &eq->child_list);
9821 /* Set up completion queue's type and subtype */
9822 cq->type = type;
9823 cq->subtype = subtype;
9824 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9825 cq->host_index = 0;
9826 cq->hba_index = 0;
9827
9828 out:
9829 mempool_free(mbox, phba->mbox_mem_pool);
9830 return status;
9831 }
9832
9833 /**
9834 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9835 * @phba: HBA structure that indicates port to create a queue on.
9836 * @mq: The queue structure to use to create the mailbox queue.
9837 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9838 * @cq: The completion queue to associate with this cq.
9839 *
9840 * This function provides failback (fb) functionality when the
9841 * mq_create_ext fails on older FW generations. It's purpose is identical
9842 * to mq_create_ext otherwise.
9843 *
9844 * This routine cannot fail as all attributes were previously accessed and
9845 * initialized in mq_create_ext.
9846 **/
9847 static void
9848 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9849 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9850 {
9851 struct lpfc_mbx_mq_create *mq_create;
9852 struct lpfc_dmabuf *dmabuf;
9853 int length;
9854
9855 length = (sizeof(struct lpfc_mbx_mq_create) -
9856 sizeof(struct lpfc_sli4_cfg_mhdr));
9857 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9858 LPFC_MBOX_OPCODE_MQ_CREATE,
9859 length, LPFC_SLI4_MBX_EMBED);
9860 mq_create = &mbox->u.mqe.un.mq_create;
9861 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9862 mq->page_count);
9863 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9864 cq->queue_id);
9865 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9866 switch (mq->entry_count) {
9867 case 16:
9868 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9869 LPFC_MQ_CNT_16);
9870 break;
9871 case 32:
9872 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9873 LPFC_MQ_CNT_32);
9874 break;
9875 case 64:
9876 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9877 LPFC_MQ_CNT_64);
9878 break;
9879 case 128:
9880 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9881 LPFC_MQ_CNT_128);
9882 break;
9883 }
9884 list_for_each_entry(dmabuf, &mq->page_list, list) {
9885 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9886 putPaddrLow(dmabuf->phys);
9887 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9888 putPaddrHigh(dmabuf->phys);
9889 }
9890 }
9891
9892 /**
9893 * lpfc_mq_create - Create a mailbox Queue on the HBA
9894 * @phba: HBA structure that indicates port to create a queue on.
9895 * @mq: The queue structure to use to create the mailbox queue.
9896 * @cq: The completion queue to associate with this cq.
9897 * @subtype: The queue's subtype.
9898 *
9899 * This function creates a mailbox queue, as detailed in @mq, on a port,
9900 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9901 *
9902 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9903 * is used to get the entry count and entry size that are necessary to
9904 * determine the number of pages to allocate and use for this queue. This
9905 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9906 * mailbox queue. This function is asynchronous and will wait for the mailbox
9907 * command to finish before continuing.
9908 *
9909 * On success this function will return a zero. If unable to allocate enough
9910 * memory this function will return ENOMEM. If the queue create mailbox command
9911 * fails this function will return ENXIO.
9912 **/
9913 int32_t
9914 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9915 struct lpfc_queue *cq, uint32_t subtype)
9916 {
9917 struct lpfc_mbx_mq_create *mq_create;
9918 struct lpfc_mbx_mq_create_ext *mq_create_ext;
9919 struct lpfc_dmabuf *dmabuf;
9920 LPFC_MBOXQ_t *mbox;
9921 int rc, length, status = 0;
9922 uint32_t shdr_status, shdr_add_status;
9923 union lpfc_sli4_cfg_shdr *shdr;
9924 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9925
9926 if (!phba->sli4_hba.pc_sli4_params.supported)
9927 hw_page_size = SLI4_PAGE_SIZE;
9928
9929 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9930 if (!mbox)
9931 return -ENOMEM;
9932 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
9933 sizeof(struct lpfc_sli4_cfg_mhdr));
9934 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9935 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
9936 length, LPFC_SLI4_MBX_EMBED);
9937
9938 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
9939 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
9940 mq->page_count);
9941 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
9942 1);
9943 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
9944 &mq_create_ext->u.request, 1);
9945 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
9946 &mq_create_ext->u.request, 1);
9947 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
9948 cq->queue_id);
9949 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
9950 switch (mq->entry_count) {
9951 default:
9952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9953 "0362 Unsupported MQ count. (%d)\n",
9954 mq->entry_count);
9955 if (mq->entry_count < 16)
9956 return -EINVAL;
9957 /* otherwise default to smallest count (drop through) */
9958 case 16:
9959 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9960 LPFC_MQ_CNT_16);
9961 break;
9962 case 32:
9963 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9964 LPFC_MQ_CNT_32);
9965 break;
9966 case 64:
9967 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9968 LPFC_MQ_CNT_64);
9969 break;
9970 case 128:
9971 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
9972 LPFC_MQ_CNT_128);
9973 break;
9974 }
9975 list_for_each_entry(dmabuf, &mq->page_list, list) {
9976 memset(dmabuf->virt, 0, hw_page_size);
9977 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
9978 putPaddrLow(dmabuf->phys);
9979 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
9980 putPaddrHigh(dmabuf->phys);
9981 }
9982 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9983 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
9984 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9985 &mq_create_ext->u.response);
9986 if (rc != MBX_SUCCESS) {
9987 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9988 "2795 MQ_CREATE_EXT failed with "
9989 "status x%x. Failback to MQ_CREATE.\n",
9990 rc);
9991 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
9992 mq_create = &mbox->u.mqe.un.mq_create;
9993 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9994 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9995 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9996 &mq_create->u.response);
9997 }
9998
9999 /* The IOCTL status is embedded in the mailbox subheader. */
10000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10002 if (shdr_status || shdr_add_status || rc) {
10003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10004 "2502 MQ_CREATE mailbox failed with "
10005 "status x%x add_status x%x, mbx status x%x\n",
10006 shdr_status, shdr_add_status, rc);
10007 status = -ENXIO;
10008 goto out;
10009 }
10010 if (mq->queue_id == 0xFFFF) {
10011 status = -ENXIO;
10012 goto out;
10013 }
10014 mq->type = LPFC_MQ;
10015 mq->subtype = subtype;
10016 mq->host_index = 0;
10017 mq->hba_index = 0;
10018
10019 /* link the mq onto the parent cq child list */
10020 list_add_tail(&mq->list, &cq->child_list);
10021 out:
10022 mempool_free(mbox, phba->mbox_mem_pool);
10023 return status;
10024 }
10025
10026 /**
10027 * lpfc_wq_create - Create a Work Queue on the HBA
10028 * @phba: HBA structure that indicates port to create a queue on.
10029 * @wq: The queue structure to use to create the work queue.
10030 * @cq: The completion queue to bind this work queue to.
10031 * @subtype: The subtype of the work queue indicating its functionality.
10032 *
10033 * This function creates a work queue, as detailed in @wq, on a port, described
10034 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10035 *
10036 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10037 * is used to get the entry count and entry size that are necessary to
10038 * determine the number of pages to allocate and use for this queue. The @cq
10039 * is used to indicate which completion queue to bind this work queue to. This
10040 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10041 * work queue. This function is asynchronous and will wait for the mailbox
10042 * command to finish before continuing.
10043 *
10044 * On success this function will return a zero. If unable to allocate enough
10045 * memory this function will return ENOMEM. If the queue create mailbox command
10046 * fails this function will return ENXIO.
10047 **/
10048 uint32_t
10049 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10050 struct lpfc_queue *cq, uint32_t subtype)
10051 {
10052 struct lpfc_mbx_wq_create *wq_create;
10053 struct lpfc_dmabuf *dmabuf;
10054 LPFC_MBOXQ_t *mbox;
10055 int rc, length, status = 0;
10056 uint32_t shdr_status, shdr_add_status;
10057 union lpfc_sli4_cfg_shdr *shdr;
10058 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10059
10060 if (!phba->sli4_hba.pc_sli4_params.supported)
10061 hw_page_size = SLI4_PAGE_SIZE;
10062
10063 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10064 if (!mbox)
10065 return -ENOMEM;
10066 length = (sizeof(struct lpfc_mbx_wq_create) -
10067 sizeof(struct lpfc_sli4_cfg_mhdr));
10068 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10069 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10070 length, LPFC_SLI4_MBX_EMBED);
10071 wq_create = &mbox->u.mqe.un.wq_create;
10072 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10073 wq->page_count);
10074 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10075 cq->queue_id);
10076 list_for_each_entry(dmabuf, &wq->page_list, list) {
10077 memset(dmabuf->virt, 0, hw_page_size);
10078 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10079 putPaddrLow(dmabuf->phys);
10080 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10081 putPaddrHigh(dmabuf->phys);
10082 }
10083 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10084 /* The IOCTL status is embedded in the mailbox subheader. */
10085 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10086 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10087 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10088 if (shdr_status || shdr_add_status || rc) {
10089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10090 "2503 WQ_CREATE mailbox failed with "
10091 "status x%x add_status x%x, mbx status x%x\n",
10092 shdr_status, shdr_add_status, rc);
10093 status = -ENXIO;
10094 goto out;
10095 }
10096 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10097 if (wq->queue_id == 0xFFFF) {
10098 status = -ENXIO;
10099 goto out;
10100 }
10101 wq->type = LPFC_WQ;
10102 wq->subtype = subtype;
10103 wq->host_index = 0;
10104 wq->hba_index = 0;
10105
10106 /* link the wq onto the parent cq child list */
10107 list_add_tail(&wq->list, &cq->child_list);
10108 out:
10109 mempool_free(mbox, phba->mbox_mem_pool);
10110 return status;
10111 }
10112
10113 /**
10114 * lpfc_rq_create - Create a Receive Queue on the HBA
10115 * @phba: HBA structure that indicates port to create a queue on.
10116 * @hrq: The queue structure to use to create the header receive queue.
10117 * @drq: The queue structure to use to create the data receive queue.
10118 * @cq: The completion queue to bind this work queue to.
10119 *
10120 * This function creates a receive buffer queue pair , as detailed in @hrq and
10121 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10122 * to the HBA.
10123 *
10124 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10125 * struct is used to get the entry count that is necessary to determine the
10126 * number of pages to use for this queue. The @cq is used to indicate which
10127 * completion queue to bind received buffers that are posted to these queues to.
10128 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10129 * receive queue pair. This function is asynchronous and will wait for the
10130 * mailbox command to finish before continuing.
10131 *
10132 * On success this function will return a zero. If unable to allocate enough
10133 * memory this function will return ENOMEM. If the queue create mailbox command
10134 * fails this function will return ENXIO.
10135 **/
10136 uint32_t
10137 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10138 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10139 {
10140 struct lpfc_mbx_rq_create *rq_create;
10141 struct lpfc_dmabuf *dmabuf;
10142 LPFC_MBOXQ_t *mbox;
10143 int rc, length, status = 0;
10144 uint32_t shdr_status, shdr_add_status;
10145 union lpfc_sli4_cfg_shdr *shdr;
10146 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10147
10148 if (!phba->sli4_hba.pc_sli4_params.supported)
10149 hw_page_size = SLI4_PAGE_SIZE;
10150
10151 if (hrq->entry_count != drq->entry_count)
10152 return -EINVAL;
10153 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10154 if (!mbox)
10155 return -ENOMEM;
10156 length = (sizeof(struct lpfc_mbx_rq_create) -
10157 sizeof(struct lpfc_sli4_cfg_mhdr));
10158 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10159 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10160 length, LPFC_SLI4_MBX_EMBED);
10161 rq_create = &mbox->u.mqe.un.rq_create;
10162 switch (hrq->entry_count) {
10163 default:
10164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10165 "2535 Unsupported RQ count. (%d)\n",
10166 hrq->entry_count);
10167 if (hrq->entry_count < 512)
10168 return -EINVAL;
10169 /* otherwise default to smallest count (drop through) */
10170 case 512:
10171 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10172 LPFC_RQ_RING_SIZE_512);
10173 break;
10174 case 1024:
10175 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10176 LPFC_RQ_RING_SIZE_1024);
10177 break;
10178 case 2048:
10179 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10180 LPFC_RQ_RING_SIZE_2048);
10181 break;
10182 case 4096:
10183 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10184 LPFC_RQ_RING_SIZE_4096);
10185 break;
10186 }
10187 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10188 cq->queue_id);
10189 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10190 hrq->page_count);
10191 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10192 LPFC_HDR_BUF_SIZE);
10193 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10194 memset(dmabuf->virt, 0, hw_page_size);
10195 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10196 putPaddrLow(dmabuf->phys);
10197 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10198 putPaddrHigh(dmabuf->phys);
10199 }
10200 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10201 /* The IOCTL status is embedded in the mailbox subheader. */
10202 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10203 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10204 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10205 if (shdr_status || shdr_add_status || rc) {
10206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10207 "2504 RQ_CREATE mailbox failed with "
10208 "status x%x add_status x%x, mbx status x%x\n",
10209 shdr_status, shdr_add_status, rc);
10210 status = -ENXIO;
10211 goto out;
10212 }
10213 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10214 if (hrq->queue_id == 0xFFFF) {
10215 status = -ENXIO;
10216 goto out;
10217 }
10218 hrq->type = LPFC_HRQ;
10219 hrq->subtype = subtype;
10220 hrq->host_index = 0;
10221 hrq->hba_index = 0;
10222
10223 /* now create the data queue */
10224 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10225 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10226 length, LPFC_SLI4_MBX_EMBED);
10227 switch (drq->entry_count) {
10228 default:
10229 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10230 "2536 Unsupported RQ count. (%d)\n",
10231 drq->entry_count);
10232 if (drq->entry_count < 512)
10233 return -EINVAL;
10234 /* otherwise default to smallest count (drop through) */
10235 case 512:
10236 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10237 LPFC_RQ_RING_SIZE_512);
10238 break;
10239 case 1024:
10240 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10241 LPFC_RQ_RING_SIZE_1024);
10242 break;
10243 case 2048:
10244 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10245 LPFC_RQ_RING_SIZE_2048);
10246 break;
10247 case 4096:
10248 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10249 LPFC_RQ_RING_SIZE_4096);
10250 break;
10251 }
10252 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10253 cq->queue_id);
10254 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10255 drq->page_count);
10256 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10257 LPFC_DATA_BUF_SIZE);
10258 list_for_each_entry(dmabuf, &drq->page_list, list) {
10259 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10260 putPaddrLow(dmabuf->phys);
10261 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10262 putPaddrHigh(dmabuf->phys);
10263 }
10264 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10265 /* The IOCTL status is embedded in the mailbox subheader. */
10266 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10269 if (shdr_status || shdr_add_status || rc) {
10270 status = -ENXIO;
10271 goto out;
10272 }
10273 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10274 if (drq->queue_id == 0xFFFF) {
10275 status = -ENXIO;
10276 goto out;
10277 }
10278 drq->type = LPFC_DRQ;
10279 drq->subtype = subtype;
10280 drq->host_index = 0;
10281 drq->hba_index = 0;
10282
10283 /* link the header and data RQs onto the parent cq child list */
10284 list_add_tail(&hrq->list, &cq->child_list);
10285 list_add_tail(&drq->list, &cq->child_list);
10286
10287 out:
10288 mempool_free(mbox, phba->mbox_mem_pool);
10289 return status;
10290 }
10291
10292 /**
10293 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10294 * @eq: The queue structure associated with the queue to destroy.
10295 *
10296 * This function destroys a queue, as detailed in @eq by sending an mailbox
10297 * command, specific to the type of queue, to the HBA.
10298 *
10299 * The @eq struct is used to get the queue ID of the queue to destroy.
10300 *
10301 * On success this function will return a zero. If the queue destroy mailbox
10302 * command fails this function will return ENXIO.
10303 **/
10304 uint32_t
10305 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10306 {
10307 LPFC_MBOXQ_t *mbox;
10308 int rc, length, status = 0;
10309 uint32_t shdr_status, shdr_add_status;
10310 union lpfc_sli4_cfg_shdr *shdr;
10311
10312 if (!eq)
10313 return -ENODEV;
10314 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10315 if (!mbox)
10316 return -ENOMEM;
10317 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10318 sizeof(struct lpfc_sli4_cfg_mhdr));
10319 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10320 LPFC_MBOX_OPCODE_EQ_DESTROY,
10321 length, LPFC_SLI4_MBX_EMBED);
10322 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10323 eq->queue_id);
10324 mbox->vport = eq->phba->pport;
10325 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10326
10327 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10328 /* The IOCTL status is embedded in the mailbox subheader. */
10329 shdr = (union lpfc_sli4_cfg_shdr *)
10330 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10331 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10332 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10333 if (shdr_status || shdr_add_status || rc) {
10334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10335 "2505 EQ_DESTROY mailbox failed with "
10336 "status x%x add_status x%x, mbx status x%x\n",
10337 shdr_status, shdr_add_status, rc);
10338 status = -ENXIO;
10339 }
10340
10341 /* Remove eq from any list */
10342 list_del_init(&eq->list);
10343 mempool_free(mbox, eq->phba->mbox_mem_pool);
10344 return status;
10345 }
10346
10347 /**
10348 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10349 * @cq: The queue structure associated with the queue to destroy.
10350 *
10351 * This function destroys a queue, as detailed in @cq by sending an mailbox
10352 * command, specific to the type of queue, to the HBA.
10353 *
10354 * The @cq struct is used to get the queue ID of the queue to destroy.
10355 *
10356 * On success this function will return a zero. If the queue destroy mailbox
10357 * command fails this function will return ENXIO.
10358 **/
10359 uint32_t
10360 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10361 {
10362 LPFC_MBOXQ_t *mbox;
10363 int rc, length, status = 0;
10364 uint32_t shdr_status, shdr_add_status;
10365 union lpfc_sli4_cfg_shdr *shdr;
10366
10367 if (!cq)
10368 return -ENODEV;
10369 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10370 if (!mbox)
10371 return -ENOMEM;
10372 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10373 sizeof(struct lpfc_sli4_cfg_mhdr));
10374 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10375 LPFC_MBOX_OPCODE_CQ_DESTROY,
10376 length, LPFC_SLI4_MBX_EMBED);
10377 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10378 cq->queue_id);
10379 mbox->vport = cq->phba->pport;
10380 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10381 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10382 /* The IOCTL status is embedded in the mailbox subheader. */
10383 shdr = (union lpfc_sli4_cfg_shdr *)
10384 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10385 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10386 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10387 if (shdr_status || shdr_add_status || rc) {
10388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10389 "2506 CQ_DESTROY mailbox failed with "
10390 "status x%x add_status x%x, mbx status x%x\n",
10391 shdr_status, shdr_add_status, rc);
10392 status = -ENXIO;
10393 }
10394 /* Remove cq from any list */
10395 list_del_init(&cq->list);
10396 mempool_free(mbox, cq->phba->mbox_mem_pool);
10397 return status;
10398 }
10399
10400 /**
10401 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10402 * @qm: The queue structure associated with the queue to destroy.
10403 *
10404 * This function destroys a queue, as detailed in @mq by sending an mailbox
10405 * command, specific to the type of queue, to the HBA.
10406 *
10407 * The @mq struct is used to get the queue ID of the queue to destroy.
10408 *
10409 * On success this function will return a zero. If the queue destroy mailbox
10410 * command fails this function will return ENXIO.
10411 **/
10412 uint32_t
10413 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10414 {
10415 LPFC_MBOXQ_t *mbox;
10416 int rc, length, status = 0;
10417 uint32_t shdr_status, shdr_add_status;
10418 union lpfc_sli4_cfg_shdr *shdr;
10419
10420 if (!mq)
10421 return -ENODEV;
10422 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10423 if (!mbox)
10424 return -ENOMEM;
10425 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10426 sizeof(struct lpfc_sli4_cfg_mhdr));
10427 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10428 LPFC_MBOX_OPCODE_MQ_DESTROY,
10429 length, LPFC_SLI4_MBX_EMBED);
10430 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10431 mq->queue_id);
10432 mbox->vport = mq->phba->pport;
10433 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10434 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10435 /* The IOCTL status is embedded in the mailbox subheader. */
10436 shdr = (union lpfc_sli4_cfg_shdr *)
10437 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10438 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10439 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10440 if (shdr_status || shdr_add_status || rc) {
10441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10442 "2507 MQ_DESTROY mailbox failed with "
10443 "status x%x add_status x%x, mbx status x%x\n",
10444 shdr_status, shdr_add_status, rc);
10445 status = -ENXIO;
10446 }
10447 /* Remove mq from any list */
10448 list_del_init(&mq->list);
10449 mempool_free(mbox, mq->phba->mbox_mem_pool);
10450 return status;
10451 }
10452
10453 /**
10454 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10455 * @wq: The queue structure associated with the queue to destroy.
10456 *
10457 * This function destroys a queue, as detailed in @wq by sending an mailbox
10458 * command, specific to the type of queue, to the HBA.
10459 *
10460 * The @wq struct is used to get the queue ID of the queue to destroy.
10461 *
10462 * On success this function will return a zero. If the queue destroy mailbox
10463 * command fails this function will return ENXIO.
10464 **/
10465 uint32_t
10466 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10467 {
10468 LPFC_MBOXQ_t *mbox;
10469 int rc, length, status = 0;
10470 uint32_t shdr_status, shdr_add_status;
10471 union lpfc_sli4_cfg_shdr *shdr;
10472
10473 if (!wq)
10474 return -ENODEV;
10475 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10476 if (!mbox)
10477 return -ENOMEM;
10478 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10479 sizeof(struct lpfc_sli4_cfg_mhdr));
10480 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10481 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10482 length, LPFC_SLI4_MBX_EMBED);
10483 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10484 wq->queue_id);
10485 mbox->vport = wq->phba->pport;
10486 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10487 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10488 shdr = (union lpfc_sli4_cfg_shdr *)
10489 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10490 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10491 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10492 if (shdr_status || shdr_add_status || rc) {
10493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10494 "2508 WQ_DESTROY mailbox failed with "
10495 "status x%x add_status x%x, mbx status x%x\n",
10496 shdr_status, shdr_add_status, rc);
10497 status = -ENXIO;
10498 }
10499 /* Remove wq from any list */
10500 list_del_init(&wq->list);
10501 mempool_free(mbox, wq->phba->mbox_mem_pool);
10502 return status;
10503 }
10504
10505 /**
10506 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10507 * @rq: The queue structure associated with the queue to destroy.
10508 *
10509 * This function destroys a queue, as detailed in @rq by sending an mailbox
10510 * command, specific to the type of queue, to the HBA.
10511 *
10512 * The @rq struct is used to get the queue ID of the queue to destroy.
10513 *
10514 * On success this function will return a zero. If the queue destroy mailbox
10515 * command fails this function will return ENXIO.
10516 **/
10517 uint32_t
10518 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10519 struct lpfc_queue *drq)
10520 {
10521 LPFC_MBOXQ_t *mbox;
10522 int rc, length, status = 0;
10523 uint32_t shdr_status, shdr_add_status;
10524 union lpfc_sli4_cfg_shdr *shdr;
10525
10526 if (!hrq || !drq)
10527 return -ENODEV;
10528 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10529 if (!mbox)
10530 return -ENOMEM;
10531 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10532 sizeof(struct mbox_header));
10533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10534 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10535 length, LPFC_SLI4_MBX_EMBED);
10536 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10537 hrq->queue_id);
10538 mbox->vport = hrq->phba->pport;
10539 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10540 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10541 /* The IOCTL status is embedded in the mailbox subheader. */
10542 shdr = (union lpfc_sli4_cfg_shdr *)
10543 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10544 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10545 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10546 if (shdr_status || shdr_add_status || rc) {
10547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10548 "2509 RQ_DESTROY mailbox failed with "
10549 "status x%x add_status x%x, mbx status x%x\n",
10550 shdr_status, shdr_add_status, rc);
10551 if (rc != MBX_TIMEOUT)
10552 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10553 return -ENXIO;
10554 }
10555 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10556 drq->queue_id);
10557 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10558 shdr = (union lpfc_sli4_cfg_shdr *)
10559 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10560 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10561 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10562 if (shdr_status || shdr_add_status || rc) {
10563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10564 "2510 RQ_DESTROY mailbox failed with "
10565 "status x%x add_status x%x, mbx status x%x\n",
10566 shdr_status, shdr_add_status, rc);
10567 status = -ENXIO;
10568 }
10569 list_del_init(&hrq->list);
10570 list_del_init(&drq->list);
10571 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10572 return status;
10573 }
10574
10575 /**
10576 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10577 * @phba: The virtual port for which this call being executed.
10578 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10579 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10580 * @xritag: the xritag that ties this io to the SGL pages.
10581 *
10582 * This routine will post the sgl pages for the IO that has the xritag
10583 * that is in the iocbq structure. The xritag is assigned during iocbq
10584 * creation and persists for as long as the driver is loaded.
10585 * if the caller has fewer than 256 scatter gather segments to map then
10586 * pdma_phys_addr1 should be 0.
10587 * If the caller needs to map more than 256 scatter gather segment then
10588 * pdma_phys_addr1 should be a valid physical address.
10589 * physical address for SGLs must be 64 byte aligned.
10590 * If you are going to map 2 SGL's then the first one must have 256 entries
10591 * the second sgl can have between 1 and 256 entries.
10592 *
10593 * Return codes:
10594 * 0 - Success
10595 * -ENXIO, -ENOMEM - Failure
10596 **/
10597 int
10598 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10599 dma_addr_t pdma_phys_addr0,
10600 dma_addr_t pdma_phys_addr1,
10601 uint16_t xritag)
10602 {
10603 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10604 LPFC_MBOXQ_t *mbox;
10605 int rc;
10606 uint32_t shdr_status, shdr_add_status;
10607 union lpfc_sli4_cfg_shdr *shdr;
10608
10609 if (xritag == NO_XRI) {
10610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10611 "0364 Invalid param:\n");
10612 return -EINVAL;
10613 }
10614
10615 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10616 if (!mbox)
10617 return -ENOMEM;
10618
10619 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10620 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10621 sizeof(struct lpfc_mbx_post_sgl_pages) -
10622 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10623
10624 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10625 &mbox->u.mqe.un.post_sgl_pages;
10626 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10627 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10628
10629 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10630 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10631 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10632 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10633
10634 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10635 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10636 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10637 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10638 if (!phba->sli4_hba.intr_enable)
10639 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10640 else
10641 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10642 /* The IOCTL status is embedded in the mailbox subheader. */
10643 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10644 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10645 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10646 if (rc != MBX_TIMEOUT)
10647 mempool_free(mbox, phba->mbox_mem_pool);
10648 if (shdr_status || shdr_add_status || rc) {
10649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10650 "2511 POST_SGL mailbox failed with "
10651 "status x%x add_status x%x, mbx status x%x\n",
10652 shdr_status, shdr_add_status, rc);
10653 rc = -ENXIO;
10654 }
10655 return 0;
10656 }
10657 /**
10658 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10659 * @phba: The virtual port for which this call being executed.
10660 *
10661 * This routine will remove all of the sgl pages registered with the hba.
10662 *
10663 * Return codes:
10664 * 0 - Success
10665 * -ENXIO, -ENOMEM - Failure
10666 **/
10667 int
10668 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10669 {
10670 LPFC_MBOXQ_t *mbox;
10671 int rc;
10672 uint32_t shdr_status, shdr_add_status;
10673 union lpfc_sli4_cfg_shdr *shdr;
10674
10675 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10676 if (!mbox)
10677 return -ENOMEM;
10678
10679 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10680 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10681 LPFC_SLI4_MBX_EMBED);
10682 if (!phba->sli4_hba.intr_enable)
10683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10684 else
10685 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10686 /* The IOCTL status is embedded in the mailbox subheader. */
10687 shdr = (union lpfc_sli4_cfg_shdr *)
10688 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10691 if (rc != MBX_TIMEOUT)
10692 mempool_free(mbox, phba->mbox_mem_pool);
10693 if (shdr_status || shdr_add_status || rc) {
10694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10695 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10696 "status x%x add_status x%x, mbx status x%x\n",
10697 shdr_status, shdr_add_status, rc);
10698 rc = -ENXIO;
10699 }
10700 return rc;
10701 }
10702
10703 /**
10704 * lpfc_sli4_next_xritag - Get an xritag for the io
10705 * @phba: Pointer to HBA context object.
10706 *
10707 * This function gets an xritag for the iocb. If there is no unused xritag
10708 * it will return 0xffff.
10709 * The function returns the allocated xritag if successful, else returns zero.
10710 * Zero is not a valid xritag.
10711 * The caller is not required to hold any lock.
10712 **/
10713 uint16_t
10714 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10715 {
10716 uint16_t xritag;
10717
10718 spin_lock_irq(&phba->hbalock);
10719 xritag = phba->sli4_hba.next_xri;
10720 if ((xritag != (uint16_t) -1) && xritag <
10721 (phba->sli4_hba.max_cfg_param.max_xri
10722 + phba->sli4_hba.max_cfg_param.xri_base)) {
10723 phba->sli4_hba.next_xri++;
10724 phba->sli4_hba.max_cfg_param.xri_used++;
10725 spin_unlock_irq(&phba->hbalock);
10726 return xritag;
10727 }
10728 spin_unlock_irq(&phba->hbalock);
10729 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10730 "2004 Failed to allocate XRI.last XRITAG is %d"
10731 " Max XRI is %d, Used XRI is %d\n",
10732 phba->sli4_hba.next_xri,
10733 phba->sli4_hba.max_cfg_param.max_xri,
10734 phba->sli4_hba.max_cfg_param.xri_used);
10735 return -1;
10736 }
10737
10738 /**
10739 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10740 * @phba: pointer to lpfc hba data structure.
10741 *
10742 * This routine is invoked to post a block of driver's sgl pages to the
10743 * HBA using non-embedded mailbox command. No Lock is held. This routine
10744 * is only called when the driver is loading and after all IO has been
10745 * stopped.
10746 **/
10747 int
10748 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10749 {
10750 struct lpfc_sglq *sglq_entry;
10751 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10752 struct sgl_page_pairs *sgl_pg_pairs;
10753 void *viraddr;
10754 LPFC_MBOXQ_t *mbox;
10755 uint32_t reqlen, alloclen, pg_pairs;
10756 uint32_t mbox_tmo;
10757 uint16_t xritag_start = 0;
10758 int els_xri_cnt, rc = 0;
10759 uint32_t shdr_status, shdr_add_status;
10760 union lpfc_sli4_cfg_shdr *shdr;
10761
10762 /* The number of sgls to be posted */
10763 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10764
10765 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10766 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10767 if (reqlen > SLI4_PAGE_SIZE) {
10768 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10769 "2559 Block sgl registration required DMA "
10770 "size (%d) great than a page\n", reqlen);
10771 return -ENOMEM;
10772 }
10773 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10774 if (!mbox) {
10775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10776 "2560 Failed to allocate mbox cmd memory\n");
10777 return -ENOMEM;
10778 }
10779
10780 /* Allocate DMA memory and set up the non-embedded mailbox command */
10781 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10782 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10783 LPFC_SLI4_MBX_NEMBED);
10784
10785 if (alloclen < reqlen) {
10786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10787 "0285 Allocated DMA memory size (%d) is "
10788 "less than the requested DMA memory "
10789 "size (%d)\n", alloclen, reqlen);
10790 lpfc_sli4_mbox_cmd_free(phba, mbox);
10791 return -ENOMEM;
10792 }
10793 /* Get the first SGE entry from the non-embedded DMA memory */
10794 viraddr = mbox->sge_array->addr[0];
10795
10796 /* Set up the SGL pages in the non-embedded DMA pages */
10797 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10798 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10799
10800 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10801 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10802 /* Set up the sge entry */
10803 sgl_pg_pairs->sgl_pg0_addr_lo =
10804 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10805 sgl_pg_pairs->sgl_pg0_addr_hi =
10806 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10807 sgl_pg_pairs->sgl_pg1_addr_lo =
10808 cpu_to_le32(putPaddrLow(0));
10809 sgl_pg_pairs->sgl_pg1_addr_hi =
10810 cpu_to_le32(putPaddrHigh(0));
10811 /* Keep the first xritag on the list */
10812 if (pg_pairs == 0)
10813 xritag_start = sglq_entry->sli4_xritag;
10814 sgl_pg_pairs++;
10815 }
10816 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10817 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10818 /* Perform endian conversion if necessary */
10819 sgl->word0 = cpu_to_le32(sgl->word0);
10820
10821 if (!phba->sli4_hba.intr_enable)
10822 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10823 else {
10824 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10825 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10826 }
10827 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10828 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10829 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10830 if (rc != MBX_TIMEOUT)
10831 lpfc_sli4_mbox_cmd_free(phba, mbox);
10832 if (shdr_status || shdr_add_status || rc) {
10833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10834 "2513 POST_SGL_BLOCK mailbox command failed "
10835 "status x%x add_status x%x mbx status x%x\n",
10836 shdr_status, shdr_add_status, rc);
10837 rc = -ENXIO;
10838 }
10839 return rc;
10840 }
10841
10842 /**
10843 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10844 * @phba: pointer to lpfc hba data structure.
10845 * @sblist: pointer to scsi buffer list.
10846 * @count: number of scsi buffers on the list.
10847 *
10848 * This routine is invoked to post a block of @count scsi sgl pages from a
10849 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10850 * No Lock is held.
10851 *
10852 **/
10853 int
10854 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10855 int cnt)
10856 {
10857 struct lpfc_scsi_buf *psb;
10858 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10859 struct sgl_page_pairs *sgl_pg_pairs;
10860 void *viraddr;
10861 LPFC_MBOXQ_t *mbox;
10862 uint32_t reqlen, alloclen, pg_pairs;
10863 uint32_t mbox_tmo;
10864 uint16_t xritag_start = 0;
10865 int rc = 0;
10866 uint32_t shdr_status, shdr_add_status;
10867 dma_addr_t pdma_phys_bpl1;
10868 union lpfc_sli4_cfg_shdr *shdr;
10869
10870 /* Calculate the requested length of the dma memory */
10871 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10872 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10873 if (reqlen > SLI4_PAGE_SIZE) {
10874 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10875 "0217 Block sgl registration required DMA "
10876 "size (%d) great than a page\n", reqlen);
10877 return -ENOMEM;
10878 }
10879 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10880 if (!mbox) {
10881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10882 "0283 Failed to allocate mbox cmd memory\n");
10883 return -ENOMEM;
10884 }
10885
10886 /* Allocate DMA memory and set up the non-embedded mailbox command */
10887 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10888 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10889 LPFC_SLI4_MBX_NEMBED);
10890
10891 if (alloclen < reqlen) {
10892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10893 "2561 Allocated DMA memory size (%d) is "
10894 "less than the requested DMA memory "
10895 "size (%d)\n", alloclen, reqlen);
10896 lpfc_sli4_mbox_cmd_free(phba, mbox);
10897 return -ENOMEM;
10898 }
10899 /* Get the first SGE entry from the non-embedded DMA memory */
10900 viraddr = mbox->sge_array->addr[0];
10901
10902 /* Set up the SGL pages in the non-embedded DMA pages */
10903 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10904 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10905
10906 pg_pairs = 0;
10907 list_for_each_entry(psb, sblist, list) {
10908 /* Set up the sge entry */
10909 sgl_pg_pairs->sgl_pg0_addr_lo =
10910 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10911 sgl_pg_pairs->sgl_pg0_addr_hi =
10912 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10913 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10914 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10915 else
10916 pdma_phys_bpl1 = 0;
10917 sgl_pg_pairs->sgl_pg1_addr_lo =
10918 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10919 sgl_pg_pairs->sgl_pg1_addr_hi =
10920 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10921 /* Keep the first xritag on the list */
10922 if (pg_pairs == 0)
10923 xritag_start = psb->cur_iocbq.sli4_xritag;
10924 sgl_pg_pairs++;
10925 pg_pairs++;
10926 }
10927 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10928 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10929 /* Perform endian conversion if necessary */
10930 sgl->word0 = cpu_to_le32(sgl->word0);
10931
10932 if (!phba->sli4_hba.intr_enable)
10933 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10934 else {
10935 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10936 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10937 }
10938 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10939 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10940 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10941 if (rc != MBX_TIMEOUT)
10942 lpfc_sli4_mbox_cmd_free(phba, mbox);
10943 if (shdr_status || shdr_add_status || rc) {
10944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10945 "2564 POST_SGL_BLOCK mailbox command failed "
10946 "status x%x add_status x%x mbx status x%x\n",
10947 shdr_status, shdr_add_status, rc);
10948 rc = -ENXIO;
10949 }
10950 return rc;
10951 }
10952
10953 /**
10954 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10955 * @phba: pointer to lpfc_hba struct that the frame was received on
10956 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10957 *
10958 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10959 * valid type of frame that the LPFC driver will handle. This function will
10960 * return a zero if the frame is a valid frame or a non zero value when the
10961 * frame does not pass the check.
10962 **/
10963 static int
10964 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10965 {
10966 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10967 char *type_names[] = FC_TYPE_NAMES_INIT;
10968 struct fc_vft_header *fc_vft_hdr;
10969
10970 switch (fc_hdr->fh_r_ctl) {
10971 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10972 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10973 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10974 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10975 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10976 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10977 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10978 case FC_RCTL_DD_CMD_STATUS: /* command status */
10979 case FC_RCTL_ELS_REQ: /* extended link services request */
10980 case FC_RCTL_ELS_REP: /* extended link services reply */
10981 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10982 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10983 case FC_RCTL_BA_NOP: /* basic link service NOP */
10984 case FC_RCTL_BA_ABTS: /* basic link service abort */
10985 case FC_RCTL_BA_RMC: /* remove connection */
10986 case FC_RCTL_BA_ACC: /* basic accept */
10987 case FC_RCTL_BA_RJT: /* basic reject */
10988 case FC_RCTL_BA_PRMT:
10989 case FC_RCTL_ACK_1: /* acknowledge_1 */
10990 case FC_RCTL_ACK_0: /* acknowledge_0 */
10991 case FC_RCTL_P_RJT: /* port reject */
10992 case FC_RCTL_F_RJT: /* fabric reject */
10993 case FC_RCTL_P_BSY: /* port busy */
10994 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10995 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10996 case FC_RCTL_LCR: /* link credit reset */
10997 case FC_RCTL_END: /* end */
10998 break;
10999 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11000 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11001 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11002 return lpfc_fc_frame_check(phba, fc_hdr);
11003 default:
11004 goto drop;
11005 }
11006 switch (fc_hdr->fh_type) {
11007 case FC_TYPE_BLS:
11008 case FC_TYPE_ELS:
11009 case FC_TYPE_FCP:
11010 case FC_TYPE_CT:
11011 break;
11012 case FC_TYPE_IP:
11013 case FC_TYPE_ILS:
11014 default:
11015 goto drop;
11016 }
11017 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11018 "2538 Received frame rctl:%s type:%s\n",
11019 rctl_names[fc_hdr->fh_r_ctl],
11020 type_names[fc_hdr->fh_type]);
11021 return 0;
11022 drop:
11023 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11024 "2539 Dropped frame rctl:%s type:%s\n",
11025 rctl_names[fc_hdr->fh_r_ctl],
11026 type_names[fc_hdr->fh_type]);
11027 return 1;
11028 }
11029
11030 /**
11031 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11032 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11033 *
11034 * This function processes the FC header to retrieve the VFI from the VF
11035 * header, if one exists. This function will return the VFI if one exists
11036 * or 0 if no VSAN Header exists.
11037 **/
11038 static uint32_t
11039 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11040 {
11041 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11042
11043 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11044 return 0;
11045 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11046 }
11047
11048 /**
11049 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11050 * @phba: Pointer to the HBA structure to search for the vport on
11051 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11052 * @fcfi: The FC Fabric ID that the frame came from
11053 *
11054 * This function searches the @phba for a vport that matches the content of the
11055 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11056 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11057 * returns the matching vport pointer or NULL if unable to match frame to a
11058 * vport.
11059 **/
11060 static struct lpfc_vport *
11061 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11062 uint16_t fcfi)
11063 {
11064 struct lpfc_vport **vports;
11065 struct lpfc_vport *vport = NULL;
11066 int i;
11067 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11068 fc_hdr->fh_d_id[1] << 8 |
11069 fc_hdr->fh_d_id[2]);
11070
11071 vports = lpfc_create_vport_work_array(phba);
11072 if (vports != NULL)
11073 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11074 if (phba->fcf.fcfi == fcfi &&
11075 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11076 vports[i]->fc_myDID == did) {
11077 vport = vports[i];
11078 break;
11079 }
11080 }
11081 lpfc_destroy_vport_work_array(phba, vports);
11082 return vport;
11083 }
11084
11085 /**
11086 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11087 * @vport: The vport to work on.
11088 *
11089 * This function updates the receive sequence time stamp for this vport. The
11090 * receive sequence time stamp indicates the time that the last frame of the
11091 * the sequence that has been idle for the longest amount of time was received.
11092 * the driver uses this time stamp to indicate if any received sequences have
11093 * timed out.
11094 **/
11095 void
11096 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11097 {
11098 struct lpfc_dmabuf *h_buf;
11099 struct hbq_dmabuf *dmabuf = NULL;
11100
11101 /* get the oldest sequence on the rcv list */
11102 h_buf = list_get_first(&vport->rcv_buffer_list,
11103 struct lpfc_dmabuf, list);
11104 if (!h_buf)
11105 return;
11106 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11107 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11108 }
11109
11110 /**
11111 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11112 * @vport: The vport that the received sequences were sent to.
11113 *
11114 * This function cleans up all outstanding received sequences. This is called
11115 * by the driver when a link event or user action invalidates all the received
11116 * sequences.
11117 **/
11118 void
11119 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11120 {
11121 struct lpfc_dmabuf *h_buf, *hnext;
11122 struct lpfc_dmabuf *d_buf, *dnext;
11123 struct hbq_dmabuf *dmabuf = NULL;
11124
11125 /* start with the oldest sequence on the rcv list */
11126 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11127 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11128 list_del_init(&dmabuf->hbuf.list);
11129 list_for_each_entry_safe(d_buf, dnext,
11130 &dmabuf->dbuf.list, list) {
11131 list_del_init(&d_buf->list);
11132 lpfc_in_buf_free(vport->phba, d_buf);
11133 }
11134 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11135 }
11136 }
11137
11138 /**
11139 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11140 * @vport: The vport that the received sequences were sent to.
11141 *
11142 * This function determines whether any received sequences have timed out by
11143 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11144 * indicates that there is at least one timed out sequence this routine will
11145 * go through the received sequences one at a time from most inactive to most
11146 * active to determine which ones need to be cleaned up. Once it has determined
11147 * that a sequence needs to be cleaned up it will simply free up the resources
11148 * without sending an abort.
11149 **/
11150 void
11151 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11152 {
11153 struct lpfc_dmabuf *h_buf, *hnext;
11154 struct lpfc_dmabuf *d_buf, *dnext;
11155 struct hbq_dmabuf *dmabuf = NULL;
11156 unsigned long timeout;
11157 int abort_count = 0;
11158
11159 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11160 vport->rcv_buffer_time_stamp);
11161 if (list_empty(&vport->rcv_buffer_list) ||
11162 time_before(jiffies, timeout))
11163 return;
11164 /* start with the oldest sequence on the rcv list */
11165 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11166 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11167 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11168 dmabuf->time_stamp);
11169 if (time_before(jiffies, timeout))
11170 break;
11171 abort_count++;
11172 list_del_init(&dmabuf->hbuf.list);
11173 list_for_each_entry_safe(d_buf, dnext,
11174 &dmabuf->dbuf.list, list) {
11175 list_del_init(&d_buf->list);
11176 lpfc_in_buf_free(vport->phba, d_buf);
11177 }
11178 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11179 }
11180 if (abort_count)
11181 lpfc_update_rcv_time_stamp(vport);
11182 }
11183
11184 /**
11185 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11186 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11187 *
11188 * This function searches through the existing incomplete sequences that have
11189 * been sent to this @vport. If the frame matches one of the incomplete
11190 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11191 * make up that sequence. If no sequence is found that matches this frame then
11192 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11193 * This function returns a pointer to the first dmabuf in the sequence list that
11194 * the frame was linked to.
11195 **/
11196 static struct hbq_dmabuf *
11197 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11198 {
11199 struct fc_frame_header *new_hdr;
11200 struct fc_frame_header *temp_hdr;
11201 struct lpfc_dmabuf *d_buf;
11202 struct lpfc_dmabuf *h_buf;
11203 struct hbq_dmabuf *seq_dmabuf = NULL;
11204 struct hbq_dmabuf *temp_dmabuf = NULL;
11205
11206 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11207 dmabuf->time_stamp = jiffies;
11208 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11209 /* Use the hdr_buf to find the sequence that this frame belongs to */
11210 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11211 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11212 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11213 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11214 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11215 continue;
11216 /* found a pending sequence that matches this frame */
11217 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11218 break;
11219 }
11220 if (!seq_dmabuf) {
11221 /*
11222 * This indicates first frame received for this sequence.
11223 * Queue the buffer on the vport's rcv_buffer_list.
11224 */
11225 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11226 lpfc_update_rcv_time_stamp(vport);
11227 return dmabuf;
11228 }
11229 temp_hdr = seq_dmabuf->hbuf.virt;
11230 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11231 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11232 list_del_init(&seq_dmabuf->hbuf.list);
11233 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11234 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11235 lpfc_update_rcv_time_stamp(vport);
11236 return dmabuf;
11237 }
11238 /* move this sequence to the tail to indicate a young sequence */
11239 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11240 seq_dmabuf->time_stamp = jiffies;
11241 lpfc_update_rcv_time_stamp(vport);
11242 if (list_empty(&seq_dmabuf->dbuf.list)) {
11243 temp_hdr = dmabuf->hbuf.virt;
11244 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11245 return seq_dmabuf;
11246 }
11247 /* find the correct place in the sequence to insert this frame */
11248 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11249 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11250 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11251 /*
11252 * If the frame's sequence count is greater than the frame on
11253 * the list then insert the frame right after this frame
11254 */
11255 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11256 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11257 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11258 return seq_dmabuf;
11259 }
11260 }
11261 return NULL;
11262 }
11263
11264 /**
11265 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11266 * @vport: pointer to a vitural port
11267 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11268 *
11269 * This function tries to abort from the partially assembed sequence, described
11270 * by the information from basic abbort @dmabuf. It checks to see whether such
11271 * partially assembled sequence held by the driver. If so, it shall free up all
11272 * the frames from the partially assembled sequence.
11273 *
11274 * Return
11275 * true -- if there is matching partially assembled sequence present and all
11276 * the frames freed with the sequence;
11277 * false -- if there is no matching partially assembled sequence present so
11278 * nothing got aborted in the lower layer driver
11279 **/
11280 static bool
11281 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11282 struct hbq_dmabuf *dmabuf)
11283 {
11284 struct fc_frame_header *new_hdr;
11285 struct fc_frame_header *temp_hdr;
11286 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11287 struct hbq_dmabuf *seq_dmabuf = NULL;
11288
11289 /* Use the hdr_buf to find the sequence that matches this frame */
11290 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11291 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11292 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11293 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11294 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11295 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11296 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11297 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11298 continue;
11299 /* found a pending sequence that matches this frame */
11300 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11301 break;
11302 }
11303
11304 /* Free up all the frames from the partially assembled sequence */
11305 if (seq_dmabuf) {
11306 list_for_each_entry_safe(d_buf, n_buf,
11307 &seq_dmabuf->dbuf.list, list) {
11308 list_del_init(&d_buf->list);
11309 lpfc_in_buf_free(vport->phba, d_buf);
11310 }
11311 return true;
11312 }
11313 return false;
11314 }
11315
11316 /**
11317 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11318 * @phba: Pointer to HBA context object.
11319 * @cmd_iocbq: pointer to the command iocbq structure.
11320 * @rsp_iocbq: pointer to the response iocbq structure.
11321 *
11322 * This function handles the sequence abort accept iocb command complete
11323 * event. It properly releases the memory allocated to the sequence abort
11324 * accept iocb.
11325 **/
11326 static void
11327 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11328 struct lpfc_iocbq *cmd_iocbq,
11329 struct lpfc_iocbq *rsp_iocbq)
11330 {
11331 if (cmd_iocbq)
11332 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11333 }
11334
11335 /**
11336 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11337 * @phba: Pointer to HBA context object.
11338 * @fc_hdr: pointer to a FC frame header.
11339 *
11340 * This function sends a basic accept to a previous unsol sequence abort
11341 * event after aborting the sequence handling.
11342 **/
11343 static void
11344 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11345 struct fc_frame_header *fc_hdr)
11346 {
11347 struct lpfc_iocbq *ctiocb = NULL;
11348 struct lpfc_nodelist *ndlp;
11349 uint16_t oxid, rxid;
11350 uint32_t sid, fctl;
11351 IOCB_t *icmd;
11352
11353 if (!lpfc_is_link_up(phba))
11354 return;
11355
11356 sid = sli4_sid_from_fc_hdr(fc_hdr);
11357 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11358 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11359
11360 ndlp = lpfc_findnode_did(phba->pport, sid);
11361 if (!ndlp) {
11362 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11363 "1268 Find ndlp returned NULL for oxid:x%x "
11364 "SID:x%x\n", oxid, sid);
11365 return;
11366 }
11367
11368 /* Allocate buffer for acc iocb */
11369 ctiocb = lpfc_sli_get_iocbq(phba);
11370 if (!ctiocb)
11371 return;
11372
11373 /* Extract the F_CTL field from FC_HDR */
11374 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11375
11376 icmd = &ctiocb->iocb;
11377 icmd->un.xseq64.bdl.bdeSize = 0;
11378 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11379 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11380 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11381 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11382
11383 /* Fill in the rest of iocb fields */
11384 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11385 icmd->ulpBdeCount = 0;
11386 icmd->ulpLe = 1;
11387 icmd->ulpClass = CLASS3;
11388 icmd->ulpContext = ndlp->nlp_rpi;
11389
11390 ctiocb->iocb_cmpl = NULL;
11391 ctiocb->vport = phba->pport;
11392 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11393
11394 if (fctl & FC_FC_EX_CTX) {
11395 /* ABTS sent by responder to CT exchange, construction
11396 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11397 * field and RX_ID from ABTS for RX_ID field.
11398 */
11399 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11400 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11401 ctiocb->sli4_xritag = oxid;
11402 } else {
11403 /* ABTS sent by initiator to CT exchange, construction
11404 * of BA_ACC will need to allocate a new XRI as for the
11405 * XRI_TAG and RX_ID fields.
11406 */
11407 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11408 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11409 ctiocb->sli4_xritag = NO_XRI;
11410 }
11411 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11412
11413 /* Xmit CT abts accept on exchange <xid> */
11414 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11415 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11416 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11417 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11418 }
11419
11420 /**
11421 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11422 * @vport: Pointer to the vport on which this sequence was received
11423 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11424 *
11425 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11426 * receive sequence is only partially assembed by the driver, it shall abort
11427 * the partially assembled frames for the sequence. Otherwise, if the
11428 * unsolicited receive sequence has been completely assembled and passed to
11429 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11430 * unsolicited sequence has been aborted. After that, it will issue a basic
11431 * accept to accept the abort.
11432 **/
11433 void
11434 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11435 struct hbq_dmabuf *dmabuf)
11436 {
11437 struct lpfc_hba *phba = vport->phba;
11438 struct fc_frame_header fc_hdr;
11439 uint32_t fctl;
11440 bool abts_par;
11441
11442 /* Make a copy of fc_hdr before the dmabuf being released */
11443 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11444 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11445
11446 if (fctl & FC_FC_EX_CTX) {
11447 /*
11448 * ABTS sent by responder to exchange, just free the buffer
11449 */
11450 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11451 } else {
11452 /*
11453 * ABTS sent by initiator to exchange, need to do cleanup
11454 */
11455 /* Try to abort partially assembled seq */
11456 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11457
11458 /* Send abort to ULP if partially seq abort failed */
11459 if (abts_par == false)
11460 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11461 else
11462 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11463 }
11464 /* Send basic accept (BA_ACC) to the abort requester */
11465 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11466 }
11467
11468 /**
11469 * lpfc_seq_complete - Indicates if a sequence is complete
11470 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11471 *
11472 * This function checks the sequence, starting with the frame described by
11473 * @dmabuf, to see if all the frames associated with this sequence are present.
11474 * the frames associated with this sequence are linked to the @dmabuf using the
11475 * dbuf list. This function looks for two major things. 1) That the first frame
11476 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11477 * set. 3) That there are no holes in the sequence count. The function will
11478 * return 1 when the sequence is complete, otherwise it will return 0.
11479 **/
11480 static int
11481 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11482 {
11483 struct fc_frame_header *hdr;
11484 struct lpfc_dmabuf *d_buf;
11485 struct hbq_dmabuf *seq_dmabuf;
11486 uint32_t fctl;
11487 int seq_count = 0;
11488
11489 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11490 /* make sure first fame of sequence has a sequence count of zero */
11491 if (hdr->fh_seq_cnt != seq_count)
11492 return 0;
11493 fctl = (hdr->fh_f_ctl[0] << 16 |
11494 hdr->fh_f_ctl[1] << 8 |
11495 hdr->fh_f_ctl[2]);
11496 /* If last frame of sequence we can return success. */
11497 if (fctl & FC_FC_END_SEQ)
11498 return 1;
11499 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11500 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11501 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11502 /* If there is a hole in the sequence count then fail. */
11503 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11504 return 0;
11505 fctl = (hdr->fh_f_ctl[0] << 16 |
11506 hdr->fh_f_ctl[1] << 8 |
11507 hdr->fh_f_ctl[2]);
11508 /* If last frame of sequence we can return success. */
11509 if (fctl & FC_FC_END_SEQ)
11510 return 1;
11511 }
11512 return 0;
11513 }
11514
11515 /**
11516 * lpfc_prep_seq - Prep sequence for ULP processing
11517 * @vport: Pointer to the vport on which this sequence was received
11518 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11519 *
11520 * This function takes a sequence, described by a list of frames, and creates
11521 * a list of iocbq structures to describe the sequence. This iocbq list will be
11522 * used to issue to the generic unsolicited sequence handler. This routine
11523 * returns a pointer to the first iocbq in the list. If the function is unable
11524 * to allocate an iocbq then it throw out the received frames that were not
11525 * able to be described and return a pointer to the first iocbq. If unable to
11526 * allocate any iocbqs (including the first) this function will return NULL.
11527 **/
11528 static struct lpfc_iocbq *
11529 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11530 {
11531 struct lpfc_dmabuf *d_buf, *n_buf;
11532 struct lpfc_iocbq *first_iocbq, *iocbq;
11533 struct fc_frame_header *fc_hdr;
11534 uint32_t sid;
11535 struct ulp_bde64 *pbde;
11536
11537 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11538 /* remove from receive buffer list */
11539 list_del_init(&seq_dmabuf->hbuf.list);
11540 lpfc_update_rcv_time_stamp(vport);
11541 /* get the Remote Port's SID */
11542 sid = sli4_sid_from_fc_hdr(fc_hdr);
11543 /* Get an iocbq struct to fill in. */
11544 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11545 if (first_iocbq) {
11546 /* Initialize the first IOCB. */
11547 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11548 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11549 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11550 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11551 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11552 vport->vpi + vport->phba->vpi_base;
11553 /* put the first buffer into the first IOCBq */
11554 first_iocbq->context2 = &seq_dmabuf->dbuf;
11555 first_iocbq->context3 = NULL;
11556 first_iocbq->iocb.ulpBdeCount = 1;
11557 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11558 LPFC_DATA_BUF_SIZE;
11559 first_iocbq->iocb.un.rcvels.remoteID = sid;
11560 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11561 bf_get(lpfc_rcqe_length,
11562 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11563 }
11564 iocbq = first_iocbq;
11565 /*
11566 * Each IOCBq can have two Buffers assigned, so go through the list
11567 * of buffers for this sequence and save two buffers in each IOCBq
11568 */
11569 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11570 if (!iocbq) {
11571 lpfc_in_buf_free(vport->phba, d_buf);
11572 continue;
11573 }
11574 if (!iocbq->context3) {
11575 iocbq->context3 = d_buf;
11576 iocbq->iocb.ulpBdeCount++;
11577 pbde = (struct ulp_bde64 *)
11578 &iocbq->iocb.unsli3.sli3Words[4];
11579 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11580 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11581 bf_get(lpfc_rcqe_length,
11582 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11583 } else {
11584 iocbq = lpfc_sli_get_iocbq(vport->phba);
11585 if (!iocbq) {
11586 if (first_iocbq) {
11587 first_iocbq->iocb.ulpStatus =
11588 IOSTAT_FCP_RSP_ERROR;
11589 first_iocbq->iocb.un.ulpWord[4] =
11590 IOERR_NO_RESOURCES;
11591 }
11592 lpfc_in_buf_free(vport->phba, d_buf);
11593 continue;
11594 }
11595 iocbq->context2 = d_buf;
11596 iocbq->context3 = NULL;
11597 iocbq->iocb.ulpBdeCount = 1;
11598 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11599 LPFC_DATA_BUF_SIZE;
11600 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11601 bf_get(lpfc_rcqe_length,
11602 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11603 iocbq->iocb.un.rcvels.remoteID = sid;
11604 list_add_tail(&iocbq->list, &first_iocbq->list);
11605 }
11606 }
11607 return first_iocbq;
11608 }
11609
11610 static void
11611 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11612 struct hbq_dmabuf *seq_dmabuf)
11613 {
11614 struct fc_frame_header *fc_hdr;
11615 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11616 struct lpfc_hba *phba = vport->phba;
11617
11618 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11619 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11620 if (!iocbq) {
11621 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11622 "2707 Ring %d handler: Failed to allocate "
11623 "iocb Rctl x%x Type x%x received\n",
11624 LPFC_ELS_RING,
11625 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11626 return;
11627 }
11628 if (!lpfc_complete_unsol_iocb(phba,
11629 &phba->sli.ring[LPFC_ELS_RING],
11630 iocbq, fc_hdr->fh_r_ctl,
11631 fc_hdr->fh_type))
11632 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11633 "2540 Ring %d handler: unexpected Rctl "
11634 "x%x Type x%x received\n",
11635 LPFC_ELS_RING,
11636 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11637
11638 /* Free iocb created in lpfc_prep_seq */
11639 list_for_each_entry_safe(curr_iocb, next_iocb,
11640 &iocbq->list, list) {
11641 list_del_init(&curr_iocb->list);
11642 lpfc_sli_release_iocbq(phba, curr_iocb);
11643 }
11644 lpfc_sli_release_iocbq(phba, iocbq);
11645 }
11646
11647 /**
11648 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11649 * @phba: Pointer to HBA context object.
11650 *
11651 * This function is called with no lock held. This function processes all
11652 * the received buffers and gives it to upper layers when a received buffer
11653 * indicates that it is the final frame in the sequence. The interrupt
11654 * service routine processes received buffers at interrupt contexts and adds
11655 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11656 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11657 * appropriate receive function when the final frame in a sequence is received.
11658 **/
11659 void
11660 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11661 struct hbq_dmabuf *dmabuf)
11662 {
11663 struct hbq_dmabuf *seq_dmabuf;
11664 struct fc_frame_header *fc_hdr;
11665 struct lpfc_vport *vport;
11666 uint32_t fcfi;
11667
11668 /* Process each received buffer */
11669 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11670 /* check to see if this a valid type of frame */
11671 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11672 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11673 return;
11674 }
11675 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11676 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11677 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11678 /* throw out the frame */
11679 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11680 return;
11681 }
11682 /* Handle the basic abort sequence (BA_ABTS) event */
11683 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11684 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11685 return;
11686 }
11687
11688 /* Link this frame */
11689 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11690 if (!seq_dmabuf) {
11691 /* unable to add frame to vport - throw it out */
11692 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11693 return;
11694 }
11695 /* If not last frame in sequence continue processing frames. */
11696 if (!lpfc_seq_complete(seq_dmabuf))
11697 return;
11698
11699 /* Send the complete sequence to the upper layer protocol */
11700 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11701 }
11702
11703 /**
11704 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11705 * @phba: pointer to lpfc hba data structure.
11706 *
11707 * This routine is invoked to post rpi header templates to the
11708 * HBA consistent with the SLI-4 interface spec. This routine
11709 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11710 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11711 *
11712 * This routine does not require any locks. It's usage is expected
11713 * to be driver load or reset recovery when the driver is
11714 * sequential.
11715 *
11716 * Return codes
11717 * 0 - successful
11718 * EIO - The mailbox failed to complete successfully.
11719 * When this error occurs, the driver is not guaranteed
11720 * to have any rpi regions posted to the device and
11721 * must either attempt to repost the regions or take a
11722 * fatal error.
11723 **/
11724 int
11725 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11726 {
11727 struct lpfc_rpi_hdr *rpi_page;
11728 uint32_t rc = 0;
11729
11730 /* Post all rpi memory regions to the port. */
11731 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11732 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11733 if (rc != MBX_SUCCESS) {
11734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11735 "2008 Error %d posting all rpi "
11736 "headers\n", rc);
11737 rc = -EIO;
11738 break;
11739 }
11740 }
11741
11742 return rc;
11743 }
11744
11745 /**
11746 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11747 * @phba: pointer to lpfc hba data structure.
11748 * @rpi_page: pointer to the rpi memory region.
11749 *
11750 * This routine is invoked to post a single rpi header to the
11751 * HBA consistent with the SLI-4 interface spec. This memory region
11752 * maps up to 64 rpi context regions.
11753 *
11754 * Return codes
11755 * 0 - successful
11756 * ENOMEM - No available memory
11757 * EIO - The mailbox failed to complete successfully.
11758 **/
11759 int
11760 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11761 {
11762 LPFC_MBOXQ_t *mboxq;
11763 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11764 uint32_t rc = 0;
11765 uint32_t mbox_tmo;
11766 uint32_t shdr_status, shdr_add_status;
11767 union lpfc_sli4_cfg_shdr *shdr;
11768
11769 /* The port is notified of the header region via a mailbox command. */
11770 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11771 if (!mboxq) {
11772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11773 "2001 Unable to allocate memory for issuing "
11774 "SLI_CONFIG_SPECIAL mailbox command\n");
11775 return -ENOMEM;
11776 }
11777
11778 /* Post all rpi memory regions to the port. */
11779 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11780 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11781 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11782 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11783 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11784 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11785 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11786 hdr_tmpl, rpi_page->page_count);
11787 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11788 rpi_page->start_rpi);
11789 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11790 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11791 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11792 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11793 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11794 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11795 if (rc != MBX_TIMEOUT)
11796 mempool_free(mboxq, phba->mbox_mem_pool);
11797 if (shdr_status || shdr_add_status || rc) {
11798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11799 "2514 POST_RPI_HDR mailbox failed with "
11800 "status x%x add_status x%x, mbx status x%x\n",
11801 shdr_status, shdr_add_status, rc);
11802 rc = -ENXIO;
11803 }
11804 return rc;
11805 }
11806
11807 /**
11808 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11809 * @phba: pointer to lpfc hba data structure.
11810 *
11811 * This routine is invoked to post rpi header templates to the
11812 * HBA consistent with the SLI-4 interface spec. This routine
11813 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11814 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
11815 *
11816 * Returns
11817 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11818 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11819 **/
11820 int
11821 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11822 {
11823 int rpi;
11824 uint16_t max_rpi, rpi_base, rpi_limit;
11825 uint16_t rpi_remaining;
11826 struct lpfc_rpi_hdr *rpi_hdr;
11827
11828 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11829 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11830 rpi_limit = phba->sli4_hba.next_rpi;
11831
11832 /*
11833 * The valid rpi range is not guaranteed to be zero-based. Start
11834 * the search at the rpi_base as reported by the port.
11835 */
11836 spin_lock_irq(&phba->hbalock);
11837 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11838 if (rpi >= rpi_limit || rpi < rpi_base)
11839 rpi = LPFC_RPI_ALLOC_ERROR;
11840 else {
11841 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11842 phba->sli4_hba.max_cfg_param.rpi_used++;
11843 phba->sli4_hba.rpi_count++;
11844 }
11845
11846 /*
11847 * Don't try to allocate more rpi header regions if the device limit
11848 * on available rpis max has been exhausted.
11849 */
11850 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11851 (phba->sli4_hba.rpi_count >= max_rpi)) {
11852 spin_unlock_irq(&phba->hbalock);
11853 return rpi;
11854 }
11855
11856 /*
11857 * If the driver is running low on rpi resources, allocate another
11858 * page now. Note that the next_rpi value is used because
11859 * it represents how many are actually in use whereas max_rpi notes
11860 * how many are supported max by the device.
11861 */
11862 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11863 phba->sli4_hba.rpi_count;
11864 spin_unlock_irq(&phba->hbalock);
11865 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11866 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11867 if (!rpi_hdr) {
11868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11869 "2002 Error Could not grow rpi "
11870 "count\n");
11871 } else {
11872 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11873 }
11874 }
11875
11876 return rpi;
11877 }
11878
11879 /**
11880 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11881 * @phba: pointer to lpfc hba data structure.
11882 *
11883 * This routine is invoked to release an rpi to the pool of
11884 * available rpis maintained by the driver.
11885 **/
11886 void
11887 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11888 {
11889 spin_lock_irq(&phba->hbalock);
11890 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11891 phba->sli4_hba.rpi_count--;
11892 phba->sli4_hba.max_cfg_param.rpi_used--;
11893 spin_unlock_irq(&phba->hbalock);
11894 }
11895
11896 /**
11897 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11898 * @phba: pointer to lpfc hba data structure.
11899 *
11900 * This routine is invoked to remove the memory region that
11901 * provided rpi via a bitmask.
11902 **/
11903 void
11904 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11905 {
11906 kfree(phba->sli4_hba.rpi_bmask);
11907 }
11908
11909 /**
11910 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11911 * @phba: pointer to lpfc hba data structure.
11912 *
11913 * This routine is invoked to remove the memory region that
11914 * provided rpi via a bitmask.
11915 **/
11916 int
11917 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11918 {
11919 LPFC_MBOXQ_t *mboxq;
11920 struct lpfc_hba *phba = ndlp->phba;
11921 int rc;
11922
11923 /* The port is notified of the header region via a mailbox command. */
11924 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11925 if (!mboxq)
11926 return -ENOMEM;
11927
11928 /* Post all rpi memory regions to the port. */
11929 lpfc_resume_rpi(mboxq, ndlp);
11930 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11931 if (rc == MBX_NOT_FINISHED) {
11932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11933 "2010 Resume RPI Mailbox failed "
11934 "status %d, mbxStatus x%x\n", rc,
11935 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11936 mempool_free(mboxq, phba->mbox_mem_pool);
11937 return -EIO;
11938 }
11939 return 0;
11940 }
11941
11942 /**
11943 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11944 * @phba: pointer to lpfc hba data structure.
11945 * @vpi: vpi value to activate with the port.
11946 *
11947 * This routine is invoked to activate a vpi with the
11948 * port when the host intends to use vports with a
11949 * nonzero vpi.
11950 *
11951 * Returns:
11952 * 0 success
11953 * -Evalue otherwise
11954 **/
11955 int
11956 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11957 {
11958 LPFC_MBOXQ_t *mboxq;
11959 int rc = 0;
11960 int retval = MBX_SUCCESS;
11961 uint32_t mbox_tmo;
11962
11963 if (vpi == 0)
11964 return -EINVAL;
11965 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11966 if (!mboxq)
11967 return -ENOMEM;
11968 lpfc_init_vpi(phba, mboxq, vpi);
11969 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11970 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11971 if (rc != MBX_SUCCESS) {
11972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11973 "2022 INIT VPI Mailbox failed "
11974 "status %d, mbxStatus x%x\n", rc,
11975 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11976 retval = -EIO;
11977 }
11978 if (rc != MBX_TIMEOUT)
11979 mempool_free(mboxq, phba->mbox_mem_pool);
11980
11981 return retval;
11982 }
11983
11984 /**
11985 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11986 * @phba: pointer to lpfc hba data structure.
11987 * @mboxq: Pointer to mailbox object.
11988 *
11989 * This routine is invoked to manually add a single FCF record. The caller
11990 * must pass a completely initialized FCF_Record. This routine takes
11991 * care of the nonembedded mailbox operations.
11992 **/
11993 static void
11994 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11995 {
11996 void *virt_addr;
11997 union lpfc_sli4_cfg_shdr *shdr;
11998 uint32_t shdr_status, shdr_add_status;
11999
12000 virt_addr = mboxq->sge_array->addr[0];
12001 /* The IOCTL status is embedded in the mailbox subheader. */
12002 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12003 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12005
12006 if ((shdr_status || shdr_add_status) &&
12007 (shdr_status != STATUS_FCF_IN_USE))
12008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12009 "2558 ADD_FCF_RECORD mailbox failed with "
12010 "status x%x add_status x%x\n",
12011 shdr_status, shdr_add_status);
12012
12013 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12014 }
12015
12016 /**
12017 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12018 * @phba: pointer to lpfc hba data structure.
12019 * @fcf_record: pointer to the initialized fcf record to add.
12020 *
12021 * This routine is invoked to manually add a single FCF record. The caller
12022 * must pass a completely initialized FCF_Record. This routine takes
12023 * care of the nonembedded mailbox operations.
12024 **/
12025 int
12026 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12027 {
12028 int rc = 0;
12029 LPFC_MBOXQ_t *mboxq;
12030 uint8_t *bytep;
12031 void *virt_addr;
12032 dma_addr_t phys_addr;
12033 struct lpfc_mbx_sge sge;
12034 uint32_t alloc_len, req_len;
12035 uint32_t fcfindex;
12036
12037 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12038 if (!mboxq) {
12039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12040 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12041 return -ENOMEM;
12042 }
12043
12044 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12045 sizeof(uint32_t);
12046
12047 /* Allocate DMA memory and set up the non-embedded mailbox command */
12048 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12049 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12050 req_len, LPFC_SLI4_MBX_NEMBED);
12051 if (alloc_len < req_len) {
12052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12053 "2523 Allocated DMA memory size (x%x) is "
12054 "less than the requested DMA memory "
12055 "size (x%x)\n", alloc_len, req_len);
12056 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12057 return -ENOMEM;
12058 }
12059
12060 /*
12061 * Get the first SGE entry from the non-embedded DMA memory. This
12062 * routine only uses a single SGE.
12063 */
12064 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12065 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12066 virt_addr = mboxq->sge_array->addr[0];
12067 /*
12068 * Configure the FCF record for FCFI 0. This is the driver's
12069 * hardcoded default and gets used in nonFIP mode.
12070 */
12071 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12072 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12073 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12074
12075 /*
12076 * Copy the fcf_index and the FCF Record Data. The data starts after
12077 * the FCoE header plus word10. The data copy needs to be endian
12078 * correct.
12079 */
12080 bytep += sizeof(uint32_t);
12081 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12082 mboxq->vport = phba->pport;
12083 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12084 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12085 if (rc == MBX_NOT_FINISHED) {
12086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12087 "2515 ADD_FCF_RECORD mailbox failed with "
12088 "status 0x%x\n", rc);
12089 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12090 rc = -EIO;
12091 } else
12092 rc = 0;
12093
12094 return rc;
12095 }
12096
12097 /**
12098 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12099 * @phba: pointer to lpfc hba data structure.
12100 * @fcf_record: pointer to the fcf record to write the default data.
12101 * @fcf_index: FCF table entry index.
12102 *
12103 * This routine is invoked to build the driver's default FCF record. The
12104 * values used are hardcoded. This routine handles memory initialization.
12105 *
12106 **/
12107 void
12108 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12109 struct fcf_record *fcf_record,
12110 uint16_t fcf_index)
12111 {
12112 memset(fcf_record, 0, sizeof(struct fcf_record));
12113 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12114 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12115 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12116 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12117 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12118 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12119 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12120 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12121 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12122 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12123 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12124 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12125 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12126 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12127 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12128 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12129 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12130 /* Set the VLAN bit map */
12131 if (phba->valid_vlan) {
12132 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12133 = 1 << (phba->vlan_id % 8);
12134 }
12135 }
12136
12137 /**
12138 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12139 * @phba: pointer to lpfc hba data structure.
12140 * @fcf_index: FCF table entry offset.
12141 *
12142 * This routine is invoked to scan the entire FCF table by reading FCF
12143 * record and processing it one at a time starting from the @fcf_index
12144 * for initial FCF discovery or fast FCF failover rediscovery.
12145 *
12146 * Return 0 if the mailbox command is submitted sucessfully, none 0
12147 * otherwise.
12148 **/
12149 int
12150 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12151 {
12152 int rc = 0, error;
12153 LPFC_MBOXQ_t *mboxq;
12154
12155 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12156 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12157 if (!mboxq) {
12158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12159 "2000 Failed to allocate mbox for "
12160 "READ_FCF cmd\n");
12161 error = -ENOMEM;
12162 goto fail_fcf_scan;
12163 }
12164 /* Construct the read FCF record mailbox command */
12165 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12166 if (rc) {
12167 error = -EINVAL;
12168 goto fail_fcf_scan;
12169 }
12170 /* Issue the mailbox command asynchronously */
12171 mboxq->vport = phba->pport;
12172 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12173 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12174 if (rc == MBX_NOT_FINISHED)
12175 error = -EIO;
12176 else {
12177 spin_lock_irq(&phba->hbalock);
12178 phba->hba_flag |= FCF_DISC_INPROGRESS;
12179 spin_unlock_irq(&phba->hbalock);
12180 /* Reset FCF round robin index bmask for new scan */
12181 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
12182 memset(phba->fcf.fcf_rr_bmask, 0,
12183 sizeof(*phba->fcf.fcf_rr_bmask));
12184 phba->fcf.eligible_fcf_cnt = 0;
12185 }
12186 error = 0;
12187 }
12188 fail_fcf_scan:
12189 if (error) {
12190 if (mboxq)
12191 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12192 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12193 spin_lock_irq(&phba->hbalock);
12194 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12195 spin_unlock_irq(&phba->hbalock);
12196 }
12197 return error;
12198 }
12199
12200 /**
12201 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12202 * @phba: pointer to lpfc hba data structure.
12203 * @fcf_index: FCF table entry offset.
12204 *
12205 * This routine is invoked to read an FCF record indicated by @fcf_index
12206 * and to use it for FLOGI round robin FCF failover.
12207 *
12208 * Return 0 if the mailbox command is submitted sucessfully, none 0
12209 * otherwise.
12210 **/
12211 int
12212 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12213 {
12214 int rc = 0, error;
12215 LPFC_MBOXQ_t *mboxq;
12216
12217 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12218 if (!mboxq) {
12219 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12220 "2763 Failed to allocate mbox for "
12221 "READ_FCF cmd\n");
12222 error = -ENOMEM;
12223 goto fail_fcf_read;
12224 }
12225 /* Construct the read FCF record mailbox command */
12226 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12227 if (rc) {
12228 error = -EINVAL;
12229 goto fail_fcf_read;
12230 }
12231 /* Issue the mailbox command asynchronously */
12232 mboxq->vport = phba->pport;
12233 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12234 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12235 if (rc == MBX_NOT_FINISHED)
12236 error = -EIO;
12237 else
12238 error = 0;
12239
12240 fail_fcf_read:
12241 if (error && mboxq)
12242 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12243 return error;
12244 }
12245
12246 /**
12247 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12248 * @phba: pointer to lpfc hba data structure.
12249 * @fcf_index: FCF table entry offset.
12250 *
12251 * This routine is invoked to read an FCF record indicated by @fcf_index to
12252 * determine whether it's eligible for FLOGI round robin failover list.
12253 *
12254 * Return 0 if the mailbox command is submitted sucessfully, none 0
12255 * otherwise.
12256 **/
12257 int
12258 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12259 {
12260 int rc = 0, error;
12261 LPFC_MBOXQ_t *mboxq;
12262
12263 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12264 if (!mboxq) {
12265 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12266 "2758 Failed to allocate mbox for "
12267 "READ_FCF cmd\n");
12268 error = -ENOMEM;
12269 goto fail_fcf_read;
12270 }
12271 /* Construct the read FCF record mailbox command */
12272 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12273 if (rc) {
12274 error = -EINVAL;
12275 goto fail_fcf_read;
12276 }
12277 /* Issue the mailbox command asynchronously */
12278 mboxq->vport = phba->pport;
12279 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12281 if (rc == MBX_NOT_FINISHED)
12282 error = -EIO;
12283 else
12284 error = 0;
12285
12286 fail_fcf_read:
12287 if (error && mboxq)
12288 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12289 return error;
12290 }
12291
12292 /**
12293 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12294 * @phba: pointer to lpfc hba data structure.
12295 *
12296 * This routine is to get the next eligible FCF record index in a round
12297 * robin fashion. If the next eligible FCF record index equals to the
12298 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12299 * shall be returned, otherwise, the next eligible FCF record's index
12300 * shall be returned.
12301 **/
12302 uint16_t
12303 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12304 {
12305 uint16_t next_fcf_index;
12306
12307 /* Search from the currently registered FCF index */
12308 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12309 LPFC_SLI4_FCF_TBL_INDX_MAX,
12310 phba->fcf.current_rec.fcf_indx);
12311 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12312 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12313 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12314 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12315 /* Round robin failover stop condition */
12316 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12317 return LPFC_FCOE_FCF_NEXT_NONE;
12318
12319 return next_fcf_index;
12320 }
12321
12322 /**
12323 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12324 * @phba: pointer to lpfc hba data structure.
12325 *
12326 * This routine sets the FCF record index in to the eligible bmask for
12327 * round robin failover search. It checks to make sure that the index
12328 * does not go beyond the range of the driver allocated bmask dimension
12329 * before setting the bit.
12330 *
12331 * Returns 0 if the index bit successfully set, otherwise, it returns
12332 * -EINVAL.
12333 **/
12334 int
12335 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12336 {
12337 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12338 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12339 "2610 HBA FCF index reached driver's "
12340 "book keeping dimension: fcf_index:%d, "
12341 "driver_bmask_max:%d\n",
12342 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12343 return -EINVAL;
12344 }
12345 /* Set the eligible FCF record index bmask */
12346 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12347
12348 return 0;
12349 }
12350
12351 /**
12352 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12353 * @phba: pointer to lpfc hba data structure.
12354 *
12355 * This routine clears the FCF record index from the eligible bmask for
12356 * round robin failover search. It checks to make sure that the index
12357 * does not go beyond the range of the driver allocated bmask dimension
12358 * before clearing the bit.
12359 **/
12360 void
12361 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12362 {
12363 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12364 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12365 "2762 HBA FCF index goes beyond driver's "
12366 "book keeping dimension: fcf_index:%d, "
12367 "driver_bmask_max:%d\n",
12368 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12369 return;
12370 }
12371 /* Clear the eligible FCF record index bmask */
12372 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12373 }
12374
12375 /**
12376 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12377 * @phba: pointer to lpfc hba data structure.
12378 *
12379 * This routine is the completion routine for the rediscover FCF table mailbox
12380 * command. If the mailbox command returned failure, it will try to stop the
12381 * FCF rediscover wait timer.
12382 **/
12383 void
12384 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12385 {
12386 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12387 uint32_t shdr_status, shdr_add_status;
12388
12389 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12390
12391 shdr_status = bf_get(lpfc_mbox_hdr_status,
12392 &redisc_fcf->header.cfg_shdr.response);
12393 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12394 &redisc_fcf->header.cfg_shdr.response);
12395 if (shdr_status || shdr_add_status) {
12396 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12397 "2746 Requesting for FCF rediscovery failed "
12398 "status x%x add_status x%x\n",
12399 shdr_status, shdr_add_status);
12400 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
12401 spin_lock_irq(&phba->hbalock);
12402 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
12403 spin_unlock_irq(&phba->hbalock);
12404 /*
12405 * CVL event triggered FCF rediscover request failed,
12406 * last resort to re-try current registered FCF entry.
12407 */
12408 lpfc_retry_pport_discovery(phba);
12409 } else {
12410 spin_lock_irq(&phba->hbalock);
12411 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
12412 spin_unlock_irq(&phba->hbalock);
12413 /*
12414 * DEAD FCF event triggered FCF rediscover request
12415 * failed, last resort to fail over as a link down
12416 * to FCF registration.
12417 */
12418 lpfc_sli4_fcf_dead_failthrough(phba);
12419 }
12420 } else {
12421 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12422 "2775 Start FCF rediscovery quiescent period "
12423 "wait timer before scaning FCF table\n");
12424 /*
12425 * Start FCF rediscovery wait timer for pending FCF
12426 * before rescan FCF record table.
12427 */
12428 lpfc_fcf_redisc_wait_start_timer(phba);
12429 }
12430
12431 mempool_free(mbox, phba->mbox_mem_pool);
12432 }
12433
12434 /**
12435 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12436 * @phba: pointer to lpfc hba data structure.
12437 *
12438 * This routine is invoked to request for rediscovery of the entire FCF table
12439 * by the port.
12440 **/
12441 int
12442 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12443 {
12444 LPFC_MBOXQ_t *mbox;
12445 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12446 int rc, length;
12447
12448 /* Cancel retry delay timers to all vports before FCF rediscover */
12449 lpfc_cancel_all_vport_retry_delay_timer(phba);
12450
12451 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12452 if (!mbox) {
12453 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12454 "2745 Failed to allocate mbox for "
12455 "requesting FCF rediscover.\n");
12456 return -ENOMEM;
12457 }
12458
12459 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12460 sizeof(struct lpfc_sli4_cfg_mhdr));
12461 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12462 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12463 length, LPFC_SLI4_MBX_EMBED);
12464
12465 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12466 /* Set count to 0 for invalidating the entire FCF database */
12467 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12468
12469 /* Issue the mailbox command asynchronously */
12470 mbox->vport = phba->pport;
12471 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12472 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12473
12474 if (rc == MBX_NOT_FINISHED) {
12475 mempool_free(mbox, phba->mbox_mem_pool);
12476 return -EIO;
12477 }
12478 return 0;
12479 }
12480
12481 /**
12482 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12483 * @phba: pointer to lpfc hba data structure.
12484 *
12485 * This function is the failover routine as a last resort to the FCF DEAD
12486 * event when driver failed to perform fast FCF failover.
12487 **/
12488 void
12489 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12490 {
12491 uint32_t link_state;
12492
12493 /*
12494 * Last resort as FCF DEAD event failover will treat this as
12495 * a link down, but save the link state because we don't want
12496 * it to be changed to Link Down unless it is already down.
12497 */
12498 link_state = phba->link_state;
12499 lpfc_linkdown(phba);
12500 phba->link_state = link_state;
12501
12502 /* Unregister FCF if no devices connected to it */
12503 lpfc_unregister_unused_fcf(phba);
12504 }
12505
12506 /**
12507 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12508 * @phba: pointer to lpfc hba data structure.
12509 *
12510 * This function read region 23 and parse TLV for port status to
12511 * decide if the user disaled the port. If the TLV indicates the
12512 * port is disabled, the hba_flag is set accordingly.
12513 **/
12514 void
12515 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12516 {
12517 LPFC_MBOXQ_t *pmb = NULL;
12518 MAILBOX_t *mb;
12519 uint8_t *rgn23_data = NULL;
12520 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12521 int rc;
12522
12523 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12524 if (!pmb) {
12525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12526 "2600 lpfc_sli_read_serdes_param failed to"
12527 " allocate mailbox memory\n");
12528 goto out;
12529 }
12530 mb = &pmb->u.mb;
12531
12532 /* Get adapter Region 23 data */
12533 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12534 if (!rgn23_data)
12535 goto out;
12536
12537 do {
12538 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12539 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12540
12541 if (rc != MBX_SUCCESS) {
12542 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12543 "2601 lpfc_sli_read_link_ste failed to"
12544 " read config region 23 rc 0x%x Status 0x%x\n",
12545 rc, mb->mbxStatus);
12546 mb->un.varDmp.word_cnt = 0;
12547 }
12548 /*
12549 * dump mem may return a zero when finished or we got a
12550 * mailbox error, either way we are done.
12551 */
12552 if (mb->un.varDmp.word_cnt == 0)
12553 break;
12554 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12555 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12556
12557 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12558 rgn23_data + offset,
12559 mb->un.varDmp.word_cnt);
12560 offset += mb->un.varDmp.word_cnt;
12561 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12562
12563 data_size = offset;
12564 offset = 0;
12565
12566 if (!data_size)
12567 goto out;
12568
12569 /* Check the region signature first */
12570 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12572 "2619 Config region 23 has bad signature\n");
12573 goto out;
12574 }
12575 offset += 4;
12576
12577 /* Check the data structure version */
12578 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12580 "2620 Config region 23 has bad version\n");
12581 goto out;
12582 }
12583 offset += 4;
12584
12585 /* Parse TLV entries in the region */
12586 while (offset < data_size) {
12587 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12588 break;
12589 /*
12590 * If the TLV is not driver specific TLV or driver id is
12591 * not linux driver id, skip the record.
12592 */
12593 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12594 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12595 (rgn23_data[offset + 3] != 0)) {
12596 offset += rgn23_data[offset + 1] * 4 + 4;
12597 continue;
12598 }
12599
12600 /* Driver found a driver specific TLV in the config region */
12601 sub_tlv_len = rgn23_data[offset + 1] * 4;
12602 offset += 4;
12603 tlv_offset = 0;
12604
12605 /*
12606 * Search for configured port state sub-TLV.
12607 */
12608 while ((offset < data_size) &&
12609 (tlv_offset < sub_tlv_len)) {
12610 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12611 offset += 4;
12612 tlv_offset += 4;
12613 break;
12614 }
12615 if (rgn23_data[offset] != PORT_STE_TYPE) {
12616 offset += rgn23_data[offset + 1] * 4 + 4;
12617 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12618 continue;
12619 }
12620
12621 /* This HBA contains PORT_STE configured */
12622 if (!rgn23_data[offset + 2])
12623 phba->hba_flag |= LINK_DISABLED;
12624
12625 goto out;
12626 }
12627 }
12628 out:
12629 if (pmb)
12630 mempool_free(pmb, phba->mbox_mem_pool);
12631 kfree(rgn23_data);
12632 return;
12633 }
12634
12635 /**
12636 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12637 * @vport: pointer to vport data structure.
12638 *
12639 * This function iterate through the mailboxq and clean up all REG_LOGIN
12640 * and REG_VPI mailbox commands associated with the vport. This function
12641 * is called when driver want to restart discovery of the vport due to
12642 * a Clear Virtual Link event.
12643 **/
12644 void
12645 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12646 {
12647 struct lpfc_hba *phba = vport->phba;
12648 LPFC_MBOXQ_t *mb, *nextmb;
12649 struct lpfc_dmabuf *mp;
12650 struct lpfc_nodelist *ndlp;
12651
12652 spin_lock_irq(&phba->hbalock);
12653 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12654 if (mb->vport != vport)
12655 continue;
12656
12657 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12658 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12659 continue;
12660
12661 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12662 mp = (struct lpfc_dmabuf *) (mb->context1);
12663 if (mp) {
12664 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12665 kfree(mp);
12666 }
12667 ndlp = (struct lpfc_nodelist *) mb->context2;
12668 if (ndlp) {
12669 lpfc_nlp_put(ndlp);
12670 mb->context2 = NULL;
12671 }
12672 }
12673 list_del(&mb->list);
12674 mempool_free(mb, phba->mbox_mem_pool);
12675 }
12676 mb = phba->sli.mbox_active;
12677 if (mb && (mb->vport == vport)) {
12678 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12679 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12680 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12681 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12682 ndlp = (struct lpfc_nodelist *) mb->context2;
12683 if (ndlp) {
12684 lpfc_nlp_put(ndlp);
12685 mb->context2 = NULL;
12686 }
12687 /* Unregister the RPI when mailbox complete */
12688 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12689 }
12690 }
12691 spin_unlock_irq(&phba->hbalock);
12692 }
12693