/kern_2.6.32/drivers/scsi/lpfc/lpfc_sli.c

http://omnia2droid.googlecode.com/ · C · 11727 lines · 7311 code · 1016 blank · 3400 comment · 1121 complexity · 2dbf256a4c0bdc2091ab8a5cb5e4b7e7 MD5 · raw file

  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. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_disc.h"
  37. #include "lpfc_scsi.h"
  38. #include "lpfc.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_logmsg.h"
  41. #include "lpfc_compat.h"
  42. #include "lpfc_debugfs.h"
  43. #include "lpfc_vport.h"
  44. /* There are only four IOCB completion types. */
  45. typedef enum _lpfc_iocb_type {
  46. LPFC_UNKNOWN_IOCB,
  47. LPFC_UNSOL_IOCB,
  48. LPFC_SOL_IOCB,
  49. LPFC_ABORT_IOCB
  50. } lpfc_iocb_type;
  51. /* Provide function prototypes local to this module. */
  52. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  53. uint32_t);
  54. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint8_t *, uint32_t *);
  56. static IOCB_t *
  57. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  58. {
  59. return &iocbq->iocb;
  60. }
  61. /**
  62. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  63. * @q: The Work Queue to operate on.
  64. * @wqe: The work Queue Entry to put on the Work queue.
  65. *
  66. * This routine will copy the contents of @wqe to the next available entry on
  67. * the @q. This function will then ring the Work Queue Doorbell to signal the
  68. * HBA to start processing the Work Queue Entry. This function returns 0 if
  69. * successful. If no entries are available on @q then this function will return
  70. * -ENOMEM.
  71. * The caller is expected to hold the hbalock when calling this routine.
  72. **/
  73. static uint32_t
  74. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  75. {
  76. union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
  77. struct lpfc_register doorbell;
  78. uint32_t host_index;
  79. /* If the host has not yet processed the next entry then we are done */
  80. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  81. return -ENOMEM;
  82. /* set consumption flag every once in a while */
  83. if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
  84. bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
  85. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  86. /* Update the host index before invoking device */
  87. host_index = q->host_index;
  88. q->host_index = ((q->host_index + 1) % q->entry_count);
  89. /* Ring Doorbell */
  90. doorbell.word0 = 0;
  91. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  92. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  93. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  94. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  95. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  96. return 0;
  97. }
  98. /**
  99. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  100. * @q: The Work Queue to operate on.
  101. * @index: The index to advance the hba index to.
  102. *
  103. * This routine will update the HBA index of a queue to reflect consumption of
  104. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  105. * an entry the host calls this function to update the queue's internal
  106. * pointers. This routine returns the number of entries that were consumed by
  107. * the HBA.
  108. **/
  109. static uint32_t
  110. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  111. {
  112. uint32_t released = 0;
  113. if (q->hba_index == index)
  114. return 0;
  115. do {
  116. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  117. released++;
  118. } while (q->hba_index != index);
  119. return released;
  120. }
  121. /**
  122. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  123. * @q: The Mailbox Queue to operate on.
  124. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  125. *
  126. * This routine will copy the contents of @mqe to the next available entry on
  127. * the @q. This function will then ring the Work Queue Doorbell to signal the
  128. * HBA to start processing the Work Queue Entry. This function returns 0 if
  129. * successful. If no entries are available on @q then this function will return
  130. * -ENOMEM.
  131. * The caller is expected to hold the hbalock when calling this routine.
  132. **/
  133. static uint32_t
  134. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  135. {
  136. struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
  137. struct lpfc_register doorbell;
  138. uint32_t host_index;
  139. /* If the host has not yet processed the next entry then we are done */
  140. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  141. return -ENOMEM;
  142. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  143. /* Save off the mailbox pointer for completion */
  144. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  145. /* Update the host index before invoking device */
  146. host_index = q->host_index;
  147. q->host_index = ((q->host_index + 1) % q->entry_count);
  148. /* Ring Doorbell */
  149. doorbell.word0 = 0;
  150. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  151. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  152. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  153. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  154. return 0;
  155. }
  156. /**
  157. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  158. * @q: The Mailbox Queue to operate on.
  159. *
  160. * This routine will update the HBA index of a queue to reflect consumption of
  161. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  162. * an entry the host calls this function to update the queue's internal
  163. * pointers. This routine returns the number of entries that were consumed by
  164. * the HBA.
  165. **/
  166. static uint32_t
  167. lpfc_sli4_mq_release(struct lpfc_queue *q)
  168. {
  169. /* Clear the mailbox pointer for completion */
  170. q->phba->mbox = NULL;
  171. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  172. return 1;
  173. }
  174. /**
  175. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  176. * @q: The Event Queue to get the first valid EQE from
  177. *
  178. * This routine will get the first valid Event Queue Entry from @q, update
  179. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  180. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  181. * processed, but not popped back to the HBA then this routine will return NULL.
  182. **/
  183. static struct lpfc_eqe *
  184. lpfc_sli4_eq_get(struct lpfc_queue *q)
  185. {
  186. struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
  187. /* If the next EQE is not valid then we are done */
  188. if (!bf_get(lpfc_eqe_valid, eqe))
  189. return NULL;
  190. /* If the host has not yet processed the next entry then we are done */
  191. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  192. return NULL;
  193. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  194. return eqe;
  195. }
  196. /**
  197. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  198. * @q: The Event Queue that the host has completed processing for.
  199. * @arm: Indicates whether the host wants to arms this CQ.
  200. *
  201. * This routine will mark all Event Queue Entries on @q, from the last
  202. * known completed entry to the last entry that was processed, as completed
  203. * by clearing the valid bit for each completion queue entry. Then it will
  204. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  205. * The internal host index in the @q will be updated by this routine to indicate
  206. * that the host has finished processing the entries. The @arm parameter
  207. * indicates that the queue should be rearmed when ringing the doorbell.
  208. *
  209. * This function will return the number of EQEs that were popped.
  210. **/
  211. uint32_t
  212. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  213. {
  214. uint32_t released = 0;
  215. struct lpfc_eqe *temp_eqe;
  216. struct lpfc_register doorbell;
  217. /* while there are valid entries */
  218. while (q->hba_index != q->host_index) {
  219. temp_eqe = q->qe[q->host_index].eqe;
  220. bf_set(lpfc_eqe_valid, temp_eqe, 0);
  221. released++;
  222. q->host_index = ((q->host_index + 1) % q->entry_count);
  223. }
  224. if (unlikely(released == 0 && !arm))
  225. return 0;
  226. /* ring doorbell for number popped */
  227. doorbell.word0 = 0;
  228. if (arm) {
  229. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  230. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  231. }
  232. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  233. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  234. bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
  235. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  236. return released;
  237. }
  238. /**
  239. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  240. * @q: The Completion Queue to get the first valid CQE from
  241. *
  242. * This routine will get the first valid Completion Queue Entry from @q, update
  243. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  244. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  245. * processed, but not popped back to the HBA then this routine will return NULL.
  246. **/
  247. static struct lpfc_cqe *
  248. lpfc_sli4_cq_get(struct lpfc_queue *q)
  249. {
  250. struct lpfc_cqe *cqe;
  251. /* If the next CQE is not valid then we are done */
  252. if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  253. return NULL;
  254. /* If the host has not yet processed the next entry then we are done */
  255. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  256. return NULL;
  257. cqe = q->qe[q->hba_index].cqe;
  258. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  259. return cqe;
  260. }
  261. /**
  262. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  263. * @q: The Completion Queue that the host has completed processing for.
  264. * @arm: Indicates whether the host wants to arms this CQ.
  265. *
  266. * This routine will mark all Completion queue entries on @q, from the last
  267. * known completed entry to the last entry that was processed, as completed
  268. * by clearing the valid bit for each completion queue entry. Then it will
  269. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  270. * The internal host index in the @q will be updated by this routine to indicate
  271. * that the host has finished processing the entries. The @arm parameter
  272. * indicates that the queue should be rearmed when ringing the doorbell.
  273. *
  274. * This function will return the number of CQEs that were released.
  275. **/
  276. uint32_t
  277. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  278. {
  279. uint32_t released = 0;
  280. struct lpfc_cqe *temp_qe;
  281. struct lpfc_register doorbell;
  282. /* while there are valid entries */
  283. while (q->hba_index != q->host_index) {
  284. temp_qe = q->qe[q->host_index].cqe;
  285. bf_set(lpfc_cqe_valid, temp_qe, 0);
  286. released++;
  287. q->host_index = ((q->host_index + 1) % q->entry_count);
  288. }
  289. if (unlikely(released == 0 && !arm))
  290. return 0;
  291. /* ring doorbell for number popped */
  292. doorbell.word0 = 0;
  293. if (arm)
  294. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  295. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  296. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  297. bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
  298. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  299. return released;
  300. }
  301. /**
  302. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  303. * @q: The Header Receive Queue to operate on.
  304. * @wqe: The Receive Queue Entry to put on the Receive queue.
  305. *
  306. * This routine will copy the contents of @wqe to the next available entry on
  307. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  308. * HBA to start processing the Receive Queue Entry. This function returns the
  309. * index that the rqe was copied to if successful. If no entries are available
  310. * on @q then this function will return -ENOMEM.
  311. * The caller is expected to hold the hbalock when calling this routine.
  312. **/
  313. static int
  314. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  315. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  316. {
  317. struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
  318. struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
  319. struct lpfc_register doorbell;
  320. int put_index = hq->host_index;
  321. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  322. return -EINVAL;
  323. if (hq->host_index != dq->host_index)
  324. return -EINVAL;
  325. /* If the host has not yet processed the next entry then we are done */
  326. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  327. return -EBUSY;
  328. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  329. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  330. /* Update the host index to point to the next slot */
  331. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  332. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  333. /* Ring The Header Receive Queue Doorbell */
  334. if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
  335. doorbell.word0 = 0;
  336. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  337. LPFC_RQ_POST_BATCH);
  338. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  339. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  340. }
  341. return put_index;
  342. }
  343. /**
  344. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  345. * @q: The Header Receive Queue to operate on.
  346. *
  347. * This routine will update the HBA index of a queue to reflect consumption of
  348. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  349. * consumed an entry the host calls this function to update the queue's
  350. * internal pointers. This routine returns the number of entries that were
  351. * consumed by the HBA.
  352. **/
  353. static uint32_t
  354. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  355. {
  356. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  357. return 0;
  358. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  359. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  360. return 1;
  361. }
  362. /**
  363. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  364. * @phba: Pointer to HBA context object.
  365. * @pring: Pointer to driver SLI ring object.
  366. *
  367. * This function returns pointer to next command iocb entry
  368. * in the command ring. The caller must hold hbalock to prevent
  369. * other threads consume the next command iocb.
  370. * SLI-2/SLI-3 provide different sized iocbs.
  371. **/
  372. static inline IOCB_t *
  373. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  374. {
  375. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  376. pring->cmdidx * phba->iocb_cmd_size);
  377. }
  378. /**
  379. * lpfc_resp_iocb - Get next response iocb entry in the ring
  380. * @phba: Pointer to HBA context object.
  381. * @pring: Pointer to driver SLI ring object.
  382. *
  383. * This function returns pointer to next response iocb entry
  384. * in the response ring. The caller must hold hbalock to make sure
  385. * that no other thread consume the next response iocb.
  386. * SLI-2/SLI-3 provide different sized iocbs.
  387. **/
  388. static inline IOCB_t *
  389. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  390. {
  391. return (IOCB_t *) (((char *) pring->rspringaddr) +
  392. pring->rspidx * phba->iocb_rsp_size);
  393. }
  394. /**
  395. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  396. * @phba: Pointer to HBA context object.
  397. *
  398. * This function is called with hbalock held. This function
  399. * allocates a new driver iocb object from the iocb pool. If the
  400. * allocation is successful, it returns pointer to the newly
  401. * allocated iocb object else it returns NULL.
  402. **/
  403. static struct lpfc_iocbq *
  404. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  405. {
  406. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  407. struct lpfc_iocbq * iocbq = NULL;
  408. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  409. return iocbq;
  410. }
  411. /**
  412. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  413. * @phba: Pointer to HBA context object.
  414. * @xritag: XRI value.
  415. *
  416. * This function clears the sglq pointer from the array of acive
  417. * sglq's. The xritag that is passed in is used to index into the
  418. * array. Before the xritag can be used it needs to be adjusted
  419. * by subtracting the xribase.
  420. *
  421. * Returns sglq ponter = success, NULL = Failure.
  422. **/
  423. static struct lpfc_sglq *
  424. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  425. {
  426. uint16_t adj_xri;
  427. struct lpfc_sglq *sglq;
  428. adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
  429. if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
  430. return NULL;
  431. sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
  432. phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
  433. return sglq;
  434. }
  435. /**
  436. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  437. * @phba: Pointer to HBA context object.
  438. * @xritag: XRI value.
  439. *
  440. * This function returns the sglq pointer from the array of acive
  441. * sglq's. The xritag that is passed in is used to index into the
  442. * array. Before the xritag can be used it needs to be adjusted
  443. * by subtracting the xribase.
  444. *
  445. * Returns sglq ponter = success, NULL = Failure.
  446. **/
  447. static struct lpfc_sglq *
  448. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  449. {
  450. uint16_t adj_xri;
  451. struct lpfc_sglq *sglq;
  452. adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
  453. if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
  454. return NULL;
  455. sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
  456. return sglq;
  457. }
  458. /**
  459. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  460. * @phba: Pointer to HBA context object.
  461. *
  462. * This function is called with hbalock held. This function
  463. * Gets a new driver sglq object from the sglq list. If the
  464. * list is not empty then it is successful, it returns pointer to the newly
  465. * allocated sglq object else it returns NULL.
  466. **/
  467. static struct lpfc_sglq *
  468. __lpfc_sli_get_sglq(struct lpfc_hba *phba)
  469. {
  470. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  471. struct lpfc_sglq *sglq = NULL;
  472. uint16_t adj_xri;
  473. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  474. adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
  475. phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
  476. return sglq;
  477. }
  478. /**
  479. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  480. * @phba: Pointer to HBA context object.
  481. *
  482. * This function is called with no lock held. This function
  483. * allocates a new driver iocb object from the iocb pool. If the
  484. * allocation is successful, it returns pointer to the newly
  485. * allocated iocb object else it returns NULL.
  486. **/
  487. struct lpfc_iocbq *
  488. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  489. {
  490. struct lpfc_iocbq * iocbq = NULL;
  491. unsigned long iflags;
  492. spin_lock_irqsave(&phba->hbalock, iflags);
  493. iocbq = __lpfc_sli_get_iocbq(phba);
  494. spin_unlock_irqrestore(&phba->hbalock, iflags);
  495. return iocbq;
  496. }
  497. /**
  498. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  499. * @phba: Pointer to HBA context object.
  500. * @iocbq: Pointer to driver iocb object.
  501. *
  502. * This function is called with hbalock held to release driver
  503. * iocb object to the iocb pool. The iotag in the iocb object
  504. * does not change for each use of the iocb object. This function
  505. * clears all other fields of the iocb object when it is freed.
  506. * The sqlq structure that holds the xritag and phys and virtual
  507. * mappings for the scatter gather list is retrieved from the
  508. * active array of sglq. The get of the sglq pointer also clears
  509. * the entry in the array. If the status of the IO indiactes that
  510. * this IO was aborted then the sglq entry it put on the
  511. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  512. * IO has good status or fails for any other reason then the sglq
  513. * entry is added to the free list (lpfc_sgl_list).
  514. **/
  515. static void
  516. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  517. {
  518. struct lpfc_sglq *sglq;
  519. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  520. unsigned long iflag;
  521. if (iocbq->sli4_xritag == NO_XRI)
  522. sglq = NULL;
  523. else
  524. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
  525. if (sglq) {
  526. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
  527. || ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  528. && (iocbq->iocb.un.ulpWord[4]
  529. == IOERR_SLI_ABORTED))) {
  530. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  531. iflag);
  532. list_add(&sglq->list,
  533. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  534. spin_unlock_irqrestore(
  535. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  536. } else
  537. list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
  538. }
  539. /*
  540. * Clean all volatile data fields, preserve iotag and node struct.
  541. */
  542. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  543. iocbq->sli4_xritag = NO_XRI;
  544. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  545. }
  546. /**
  547. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  548. * @phba: Pointer to HBA context object.
  549. * @iocbq: Pointer to driver iocb object.
  550. *
  551. * This function is called with hbalock held to release driver
  552. * iocb object to the iocb pool. The iotag in the iocb object
  553. * does not change for each use of the iocb object. This function
  554. * clears all other fields of the iocb object when it is freed.
  555. **/
  556. static void
  557. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  558. {
  559. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  560. /*
  561. * Clean all volatile data fields, preserve iotag and node struct.
  562. */
  563. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  564. iocbq->sli4_xritag = NO_XRI;
  565. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  566. }
  567. /**
  568. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  569. * @phba: Pointer to HBA context object.
  570. * @iocbq: Pointer to driver iocb object.
  571. *
  572. * This function is called with hbalock held to release driver
  573. * iocb object to the iocb pool. The iotag in the iocb object
  574. * does not change for each use of the iocb object. This function
  575. * clears all other fields of the iocb object when it is freed.
  576. **/
  577. static void
  578. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  579. {
  580. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  581. }
  582. /**
  583. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  584. * @phba: Pointer to HBA context object.
  585. * @iocbq: Pointer to driver iocb object.
  586. *
  587. * This function is called with no lock held to release the iocb to
  588. * iocb pool.
  589. **/
  590. void
  591. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  592. {
  593. unsigned long iflags;
  594. /*
  595. * Clean all volatile data fields, preserve iotag and node struct.
  596. */
  597. spin_lock_irqsave(&phba->hbalock, iflags);
  598. __lpfc_sli_release_iocbq(phba, iocbq);
  599. spin_unlock_irqrestore(&phba->hbalock, iflags);
  600. }
  601. /**
  602. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  603. * @phba: Pointer to HBA context object.
  604. * @iocblist: List of IOCBs.
  605. * @ulpstatus: ULP status in IOCB command field.
  606. * @ulpWord4: ULP word-4 in IOCB command field.
  607. *
  608. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  609. * on the list by invoking the complete callback function associated with the
  610. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  611. * fields.
  612. **/
  613. void
  614. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  615. uint32_t ulpstatus, uint32_t ulpWord4)
  616. {
  617. struct lpfc_iocbq *piocb;
  618. while (!list_empty(iocblist)) {
  619. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  620. if (!piocb->iocb_cmpl)
  621. lpfc_sli_release_iocbq(phba, piocb);
  622. else {
  623. piocb->iocb.ulpStatus = ulpstatus;
  624. piocb->iocb.un.ulpWord[4] = ulpWord4;
  625. (piocb->iocb_cmpl) (phba, piocb, piocb);
  626. }
  627. }
  628. return;
  629. }
  630. /**
  631. * lpfc_sli_iocb_cmd_type - Get the iocb type
  632. * @iocb_cmnd: iocb command code.
  633. *
  634. * This function is called by ring event handler function to get the iocb type.
  635. * This function translates the iocb command to an iocb command type used to
  636. * decide the final disposition of each completed IOCB.
  637. * The function returns
  638. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  639. * LPFC_SOL_IOCB if it is a solicited iocb completion
  640. * LPFC_ABORT_IOCB if it is an abort iocb
  641. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  642. *
  643. * The caller is not required to hold any lock.
  644. **/
  645. static lpfc_iocb_type
  646. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  647. {
  648. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  649. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  650. return 0;
  651. switch (iocb_cmnd) {
  652. case CMD_XMIT_SEQUENCE_CR:
  653. case CMD_XMIT_SEQUENCE_CX:
  654. case CMD_XMIT_BCAST_CN:
  655. case CMD_XMIT_BCAST_CX:
  656. case CMD_ELS_REQUEST_CR:
  657. case CMD_ELS_REQUEST_CX:
  658. case CMD_CREATE_XRI_CR:
  659. case CMD_CREATE_XRI_CX:
  660. case CMD_GET_RPI_CN:
  661. case CMD_XMIT_ELS_RSP_CX:
  662. case CMD_GET_RPI_CR:
  663. case CMD_FCP_IWRITE_CR:
  664. case CMD_FCP_IWRITE_CX:
  665. case CMD_FCP_IREAD_CR:
  666. case CMD_FCP_IREAD_CX:
  667. case CMD_FCP_ICMND_CR:
  668. case CMD_FCP_ICMND_CX:
  669. case CMD_FCP_TSEND_CX:
  670. case CMD_FCP_TRSP_CX:
  671. case CMD_FCP_TRECEIVE_CX:
  672. case CMD_FCP_AUTO_TRSP_CX:
  673. case CMD_ADAPTER_MSG:
  674. case CMD_ADAPTER_DUMP:
  675. case CMD_XMIT_SEQUENCE64_CR:
  676. case CMD_XMIT_SEQUENCE64_CX:
  677. case CMD_XMIT_BCAST64_CN:
  678. case CMD_XMIT_BCAST64_CX:
  679. case CMD_ELS_REQUEST64_CR:
  680. case CMD_ELS_REQUEST64_CX:
  681. case CMD_FCP_IWRITE64_CR:
  682. case CMD_FCP_IWRITE64_CX:
  683. case CMD_FCP_IREAD64_CR:
  684. case CMD_FCP_IREAD64_CX:
  685. case CMD_FCP_ICMND64_CR:
  686. case CMD_FCP_ICMND64_CX:
  687. case CMD_FCP_TSEND64_CX:
  688. case CMD_FCP_TRSP64_CX:
  689. case CMD_FCP_TRECEIVE64_CX:
  690. case CMD_GEN_REQUEST64_CR:
  691. case CMD_GEN_REQUEST64_CX:
  692. case CMD_XMIT_ELS_RSP64_CX:
  693. case DSSCMD_IWRITE64_CR:
  694. case DSSCMD_IWRITE64_CX:
  695. case DSSCMD_IREAD64_CR:
  696. case DSSCMD_IREAD64_CX:
  697. case DSSCMD_INVALIDATE_DEK:
  698. case DSSCMD_SET_KEK:
  699. case DSSCMD_GET_KEK_ID:
  700. case DSSCMD_GEN_XFER:
  701. type = LPFC_SOL_IOCB;
  702. break;
  703. case CMD_ABORT_XRI_CN:
  704. case CMD_ABORT_XRI_CX:
  705. case CMD_CLOSE_XRI_CN:
  706. case CMD_CLOSE_XRI_CX:
  707. case CMD_XRI_ABORTED_CX:
  708. case CMD_ABORT_MXRI64_CN:
  709. type = LPFC_ABORT_IOCB;
  710. break;
  711. case CMD_RCV_SEQUENCE_CX:
  712. case CMD_RCV_ELS_REQ_CX:
  713. case CMD_RCV_SEQUENCE64_CX:
  714. case CMD_RCV_ELS_REQ64_CX:
  715. case CMD_ASYNC_STATUS:
  716. case CMD_IOCB_RCV_SEQ64_CX:
  717. case CMD_IOCB_RCV_ELS64_CX:
  718. case CMD_IOCB_RCV_CONT64_CX:
  719. case CMD_IOCB_RET_XRI64_CX:
  720. type = LPFC_UNSOL_IOCB;
  721. break;
  722. case CMD_IOCB_XMIT_MSEQ64_CR:
  723. case CMD_IOCB_XMIT_MSEQ64_CX:
  724. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  725. case CMD_IOCB_RCV_ELS_LIST64_CX:
  726. case CMD_IOCB_CLOSE_EXTENDED_CN:
  727. case CMD_IOCB_ABORT_EXTENDED_CN:
  728. case CMD_IOCB_RET_HBQE64_CN:
  729. case CMD_IOCB_FCP_IBIDIR64_CR:
  730. case CMD_IOCB_FCP_IBIDIR64_CX:
  731. case CMD_IOCB_FCP_ITASKMGT64_CX:
  732. case CMD_IOCB_LOGENTRY_CN:
  733. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  734. printk("%s - Unhandled SLI-3 Command x%x\n",
  735. __func__, iocb_cmnd);
  736. type = LPFC_UNKNOWN_IOCB;
  737. break;
  738. default:
  739. type = LPFC_UNKNOWN_IOCB;
  740. break;
  741. }
  742. return type;
  743. }
  744. /**
  745. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  746. * @phba: Pointer to HBA context object.
  747. *
  748. * This function is called from SLI initialization code
  749. * to configure every ring of the HBA's SLI interface. The
  750. * caller is not required to hold any lock. This function issues
  751. * a config_ring mailbox command for each ring.
  752. * This function returns zero if successful else returns a negative
  753. * error code.
  754. **/
  755. static int
  756. lpfc_sli_ring_map(struct lpfc_hba *phba)
  757. {
  758. struct lpfc_sli *psli = &phba->sli;
  759. LPFC_MBOXQ_t *pmb;
  760. MAILBOX_t *pmbox;
  761. int i, rc, ret = 0;
  762. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  763. if (!pmb)
  764. return -ENOMEM;
  765. pmbox = &pmb->u.mb;
  766. phba->link_state = LPFC_INIT_MBX_CMDS;
  767. for (i = 0; i < psli->num_rings; i++) {
  768. lpfc_config_ring(phba, i, pmb);
  769. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  770. if (rc != MBX_SUCCESS) {
  771. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  772. "0446 Adapter failed to init (%d), "
  773. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  774. "ring %d\n",
  775. rc, pmbox->mbxCommand,
  776. pmbox->mbxStatus, i);
  777. phba->link_state = LPFC_HBA_ERROR;
  778. ret = -ENXIO;
  779. break;
  780. }
  781. }
  782. mempool_free(pmb, phba->mbox_mem_pool);
  783. return ret;
  784. }
  785. /**
  786. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  787. * @phba: Pointer to HBA context object.
  788. * @pring: Pointer to driver SLI ring object.
  789. * @piocb: Pointer to the driver iocb object.
  790. *
  791. * This function is called with hbalock held. The function adds the
  792. * new iocb to txcmplq of the given ring. This function always returns
  793. * 0. If this function is called for ELS ring, this function checks if
  794. * there is a vport associated with the ELS command. This function also
  795. * starts els_tmofunc timer if this is an ELS command.
  796. **/
  797. static int
  798. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  799. struct lpfc_iocbq *piocb)
  800. {
  801. list_add_tail(&piocb->list, &pring->txcmplq);
  802. pring->txcmplq_cnt++;
  803. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  804. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  805. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  806. if (!piocb->vport)
  807. BUG();
  808. else
  809. mod_timer(&piocb->vport->els_tmofunc,
  810. jiffies + HZ * (phba->fc_ratov << 1));
  811. }
  812. return 0;
  813. }
  814. /**
  815. * lpfc_sli_ringtx_get - Get first element of the txq
  816. * @phba: Pointer to HBA context object.
  817. * @pring: Pointer to driver SLI ring object.
  818. *
  819. * This function is called with hbalock held to get next
  820. * iocb in txq of the given ring. If there is any iocb in
  821. * the txq, the function returns first iocb in the list after
  822. * removing the iocb from the list, else it returns NULL.
  823. **/
  824. static struct lpfc_iocbq *
  825. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  826. {
  827. struct lpfc_iocbq *cmd_iocb;
  828. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  829. if (cmd_iocb != NULL)
  830. pring->txq_cnt--;
  831. return cmd_iocb;
  832. }
  833. /**
  834. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  835. * @phba: Pointer to HBA context object.
  836. * @pring: Pointer to driver SLI ring object.
  837. *
  838. * This function is called with hbalock held and the caller must post the
  839. * iocb without releasing the lock. If the caller releases the lock,
  840. * iocb slot returned by the function is not guaranteed to be available.
  841. * The function returns pointer to the next available iocb slot if there
  842. * is available slot in the ring, else it returns NULL.
  843. * If the get index of the ring is ahead of the put index, the function
  844. * will post an error attention event to the worker thread to take the
  845. * HBA to offline state.
  846. **/
  847. static IOCB_t *
  848. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  849. {
  850. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  851. uint32_t max_cmd_idx = pring->numCiocb;
  852. if ((pring->next_cmdidx == pring->cmdidx) &&
  853. (++pring->next_cmdidx >= max_cmd_idx))
  854. pring->next_cmdidx = 0;
  855. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  856. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  857. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  858. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  859. "0315 Ring %d issue: portCmdGet %d "
  860. "is bigger than cmd ring %d\n",
  861. pring->ringno,
  862. pring->local_getidx, max_cmd_idx);
  863. phba->link_state = LPFC_HBA_ERROR;
  864. /*
  865. * All error attention handlers are posted to
  866. * worker thread
  867. */
  868. phba->work_ha |= HA_ERATT;
  869. phba->work_hs = HS_FFER3;
  870. lpfc_worker_wake_up(phba);
  871. return NULL;
  872. }
  873. if (pring->local_getidx == pring->next_cmdidx)
  874. return NULL;
  875. }
  876. return lpfc_cmd_iocb(phba, pring);
  877. }
  878. /**
  879. * lpfc_sli_next_iotag - Get an iotag for the iocb
  880. * @phba: Pointer to HBA context object.
  881. * @iocbq: Pointer to driver iocb object.
  882. *
  883. * This function gets an iotag for the iocb. If there is no unused iotag and
  884. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  885. * array and assigns a new iotag.
  886. * The function returns the allocated iotag if successful, else returns zero.
  887. * Zero is not a valid iotag.
  888. * The caller is not required to hold any lock.
  889. **/
  890. uint16_t
  891. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  892. {
  893. struct lpfc_iocbq **new_arr;
  894. struct lpfc_iocbq **old_arr;
  895. size_t new_len;
  896. struct lpfc_sli *psli = &phba->sli;
  897. uint16_t iotag;
  898. spin_lock_irq(&phba->hbalock);
  899. iotag = psli->last_iotag;
  900. if(++iotag < psli->iocbq_lookup_len) {
  901. psli->last_iotag = iotag;
  902. psli->iocbq_lookup[iotag] = iocbq;
  903. spin_unlock_irq(&phba->hbalock);
  904. iocbq->iotag = iotag;
  905. return iotag;
  906. } else if (psli->iocbq_lookup_len < (0xffff
  907. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  908. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  909. spin_unlock_irq(&phba->hbalock);
  910. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  911. GFP_KERNEL);
  912. if (new_arr) {
  913. spin_lock_irq(&phba->hbalock);
  914. old_arr = psli->iocbq_lookup;
  915. if (new_len <= psli->iocbq_lookup_len) {
  916. /* highly unprobable case */
  917. kfree(new_arr);
  918. iotag = psli->last_iotag;
  919. if(++iotag < psli->iocbq_lookup_len) {
  920. psli->last_iotag = iotag;
  921. psli->iocbq_lookup[iotag] = iocbq;
  922. spin_unlock_irq(&phba->hbalock);
  923. iocbq->iotag = iotag;
  924. return iotag;
  925. }
  926. spin_unlock_irq(&phba->hbalock);
  927. return 0;
  928. }
  929. if (psli->iocbq_lookup)
  930. memcpy(new_arr, old_arr,
  931. ((psli->last_iotag + 1) *
  932. sizeof (struct lpfc_iocbq *)));
  933. psli->iocbq_lookup = new_arr;
  934. psli->iocbq_lookup_len = new_len;
  935. psli->last_iotag = iotag;
  936. psli->iocbq_lookup[iotag] = iocbq;
  937. spin_unlock_irq(&phba->hbalock);
  938. iocbq->iotag = iotag;
  939. kfree(old_arr);
  940. return iotag;
  941. }
  942. } else
  943. spin_unlock_irq(&phba->hbalock);
  944. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  945. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  946. psli->last_iotag);
  947. return 0;
  948. }
  949. /**
  950. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  951. * @phba: Pointer to HBA context object.
  952. * @pring: Pointer to driver SLI ring object.
  953. * @iocb: Pointer to iocb slot in the ring.
  954. * @nextiocb: Pointer to driver iocb object which need to be
  955. * posted to firmware.
  956. *
  957. * This function is called with hbalock held to post a new iocb to
  958. * the firmware. This function copies the new iocb to ring iocb slot and
  959. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  960. * a completion call back for this iocb else the function will free the
  961. * iocb object.
  962. **/
  963. static void
  964. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  965. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  966. {
  967. /*
  968. * Set up an iotag
  969. */
  970. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  971. if (pring->ringno == LPFC_ELS_RING) {
  972. lpfc_debugfs_slow_ring_trc(phba,
  973. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  974. *(((uint32_t *) &nextiocb->iocb) + 4),
  975. *(((uint32_t *) &nextiocb->iocb) + 6),
  976. *(((uint32_t *) &nextiocb->iocb) + 7));
  977. }
  978. /*
  979. * Issue iocb command to adapter
  980. */
  981. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  982. wmb();
  983. pring->stats.iocb_cmd++;
  984. /*
  985. * If there is no completion routine to call, we can release the
  986. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  987. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  988. */
  989. if (nextiocb->iocb_cmpl)
  990. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  991. else
  992. __lpfc_sli_release_iocbq(phba, nextiocb);
  993. /*
  994. * Let the HBA know what IOCB slot will be the next one the
  995. * driver will put a command into.
  996. */
  997. pring->cmdidx = pring->next_cmdidx;
  998. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  999. }
  1000. /**
  1001. * lpfc_sli_update_full_ring - Update the chip attention register
  1002. * @phba: Pointer to HBA context object.
  1003. * @pring: Pointer to driver SLI ring object.
  1004. *
  1005. * The caller is not required to hold any lock for calling this function.
  1006. * This function updates the chip attention bits for the ring to inform firmware
  1007. * that there are pending work to be done for this ring and requests an
  1008. * interrupt when there is space available in the ring. This function is
  1009. * called when the driver is unable to post more iocbs to the ring due
  1010. * to unavailability of space in the ring.
  1011. **/
  1012. static void
  1013. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1014. {
  1015. int ringno = pring->ringno;
  1016. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1017. wmb();
  1018. /*
  1019. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1020. * The HBA will tell us when an IOCB entry is available.
  1021. */
  1022. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1023. readl(phba->CAregaddr); /* flush */
  1024. pring->stats.iocb_cmd_full++;
  1025. }
  1026. /**
  1027. * lpfc_sli_update_ring - Update chip attention register
  1028. * @phba: Pointer to HBA context object.
  1029. * @pring: Pointer to driver SLI ring object.
  1030. *
  1031. * This function updates the chip attention register bit for the
  1032. * given ring to inform HBA that there is more work to be done
  1033. * in this ring. The caller is not required to hold any lock.
  1034. **/
  1035. static void
  1036. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1037. {
  1038. int ringno = pring->ringno;
  1039. /*
  1040. * Tell the HBA that there is work to do in this ring.
  1041. */
  1042. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1043. wmb();
  1044. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1045. readl(phba->CAregaddr); /* flush */
  1046. }
  1047. }
  1048. /**
  1049. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1050. * @phba: Pointer to HBA context object.
  1051. * @pring: Pointer to driver SLI ring object.
  1052. *
  1053. * This function is called with hbalock held to post pending iocbs
  1054. * in the txq to the firmware. This function is called when driver
  1055. * detects space available in the ring.
  1056. **/
  1057. static void
  1058. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1059. {
  1060. IOCB_t *iocb;
  1061. struct lpfc_iocbq *nextiocb;
  1062. /*
  1063. * Check to see if:
  1064. * (a) there is anything on the txq to send
  1065. * (b) link is up
  1066. * (c) link attention events can be processed (fcp ring only)
  1067. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1068. */
  1069. if (pring->txq_cnt &&
  1070. lpfc_is_link_up(phba) &&
  1071. (pring->ringno != phba->sli.fcp_ring ||
  1072. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1073. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1074. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1075. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1076. if (iocb)
  1077. lpfc_sli_update_ring(phba, pring);
  1078. else
  1079. lpfc_sli_update_full_ring(phba, pring);
  1080. }
  1081. return;
  1082. }
  1083. /**
  1084. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1085. * @phba: Pointer to HBA context object.
  1086. * @hbqno: HBQ number.
  1087. *
  1088. * This function is called with hbalock held to get the next
  1089. * available slot for the given HBQ. If there is free slot
  1090. * available for the HBQ it will return pointer to the next available
  1091. * HBQ entry else it will return NULL.
  1092. **/
  1093. static struct lpfc_hbq_entry *
  1094. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1095. {
  1096. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1097. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1098. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1099. hbqp->next_hbqPutIdx = 0;
  1100. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1101. uint32_t raw_index = phba->hbq_get[hbqno];
  1102. uint32_t getidx = le32_to_cpu(raw_index);
  1103. hbqp->local_hbqGetIdx = getidx;
  1104. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1105. lpfc_printf_log(phba, KERN_ERR,
  1106. LOG_SLI | LOG_VPORT,
  1107. "1802 HBQ %d: local_hbqGetIdx "
  1108. "%u is > than hbqp->entry_count %u\n",
  1109. hbqno, hbqp->local_hbqGetIdx,
  1110. hbqp->entry_count);
  1111. phba->link_state = LPFC_HBA_ERROR;
  1112. return NULL;
  1113. }
  1114. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1115. return NULL;
  1116. }
  1117. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1118. hbqp->hbqPutIdx;
  1119. }
  1120. /**
  1121. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1122. * @phba: Pointer to HBA context object.
  1123. *
  1124. * This function is called with no lock held to free all the
  1125. * hbq buffers while uninitializing the SLI interface. It also
  1126. * frees the HBQ buffers returned by the firmware but not yet
  1127. * processed by the upper layers.
  1128. **/
  1129. void
  1130. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1131. {
  1132. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1133. struct hbq_dmabuf *hbq_buf;
  1134. unsigned long flags;
  1135. int i, hbq_count;
  1136. uint32_t hbqno;
  1137. hbq_count = lpfc_sli_hbq_count();
  1138. /* Return all memory used by all HBQs */
  1139. spin_lock_irqsave(&phba->hbalock, flags);
  1140. for (i = 0; i < hbq_count; ++i) {
  1141. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1142. &phba->hbqs[i].hbq_buffer_list, list) {
  1143. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1144. list_del(&hbq_buf->dbuf.list);
  1145. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1146. }
  1147. phba->hbqs[i].buffer_count = 0;
  1148. }
  1149. /* Return all HBQ buffer that are in-fly */
  1150. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1151. list) {
  1152. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1153. list_del(&hbq_buf->dbuf.list);
  1154. if (hbq_buf->tag == -1) {
  1155. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1156. (phba, hbq_buf);
  1157. } else {
  1158. hbqno = hbq_buf->tag >> 16;
  1159. if (hbqno >= LPFC_MAX_HBQS)
  1160. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1161. (phba, hbq_buf);
  1162. else
  1163. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1164. hbq_buf);
  1165. }
  1166. }
  1167. /* Mark the HBQs not in use */
  1168. phba->hbq_in_use = 0;
  1169. spin_unlock_irqrestore(&phba->hbalock, flags);
  1170. }
  1171. /**
  1172. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1173. * @phba: Pointer to HBA context object.
  1174. * @hbqno: HBQ number.
  1175. * @hbq_buf: Pointer to HBQ buffer.
  1176. *
  1177. * This function is called with the hbalock held to post a
  1178. * hbq buffer to the firmware. If the function finds an empty
  1179. * slot in the HBQ, it will post the buffer. The function will return
  1180. * pointer to the hbq entry if it successfully post the buffer
  1181. * else it will return NULL.
  1182. **/
  1183. static int
  1184. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1185. struct hbq_dmabuf *hbq_buf)
  1186. {
  1187. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1188. }
  1189. /**
  1190. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1191. * @phba: Pointer to HBA context object.
  1192. * @hbqno: HBQ number.
  1193. * @hbq_buf: Pointer to HBQ buffer.
  1194. *
  1195. * This function is called with the hbalock held to post a hbq buffer to the
  1196. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1197. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1198. * it successfully post the buffer else it will return an error.
  1199. **/
  1200. static int
  1201. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1202. struct hbq_dmabuf *hbq_buf)
  1203. {
  1204. struct lpfc_hbq_entry *hbqe;
  1205. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1206. /* Get next HBQ entry slot to use */
  1207. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1208. if (hbqe) {
  1209. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1210. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1211. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1212. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1213. hbqe->bde.tus.f.bdeFlags = 0;
  1214. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1215. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1216. /* Sync SLIM */
  1217. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1218. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1219. /* flush */
  1220. readl(phba->hbq_put + hbqno);
  1221. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1222. return 0;
  1223. } else
  1224. return -ENOMEM;
  1225. }
  1226. /**
  1227. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1228. * @phba: Pointer to HBA context object.
  1229. * @hbqno: HBQ number.
  1230. * @hbq_buf: Pointer to HBQ buffer.
  1231. *
  1232. * This function is called with the hbalock held to post an RQE to the SLI4
  1233. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1234. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1235. **/
  1236. static int
  1237. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1238. struct hbq_dmabuf *hbq_buf)
  1239. {
  1240. int rc;
  1241. struct lpfc_rqe hrqe;
  1242. struct lpfc_rqe drqe;
  1243. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1244. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1245. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1246. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1247. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1248. &hrqe, &drqe);
  1249. if (rc < 0)
  1250. return rc;
  1251. hbq_buf->tag = rc;
  1252. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1253. return 0;
  1254. }
  1255. /* HBQ for ELS and CT traffic. */
  1256. static struct lpfc_hbq_init lpfc_els_hbq = {
  1257. .rn = 1,
  1258. .entry_count = 200,
  1259. .mask_count = 0,
  1260. .profile = 0,
  1261. .ring_mask = (1 << LPFC_ELS_RING),
  1262. .buffer_count = 0,
  1263. .init_count = 40,
  1264. .add_count = 40,
  1265. };
  1266. /* HBQ for the extra ring if needed */
  1267. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1268. .rn = 1,
  1269. .entry_count = 200,
  1270. .mask_count = 0,
  1271. .profile = 0,
  1272. .ring_mask = (1 << LPFC_EXTRA_RING),
  1273. .buffer_count = 0,
  1274. .init_count = 0,
  1275. .add_count = 5,
  1276. };
  1277. /* Array of HBQs */
  1278. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1279. &lpfc_els_hbq,
  1280. &lpfc_extra_hbq,
  1281. };
  1282. /**
  1283. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1284. * @phba: Pointer to HBA context object.
  1285. * @hbqno: HBQ number.
  1286. * @count: Number of HBQ buffers to be posted.
  1287. *
  1288. * This function is called with no lock held to post more hbq buffers to the
  1289. * given HBQ. The function returns the number of HBQ buffers successfully
  1290. * posted.
  1291. **/
  1292. static int
  1293. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1294. {
  1295. uint32_t i, posted = 0;
  1296. unsigned long flags;
  1297. struct hbq_dmabuf *hbq_buffer;
  1298. LIST_HEAD(hbq_buf_list);
  1299. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1300. return 0;
  1301. if ((phba->hbqs[hbqno].buffer_count + count) >
  1302. lpfc_hbq_defs[hbqno]->entry_count)
  1303. count = lpfc_hbq_defs[hbqno]->entry_count -
  1304. phba->hbqs[hbqno].buffer_count;
  1305. if (!count)
  1306. return 0;
  1307. /* Allocate HBQ entries */
  1308. for (i = 0; i < count; i++) {
  1309. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1310. if (!hbq_buffer)
  1311. break;
  1312. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1313. }
  1314. /* Check whether HBQ is still in use */
  1315. spin_lock_irqsave(&phba->hbalock, flags);
  1316. if (!phba->hbq_in_use)
  1317. goto err;
  1318. while (!list_empty(&hbq_buf_list)) {
  1319. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1320. dbuf.list);
  1321. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1322. (hbqno << 16));
  1323. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1324. phba->hbqs[hbqno].buffer_count++;
  1325. posted++;
  1326. } else
  1327. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1328. }
  1329. spin_unlock_irqrestore(&phba->hbalock, flags);
  1330. return posted;
  1331. err:
  1332. spin_unlock_irqrestore(&phba->hbalock, flags);
  1333. while (!list_empty(&hbq_buf_list)) {
  1334. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1335. dbuf.list);
  1336. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1342. * @phba: Pointer to HBA context object.
  1343. * @qno: HBQ number.
  1344. *
  1345. * This function posts more buffers to the HBQ. This function
  1346. * is called with no lock held. The function returns the number of HBQ entries
  1347. * successfully allocated.
  1348. **/
  1349. int
  1350. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1351. {
  1352. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1353. lpfc_hbq_defs[qno]->add_count));
  1354. }
  1355. /**
  1356. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1357. * @phba: Pointer to HBA context object.
  1358. * @qno: HBQ queue number.
  1359. *
  1360. * This function is called from SLI initialization code path with
  1361. * no lock held to post initial HBQ buffers to firmware. The
  1362. * function returns the number of HBQ entries successfully allocated.
  1363. **/
  1364. static int
  1365. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1366. {
  1367. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1368. lpfc_hbq_defs[qno]->init_count));
  1369. }
  1370. /**
  1371. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1372. * @phba: Pointer to HBA context object.
  1373. * @hbqno: HBQ number.
  1374. *
  1375. * This function removes the first hbq buffer on an hbq list and returns a
  1376. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1377. **/
  1378. static struct hbq_dmabuf *
  1379. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1380. {
  1381. struct lpfc_dmabuf *d_buf;
  1382. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1383. if (!d_buf)
  1384. return NULL;
  1385. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1386. }
  1387. /**
  1388. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1389. * @phba: Pointer to HBA context object.
  1390. * @tag: Tag of the hbq buffer.
  1391. *
  1392. * This function is called with hbalock held. This function searches
  1393. * for the hbq buffer associated with the given tag in the hbq buffer
  1394. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1395. * it returns NULL.
  1396. **/
  1397. static struct hbq_dmabuf *
  1398. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1399. {
  1400. struct lpfc_dmabuf *d_buf;
  1401. struct hbq_dmabuf *hbq_buf;
  1402. uint32_t hbqno;
  1403. hbqno = tag >> 16;
  1404. if (hbqno >= LPFC_MAX_HBQS)
  1405. return NULL;
  1406. spin_lock_irq(&phba->hbalock);
  1407. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1408. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1409. if (hbq_buf->tag == tag) {
  1410. spin_unlock_irq(&phba->hbalock);
  1411. return hbq_buf;
  1412. }
  1413. }
  1414. spin_unlock_irq(&phba->hbalock);
  1415. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1416. "1803 Bad hbq tag. Data: x%x x%x\n",
  1417. tag, phba->hbqs[tag >> 16].buffer_count);
  1418. return NULL;
  1419. }
  1420. /**
  1421. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1422. * @phba: Pointer to HBA context object.
  1423. * @hbq_buffer: Pointer to HBQ buffer.
  1424. *
  1425. * This function is called with hbalock. This function gives back
  1426. * the hbq buffer to firmware. If the HBQ does not have space to
  1427. * post the buffer, it will free the buffer.
  1428. **/
  1429. void
  1430. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1431. {
  1432. uint32_t hbqno;
  1433. if (hbq_buffer) {
  1434. hbqno = hbq_buffer->tag >> 16;
  1435. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1436. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1437. }
  1438. }
  1439. /**
  1440. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1441. * @mbxCommand: mailbox command code.
  1442. *
  1443. * This function is called by the mailbox event handler function to verify
  1444. * that the completed mailbox command is a legitimate mailbox command. If the
  1445. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1446. * and the mailbox event handler will take the HBA offline.
  1447. **/
  1448. static int
  1449. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1450. {
  1451. uint8_t ret;
  1452. switch (mbxCommand) {
  1453. case MBX_LOAD_SM:
  1454. case MBX_READ_NV:
  1455. case MBX_WRITE_NV:
  1456. case MBX_WRITE_VPARMS:
  1457. case MBX_RUN_BIU_DIAG:
  1458. case MBX_INIT_LINK:
  1459. case MBX_DOWN_LINK:
  1460. case MBX_CONFIG_LINK:
  1461. case MBX_CONFIG_RING:
  1462. case MBX_RESET_RING:
  1463. case MBX_READ_CONFIG:
  1464. case MBX_READ_RCONFIG:
  1465. case MBX_READ_SPARM:
  1466. case MBX_READ_STATUS:
  1467. case MBX_READ_RPI:
  1468. case MBX_READ_XRI:
  1469. case MBX_READ_REV:
  1470. case MBX_READ_LNK_STAT:
  1471. case MBX_REG_LOGIN:
  1472. case MBX_UNREG_LOGIN:
  1473. case MBX_READ_LA:
  1474. case MBX_CLEAR_LA:
  1475. case MBX_DUMP_MEMORY:
  1476. case MBX_DUMP_CONTEXT:
  1477. case MBX_RUN_DIAGS:
  1478. case MBX_RESTART:
  1479. case MBX_UPDATE_CFG:
  1480. case MBX_DOWN_LOAD:
  1481. case MBX_DEL_LD_ENTRY:
  1482. case MBX_RUN_PROGRAM:
  1483. case MBX_SET_MASK:
  1484. case MBX_SET_VARIABLE:
  1485. case MBX_UNREG_D_ID:
  1486. case MBX_KILL_BOARD:
  1487. case MBX_CONFIG_FARP:
  1488. case MBX_BEACON:
  1489. case MBX_LOAD_AREA:
  1490. case MBX_RUN_BIU_DIAG64:
  1491. case MBX_CONFIG_PORT:
  1492. case MBX_READ_SPARM64:
  1493. case MBX_READ_RPI64:
  1494. case MBX_REG_LOGIN64:
  1495. case MBX_READ_LA64:
  1496. case MBX_WRITE_WWN:
  1497. case MBX_SET_DEBUG:
  1498. case MBX_LOAD_EXP_ROM:
  1499. case MBX_ASYNCEVT_ENABLE:
  1500. case MBX_REG_VPI:
  1501. case MBX_UNREG_VPI:
  1502. case MBX_HEARTBEAT:
  1503. case MBX_PORT_CAPABILITIES:
  1504. case MBX_PORT_IOV_CONTROL:
  1505. case MBX_SLI4_CONFIG:
  1506. case MBX_SLI4_REQ_FTRS:
  1507. case MBX_REG_FCFI:
  1508. case MBX_UNREG_FCFI:
  1509. case MBX_REG_VFI:
  1510. case MBX_UNREG_VFI:
  1511. case MBX_INIT_VPI:
  1512. case MBX_INIT_VFI:
  1513. case MBX_RESUME_RPI:
  1514. ret = mbxCommand;
  1515. break;
  1516. default:
  1517. ret = MBX_SHUTDOWN;
  1518. break;
  1519. }
  1520. return ret;
  1521. }
  1522. /**
  1523. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1524. * @phba: Pointer to HBA context object.
  1525. * @pmboxq: Pointer to mailbox command.
  1526. *
  1527. * This is completion handler function for mailbox commands issued from
  1528. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1529. * mailbox event handler function with no lock held. This function
  1530. * will wake up thread waiting on the wait queue pointed by context1
  1531. * of the mailbox.
  1532. **/
  1533. void
  1534. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1535. {
  1536. wait_queue_head_t *pdone_q;
  1537. unsigned long drvr_flag;
  1538. /*
  1539. * If pdone_q is empty, the driver thread gave up waiting and
  1540. * continued running.
  1541. */
  1542. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1543. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1544. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1545. if (pdone_q)
  1546. wake_up_interruptible(pdone_q);
  1547. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1548. return;
  1549. }
  1550. /**
  1551. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1552. * @phba: Pointer to HBA context object.
  1553. * @pmb: Pointer to mailbox object.
  1554. *
  1555. * This function is the default mailbox completion handler. It
  1556. * frees the memory resources associated with the completed mailbox
  1557. * command. If the completed command is a REG_LOGIN mailbox command,
  1558. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1559. **/
  1560. void
  1561. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1562. {
  1563. struct lpfc_dmabuf *mp;
  1564. uint16_t rpi, vpi;
  1565. int rc;
  1566. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1567. if (mp) {
  1568. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1569. kfree(mp);
  1570. }
  1571. if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
  1572. (phba->sli_rev == LPFC_SLI_REV4))
  1573. lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
  1574. /*
  1575. * If a REG_LOGIN succeeded after node is destroyed or node
  1576. * is in re-discovery driver need to cleanup the RPI.
  1577. */
  1578. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1579. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1580. !pmb->u.mb.mbxStatus) {
  1581. rpi = pmb->u.mb.un.varWords[0];
  1582. vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
  1583. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1584. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1585. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1586. if (rc != MBX_NOT_FINISHED)
  1587. return;
  1588. }
  1589. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1590. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1591. else
  1592. mempool_free(pmb, phba->mbox_mem_pool);
  1593. }
  1594. /**
  1595. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1596. * @phba: Pointer to HBA context object.
  1597. *
  1598. * This function is called with no lock held. This function processes all
  1599. * the completed mailbox commands and gives it to upper layers. The interrupt
  1600. * service routine processes mailbox completion interrupt and adds completed
  1601. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1602. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1603. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1604. * function returns the mailbox commands to the upper layer by calling the
  1605. * completion handler function of each mailbox.
  1606. **/
  1607. int
  1608. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1609. {
  1610. MAILBOX_t *pmbox;
  1611. LPFC_MBOXQ_t *pmb;
  1612. int rc;
  1613. LIST_HEAD(cmplq);
  1614. phba->sli.slistat.mbox_event++;
  1615. /* Get all completed mailboxe buffers into the cmplq */
  1616. spin_lock_irq(&phba->hbalock);
  1617. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1618. spin_unlock_irq(&phba->hbalock);
  1619. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1620. do {
  1621. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1622. if (pmb == NULL)
  1623. break;
  1624. pmbox = &pmb->u.mb;
  1625. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1626. if (pmb->vport) {
  1627. lpfc_debugfs_disc_trc(pmb->vport,
  1628. LPFC_DISC_TRC_MBOX_VPORT,
  1629. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1630. (uint32_t)pmbox->mbxCommand,
  1631. pmbox->un.varWords[0],
  1632. pmbox->un.varWords[1]);
  1633. }
  1634. else {
  1635. lpfc_debugfs_disc_trc(phba->pport,
  1636. LPFC_DISC_TRC_MBOX,
  1637. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  1638. (uint32_t)pmbox->mbxCommand,
  1639. pmbox->un.varWords[0],
  1640. pmbox->un.varWords[1]);
  1641. }
  1642. }
  1643. /*
  1644. * It is a fatal error if unknown mbox command completion.
  1645. */
  1646. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  1647. MBX_SHUTDOWN) {
  1648. /* Unknow mailbox command compl */
  1649. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1650. "(%d):0323 Unknown Mailbox command "
  1651. "x%x (x%x) Cmpl\n",
  1652. pmb->vport ? pmb->vport->vpi : 0,
  1653. pmbox->mbxCommand,
  1654. lpfc_sli4_mbox_opcode_get(phba, pmb));
  1655. phba->link_state = LPFC_HBA_ERROR;
  1656. phba->work_hs = HS_FFER3;
  1657. lpfc_handle_eratt(phba);
  1658. continue;
  1659. }
  1660. if (pmbox->mbxStatus) {
  1661. phba->sli.slistat.mbox_stat_err++;
  1662. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  1663. /* Mbox cmd cmpl error - RETRYing */
  1664. lpfc_printf_log(phba, KERN_INFO,
  1665. LOG_MBOX | LOG_SLI,
  1666. "(%d):0305 Mbox cmd cmpl "
  1667. "error - RETRYing Data: x%x "
  1668. "(x%x) x%x x%x x%x\n",
  1669. pmb->vport ? pmb->vport->vpi :0,
  1670. pmbox->mbxCommand,
  1671. lpfc_sli4_mbox_opcode_get(phba,
  1672. pmb),
  1673. pmbox->mbxStatus,
  1674. pmbox->un.varWords[0],
  1675. pmb->vport->port_state);
  1676. pmbox->mbxStatus = 0;
  1677. pmbox->mbxOwner = OWN_HOST;
  1678. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1679. if (rc != MBX_NOT_FINISHED)
  1680. continue;
  1681. }
  1682. }
  1683. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  1684. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  1685. "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
  1686. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1687. pmb->vport ? pmb->vport->vpi : 0,
  1688. pmbox->mbxCommand,
  1689. lpfc_sli4_mbox_opcode_get(phba, pmb),
  1690. pmb->mbox_cmpl,
  1691. *((uint32_t *) pmbox),
  1692. pmbox->un.varWords[0],
  1693. pmbox->un.varWords[1],
  1694. pmbox->un.varWords[2],
  1695. pmbox->un.varWords[3],
  1696. pmbox->un.varWords[4],
  1697. pmbox->un.varWords[5],
  1698. pmbox->un.varWords[6],
  1699. pmbox->un.varWords[7]);
  1700. if (pmb->mbox_cmpl)
  1701. pmb->mbox_cmpl(phba,pmb);
  1702. } while (1);
  1703. return 0;
  1704. }
  1705. /**
  1706. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  1707. * @phba: Pointer to HBA context object.
  1708. * @pring: Pointer to driver SLI ring object.
  1709. * @tag: buffer tag.
  1710. *
  1711. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  1712. * is set in the tag the buffer is posted for a particular exchange,
  1713. * the function will return the buffer without replacing the buffer.
  1714. * If the buffer is for unsolicited ELS or CT traffic, this function
  1715. * returns the buffer and also posts another buffer to the firmware.
  1716. **/
  1717. static struct lpfc_dmabuf *
  1718. lpfc_sli_get_buff(struct lpfc_hba *phba,
  1719. struct lpfc_sli_ring *pring,
  1720. uint32_t tag)
  1721. {
  1722. struct hbq_dmabuf *hbq_entry;
  1723. if (tag & QUE_BUFTAG_BIT)
  1724. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  1725. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  1726. if (!hbq_entry)
  1727. return NULL;
  1728. return &hbq_entry->dbuf;
  1729. }
  1730. /**
  1731. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  1732. * @phba: Pointer to HBA context object.
  1733. * @pring: Pointer to driver SLI ring object.
  1734. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  1735. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  1736. * @fch_type: the type for the first frame of the sequence.
  1737. *
  1738. * This function is called with no lock held. This function uses the r_ctl and
  1739. * type of the received sequence to find the correct callback function to call
  1740. * to process the sequence.
  1741. **/
  1742. static int
  1743. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1744. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  1745. uint32_t fch_type)
  1746. {
  1747. int i;
  1748. /* unSolicited Responses */
  1749. if (pring->prt[0].profile) {
  1750. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  1751. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  1752. saveq);
  1753. return 1;
  1754. }
  1755. /* We must search, based on rctl / type
  1756. for the right routine */
  1757. for (i = 0; i < pring->num_mask; i++) {
  1758. if ((pring->prt[i].rctl == fch_r_ctl) &&
  1759. (pring->prt[i].type == fch_type)) {
  1760. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1761. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1762. (phba, pring, saveq);
  1763. return 1;
  1764. }
  1765. }
  1766. return 0;
  1767. }
  1768. /**
  1769. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  1770. * @phba: Pointer to HBA context object.
  1771. * @pring: Pointer to driver SLI ring object.
  1772. * @saveq: Pointer to the unsolicited iocb.
  1773. *
  1774. * This function is called with no lock held by the ring event handler
  1775. * when there is an unsolicited iocb posted to the response ring by the
  1776. * firmware. This function gets the buffer associated with the iocbs
  1777. * and calls the event handler for the ring. This function handles both
  1778. * qring buffers and hbq buffers.
  1779. * When the function returns 1 the caller can free the iocb object otherwise
  1780. * upper layer functions will free the iocb objects.
  1781. **/
  1782. static int
  1783. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1784. struct lpfc_iocbq *saveq)
  1785. {
  1786. IOCB_t * irsp;
  1787. WORD5 * w5p;
  1788. uint32_t Rctl, Type;
  1789. uint32_t match;
  1790. struct lpfc_iocbq *iocbq;
  1791. struct lpfc_dmabuf *dmzbuf;
  1792. match = 0;
  1793. irsp = &(saveq->iocb);
  1794. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  1795. if (pring->lpfc_sli_rcv_async_status)
  1796. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  1797. else
  1798. lpfc_printf_log(phba,
  1799. KERN_WARNING,
  1800. LOG_SLI,
  1801. "0316 Ring %d handler: unexpected "
  1802. "ASYNC_STATUS iocb received evt_code "
  1803. "0x%x\n",
  1804. pring->ringno,
  1805. irsp->un.asyncstat.evt_code);
  1806. return 1;
  1807. }
  1808. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  1809. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  1810. if (irsp->ulpBdeCount > 0) {
  1811. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1812. irsp->un.ulpWord[3]);
  1813. lpfc_in_buf_free(phba, dmzbuf);
  1814. }
  1815. if (irsp->ulpBdeCount > 1) {
  1816. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1817. irsp->unsli3.sli3Words[3]);
  1818. lpfc_in_buf_free(phba, dmzbuf);
  1819. }
  1820. if (irsp->ulpBdeCount > 2) {
  1821. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1822. irsp->unsli3.sli3Words[7]);
  1823. lpfc_in_buf_free(phba, dmzbuf);
  1824. }
  1825. return 1;
  1826. }
  1827. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1828. if (irsp->ulpBdeCount != 0) {
  1829. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  1830. irsp->un.ulpWord[3]);
  1831. if (!saveq->context2)
  1832. lpfc_printf_log(phba,
  1833. KERN_ERR,
  1834. LOG_SLI,
  1835. "0341 Ring %d Cannot find buffer for "
  1836. "an unsolicited iocb. tag 0x%x\n",
  1837. pring->ringno,
  1838. irsp->un.ulpWord[3]);
  1839. }
  1840. if (irsp->ulpBdeCount == 2) {
  1841. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  1842. irsp->unsli3.sli3Words[7]);
  1843. if (!saveq->context3)
  1844. lpfc_printf_log(phba,
  1845. KERN_ERR,
  1846. LOG_SLI,
  1847. "0342 Ring %d Cannot find buffer for an"
  1848. " unsolicited iocb. tag 0x%x\n",
  1849. pring->ringno,
  1850. irsp->unsli3.sli3Words[7]);
  1851. }
  1852. list_for_each_entry(iocbq, &saveq->list, list) {
  1853. irsp = &(iocbq->iocb);
  1854. if (irsp->ulpBdeCount != 0) {
  1855. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  1856. irsp->un.ulpWord[3]);
  1857. if (!iocbq->context2)
  1858. lpfc_printf_log(phba,
  1859. KERN_ERR,
  1860. LOG_SLI,
  1861. "0343 Ring %d Cannot find "
  1862. "buffer for an unsolicited iocb"
  1863. ". tag 0x%x\n", pring->ringno,
  1864. irsp->un.ulpWord[3]);
  1865. }
  1866. if (irsp->ulpBdeCount == 2) {
  1867. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  1868. irsp->unsli3.sli3Words[7]);
  1869. if (!iocbq->context3)
  1870. lpfc_printf_log(phba,
  1871. KERN_ERR,
  1872. LOG_SLI,
  1873. "0344 Ring %d Cannot find "
  1874. "buffer for an unsolicited "
  1875. "iocb. tag 0x%x\n",
  1876. pring->ringno,
  1877. irsp->unsli3.sli3Words[7]);
  1878. }
  1879. }
  1880. }
  1881. if (irsp->ulpBdeCount != 0 &&
  1882. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  1883. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  1884. int found = 0;
  1885. /* search continue save q for same XRI */
  1886. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  1887. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  1888. list_add_tail(&saveq->list, &iocbq->list);
  1889. found = 1;
  1890. break;
  1891. }
  1892. }
  1893. if (!found)
  1894. list_add_tail(&saveq->clist,
  1895. &pring->iocb_continue_saveq);
  1896. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  1897. list_del_init(&iocbq->clist);
  1898. saveq = iocbq;
  1899. irsp = &(saveq->iocb);
  1900. } else
  1901. return 0;
  1902. }
  1903. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  1904. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  1905. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  1906. Rctl = FC_ELS_REQ;
  1907. Type = FC_ELS_DATA;
  1908. } else {
  1909. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  1910. Rctl = w5p->hcsw.Rctl;
  1911. Type = w5p->hcsw.Type;
  1912. /* Firmware Workaround */
  1913. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  1914. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  1915. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  1916. Rctl = FC_ELS_REQ;
  1917. Type = FC_ELS_DATA;
  1918. w5p->hcsw.Rctl = Rctl;
  1919. w5p->hcsw.Type = Type;
  1920. }
  1921. }
  1922. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  1923. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1924. "0313 Ring %d handler: unexpected Rctl x%x "
  1925. "Type x%x received\n",
  1926. pring->ringno, Rctl, Type);
  1927. return 1;
  1928. }
  1929. /**
  1930. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  1931. * @phba: Pointer to HBA context object.
  1932. * @pring: Pointer to driver SLI ring object.
  1933. * @prspiocb: Pointer to response iocb object.
  1934. *
  1935. * This function looks up the iocb_lookup table to get the command iocb
  1936. * corresponding to the given response iocb using the iotag of the
  1937. * response iocb. This function is called with the hbalock held.
  1938. * This function returns the command iocb object if it finds the command
  1939. * iocb else returns NULL.
  1940. **/
  1941. static struct lpfc_iocbq *
  1942. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  1943. struct lpfc_sli_ring *pring,
  1944. struct lpfc_iocbq *prspiocb)
  1945. {
  1946. struct lpfc_iocbq *cmd_iocb = NULL;
  1947. uint16_t iotag;
  1948. iotag = prspiocb->iocb.ulpIoTag;
  1949. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1950. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1951. list_del_init(&cmd_iocb->list);
  1952. pring->txcmplq_cnt--;
  1953. return cmd_iocb;
  1954. }
  1955. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1956. "0317 iotag x%x is out off "
  1957. "range: max iotag x%x wd0 x%x\n",
  1958. iotag, phba->sli.last_iotag,
  1959. *(((uint32_t *) &prspiocb->iocb) + 7));
  1960. return NULL;
  1961. }
  1962. /**
  1963. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  1964. * @phba: Pointer to HBA context object.
  1965. * @pring: Pointer to driver SLI ring object.
  1966. * @iotag: IOCB tag.
  1967. *
  1968. * This function looks up the iocb_lookup table to get the command iocb
  1969. * corresponding to the given iotag. This function is called with the
  1970. * hbalock held.
  1971. * This function returns the command iocb object if it finds the command
  1972. * iocb else returns NULL.
  1973. **/
  1974. static struct lpfc_iocbq *
  1975. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  1976. struct lpfc_sli_ring *pring, uint16_t iotag)
  1977. {
  1978. struct lpfc_iocbq *cmd_iocb;
  1979. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1980. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1981. list_del_init(&cmd_iocb->list);
  1982. pring->txcmplq_cnt--;
  1983. return cmd_iocb;
  1984. }
  1985. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1986. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  1987. iotag, phba->sli.last_iotag);
  1988. return NULL;
  1989. }
  1990. /**
  1991. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  1992. * @phba: Pointer to HBA context object.
  1993. * @pring: Pointer to driver SLI ring object.
  1994. * @saveq: Pointer to the response iocb to be processed.
  1995. *
  1996. * This function is called by the ring event handler for non-fcp
  1997. * rings when there is a new response iocb in the response ring.
  1998. * The caller is not required to hold any locks. This function
  1999. * gets the command iocb associated with the response iocb and
  2000. * calls the completion handler for the command iocb. If there
  2001. * is no completion handler, the function will free the resources
  2002. * associated with command iocb. If the response iocb is for
  2003. * an already aborted command iocb, the status of the completion
  2004. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2005. * This function always returns 1.
  2006. **/
  2007. static int
  2008. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2009. struct lpfc_iocbq *saveq)
  2010. {
  2011. struct lpfc_iocbq *cmdiocbp;
  2012. int rc = 1;
  2013. unsigned long iflag;
  2014. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2015. spin_lock_irqsave(&phba->hbalock, iflag);
  2016. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2017. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2018. if (cmdiocbp) {
  2019. if (cmdiocbp->iocb_cmpl) {
  2020. /*
  2021. * If an ELS command failed send an event to mgmt
  2022. * application.
  2023. */
  2024. if (saveq->iocb.ulpStatus &&
  2025. (pring->ringno == LPFC_ELS_RING) &&
  2026. (cmdiocbp->iocb.ulpCommand ==
  2027. CMD_ELS_REQUEST64_CR))
  2028. lpfc_send_els_failure_event(phba,
  2029. cmdiocbp, saveq);
  2030. /*
  2031. * Post all ELS completions to the worker thread.
  2032. * All other are passed to the completion callback.
  2033. */
  2034. if (pring->ringno == LPFC_ELS_RING) {
  2035. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  2036. cmdiocbp->iocb_flag &=
  2037. ~LPFC_DRIVER_ABORTED;
  2038. saveq->iocb.ulpStatus =
  2039. IOSTAT_LOCAL_REJECT;
  2040. saveq->iocb.un.ulpWord[4] =
  2041. IOERR_SLI_ABORTED;
  2042. /* Firmware could still be in progress
  2043. * of DMAing payload, so don't free data
  2044. * buffer till after a hbeat.
  2045. */
  2046. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2047. }
  2048. }
  2049. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2050. } else
  2051. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2052. } else {
  2053. /*
  2054. * Unknown initiating command based on the response iotag.
  2055. * This could be the case on the ELS ring because of
  2056. * lpfc_els_abort().
  2057. */
  2058. if (pring->ringno != LPFC_ELS_RING) {
  2059. /*
  2060. * Ring <ringno> handler: unexpected completion IoTag
  2061. * <IoTag>
  2062. */
  2063. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2064. "0322 Ring %d handler: "
  2065. "unexpected completion IoTag x%x "
  2066. "Data: x%x x%x x%x x%x\n",
  2067. pring->ringno,
  2068. saveq->iocb.ulpIoTag,
  2069. saveq->iocb.ulpStatus,
  2070. saveq->iocb.un.ulpWord[4],
  2071. saveq->iocb.ulpCommand,
  2072. saveq->iocb.ulpContext);
  2073. }
  2074. }
  2075. return rc;
  2076. }
  2077. /**
  2078. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2079. * @phba: Pointer to HBA context object.
  2080. * @pring: Pointer to driver SLI ring object.
  2081. *
  2082. * This function is called from the iocb ring event handlers when
  2083. * put pointer is ahead of the get pointer for a ring. This function signal
  2084. * an error attention condition to the worker thread and the worker
  2085. * thread will transition the HBA to offline state.
  2086. **/
  2087. static void
  2088. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2089. {
  2090. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2091. /*
  2092. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2093. * rsp ring <portRspMax>
  2094. */
  2095. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2096. "0312 Ring %d handler: portRspPut %d "
  2097. "is bigger than rsp ring %d\n",
  2098. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2099. pring->numRiocb);
  2100. phba->link_state = LPFC_HBA_ERROR;
  2101. /*
  2102. * All error attention handlers are posted to
  2103. * worker thread
  2104. */
  2105. phba->work_ha |= HA_ERATT;
  2106. phba->work_hs = HS_FFER3;
  2107. lpfc_worker_wake_up(phba);
  2108. return;
  2109. }
  2110. /**
  2111. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2112. * @ptr: Pointer to address of HBA context object.
  2113. *
  2114. * This function is invoked by the Error Attention polling timer when the
  2115. * timer times out. It will check the SLI Error Attention register for
  2116. * possible attention events. If so, it will post an Error Attention event
  2117. * and wake up worker thread to process it. Otherwise, it will set up the
  2118. * Error Attention polling timer for the next poll.
  2119. **/
  2120. void lpfc_poll_eratt(unsigned long ptr)
  2121. {
  2122. struct lpfc_hba *phba;
  2123. uint32_t eratt = 0;
  2124. phba = (struct lpfc_hba *)ptr;
  2125. /* Check chip HA register for error event */
  2126. eratt = lpfc_sli_check_eratt(phba);
  2127. if (eratt)
  2128. /* Tell the worker thread there is work to do */
  2129. lpfc_worker_wake_up(phba);
  2130. else
  2131. /* Restart the timer for next eratt poll */
  2132. mod_timer(&phba->eratt_poll, jiffies +
  2133. HZ * LPFC_ERATT_POLL_INTERVAL);
  2134. return;
  2135. }
  2136. /**
  2137. * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
  2138. * @phba: Pointer to HBA context object.
  2139. *
  2140. * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
  2141. * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
  2142. * is enabled.
  2143. *
  2144. * The caller does not hold any lock.
  2145. * The function processes each response iocb in the response ring until it
  2146. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  2147. * LE bit set. The function will call the completion handler of the command iocb
  2148. * if the response iocb indicates a completion for a command iocb or it is
  2149. * an abort completion.
  2150. **/
  2151. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  2152. {
  2153. struct lpfc_sli *psli = &phba->sli;
  2154. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  2155. IOCB_t *irsp = NULL;
  2156. IOCB_t *entry = NULL;
  2157. struct lpfc_iocbq *cmdiocbq = NULL;
  2158. struct lpfc_iocbq rspiocbq;
  2159. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2160. uint32_t status;
  2161. uint32_t portRspPut, portRspMax;
  2162. int type;
  2163. uint32_t rsp_cmpl = 0;
  2164. uint32_t ha_copy;
  2165. unsigned long iflags;
  2166. pring->stats.iocb_event++;
  2167. /*
  2168. * The next available response entry should never exceed the maximum
  2169. * entries. If it does, treat it as an adapter hardware error.
  2170. */
  2171. portRspMax = pring->numRiocb;
  2172. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2173. if (unlikely(portRspPut >= portRspMax)) {
  2174. lpfc_sli_rsp_pointers_error(phba, pring);
  2175. return;
  2176. }
  2177. rmb();
  2178. while (pring->rspidx != portRspPut) {
  2179. entry = lpfc_resp_iocb(phba, pring);
  2180. if (++pring->rspidx >= portRspMax)
  2181. pring->rspidx = 0;
  2182. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2183. (uint32_t *) &rspiocbq.iocb,
  2184. phba->iocb_rsp_size);
  2185. irsp = &rspiocbq.iocb;
  2186. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2187. pring->stats.iocb_rsp++;
  2188. rsp_cmpl++;
  2189. if (unlikely(irsp->ulpStatus)) {
  2190. /* Rsp ring <ringno> error: IOCB */
  2191. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2192. "0326 Rsp Ring %d error: IOCB Data: "
  2193. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2194. pring->ringno,
  2195. irsp->un.ulpWord[0],
  2196. irsp->un.ulpWord[1],
  2197. irsp->un.ulpWord[2],
  2198. irsp->un.ulpWord[3],
  2199. irsp->un.ulpWord[4],
  2200. irsp->un.ulpWord[5],
  2201. *(uint32_t *)&irsp->un1,
  2202. *((uint32_t *)&irsp->un1 + 1));
  2203. }
  2204. switch (type) {
  2205. case LPFC_ABORT_IOCB:
  2206. case LPFC_SOL_IOCB:
  2207. /*
  2208. * Idle exchange closed via ABTS from port. No iocb
  2209. * resources need to be recovered.
  2210. */
  2211. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2212. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2213. "0314 IOCB cmd 0x%x "
  2214. "processed. Skipping "
  2215. "completion",
  2216. irsp->ulpCommand);
  2217. break;
  2218. }
  2219. spin_lock_irqsave(&phba->hbalock, iflags);
  2220. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2221. &rspiocbq);
  2222. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2223. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  2224. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2225. &rspiocbq);
  2226. }
  2227. break;
  2228. default:
  2229. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2230. char adaptermsg[LPFC_MAX_ADPTMSG];
  2231. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2232. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2233. MAX_MSG_DATA);
  2234. dev_warn(&((phba->pcidev)->dev),
  2235. "lpfc%d: %s\n",
  2236. phba->brd_no, adaptermsg);
  2237. } else {
  2238. /* Unknown IOCB command */
  2239. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2240. "0321 Unknown IOCB command "
  2241. "Data: x%x, x%x x%x x%x x%x\n",
  2242. type, irsp->ulpCommand,
  2243. irsp->ulpStatus,
  2244. irsp->ulpIoTag,
  2245. irsp->ulpContext);
  2246. }
  2247. break;
  2248. }
  2249. /*
  2250. * The response IOCB has been processed. Update the ring
  2251. * pointer in SLIM. If the port response put pointer has not
  2252. * been updated, sync the pgp->rspPutInx and fetch the new port
  2253. * response put pointer.
  2254. */
  2255. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2256. if (pring->rspidx == portRspPut)
  2257. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2258. }
  2259. ha_copy = readl(phba->HAregaddr);
  2260. ha_copy >>= (LPFC_FCP_RING * 4);
  2261. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  2262. spin_lock_irqsave(&phba->hbalock, iflags);
  2263. pring->stats.iocb_rsp_full++;
  2264. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  2265. writel(status, phba->CAregaddr);
  2266. readl(phba->CAregaddr);
  2267. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2268. }
  2269. if ((ha_copy & HA_R0CE_RSP) &&
  2270. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2271. spin_lock_irqsave(&phba->hbalock, iflags);
  2272. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2273. pring->stats.iocb_cmd_empty++;
  2274. /* Force update of the local copy of cmdGetInx */
  2275. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2276. lpfc_sli_resume_iocb(phba, pring);
  2277. if ((pring->lpfc_sli_cmd_available))
  2278. (pring->lpfc_sli_cmd_available) (phba, pring);
  2279. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2280. }
  2281. return;
  2282. }
  2283. /**
  2284. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2285. * @phba: Pointer to HBA context object.
  2286. * @pring: Pointer to driver SLI ring object.
  2287. * @mask: Host attention register mask for this ring.
  2288. *
  2289. * This function is called from the interrupt context when there is a ring
  2290. * event for the fcp ring. The caller does not hold any lock.
  2291. * The function processes each response iocb in the response ring until it
  2292. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  2293. * LE bit set. The function will call the completion handler of the command iocb
  2294. * if the response iocb indicates a completion for a command iocb or it is
  2295. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2296. * function if this is an unsolicited iocb.
  2297. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2298. * to check it explicitly. This function always returns 1.
  2299. **/
  2300. static int
  2301. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2302. struct lpfc_sli_ring *pring, uint32_t mask)
  2303. {
  2304. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2305. IOCB_t *irsp = NULL;
  2306. IOCB_t *entry = NULL;
  2307. struct lpfc_iocbq *cmdiocbq = NULL;
  2308. struct lpfc_iocbq rspiocbq;
  2309. uint32_t status;
  2310. uint32_t portRspPut, portRspMax;
  2311. int rc = 1;
  2312. lpfc_iocb_type type;
  2313. unsigned long iflag;
  2314. uint32_t rsp_cmpl = 0;
  2315. spin_lock_irqsave(&phba->hbalock, iflag);
  2316. pring->stats.iocb_event++;
  2317. /*
  2318. * The next available response entry should never exceed the maximum
  2319. * entries. If it does, treat it as an adapter hardware error.
  2320. */
  2321. portRspMax = pring->numRiocb;
  2322. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2323. if (unlikely(portRspPut >= portRspMax)) {
  2324. lpfc_sli_rsp_pointers_error(phba, pring);
  2325. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2326. return 1;
  2327. }
  2328. rmb();
  2329. while (pring->rspidx != portRspPut) {
  2330. /*
  2331. * Fetch an entry off the ring and copy it into a local data
  2332. * structure. The copy involves a byte-swap since the
  2333. * network byte order and pci byte orders are different.
  2334. */
  2335. entry = lpfc_resp_iocb(phba, pring);
  2336. phba->last_completion_time = jiffies;
  2337. if (++pring->rspidx >= portRspMax)
  2338. pring->rspidx = 0;
  2339. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2340. (uint32_t *) &rspiocbq.iocb,
  2341. phba->iocb_rsp_size);
  2342. INIT_LIST_HEAD(&(rspiocbq.list));
  2343. irsp = &rspiocbq.iocb;
  2344. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2345. pring->stats.iocb_rsp++;
  2346. rsp_cmpl++;
  2347. if (unlikely(irsp->ulpStatus)) {
  2348. /*
  2349. * If resource errors reported from HBA, reduce
  2350. * queuedepths of the SCSI device.
  2351. */
  2352. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2353. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2354. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2355. phba->lpfc_rampdown_queue_depth(phba);
  2356. spin_lock_irqsave(&phba->hbalock, iflag);
  2357. }
  2358. /* Rsp ring <ringno> error: IOCB */
  2359. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2360. "0336 Rsp Ring %d error: IOCB Data: "
  2361. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2362. pring->ringno,
  2363. irsp->un.ulpWord[0],
  2364. irsp->un.ulpWord[1],
  2365. irsp->un.ulpWord[2],
  2366. irsp->un.ulpWord[3],
  2367. irsp->un.ulpWord[4],
  2368. irsp->un.ulpWord[5],
  2369. *(uint32_t *)&irsp->un1,
  2370. *((uint32_t *)&irsp->un1 + 1));
  2371. }
  2372. switch (type) {
  2373. case LPFC_ABORT_IOCB:
  2374. case LPFC_SOL_IOCB:
  2375. /*
  2376. * Idle exchange closed via ABTS from port. No iocb
  2377. * resources need to be recovered.
  2378. */
  2379. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2380. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2381. "0333 IOCB cmd 0x%x"
  2382. " processed. Skipping"
  2383. " completion\n",
  2384. irsp->ulpCommand);
  2385. break;
  2386. }
  2387. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2388. &rspiocbq);
  2389. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  2390. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2391. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2392. &rspiocbq);
  2393. } else {
  2394. spin_unlock_irqrestore(&phba->hbalock,
  2395. iflag);
  2396. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2397. &rspiocbq);
  2398. spin_lock_irqsave(&phba->hbalock,
  2399. iflag);
  2400. }
  2401. }
  2402. break;
  2403. case LPFC_UNSOL_IOCB:
  2404. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2405. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2406. spin_lock_irqsave(&phba->hbalock, iflag);
  2407. break;
  2408. default:
  2409. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2410. char adaptermsg[LPFC_MAX_ADPTMSG];
  2411. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2412. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2413. MAX_MSG_DATA);
  2414. dev_warn(&((phba->pcidev)->dev),
  2415. "lpfc%d: %s\n",
  2416. phba->brd_no, adaptermsg);
  2417. } else {
  2418. /* Unknown IOCB command */
  2419. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2420. "0334 Unknown IOCB command "
  2421. "Data: x%x, x%x x%x x%x x%x\n",
  2422. type, irsp->ulpCommand,
  2423. irsp->ulpStatus,
  2424. irsp->ulpIoTag,
  2425. irsp->ulpContext);
  2426. }
  2427. break;
  2428. }
  2429. /*
  2430. * The response IOCB has been processed. Update the ring
  2431. * pointer in SLIM. If the port response put pointer has not
  2432. * been updated, sync the pgp->rspPutInx and fetch the new port
  2433. * response put pointer.
  2434. */
  2435. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2436. if (pring->rspidx == portRspPut)
  2437. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2438. }
  2439. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2440. pring->stats.iocb_rsp_full++;
  2441. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2442. writel(status, phba->CAregaddr);
  2443. readl(phba->CAregaddr);
  2444. }
  2445. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2446. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2447. pring->stats.iocb_cmd_empty++;
  2448. /* Force update of the local copy of cmdGetInx */
  2449. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2450. lpfc_sli_resume_iocb(phba, pring);
  2451. if ((pring->lpfc_sli_cmd_available))
  2452. (pring->lpfc_sli_cmd_available) (phba, pring);
  2453. }
  2454. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2455. return rc;
  2456. }
  2457. /**
  2458. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2459. * @phba: Pointer to HBA context object.
  2460. * @pring: Pointer to driver SLI ring object.
  2461. * @rspiocbp: Pointer to driver response IOCB object.
  2462. *
  2463. * This function is called from the worker thread when there is a slow-path
  2464. * response IOCB to process. This function chains all the response iocbs until
  2465. * seeing the iocb with the LE bit set. The function will call
  2466. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2467. * completion of a command iocb. The function will call the
  2468. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2469. * The function frees the resources or calls the completion handler if this
  2470. * iocb is an abort completion. The function returns NULL when the response
  2471. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2472. * this function shall chain the iocb on to the iocb_continueq and return the
  2473. * response iocb passed in.
  2474. **/
  2475. static struct lpfc_iocbq *
  2476. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2477. struct lpfc_iocbq *rspiocbp)
  2478. {
  2479. struct lpfc_iocbq *saveq;
  2480. struct lpfc_iocbq *cmdiocbp;
  2481. struct lpfc_iocbq *next_iocb;
  2482. IOCB_t *irsp = NULL;
  2483. uint32_t free_saveq;
  2484. uint8_t iocb_cmd_type;
  2485. lpfc_iocb_type type;
  2486. unsigned long iflag;
  2487. int rc;
  2488. spin_lock_irqsave(&phba->hbalock, iflag);
  2489. /* First add the response iocb to the countinueq list */
  2490. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2491. pring->iocb_continueq_cnt++;
  2492. /* Now, determine whetehr the list is completed for processing */
  2493. irsp = &rspiocbp->iocb;
  2494. if (irsp->ulpLe) {
  2495. /*
  2496. * By default, the driver expects to free all resources
  2497. * associated with this iocb completion.
  2498. */
  2499. free_saveq = 1;
  2500. saveq = list_get_first(&pring->iocb_continueq,
  2501. struct lpfc_iocbq, list);
  2502. irsp = &(saveq->iocb);
  2503. list_del_init(&pring->iocb_continueq);
  2504. pring->iocb_continueq_cnt = 0;
  2505. pring->stats.iocb_rsp++;
  2506. /*
  2507. * If resource errors reported from HBA, reduce
  2508. * queuedepths of the SCSI device.
  2509. */
  2510. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2511. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2512. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2513. phba->lpfc_rampdown_queue_depth(phba);
  2514. spin_lock_irqsave(&phba->hbalock, iflag);
  2515. }
  2516. if (irsp->ulpStatus) {
  2517. /* Rsp ring <ringno> error: IOCB */
  2518. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2519. "0328 Rsp Ring %d error: "
  2520. "IOCB Data: "
  2521. "x%x x%x x%x x%x "
  2522. "x%x x%x x%x x%x "
  2523. "x%x x%x x%x x%x "
  2524. "x%x x%x x%x x%x\n",
  2525. pring->ringno,
  2526. irsp->un.ulpWord[0],
  2527. irsp->un.ulpWord[1],
  2528. irsp->un.ulpWord[2],
  2529. irsp->un.ulpWord[3],
  2530. irsp->un.ulpWord[4],
  2531. irsp->un.ulpWord[5],
  2532. *(((uint32_t *) irsp) + 6),
  2533. *(((uint32_t *) irsp) + 7),
  2534. *(((uint32_t *) irsp) + 8),
  2535. *(((uint32_t *) irsp) + 9),
  2536. *(((uint32_t *) irsp) + 10),
  2537. *(((uint32_t *) irsp) + 11),
  2538. *(((uint32_t *) irsp) + 12),
  2539. *(((uint32_t *) irsp) + 13),
  2540. *(((uint32_t *) irsp) + 14),
  2541. *(((uint32_t *) irsp) + 15));
  2542. }
  2543. /*
  2544. * Fetch the IOCB command type and call the correct completion
  2545. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2546. * get freed back to the lpfc_iocb_list by the discovery
  2547. * kernel thread.
  2548. */
  2549. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2550. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2551. switch (type) {
  2552. case LPFC_SOL_IOCB:
  2553. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2554. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2555. spin_lock_irqsave(&phba->hbalock, iflag);
  2556. break;
  2557. case LPFC_UNSOL_IOCB:
  2558. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2559. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2560. spin_lock_irqsave(&phba->hbalock, iflag);
  2561. if (!rc)
  2562. free_saveq = 0;
  2563. break;
  2564. case LPFC_ABORT_IOCB:
  2565. cmdiocbp = NULL;
  2566. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2567. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2568. saveq);
  2569. if (cmdiocbp) {
  2570. /* Call the specified completion routine */
  2571. if (cmdiocbp->iocb_cmpl) {
  2572. spin_unlock_irqrestore(&phba->hbalock,
  2573. iflag);
  2574. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2575. saveq);
  2576. spin_lock_irqsave(&phba->hbalock,
  2577. iflag);
  2578. } else
  2579. __lpfc_sli_release_iocbq(phba,
  2580. cmdiocbp);
  2581. }
  2582. break;
  2583. case LPFC_UNKNOWN_IOCB:
  2584. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2585. char adaptermsg[LPFC_MAX_ADPTMSG];
  2586. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2587. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2588. MAX_MSG_DATA);
  2589. dev_warn(&((phba->pcidev)->dev),
  2590. "lpfc%d: %s\n",
  2591. phba->brd_no, adaptermsg);
  2592. } else {
  2593. /* Unknown IOCB command */
  2594. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2595. "0335 Unknown IOCB "
  2596. "command Data: x%x "
  2597. "x%x x%x x%x\n",
  2598. irsp->ulpCommand,
  2599. irsp->ulpStatus,
  2600. irsp->ulpIoTag,
  2601. irsp->ulpContext);
  2602. }
  2603. break;
  2604. }
  2605. if (free_saveq) {
  2606. list_for_each_entry_safe(rspiocbp, next_iocb,
  2607. &saveq->list, list) {
  2608. list_del(&rspiocbp->list);
  2609. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2610. }
  2611. __lpfc_sli_release_iocbq(phba, saveq);
  2612. }
  2613. rspiocbp = NULL;
  2614. }
  2615. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2616. return rspiocbp;
  2617. }
  2618. /**
  2619. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2620. * @phba: Pointer to HBA context object.
  2621. * @pring: Pointer to driver SLI ring object.
  2622. * @mask: Host attention register mask for this ring.
  2623. *
  2624. * This routine wraps the actual slow_ring event process routine from the
  2625. * API jump table function pointer from the lpfc_hba struct.
  2626. **/
  2627. void
  2628. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2629. struct lpfc_sli_ring *pring, uint32_t mask)
  2630. {
  2631. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2632. }
  2633. /**
  2634. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2635. * @phba: Pointer to HBA context object.
  2636. * @pring: Pointer to driver SLI ring object.
  2637. * @mask: Host attention register mask for this ring.
  2638. *
  2639. * This function is called from the worker thread when there is a ring event
  2640. * for non-fcp rings. The caller does not hold any lock. The function will
  2641. * remove each response iocb in the response ring and calls the handle
  2642. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2643. **/
  2644. static void
  2645. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2646. struct lpfc_sli_ring *pring, uint32_t mask)
  2647. {
  2648. struct lpfc_pgp *pgp;
  2649. IOCB_t *entry;
  2650. IOCB_t *irsp = NULL;
  2651. struct lpfc_iocbq *rspiocbp = NULL;
  2652. uint32_t portRspPut, portRspMax;
  2653. unsigned long iflag;
  2654. uint32_t status;
  2655. pgp = &phba->port_gp[pring->ringno];
  2656. spin_lock_irqsave(&phba->hbalock, iflag);
  2657. pring->stats.iocb_event++;
  2658. /*
  2659. * The next available response entry should never exceed the maximum
  2660. * entries. If it does, treat it as an adapter hardware error.
  2661. */
  2662. portRspMax = pring->numRiocb;
  2663. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2664. if (portRspPut >= portRspMax) {
  2665. /*
  2666. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2667. * rsp ring <portRspMax>
  2668. */
  2669. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2670. "0303 Ring %d handler: portRspPut %d "
  2671. "is bigger than rsp ring %d\n",
  2672. pring->ringno, portRspPut, portRspMax);
  2673. phba->link_state = LPFC_HBA_ERROR;
  2674. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2675. phba->work_hs = HS_FFER3;
  2676. lpfc_handle_eratt(phba);
  2677. return;
  2678. }
  2679. rmb();
  2680. while (pring->rspidx != portRspPut) {
  2681. /*
  2682. * Build a completion list and call the appropriate handler.
  2683. * The process is to get the next available response iocb, get
  2684. * a free iocb from the list, copy the response data into the
  2685. * free iocb, insert to the continuation list, and update the
  2686. * next response index to slim. This process makes response
  2687. * iocb's in the ring available to DMA as fast as possible but
  2688. * pays a penalty for a copy operation. Since the iocb is
  2689. * only 32 bytes, this penalty is considered small relative to
  2690. * the PCI reads for register values and a slim write. When
  2691. * the ulpLe field is set, the entire Command has been
  2692. * received.
  2693. */
  2694. entry = lpfc_resp_iocb(phba, pring);
  2695. phba->last_completion_time = jiffies;
  2696. rspiocbp = __lpfc_sli_get_iocbq(phba);
  2697. if (rspiocbp == NULL) {
  2698. printk(KERN_ERR "%s: out of buffers! Failing "
  2699. "completion.\n", __func__);
  2700. break;
  2701. }
  2702. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2703. phba->iocb_rsp_size);
  2704. irsp = &rspiocbp->iocb;
  2705. if (++pring->rspidx >= portRspMax)
  2706. pring->rspidx = 0;
  2707. if (pring->ringno == LPFC_ELS_RING) {
  2708. lpfc_debugfs_slow_ring_trc(phba,
  2709. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  2710. *(((uint32_t *) irsp) + 4),
  2711. *(((uint32_t *) irsp) + 6),
  2712. *(((uint32_t *) irsp) + 7));
  2713. }
  2714. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2715. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2716. /* Handle the response IOCB */
  2717. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  2718. spin_lock_irqsave(&phba->hbalock, iflag);
  2719. /*
  2720. * If the port response put pointer has not been updated, sync
  2721. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  2722. * response put pointer.
  2723. */
  2724. if (pring->rspidx == portRspPut) {
  2725. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2726. }
  2727. } /* while (pring->rspidx != portRspPut) */
  2728. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  2729. /* At least one response entry has been freed */
  2730. pring->stats.iocb_rsp_full++;
  2731. /* SET RxRE_RSP in Chip Att register */
  2732. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2733. writel(status, phba->CAregaddr);
  2734. readl(phba->CAregaddr); /* flush */
  2735. }
  2736. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2737. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2738. pring->stats.iocb_cmd_empty++;
  2739. /* Force update of the local copy of cmdGetInx */
  2740. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2741. lpfc_sli_resume_iocb(phba, pring);
  2742. if ((pring->lpfc_sli_cmd_available))
  2743. (pring->lpfc_sli_cmd_available) (phba, pring);
  2744. }
  2745. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2746. return;
  2747. }
  2748. /**
  2749. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  2750. * @phba: Pointer to HBA context object.
  2751. * @pring: Pointer to driver SLI ring object.
  2752. * @mask: Host attention register mask for this ring.
  2753. *
  2754. * This function is called from the worker thread when there is a pending
  2755. * ELS response iocb on the driver internal slow-path response iocb worker
  2756. * queue. The caller does not hold any lock. The function will remove each
  2757. * response iocb from the response worker queue and calls the handle
  2758. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2759. **/
  2760. static void
  2761. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  2762. struct lpfc_sli_ring *pring, uint32_t mask)
  2763. {
  2764. struct lpfc_iocbq *irspiocbq;
  2765. unsigned long iflag;
  2766. while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
  2767. /* Get the response iocb from the head of work queue */
  2768. spin_lock_irqsave(&phba->hbalock, iflag);
  2769. list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
  2770. irspiocbq, struct lpfc_iocbq, list);
  2771. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2772. /* Process the response iocb */
  2773. lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
  2774. }
  2775. }
  2776. /**
  2777. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  2778. * @phba: Pointer to HBA context object.
  2779. * @pring: Pointer to driver SLI ring object.
  2780. *
  2781. * This function aborts all iocbs in the given ring and frees all the iocb
  2782. * objects in txq. This function issues an abort iocb for all the iocb commands
  2783. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  2784. * the return of this function. The caller is not required to hold any locks.
  2785. **/
  2786. void
  2787. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2788. {
  2789. LIST_HEAD(completions);
  2790. struct lpfc_iocbq *iocb, *next_iocb;
  2791. if (pring->ringno == LPFC_ELS_RING) {
  2792. lpfc_fabric_abort_hba(phba);
  2793. }
  2794. /* Error everything on txq and txcmplq
  2795. * First do the txq.
  2796. */
  2797. spin_lock_irq(&phba->hbalock);
  2798. list_splice_init(&pring->txq, &completions);
  2799. pring->txq_cnt = 0;
  2800. /* Next issue ABTS for everything on the txcmplq */
  2801. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  2802. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2803. spin_unlock_irq(&phba->hbalock);
  2804. /* Cancel all the IOCBs from the completions list */
  2805. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  2806. IOERR_SLI_ABORTED);
  2807. }
  2808. /**
  2809. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  2810. * @phba: Pointer to HBA context object.
  2811. *
  2812. * This function flushes all iocbs in the fcp ring and frees all the iocb
  2813. * objects in txq and txcmplq. This function will not issue abort iocbs
  2814. * for all the iocb commands in txcmplq, they will just be returned with
  2815. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2816. * slot has been permanently disabled.
  2817. **/
  2818. void
  2819. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  2820. {
  2821. LIST_HEAD(txq);
  2822. LIST_HEAD(txcmplq);
  2823. struct lpfc_sli *psli = &phba->sli;
  2824. struct lpfc_sli_ring *pring;
  2825. /* Currently, only one fcp ring */
  2826. pring = &psli->ring[psli->fcp_ring];
  2827. spin_lock_irq(&phba->hbalock);
  2828. /* Retrieve everything on txq */
  2829. list_splice_init(&pring->txq, &txq);
  2830. pring->txq_cnt = 0;
  2831. /* Retrieve everything on the txcmplq */
  2832. list_splice_init(&pring->txcmplq, &txcmplq);
  2833. pring->txcmplq_cnt = 0;
  2834. spin_unlock_irq(&phba->hbalock);
  2835. /* Flush the txq */
  2836. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  2837. IOERR_SLI_DOWN);
  2838. /* Flush the txcmpq */
  2839. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  2840. IOERR_SLI_DOWN);
  2841. }
  2842. /**
  2843. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  2844. * @phba: Pointer to HBA context object.
  2845. * @mask: Bit mask to be checked.
  2846. *
  2847. * This function reads the host status register and compares
  2848. * with the provided bit mask to check if HBA completed
  2849. * the restart. This function will wait in a loop for the
  2850. * HBA to complete restart. If the HBA does not restart within
  2851. * 15 iterations, the function will reset the HBA again. The
  2852. * function returns 1 when HBA fail to restart otherwise returns
  2853. * zero.
  2854. **/
  2855. static int
  2856. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  2857. {
  2858. uint32_t status;
  2859. int i = 0;
  2860. int retval = 0;
  2861. /* Read the HBA Host Status Register */
  2862. status = readl(phba->HSregaddr);
  2863. /*
  2864. * Check status register every 100ms for 5 retries, then every
  2865. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  2866. * every 2.5 sec for 4.
  2867. * Break our of the loop if errors occurred during init.
  2868. */
  2869. while (((status & mask) != mask) &&
  2870. !(status & HS_FFERM) &&
  2871. i++ < 20) {
  2872. if (i <= 5)
  2873. msleep(10);
  2874. else if (i <= 10)
  2875. msleep(500);
  2876. else
  2877. msleep(2500);
  2878. if (i == 15) {
  2879. /* Do post */
  2880. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2881. lpfc_sli_brdrestart(phba);
  2882. }
  2883. /* Read the HBA Host Status Register */
  2884. status = readl(phba->HSregaddr);
  2885. }
  2886. /* Check to see if any errors occurred during init */
  2887. if ((status & HS_FFERM) || (i >= 20)) {
  2888. phba->link_state = LPFC_HBA_ERROR;
  2889. retval = 1;
  2890. }
  2891. return retval;
  2892. }
  2893. /**
  2894. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  2895. * @phba: Pointer to HBA context object.
  2896. * @mask: Bit mask to be checked.
  2897. *
  2898. * This function checks the host status register to check if HBA is
  2899. * ready. This function will wait in a loop for the HBA to be ready
  2900. * If the HBA is not ready , the function will will reset the HBA PCI
  2901. * function again. The function returns 1 when HBA fail to be ready
  2902. * otherwise returns zero.
  2903. **/
  2904. static int
  2905. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  2906. {
  2907. uint32_t status;
  2908. int retval = 0;
  2909. /* Read the HBA Host Status Register */
  2910. status = lpfc_sli4_post_status_check(phba);
  2911. if (status) {
  2912. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2913. lpfc_sli_brdrestart(phba);
  2914. status = lpfc_sli4_post_status_check(phba);
  2915. }
  2916. /* Check to see if any errors occurred during init */
  2917. if (status) {
  2918. phba->link_state = LPFC_HBA_ERROR;
  2919. retval = 1;
  2920. } else
  2921. phba->sli4_hba.intr_enable = 0;
  2922. return retval;
  2923. }
  2924. /**
  2925. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  2926. * @phba: Pointer to HBA context object.
  2927. * @mask: Bit mask to be checked.
  2928. *
  2929. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  2930. * from the API jump table function pointer from the lpfc_hba struct.
  2931. **/
  2932. int
  2933. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  2934. {
  2935. return phba->lpfc_sli_brdready(phba, mask);
  2936. }
  2937. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  2938. /**
  2939. * lpfc_reset_barrier - Make HBA ready for HBA reset
  2940. * @phba: Pointer to HBA context object.
  2941. *
  2942. * This function is called before resetting an HBA. This
  2943. * function requests HBA to quiesce DMAs before a reset.
  2944. **/
  2945. void lpfc_reset_barrier(struct lpfc_hba *phba)
  2946. {
  2947. uint32_t __iomem *resp_buf;
  2948. uint32_t __iomem *mbox_buf;
  2949. volatile uint32_t mbox;
  2950. uint32_t hc_copy;
  2951. int i;
  2952. uint8_t hdrtype;
  2953. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  2954. if (hdrtype != 0x80 ||
  2955. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  2956. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  2957. return;
  2958. /*
  2959. * Tell the other part of the chip to suspend temporarily all
  2960. * its DMA activity.
  2961. */
  2962. resp_buf = phba->MBslimaddr;
  2963. /* Disable the error attention */
  2964. hc_copy = readl(phba->HCregaddr);
  2965. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  2966. readl(phba->HCregaddr); /* flush */
  2967. phba->link_flag |= LS_IGNORE_ERATT;
  2968. if (readl(phba->HAregaddr) & HA_ERATT) {
  2969. /* Clear Chip error bit */
  2970. writel(HA_ERATT, phba->HAregaddr);
  2971. phba->pport->stopped = 1;
  2972. }
  2973. mbox = 0;
  2974. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  2975. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  2976. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  2977. mbox_buf = phba->MBslimaddr;
  2978. writel(mbox, mbox_buf);
  2979. for (i = 0;
  2980. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  2981. mdelay(1);
  2982. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  2983. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  2984. phba->pport->stopped)
  2985. goto restore_hc;
  2986. else
  2987. goto clear_errat;
  2988. }
  2989. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  2990. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  2991. mdelay(1);
  2992. clear_errat:
  2993. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  2994. mdelay(1);
  2995. if (readl(phba->HAregaddr) & HA_ERATT) {
  2996. writel(HA_ERATT, phba->HAregaddr);
  2997. phba->pport->stopped = 1;
  2998. }
  2999. restore_hc:
  3000. phba->link_flag &= ~LS_IGNORE_ERATT;
  3001. writel(hc_copy, phba->HCregaddr);
  3002. readl(phba->HCregaddr); /* flush */
  3003. }
  3004. /**
  3005. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3006. * @phba: Pointer to HBA context object.
  3007. *
  3008. * This function issues a kill_board mailbox command and waits for
  3009. * the error attention interrupt. This function is called for stopping
  3010. * the firmware processing. The caller is not required to hold any
  3011. * locks. This function calls lpfc_hba_down_post function to free
  3012. * any pending commands after the kill. The function will return 1 when it
  3013. * fails to kill the board else will return 0.
  3014. **/
  3015. int
  3016. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3017. {
  3018. struct lpfc_sli *psli;
  3019. LPFC_MBOXQ_t *pmb;
  3020. uint32_t status;
  3021. uint32_t ha_copy;
  3022. int retval;
  3023. int i = 0;
  3024. psli = &phba->sli;
  3025. /* Kill HBA */
  3026. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3027. "0329 Kill HBA Data: x%x x%x\n",
  3028. phba->pport->port_state, psli->sli_flag);
  3029. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3030. if (!pmb)
  3031. return 1;
  3032. /* Disable the error attention */
  3033. spin_lock_irq(&phba->hbalock);
  3034. status = readl(phba->HCregaddr);
  3035. status &= ~HC_ERINT_ENA;
  3036. writel(status, phba->HCregaddr);
  3037. readl(phba->HCregaddr); /* flush */
  3038. phba->link_flag |= LS_IGNORE_ERATT;
  3039. spin_unlock_irq(&phba->hbalock);
  3040. lpfc_kill_board(phba, pmb);
  3041. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3042. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3043. if (retval != MBX_SUCCESS) {
  3044. if (retval != MBX_BUSY)
  3045. mempool_free(pmb, phba->mbox_mem_pool);
  3046. spin_lock_irq(&phba->hbalock);
  3047. phba->link_flag &= ~LS_IGNORE_ERATT;
  3048. spin_unlock_irq(&phba->hbalock);
  3049. return 1;
  3050. }
  3051. spin_lock_irq(&phba->hbalock);
  3052. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3053. spin_unlock_irq(&phba->hbalock);
  3054. mempool_free(pmb, phba->mbox_mem_pool);
  3055. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3056. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3057. * 3 seconds we still set HBA_ERROR state because the status of the
  3058. * board is now undefined.
  3059. */
  3060. ha_copy = readl(phba->HAregaddr);
  3061. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3062. mdelay(100);
  3063. ha_copy = readl(phba->HAregaddr);
  3064. }
  3065. del_timer_sync(&psli->mbox_tmo);
  3066. if (ha_copy & HA_ERATT) {
  3067. writel(HA_ERATT, phba->HAregaddr);
  3068. phba->pport->stopped = 1;
  3069. }
  3070. spin_lock_irq(&phba->hbalock);
  3071. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3072. psli->mbox_active = NULL;
  3073. phba->link_flag &= ~LS_IGNORE_ERATT;
  3074. spin_unlock_irq(&phba->hbalock);
  3075. lpfc_hba_down_post(phba);
  3076. phba->link_state = LPFC_HBA_ERROR;
  3077. return ha_copy & HA_ERATT ? 0 : 1;
  3078. }
  3079. /**
  3080. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3081. * @phba: Pointer to HBA context object.
  3082. *
  3083. * This function resets the HBA by writing HC_INITFF to the control
  3084. * register. After the HBA resets, this function resets all the iocb ring
  3085. * indices. This function disables PCI layer parity checking during
  3086. * the reset.
  3087. * This function returns 0 always.
  3088. * The caller is not required to hold any locks.
  3089. **/
  3090. int
  3091. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3092. {
  3093. struct lpfc_sli *psli;
  3094. struct lpfc_sli_ring *pring;
  3095. uint16_t cfg_value;
  3096. int i;
  3097. psli = &phba->sli;
  3098. /* Reset HBA */
  3099. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3100. "0325 Reset HBA Data: x%x x%x\n",
  3101. phba->pport->port_state, psli->sli_flag);
  3102. /* perform board reset */
  3103. phba->fc_eventTag = 0;
  3104. phba->pport->fc_myDID = 0;
  3105. phba->pport->fc_prevDID = 0;
  3106. /* Turn off parity checking and serr during the physical reset */
  3107. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3108. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3109. (cfg_value &
  3110. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3111. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3112. /* Now toggle INITFF bit in the Host Control Register */
  3113. writel(HC_INITFF, phba->HCregaddr);
  3114. mdelay(1);
  3115. readl(phba->HCregaddr); /* flush */
  3116. writel(0, phba->HCregaddr);
  3117. readl(phba->HCregaddr); /* flush */
  3118. /* Restore PCI cmd register */
  3119. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3120. /* Initialize relevant SLI info */
  3121. for (i = 0; i < psli->num_rings; i++) {
  3122. pring = &psli->ring[i];
  3123. pring->flag = 0;
  3124. pring->rspidx = 0;
  3125. pring->next_cmdidx = 0;
  3126. pring->local_getidx = 0;
  3127. pring->cmdidx = 0;
  3128. pring->missbufcnt = 0;
  3129. }
  3130. phba->link_state = LPFC_WARM_START;
  3131. return 0;
  3132. }
  3133. /**
  3134. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3135. * @phba: Pointer to HBA context object.
  3136. *
  3137. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3138. * checking during resets the device. The caller is not required to hold
  3139. * any locks.
  3140. *
  3141. * This function returns 0 always.
  3142. **/
  3143. int
  3144. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3145. {
  3146. struct lpfc_sli *psli = &phba->sli;
  3147. uint16_t cfg_value;
  3148. uint8_t qindx;
  3149. /* Reset HBA */
  3150. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3151. "0295 Reset HBA Data: x%x x%x\n",
  3152. phba->pport->port_state, psli->sli_flag);
  3153. /* perform board reset */
  3154. phba->fc_eventTag = 0;
  3155. phba->pport->fc_myDID = 0;
  3156. phba->pport->fc_prevDID = 0;
  3157. /* Turn off parity checking and serr during the physical reset */
  3158. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3159. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3160. (cfg_value &
  3161. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3162. spin_lock_irq(&phba->hbalock);
  3163. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3164. phba->fcf.fcf_flag = 0;
  3165. /* Clean up the child queue list for the CQs */
  3166. list_del_init(&phba->sli4_hba.mbx_wq->list);
  3167. list_del_init(&phba->sli4_hba.els_wq->list);
  3168. list_del_init(&phba->sli4_hba.hdr_rq->list);
  3169. list_del_init(&phba->sli4_hba.dat_rq->list);
  3170. list_del_init(&phba->sli4_hba.mbx_cq->list);
  3171. list_del_init(&phba->sli4_hba.els_cq->list);
  3172. list_del_init(&phba->sli4_hba.rxq_cq->list);
  3173. for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
  3174. list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
  3175. for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
  3176. list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
  3177. spin_unlock_irq(&phba->hbalock);
  3178. /* Now physically reset the device */
  3179. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3180. "0389 Performing PCI function reset!\n");
  3181. /* Perform FCoE PCI function reset */
  3182. lpfc_pci_function_reset(phba);
  3183. return 0;
  3184. }
  3185. /**
  3186. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3187. * @phba: Pointer to HBA context object.
  3188. *
  3189. * This function is called in the SLI initialization code path to
  3190. * restart the HBA. The caller is not required to hold any lock.
  3191. * This function writes MBX_RESTART mailbox command to the SLIM and
  3192. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3193. * function to free any pending commands. The function enables
  3194. * POST only during the first initialization. The function returns zero.
  3195. * The function does not guarantee completion of MBX_RESTART mailbox
  3196. * command before the return of this function.
  3197. **/
  3198. static int
  3199. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3200. {
  3201. MAILBOX_t *mb;
  3202. struct lpfc_sli *psli;
  3203. volatile uint32_t word0;
  3204. void __iomem *to_slim;
  3205. spin_lock_irq(&phba->hbalock);
  3206. psli = &phba->sli;
  3207. /* Restart HBA */
  3208. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3209. "0337 Restart HBA Data: x%x x%x\n",
  3210. phba->pport->port_state, psli->sli_flag);
  3211. word0 = 0;
  3212. mb = (MAILBOX_t *) &word0;
  3213. mb->mbxCommand = MBX_RESTART;
  3214. mb->mbxHc = 1;
  3215. lpfc_reset_barrier(phba);
  3216. to_slim = phba->MBslimaddr;
  3217. writel(*(uint32_t *) mb, to_slim);
  3218. readl(to_slim); /* flush */
  3219. /* Only skip post after fc_ffinit is completed */
  3220. if (phba->pport->port_state)
  3221. word0 = 1; /* This is really setting up word1 */
  3222. else
  3223. word0 = 0; /* This is really setting up word1 */
  3224. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3225. writel(*(uint32_t *) mb, to_slim);
  3226. readl(to_slim); /* flush */
  3227. lpfc_sli_brdreset(phba);
  3228. phba->pport->stopped = 0;
  3229. phba->link_state = LPFC_INIT_START;
  3230. phba->hba_flag = 0;
  3231. spin_unlock_irq(&phba->hbalock);
  3232. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3233. psli->stats_start = get_seconds();
  3234. /* Give the INITFF and Post time to settle. */
  3235. mdelay(100);
  3236. lpfc_hba_down_post(phba);
  3237. return 0;
  3238. }
  3239. /**
  3240. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3241. * @phba: Pointer to HBA context object.
  3242. *
  3243. * This function is called in the SLI initialization code path to restart
  3244. * a SLI4 HBA. The caller is not required to hold any lock.
  3245. * At the end of the function, it calls lpfc_hba_down_post function to
  3246. * free any pending commands.
  3247. **/
  3248. static int
  3249. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3250. {
  3251. struct lpfc_sli *psli = &phba->sli;
  3252. /* Restart HBA */
  3253. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3254. "0296 Restart HBA Data: x%x x%x\n",
  3255. phba->pport->port_state, psli->sli_flag);
  3256. lpfc_sli4_brdreset(phba);
  3257. spin_lock_irq(&phba->hbalock);
  3258. phba->pport->stopped = 0;
  3259. phba->link_state = LPFC_INIT_START;
  3260. phba->hba_flag = 0;
  3261. spin_unlock_irq(&phba->hbalock);
  3262. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3263. psli->stats_start = get_seconds();
  3264. lpfc_hba_down_post(phba);
  3265. return 0;
  3266. }
  3267. /**
  3268. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3269. * @phba: Pointer to HBA context object.
  3270. *
  3271. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3272. * API jump table function pointer from the lpfc_hba struct.
  3273. **/
  3274. int
  3275. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3276. {
  3277. return phba->lpfc_sli_brdrestart(phba);
  3278. }
  3279. /**
  3280. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3281. * @phba: Pointer to HBA context object.
  3282. *
  3283. * This function is called after a HBA restart to wait for successful
  3284. * restart of the HBA. Successful restart of the HBA is indicated by
  3285. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3286. * iteration, the function will restart the HBA again. The function returns
  3287. * zero if HBA successfully restarted else returns negative error code.
  3288. **/
  3289. static int
  3290. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3291. {
  3292. uint32_t status, i = 0;
  3293. /* Read the HBA Host Status Register */
  3294. status = readl(phba->HSregaddr);
  3295. /* Check status register to see what current state is */
  3296. i = 0;
  3297. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3298. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  3299. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  3300. * 4.
  3301. */
  3302. if (i++ >= 20) {
  3303. /* Adapter failed to init, timeout, status reg
  3304. <status> */
  3305. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3306. "0436 Adapter failed to init, "
  3307. "timeout, status reg x%x, "
  3308. "FW Data: A8 x%x AC x%x\n", status,
  3309. readl(phba->MBslimaddr + 0xa8),
  3310. readl(phba->MBslimaddr + 0xac));
  3311. phba->link_state = LPFC_HBA_ERROR;
  3312. return -ETIMEDOUT;
  3313. }
  3314. /* Check to see if any errors occurred during init */
  3315. if (status & HS_FFERM) {
  3316. /* ERROR: During chipset initialization */
  3317. /* Adapter failed to init, chipset, status reg
  3318. <status> */
  3319. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3320. "0437 Adapter failed to init, "
  3321. "chipset, status reg x%x, "
  3322. "FW Data: A8 x%x AC x%x\n", status,
  3323. readl(phba->MBslimaddr + 0xa8),
  3324. readl(phba->MBslimaddr + 0xac));
  3325. phba->link_state = LPFC_HBA_ERROR;
  3326. return -EIO;
  3327. }
  3328. if (i <= 5) {
  3329. msleep(10);
  3330. } else if (i <= 10) {
  3331. msleep(500);
  3332. } else {
  3333. msleep(2500);
  3334. }
  3335. if (i == 15) {
  3336. /* Do post */
  3337. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3338. lpfc_sli_brdrestart(phba);
  3339. }
  3340. /* Read the HBA Host Status Register */
  3341. status = readl(phba->HSregaddr);
  3342. }
  3343. /* Check to see if any errors occurred during init */
  3344. if (status & HS_FFERM) {
  3345. /* ERROR: During chipset initialization */
  3346. /* Adapter failed to init, chipset, status reg <status> */
  3347. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3348. "0438 Adapter failed to init, chipset, "
  3349. "status reg x%x, "
  3350. "FW Data: A8 x%x AC x%x\n", status,
  3351. readl(phba->MBslimaddr + 0xa8),
  3352. readl(phba->MBslimaddr + 0xac));
  3353. phba->link_state = LPFC_HBA_ERROR;
  3354. return -EIO;
  3355. }
  3356. /* Clear all interrupt enable conditions */
  3357. writel(0, phba->HCregaddr);
  3358. readl(phba->HCregaddr); /* flush */
  3359. /* setup host attn register */
  3360. writel(0xffffffff, phba->HAregaddr);
  3361. readl(phba->HAregaddr); /* flush */
  3362. return 0;
  3363. }
  3364. /**
  3365. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3366. *
  3367. * This function calculates and returns the number of HBQs required to be
  3368. * configured.
  3369. **/
  3370. int
  3371. lpfc_sli_hbq_count(void)
  3372. {
  3373. return ARRAY_SIZE(lpfc_hbq_defs);
  3374. }
  3375. /**
  3376. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3377. *
  3378. * This function adds the number of hbq entries in every HBQ to get
  3379. * the total number of hbq entries required for the HBA and returns
  3380. * the total count.
  3381. **/
  3382. static int
  3383. lpfc_sli_hbq_entry_count(void)
  3384. {
  3385. int hbq_count = lpfc_sli_hbq_count();
  3386. int count = 0;
  3387. int i;
  3388. for (i = 0; i < hbq_count; ++i)
  3389. count += lpfc_hbq_defs[i]->entry_count;
  3390. return count;
  3391. }
  3392. /**
  3393. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3394. *
  3395. * This function calculates amount of memory required for all hbq entries
  3396. * to be configured and returns the total memory required.
  3397. **/
  3398. int
  3399. lpfc_sli_hbq_size(void)
  3400. {
  3401. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3402. }
  3403. /**
  3404. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3405. * @phba: Pointer to HBA context object.
  3406. *
  3407. * This function is called during the SLI initialization to configure
  3408. * all the HBQs and post buffers to the HBQ. The caller is not
  3409. * required to hold any locks. This function will return zero if successful
  3410. * else it will return negative error code.
  3411. **/
  3412. static int
  3413. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3414. {
  3415. int hbq_count = lpfc_sli_hbq_count();
  3416. LPFC_MBOXQ_t *pmb;
  3417. MAILBOX_t *pmbox;
  3418. uint32_t hbqno;
  3419. uint32_t hbq_entry_index;
  3420. /* Get a Mailbox buffer to setup mailbox
  3421. * commands for HBA initialization
  3422. */
  3423. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3424. if (!pmb)
  3425. return -ENOMEM;
  3426. pmbox = &pmb->u.mb;
  3427. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3428. phba->link_state = LPFC_INIT_MBX_CMDS;
  3429. phba->hbq_in_use = 1;
  3430. hbq_entry_index = 0;
  3431. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3432. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3433. phba->hbqs[hbqno].hbqPutIdx = 0;
  3434. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3435. phba->hbqs[hbqno].entry_count =
  3436. lpfc_hbq_defs[hbqno]->entry_count;
  3437. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3438. hbq_entry_index, pmb);
  3439. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3440. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3441. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3442. mbxStatus <status>, ring <num> */
  3443. lpfc_printf_log(phba, KERN_ERR,
  3444. LOG_SLI | LOG_VPORT,
  3445. "1805 Adapter failed to init. "
  3446. "Data: x%x x%x x%x\n",
  3447. pmbox->mbxCommand,
  3448. pmbox->mbxStatus, hbqno);
  3449. phba->link_state = LPFC_HBA_ERROR;
  3450. mempool_free(pmb, phba->mbox_mem_pool);
  3451. return ENXIO;
  3452. }
  3453. }
  3454. phba->hbq_count = hbq_count;
  3455. mempool_free(pmb, phba->mbox_mem_pool);
  3456. /* Initially populate or replenish the HBQs */
  3457. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3458. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3459. return 0;
  3460. }
  3461. /**
  3462. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3463. * @phba: Pointer to HBA context object.
  3464. *
  3465. * This function is called during the SLI initialization to configure
  3466. * all the HBQs and post buffers to the HBQ. The caller is not
  3467. * required to hold any locks. This function will return zero if successful
  3468. * else it will return negative error code.
  3469. **/
  3470. static int
  3471. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3472. {
  3473. phba->hbq_in_use = 1;
  3474. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3475. phba->hbq_count = 1;
  3476. /* Initially populate or replenish the HBQs */
  3477. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3478. return 0;
  3479. }
  3480. /**
  3481. * lpfc_sli_config_port - Issue config port mailbox command
  3482. * @phba: Pointer to HBA context object.
  3483. * @sli_mode: sli mode - 2/3
  3484. *
  3485. * This function is called by the sli intialization code path
  3486. * to issue config_port mailbox command. This function restarts the
  3487. * HBA firmware and issues a config_port mailbox command to configure
  3488. * the SLI interface in the sli mode specified by sli_mode
  3489. * variable. The caller is not required to hold any locks.
  3490. * The function returns 0 if successful, else returns negative error
  3491. * code.
  3492. **/
  3493. int
  3494. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3495. {
  3496. LPFC_MBOXQ_t *pmb;
  3497. uint32_t resetcount = 0, rc = 0, done = 0;
  3498. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3499. if (!pmb) {
  3500. phba->link_state = LPFC_HBA_ERROR;
  3501. return -ENOMEM;
  3502. }
  3503. phba->sli_rev = sli_mode;
  3504. while (resetcount < 2 && !done) {
  3505. spin_lock_irq(&phba->hbalock);
  3506. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3507. spin_unlock_irq(&phba->hbalock);
  3508. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3509. lpfc_sli_brdrestart(phba);
  3510. rc = lpfc_sli_chipset_init(phba);
  3511. if (rc)
  3512. break;
  3513. spin_lock_irq(&phba->hbalock);
  3514. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3515. spin_unlock_irq(&phba->hbalock);
  3516. resetcount++;
  3517. /* Call pre CONFIG_PORT mailbox command initialization. A
  3518. * value of 0 means the call was successful. Any other
  3519. * nonzero value is a failure, but if ERESTART is returned,
  3520. * the driver may reset the HBA and try again.
  3521. */
  3522. rc = lpfc_config_port_prep(phba);
  3523. if (rc == -ERESTART) {
  3524. phba->link_state = LPFC_LINK_UNKNOWN;
  3525. continue;
  3526. } else if (rc)
  3527. break;
  3528. phba->link_state = LPFC_INIT_MBX_CMDS;
  3529. lpfc_config_port(phba, pmb);
  3530. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3531. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3532. LPFC_SLI3_HBQ_ENABLED |
  3533. LPFC_SLI3_CRP_ENABLED |
  3534. LPFC_SLI3_INB_ENABLED |
  3535. LPFC_SLI3_BG_ENABLED);
  3536. if (rc != MBX_SUCCESS) {
  3537. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3538. "0442 Adapter failed to init, mbxCmd x%x "
  3539. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3540. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3541. spin_lock_irq(&phba->hbalock);
  3542. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3543. spin_unlock_irq(&phba->hbalock);
  3544. rc = -ENXIO;
  3545. } else {
  3546. /* Allow asynchronous mailbox command to go through */
  3547. spin_lock_irq(&phba->hbalock);
  3548. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3549. spin_unlock_irq(&phba->hbalock);
  3550. done = 1;
  3551. }
  3552. }
  3553. if (!done) {
  3554. rc = -EINVAL;
  3555. goto do_prep_failed;
  3556. }
  3557. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3558. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3559. rc = -ENXIO;
  3560. goto do_prep_failed;
  3561. }
  3562. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3563. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3564. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3565. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3566. phba->max_vpi : phba->max_vports;
  3567. } else
  3568. phba->max_vpi = 0;
  3569. if (pmb->u.mb.un.varCfgPort.gdss)
  3570. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3571. if (pmb->u.mb.un.varCfgPort.gerbm)
  3572. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3573. if (pmb->u.mb.un.varCfgPort.gcrp)
  3574. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3575. if (pmb->u.mb.un.varCfgPort.ginb) {
  3576. phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
  3577. phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
  3578. phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
  3579. phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
  3580. phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
  3581. phba->inb_last_counter =
  3582. phba->mbox->us.s3_inb_pgp.counter;
  3583. } else {
  3584. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3585. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3586. phba->inb_ha_copy = NULL;
  3587. phba->inb_counter = NULL;
  3588. }
  3589. if (phba->cfg_enable_bg) {
  3590. if (pmb->u.mb.un.varCfgPort.gbg)
  3591. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3592. else
  3593. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3594. "0443 Adapter did not grant "
  3595. "BlockGuard\n");
  3596. }
  3597. } else {
  3598. phba->hbq_get = NULL;
  3599. phba->port_gp = phba->mbox->us.s2.port;
  3600. phba->inb_ha_copy = NULL;
  3601. phba->inb_counter = NULL;
  3602. phba->max_vpi = 0;
  3603. }
  3604. do_prep_failed:
  3605. mempool_free(pmb, phba->mbox_mem_pool);
  3606. return rc;
  3607. }
  3608. /**
  3609. * lpfc_sli_hba_setup - SLI intialization function
  3610. * @phba: Pointer to HBA context object.
  3611. *
  3612. * This function is the main SLI intialization function. This function
  3613. * is called by the HBA intialization code, HBA reset code and HBA
  3614. * error attention handler code. Caller is not required to hold any
  3615. * locks. This function issues config_port mailbox command to configure
  3616. * the SLI, setup iocb rings and HBQ rings. In the end the function
  3617. * calls the config_port_post function to issue init_link mailbox
  3618. * command and to start the discovery. The function will return zero
  3619. * if successful, else it will return negative error code.
  3620. **/
  3621. int
  3622. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  3623. {
  3624. uint32_t rc;
  3625. int mode = 3;
  3626. switch (lpfc_sli_mode) {
  3627. case 2:
  3628. if (phba->cfg_enable_npiv) {
  3629. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3630. "1824 NPIV enabled: Override lpfc_sli_mode "
  3631. "parameter (%d) to auto (0).\n",
  3632. lpfc_sli_mode);
  3633. break;
  3634. }
  3635. mode = 2;
  3636. break;
  3637. case 0:
  3638. case 3:
  3639. break;
  3640. default:
  3641. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3642. "1819 Unrecognized lpfc_sli_mode "
  3643. "parameter: %d.\n", lpfc_sli_mode);
  3644. break;
  3645. }
  3646. rc = lpfc_sli_config_port(phba, mode);
  3647. if (rc && lpfc_sli_mode == 3)
  3648. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  3649. "1820 Unable to select SLI-3. "
  3650. "Not supported by adapter.\n");
  3651. if (rc && mode != 2)
  3652. rc = lpfc_sli_config_port(phba, 2);
  3653. if (rc)
  3654. goto lpfc_sli_hba_setup_error;
  3655. if (phba->sli_rev == 3) {
  3656. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  3657. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  3658. } else {
  3659. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  3660. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  3661. phba->sli3_options = 0;
  3662. }
  3663. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3664. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  3665. phba->sli_rev, phba->max_vpi);
  3666. rc = lpfc_sli_ring_map(phba);
  3667. if (rc)
  3668. goto lpfc_sli_hba_setup_error;
  3669. /* Init HBQs */
  3670. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  3671. rc = lpfc_sli_hbq_setup(phba);
  3672. if (rc)
  3673. goto lpfc_sli_hba_setup_error;
  3674. }
  3675. spin_lock_irq(&phba->hbalock);
  3676. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  3677. spin_unlock_irq(&phba->hbalock);
  3678. rc = lpfc_config_port_post(phba);
  3679. if (rc)
  3680. goto lpfc_sli_hba_setup_error;
  3681. return rc;
  3682. lpfc_sli_hba_setup_error:
  3683. phba->link_state = LPFC_HBA_ERROR;
  3684. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3685. "0445 Firmware initialization failed\n");
  3686. return rc;
  3687. }
  3688. /**
  3689. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  3690. * @phba: Pointer to HBA context object.
  3691. * @mboxq: mailbox pointer.
  3692. * This function issue a dump mailbox command to read config region
  3693. * 23 and parse the records in the region and populate driver
  3694. * data structure.
  3695. **/
  3696. static int
  3697. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
  3698. LPFC_MBOXQ_t *mboxq)
  3699. {
  3700. struct lpfc_dmabuf *mp;
  3701. struct lpfc_mqe *mqe;
  3702. uint32_t data_length;
  3703. int rc;
  3704. /* Program the default value of vlan_id and fc_map */
  3705. phba->valid_vlan = 0;
  3706. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  3707. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  3708. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  3709. mqe = &mboxq->u.mqe;
  3710. if (lpfc_dump_fcoe_param(phba, mboxq))
  3711. return -ENOMEM;
  3712. mp = (struct lpfc_dmabuf *) mboxq->context1;
  3713. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3714. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3715. "(%d):2571 Mailbox cmd x%x Status x%x "
  3716. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  3717. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  3718. "CQ: x%x x%x x%x x%x\n",
  3719. mboxq->vport ? mboxq->vport->vpi : 0,
  3720. bf_get(lpfc_mqe_command, mqe),
  3721. bf_get(lpfc_mqe_status, mqe),
  3722. mqe->un.mb_words[0], mqe->un.mb_words[1],
  3723. mqe->un.mb_words[2], mqe->un.mb_words[3],
  3724. mqe->un.mb_words[4], mqe->un.mb_words[5],
  3725. mqe->un.mb_words[6], mqe->un.mb_words[7],
  3726. mqe->un.mb_words[8], mqe->un.mb_words[9],
  3727. mqe->un.mb_words[10], mqe->un.mb_words[11],
  3728. mqe->un.mb_words[12], mqe->un.mb_words[13],
  3729. mqe->un.mb_words[14], mqe->un.mb_words[15],
  3730. mqe->un.mb_words[16], mqe->un.mb_words[50],
  3731. mboxq->mcqe.word0,
  3732. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  3733. mboxq->mcqe.trailer);
  3734. if (rc) {
  3735. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3736. kfree(mp);
  3737. return -EIO;
  3738. }
  3739. data_length = mqe->un.mb_words[5];
  3740. if (data_length > DMP_RGN23_SIZE) {
  3741. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3742. kfree(mp);
  3743. return -EIO;
  3744. }
  3745. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  3746. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3747. kfree(mp);
  3748. return 0;
  3749. }
  3750. /**
  3751. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  3752. * @phba: pointer to lpfc hba data structure.
  3753. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  3754. * @vpd: pointer to the memory to hold resulting port vpd data.
  3755. * @vpd_size: On input, the number of bytes allocated to @vpd.
  3756. * On output, the number of data bytes in @vpd.
  3757. *
  3758. * This routine executes a READ_REV SLI4 mailbox command. In
  3759. * addition, this routine gets the port vpd data.
  3760. *
  3761. * Return codes
  3762. * 0 - sucessful
  3763. * ENOMEM - could not allocated memory.
  3764. **/
  3765. static int
  3766. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  3767. uint8_t *vpd, uint32_t *vpd_size)
  3768. {
  3769. int rc = 0;
  3770. uint32_t dma_size;
  3771. struct lpfc_dmabuf *dmabuf;
  3772. struct lpfc_mqe *mqe;
  3773. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  3774. if (!dmabuf)
  3775. return -ENOMEM;
  3776. /*
  3777. * Get a DMA buffer for the vpd data resulting from the READ_REV
  3778. * mailbox command.
  3779. */
  3780. dma_size = *vpd_size;
  3781. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  3782. dma_size,
  3783. &dmabuf->phys,
  3784. GFP_KERNEL);
  3785. if (!dmabuf->virt) {
  3786. kfree(dmabuf);
  3787. return -ENOMEM;
  3788. }
  3789. memset(dmabuf->virt, 0, dma_size);
  3790. /*
  3791. * The SLI4 implementation of READ_REV conflicts at word1,
  3792. * bits 31:16 and SLI4 adds vpd functionality not present
  3793. * in SLI3. This code corrects the conflicts.
  3794. */
  3795. lpfc_read_rev(phba, mboxq);
  3796. mqe = &mboxq->u.mqe;
  3797. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  3798. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  3799. mqe->un.read_rev.word1 &= 0x0000FFFF;
  3800. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  3801. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  3802. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3803. if (rc) {
  3804. dma_free_coherent(&phba->pcidev->dev, dma_size,
  3805. dmabuf->virt, dmabuf->phys);
  3806. return -EIO;
  3807. }
  3808. /*
  3809. * The available vpd length cannot be bigger than the
  3810. * DMA buffer passed to the port. Catch the less than
  3811. * case and update the caller's size.
  3812. */
  3813. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  3814. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  3815. lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
  3816. dma_free_coherent(&phba->pcidev->dev, dma_size,
  3817. dmabuf->virt, dmabuf->phys);
  3818. kfree(dmabuf);
  3819. return 0;
  3820. }
  3821. /**
  3822. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  3823. * @phba: pointer to lpfc hba data structure.
  3824. *
  3825. * This routine is called to explicitly arm the SLI4 device's completion and
  3826. * event queues
  3827. **/
  3828. static void
  3829. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  3830. {
  3831. uint8_t fcp_eqidx;
  3832. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  3833. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  3834. lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
  3835. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  3836. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  3837. LPFC_QUEUE_REARM);
  3838. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  3839. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  3840. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  3841. LPFC_QUEUE_REARM);
  3842. }
  3843. /**
  3844. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  3845. * @phba: Pointer to HBA context object.
  3846. *
  3847. * This function is the main SLI4 device intialization PCI function. This
  3848. * function is called by the HBA intialization code, HBA reset code and
  3849. * HBA error attention handler code. Caller is not required to hold any
  3850. * locks.
  3851. **/
  3852. int
  3853. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  3854. {
  3855. int rc;
  3856. LPFC_MBOXQ_t *mboxq;
  3857. struct lpfc_mqe *mqe;
  3858. uint8_t *vpd;
  3859. uint32_t vpd_size;
  3860. uint32_t ftr_rsp = 0;
  3861. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  3862. struct lpfc_vport *vport = phba->pport;
  3863. struct lpfc_dmabuf *mp;
  3864. /* Perform a PCI function reset to start from clean */
  3865. rc = lpfc_pci_function_reset(phba);
  3866. if (unlikely(rc))
  3867. return -ENODEV;
  3868. /* Check the HBA Host Status Register for readyness */
  3869. rc = lpfc_sli4_post_status_check(phba);
  3870. if (unlikely(rc))
  3871. return -ENODEV;
  3872. else {
  3873. spin_lock_irq(&phba->hbalock);
  3874. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  3875. spin_unlock_irq(&phba->hbalock);
  3876. }
  3877. /*
  3878. * Allocate a single mailbox container for initializing the
  3879. * port.
  3880. */
  3881. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3882. if (!mboxq)
  3883. return -ENOMEM;
  3884. /*
  3885. * Continue initialization with default values even if driver failed
  3886. * to read FCoE param config regions
  3887. */
  3888. if (lpfc_sli4_read_fcoe_params(phba, mboxq))
  3889. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  3890. "2570 Failed to read FCoE parameters\n");
  3891. /* Issue READ_REV to collect vpd and FW information. */
  3892. vpd_size = PAGE_SIZE;
  3893. vpd = kzalloc(vpd_size, GFP_KERNEL);
  3894. if (!vpd) {
  3895. rc = -ENOMEM;
  3896. goto out_free_mbox;
  3897. }
  3898. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  3899. if (unlikely(rc))
  3900. goto out_free_vpd;
  3901. mqe = &mboxq->u.mqe;
  3902. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  3903. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  3904. phba->hba_flag |= HBA_FCOE_SUPPORT;
  3905. if (phba->sli_rev != LPFC_SLI_REV4 ||
  3906. !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
  3907. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  3908. "0376 READ_REV Error. SLI Level %d "
  3909. "FCoE enabled %d\n",
  3910. phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
  3911. rc = -EIO;
  3912. goto out_free_vpd;
  3913. }
  3914. /*
  3915. * Evaluate the read rev and vpd data. Populate the driver
  3916. * state with the results. If this routine fails, the failure
  3917. * is not fatal as the driver will use generic values.
  3918. */
  3919. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  3920. if (unlikely(!rc)) {
  3921. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  3922. "0377 Error %d parsing vpd. "
  3923. "Using defaults.\n", rc);
  3924. rc = 0;
  3925. }
  3926. /* Save information as VPD data */
  3927. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  3928. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  3929. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  3930. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  3931. &mqe->un.read_rev);
  3932. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  3933. &mqe->un.read_rev);
  3934. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  3935. &mqe->un.read_rev);
  3936. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  3937. &mqe->un.read_rev);
  3938. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  3939. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  3940. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  3941. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  3942. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  3943. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  3944. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3945. "(%d):0380 READ_REV Status x%x "
  3946. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  3947. mboxq->vport ? mboxq->vport->vpi : 0,
  3948. bf_get(lpfc_mqe_status, mqe),
  3949. phba->vpd.rev.opFwName,
  3950. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  3951. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  3952. /*
  3953. * Discover the port's supported feature set and match it against the
  3954. * hosts requests.
  3955. */
  3956. lpfc_request_features(phba, mboxq);
  3957. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  3958. if (unlikely(rc)) {
  3959. rc = -EIO;
  3960. goto out_free_vpd;
  3961. }
  3962. /*
  3963. * The port must support FCP initiator mode as this is the
  3964. * only mode running in the host.
  3965. */
  3966. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  3967. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  3968. "0378 No support for fcpi mode.\n");
  3969. ftr_rsp++;
  3970. }
  3971. /*
  3972. * If the port cannot support the host's requested features
  3973. * then turn off the global config parameters to disable the
  3974. * feature in the driver. This is not a fatal error.
  3975. */
  3976. if ((phba->cfg_enable_bg) &&
  3977. !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  3978. ftr_rsp++;
  3979. if (phba->max_vpi && phba->cfg_enable_npiv &&
  3980. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  3981. ftr_rsp++;
  3982. if (ftr_rsp) {
  3983. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  3984. "0379 Feature Mismatch Data: x%08x %08x "
  3985. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  3986. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  3987. phba->cfg_enable_npiv, phba->max_vpi);
  3988. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  3989. phba->cfg_enable_bg = 0;
  3990. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  3991. phba->cfg_enable_npiv = 0;
  3992. }
  3993. /* These SLI3 features are assumed in SLI4 */
  3994. spin_lock_irq(&phba->hbalock);
  3995. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  3996. spin_unlock_irq(&phba->hbalock);
  3997. /* Read the port's service parameters. */
  3998. lpfc_read_sparam(phba, mboxq, vport->vpi);
  3999. mboxq->vport = vport;
  4000. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4001. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4002. if (rc == MBX_SUCCESS) {
  4003. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  4004. rc = 0;
  4005. }
  4006. /*
  4007. * This memory was allocated by the lpfc_read_sparam routine. Release
  4008. * it to the mbuf pool.
  4009. */
  4010. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4011. kfree(mp);
  4012. mboxq->context1 = NULL;
  4013. if (unlikely(rc)) {
  4014. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4015. "0382 READ_SPARAM command failed "
  4016. "status %d, mbxStatus x%x\n",
  4017. rc, bf_get(lpfc_mqe_status, mqe));
  4018. phba->link_state = LPFC_HBA_ERROR;
  4019. rc = -EIO;
  4020. goto out_free_vpd;
  4021. }
  4022. if (phba->cfg_soft_wwnn)
  4023. u64_to_wwn(phba->cfg_soft_wwnn,
  4024. vport->fc_sparam.nodeName.u.wwn);
  4025. if (phba->cfg_soft_wwpn)
  4026. u64_to_wwn(phba->cfg_soft_wwpn,
  4027. vport->fc_sparam.portName.u.wwn);
  4028. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  4029. sizeof(struct lpfc_name));
  4030. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  4031. sizeof(struct lpfc_name));
  4032. /* Update the fc_host data structures with new wwn. */
  4033. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  4034. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  4035. /* Register SGL pool to the device using non-embedded mailbox command */
  4036. rc = lpfc_sli4_post_sgl_list(phba);
  4037. if (unlikely(rc)) {
  4038. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4039. "0582 Error %d during sgl post operation", rc);
  4040. rc = -ENODEV;
  4041. goto out_free_vpd;
  4042. }
  4043. /* Register SCSI SGL pool to the device */
  4044. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  4045. if (unlikely(rc)) {
  4046. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  4047. "0383 Error %d during scsi sgl post opeation",
  4048. rc);
  4049. /* Some Scsi buffers were moved to the abort scsi list */
  4050. /* A pci function reset will repost them */
  4051. rc = -ENODEV;
  4052. goto out_free_vpd;
  4053. }
  4054. /* Post the rpi header region to the device. */
  4055. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  4056. if (unlikely(rc)) {
  4057. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4058. "0393 Error %d during rpi post operation\n",
  4059. rc);
  4060. rc = -ENODEV;
  4061. goto out_free_vpd;
  4062. }
  4063. if (phba->cfg_enable_fip)
  4064. bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
  4065. else
  4066. bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
  4067. /* Set up all the queues to the device */
  4068. rc = lpfc_sli4_queue_setup(phba);
  4069. if (unlikely(rc)) {
  4070. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4071. "0381 Error %d during queue setup.\n ", rc);
  4072. goto out_stop_timers;
  4073. }
  4074. /* Arm the CQs and then EQs on device */
  4075. lpfc_sli4_arm_cqeq_intr(phba);
  4076. /* Indicate device interrupt mode */
  4077. phba->sli4_hba.intr_enable = 1;
  4078. /* Allow asynchronous mailbox command to go through */
  4079. spin_lock_irq(&phba->hbalock);
  4080. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4081. spin_unlock_irq(&phba->hbalock);
  4082. /* Post receive buffers to the device */
  4083. lpfc_sli4_rb_setup(phba);
  4084. /* Start the ELS watchdog timer */
  4085. mod_timer(&vport->els_tmofunc,
  4086. jiffies + HZ * (phba->fc_ratov * 2));
  4087. /* Start heart beat timer */
  4088. mod_timer(&phba->hb_tmofunc,
  4089. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  4090. phba->hb_outstanding = 0;
  4091. phba->last_completion_time = jiffies;
  4092. /* Start error attention (ERATT) polling timer */
  4093. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  4094. /*
  4095. * The port is ready, set the host's link state to LINK_DOWN
  4096. * in preparation for link interrupts.
  4097. */
  4098. lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
  4099. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4100. lpfc_set_loopback_flag(phba);
  4101. /* Change driver state to LPFC_LINK_DOWN right before init link */
  4102. spin_lock_irq(&phba->hbalock);
  4103. phba->link_state = LPFC_LINK_DOWN;
  4104. spin_unlock_irq(&phba->hbalock);
  4105. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  4106. if (unlikely(rc != MBX_NOT_FINISHED)) {
  4107. kfree(vpd);
  4108. return 0;
  4109. } else
  4110. rc = -EIO;
  4111. /* Unset all the queues set up in this routine when error out */
  4112. if (rc)
  4113. lpfc_sli4_queue_unset(phba);
  4114. out_stop_timers:
  4115. if (rc)
  4116. lpfc_stop_hba_timers(phba);
  4117. out_free_vpd:
  4118. kfree(vpd);
  4119. out_free_mbox:
  4120. mempool_free(mboxq, phba->mbox_mem_pool);
  4121. return rc;
  4122. }
  4123. /**
  4124. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  4125. * @ptr: context object - pointer to hba structure.
  4126. *
  4127. * This is the callback function for mailbox timer. The mailbox
  4128. * timer is armed when a new mailbox command is issued and the timer
  4129. * is deleted when the mailbox complete. The function is called by
  4130. * the kernel timer code when a mailbox does not complete within
  4131. * expected time. This function wakes up the worker thread to
  4132. * process the mailbox timeout and returns. All the processing is
  4133. * done by the worker thread function lpfc_mbox_timeout_handler.
  4134. **/
  4135. void
  4136. lpfc_mbox_timeout(unsigned long ptr)
  4137. {
  4138. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  4139. unsigned long iflag;
  4140. uint32_t tmo_posted;
  4141. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  4142. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  4143. if (!tmo_posted)
  4144. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  4145. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  4146. if (!tmo_posted)
  4147. lpfc_worker_wake_up(phba);
  4148. return;
  4149. }
  4150. /**
  4151. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  4152. * @phba: Pointer to HBA context object.
  4153. *
  4154. * This function is called from worker thread when a mailbox command times out.
  4155. * The caller is not required to hold any locks. This function will reset the
  4156. * HBA and recover all the pending commands.
  4157. **/
  4158. void
  4159. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  4160. {
  4161. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  4162. MAILBOX_t *mb = &pmbox->u.mb;
  4163. struct lpfc_sli *psli = &phba->sli;
  4164. struct lpfc_sli_ring *pring;
  4165. /* Check the pmbox pointer first. There is a race condition
  4166. * between the mbox timeout handler getting executed in the
  4167. * worklist and the mailbox actually completing. When this
  4168. * race condition occurs, the mbox_active will be NULL.
  4169. */
  4170. spin_lock_irq(&phba->hbalock);
  4171. if (pmbox == NULL) {
  4172. lpfc_printf_log(phba, KERN_WARNING,
  4173. LOG_MBOX | LOG_SLI,
  4174. "0353 Active Mailbox cleared - mailbox timeout "
  4175. "exiting\n");
  4176. spin_unlock_irq(&phba->hbalock);
  4177. return;
  4178. }
  4179. /* Mbox cmd <mbxCommand> timeout */
  4180. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4181. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  4182. mb->mbxCommand,
  4183. phba->pport->port_state,
  4184. phba->sli.sli_flag,
  4185. phba->sli.mbox_active);
  4186. spin_unlock_irq(&phba->hbalock);
  4187. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  4188. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  4189. * it to fail all oustanding SCSI IO.
  4190. */
  4191. spin_lock_irq(&phba->pport->work_port_lock);
  4192. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  4193. spin_unlock_irq(&phba->pport->work_port_lock);
  4194. spin_lock_irq(&phba->hbalock);
  4195. phba->link_state = LPFC_LINK_UNKNOWN;
  4196. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  4197. spin_unlock_irq(&phba->hbalock);
  4198. pring = &psli->ring[psli->fcp_ring];
  4199. lpfc_sli_abort_iocb_ring(phba, pring);
  4200. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4201. "0345 Resetting board due to mailbox timeout\n");
  4202. /* Reset the HBA device */
  4203. lpfc_reset_hba(phba);
  4204. }
  4205. /**
  4206. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  4207. * @phba: Pointer to HBA context object.
  4208. * @pmbox: Pointer to mailbox object.
  4209. * @flag: Flag indicating how the mailbox need to be processed.
  4210. *
  4211. * This function is called by discovery code and HBA management code
  4212. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  4213. * function gets the hbalock to protect the data structures.
  4214. * The mailbox command can be submitted in polling mode, in which case
  4215. * this function will wait in a polling loop for the completion of the
  4216. * mailbox.
  4217. * If the mailbox is submitted in no_wait mode (not polling) the
  4218. * function will submit the command and returns immediately without waiting
  4219. * for the mailbox completion. The no_wait is supported only when HBA
  4220. * is in SLI2/SLI3 mode - interrupts are enabled.
  4221. * The SLI interface allows only one mailbox pending at a time. If the
  4222. * mailbox is issued in polling mode and there is already a mailbox
  4223. * pending, then the function will return an error. If the mailbox is issued
  4224. * in NO_WAIT mode and there is a mailbox pending already, the function
  4225. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  4226. * The sli layer owns the mailbox object until the completion of mailbox
  4227. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  4228. * return codes the caller owns the mailbox command after the return of
  4229. * the function.
  4230. **/
  4231. static int
  4232. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  4233. uint32_t flag)
  4234. {
  4235. MAILBOX_t *mb;
  4236. struct lpfc_sli *psli = &phba->sli;
  4237. uint32_t status, evtctr;
  4238. uint32_t ha_copy;
  4239. int i;
  4240. unsigned long timeout;
  4241. unsigned long drvr_flag = 0;
  4242. uint32_t word0, ldata;
  4243. void __iomem *to_slim;
  4244. int processing_queue = 0;
  4245. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  4246. if (!pmbox) {
  4247. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4248. /* processing mbox queue from intr_handler */
  4249. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4250. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4251. return MBX_SUCCESS;
  4252. }
  4253. processing_queue = 1;
  4254. pmbox = lpfc_mbox_get(phba);
  4255. if (!pmbox) {
  4256. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4257. return MBX_SUCCESS;
  4258. }
  4259. }
  4260. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  4261. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  4262. if(!pmbox->vport) {
  4263. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4264. lpfc_printf_log(phba, KERN_ERR,
  4265. LOG_MBOX | LOG_VPORT,
  4266. "1806 Mbox x%x failed. No vport\n",
  4267. pmbox->u.mb.mbxCommand);
  4268. dump_stack();
  4269. goto out_not_finished;
  4270. }
  4271. }
  4272. /* If the PCI channel is in offline state, do not post mbox. */
  4273. if (unlikely(pci_channel_offline(phba->pcidev))) {
  4274. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4275. goto out_not_finished;
  4276. }
  4277. /* If HBA has a deferred error attention, fail the iocb. */
  4278. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  4279. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4280. goto out_not_finished;
  4281. }
  4282. psli = &phba->sli;
  4283. mb = &pmbox->u.mb;
  4284. status = MBX_SUCCESS;
  4285. if (phba->link_state == LPFC_HBA_ERROR) {
  4286. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4287. /* Mbox command <mbxCommand> cannot issue */
  4288. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4289. "(%d):0311 Mailbox command x%x cannot "
  4290. "issue Data: x%x x%x\n",
  4291. pmbox->vport ? pmbox->vport->vpi : 0,
  4292. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  4293. goto out_not_finished;
  4294. }
  4295. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  4296. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  4297. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4298. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4299. "(%d):2528 Mailbox command x%x cannot "
  4300. "issue Data: x%x x%x\n",
  4301. pmbox->vport ? pmbox->vport->vpi : 0,
  4302. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  4303. goto out_not_finished;
  4304. }
  4305. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4306. /* Polling for a mbox command when another one is already active
  4307. * is not allowed in SLI. Also, the driver must have established
  4308. * SLI2 mode to queue and process multiple mbox commands.
  4309. */
  4310. if (flag & MBX_POLL) {
  4311. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4312. /* Mbox command <mbxCommand> cannot issue */
  4313. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4314. "(%d):2529 Mailbox command x%x "
  4315. "cannot issue Data: x%x x%x\n",
  4316. pmbox->vport ? pmbox->vport->vpi : 0,
  4317. pmbox->u.mb.mbxCommand,
  4318. psli->sli_flag, flag);
  4319. goto out_not_finished;
  4320. }
  4321. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  4322. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4323. /* Mbox command <mbxCommand> cannot issue */
  4324. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4325. "(%d):2530 Mailbox command x%x "
  4326. "cannot issue Data: x%x x%x\n",
  4327. pmbox->vport ? pmbox->vport->vpi : 0,
  4328. pmbox->u.mb.mbxCommand,
  4329. psli->sli_flag, flag);
  4330. goto out_not_finished;
  4331. }
  4332. /* Another mailbox command is still being processed, queue this
  4333. * command to be processed later.
  4334. */
  4335. lpfc_mbox_put(phba, pmbox);
  4336. /* Mbox cmd issue - BUSY */
  4337. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4338. "(%d):0308 Mbox cmd issue - BUSY Data: "
  4339. "x%x x%x x%x x%x\n",
  4340. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  4341. mb->mbxCommand, phba->pport->port_state,
  4342. psli->sli_flag, flag);
  4343. psli->slistat.mbox_busy++;
  4344. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4345. if (pmbox->vport) {
  4346. lpfc_debugfs_disc_trc(pmbox->vport,
  4347. LPFC_DISC_TRC_MBOX_VPORT,
  4348. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  4349. (uint32_t)mb->mbxCommand,
  4350. mb->un.varWords[0], mb->un.varWords[1]);
  4351. }
  4352. else {
  4353. lpfc_debugfs_disc_trc(phba->pport,
  4354. LPFC_DISC_TRC_MBOX,
  4355. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  4356. (uint32_t)mb->mbxCommand,
  4357. mb->un.varWords[0], mb->un.varWords[1]);
  4358. }
  4359. return MBX_BUSY;
  4360. }
  4361. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4362. /* If we are not polling, we MUST be in SLI2 mode */
  4363. if (flag != MBX_POLL) {
  4364. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  4365. (mb->mbxCommand != MBX_KILL_BOARD)) {
  4366. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4367. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4368. /* Mbox command <mbxCommand> cannot issue */
  4369. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4370. "(%d):2531 Mailbox command x%x "
  4371. "cannot issue Data: x%x x%x\n",
  4372. pmbox->vport ? pmbox->vport->vpi : 0,
  4373. pmbox->u.mb.mbxCommand,
  4374. psli->sli_flag, flag);
  4375. goto out_not_finished;
  4376. }
  4377. /* timeout active mbox command */
  4378. mod_timer(&psli->mbox_tmo, (jiffies +
  4379. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  4380. }
  4381. /* Mailbox cmd <cmd> issue */
  4382. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4383. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  4384. "x%x\n",
  4385. pmbox->vport ? pmbox->vport->vpi : 0,
  4386. mb->mbxCommand, phba->pport->port_state,
  4387. psli->sli_flag, flag);
  4388. if (mb->mbxCommand != MBX_HEARTBEAT) {
  4389. if (pmbox->vport) {
  4390. lpfc_debugfs_disc_trc(pmbox->vport,
  4391. LPFC_DISC_TRC_MBOX_VPORT,
  4392. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  4393. (uint32_t)mb->mbxCommand,
  4394. mb->un.varWords[0], mb->un.varWords[1]);
  4395. }
  4396. else {
  4397. lpfc_debugfs_disc_trc(phba->pport,
  4398. LPFC_DISC_TRC_MBOX,
  4399. "MBOX Send: cmd:x%x mb:x%x x%x",
  4400. (uint32_t)mb->mbxCommand,
  4401. mb->un.varWords[0], mb->un.varWords[1]);
  4402. }
  4403. }
  4404. psli->slistat.mbox_cmd++;
  4405. evtctr = psli->slistat.mbox_event;
  4406. /* next set own bit for the adapter and copy over command word */
  4407. mb->mbxOwner = OWN_CHIP;
  4408. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4409. /* First copy command data to host SLIM area */
  4410. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  4411. } else {
  4412. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4413. /* copy command data into host mbox for cmpl */
  4414. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  4415. }
  4416. /* First copy mbox command data to HBA SLIM, skip past first
  4417. word */
  4418. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  4419. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  4420. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  4421. /* Next copy over first word, with mbxOwner set */
  4422. ldata = *((uint32_t *)mb);
  4423. to_slim = phba->MBslimaddr;
  4424. writel(ldata, to_slim);
  4425. readl(to_slim); /* flush */
  4426. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4427. /* switch over to host mailbox */
  4428. psli->sli_flag |= LPFC_SLI_ACTIVE;
  4429. }
  4430. }
  4431. wmb();
  4432. switch (flag) {
  4433. case MBX_NOWAIT:
  4434. /* Set up reference to mailbox command */
  4435. psli->mbox_active = pmbox;
  4436. /* Interrupt board to do it */
  4437. writel(CA_MBATT, phba->CAregaddr);
  4438. readl(phba->CAregaddr); /* flush */
  4439. /* Don't wait for it to finish, just return */
  4440. break;
  4441. case MBX_POLL:
  4442. /* Set up null reference to mailbox command */
  4443. psli->mbox_active = NULL;
  4444. /* Interrupt board to do it */
  4445. writel(CA_MBATT, phba->CAregaddr);
  4446. readl(phba->CAregaddr); /* flush */
  4447. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4448. /* First read mbox status word */
  4449. word0 = *((uint32_t *)phba->mbox);
  4450. word0 = le32_to_cpu(word0);
  4451. } else {
  4452. /* First read mbox status word */
  4453. word0 = readl(phba->MBslimaddr);
  4454. }
  4455. /* Read the HBA Host Attention Register */
  4456. ha_copy = readl(phba->HAregaddr);
  4457. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  4458. mb->mbxCommand) *
  4459. 1000) + jiffies;
  4460. i = 0;
  4461. /* Wait for command to complete */
  4462. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  4463. (!(ha_copy & HA_MBATT) &&
  4464. (phba->link_state > LPFC_WARM_START))) {
  4465. if (time_after(jiffies, timeout)) {
  4466. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4467. spin_unlock_irqrestore(&phba->hbalock,
  4468. drvr_flag);
  4469. goto out_not_finished;
  4470. }
  4471. /* Check if we took a mbox interrupt while we were
  4472. polling */
  4473. if (((word0 & OWN_CHIP) != OWN_CHIP)
  4474. && (evtctr != psli->slistat.mbox_event))
  4475. break;
  4476. if (i++ > 10) {
  4477. spin_unlock_irqrestore(&phba->hbalock,
  4478. drvr_flag);
  4479. msleep(1);
  4480. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  4481. }
  4482. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4483. /* First copy command data */
  4484. word0 = *((uint32_t *)phba->mbox);
  4485. word0 = le32_to_cpu(word0);
  4486. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  4487. MAILBOX_t *slimmb;
  4488. uint32_t slimword0;
  4489. /* Check real SLIM for any errors */
  4490. slimword0 = readl(phba->MBslimaddr);
  4491. slimmb = (MAILBOX_t *) & slimword0;
  4492. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  4493. && slimmb->mbxStatus) {
  4494. psli->sli_flag &=
  4495. ~LPFC_SLI_ACTIVE;
  4496. word0 = slimword0;
  4497. }
  4498. }
  4499. } else {
  4500. /* First copy command data */
  4501. word0 = readl(phba->MBslimaddr);
  4502. }
  4503. /* Read the HBA Host Attention Register */
  4504. ha_copy = readl(phba->HAregaddr);
  4505. }
  4506. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  4507. /* copy results back to user */
  4508. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  4509. } else {
  4510. /* First copy command data */
  4511. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  4512. MAILBOX_CMD_SIZE);
  4513. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  4514. pmbox->context2) {
  4515. lpfc_memcpy_from_slim((void *)pmbox->context2,
  4516. phba->MBslimaddr + DMP_RSP_OFFSET,
  4517. mb->un.varDmp.word_cnt);
  4518. }
  4519. }
  4520. writel(HA_MBATT, phba->HAregaddr);
  4521. readl(phba->HAregaddr); /* flush */
  4522. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4523. status = mb->mbxStatus;
  4524. }
  4525. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  4526. return status;
  4527. out_not_finished:
  4528. if (processing_queue) {
  4529. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  4530. lpfc_mbox_cmpl_put(phba, pmbox);
  4531. }
  4532. return MBX_NOT_FINISHED;
  4533. }
  4534. /**
  4535. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  4536. * @phba: Pointer to HBA context object.
  4537. *
  4538. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  4539. * the driver internal pending mailbox queue. It will then try to wait out the
  4540. * possible outstanding mailbox command before return.
  4541. *
  4542. * Returns:
  4543. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  4544. * the outstanding mailbox command timed out.
  4545. **/
  4546. static int
  4547. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  4548. {
  4549. struct lpfc_sli *psli = &phba->sli;
  4550. uint8_t actcmd = MBX_HEARTBEAT;
  4551. int rc = 0;
  4552. unsigned long timeout;
  4553. /* Mark the asynchronous mailbox command posting as blocked */
  4554. spin_lock_irq(&phba->hbalock);
  4555. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  4556. if (phba->sli.mbox_active)
  4557. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  4558. spin_unlock_irq(&phba->hbalock);
  4559. /* Determine how long we might wait for the active mailbox
  4560. * command to be gracefully completed by firmware.
  4561. */
  4562. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
  4563. jiffies;
  4564. /* Wait for the outstnading mailbox command to complete */
  4565. while (phba->sli.mbox_active) {
  4566. /* Check active mailbox complete status every 2ms */
  4567. msleep(2);
  4568. if (time_after(jiffies, timeout)) {
  4569. /* Timeout, marked the outstanding cmd not complete */
  4570. rc = 1;
  4571. break;
  4572. }
  4573. }
  4574. /* Can not cleanly block async mailbox command, fails it */
  4575. if (rc) {
  4576. spin_lock_irq(&phba->hbalock);
  4577. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4578. spin_unlock_irq(&phba->hbalock);
  4579. }
  4580. return rc;
  4581. }
  4582. /**
  4583. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  4584. * @phba: Pointer to HBA context object.
  4585. *
  4586. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  4587. * commands from the driver internal pending mailbox queue. It makes sure
  4588. * that there is no outstanding mailbox command before resuming posting
  4589. * asynchronous mailbox commands. If, for any reason, there is outstanding
  4590. * mailbox command, it will try to wait it out before resuming asynchronous
  4591. * mailbox command posting.
  4592. **/
  4593. static void
  4594. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  4595. {
  4596. struct lpfc_sli *psli = &phba->sli;
  4597. spin_lock_irq(&phba->hbalock);
  4598. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4599. /* Asynchronous mailbox posting is not blocked, do nothing */
  4600. spin_unlock_irq(&phba->hbalock);
  4601. return;
  4602. }
  4603. /* Outstanding synchronous mailbox command is guaranteed to be done,
  4604. * successful or timeout, after timing-out the outstanding mailbox
  4605. * command shall always be removed, so just unblock posting async
  4606. * mailbox command and resume
  4607. */
  4608. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4609. spin_unlock_irq(&phba->hbalock);
  4610. /* wake up worker thread to post asynchronlous mailbox command */
  4611. lpfc_worker_wake_up(phba);
  4612. }
  4613. /**
  4614. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  4615. * @phba: Pointer to HBA context object.
  4616. * @mboxq: Pointer to mailbox object.
  4617. *
  4618. * The function posts a mailbox to the port. The mailbox is expected
  4619. * to be comletely filled in and ready for the port to operate on it.
  4620. * This routine executes a synchronous completion operation on the
  4621. * mailbox by polling for its completion.
  4622. *
  4623. * The caller must not be holding any locks when calling this routine.
  4624. *
  4625. * Returns:
  4626. * MBX_SUCCESS - mailbox posted successfully
  4627. * Any of the MBX error values.
  4628. **/
  4629. static int
  4630. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4631. {
  4632. int rc = MBX_SUCCESS;
  4633. unsigned long iflag;
  4634. uint32_t db_ready;
  4635. uint32_t mcqe_status;
  4636. uint32_t mbx_cmnd;
  4637. unsigned long timeout;
  4638. struct lpfc_sli *psli = &phba->sli;
  4639. struct lpfc_mqe *mb = &mboxq->u.mqe;
  4640. struct lpfc_bmbx_create *mbox_rgn;
  4641. struct dma_address *dma_address;
  4642. struct lpfc_register bmbx_reg;
  4643. /*
  4644. * Only one mailbox can be active to the bootstrap mailbox region
  4645. * at a time and there is no queueing provided.
  4646. */
  4647. spin_lock_irqsave(&phba->hbalock, iflag);
  4648. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4649. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4650. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4651. "(%d):2532 Mailbox command x%x (x%x) "
  4652. "cannot issue Data: x%x x%x\n",
  4653. mboxq->vport ? mboxq->vport->vpi : 0,
  4654. mboxq->u.mb.mbxCommand,
  4655. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4656. psli->sli_flag, MBX_POLL);
  4657. return MBXERR_ERROR;
  4658. }
  4659. /* The server grabs the token and owns it until release */
  4660. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4661. phba->sli.mbox_active = mboxq;
  4662. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4663. /*
  4664. * Initialize the bootstrap memory region to avoid stale data areas
  4665. * in the mailbox post. Then copy the caller's mailbox contents to
  4666. * the bmbx mailbox region.
  4667. */
  4668. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  4669. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  4670. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  4671. sizeof(struct lpfc_mqe));
  4672. /* Post the high mailbox dma address to the port and wait for ready. */
  4673. dma_address = &phba->sli4_hba.bmbx.dma_address;
  4674. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  4675. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  4676. * 1000) + jiffies;
  4677. do {
  4678. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  4679. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  4680. if (!db_ready)
  4681. msleep(2);
  4682. if (time_after(jiffies, timeout)) {
  4683. rc = MBXERR_ERROR;
  4684. goto exit;
  4685. }
  4686. } while (!db_ready);
  4687. /* Post the low mailbox dma address to the port. */
  4688. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  4689. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
  4690. * 1000) + jiffies;
  4691. do {
  4692. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  4693. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  4694. if (!db_ready)
  4695. msleep(2);
  4696. if (time_after(jiffies, timeout)) {
  4697. rc = MBXERR_ERROR;
  4698. goto exit;
  4699. }
  4700. } while (!db_ready);
  4701. /*
  4702. * Read the CQ to ensure the mailbox has completed.
  4703. * If so, update the mailbox status so that the upper layers
  4704. * can complete the request normally.
  4705. */
  4706. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  4707. sizeof(struct lpfc_mqe));
  4708. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  4709. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  4710. sizeof(struct lpfc_mcqe));
  4711. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  4712. /* Prefix the mailbox status with range x4000 to note SLI4 status. */
  4713. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  4714. bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
  4715. rc = MBXERR_ERROR;
  4716. }
  4717. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4718. "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
  4719. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  4720. " x%x x%x CQ: x%x x%x x%x x%x\n",
  4721. mboxq->vport ? mboxq->vport->vpi : 0,
  4722. mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4723. bf_get(lpfc_mqe_status, mb),
  4724. mb->un.mb_words[0], mb->un.mb_words[1],
  4725. mb->un.mb_words[2], mb->un.mb_words[3],
  4726. mb->un.mb_words[4], mb->un.mb_words[5],
  4727. mb->un.mb_words[6], mb->un.mb_words[7],
  4728. mb->un.mb_words[8], mb->un.mb_words[9],
  4729. mb->un.mb_words[10], mb->un.mb_words[11],
  4730. mb->un.mb_words[12], mboxq->mcqe.word0,
  4731. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4732. mboxq->mcqe.trailer);
  4733. exit:
  4734. /* We are holding the token, no needed for lock when release */
  4735. spin_lock_irqsave(&phba->hbalock, iflag);
  4736. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4737. phba->sli.mbox_active = NULL;
  4738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4739. return rc;
  4740. }
  4741. /**
  4742. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  4743. * @phba: Pointer to HBA context object.
  4744. * @pmbox: Pointer to mailbox object.
  4745. * @flag: Flag indicating how the mailbox need to be processed.
  4746. *
  4747. * This function is called by discovery code and HBA management code to submit
  4748. * a mailbox command to firmware with SLI-4 interface spec.
  4749. *
  4750. * Return codes the caller owns the mailbox command after the return of the
  4751. * function.
  4752. **/
  4753. static int
  4754. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4755. uint32_t flag)
  4756. {
  4757. struct lpfc_sli *psli = &phba->sli;
  4758. unsigned long iflags;
  4759. int rc;
  4760. rc = lpfc_mbox_dev_check(phba);
  4761. if (unlikely(rc)) {
  4762. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4763. "(%d):2544 Mailbox command x%x (x%x) "
  4764. "cannot issue Data: x%x x%x\n",
  4765. mboxq->vport ? mboxq->vport->vpi : 0,
  4766. mboxq->u.mb.mbxCommand,
  4767. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4768. psli->sli_flag, flag);
  4769. goto out_not_finished;
  4770. }
  4771. /* Detect polling mode and jump to a handler */
  4772. if (!phba->sli4_hba.intr_enable) {
  4773. if (flag == MBX_POLL)
  4774. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  4775. else
  4776. rc = -EIO;
  4777. if (rc != MBX_SUCCESS)
  4778. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4779. "(%d):2541 Mailbox command x%x "
  4780. "(x%x) cannot issue Data: x%x x%x\n",
  4781. mboxq->vport ? mboxq->vport->vpi : 0,
  4782. mboxq->u.mb.mbxCommand,
  4783. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4784. psli->sli_flag, flag);
  4785. return rc;
  4786. } else if (flag == MBX_POLL) {
  4787. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  4788. "(%d):2542 Try to issue mailbox command "
  4789. "x%x (x%x) synchronously ahead of async"
  4790. "mailbox command queue: x%x x%x\n",
  4791. mboxq->vport ? mboxq->vport->vpi : 0,
  4792. mboxq->u.mb.mbxCommand,
  4793. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4794. psli->sli_flag, flag);
  4795. /* Try to block the asynchronous mailbox posting */
  4796. rc = lpfc_sli4_async_mbox_block(phba);
  4797. if (!rc) {
  4798. /* Successfully blocked, now issue sync mbox cmd */
  4799. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  4800. if (rc != MBX_SUCCESS)
  4801. lpfc_printf_log(phba, KERN_ERR,
  4802. LOG_MBOX | LOG_SLI,
  4803. "(%d):2597 Mailbox command "
  4804. "x%x (x%x) cannot issue "
  4805. "Data: x%x x%x\n",
  4806. mboxq->vport ?
  4807. mboxq->vport->vpi : 0,
  4808. mboxq->u.mb.mbxCommand,
  4809. lpfc_sli4_mbox_opcode_get(phba,
  4810. mboxq),
  4811. psli->sli_flag, flag);
  4812. /* Unblock the async mailbox posting afterward */
  4813. lpfc_sli4_async_mbox_unblock(phba);
  4814. }
  4815. return rc;
  4816. }
  4817. /* Now, interrupt mode asynchrous mailbox command */
  4818. rc = lpfc_mbox_cmd_check(phba, mboxq);
  4819. if (rc) {
  4820. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4821. "(%d):2543 Mailbox command x%x (x%x) "
  4822. "cannot issue Data: x%x x%x\n",
  4823. mboxq->vport ? mboxq->vport->vpi : 0,
  4824. mboxq->u.mb.mbxCommand,
  4825. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4826. psli->sli_flag, flag);
  4827. goto out_not_finished;
  4828. }
  4829. /* Put the mailbox command to the driver internal FIFO */
  4830. psli->slistat.mbox_busy++;
  4831. spin_lock_irqsave(&phba->hbalock, iflags);
  4832. lpfc_mbox_put(phba, mboxq);
  4833. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4834. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4835. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  4836. "x%x (x%x) x%x x%x x%x\n",
  4837. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  4838. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4839. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4840. phba->pport->port_state,
  4841. psli->sli_flag, MBX_NOWAIT);
  4842. /* Wake up worker thread to transport mailbox command from head */
  4843. lpfc_worker_wake_up(phba);
  4844. return MBX_BUSY;
  4845. out_not_finished:
  4846. return MBX_NOT_FINISHED;
  4847. }
  4848. /**
  4849. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  4850. * @phba: Pointer to HBA context object.
  4851. *
  4852. * This function is called by worker thread to send a mailbox command to
  4853. * SLI4 HBA firmware.
  4854. *
  4855. **/
  4856. int
  4857. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  4858. {
  4859. struct lpfc_sli *psli = &phba->sli;
  4860. LPFC_MBOXQ_t *mboxq;
  4861. int rc = MBX_SUCCESS;
  4862. unsigned long iflags;
  4863. struct lpfc_mqe *mqe;
  4864. uint32_t mbx_cmnd;
  4865. /* Check interrupt mode before post async mailbox command */
  4866. if (unlikely(!phba->sli4_hba.intr_enable))
  4867. return MBX_NOT_FINISHED;
  4868. /* Check for mailbox command service token */
  4869. spin_lock_irqsave(&phba->hbalock, iflags);
  4870. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  4871. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4872. return MBX_NOT_FINISHED;
  4873. }
  4874. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  4875. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4876. return MBX_NOT_FINISHED;
  4877. }
  4878. if (unlikely(phba->sli.mbox_active)) {
  4879. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4880. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4881. "0384 There is pending active mailbox cmd\n");
  4882. return MBX_NOT_FINISHED;
  4883. }
  4884. /* Take the mailbox command service token */
  4885. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4886. /* Get the next mailbox command from head of queue */
  4887. mboxq = lpfc_mbox_get(phba);
  4888. /* If no more mailbox command waiting for post, we're done */
  4889. if (!mboxq) {
  4890. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4891. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4892. return MBX_SUCCESS;
  4893. }
  4894. phba->sli.mbox_active = mboxq;
  4895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4896. /* Check device readiness for posting mailbox command */
  4897. rc = lpfc_mbox_dev_check(phba);
  4898. if (unlikely(rc))
  4899. /* Driver clean routine will clean up pending mailbox */
  4900. goto out_not_finished;
  4901. /* Prepare the mbox command to be posted */
  4902. mqe = &mboxq->u.mqe;
  4903. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  4904. /* Start timer for the mbox_tmo and log some mailbox post messages */
  4905. mod_timer(&psli->mbox_tmo, (jiffies +
  4906. (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
  4907. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4908. "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
  4909. "x%x x%x\n",
  4910. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  4911. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4912. phba->pport->port_state, psli->sli_flag);
  4913. if (mbx_cmnd != MBX_HEARTBEAT) {
  4914. if (mboxq->vport) {
  4915. lpfc_debugfs_disc_trc(mboxq->vport,
  4916. LPFC_DISC_TRC_MBOX_VPORT,
  4917. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  4918. mbx_cmnd, mqe->un.mb_words[0],
  4919. mqe->un.mb_words[1]);
  4920. } else {
  4921. lpfc_debugfs_disc_trc(phba->pport,
  4922. LPFC_DISC_TRC_MBOX,
  4923. "MBOX Send: cmd:x%x mb:x%x x%x",
  4924. mbx_cmnd, mqe->un.mb_words[0],
  4925. mqe->un.mb_words[1]);
  4926. }
  4927. }
  4928. psli->slistat.mbox_cmd++;
  4929. /* Post the mailbox command to the port */
  4930. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  4931. if (rc != MBX_SUCCESS) {
  4932. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  4933. "(%d):2533 Mailbox command x%x (x%x) "
  4934. "cannot issue Data: x%x x%x\n",
  4935. mboxq->vport ? mboxq->vport->vpi : 0,
  4936. mboxq->u.mb.mbxCommand,
  4937. lpfc_sli4_mbox_opcode_get(phba, mboxq),
  4938. psli->sli_flag, MBX_NOWAIT);
  4939. goto out_not_finished;
  4940. }
  4941. return rc;
  4942. out_not_finished:
  4943. spin_lock_irqsave(&phba->hbalock, iflags);
  4944. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  4945. __lpfc_mbox_cmpl_put(phba, mboxq);
  4946. /* Release the token */
  4947. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4948. phba->sli.mbox_active = NULL;
  4949. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4950. return MBX_NOT_FINISHED;
  4951. }
  4952. /**
  4953. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  4954. * @phba: Pointer to HBA context object.
  4955. * @pmbox: Pointer to mailbox object.
  4956. * @flag: Flag indicating how the mailbox need to be processed.
  4957. *
  4958. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  4959. * the API jump table function pointer from the lpfc_hba struct.
  4960. *
  4961. * Return codes the caller owns the mailbox command after the return of the
  4962. * function.
  4963. **/
  4964. int
  4965. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  4966. {
  4967. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  4968. }
  4969. /**
  4970. * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
  4971. * @phba: The hba struct for which this call is being executed.
  4972. * @dev_grp: The HBA PCI-Device group number.
  4973. *
  4974. * This routine sets up the mbox interface API function jump table in @phba
  4975. * struct.
  4976. * Returns: 0 - success, -ENODEV - failure.
  4977. **/
  4978. int
  4979. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  4980. {
  4981. switch (dev_grp) {
  4982. case LPFC_PCI_DEV_LP:
  4983. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  4984. phba->lpfc_sli_handle_slow_ring_event =
  4985. lpfc_sli_handle_slow_ring_event_s3;
  4986. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  4987. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  4988. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  4989. break;
  4990. case LPFC_PCI_DEV_OC:
  4991. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  4992. phba->lpfc_sli_handle_slow_ring_event =
  4993. lpfc_sli_handle_slow_ring_event_s4;
  4994. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  4995. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  4996. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  4997. break;
  4998. default:
  4999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5000. "1420 Invalid HBA PCI-device group: 0x%x\n",
  5001. dev_grp);
  5002. return -ENODEV;
  5003. break;
  5004. }
  5005. return 0;
  5006. }
  5007. /**
  5008. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  5009. * @phba: Pointer to HBA context object.
  5010. * @pring: Pointer to driver SLI ring object.
  5011. * @piocb: Pointer to address of newly added command iocb.
  5012. *
  5013. * This function is called with hbalock held to add a command
  5014. * iocb to the txq when SLI layer cannot submit the command iocb
  5015. * to the ring.
  5016. **/
  5017. static void
  5018. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  5019. struct lpfc_iocbq *piocb)
  5020. {
  5021. /* Insert the caller's iocb in the txq tail for later processing. */
  5022. list_add_tail(&piocb->list, &pring->txq);
  5023. pring->txq_cnt++;
  5024. }
  5025. /**
  5026. * lpfc_sli_next_iocb - Get the next iocb in the txq
  5027. * @phba: Pointer to HBA context object.
  5028. * @pring: Pointer to driver SLI ring object.
  5029. * @piocb: Pointer to address of newly added command iocb.
  5030. *
  5031. * This function is called with hbalock held before a new
  5032. * iocb is submitted to the firmware. This function checks
  5033. * txq to flush the iocbs in txq to Firmware before
  5034. * submitting new iocbs to the Firmware.
  5035. * If there are iocbs in the txq which need to be submitted
  5036. * to firmware, lpfc_sli_next_iocb returns the first element
  5037. * of the txq after dequeuing it from txq.
  5038. * If there is no iocb in the txq then the function will return
  5039. * *piocb and *piocb is set to NULL. Caller needs to check
  5040. * *piocb to find if there are more commands in the txq.
  5041. **/
  5042. static struct lpfc_iocbq *
  5043. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  5044. struct lpfc_iocbq **piocb)
  5045. {
  5046. struct lpfc_iocbq * nextiocb;
  5047. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  5048. if (!nextiocb) {
  5049. nextiocb = *piocb;
  5050. *piocb = NULL;
  5051. }
  5052. return nextiocb;
  5053. }
  5054. /**
  5055. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  5056. * @phba: Pointer to HBA context object.
  5057. * @ring_number: SLI ring number to issue iocb on.
  5058. * @piocb: Pointer to command iocb.
  5059. * @flag: Flag indicating if this command can be put into txq.
  5060. *
  5061. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  5062. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  5063. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  5064. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  5065. * this function allows only iocbs for posting buffers. This function finds
  5066. * next available slot in the command ring and posts the command to the
  5067. * available slot and writes the port attention register to request HBA start
  5068. * processing new iocb. If there is no slot available in the ring and
  5069. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  5070. * the function returns IOCB_BUSY.
  5071. *
  5072. * This function is called with hbalock held. The function will return success
  5073. * after it successfully submit the iocb to firmware or after adding to the
  5074. * txq.
  5075. **/
  5076. static int
  5077. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  5078. struct lpfc_iocbq *piocb, uint32_t flag)
  5079. {
  5080. struct lpfc_iocbq *nextiocb;
  5081. IOCB_t *iocb;
  5082. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  5083. if (piocb->iocb_cmpl && (!piocb->vport) &&
  5084. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  5085. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  5086. lpfc_printf_log(phba, KERN_ERR,
  5087. LOG_SLI | LOG_VPORT,
  5088. "1807 IOCB x%x failed. No vport\n",
  5089. piocb->iocb.ulpCommand);
  5090. dump_stack();
  5091. return IOCB_ERROR;
  5092. }
  5093. /* If the PCI channel is in offline state, do not post iocbs. */
  5094. if (unlikely(pci_channel_offline(phba->pcidev)))
  5095. return IOCB_ERROR;
  5096. /* If HBA has a deferred error attention, fail the iocb. */
  5097. if (unlikely(phba->hba_flag & DEFER_ERATT))
  5098. return IOCB_ERROR;
  5099. /*
  5100. * We should never get an IOCB if we are in a < LINK_DOWN state
  5101. */
  5102. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  5103. return IOCB_ERROR;
  5104. /*
  5105. * Check to see if we are blocking IOCB processing because of a
  5106. * outstanding event.
  5107. */
  5108. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  5109. goto iocb_busy;
  5110. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  5111. /*
  5112. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  5113. * can be issued if the link is not up.
  5114. */
  5115. switch (piocb->iocb.ulpCommand) {
  5116. case CMD_GEN_REQUEST64_CR:
  5117. case CMD_GEN_REQUEST64_CX:
  5118. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  5119. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  5120. FC_FCP_CMND) ||
  5121. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  5122. MENLO_TRANSPORT_TYPE))
  5123. goto iocb_busy;
  5124. break;
  5125. case CMD_QUE_RING_BUF_CN:
  5126. case CMD_QUE_RING_BUF64_CN:
  5127. /*
  5128. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  5129. * completion, iocb_cmpl MUST be 0.
  5130. */
  5131. if (piocb->iocb_cmpl)
  5132. piocb->iocb_cmpl = NULL;
  5133. /*FALLTHROUGH*/
  5134. case CMD_CREATE_XRI_CR:
  5135. case CMD_CLOSE_XRI_CN:
  5136. case CMD_CLOSE_XRI_CX:
  5137. break;
  5138. default:
  5139. goto iocb_busy;
  5140. }
  5141. /*
  5142. * For FCP commands, we must be in a state where we can process link
  5143. * attention events.
  5144. */
  5145. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  5146. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  5147. goto iocb_busy;
  5148. }
  5149. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  5150. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  5151. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  5152. if (iocb)
  5153. lpfc_sli_update_ring(phba, pring);
  5154. else
  5155. lpfc_sli_update_full_ring(phba, pring);
  5156. if (!piocb)
  5157. return IOCB_SUCCESS;
  5158. goto out_busy;
  5159. iocb_busy:
  5160. pring->stats.iocb_cmd_delay++;
  5161. out_busy:
  5162. if (!(flag & SLI_IOCB_RET_IOCB)) {
  5163. __lpfc_sli_ringtx_put(phba, pring, piocb);
  5164. return IOCB_SUCCESS;
  5165. }
  5166. return IOCB_BUSY;
  5167. }
  5168. /**
  5169. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  5170. * @phba: Pointer to HBA context object.
  5171. * @piocb: Pointer to command iocb.
  5172. * @sglq: Pointer to the scatter gather queue object.
  5173. *
  5174. * This routine converts the bpl or bde that is in the IOCB
  5175. * to a sgl list for the sli4 hardware. The physical address
  5176. * of the bpl/bde is converted back to a virtual address.
  5177. * If the IOCB contains a BPL then the list of BDE's is
  5178. * converted to sli4_sge's. If the IOCB contains a single
  5179. * BDE then it is converted to a single sli_sge.
  5180. * The IOCB is still in cpu endianess so the contents of
  5181. * the bpl can be used without byte swapping.
  5182. *
  5183. * Returns valid XRI = Success, NO_XRI = Failure.
  5184. **/
  5185. static uint16_t
  5186. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  5187. struct lpfc_sglq *sglq)
  5188. {
  5189. uint16_t xritag = NO_XRI;
  5190. struct ulp_bde64 *bpl = NULL;
  5191. struct ulp_bde64 bde;
  5192. struct sli4_sge *sgl = NULL;
  5193. IOCB_t *icmd;
  5194. int numBdes = 0;
  5195. int i = 0;
  5196. if (!piocbq || !sglq)
  5197. return xritag;
  5198. sgl = (struct sli4_sge *)sglq->sgl;
  5199. icmd = &piocbq->iocb;
  5200. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  5201. numBdes = icmd->un.genreq64.bdl.bdeSize /
  5202. sizeof(struct ulp_bde64);
  5203. /* The addrHigh and addrLow fields within the IOCB
  5204. * have not been byteswapped yet so there is no
  5205. * need to swap them back.
  5206. */
  5207. bpl = (struct ulp_bde64 *)
  5208. ((struct lpfc_dmabuf *)piocbq->context3)->virt;
  5209. if (!bpl)
  5210. return xritag;
  5211. for (i = 0; i < numBdes; i++) {
  5212. /* Should already be byte swapped. */
  5213. sgl->addr_hi = bpl->addrHigh;
  5214. sgl->addr_lo = bpl->addrLow;
  5215. /* swap the size field back to the cpu so we
  5216. * can assign it to the sgl.
  5217. */
  5218. bde.tus.w = le32_to_cpu(bpl->tus.w);
  5219. bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
  5220. if ((i+1) == numBdes)
  5221. bf_set(lpfc_sli4_sge_last, sgl, 1);
  5222. else
  5223. bf_set(lpfc_sli4_sge_last, sgl, 0);
  5224. sgl->word2 = cpu_to_le32(sgl->word2);
  5225. sgl->word3 = cpu_to_le32(sgl->word3);
  5226. bpl++;
  5227. sgl++;
  5228. }
  5229. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  5230. /* The addrHigh and addrLow fields of the BDE have not
  5231. * been byteswapped yet so they need to be swapped
  5232. * before putting them in the sgl.
  5233. */
  5234. sgl->addr_hi =
  5235. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  5236. sgl->addr_lo =
  5237. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  5238. bf_set(lpfc_sli4_sge_len, sgl,
  5239. icmd->un.genreq64.bdl.bdeSize);
  5240. bf_set(lpfc_sli4_sge_last, sgl, 1);
  5241. sgl->word2 = cpu_to_le32(sgl->word2);
  5242. sgl->word3 = cpu_to_le32(sgl->word3);
  5243. }
  5244. return sglq->sli4_xritag;
  5245. }
  5246. /**
  5247. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  5248. * @phba: Pointer to HBA context object.
  5249. *
  5250. * This routine performs a round robin SCSI command to SLI4 FCP WQ index
  5251. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  5252. * held.
  5253. *
  5254. * Return: index into SLI4 fast-path FCP queue index.
  5255. **/
  5256. static uint32_t
  5257. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  5258. {
  5259. ++phba->fcp_qidx;
  5260. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  5261. phba->fcp_qidx = 0;
  5262. return phba->fcp_qidx;
  5263. }
  5264. /**
  5265. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  5266. * @phba: Pointer to HBA context object.
  5267. * @piocb: Pointer to command iocb.
  5268. * @wqe: Pointer to the work queue entry.
  5269. *
  5270. * This routine converts the iocb command to its Work Queue Entry
  5271. * equivalent. The wqe pointer should not have any fields set when
  5272. * this routine is called because it will memcpy over them.
  5273. * This routine does not set the CQ_ID or the WQEC bits in the
  5274. * wqe.
  5275. *
  5276. * Returns: 0 = Success, IOCB_ERROR = Failure.
  5277. **/
  5278. static int
  5279. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  5280. union lpfc_wqe *wqe)
  5281. {
  5282. uint32_t payload_len = 0;
  5283. uint8_t ct = 0;
  5284. uint32_t fip;
  5285. uint32_t abort_tag;
  5286. uint8_t command_type = ELS_COMMAND_NON_FIP;
  5287. uint8_t cmnd;
  5288. uint16_t xritag;
  5289. struct ulp_bde64 *bpl = NULL;
  5290. fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
  5291. /* The fcp commands will set command type */
  5292. if (iocbq->iocb_flag & LPFC_IO_FCP)
  5293. command_type = FCP_COMMAND;
  5294. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
  5295. command_type = ELS_COMMAND_FIP;
  5296. else
  5297. command_type = ELS_COMMAND_NON_FIP;
  5298. /* Some of the fields are in the right position already */
  5299. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  5300. abort_tag = (uint32_t) iocbq->iotag;
  5301. xritag = iocbq->sli4_xritag;
  5302. wqe->words[7] = 0; /* The ct field has moved so reset */
  5303. /* words0-2 bpl convert bde */
  5304. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  5305. bpl = (struct ulp_bde64 *)
  5306. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  5307. if (!bpl)
  5308. return IOCB_ERROR;
  5309. /* Should already be byte swapped. */
  5310. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  5311. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  5312. /* swap the size field back to the cpu so we
  5313. * can assign it to the sgl.
  5314. */
  5315. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  5316. payload_len = wqe->generic.bde.tus.f.bdeSize;
  5317. } else
  5318. payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  5319. iocbq->iocb.ulpIoTag = iocbq->iotag;
  5320. cmnd = iocbq->iocb.ulpCommand;
  5321. switch (iocbq->iocb.ulpCommand) {
  5322. case CMD_ELS_REQUEST64_CR:
  5323. if (!iocbq->iocb.ulpLe) {
  5324. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5325. "2007 Only Limited Edition cmd Format"
  5326. " supported 0x%x\n",
  5327. iocbq->iocb.ulpCommand);
  5328. return IOCB_ERROR;
  5329. }
  5330. wqe->els_req.payload_len = payload_len;
  5331. /* Els_reguest64 has a TMO */
  5332. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  5333. iocbq->iocb.ulpTimeout);
  5334. /* Need a VF for word 4 set the vf bit*/
  5335. bf_set(els_req64_vf, &wqe->els_req, 0);
  5336. /* And a VFID for word 12 */
  5337. bf_set(els_req64_vfid, &wqe->els_req, 0);
  5338. /*
  5339. * Set ct field to 3, indicates that the context_tag field
  5340. * contains the FCFI and remote N_Port_ID is
  5341. * in word 5.
  5342. */
  5343. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  5344. bf_set(lpfc_wqe_gen_context, &wqe->generic,
  5345. iocbq->iocb.ulpContext);
  5346. bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
  5347. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5348. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  5349. break;
  5350. case CMD_XMIT_SEQUENCE64_CR:
  5351. /* word3 iocb=io_tag32 wqe=payload_offset */
  5352. /* payload offset used for multilpe outstanding
  5353. * sequences on the same exchange
  5354. */
  5355. wqe->words[3] = 0;
  5356. /* word4 relative_offset memcpy */
  5357. /* word5 r_ctl/df_ctl memcpy */
  5358. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5359. wqe->xmit_sequence.xmit_len = payload_len;
  5360. break;
  5361. case CMD_XMIT_BCAST64_CN:
  5362. /* word3 iocb=iotag32 wqe=payload_len */
  5363. wqe->words[3] = 0; /* no definition for this in wqe */
  5364. /* word4 iocb=rsvd wqe=rsvd */
  5365. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  5366. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  5367. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5368. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5369. break;
  5370. case CMD_FCP_IWRITE64_CR:
  5371. command_type = FCP_COMMAND_DATA_OUT;
  5372. /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
  5373. * confusing.
  5374. * word3 is payload_len: byte offset to the sgl entry for the
  5375. * fcp_command.
  5376. * word4 is total xfer len, same as the IOCB->ulpParameter.
  5377. * word5 is initial xfer len 0 = wait for xfer-ready
  5378. */
  5379. /* Always wait for xfer-ready before sending data */
  5380. wqe->fcp_iwrite.initial_xfer_len = 0;
  5381. /* word 4 (xfer length) should have been set on the memcpy */
  5382. /* allow write to fall through to read */
  5383. case CMD_FCP_IREAD64_CR:
  5384. /* FCP_CMD is always the 1st sgl entry */
  5385. wqe->fcp_iread.payload_len =
  5386. payload_len + sizeof(struct fcp_rsp);
  5387. /* word 4 (xfer length) should have been set on the memcpy */
  5388. bf_set(lpfc_wqe_gen_erp, &wqe->generic,
  5389. iocbq->iocb.ulpFCP2Rcvy);
  5390. bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
  5391. /* The XC bit and the XS bit are similar. The driver never
  5392. * tracked whether or not the exchange was previouslly open.
  5393. * XC = Exchange create, 0 is create. 1 is already open.
  5394. * XS = link cmd: 1 do not close the exchange after command.
  5395. * XS = 0 close exchange when command completes.
  5396. * The only time we would not set the XC bit is when the XS bit
  5397. * is set and we are sending our 2nd or greater command on
  5398. * this exchange.
  5399. */
  5400. /* Always open the exchange */
  5401. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  5402. wqe->words[10] &= 0xffff0000; /* zero out ebde count */
  5403. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5404. break;
  5405. case CMD_FCP_ICMND64_CR:
  5406. /* Always open the exchange */
  5407. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  5408. wqe->words[4] = 0;
  5409. wqe->words[10] &= 0xffff0000; /* zero out ebde count */
  5410. bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
  5411. break;
  5412. case CMD_GEN_REQUEST64_CR:
  5413. /* word3 command length is described as byte offset to the
  5414. * rsp_data. Would always be 16, sizeof(struct sli4_sge)
  5415. * sgl[0] = cmnd
  5416. * sgl[1] = rsp.
  5417. *
  5418. */
  5419. wqe->gen_req.command_len = payload_len;
  5420. /* Word4 parameter copied in the memcpy */
  5421. /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
  5422. /* word6 context tag copied in memcpy */
  5423. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  5424. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  5425. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5426. "2015 Invalid CT %x command 0x%x\n",
  5427. ct, iocbq->iocb.ulpCommand);
  5428. return IOCB_ERROR;
  5429. }
  5430. bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
  5431. bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
  5432. iocbq->iocb.ulpTimeout);
  5433. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5434. command_type = OTHER_COMMAND;
  5435. break;
  5436. case CMD_XMIT_ELS_RSP64_CX:
  5437. /* words0-2 BDE memcpy */
  5438. /* word3 iocb=iotag32 wqe=rsvd */
  5439. wqe->words[3] = 0;
  5440. /* word4 iocb=did wge=rsvd. */
  5441. wqe->words[4] = 0;
  5442. /* word5 iocb=rsvd wge=did */
  5443. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  5444. iocbq->iocb.un.elsreq64.remoteID);
  5445. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5446. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5447. bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
  5448. bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
  5449. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  5450. bf_set(lpfc_wqe_gen_context, &wqe->generic,
  5451. iocbq->vport->vpi + phba->vpi_base);
  5452. command_type = OTHER_COMMAND;
  5453. break;
  5454. case CMD_CLOSE_XRI_CN:
  5455. case CMD_ABORT_XRI_CN:
  5456. case CMD_ABORT_XRI_CX:
  5457. /* words 0-2 memcpy should be 0 rserved */
  5458. /* port will send abts */
  5459. if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  5460. /*
  5461. * The link is down so the fw does not need to send abts
  5462. * on the wire.
  5463. */
  5464. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  5465. else
  5466. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  5467. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  5468. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  5469. wqe->words[5] = 0;
  5470. bf_set(lpfc_wqe_gen_ct, &wqe->generic,
  5471. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  5472. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  5473. wqe->generic.abort_tag = abort_tag;
  5474. /*
  5475. * The abort handler will send us CMD_ABORT_XRI_CN or
  5476. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  5477. */
  5478. bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
  5479. cmnd = CMD_ABORT_XRI_CX;
  5480. command_type = OTHER_COMMAND;
  5481. xritag = 0;
  5482. break;
  5483. case CMD_XRI_ABORTED_CX:
  5484. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  5485. /* words0-2 are all 0's no bde */
  5486. /* word3 and word4 are rsvrd */
  5487. wqe->words[3] = 0;
  5488. wqe->words[4] = 0;
  5489. /* word5 iocb=rsvd wge=did */
  5490. /* There is no remote port id in the IOCB? */
  5491. /* Let this fall through and fail */
  5492. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  5493. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  5494. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  5495. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  5496. default:
  5497. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5498. "2014 Invalid command 0x%x\n",
  5499. iocbq->iocb.ulpCommand);
  5500. return IOCB_ERROR;
  5501. break;
  5502. }
  5503. bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
  5504. bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
  5505. wqe->generic.abort_tag = abort_tag;
  5506. bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
  5507. bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
  5508. bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
  5509. bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
  5510. return 0;
  5511. }
  5512. /**
  5513. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  5514. * @phba: Pointer to HBA context object.
  5515. * @ring_number: SLI ring number to issue iocb on.
  5516. * @piocb: Pointer to command iocb.
  5517. * @flag: Flag indicating if this command can be put into txq.
  5518. *
  5519. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  5520. * an iocb command to an HBA with SLI-4 interface spec.
  5521. *
  5522. * This function is called with hbalock held. The function will return success
  5523. * after it successfully submit the iocb to firmware or after adding to the
  5524. * txq.
  5525. **/
  5526. static int
  5527. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  5528. struct lpfc_iocbq *piocb, uint32_t flag)
  5529. {
  5530. struct lpfc_sglq *sglq;
  5531. uint16_t xritag;
  5532. union lpfc_wqe wqe;
  5533. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  5534. uint32_t fcp_wqidx;
  5535. if (piocb->sli4_xritag == NO_XRI) {
  5536. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  5537. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  5538. sglq = NULL;
  5539. else {
  5540. sglq = __lpfc_sli_get_sglq(phba);
  5541. if (!sglq)
  5542. return IOCB_ERROR;
  5543. piocb->sli4_xritag = sglq->sli4_xritag;
  5544. }
  5545. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  5546. sglq = NULL; /* These IO's already have an XRI and
  5547. * a mapped sgl.
  5548. */
  5549. } else {
  5550. /* This is a continuation of a commandi,(CX) so this
  5551. * sglq is on the active list
  5552. */
  5553. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  5554. if (!sglq)
  5555. return IOCB_ERROR;
  5556. }
  5557. if (sglq) {
  5558. xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
  5559. if (xritag != sglq->sli4_xritag)
  5560. return IOCB_ERROR;
  5561. }
  5562. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  5563. return IOCB_ERROR;
  5564. if (piocb->iocb_flag & LPFC_IO_FCP) {
  5565. fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  5566. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
  5567. return IOCB_ERROR;
  5568. } else {
  5569. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  5570. return IOCB_ERROR;
  5571. }
  5572. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  5573. return 0;
  5574. }
  5575. /**
  5576. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  5577. *
  5578. * This routine wraps the actual lockless version for issusing IOCB function
  5579. * pointer from the lpfc_hba struct.
  5580. *
  5581. * Return codes:
  5582. * IOCB_ERROR - Error
  5583. * IOCB_SUCCESS - Success
  5584. * IOCB_BUSY - Busy
  5585. **/
  5586. static inline int
  5587. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  5588. struct lpfc_iocbq *piocb, uint32_t flag)
  5589. {
  5590. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  5591. }
  5592. /**
  5593. * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
  5594. * @phba: The hba struct for which this call is being executed.
  5595. * @dev_grp: The HBA PCI-Device group number.
  5596. *
  5597. * This routine sets up the SLI interface API function jump table in @phba
  5598. * struct.
  5599. * Returns: 0 - success, -ENODEV - failure.
  5600. **/
  5601. int
  5602. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  5603. {
  5604. switch (dev_grp) {
  5605. case LPFC_PCI_DEV_LP:
  5606. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  5607. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  5608. break;
  5609. case LPFC_PCI_DEV_OC:
  5610. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  5611. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  5612. break;
  5613. default:
  5614. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5615. "1419 Invalid HBA PCI-device group: 0x%x\n",
  5616. dev_grp);
  5617. return -ENODEV;
  5618. break;
  5619. }
  5620. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  5621. return 0;
  5622. }
  5623. /**
  5624. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  5625. * @phba: Pointer to HBA context object.
  5626. * @pring: Pointer to driver SLI ring object.
  5627. * @piocb: Pointer to command iocb.
  5628. * @flag: Flag indicating if this command can be put into txq.
  5629. *
  5630. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  5631. * function. This function gets the hbalock and calls
  5632. * __lpfc_sli_issue_iocb function and will return the error returned
  5633. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  5634. * functions which do not hold hbalock.
  5635. **/
  5636. int
  5637. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  5638. struct lpfc_iocbq *piocb, uint32_t flag)
  5639. {
  5640. unsigned long iflags;
  5641. int rc;
  5642. spin_lock_irqsave(&phba->hbalock, iflags);
  5643. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  5644. spin_unlock_irqrestore(&phba->hbalock, iflags);
  5645. return rc;
  5646. }
  5647. /**
  5648. * lpfc_extra_ring_setup - Extra ring setup function
  5649. * @phba: Pointer to HBA context object.
  5650. *
  5651. * This function is called while driver attaches with the
  5652. * HBA to setup the extra ring. The extra ring is used
  5653. * only when driver needs to support target mode functionality
  5654. * or IP over FC functionalities.
  5655. *
  5656. * This function is called with no lock held.
  5657. **/
  5658. static int
  5659. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  5660. {
  5661. struct lpfc_sli *psli;
  5662. struct lpfc_sli_ring *pring;
  5663. psli = &phba->sli;
  5664. /* Adjust cmd/rsp ring iocb entries more evenly */
  5665. /* Take some away from the FCP ring */
  5666. pring = &psli->ring[psli->fcp_ring];
  5667. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5668. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5669. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5670. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5671. /* and give them to the extra ring */
  5672. pring = &psli->ring[psli->extra_ring];
  5673. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5674. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5675. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5676. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5677. /* Setup default profile for this ring */
  5678. pring->iotag_max = 4096;
  5679. pring->num_mask = 1;
  5680. pring->prt[0].profile = 0; /* Mask 0 */
  5681. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  5682. pring->prt[0].type = phba->cfg_multi_ring_type;
  5683. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  5684. return 0;
  5685. }
  5686. /**
  5687. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  5688. * @phba: Pointer to HBA context object.
  5689. * @pring: Pointer to driver SLI ring object.
  5690. * @iocbq: Pointer to iocb object.
  5691. *
  5692. * This function is called by the slow ring event handler
  5693. * function when there is an ASYNC event iocb in the ring.
  5694. * This function is called with no lock held.
  5695. * Currently this function handles only temperature related
  5696. * ASYNC events. The function decodes the temperature sensor
  5697. * event message and posts events for the management applications.
  5698. **/
  5699. static void
  5700. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  5701. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  5702. {
  5703. IOCB_t *icmd;
  5704. uint16_t evt_code;
  5705. uint16_t temp;
  5706. struct temp_event temp_event_data;
  5707. struct Scsi_Host *shost;
  5708. uint32_t *iocb_w;
  5709. icmd = &iocbq->iocb;
  5710. evt_code = icmd->un.asyncstat.evt_code;
  5711. temp = icmd->ulpContext;
  5712. if ((evt_code != ASYNC_TEMP_WARN) &&
  5713. (evt_code != ASYNC_TEMP_SAFE)) {
  5714. iocb_w = (uint32_t *) icmd;
  5715. lpfc_printf_log(phba,
  5716. KERN_ERR,
  5717. LOG_SLI,
  5718. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  5719. " evt_code 0x%x\n"
  5720. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  5721. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  5722. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  5723. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  5724. pring->ringno,
  5725. icmd->un.asyncstat.evt_code,
  5726. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  5727. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  5728. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  5729. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  5730. return;
  5731. }
  5732. temp_event_data.data = (uint32_t)temp;
  5733. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  5734. if (evt_code == ASYNC_TEMP_WARN) {
  5735. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  5736. lpfc_printf_log(phba,
  5737. KERN_ERR,
  5738. LOG_TEMP,
  5739. "0347 Adapter is very hot, please take "
  5740. "corrective action. temperature : %d Celsius\n",
  5741. temp);
  5742. }
  5743. if (evt_code == ASYNC_TEMP_SAFE) {
  5744. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  5745. lpfc_printf_log(phba,
  5746. KERN_ERR,
  5747. LOG_TEMP,
  5748. "0340 Adapter temperature is OK now. "
  5749. "temperature : %d Celsius\n",
  5750. temp);
  5751. }
  5752. /* Send temperature change event to applications */
  5753. shost = lpfc_shost_from_vport(phba->pport);
  5754. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5755. sizeof(temp_event_data), (char *) &temp_event_data,
  5756. LPFC_NL_VENDOR_ID);
  5757. }
  5758. /**
  5759. * lpfc_sli_setup - SLI ring setup function
  5760. * @phba: Pointer to HBA context object.
  5761. *
  5762. * lpfc_sli_setup sets up rings of the SLI interface with
  5763. * number of iocbs per ring and iotags. This function is
  5764. * called while driver attach to the HBA and before the
  5765. * interrupts are enabled. So there is no need for locking.
  5766. *
  5767. * This function always returns 0.
  5768. **/
  5769. int
  5770. lpfc_sli_setup(struct lpfc_hba *phba)
  5771. {
  5772. int i, totiocbsize = 0;
  5773. struct lpfc_sli *psli = &phba->sli;
  5774. struct lpfc_sli_ring *pring;
  5775. psli->num_rings = MAX_CONFIGURED_RINGS;
  5776. psli->sli_flag = 0;
  5777. psli->fcp_ring = LPFC_FCP_RING;
  5778. psli->next_ring = LPFC_FCP_NEXT_RING;
  5779. psli->extra_ring = LPFC_EXTRA_RING;
  5780. psli->iocbq_lookup = NULL;
  5781. psli->iocbq_lookup_len = 0;
  5782. psli->last_iotag = 0;
  5783. for (i = 0; i < psli->num_rings; i++) {
  5784. pring = &psli->ring[i];
  5785. switch (i) {
  5786. case LPFC_FCP_RING: /* ring 0 - FCP */
  5787. /* numCiocb and numRiocb are used in config_port */
  5788. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  5789. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  5790. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  5791. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  5792. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  5793. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  5794. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5795. SLI3_IOCB_CMD_SIZE :
  5796. SLI2_IOCB_CMD_SIZE;
  5797. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5798. SLI3_IOCB_RSP_SIZE :
  5799. SLI2_IOCB_RSP_SIZE;
  5800. pring->iotag_ctr = 0;
  5801. pring->iotag_max =
  5802. (phba->cfg_hba_queue_depth * 2);
  5803. pring->fast_iotag = pring->iotag_max;
  5804. pring->num_mask = 0;
  5805. break;
  5806. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  5807. /* numCiocb and numRiocb are used in config_port */
  5808. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  5809. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  5810. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5811. SLI3_IOCB_CMD_SIZE :
  5812. SLI2_IOCB_CMD_SIZE;
  5813. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5814. SLI3_IOCB_RSP_SIZE :
  5815. SLI2_IOCB_RSP_SIZE;
  5816. pring->iotag_max = phba->cfg_hba_queue_depth;
  5817. pring->num_mask = 0;
  5818. break;
  5819. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  5820. /* numCiocb and numRiocb are used in config_port */
  5821. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  5822. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  5823. pring->sizeCiocb = (phba->sli_rev == 3) ?
  5824. SLI3_IOCB_CMD_SIZE :
  5825. SLI2_IOCB_CMD_SIZE;
  5826. pring->sizeRiocb = (phba->sli_rev == 3) ?
  5827. SLI3_IOCB_RSP_SIZE :
  5828. SLI2_IOCB_RSP_SIZE;
  5829. pring->fast_iotag = 0;
  5830. pring->iotag_ctr = 0;
  5831. pring->iotag_max = 4096;
  5832. pring->lpfc_sli_rcv_async_status =
  5833. lpfc_sli_async_event_handler;
  5834. pring->num_mask = 4;
  5835. pring->prt[0].profile = 0; /* Mask 0 */
  5836. pring->prt[0].rctl = FC_ELS_REQ;
  5837. pring->prt[0].type = FC_ELS_DATA;
  5838. pring->prt[0].lpfc_sli_rcv_unsol_event =
  5839. lpfc_els_unsol_event;
  5840. pring->prt[1].profile = 0; /* Mask 1 */
  5841. pring->prt[1].rctl = FC_ELS_RSP;
  5842. pring->prt[1].type = FC_ELS_DATA;
  5843. pring->prt[1].lpfc_sli_rcv_unsol_event =
  5844. lpfc_els_unsol_event;
  5845. pring->prt[2].profile = 0; /* Mask 2 */
  5846. /* NameServer Inquiry */
  5847. pring->prt[2].rctl = FC_UNSOL_CTL;
  5848. /* NameServer */
  5849. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  5850. pring->prt[2].lpfc_sli_rcv_unsol_event =
  5851. lpfc_ct_unsol_event;
  5852. pring->prt[3].profile = 0; /* Mask 3 */
  5853. /* NameServer response */
  5854. pring->prt[3].rctl = FC_SOL_CTL;
  5855. /* NameServer */
  5856. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  5857. pring->prt[3].lpfc_sli_rcv_unsol_event =
  5858. lpfc_ct_unsol_event;
  5859. break;
  5860. }
  5861. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  5862. (pring->numRiocb * pring->sizeRiocb);
  5863. }
  5864. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  5865. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  5866. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  5867. "SLI2 SLIM Data: x%x x%lx\n",
  5868. phba->brd_no, totiocbsize,
  5869. (unsigned long) MAX_SLIM_IOCB_SIZE);
  5870. }
  5871. if (phba->cfg_multi_ring_support == 2)
  5872. lpfc_extra_ring_setup(phba);
  5873. return 0;
  5874. }
  5875. /**
  5876. * lpfc_sli_queue_setup - Queue initialization function
  5877. * @phba: Pointer to HBA context object.
  5878. *
  5879. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  5880. * ring. This function also initializes ring indices of each ring.
  5881. * This function is called during the initialization of the SLI
  5882. * interface of an HBA.
  5883. * This function is called with no lock held and always returns
  5884. * 1.
  5885. **/
  5886. int
  5887. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  5888. {
  5889. struct lpfc_sli *psli;
  5890. struct lpfc_sli_ring *pring;
  5891. int i;
  5892. psli = &phba->sli;
  5893. spin_lock_irq(&phba->hbalock);
  5894. INIT_LIST_HEAD(&psli->mboxq);
  5895. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  5896. /* Initialize list headers for txq and txcmplq as double linked lists */
  5897. for (i = 0; i < psli->num_rings; i++) {
  5898. pring = &psli->ring[i];
  5899. pring->ringno = i;
  5900. pring->next_cmdidx = 0;
  5901. pring->local_getidx = 0;
  5902. pring->cmdidx = 0;
  5903. INIT_LIST_HEAD(&pring->txq);
  5904. INIT_LIST_HEAD(&pring->txcmplq);
  5905. INIT_LIST_HEAD(&pring->iocb_continueq);
  5906. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  5907. INIT_LIST_HEAD(&pring->postbufq);
  5908. }
  5909. spin_unlock_irq(&phba->hbalock);
  5910. return 1;
  5911. }
  5912. /**
  5913. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  5914. * @phba: Pointer to HBA context object.
  5915. *
  5916. * This routine flushes the mailbox command subsystem. It will unconditionally
  5917. * flush all the mailbox commands in the three possible stages in the mailbox
  5918. * command sub-system: pending mailbox command queue; the outstanding mailbox
  5919. * command; and completed mailbox command queue. It is caller's responsibility
  5920. * to make sure that the driver is in the proper state to flush the mailbox
  5921. * command sub-system. Namely, the posting of mailbox commands into the
  5922. * pending mailbox command queue from the various clients must be stopped;
  5923. * either the HBA is in a state that it will never works on the outstanding
  5924. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  5925. * mailbox command has been completed.
  5926. **/
  5927. static void
  5928. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  5929. {
  5930. LIST_HEAD(completions);
  5931. struct lpfc_sli *psli = &phba->sli;
  5932. LPFC_MBOXQ_t *pmb;
  5933. unsigned long iflag;
  5934. /* Flush all the mailbox commands in the mbox system */
  5935. spin_lock_irqsave(&phba->hbalock, iflag);
  5936. /* The pending mailbox command queue */
  5937. list_splice_init(&phba->sli.mboxq, &completions);
  5938. /* The outstanding active mailbox command */
  5939. if (psli->mbox_active) {
  5940. list_add_tail(&psli->mbox_active->list, &completions);
  5941. psli->mbox_active = NULL;
  5942. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5943. }
  5944. /* The completed mailbox command queue */
  5945. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  5946. spin_unlock_irqrestore(&phba->hbalock, iflag);
  5947. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  5948. while (!list_empty(&completions)) {
  5949. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  5950. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  5951. if (pmb->mbox_cmpl)
  5952. pmb->mbox_cmpl(phba, pmb);
  5953. }
  5954. }
  5955. /**
  5956. * lpfc_sli_host_down - Vport cleanup function
  5957. * @vport: Pointer to virtual port object.
  5958. *
  5959. * lpfc_sli_host_down is called to clean up the resources
  5960. * associated with a vport before destroying virtual
  5961. * port data structures.
  5962. * This function does following operations:
  5963. * - Free discovery resources associated with this virtual
  5964. * port.
  5965. * - Free iocbs associated with this virtual port in
  5966. * the txq.
  5967. * - Send abort for all iocb commands associated with this
  5968. * vport in txcmplq.
  5969. *
  5970. * This function is called with no lock held and always returns 1.
  5971. **/
  5972. int
  5973. lpfc_sli_host_down(struct lpfc_vport *vport)
  5974. {
  5975. LIST_HEAD(completions);
  5976. struct lpfc_hba *phba = vport->phba;
  5977. struct lpfc_sli *psli = &phba->sli;
  5978. struct lpfc_sli_ring *pring;
  5979. struct lpfc_iocbq *iocb, *next_iocb;
  5980. int i;
  5981. unsigned long flags = 0;
  5982. uint16_t prev_pring_flag;
  5983. lpfc_cleanup_discovery_resources(vport);
  5984. spin_lock_irqsave(&phba->hbalock, flags);
  5985. for (i = 0; i < psli->num_rings; i++) {
  5986. pring = &psli->ring[i];
  5987. prev_pring_flag = pring->flag;
  5988. /* Only slow rings */
  5989. if (pring->ringno == LPFC_ELS_RING) {
  5990. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  5991. /* Set the lpfc data pending flag */
  5992. set_bit(LPFC_DATA_READY, &phba->data_flags);
  5993. }
  5994. /*
  5995. * Error everything on the txq since these iocbs have not been
  5996. * given to the FW yet.
  5997. */
  5998. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  5999. if (iocb->vport != vport)
  6000. continue;
  6001. list_move_tail(&iocb->list, &completions);
  6002. pring->txq_cnt--;
  6003. }
  6004. /* Next issue ABTS for everything on the txcmplq */
  6005. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  6006. list) {
  6007. if (iocb->vport != vport)
  6008. continue;
  6009. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  6010. }
  6011. pring->flag = prev_pring_flag;
  6012. }
  6013. spin_unlock_irqrestore(&phba->hbalock, flags);
  6014. /* Cancel all the IOCBs from the completions list */
  6015. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  6016. IOERR_SLI_DOWN);
  6017. return 1;
  6018. }
  6019. /**
  6020. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  6021. * @phba: Pointer to HBA context object.
  6022. *
  6023. * This function cleans up all iocb, buffers, mailbox commands
  6024. * while shutting down the HBA. This function is called with no
  6025. * lock held and always returns 1.
  6026. * This function does the following to cleanup driver resources:
  6027. * - Free discovery resources for each virtual port
  6028. * - Cleanup any pending fabric iocbs
  6029. * - Iterate through the iocb txq and free each entry
  6030. * in the list.
  6031. * - Free up any buffer posted to the HBA
  6032. * - Free mailbox commands in the mailbox queue.
  6033. **/
  6034. int
  6035. lpfc_sli_hba_down(struct lpfc_hba *phba)
  6036. {
  6037. LIST_HEAD(completions);
  6038. struct lpfc_sli *psli = &phba->sli;
  6039. struct lpfc_sli_ring *pring;
  6040. struct lpfc_dmabuf *buf_ptr;
  6041. unsigned long flags = 0;
  6042. int i;
  6043. /* Shutdown the mailbox command sub-system */
  6044. lpfc_sli_mbox_sys_shutdown(phba);
  6045. lpfc_hba_down_prep(phba);
  6046. lpfc_fabric_abort_hba(phba);
  6047. spin_lock_irqsave(&phba->hbalock, flags);
  6048. for (i = 0; i < psli->num_rings; i++) {
  6049. pring = &psli->ring[i];
  6050. /* Only slow rings */
  6051. if (pring->ringno == LPFC_ELS_RING) {
  6052. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  6053. /* Set the lpfc data pending flag */
  6054. set_bit(LPFC_DATA_READY, &phba->data_flags);
  6055. }
  6056. /*
  6057. * Error everything on the txq since these iocbs have not been
  6058. * given to the FW yet.
  6059. */
  6060. list_splice_init(&pring->txq, &completions);
  6061. pring->txq_cnt = 0;
  6062. }
  6063. spin_unlock_irqrestore(&phba->hbalock, flags);
  6064. /* Cancel all the IOCBs from the completions list */
  6065. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  6066. IOERR_SLI_DOWN);
  6067. spin_lock_irqsave(&phba->hbalock, flags);
  6068. list_splice_init(&phba->elsbuf, &completions);
  6069. phba->elsbuf_cnt = 0;
  6070. phba->elsbuf_prev_cnt = 0;
  6071. spin_unlock_irqrestore(&phba->hbalock, flags);
  6072. while (!list_empty(&completions)) {
  6073. list_remove_head(&completions, buf_ptr,
  6074. struct lpfc_dmabuf, list);
  6075. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  6076. kfree(buf_ptr);
  6077. }
  6078. /* Return any active mbox cmds */
  6079. del_timer_sync(&psli->mbox_tmo);
  6080. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  6081. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6082. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  6083. return 1;
  6084. }
  6085. /**
  6086. * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
  6087. * @phba: Pointer to HBA context object.
  6088. *
  6089. * This function cleans up all queues, iocb, buffers, mailbox commands while
  6090. * shutting down the SLI4 HBA FCoE function. This function is called with no
  6091. * lock held and always returns 1.
  6092. *
  6093. * This function does the following to cleanup driver FCoE function resources:
  6094. * - Free discovery resources for each virtual port
  6095. * - Cleanup any pending fabric iocbs
  6096. * - Iterate through the iocb txq and free each entry in the list.
  6097. * - Free up any buffer posted to the HBA.
  6098. * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
  6099. * - Free mailbox commands in the mailbox queue.
  6100. **/
  6101. int
  6102. lpfc_sli4_hba_down(struct lpfc_hba *phba)
  6103. {
  6104. /* Stop the SLI4 device port */
  6105. lpfc_stop_port(phba);
  6106. /* Tear down the queues in the HBA */
  6107. lpfc_sli4_queue_unset(phba);
  6108. /* unregister default FCFI from the HBA */
  6109. lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
  6110. return 1;
  6111. }
  6112. /**
  6113. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  6114. * @srcp: Source memory pointer.
  6115. * @destp: Destination memory pointer.
  6116. * @cnt: Number of words required to be copied.
  6117. *
  6118. * This function is used for copying data between driver memory
  6119. * and the SLI memory. This function also changes the endianness
  6120. * of each word if native endianness is different from SLI
  6121. * endianness. This function can be called with or without
  6122. * lock.
  6123. **/
  6124. void
  6125. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  6126. {
  6127. uint32_t *src = srcp;
  6128. uint32_t *dest = destp;
  6129. uint32_t ldata;
  6130. int i;
  6131. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  6132. ldata = *src;
  6133. ldata = le32_to_cpu(ldata);
  6134. *dest = ldata;
  6135. src++;
  6136. dest++;
  6137. }
  6138. }
  6139. /**
  6140. * lpfc_sli_bemem_bcopy - SLI memory copy function
  6141. * @srcp: Source memory pointer.
  6142. * @destp: Destination memory pointer.
  6143. * @cnt: Number of words required to be copied.
  6144. *
  6145. * This function is used for copying data between a data structure
  6146. * with big endian representation to local endianness.
  6147. * This function can be called with or without lock.
  6148. **/
  6149. void
  6150. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  6151. {
  6152. uint32_t *src = srcp;
  6153. uint32_t *dest = destp;
  6154. uint32_t ldata;
  6155. int i;
  6156. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  6157. ldata = *src;
  6158. ldata = be32_to_cpu(ldata);
  6159. *dest = ldata;
  6160. src++;
  6161. dest++;
  6162. }
  6163. }
  6164. /**
  6165. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  6166. * @phba: Pointer to HBA context object.
  6167. * @pring: Pointer to driver SLI ring object.
  6168. * @mp: Pointer to driver buffer object.
  6169. *
  6170. * This function is called with no lock held.
  6171. * It always return zero after adding the buffer to the postbufq
  6172. * buffer list.
  6173. **/
  6174. int
  6175. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6176. struct lpfc_dmabuf *mp)
  6177. {
  6178. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  6179. later */
  6180. spin_lock_irq(&phba->hbalock);
  6181. list_add_tail(&mp->list, &pring->postbufq);
  6182. pring->postbufq_cnt++;
  6183. spin_unlock_irq(&phba->hbalock);
  6184. return 0;
  6185. }
  6186. /**
  6187. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  6188. * @phba: Pointer to HBA context object.
  6189. *
  6190. * When HBQ is enabled, buffers are searched based on tags. This function
  6191. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  6192. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  6193. * does not conflict with tags of buffer posted for unsolicited events.
  6194. * The function returns the allocated tag. The function is called with
  6195. * no locks held.
  6196. **/
  6197. uint32_t
  6198. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  6199. {
  6200. spin_lock_irq(&phba->hbalock);
  6201. phba->buffer_tag_count++;
  6202. /*
  6203. * Always set the QUE_BUFTAG_BIT to distiguish between
  6204. * a tag assigned by HBQ.
  6205. */
  6206. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  6207. spin_unlock_irq(&phba->hbalock);
  6208. return phba->buffer_tag_count;
  6209. }
  6210. /**
  6211. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  6212. * @phba: Pointer to HBA context object.
  6213. * @pring: Pointer to driver SLI ring object.
  6214. * @tag: Buffer tag.
  6215. *
  6216. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  6217. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  6218. * iocb is posted to the response ring with the tag of the buffer.
  6219. * This function searches the pring->postbufq list using the tag
  6220. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  6221. * iocb. If the buffer is found then lpfc_dmabuf object of the
  6222. * buffer is returned to the caller else NULL is returned.
  6223. * This function is called with no lock held.
  6224. **/
  6225. struct lpfc_dmabuf *
  6226. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6227. uint32_t tag)
  6228. {
  6229. struct lpfc_dmabuf *mp, *next_mp;
  6230. struct list_head *slp = &pring->postbufq;
  6231. /* Search postbufq, from the begining, looking for a match on tag */
  6232. spin_lock_irq(&phba->hbalock);
  6233. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  6234. if (mp->buffer_tag == tag) {
  6235. list_del_init(&mp->list);
  6236. pring->postbufq_cnt--;
  6237. spin_unlock_irq(&phba->hbalock);
  6238. return mp;
  6239. }
  6240. }
  6241. spin_unlock_irq(&phba->hbalock);
  6242. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6243. "0402 Cannot find virtual addr for buffer tag on "
  6244. "ring %d Data x%lx x%p x%p x%x\n",
  6245. pring->ringno, (unsigned long) tag,
  6246. slp->next, slp->prev, pring->postbufq_cnt);
  6247. return NULL;
  6248. }
  6249. /**
  6250. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  6251. * @phba: Pointer to HBA context object.
  6252. * @pring: Pointer to driver SLI ring object.
  6253. * @phys: DMA address of the buffer.
  6254. *
  6255. * This function searches the buffer list using the dma_address
  6256. * of unsolicited event to find the driver's lpfc_dmabuf object
  6257. * corresponding to the dma_address. The function returns the
  6258. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  6259. * This function is called by the ct and els unsolicited event
  6260. * handlers to get the buffer associated with the unsolicited
  6261. * event.
  6262. *
  6263. * This function is called with no lock held.
  6264. **/
  6265. struct lpfc_dmabuf *
  6266. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6267. dma_addr_t phys)
  6268. {
  6269. struct lpfc_dmabuf *mp, *next_mp;
  6270. struct list_head *slp = &pring->postbufq;
  6271. /* Search postbufq, from the begining, looking for a match on phys */
  6272. spin_lock_irq(&phba->hbalock);
  6273. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  6274. if (mp->phys == phys) {
  6275. list_del_init(&mp->list);
  6276. pring->postbufq_cnt--;
  6277. spin_unlock_irq(&phba->hbalock);
  6278. return mp;
  6279. }
  6280. }
  6281. spin_unlock_irq(&phba->hbalock);
  6282. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6283. "0410 Cannot find virtual addr for mapped buf on "
  6284. "ring %d Data x%llx x%p x%p x%x\n",
  6285. pring->ringno, (unsigned long long)phys,
  6286. slp->next, slp->prev, pring->postbufq_cnt);
  6287. return NULL;
  6288. }
  6289. /**
  6290. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  6291. * @phba: Pointer to HBA context object.
  6292. * @cmdiocb: Pointer to driver command iocb object.
  6293. * @rspiocb: Pointer to driver response iocb object.
  6294. *
  6295. * This function is the completion handler for the abort iocbs for
  6296. * ELS commands. This function is called from the ELS ring event
  6297. * handler with no lock held. This function frees memory resources
  6298. * associated with the abort iocb.
  6299. **/
  6300. static void
  6301. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6302. struct lpfc_iocbq *rspiocb)
  6303. {
  6304. IOCB_t *irsp = &rspiocb->iocb;
  6305. uint16_t abort_iotag, abort_context;
  6306. struct lpfc_iocbq *abort_iocb;
  6307. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  6308. abort_iocb = NULL;
  6309. if (irsp->ulpStatus) {
  6310. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  6311. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  6312. spin_lock_irq(&phba->hbalock);
  6313. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  6314. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  6315. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  6316. "0327 Cannot abort els iocb %p "
  6317. "with tag %x context %x, abort status %x, "
  6318. "abort code %x\n",
  6319. abort_iocb, abort_iotag, abort_context,
  6320. irsp->ulpStatus, irsp->un.ulpWord[4]);
  6321. /*
  6322. * If the iocb is not found in Firmware queue the iocb
  6323. * might have completed already. Do not free it again.
  6324. */
  6325. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  6326. spin_unlock_irq(&phba->hbalock);
  6327. lpfc_sli_release_iocbq(phba, cmdiocb);
  6328. return;
  6329. }
  6330. /*
  6331. * make sure we have the right iocbq before taking it
  6332. * off the txcmplq and try to call completion routine.
  6333. */
  6334. if (!abort_iocb ||
  6335. abort_iocb->iocb.ulpContext != abort_context ||
  6336. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  6337. spin_unlock_irq(&phba->hbalock);
  6338. else {
  6339. list_del_init(&abort_iocb->list);
  6340. pring->txcmplq_cnt--;
  6341. spin_unlock_irq(&phba->hbalock);
  6342. /* Firmware could still be in progress of DMAing
  6343. * payload, so don't free data buffer till after
  6344. * a hbeat.
  6345. */
  6346. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  6347. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  6348. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  6349. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  6350. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  6351. }
  6352. }
  6353. lpfc_sli_release_iocbq(phba, cmdiocb);
  6354. return;
  6355. }
  6356. /**
  6357. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  6358. * @phba: Pointer to HBA context object.
  6359. * @cmdiocb: Pointer to driver command iocb object.
  6360. * @rspiocb: Pointer to driver response iocb object.
  6361. *
  6362. * The function is called from SLI ring event handler with no
  6363. * lock held. This function is the completion handler for ELS commands
  6364. * which are aborted. The function frees memory resources used for
  6365. * the aborted ELS commands.
  6366. **/
  6367. static void
  6368. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6369. struct lpfc_iocbq *rspiocb)
  6370. {
  6371. IOCB_t *irsp = &rspiocb->iocb;
  6372. /* ELS cmd tag <ulpIoTag> completes */
  6373. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  6374. "0139 Ignoring ELS cmd tag x%x completion Data: "
  6375. "x%x x%x x%x\n",
  6376. irsp->ulpIoTag, irsp->ulpStatus,
  6377. irsp->un.ulpWord[4], irsp->ulpTimeout);
  6378. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  6379. lpfc_ct_free_iocb(phba, cmdiocb);
  6380. else
  6381. lpfc_els_free_iocb(phba, cmdiocb);
  6382. return;
  6383. }
  6384. /**
  6385. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  6386. * @phba: Pointer to HBA context object.
  6387. * @pring: Pointer to driver SLI ring object.
  6388. * @cmdiocb: Pointer to driver command iocb object.
  6389. *
  6390. * This function issues an abort iocb for the provided command
  6391. * iocb. This function is called with hbalock held.
  6392. * The function returns 0 when it fails due to memory allocation
  6393. * failure or when the command iocb is an abort request.
  6394. **/
  6395. int
  6396. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6397. struct lpfc_iocbq *cmdiocb)
  6398. {
  6399. struct lpfc_vport *vport = cmdiocb->vport;
  6400. struct lpfc_iocbq *abtsiocbp;
  6401. IOCB_t *icmd = NULL;
  6402. IOCB_t *iabt = NULL;
  6403. int retval = IOCB_ERROR;
  6404. /*
  6405. * There are certain command types we don't want to abort. And we
  6406. * don't want to abort commands that are already in the process of
  6407. * being aborted.
  6408. */
  6409. icmd = &cmdiocb->iocb;
  6410. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  6411. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  6412. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  6413. return 0;
  6414. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  6415. * callback so that nothing happens when it finishes.
  6416. */
  6417. if ((vport->load_flag & FC_UNLOADING) &&
  6418. (pring->ringno == LPFC_ELS_RING)) {
  6419. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  6420. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  6421. else
  6422. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  6423. goto abort_iotag_exit;
  6424. }
  6425. /* issue ABTS for this IOCB based on iotag */
  6426. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  6427. if (abtsiocbp == NULL)
  6428. return 0;
  6429. /* This signals the response to set the correct status
  6430. * before calling the completion handler.
  6431. */
  6432. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  6433. iabt = &abtsiocbp->iocb;
  6434. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  6435. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  6436. if (phba->sli_rev == LPFC_SLI_REV4)
  6437. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  6438. else
  6439. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  6440. iabt->ulpLe = 1;
  6441. iabt->ulpClass = icmd->ulpClass;
  6442. if (phba->link_state >= LPFC_LINK_UP)
  6443. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  6444. else
  6445. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  6446. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  6447. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  6448. "0339 Abort xri x%x, original iotag x%x, "
  6449. "abort cmd iotag x%x\n",
  6450. iabt->un.acxri.abortContextTag,
  6451. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  6452. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  6453. if (retval)
  6454. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  6455. abort_iotag_exit:
  6456. /*
  6457. * Caller to this routine should check for IOCB_ERROR
  6458. * and handle it properly. This routine no longer removes
  6459. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  6460. */
  6461. return retval;
  6462. }
  6463. /**
  6464. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  6465. * @iocbq: Pointer to driver iocb object.
  6466. * @vport: Pointer to driver virtual port object.
  6467. * @tgt_id: SCSI ID of the target.
  6468. * @lun_id: LUN ID of the scsi device.
  6469. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  6470. *
  6471. * This function acts as an iocb filter for functions which abort or count
  6472. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  6473. * 0 if the filtering criteria is met for the given iocb and will return
  6474. * 1 if the filtering criteria is not met.
  6475. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  6476. * given iocb is for the SCSI device specified by vport, tgt_id and
  6477. * lun_id parameter.
  6478. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  6479. * given iocb is for the SCSI target specified by vport and tgt_id
  6480. * parameters.
  6481. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  6482. * given iocb is for the SCSI host associated with the given vport.
  6483. * This function is called with no locks held.
  6484. **/
  6485. static int
  6486. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  6487. uint16_t tgt_id, uint64_t lun_id,
  6488. lpfc_ctx_cmd ctx_cmd)
  6489. {
  6490. struct lpfc_scsi_buf *lpfc_cmd;
  6491. int rc = 1;
  6492. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  6493. return rc;
  6494. if (iocbq->vport != vport)
  6495. return rc;
  6496. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  6497. if (lpfc_cmd->pCmd == NULL)
  6498. return rc;
  6499. switch (ctx_cmd) {
  6500. case LPFC_CTX_LUN:
  6501. if ((lpfc_cmd->rdata->pnode) &&
  6502. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  6503. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  6504. rc = 0;
  6505. break;
  6506. case LPFC_CTX_TGT:
  6507. if ((lpfc_cmd->rdata->pnode) &&
  6508. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  6509. rc = 0;
  6510. break;
  6511. case LPFC_CTX_HOST:
  6512. rc = 0;
  6513. break;
  6514. default:
  6515. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  6516. __func__, ctx_cmd);
  6517. break;
  6518. }
  6519. return rc;
  6520. }
  6521. /**
  6522. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  6523. * @vport: Pointer to virtual port.
  6524. * @tgt_id: SCSI ID of the target.
  6525. * @lun_id: LUN ID of the scsi device.
  6526. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  6527. *
  6528. * This function returns number of FCP commands pending for the vport.
  6529. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  6530. * commands pending on the vport associated with SCSI device specified
  6531. * by tgt_id and lun_id parameters.
  6532. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  6533. * commands pending on the vport associated with SCSI target specified
  6534. * by tgt_id parameter.
  6535. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  6536. * commands pending on the vport.
  6537. * This function returns the number of iocbs which satisfy the filter.
  6538. * This function is called without any lock held.
  6539. **/
  6540. int
  6541. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  6542. lpfc_ctx_cmd ctx_cmd)
  6543. {
  6544. struct lpfc_hba *phba = vport->phba;
  6545. struct lpfc_iocbq *iocbq;
  6546. int sum, i;
  6547. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  6548. iocbq = phba->sli.iocbq_lookup[i];
  6549. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  6550. ctx_cmd) == 0)
  6551. sum++;
  6552. }
  6553. return sum;
  6554. }
  6555. /**
  6556. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  6557. * @phba: Pointer to HBA context object
  6558. * @cmdiocb: Pointer to command iocb object.
  6559. * @rspiocb: Pointer to response iocb object.
  6560. *
  6561. * This function is called when an aborted FCP iocb completes. This
  6562. * function is called by the ring event handler with no lock held.
  6563. * This function frees the iocb.
  6564. **/
  6565. void
  6566. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  6567. struct lpfc_iocbq *rspiocb)
  6568. {
  6569. lpfc_sli_release_iocbq(phba, cmdiocb);
  6570. return;
  6571. }
  6572. /**
  6573. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  6574. * @vport: Pointer to virtual port.
  6575. * @pring: Pointer to driver SLI ring object.
  6576. * @tgt_id: SCSI ID of the target.
  6577. * @lun_id: LUN ID of the scsi device.
  6578. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  6579. *
  6580. * This function sends an abort command for every SCSI command
  6581. * associated with the given virtual port pending on the ring
  6582. * filtered by lpfc_sli_validate_fcp_iocb function.
  6583. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  6584. * FCP iocbs associated with lun specified by tgt_id and lun_id
  6585. * parameters
  6586. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  6587. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  6588. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  6589. * FCP iocbs associated with virtual port.
  6590. * This function returns number of iocbs it failed to abort.
  6591. * This function is called with no locks held.
  6592. **/
  6593. int
  6594. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  6595. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  6596. {
  6597. struct lpfc_hba *phba = vport->phba;
  6598. struct lpfc_iocbq *iocbq;
  6599. struct lpfc_iocbq *abtsiocb;
  6600. IOCB_t *cmd = NULL;
  6601. int errcnt = 0, ret_val = 0;
  6602. int i;
  6603. for (i = 1; i <= phba->sli.last_iotag; i++) {
  6604. iocbq = phba->sli.iocbq_lookup[i];
  6605. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  6606. abort_cmd) != 0)
  6607. continue;
  6608. /* issue ABTS for this IOCB based on iotag */
  6609. abtsiocb = lpfc_sli_get_iocbq(phba);
  6610. if (abtsiocb == NULL) {
  6611. errcnt++;
  6612. continue;
  6613. }
  6614. cmd = &iocbq->iocb;
  6615. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  6616. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  6617. if (phba->sli_rev == LPFC_SLI_REV4)
  6618. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  6619. else
  6620. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  6621. abtsiocb->iocb.ulpLe = 1;
  6622. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  6623. abtsiocb->vport = phba->pport;
  6624. if (lpfc_is_link_up(phba))
  6625. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  6626. else
  6627. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  6628. /* Setup callback routine and issue the command. */
  6629. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  6630. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  6631. abtsiocb, 0);
  6632. if (ret_val == IOCB_ERROR) {
  6633. lpfc_sli_release_iocbq(phba, abtsiocb);
  6634. errcnt++;
  6635. continue;
  6636. }
  6637. }
  6638. return errcnt;
  6639. }
  6640. /**
  6641. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  6642. * @phba: Pointer to HBA context object.
  6643. * @cmdiocbq: Pointer to command iocb.
  6644. * @rspiocbq: Pointer to response iocb.
  6645. *
  6646. * This function is the completion handler for iocbs issued using
  6647. * lpfc_sli_issue_iocb_wait function. This function is called by the
  6648. * ring event handler function without any lock held. This function
  6649. * can be called from both worker thread context and interrupt
  6650. * context. This function also can be called from other thread which
  6651. * cleans up the SLI layer objects.
  6652. * This function copy the contents of the response iocb to the
  6653. * response iocb memory object provided by the caller of
  6654. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  6655. * sleeps for the iocb completion.
  6656. **/
  6657. static void
  6658. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  6659. struct lpfc_iocbq *cmdiocbq,
  6660. struct lpfc_iocbq *rspiocbq)
  6661. {
  6662. wait_queue_head_t *pdone_q;
  6663. unsigned long iflags;
  6664. spin_lock_irqsave(&phba->hbalock, iflags);
  6665. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  6666. if (cmdiocbq->context2 && rspiocbq)
  6667. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  6668. &rspiocbq->iocb, sizeof(IOCB_t));
  6669. pdone_q = cmdiocbq->context_un.wait_queue;
  6670. if (pdone_q)
  6671. wake_up(pdone_q);
  6672. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6673. return;
  6674. }
  6675. /**
  6676. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  6677. * @phba: Pointer to HBA context object..
  6678. * @piocbq: Pointer to command iocb.
  6679. * @flag: Flag to test.
  6680. *
  6681. * This routine grabs the hbalock and then test the iocb_flag to
  6682. * see if the passed in flag is set.
  6683. * Returns:
  6684. * 1 if flag is set.
  6685. * 0 if flag is not set.
  6686. **/
  6687. static int
  6688. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  6689. struct lpfc_iocbq *piocbq, uint32_t flag)
  6690. {
  6691. unsigned long iflags;
  6692. int ret;
  6693. spin_lock_irqsave(&phba->hbalock, iflags);
  6694. ret = piocbq->iocb_flag & flag;
  6695. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6696. return ret;
  6697. }
  6698. /**
  6699. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  6700. * @phba: Pointer to HBA context object..
  6701. * @pring: Pointer to sli ring.
  6702. * @piocb: Pointer to command iocb.
  6703. * @prspiocbq: Pointer to response iocb.
  6704. * @timeout: Timeout in number of seconds.
  6705. *
  6706. * This function issues the iocb to firmware and waits for the
  6707. * iocb to complete. If the iocb command is not
  6708. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  6709. * Caller should not free the iocb resources if this function
  6710. * returns IOCB_TIMEDOUT.
  6711. * The function waits for the iocb completion using an
  6712. * non-interruptible wait.
  6713. * This function will sleep while waiting for iocb completion.
  6714. * So, this function should not be called from any context which
  6715. * does not allow sleeping. Due to the same reason, this function
  6716. * cannot be called with interrupt disabled.
  6717. * This function assumes that the iocb completions occur while
  6718. * this function sleep. So, this function cannot be called from
  6719. * the thread which process iocb completion for this ring.
  6720. * This function clears the iocb_flag of the iocb object before
  6721. * issuing the iocb and the iocb completion handler sets this
  6722. * flag and wakes this thread when the iocb completes.
  6723. * The contents of the response iocb will be copied to prspiocbq
  6724. * by the completion handler when the command completes.
  6725. * This function returns IOCB_SUCCESS when success.
  6726. * This function is called with no lock held.
  6727. **/
  6728. int
  6729. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  6730. uint32_t ring_number,
  6731. struct lpfc_iocbq *piocb,
  6732. struct lpfc_iocbq *prspiocbq,
  6733. uint32_t timeout)
  6734. {
  6735. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  6736. long timeleft, timeout_req = 0;
  6737. int retval = IOCB_SUCCESS;
  6738. uint32_t creg_val;
  6739. /*
  6740. * If the caller has provided a response iocbq buffer, then context2
  6741. * is NULL or its an error.
  6742. */
  6743. if (prspiocbq) {
  6744. if (piocb->context2)
  6745. return IOCB_ERROR;
  6746. piocb->context2 = prspiocbq;
  6747. }
  6748. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  6749. piocb->context_un.wait_queue = &done_q;
  6750. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  6751. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  6752. creg_val = readl(phba->HCregaddr);
  6753. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  6754. writel(creg_val, phba->HCregaddr);
  6755. readl(phba->HCregaddr); /* flush */
  6756. }
  6757. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
  6758. if (retval == IOCB_SUCCESS) {
  6759. timeout_req = timeout * HZ;
  6760. timeleft = wait_event_timeout(done_q,
  6761. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  6762. timeout_req);
  6763. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  6764. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6765. "0331 IOCB wake signaled\n");
  6766. } else if (timeleft == 0) {
  6767. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6768. "0338 IOCB wait timeout error - no "
  6769. "wake response Data x%x\n", timeout);
  6770. retval = IOCB_TIMEDOUT;
  6771. } else {
  6772. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6773. "0330 IOCB wake NOT set, "
  6774. "Data x%x x%lx\n",
  6775. timeout, (timeleft / jiffies));
  6776. retval = IOCB_TIMEDOUT;
  6777. }
  6778. } else {
  6779. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6780. "0332 IOCB wait issue failed, Data x%x\n",
  6781. retval);
  6782. retval = IOCB_ERROR;
  6783. }
  6784. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  6785. creg_val = readl(phba->HCregaddr);
  6786. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  6787. writel(creg_val, phba->HCregaddr);
  6788. readl(phba->HCregaddr); /* flush */
  6789. }
  6790. if (prspiocbq)
  6791. piocb->context2 = NULL;
  6792. piocb->context_un.wait_queue = NULL;
  6793. piocb->iocb_cmpl = NULL;
  6794. return retval;
  6795. }
  6796. /**
  6797. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  6798. * @phba: Pointer to HBA context object.
  6799. * @pmboxq: Pointer to driver mailbox object.
  6800. * @timeout: Timeout in number of seconds.
  6801. *
  6802. * This function issues the mailbox to firmware and waits for the
  6803. * mailbox command to complete. If the mailbox command is not
  6804. * completed within timeout seconds, it returns MBX_TIMEOUT.
  6805. * The function waits for the mailbox completion using an
  6806. * interruptible wait. If the thread is woken up due to a
  6807. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  6808. * should not free the mailbox resources, if this function returns
  6809. * MBX_TIMEOUT.
  6810. * This function will sleep while waiting for mailbox completion.
  6811. * So, this function should not be called from any context which
  6812. * does not allow sleeping. Due to the same reason, this function
  6813. * cannot be called with interrupt disabled.
  6814. * This function assumes that the mailbox completion occurs while
  6815. * this function sleep. So, this function cannot be called from
  6816. * the worker thread which processes mailbox completion.
  6817. * This function is called in the context of HBA management
  6818. * applications.
  6819. * This function returns MBX_SUCCESS when successful.
  6820. * This function is called with no lock held.
  6821. **/
  6822. int
  6823. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  6824. uint32_t timeout)
  6825. {
  6826. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  6827. int retval;
  6828. unsigned long flag;
  6829. /* The caller must leave context1 empty. */
  6830. if (pmboxq->context1)
  6831. return MBX_NOT_FINISHED;
  6832. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  6833. /* setup wake call as IOCB callback */
  6834. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  6835. /* setup context field to pass wait_queue pointer to wake function */
  6836. pmboxq->context1 = &done_q;
  6837. /* now issue the command */
  6838. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  6839. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  6840. wait_event_interruptible_timeout(done_q,
  6841. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  6842. timeout * HZ);
  6843. spin_lock_irqsave(&phba->hbalock, flag);
  6844. pmboxq->context1 = NULL;
  6845. /*
  6846. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  6847. * else do not free the resources.
  6848. */
  6849. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  6850. retval = MBX_SUCCESS;
  6851. else {
  6852. retval = MBX_TIMEOUT;
  6853. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  6854. }
  6855. spin_unlock_irqrestore(&phba->hbalock, flag);
  6856. }
  6857. return retval;
  6858. }
  6859. /**
  6860. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  6861. * @phba: Pointer to HBA context.
  6862. *
  6863. * This function is called to shutdown the driver's mailbox sub-system.
  6864. * It first marks the mailbox sub-system is in a block state to prevent
  6865. * the asynchronous mailbox command from issued off the pending mailbox
  6866. * command queue. If the mailbox command sub-system shutdown is due to
  6867. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  6868. * the mailbox sub-system flush routine to forcefully bring down the
  6869. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  6870. * as with offline or HBA function reset), this routine will wait for the
  6871. * outstanding mailbox command to complete before invoking the mailbox
  6872. * sub-system flush routine to gracefully bring down mailbox sub-system.
  6873. **/
  6874. void
  6875. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  6876. {
  6877. struct lpfc_sli *psli = &phba->sli;
  6878. uint8_t actcmd = MBX_HEARTBEAT;
  6879. unsigned long timeout;
  6880. spin_lock_irq(&phba->hbalock);
  6881. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6882. spin_unlock_irq(&phba->hbalock);
  6883. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6884. spin_lock_irq(&phba->hbalock);
  6885. if (phba->sli.mbox_active)
  6886. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  6887. spin_unlock_irq(&phba->hbalock);
  6888. /* Determine how long we might wait for the active mailbox
  6889. * command to be gracefully completed by firmware.
  6890. */
  6891. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
  6892. 1000) + jiffies;
  6893. while (phba->sli.mbox_active) {
  6894. /* Check active mailbox complete status every 2ms */
  6895. msleep(2);
  6896. if (time_after(jiffies, timeout))
  6897. /* Timeout, let the mailbox flush routine to
  6898. * forcefully release active mailbox command
  6899. */
  6900. break;
  6901. }
  6902. }
  6903. lpfc_sli_mbox_sys_flush(phba);
  6904. }
  6905. /**
  6906. * lpfc_sli_eratt_read - read sli-3 error attention events
  6907. * @phba: Pointer to HBA context.
  6908. *
  6909. * This function is called to read the SLI3 device error attention registers
  6910. * for possible error attention events. The caller must hold the hostlock
  6911. * with spin_lock_irq().
  6912. *
  6913. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6914. * Register and returns 0 otherwise.
  6915. **/
  6916. static int
  6917. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  6918. {
  6919. uint32_t ha_copy;
  6920. /* Read chip Host Attention (HA) register */
  6921. ha_copy = readl(phba->HAregaddr);
  6922. if (ha_copy & HA_ERATT) {
  6923. /* Read host status register to retrieve error event */
  6924. lpfc_sli_read_hs(phba);
  6925. /* Check if there is a deferred error condition is active */
  6926. if ((HS_FFER1 & phba->work_hs) &&
  6927. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  6928. HS_FFER6 | HS_FFER7) & phba->work_hs)) {
  6929. phba->hba_flag |= DEFER_ERATT;
  6930. /* Clear all interrupt enable conditions */
  6931. writel(0, phba->HCregaddr);
  6932. readl(phba->HCregaddr);
  6933. }
  6934. /* Set the driver HA work bitmap */
  6935. phba->work_ha |= HA_ERATT;
  6936. /* Indicate polling handles this ERATT */
  6937. phba->hba_flag |= HBA_ERATT_HANDLED;
  6938. return 1;
  6939. }
  6940. return 0;
  6941. }
  6942. /**
  6943. * lpfc_sli4_eratt_read - read sli-4 error attention events
  6944. * @phba: Pointer to HBA context.
  6945. *
  6946. * This function is called to read the SLI4 device error attention registers
  6947. * for possible error attention events. The caller must hold the hostlock
  6948. * with spin_lock_irq().
  6949. *
  6950. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6951. * Register and returns 0 otherwise.
  6952. **/
  6953. static int
  6954. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  6955. {
  6956. uint32_t uerr_sta_hi, uerr_sta_lo;
  6957. uint32_t onlnreg0, onlnreg1;
  6958. /* For now, use the SLI4 device internal unrecoverable error
  6959. * registers for error attention. This can be changed later.
  6960. */
  6961. onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
  6962. onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
  6963. if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
  6964. uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
  6965. uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
  6966. if (uerr_sta_lo || uerr_sta_hi) {
  6967. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6968. "1423 HBA Unrecoverable error: "
  6969. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  6970. "online0_reg=0x%x, online1_reg=0x%x\n",
  6971. uerr_sta_lo, uerr_sta_hi,
  6972. onlnreg0, onlnreg1);
  6973. phba->work_status[0] = uerr_sta_lo;
  6974. phba->work_status[1] = uerr_sta_hi;
  6975. /* Set the driver HA work bitmap */
  6976. phba->work_ha |= HA_ERATT;
  6977. /* Indicate polling handles this ERATT */
  6978. phba->hba_flag |= HBA_ERATT_HANDLED;
  6979. return 1;
  6980. }
  6981. }
  6982. return 0;
  6983. }
  6984. /**
  6985. * lpfc_sli_check_eratt - check error attention events
  6986. * @phba: Pointer to HBA context.
  6987. *
  6988. * This function is called from timer soft interrupt context to check HBA's
  6989. * error attention register bit for error attention events.
  6990. *
  6991. * This fucntion returns 1 when there is Error Attention in the Host Attention
  6992. * Register and returns 0 otherwise.
  6993. **/
  6994. int
  6995. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  6996. {
  6997. uint32_t ha_copy;
  6998. /* If somebody is waiting to handle an eratt, don't process it
  6999. * here. The brdkill function will do this.
  7000. */
  7001. if (phba->link_flag & LS_IGNORE_ERATT)
  7002. return 0;
  7003. /* Check if interrupt handler handles this ERATT */
  7004. spin_lock_irq(&phba->hbalock);
  7005. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  7006. /* Interrupt handler has handled ERATT */
  7007. spin_unlock_irq(&phba->hbalock);
  7008. return 0;
  7009. }
  7010. /*
  7011. * If there is deferred error attention, do not check for error
  7012. * attention
  7013. */
  7014. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7015. spin_unlock_irq(&phba->hbalock);
  7016. return 0;
  7017. }
  7018. /* If PCI channel is offline, don't process it */
  7019. if (unlikely(pci_channel_offline(phba->pcidev))) {
  7020. spin_unlock_irq(&phba->hbalock);
  7021. return 0;
  7022. }
  7023. switch (phba->sli_rev) {
  7024. case LPFC_SLI_REV2:
  7025. case LPFC_SLI_REV3:
  7026. /* Read chip Host Attention (HA) register */
  7027. ha_copy = lpfc_sli_eratt_read(phba);
  7028. break;
  7029. case LPFC_SLI_REV4:
  7030. /* Read devcie Uncoverable Error (UERR) registers */
  7031. ha_copy = lpfc_sli4_eratt_read(phba);
  7032. break;
  7033. default:
  7034. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7035. "0299 Invalid SLI revision (%d)\n",
  7036. phba->sli_rev);
  7037. ha_copy = 0;
  7038. break;
  7039. }
  7040. spin_unlock_irq(&phba->hbalock);
  7041. return ha_copy;
  7042. }
  7043. /**
  7044. * lpfc_intr_state_check - Check device state for interrupt handling
  7045. * @phba: Pointer to HBA context.
  7046. *
  7047. * This inline routine checks whether a device or its PCI slot is in a state
  7048. * that the interrupt should be handled.
  7049. *
  7050. * This function returns 0 if the device or the PCI slot is in a state that
  7051. * interrupt should be handled, otherwise -EIO.
  7052. */
  7053. static inline int
  7054. lpfc_intr_state_check(struct lpfc_hba *phba)
  7055. {
  7056. /* If the pci channel is offline, ignore all the interrupts */
  7057. if (unlikely(pci_channel_offline(phba->pcidev)))
  7058. return -EIO;
  7059. /* Update device level interrupt statistics */
  7060. phba->sli.slistat.sli_intr++;
  7061. /* Ignore all interrupts during initialization. */
  7062. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7063. return -EIO;
  7064. return 0;
  7065. }
  7066. /**
  7067. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  7068. * @irq: Interrupt number.
  7069. * @dev_id: The device context pointer.
  7070. *
  7071. * This function is directly called from the PCI layer as an interrupt
  7072. * service routine when device with SLI-3 interface spec is enabled with
  7073. * MSI-X multi-message interrupt mode and there are slow-path events in
  7074. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  7075. * interrupt mode, this function is called as part of the device-level
  7076. * interrupt handler. When the PCI slot is in error recovery or the HBA
  7077. * is undergoing initialization, the interrupt handler will not process
  7078. * the interrupt. The link attention and ELS ring attention events are
  7079. * handled by the worker thread. The interrupt handler signals the worker
  7080. * thread and returns for these events. This function is called without
  7081. * any lock held. It gets the hbalock to access and update SLI data
  7082. * structures.
  7083. *
  7084. * This function returns IRQ_HANDLED when interrupt is handled else it
  7085. * returns IRQ_NONE.
  7086. **/
  7087. irqreturn_t
  7088. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  7089. {
  7090. struct lpfc_hba *phba;
  7091. uint32_t ha_copy;
  7092. uint32_t work_ha_copy;
  7093. unsigned long status;
  7094. unsigned long iflag;
  7095. uint32_t control;
  7096. MAILBOX_t *mbox, *pmbox;
  7097. struct lpfc_vport *vport;
  7098. struct lpfc_nodelist *ndlp;
  7099. struct lpfc_dmabuf *mp;
  7100. LPFC_MBOXQ_t *pmb;
  7101. int rc;
  7102. /*
  7103. * Get the driver's phba structure from the dev_id and
  7104. * assume the HBA is not interrupting.
  7105. */
  7106. phba = (struct lpfc_hba *)dev_id;
  7107. if (unlikely(!phba))
  7108. return IRQ_NONE;
  7109. /*
  7110. * Stuff needs to be attented to when this function is invoked as an
  7111. * individual interrupt handler in MSI-X multi-message interrupt mode
  7112. */
  7113. if (phba->intr_type == MSIX) {
  7114. /* Check device state for handling interrupt */
  7115. if (lpfc_intr_state_check(phba))
  7116. return IRQ_NONE;
  7117. /* Need to read HA REG for slow-path events */
  7118. spin_lock_irqsave(&phba->hbalock, iflag);
  7119. ha_copy = readl(phba->HAregaddr);
  7120. /* If somebody is waiting to handle an eratt don't process it
  7121. * here. The brdkill function will do this.
  7122. */
  7123. if (phba->link_flag & LS_IGNORE_ERATT)
  7124. ha_copy &= ~HA_ERATT;
  7125. /* Check the need for handling ERATT in interrupt handler */
  7126. if (ha_copy & HA_ERATT) {
  7127. if (phba->hba_flag & HBA_ERATT_HANDLED)
  7128. /* ERATT polling has handled ERATT */
  7129. ha_copy &= ~HA_ERATT;
  7130. else
  7131. /* Indicate interrupt handler handles ERATT */
  7132. phba->hba_flag |= HBA_ERATT_HANDLED;
  7133. }
  7134. /*
  7135. * If there is deferred error attention, do not check for any
  7136. * interrupt.
  7137. */
  7138. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7139. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7140. return IRQ_NONE;
  7141. }
  7142. /* Clear up only attention source related to slow-path */
  7143. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  7144. phba->HAregaddr);
  7145. readl(phba->HAregaddr); /* flush */
  7146. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7147. } else
  7148. ha_copy = phba->ha_copy;
  7149. work_ha_copy = ha_copy & phba->work_ha_mask;
  7150. if (work_ha_copy) {
  7151. if (work_ha_copy & HA_LATT) {
  7152. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  7153. /*
  7154. * Turn off Link Attention interrupts
  7155. * until CLEAR_LA done
  7156. */
  7157. spin_lock_irqsave(&phba->hbalock, iflag);
  7158. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  7159. control = readl(phba->HCregaddr);
  7160. control &= ~HC_LAINT_ENA;
  7161. writel(control, phba->HCregaddr);
  7162. readl(phba->HCregaddr); /* flush */
  7163. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7164. }
  7165. else
  7166. work_ha_copy &= ~HA_LATT;
  7167. }
  7168. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  7169. /*
  7170. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  7171. * the only slow ring.
  7172. */
  7173. status = (work_ha_copy &
  7174. (HA_RXMASK << (4*LPFC_ELS_RING)));
  7175. status >>= (4*LPFC_ELS_RING);
  7176. if (status & HA_RXMASK) {
  7177. spin_lock_irqsave(&phba->hbalock, iflag);
  7178. control = readl(phba->HCregaddr);
  7179. lpfc_debugfs_slow_ring_trc(phba,
  7180. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  7181. control, status,
  7182. (uint32_t)phba->sli.slistat.sli_intr);
  7183. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  7184. lpfc_debugfs_slow_ring_trc(phba,
  7185. "ISR Disable ring:"
  7186. "pwork:x%x hawork:x%x wait:x%x",
  7187. phba->work_ha, work_ha_copy,
  7188. (uint32_t)((unsigned long)
  7189. &phba->work_waitq));
  7190. control &=
  7191. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  7192. writel(control, phba->HCregaddr);
  7193. readl(phba->HCregaddr); /* flush */
  7194. }
  7195. else {
  7196. lpfc_debugfs_slow_ring_trc(phba,
  7197. "ISR slow ring: pwork:"
  7198. "x%x hawork:x%x wait:x%x",
  7199. phba->work_ha, work_ha_copy,
  7200. (uint32_t)((unsigned long)
  7201. &phba->work_waitq));
  7202. }
  7203. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7204. }
  7205. }
  7206. spin_lock_irqsave(&phba->hbalock, iflag);
  7207. if (work_ha_copy & HA_ERATT) {
  7208. lpfc_sli_read_hs(phba);
  7209. /*
  7210. * Check if there is a deferred error condition
  7211. * is active
  7212. */
  7213. if ((HS_FFER1 & phba->work_hs) &&
  7214. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  7215. HS_FFER6 | HS_FFER7) & phba->work_hs)) {
  7216. phba->hba_flag |= DEFER_ERATT;
  7217. /* Clear all interrupt enable conditions */
  7218. writel(0, phba->HCregaddr);
  7219. readl(phba->HCregaddr);
  7220. }
  7221. }
  7222. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  7223. pmb = phba->sli.mbox_active;
  7224. pmbox = &pmb->u.mb;
  7225. mbox = phba->mbox;
  7226. vport = pmb->vport;
  7227. /* First check out the status word */
  7228. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  7229. if (pmbox->mbxOwner != OWN_HOST) {
  7230. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7231. /*
  7232. * Stray Mailbox Interrupt, mbxCommand <cmd>
  7233. * mbxStatus <status>
  7234. */
  7235. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7236. LOG_SLI,
  7237. "(%d):0304 Stray Mailbox "
  7238. "Interrupt mbxCommand x%x "
  7239. "mbxStatus x%x\n",
  7240. (vport ? vport->vpi : 0),
  7241. pmbox->mbxCommand,
  7242. pmbox->mbxStatus);
  7243. /* clear mailbox attention bit */
  7244. work_ha_copy &= ~HA_MBATT;
  7245. } else {
  7246. phba->sli.mbox_active = NULL;
  7247. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7248. phba->last_completion_time = jiffies;
  7249. del_timer(&phba->sli.mbox_tmo);
  7250. if (pmb->mbox_cmpl) {
  7251. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  7252. MAILBOX_CMD_SIZE);
  7253. }
  7254. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  7255. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  7256. lpfc_debugfs_disc_trc(vport,
  7257. LPFC_DISC_TRC_MBOX_VPORT,
  7258. "MBOX dflt rpi: : "
  7259. "status:x%x rpi:x%x",
  7260. (uint32_t)pmbox->mbxStatus,
  7261. pmbox->un.varWords[0], 0);
  7262. if (!pmbox->mbxStatus) {
  7263. mp = (struct lpfc_dmabuf *)
  7264. (pmb->context1);
  7265. ndlp = (struct lpfc_nodelist *)
  7266. pmb->context2;
  7267. /* Reg_LOGIN of dflt RPI was
  7268. * successful. new lets get
  7269. * rid of the RPI using the
  7270. * same mbox buffer.
  7271. */
  7272. lpfc_unreg_login(phba,
  7273. vport->vpi,
  7274. pmbox->un.varWords[0],
  7275. pmb);
  7276. pmb->mbox_cmpl =
  7277. lpfc_mbx_cmpl_dflt_rpi;
  7278. pmb->context1 = mp;
  7279. pmb->context2 = ndlp;
  7280. pmb->vport = vport;
  7281. rc = lpfc_sli_issue_mbox(phba,
  7282. pmb,
  7283. MBX_NOWAIT);
  7284. if (rc != MBX_BUSY)
  7285. lpfc_printf_log(phba,
  7286. KERN_ERR,
  7287. LOG_MBOX | LOG_SLI,
  7288. "0350 rc should have"
  7289. "been MBX_BUSY");
  7290. if (rc != MBX_NOT_FINISHED)
  7291. goto send_current_mbox;
  7292. }
  7293. }
  7294. spin_lock_irqsave(
  7295. &phba->pport->work_port_lock,
  7296. iflag);
  7297. phba->pport->work_port_events &=
  7298. ~WORKER_MBOX_TMO;
  7299. spin_unlock_irqrestore(
  7300. &phba->pport->work_port_lock,
  7301. iflag);
  7302. lpfc_mbox_cmpl_put(phba, pmb);
  7303. }
  7304. } else
  7305. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7306. if ((work_ha_copy & HA_MBATT) &&
  7307. (phba->sli.mbox_active == NULL)) {
  7308. send_current_mbox:
  7309. /* Process next mailbox command if there is one */
  7310. do {
  7311. rc = lpfc_sli_issue_mbox(phba, NULL,
  7312. MBX_NOWAIT);
  7313. } while (rc == MBX_NOT_FINISHED);
  7314. if (rc != MBX_SUCCESS)
  7315. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7316. LOG_SLI, "0349 rc should be "
  7317. "MBX_SUCCESS");
  7318. }
  7319. spin_lock_irqsave(&phba->hbalock, iflag);
  7320. phba->work_ha |= work_ha_copy;
  7321. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7322. lpfc_worker_wake_up(phba);
  7323. }
  7324. return IRQ_HANDLED;
  7325. } /* lpfc_sli_sp_intr_handler */
  7326. /**
  7327. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  7328. * @irq: Interrupt number.
  7329. * @dev_id: The device context pointer.
  7330. *
  7331. * This function is directly called from the PCI layer as an interrupt
  7332. * service routine when device with SLI-3 interface spec is enabled with
  7333. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  7334. * ring event in the HBA. However, when the device is enabled with either
  7335. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  7336. * device-level interrupt handler. When the PCI slot is in error recovery
  7337. * or the HBA is undergoing initialization, the interrupt handler will not
  7338. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  7339. * the intrrupt context. This function is called without any lock held.
  7340. * It gets the hbalock to access and update SLI data structures.
  7341. *
  7342. * This function returns IRQ_HANDLED when interrupt is handled else it
  7343. * returns IRQ_NONE.
  7344. **/
  7345. irqreturn_t
  7346. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  7347. {
  7348. struct lpfc_hba *phba;
  7349. uint32_t ha_copy;
  7350. unsigned long status;
  7351. unsigned long iflag;
  7352. /* Get the driver's phba structure from the dev_id and
  7353. * assume the HBA is not interrupting.
  7354. */
  7355. phba = (struct lpfc_hba *) dev_id;
  7356. if (unlikely(!phba))
  7357. return IRQ_NONE;
  7358. /*
  7359. * Stuff needs to be attented to when this function is invoked as an
  7360. * individual interrupt handler in MSI-X multi-message interrupt mode
  7361. */
  7362. if (phba->intr_type == MSIX) {
  7363. /* Check device state for handling interrupt */
  7364. if (lpfc_intr_state_check(phba))
  7365. return IRQ_NONE;
  7366. /* Need to read HA REG for FCP ring and other ring events */
  7367. ha_copy = readl(phba->HAregaddr);
  7368. /* Clear up only attention source related to fast-path */
  7369. spin_lock_irqsave(&phba->hbalock, iflag);
  7370. /*
  7371. * If there is deferred error attention, do not check for
  7372. * any interrupt.
  7373. */
  7374. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7375. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7376. return IRQ_NONE;
  7377. }
  7378. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  7379. phba->HAregaddr);
  7380. readl(phba->HAregaddr); /* flush */
  7381. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7382. } else
  7383. ha_copy = phba->ha_copy;
  7384. /*
  7385. * Process all events on FCP ring. Take the optimized path for FCP IO.
  7386. */
  7387. ha_copy &= ~(phba->work_ha_mask);
  7388. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  7389. status >>= (4*LPFC_FCP_RING);
  7390. if (status & HA_RXMASK)
  7391. lpfc_sli_handle_fast_ring_event(phba,
  7392. &phba->sli.ring[LPFC_FCP_RING],
  7393. status);
  7394. if (phba->cfg_multi_ring_support == 2) {
  7395. /*
  7396. * Process all events on extra ring. Take the optimized path
  7397. * for extra ring IO.
  7398. */
  7399. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  7400. status >>= (4*LPFC_EXTRA_RING);
  7401. if (status & HA_RXMASK) {
  7402. lpfc_sli_handle_fast_ring_event(phba,
  7403. &phba->sli.ring[LPFC_EXTRA_RING],
  7404. status);
  7405. }
  7406. }
  7407. return IRQ_HANDLED;
  7408. } /* lpfc_sli_fp_intr_handler */
  7409. /**
  7410. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  7411. * @irq: Interrupt number.
  7412. * @dev_id: The device context pointer.
  7413. *
  7414. * This function is the HBA device-level interrupt handler to device with
  7415. * SLI-3 interface spec, called from the PCI layer when either MSI or
  7416. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  7417. * requires driver attention. This function invokes the slow-path interrupt
  7418. * attention handling function and fast-path interrupt attention handling
  7419. * function in turn to process the relevant HBA attention events. This
  7420. * function is called without any lock held. It gets the hbalock to access
  7421. * and update SLI data structures.
  7422. *
  7423. * This function returns IRQ_HANDLED when interrupt is handled, else it
  7424. * returns IRQ_NONE.
  7425. **/
  7426. irqreturn_t
  7427. lpfc_sli_intr_handler(int irq, void *dev_id)
  7428. {
  7429. struct lpfc_hba *phba;
  7430. irqreturn_t sp_irq_rc, fp_irq_rc;
  7431. unsigned long status1, status2;
  7432. /*
  7433. * Get the driver's phba structure from the dev_id and
  7434. * assume the HBA is not interrupting.
  7435. */
  7436. phba = (struct lpfc_hba *) dev_id;
  7437. if (unlikely(!phba))
  7438. return IRQ_NONE;
  7439. /* Check device state for handling interrupt */
  7440. if (lpfc_intr_state_check(phba))
  7441. return IRQ_NONE;
  7442. spin_lock(&phba->hbalock);
  7443. phba->ha_copy = readl(phba->HAregaddr);
  7444. if (unlikely(!phba->ha_copy)) {
  7445. spin_unlock(&phba->hbalock);
  7446. return IRQ_NONE;
  7447. } else if (phba->ha_copy & HA_ERATT) {
  7448. if (phba->hba_flag & HBA_ERATT_HANDLED)
  7449. /* ERATT polling has handled ERATT */
  7450. phba->ha_copy &= ~HA_ERATT;
  7451. else
  7452. /* Indicate interrupt handler handles ERATT */
  7453. phba->hba_flag |= HBA_ERATT_HANDLED;
  7454. }
  7455. /*
  7456. * If there is deferred error attention, do not check for any interrupt.
  7457. */
  7458. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  7459. spin_unlock_irq(&phba->hbalock);
  7460. return IRQ_NONE;
  7461. }
  7462. /* Clear attention sources except link and error attentions */
  7463. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  7464. readl(phba->HAregaddr); /* flush */
  7465. spin_unlock(&phba->hbalock);
  7466. /*
  7467. * Invokes slow-path host attention interrupt handling as appropriate.
  7468. */
  7469. /* status of events with mailbox and link attention */
  7470. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  7471. /* status of events with ELS ring */
  7472. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  7473. status2 >>= (4*LPFC_ELS_RING);
  7474. if (status1 || (status2 & HA_RXMASK))
  7475. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  7476. else
  7477. sp_irq_rc = IRQ_NONE;
  7478. /*
  7479. * Invoke fast-path host attention interrupt handling as appropriate.
  7480. */
  7481. /* status of events with FCP ring */
  7482. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  7483. status1 >>= (4*LPFC_FCP_RING);
  7484. /* status of events with extra ring */
  7485. if (phba->cfg_multi_ring_support == 2) {
  7486. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  7487. status2 >>= (4*LPFC_EXTRA_RING);
  7488. } else
  7489. status2 = 0;
  7490. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  7491. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  7492. else
  7493. fp_irq_rc = IRQ_NONE;
  7494. /* Return device-level interrupt handling status */
  7495. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  7496. } /* lpfc_sli_intr_handler */
  7497. /**
  7498. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  7499. * @phba: pointer to lpfc hba data structure.
  7500. *
  7501. * This routine is invoked by the worker thread to process all the pending
  7502. * SLI4 FCP abort XRI events.
  7503. **/
  7504. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  7505. {
  7506. struct lpfc_cq_event *cq_event;
  7507. /* First, declare the fcp xri abort event has been handled */
  7508. spin_lock_irq(&phba->hbalock);
  7509. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  7510. spin_unlock_irq(&phba->hbalock);
  7511. /* Now, handle all the fcp xri abort events */
  7512. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  7513. /* Get the first event from the head of the event queue */
  7514. spin_lock_irq(&phba->hbalock);
  7515. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  7516. cq_event, struct lpfc_cq_event, list);
  7517. spin_unlock_irq(&phba->hbalock);
  7518. /* Notify aborted XRI for FCP work queue */
  7519. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  7520. /* Free the event processed back to the free pool */
  7521. lpfc_sli4_cq_event_release(phba, cq_event);
  7522. }
  7523. }
  7524. /**
  7525. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  7526. * @phba: pointer to lpfc hba data structure.
  7527. *
  7528. * This routine is invoked by the worker thread to process all the pending
  7529. * SLI4 els abort xri events.
  7530. **/
  7531. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  7532. {
  7533. struct lpfc_cq_event *cq_event;
  7534. /* First, declare the els xri abort event has been handled */
  7535. spin_lock_irq(&phba->hbalock);
  7536. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  7537. spin_unlock_irq(&phba->hbalock);
  7538. /* Now, handle all the els xri abort events */
  7539. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  7540. /* Get the first event from the head of the event queue */
  7541. spin_lock_irq(&phba->hbalock);
  7542. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  7543. cq_event, struct lpfc_cq_event, list);
  7544. spin_unlock_irq(&phba->hbalock);
  7545. /* Notify aborted XRI for ELS work queue */
  7546. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  7547. /* Free the event processed back to the free pool */
  7548. lpfc_sli4_cq_event_release(phba, cq_event);
  7549. }
  7550. }
  7551. static void
  7552. lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
  7553. struct lpfc_iocbq *pIocbOut,
  7554. struct lpfc_wcqe_complete *wcqe)
  7555. {
  7556. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  7557. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  7558. sizeof(struct lpfc_iocbq) - offset);
  7559. memset(&pIocbIn->sli4_info, 0,
  7560. sizeof(struct lpfc_sli4_rspiocb_info));
  7561. /* Map WCQE parameters into irspiocb parameters */
  7562. pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
  7563. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  7564. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  7565. pIocbIn->iocb.un.fcpi.fcpi_parm =
  7566. pIocbOut->iocb.un.fcpi.fcpi_parm -
  7567. wcqe->total_data_placed;
  7568. else
  7569. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  7570. else
  7571. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  7572. /* Load in additional WCQE parameters */
  7573. pIocbIn->sli4_info.hw_status = bf_get(lpfc_wcqe_c_hw_status, wcqe);
  7574. pIocbIn->sli4_info.bfield = 0;
  7575. if (bf_get(lpfc_wcqe_c_xb, wcqe))
  7576. pIocbIn->sli4_info.bfield |= LPFC_XB;
  7577. if (bf_get(lpfc_wcqe_c_pv, wcqe)) {
  7578. pIocbIn->sli4_info.bfield |= LPFC_PV;
  7579. pIocbIn->sli4_info.priority =
  7580. bf_get(lpfc_wcqe_c_priority, wcqe);
  7581. }
  7582. }
  7583. /**
  7584. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  7585. * @phba: Pointer to HBA context object.
  7586. * @cqe: Pointer to mailbox completion queue entry.
  7587. *
  7588. * This routine process a mailbox completion queue entry with asynchrous
  7589. * event.
  7590. *
  7591. * Return: true if work posted to worker thread, otherwise false.
  7592. **/
  7593. static bool
  7594. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  7595. {
  7596. struct lpfc_cq_event *cq_event;
  7597. unsigned long iflags;
  7598. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7599. "0392 Async Event: word0:x%x, word1:x%x, "
  7600. "word2:x%x, word3:x%x\n", mcqe->word0,
  7601. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  7602. /* Allocate a new internal CQ_EVENT entry */
  7603. cq_event = lpfc_sli4_cq_event_alloc(phba);
  7604. if (!cq_event) {
  7605. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7606. "0394 Failed to allocate CQ_EVENT entry\n");
  7607. return false;
  7608. }
  7609. /* Move the CQE into an asynchronous event entry */
  7610. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  7611. spin_lock_irqsave(&phba->hbalock, iflags);
  7612. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  7613. /* Set the async event flag */
  7614. phba->hba_flag |= ASYNC_EVENT;
  7615. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7616. return true;
  7617. }
  7618. /**
  7619. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  7620. * @phba: Pointer to HBA context object.
  7621. * @cqe: Pointer to mailbox completion queue entry.
  7622. *
  7623. * This routine process a mailbox completion queue entry with mailbox
  7624. * completion event.
  7625. *
  7626. * Return: true if work posted to worker thread, otherwise false.
  7627. **/
  7628. static bool
  7629. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  7630. {
  7631. uint32_t mcqe_status;
  7632. MAILBOX_t *mbox, *pmbox;
  7633. struct lpfc_mqe *mqe;
  7634. struct lpfc_vport *vport;
  7635. struct lpfc_nodelist *ndlp;
  7636. struct lpfc_dmabuf *mp;
  7637. unsigned long iflags;
  7638. LPFC_MBOXQ_t *pmb;
  7639. bool workposted = false;
  7640. int rc;
  7641. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  7642. if (!bf_get(lpfc_trailer_completed, mcqe))
  7643. goto out_no_mqe_complete;
  7644. /* Get the reference to the active mbox command */
  7645. spin_lock_irqsave(&phba->hbalock, iflags);
  7646. pmb = phba->sli.mbox_active;
  7647. if (unlikely(!pmb)) {
  7648. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  7649. "1832 No pending MBOX command to handle\n");
  7650. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7651. goto out_no_mqe_complete;
  7652. }
  7653. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7654. mqe = &pmb->u.mqe;
  7655. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  7656. mbox = phba->mbox;
  7657. vport = pmb->vport;
  7658. /* Reset heartbeat timer */
  7659. phba->last_completion_time = jiffies;
  7660. del_timer(&phba->sli.mbox_tmo);
  7661. /* Move mbox data to caller's mailbox region, do endian swapping */
  7662. if (pmb->mbox_cmpl && mbox)
  7663. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  7664. /* Set the mailbox status with SLI4 range 0x4000 */
  7665. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  7666. if (mcqe_status != MB_CQE_STATUS_SUCCESS)
  7667. bf_set(lpfc_mqe_status, mqe,
  7668. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  7669. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  7670. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  7671. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  7672. "MBOX dflt rpi: status:x%x rpi:x%x",
  7673. mcqe_status,
  7674. pmbox->un.varWords[0], 0);
  7675. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  7676. mp = (struct lpfc_dmabuf *)(pmb->context1);
  7677. ndlp = (struct lpfc_nodelist *)pmb->context2;
  7678. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  7679. * RID of the PPI using the same mbox buffer.
  7680. */
  7681. lpfc_unreg_login(phba, vport->vpi,
  7682. pmbox->un.varWords[0], pmb);
  7683. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  7684. pmb->context1 = mp;
  7685. pmb->context2 = ndlp;
  7686. pmb->vport = vport;
  7687. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  7688. if (rc != MBX_BUSY)
  7689. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  7690. LOG_SLI, "0385 rc should "
  7691. "have been MBX_BUSY\n");
  7692. if (rc != MBX_NOT_FINISHED)
  7693. goto send_current_mbox;
  7694. }
  7695. }
  7696. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  7697. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  7698. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  7699. /* There is mailbox completion work to do */
  7700. spin_lock_irqsave(&phba->hbalock, iflags);
  7701. __lpfc_mbox_cmpl_put(phba, pmb);
  7702. phba->work_ha |= HA_MBATT;
  7703. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7704. workposted = true;
  7705. send_current_mbox:
  7706. spin_lock_irqsave(&phba->hbalock, iflags);
  7707. /* Release the mailbox command posting token */
  7708. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7709. /* Setting active mailbox pointer need to be in sync to flag clear */
  7710. phba->sli.mbox_active = NULL;
  7711. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7712. /* Wake up worker thread to post the next pending mailbox command */
  7713. lpfc_worker_wake_up(phba);
  7714. out_no_mqe_complete:
  7715. if (bf_get(lpfc_trailer_consumed, mcqe))
  7716. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  7717. return workposted;
  7718. }
  7719. /**
  7720. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  7721. * @phba: Pointer to HBA context object.
  7722. * @cqe: Pointer to mailbox completion queue entry.
  7723. *
  7724. * This routine process a mailbox completion queue entry, it invokes the
  7725. * proper mailbox complete handling or asynchrous event handling routine
  7726. * according to the MCQE's async bit.
  7727. *
  7728. * Return: true if work posted to worker thread, otherwise false.
  7729. **/
  7730. static bool
  7731. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  7732. {
  7733. struct lpfc_mcqe mcqe;
  7734. bool workposted;
  7735. /* Copy the mailbox MCQE and convert endian order as needed */
  7736. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  7737. /* Invoke the proper event handling routine */
  7738. if (!bf_get(lpfc_trailer_async, &mcqe))
  7739. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  7740. else
  7741. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  7742. return workposted;
  7743. }
  7744. /**
  7745. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  7746. * @phba: Pointer to HBA context object.
  7747. * @wcqe: Pointer to work-queue completion queue entry.
  7748. *
  7749. * This routine handles an ELS work-queue completion event.
  7750. *
  7751. * Return: true if work posted to worker thread, otherwise false.
  7752. **/
  7753. static bool
  7754. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  7755. struct lpfc_wcqe_complete *wcqe)
  7756. {
  7757. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  7758. struct lpfc_iocbq *cmdiocbq;
  7759. struct lpfc_iocbq *irspiocbq;
  7760. unsigned long iflags;
  7761. bool workposted = false;
  7762. spin_lock_irqsave(&phba->hbalock, iflags);
  7763. pring->stats.iocb_event++;
  7764. /* Look up the ELS command IOCB and create pseudo response IOCB */
  7765. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  7766. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  7767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7768. if (unlikely(!cmdiocbq)) {
  7769. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7770. "0386 ELS complete with no corresponding "
  7771. "cmdiocb: iotag (%d)\n",
  7772. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  7773. return workposted;
  7774. }
  7775. /* Fake the irspiocbq and copy necessary response information */
  7776. irspiocbq = lpfc_sli_get_iocbq(phba);
  7777. if (!irspiocbq) {
  7778. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7779. "0387 Failed to allocate an iocbq\n");
  7780. return workposted;
  7781. }
  7782. lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
  7783. /* Add the irspiocb to the response IOCB work list */
  7784. spin_lock_irqsave(&phba->hbalock, iflags);
  7785. list_add_tail(&irspiocbq->list, &phba->sli4_hba.sp_rspiocb_work_queue);
  7786. /* Indicate ELS ring attention */
  7787. phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
  7788. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7789. workposted = true;
  7790. return workposted;
  7791. }
  7792. /**
  7793. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  7794. * @phba: Pointer to HBA context object.
  7795. * @wcqe: Pointer to work-queue completion queue entry.
  7796. *
  7797. * This routine handles slow-path WQ entry comsumed event by invoking the
  7798. * proper WQ release routine to the slow-path WQ.
  7799. **/
  7800. static void
  7801. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  7802. struct lpfc_wcqe_release *wcqe)
  7803. {
  7804. /* Check for the slow-path ELS work queue */
  7805. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  7806. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  7807. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  7808. else
  7809. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7810. "2579 Slow-path wqe consume event carries "
  7811. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  7812. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  7813. phba->sli4_hba.els_wq->queue_id);
  7814. }
  7815. /**
  7816. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  7817. * @phba: Pointer to HBA context object.
  7818. * @cq: Pointer to a WQ completion queue.
  7819. * @wcqe: Pointer to work-queue completion queue entry.
  7820. *
  7821. * This routine handles an XRI abort event.
  7822. *
  7823. * Return: true if work posted to worker thread, otherwise false.
  7824. **/
  7825. static bool
  7826. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  7827. struct lpfc_queue *cq,
  7828. struct sli4_wcqe_xri_aborted *wcqe)
  7829. {
  7830. bool workposted = false;
  7831. struct lpfc_cq_event *cq_event;
  7832. unsigned long iflags;
  7833. /* Allocate a new internal CQ_EVENT entry */
  7834. cq_event = lpfc_sli4_cq_event_alloc(phba);
  7835. if (!cq_event) {
  7836. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7837. "0602 Failed to allocate CQ_EVENT entry\n");
  7838. return false;
  7839. }
  7840. /* Move the CQE into the proper xri abort event list */
  7841. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  7842. switch (cq->subtype) {
  7843. case LPFC_FCP:
  7844. spin_lock_irqsave(&phba->hbalock, iflags);
  7845. list_add_tail(&cq_event->list,
  7846. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  7847. /* Set the fcp xri abort event flag */
  7848. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  7849. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7850. workposted = true;
  7851. break;
  7852. case LPFC_ELS:
  7853. spin_lock_irqsave(&phba->hbalock, iflags);
  7854. list_add_tail(&cq_event->list,
  7855. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  7856. /* Set the els xri abort event flag */
  7857. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  7858. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7859. workposted = true;
  7860. break;
  7861. default:
  7862. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7863. "0603 Invalid work queue CQE subtype (x%x)\n",
  7864. cq->subtype);
  7865. workposted = false;
  7866. break;
  7867. }
  7868. return workposted;
  7869. }
  7870. /**
  7871. * lpfc_sli4_sp_handle_wcqe - Process a work-queue completion queue entry
  7872. * @phba: Pointer to HBA context object.
  7873. * @cq: Pointer to the completion queue.
  7874. * @wcqe: Pointer to a completion queue entry.
  7875. *
  7876. * This routine process a slow-path work-queue completion queue entry.
  7877. *
  7878. * Return: true if work posted to worker thread, otherwise false.
  7879. **/
  7880. static bool
  7881. lpfc_sli4_sp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  7882. struct lpfc_cqe *cqe)
  7883. {
  7884. struct lpfc_wcqe_complete wcqe;
  7885. bool workposted = false;
  7886. /* Copy the work queue CQE and convert endian order if needed */
  7887. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  7888. /* Check and process for different type of WCQE and dispatch */
  7889. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  7890. case CQE_CODE_COMPL_WQE:
  7891. /* Process the WQ complete event */
  7892. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  7893. (struct lpfc_wcqe_complete *)&wcqe);
  7894. break;
  7895. case CQE_CODE_RELEASE_WQE:
  7896. /* Process the WQ release event */
  7897. lpfc_sli4_sp_handle_rel_wcqe(phba,
  7898. (struct lpfc_wcqe_release *)&wcqe);
  7899. break;
  7900. case CQE_CODE_XRI_ABORTED:
  7901. /* Process the WQ XRI abort event */
  7902. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  7903. (struct sli4_wcqe_xri_aborted *)&wcqe);
  7904. break;
  7905. default:
  7906. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7907. "0388 Not a valid WCQE code: x%x\n",
  7908. bf_get(lpfc_wcqe_c_code, &wcqe));
  7909. break;
  7910. }
  7911. return workposted;
  7912. }
  7913. /**
  7914. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  7915. * @phba: Pointer to HBA context object.
  7916. * @rcqe: Pointer to receive-queue completion queue entry.
  7917. *
  7918. * This routine process a receive-queue completion queue entry.
  7919. *
  7920. * Return: true if work posted to worker thread, otherwise false.
  7921. **/
  7922. static bool
  7923. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  7924. {
  7925. struct lpfc_rcqe rcqe;
  7926. bool workposted = false;
  7927. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  7928. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  7929. struct hbq_dmabuf *dma_buf;
  7930. uint32_t status;
  7931. unsigned long iflags;
  7932. /* Copy the receive queue CQE and convert endian order if needed */
  7933. lpfc_sli_pcimem_bcopy(cqe, &rcqe, sizeof(struct lpfc_rcqe));
  7934. lpfc_sli4_rq_release(hrq, drq);
  7935. if (bf_get(lpfc_rcqe_code, &rcqe) != CQE_CODE_RECEIVE)
  7936. goto out;
  7937. if (bf_get(lpfc_rcqe_rq_id, &rcqe) != hrq->queue_id)
  7938. goto out;
  7939. status = bf_get(lpfc_rcqe_status, &rcqe);
  7940. switch (status) {
  7941. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  7942. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7943. "2537 Receive Frame Truncated!!\n");
  7944. case FC_STATUS_RQ_SUCCESS:
  7945. spin_lock_irqsave(&phba->hbalock, iflags);
  7946. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  7947. if (!dma_buf) {
  7948. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7949. goto out;
  7950. }
  7951. memcpy(&dma_buf->rcqe, &rcqe, sizeof(rcqe));
  7952. /* save off the frame for the word thread to process */
  7953. list_add_tail(&dma_buf->dbuf.list, &phba->rb_pend_list);
  7954. /* Frame received */
  7955. phba->hba_flag |= HBA_RECEIVE_BUFFER;
  7956. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7957. workposted = true;
  7958. break;
  7959. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  7960. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  7961. /* Post more buffers if possible */
  7962. spin_lock_irqsave(&phba->hbalock, iflags);
  7963. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  7964. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7965. workposted = true;
  7966. break;
  7967. }
  7968. out:
  7969. return workposted;
  7970. }
  7971. /**
  7972. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  7973. * @phba: Pointer to HBA context object.
  7974. * @eqe: Pointer to fast-path event queue entry.
  7975. *
  7976. * This routine process a event queue entry from the slow-path event queue.
  7977. * It will check the MajorCode and MinorCode to determine this is for a
  7978. * completion event on a completion queue, if not, an error shall be logged
  7979. * and just return. Otherwise, it will get to the corresponding completion
  7980. * queue and process all the entries on that completion queue, rearm the
  7981. * completion queue, and then return.
  7982. *
  7983. **/
  7984. static void
  7985. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  7986. {
  7987. struct lpfc_queue *cq = NULL, *childq, *speq;
  7988. struct lpfc_cqe *cqe;
  7989. bool workposted = false;
  7990. int ecount = 0;
  7991. uint16_t cqid;
  7992. if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
  7993. bf_get(lpfc_eqe_minor_code, eqe) != 0) {
  7994. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7995. "0359 Not a valid slow-path completion "
  7996. "event: majorcode=x%x, minorcode=x%x\n",
  7997. bf_get(lpfc_eqe_major_code, eqe),
  7998. bf_get(lpfc_eqe_minor_code, eqe));
  7999. return;
  8000. }
  8001. /* Get the reference to the corresponding CQ */
  8002. cqid = bf_get(lpfc_eqe_resource_id, eqe);
  8003. /* Search for completion queue pointer matching this cqid */
  8004. speq = phba->sli4_hba.sp_eq;
  8005. list_for_each_entry(childq, &speq->child_list, list) {
  8006. if (childq->queue_id == cqid) {
  8007. cq = childq;
  8008. break;
  8009. }
  8010. }
  8011. if (unlikely(!cq)) {
  8012. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8013. "0365 Slow-path CQ identifier (%d) does "
  8014. "not exist\n", cqid);
  8015. return;
  8016. }
  8017. /* Process all the entries to the CQ */
  8018. switch (cq->type) {
  8019. case LPFC_MCQ:
  8020. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8021. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  8022. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8023. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8024. }
  8025. break;
  8026. case LPFC_WCQ:
  8027. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8028. workposted |= lpfc_sli4_sp_handle_wcqe(phba, cq, cqe);
  8029. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8030. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8031. }
  8032. break;
  8033. case LPFC_RCQ:
  8034. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8035. workposted |= lpfc_sli4_sp_handle_rcqe(phba, cqe);
  8036. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8037. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8038. }
  8039. break;
  8040. default:
  8041. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8042. "0370 Invalid completion queue type (%d)\n",
  8043. cq->type);
  8044. return;
  8045. }
  8046. /* Catch the no cq entry condition, log an error */
  8047. if (unlikely(ecount == 0))
  8048. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8049. "0371 No entry from the CQ: identifier "
  8050. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  8051. /* In any case, flash and re-arm the RCQ */
  8052. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  8053. /* wake up worker thread if there are works to be done */
  8054. if (workposted)
  8055. lpfc_worker_wake_up(phba);
  8056. }
  8057. /**
  8058. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  8059. * @eqe: Pointer to fast-path completion queue entry.
  8060. *
  8061. * This routine process a fast-path work queue completion entry from fast-path
  8062. * event queue for FCP command response completion.
  8063. **/
  8064. static void
  8065. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  8066. struct lpfc_wcqe_complete *wcqe)
  8067. {
  8068. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  8069. struct lpfc_iocbq *cmdiocbq;
  8070. struct lpfc_iocbq irspiocbq;
  8071. unsigned long iflags;
  8072. spin_lock_irqsave(&phba->hbalock, iflags);
  8073. pring->stats.iocb_event++;
  8074. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8075. /* Check for response status */
  8076. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  8077. /* If resource errors reported from HBA, reduce queue
  8078. * depth of the SCSI device.
  8079. */
  8080. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  8081. IOSTAT_LOCAL_REJECT) &&
  8082. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  8083. phba->lpfc_rampdown_queue_depth(phba);
  8084. }
  8085. /* Log the error status */
  8086. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8087. "0373 FCP complete error: status=x%x, "
  8088. "hw_status=x%x, total_data_specified=%d, "
  8089. "parameter=x%x, word3=x%x\n",
  8090. bf_get(lpfc_wcqe_c_status, wcqe),
  8091. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  8092. wcqe->total_data_placed, wcqe->parameter,
  8093. wcqe->word3);
  8094. }
  8095. /* Look up the FCP command IOCB and create pseudo response IOCB */
  8096. spin_lock_irqsave(&phba->hbalock, iflags);
  8097. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  8098. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8099. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8100. if (unlikely(!cmdiocbq)) {
  8101. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8102. "0374 FCP complete with no corresponding "
  8103. "cmdiocb: iotag (%d)\n",
  8104. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8105. return;
  8106. }
  8107. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  8108. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8109. "0375 FCP cmdiocb not callback function "
  8110. "iotag: (%d)\n",
  8111. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  8112. return;
  8113. }
  8114. /* Fake the irspiocb and copy necessary response information */
  8115. lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
  8116. /* Pass the cmd_iocb and the rsp state to the upper layer */
  8117. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  8118. }
  8119. /**
  8120. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  8121. * @phba: Pointer to HBA context object.
  8122. * @cq: Pointer to completion queue.
  8123. * @wcqe: Pointer to work-queue completion queue entry.
  8124. *
  8125. * This routine handles an fast-path WQ entry comsumed event by invoking the
  8126. * proper WQ release routine to the slow-path WQ.
  8127. **/
  8128. static void
  8129. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8130. struct lpfc_wcqe_release *wcqe)
  8131. {
  8132. struct lpfc_queue *childwq;
  8133. bool wqid_matched = false;
  8134. uint16_t fcp_wqid;
  8135. /* Check for fast-path FCP work queue release */
  8136. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  8137. list_for_each_entry(childwq, &cq->child_list, list) {
  8138. if (childwq->queue_id == fcp_wqid) {
  8139. lpfc_sli4_wq_release(childwq,
  8140. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  8141. wqid_matched = true;
  8142. break;
  8143. }
  8144. }
  8145. /* Report warning log message if no match found */
  8146. if (wqid_matched != true)
  8147. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8148. "2580 Fast-path wqe consume event carries "
  8149. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  8150. }
  8151. /**
  8152. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  8153. * @cq: Pointer to the completion queue.
  8154. * @eqe: Pointer to fast-path completion queue entry.
  8155. *
  8156. * This routine process a fast-path work queue completion entry from fast-path
  8157. * event queue for FCP command response completion.
  8158. **/
  8159. static int
  8160. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8161. struct lpfc_cqe *cqe)
  8162. {
  8163. struct lpfc_wcqe_release wcqe;
  8164. bool workposted = false;
  8165. /* Copy the work queue CQE and convert endian order if needed */
  8166. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  8167. /* Check and process for different type of WCQE and dispatch */
  8168. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  8169. case CQE_CODE_COMPL_WQE:
  8170. /* Process the WQ complete event */
  8171. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  8172. (struct lpfc_wcqe_complete *)&wcqe);
  8173. break;
  8174. case CQE_CODE_RELEASE_WQE:
  8175. /* Process the WQ release event */
  8176. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  8177. (struct lpfc_wcqe_release *)&wcqe);
  8178. break;
  8179. case CQE_CODE_XRI_ABORTED:
  8180. /* Process the WQ XRI abort event */
  8181. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  8182. (struct sli4_wcqe_xri_aborted *)&wcqe);
  8183. break;
  8184. default:
  8185. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8186. "0144 Not a valid WCQE code: x%x\n",
  8187. bf_get(lpfc_wcqe_c_code, &wcqe));
  8188. break;
  8189. }
  8190. return workposted;
  8191. }
  8192. /**
  8193. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  8194. * @phba: Pointer to HBA context object.
  8195. * @eqe: Pointer to fast-path event queue entry.
  8196. *
  8197. * This routine process a event queue entry from the fast-path event queue.
  8198. * It will check the MajorCode and MinorCode to determine this is for a
  8199. * completion event on a completion queue, if not, an error shall be logged
  8200. * and just return. Otherwise, it will get to the corresponding completion
  8201. * queue and process all the entries on the completion queue, rearm the
  8202. * completion queue, and then return.
  8203. **/
  8204. static void
  8205. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  8206. uint32_t fcp_cqidx)
  8207. {
  8208. struct lpfc_queue *cq;
  8209. struct lpfc_cqe *cqe;
  8210. bool workposted = false;
  8211. uint16_t cqid;
  8212. int ecount = 0;
  8213. if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
  8214. unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
  8215. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8216. "0366 Not a valid fast-path completion "
  8217. "event: majorcode=x%x, minorcode=x%x\n",
  8218. bf_get(lpfc_eqe_major_code, eqe),
  8219. bf_get(lpfc_eqe_minor_code, eqe));
  8220. return;
  8221. }
  8222. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  8223. if (unlikely(!cq)) {
  8224. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8225. "0367 Fast-path completion queue does not "
  8226. "exist\n");
  8227. return;
  8228. }
  8229. /* Get the reference to the corresponding CQ */
  8230. cqid = bf_get(lpfc_eqe_resource_id, eqe);
  8231. if (unlikely(cqid != cq->queue_id)) {
  8232. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8233. "0368 Miss-matched fast-path completion "
  8234. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  8235. cqid, cq->queue_id);
  8236. return;
  8237. }
  8238. /* Process all the entries to the CQ */
  8239. while ((cqe = lpfc_sli4_cq_get(cq))) {
  8240. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  8241. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8242. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  8243. }
  8244. /* Catch the no cq entry condition */
  8245. if (unlikely(ecount == 0))
  8246. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8247. "0369 No entry from fast-path completion "
  8248. "queue fcpcqid=%d\n", cq->queue_id);
  8249. /* In any case, flash and re-arm the CQ */
  8250. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  8251. /* wake up worker thread if there are works to be done */
  8252. if (workposted)
  8253. lpfc_worker_wake_up(phba);
  8254. }
  8255. static void
  8256. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  8257. {
  8258. struct lpfc_eqe *eqe;
  8259. /* walk all the EQ entries and drop on the floor */
  8260. while ((eqe = lpfc_sli4_eq_get(eq)))
  8261. ;
  8262. /* Clear and re-arm the EQ */
  8263. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  8264. }
  8265. /**
  8266. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  8267. * @irq: Interrupt number.
  8268. * @dev_id: The device context pointer.
  8269. *
  8270. * This function is directly called from the PCI layer as an interrupt
  8271. * service routine when device with SLI-4 interface spec is enabled with
  8272. * MSI-X multi-message interrupt mode and there are slow-path events in
  8273. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  8274. * interrupt mode, this function is called as part of the device-level
  8275. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  8276. * undergoing initialization, the interrupt handler will not process the
  8277. * interrupt. The link attention and ELS ring attention events are handled
  8278. * by the worker thread. The interrupt handler signals the worker thread
  8279. * and returns for these events. This function is called without any lock
  8280. * held. It gets the hbalock to access and update SLI data structures.
  8281. *
  8282. * This function returns IRQ_HANDLED when interrupt is handled else it
  8283. * returns IRQ_NONE.
  8284. **/
  8285. irqreturn_t
  8286. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  8287. {
  8288. struct lpfc_hba *phba;
  8289. struct lpfc_queue *speq;
  8290. struct lpfc_eqe *eqe;
  8291. unsigned long iflag;
  8292. int ecount = 0;
  8293. /*
  8294. * Get the driver's phba structure from the dev_id
  8295. */
  8296. phba = (struct lpfc_hba *)dev_id;
  8297. if (unlikely(!phba))
  8298. return IRQ_NONE;
  8299. /* Get to the EQ struct associated with this vector */
  8300. speq = phba->sli4_hba.sp_eq;
  8301. /* Check device state for handling interrupt */
  8302. if (unlikely(lpfc_intr_state_check(phba))) {
  8303. /* Check again for link_state with lock held */
  8304. spin_lock_irqsave(&phba->hbalock, iflag);
  8305. if (phba->link_state < LPFC_LINK_DOWN)
  8306. /* Flush, clear interrupt, and rearm the EQ */
  8307. lpfc_sli4_eq_flush(phba, speq);
  8308. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8309. return IRQ_NONE;
  8310. }
  8311. /*
  8312. * Process all the event on FCP slow-path EQ
  8313. */
  8314. while ((eqe = lpfc_sli4_eq_get(speq))) {
  8315. lpfc_sli4_sp_handle_eqe(phba, eqe);
  8316. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8317. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  8318. }
  8319. /* Always clear and re-arm the slow-path EQ */
  8320. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  8321. /* Catch the no cq entry condition */
  8322. if (unlikely(ecount == 0)) {
  8323. if (phba->intr_type == MSIX)
  8324. /* MSI-X treated interrupt served as no EQ share INT */
  8325. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8326. "0357 MSI-X interrupt with no EQE\n");
  8327. else
  8328. /* Non MSI-X treated on interrupt as EQ share INT */
  8329. return IRQ_NONE;
  8330. }
  8331. return IRQ_HANDLED;
  8332. } /* lpfc_sli4_sp_intr_handler */
  8333. /**
  8334. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  8335. * @irq: Interrupt number.
  8336. * @dev_id: The device context pointer.
  8337. *
  8338. * This function is directly called from the PCI layer as an interrupt
  8339. * service routine when device with SLI-4 interface spec is enabled with
  8340. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  8341. * ring event in the HBA. However, when the device is enabled with either
  8342. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  8343. * device-level interrupt handler. When the PCI slot is in error recovery
  8344. * or the HBA is undergoing initialization, the interrupt handler will not
  8345. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  8346. * the intrrupt context. This function is called without any lock held.
  8347. * It gets the hbalock to access and update SLI data structures. Note that,
  8348. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  8349. * equal to that of FCP CQ index.
  8350. *
  8351. * This function returns IRQ_HANDLED when interrupt is handled else it
  8352. * returns IRQ_NONE.
  8353. **/
  8354. irqreturn_t
  8355. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  8356. {
  8357. struct lpfc_hba *phba;
  8358. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  8359. struct lpfc_queue *fpeq;
  8360. struct lpfc_eqe *eqe;
  8361. unsigned long iflag;
  8362. int ecount = 0;
  8363. uint32_t fcp_eqidx;
  8364. /* Get the driver's phba structure from the dev_id */
  8365. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  8366. phba = fcp_eq_hdl->phba;
  8367. fcp_eqidx = fcp_eq_hdl->idx;
  8368. if (unlikely(!phba))
  8369. return IRQ_NONE;
  8370. /* Get to the EQ struct associated with this vector */
  8371. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  8372. /* Check device state for handling interrupt */
  8373. if (unlikely(lpfc_intr_state_check(phba))) {
  8374. /* Check again for link_state with lock held */
  8375. spin_lock_irqsave(&phba->hbalock, iflag);
  8376. if (phba->link_state < LPFC_LINK_DOWN)
  8377. /* Flush, clear interrupt, and rearm the EQ */
  8378. lpfc_sli4_eq_flush(phba, fpeq);
  8379. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8380. return IRQ_NONE;
  8381. }
  8382. /*
  8383. * Process all the event on FCP fast-path EQ
  8384. */
  8385. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8386. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  8387. if (!(++ecount % LPFC_GET_QE_REL_INT))
  8388. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  8389. }
  8390. /* Always clear and re-arm the fast-path EQ */
  8391. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  8392. if (unlikely(ecount == 0)) {
  8393. if (phba->intr_type == MSIX)
  8394. /* MSI-X treated interrupt served as no EQ share INT */
  8395. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8396. "0358 MSI-X interrupt with no EQE\n");
  8397. else
  8398. /* Non MSI-X treated on interrupt as EQ share INT */
  8399. return IRQ_NONE;
  8400. }
  8401. return IRQ_HANDLED;
  8402. } /* lpfc_sli4_fp_intr_handler */
  8403. /**
  8404. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  8405. * @irq: Interrupt number.
  8406. * @dev_id: The device context pointer.
  8407. *
  8408. * This function is the device-level interrupt handler to device with SLI-4
  8409. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  8410. * interrupt mode is enabled and there is an event in the HBA which requires
  8411. * driver attention. This function invokes the slow-path interrupt attention
  8412. * handling function and fast-path interrupt attention handling function in
  8413. * turn to process the relevant HBA attention events. This function is called
  8414. * without any lock held. It gets the hbalock to access and update SLI data
  8415. * structures.
  8416. *
  8417. * This function returns IRQ_HANDLED when interrupt is handled, else it
  8418. * returns IRQ_NONE.
  8419. **/
  8420. irqreturn_t
  8421. lpfc_sli4_intr_handler(int irq, void *dev_id)
  8422. {
  8423. struct lpfc_hba *phba;
  8424. irqreturn_t sp_irq_rc, fp_irq_rc;
  8425. bool fp_handled = false;
  8426. uint32_t fcp_eqidx;
  8427. /* Get the driver's phba structure from the dev_id */
  8428. phba = (struct lpfc_hba *)dev_id;
  8429. if (unlikely(!phba))
  8430. return IRQ_NONE;
  8431. /*
  8432. * Invokes slow-path host attention interrupt handling as appropriate.
  8433. */
  8434. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  8435. /*
  8436. * Invoke fast-path host attention interrupt handling as appropriate.
  8437. */
  8438. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  8439. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  8440. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  8441. if (fp_irq_rc == IRQ_HANDLED)
  8442. fp_handled |= true;
  8443. }
  8444. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  8445. } /* lpfc_sli4_intr_handler */
  8446. /**
  8447. * lpfc_sli4_queue_free - free a queue structure and associated memory
  8448. * @queue: The queue structure to free.
  8449. *
  8450. * This function frees a queue structure and the DMAable memeory used for
  8451. * the host resident queue. This function must be called after destroying the
  8452. * queue on the HBA.
  8453. **/
  8454. void
  8455. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  8456. {
  8457. struct lpfc_dmabuf *dmabuf;
  8458. if (!queue)
  8459. return;
  8460. while (!list_empty(&queue->page_list)) {
  8461. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  8462. list);
  8463. dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
  8464. dmabuf->virt, dmabuf->phys);
  8465. kfree(dmabuf);
  8466. }
  8467. kfree(queue);
  8468. return;
  8469. }
  8470. /**
  8471. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  8472. * @phba: The HBA that this queue is being created on.
  8473. * @entry_size: The size of each queue entry for this queue.
  8474. * @entry count: The number of entries that this queue will handle.
  8475. *
  8476. * This function allocates a queue structure and the DMAable memory used for
  8477. * the host resident queue. This function must be called before creating the
  8478. * queue on the HBA.
  8479. **/
  8480. struct lpfc_queue *
  8481. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  8482. uint32_t entry_count)
  8483. {
  8484. struct lpfc_queue *queue;
  8485. struct lpfc_dmabuf *dmabuf;
  8486. int x, total_qe_count;
  8487. void *dma_pointer;
  8488. queue = kzalloc(sizeof(struct lpfc_queue) +
  8489. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  8490. if (!queue)
  8491. return NULL;
  8492. queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
  8493. INIT_LIST_HEAD(&queue->list);
  8494. INIT_LIST_HEAD(&queue->page_list);
  8495. INIT_LIST_HEAD(&queue->child_list);
  8496. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  8497. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  8498. if (!dmabuf)
  8499. goto out_fail;
  8500. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  8501. PAGE_SIZE, &dmabuf->phys,
  8502. GFP_KERNEL);
  8503. if (!dmabuf->virt) {
  8504. kfree(dmabuf);
  8505. goto out_fail;
  8506. }
  8507. memset(dmabuf->virt, 0, PAGE_SIZE);
  8508. dmabuf->buffer_tag = x;
  8509. list_add_tail(&dmabuf->list, &queue->page_list);
  8510. /* initialize queue's entry array */
  8511. dma_pointer = dmabuf->virt;
  8512. for (; total_qe_count < entry_count &&
  8513. dma_pointer < (PAGE_SIZE + dmabuf->virt);
  8514. total_qe_count++, dma_pointer += entry_size) {
  8515. queue->qe[total_qe_count].address = dma_pointer;
  8516. }
  8517. }
  8518. queue->entry_size = entry_size;
  8519. queue->entry_count = entry_count;
  8520. queue->phba = phba;
  8521. return queue;
  8522. out_fail:
  8523. lpfc_sli4_queue_free(queue);
  8524. return NULL;
  8525. }
  8526. /**
  8527. * lpfc_eq_create - Create an Event Queue on the HBA
  8528. * @phba: HBA structure that indicates port to create a queue on.
  8529. * @eq: The queue structure to use to create the event queue.
  8530. * @imax: The maximum interrupt per second limit.
  8531. *
  8532. * This function creates an event queue, as detailed in @eq, on a port,
  8533. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  8534. *
  8535. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  8536. * is used to get the entry count and entry size that are necessary to
  8537. * determine the number of pages to allocate and use for this queue. This
  8538. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  8539. * event queue. This function is asynchronous and will wait for the mailbox
  8540. * command to finish before continuing.
  8541. *
  8542. * On success this function will return a zero. If unable to allocate enough
  8543. * memory this function will return ENOMEM. If the queue create mailbox command
  8544. * fails this function will return ENXIO.
  8545. **/
  8546. uint32_t
  8547. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  8548. {
  8549. struct lpfc_mbx_eq_create *eq_create;
  8550. LPFC_MBOXQ_t *mbox;
  8551. int rc, length, status = 0;
  8552. struct lpfc_dmabuf *dmabuf;
  8553. uint32_t shdr_status, shdr_add_status;
  8554. union lpfc_sli4_cfg_shdr *shdr;
  8555. uint16_t dmult;
  8556. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8557. if (!mbox)
  8558. return -ENOMEM;
  8559. length = (sizeof(struct lpfc_mbx_eq_create) -
  8560. sizeof(struct lpfc_sli4_cfg_mhdr));
  8561. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8562. LPFC_MBOX_OPCODE_EQ_CREATE,
  8563. length, LPFC_SLI4_MBX_EMBED);
  8564. eq_create = &mbox->u.mqe.un.eq_create;
  8565. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  8566. eq->page_count);
  8567. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  8568. LPFC_EQE_SIZE);
  8569. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  8570. /* Calculate delay multiper from maximum interrupt per second */
  8571. dmult = LPFC_DMULT_CONST/imax - 1;
  8572. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  8573. dmult);
  8574. switch (eq->entry_count) {
  8575. default:
  8576. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8577. "0360 Unsupported EQ count. (%d)\n",
  8578. eq->entry_count);
  8579. if (eq->entry_count < 256)
  8580. return -EINVAL;
  8581. /* otherwise default to smallest count (drop through) */
  8582. case 256:
  8583. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8584. LPFC_EQ_CNT_256);
  8585. break;
  8586. case 512:
  8587. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8588. LPFC_EQ_CNT_512);
  8589. break;
  8590. case 1024:
  8591. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8592. LPFC_EQ_CNT_1024);
  8593. break;
  8594. case 2048:
  8595. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8596. LPFC_EQ_CNT_2048);
  8597. break;
  8598. case 4096:
  8599. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  8600. LPFC_EQ_CNT_4096);
  8601. break;
  8602. }
  8603. list_for_each_entry(dmabuf, &eq->page_list, list) {
  8604. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8605. putPaddrLow(dmabuf->phys);
  8606. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8607. putPaddrHigh(dmabuf->phys);
  8608. }
  8609. mbox->vport = phba->pport;
  8610. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8611. mbox->context1 = NULL;
  8612. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8613. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  8614. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8615. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8616. if (shdr_status || shdr_add_status || rc) {
  8617. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8618. "2500 EQ_CREATE mailbox failed with "
  8619. "status x%x add_status x%x, mbx status x%x\n",
  8620. shdr_status, shdr_add_status, rc);
  8621. status = -ENXIO;
  8622. }
  8623. eq->type = LPFC_EQ;
  8624. eq->subtype = LPFC_NONE;
  8625. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  8626. if (eq->queue_id == 0xFFFF)
  8627. status = -ENXIO;
  8628. eq->host_index = 0;
  8629. eq->hba_index = 0;
  8630. mempool_free(mbox, phba->mbox_mem_pool);
  8631. return status;
  8632. }
  8633. /**
  8634. * lpfc_cq_create - Create a Completion Queue on the HBA
  8635. * @phba: HBA structure that indicates port to create a queue on.
  8636. * @cq: The queue structure to use to create the completion queue.
  8637. * @eq: The event queue to bind this completion queue to.
  8638. *
  8639. * This function creates a completion queue, as detailed in @wq, on a port,
  8640. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  8641. *
  8642. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  8643. * is used to get the entry count and entry size that are necessary to
  8644. * determine the number of pages to allocate and use for this queue. The @eq
  8645. * is used to indicate which event queue to bind this completion queue to. This
  8646. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  8647. * completion queue. This function is asynchronous and will wait for the mailbox
  8648. * command to finish before continuing.
  8649. *
  8650. * On success this function will return a zero. If unable to allocate enough
  8651. * memory this function will return ENOMEM. If the queue create mailbox command
  8652. * fails this function will return ENXIO.
  8653. **/
  8654. uint32_t
  8655. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  8656. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  8657. {
  8658. struct lpfc_mbx_cq_create *cq_create;
  8659. struct lpfc_dmabuf *dmabuf;
  8660. LPFC_MBOXQ_t *mbox;
  8661. int rc, length, status = 0;
  8662. uint32_t shdr_status, shdr_add_status;
  8663. union lpfc_sli4_cfg_shdr *shdr;
  8664. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8665. if (!mbox)
  8666. return -ENOMEM;
  8667. length = (sizeof(struct lpfc_mbx_cq_create) -
  8668. sizeof(struct lpfc_sli4_cfg_mhdr));
  8669. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8670. LPFC_MBOX_OPCODE_CQ_CREATE,
  8671. length, LPFC_SLI4_MBX_EMBED);
  8672. cq_create = &mbox->u.mqe.un.cq_create;
  8673. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  8674. cq->page_count);
  8675. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  8676. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  8677. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
  8678. switch (cq->entry_count) {
  8679. default:
  8680. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8681. "0361 Unsupported CQ count. (%d)\n",
  8682. cq->entry_count);
  8683. if (cq->entry_count < 256)
  8684. return -EINVAL;
  8685. /* otherwise default to smallest count (drop through) */
  8686. case 256:
  8687. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8688. LPFC_CQ_CNT_256);
  8689. break;
  8690. case 512:
  8691. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8692. LPFC_CQ_CNT_512);
  8693. break;
  8694. case 1024:
  8695. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  8696. LPFC_CQ_CNT_1024);
  8697. break;
  8698. }
  8699. list_for_each_entry(dmabuf, &cq->page_list, list) {
  8700. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8701. putPaddrLow(dmabuf->phys);
  8702. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8703. putPaddrHigh(dmabuf->phys);
  8704. }
  8705. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8706. /* The IOCTL status is embedded in the mailbox subheader. */
  8707. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  8708. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8709. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8710. if (shdr_status || shdr_add_status || rc) {
  8711. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8712. "2501 CQ_CREATE mailbox failed with "
  8713. "status x%x add_status x%x, mbx status x%x\n",
  8714. shdr_status, shdr_add_status, rc);
  8715. status = -ENXIO;
  8716. goto out;
  8717. }
  8718. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  8719. if (cq->queue_id == 0xFFFF) {
  8720. status = -ENXIO;
  8721. goto out;
  8722. }
  8723. /* link the cq onto the parent eq child list */
  8724. list_add_tail(&cq->list, &eq->child_list);
  8725. /* Set up completion queue's type and subtype */
  8726. cq->type = type;
  8727. cq->subtype = subtype;
  8728. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  8729. cq->host_index = 0;
  8730. cq->hba_index = 0;
  8731. out:
  8732. mempool_free(mbox, phba->mbox_mem_pool);
  8733. return status;
  8734. }
  8735. /**
  8736. * lpfc_mq_create - Create a mailbox Queue on the HBA
  8737. * @phba: HBA structure that indicates port to create a queue on.
  8738. * @mq: The queue structure to use to create the mailbox queue.
  8739. *
  8740. * This function creates a mailbox queue, as detailed in @mq, on a port,
  8741. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  8742. *
  8743. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  8744. * is used to get the entry count and entry size that are necessary to
  8745. * determine the number of pages to allocate and use for this queue. This
  8746. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  8747. * mailbox queue. This function is asynchronous and will wait for the mailbox
  8748. * command to finish before continuing.
  8749. *
  8750. * On success this function will return a zero. If unable to allocate enough
  8751. * memory this function will return ENOMEM. If the queue create mailbox command
  8752. * fails this function will return ENXIO.
  8753. **/
  8754. uint32_t
  8755. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  8756. struct lpfc_queue *cq, uint32_t subtype)
  8757. {
  8758. struct lpfc_mbx_mq_create *mq_create;
  8759. struct lpfc_dmabuf *dmabuf;
  8760. LPFC_MBOXQ_t *mbox;
  8761. int rc, length, status = 0;
  8762. uint32_t shdr_status, shdr_add_status;
  8763. union lpfc_sli4_cfg_shdr *shdr;
  8764. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8765. if (!mbox)
  8766. return -ENOMEM;
  8767. length = (sizeof(struct lpfc_mbx_mq_create) -
  8768. sizeof(struct lpfc_sli4_cfg_mhdr));
  8769. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  8770. LPFC_MBOX_OPCODE_MQ_CREATE,
  8771. length, LPFC_SLI4_MBX_EMBED);
  8772. mq_create = &mbox->u.mqe.un.mq_create;
  8773. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  8774. mq->page_count);
  8775. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  8776. cq->queue_id);
  8777. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  8778. switch (mq->entry_count) {
  8779. default:
  8780. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8781. "0362 Unsupported MQ count. (%d)\n",
  8782. mq->entry_count);
  8783. if (mq->entry_count < 16)
  8784. return -EINVAL;
  8785. /* otherwise default to smallest count (drop through) */
  8786. case 16:
  8787. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8788. LPFC_MQ_CNT_16);
  8789. break;
  8790. case 32:
  8791. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8792. LPFC_MQ_CNT_32);
  8793. break;
  8794. case 64:
  8795. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8796. LPFC_MQ_CNT_64);
  8797. break;
  8798. case 128:
  8799. bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
  8800. LPFC_MQ_CNT_128);
  8801. break;
  8802. }
  8803. list_for_each_entry(dmabuf, &mq->page_list, list) {
  8804. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8805. putPaddrLow(dmabuf->phys);
  8806. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8807. putPaddrHigh(dmabuf->phys);
  8808. }
  8809. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8810. /* The IOCTL status is embedded in the mailbox subheader. */
  8811. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  8812. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8813. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8814. if (shdr_status || shdr_add_status || rc) {
  8815. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8816. "2502 MQ_CREATE mailbox failed with "
  8817. "status x%x add_status x%x, mbx status x%x\n",
  8818. shdr_status, shdr_add_status, rc);
  8819. status = -ENXIO;
  8820. goto out;
  8821. }
  8822. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
  8823. if (mq->queue_id == 0xFFFF) {
  8824. status = -ENXIO;
  8825. goto out;
  8826. }
  8827. mq->type = LPFC_MQ;
  8828. mq->subtype = subtype;
  8829. mq->host_index = 0;
  8830. mq->hba_index = 0;
  8831. /* link the mq onto the parent cq child list */
  8832. list_add_tail(&mq->list, &cq->child_list);
  8833. out:
  8834. mempool_free(mbox, phba->mbox_mem_pool);
  8835. return status;
  8836. }
  8837. /**
  8838. * lpfc_wq_create - Create a Work Queue on the HBA
  8839. * @phba: HBA structure that indicates port to create a queue on.
  8840. * @wq: The queue structure to use to create the work queue.
  8841. * @cq: The completion queue to bind this work queue to.
  8842. * @subtype: The subtype of the work queue indicating its functionality.
  8843. *
  8844. * This function creates a work queue, as detailed in @wq, on a port, described
  8845. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  8846. *
  8847. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  8848. * is used to get the entry count and entry size that are necessary to
  8849. * determine the number of pages to allocate and use for this queue. The @cq
  8850. * is used to indicate which completion queue to bind this work queue to. This
  8851. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  8852. * work queue. This function is asynchronous and will wait for the mailbox
  8853. * command to finish before continuing.
  8854. *
  8855. * On success this function will return a zero. If unable to allocate enough
  8856. * memory this function will return ENOMEM. If the queue create mailbox command
  8857. * fails this function will return ENXIO.
  8858. **/
  8859. uint32_t
  8860. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  8861. struct lpfc_queue *cq, uint32_t subtype)
  8862. {
  8863. struct lpfc_mbx_wq_create *wq_create;
  8864. struct lpfc_dmabuf *dmabuf;
  8865. LPFC_MBOXQ_t *mbox;
  8866. int rc, length, status = 0;
  8867. uint32_t shdr_status, shdr_add_status;
  8868. union lpfc_sli4_cfg_shdr *shdr;
  8869. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8870. if (!mbox)
  8871. return -ENOMEM;
  8872. length = (sizeof(struct lpfc_mbx_wq_create) -
  8873. sizeof(struct lpfc_sli4_cfg_mhdr));
  8874. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  8875. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  8876. length, LPFC_SLI4_MBX_EMBED);
  8877. wq_create = &mbox->u.mqe.un.wq_create;
  8878. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  8879. wq->page_count);
  8880. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  8881. cq->queue_id);
  8882. list_for_each_entry(dmabuf, &wq->page_list, list) {
  8883. wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8884. putPaddrLow(dmabuf->phys);
  8885. wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8886. putPaddrHigh(dmabuf->phys);
  8887. }
  8888. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8889. /* The IOCTL status is embedded in the mailbox subheader. */
  8890. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  8891. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  8892. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  8893. if (shdr_status || shdr_add_status || rc) {
  8894. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8895. "2503 WQ_CREATE mailbox failed with "
  8896. "status x%x add_status x%x, mbx status x%x\n",
  8897. shdr_status, shdr_add_status, rc);
  8898. status = -ENXIO;
  8899. goto out;
  8900. }
  8901. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  8902. if (wq->queue_id == 0xFFFF) {
  8903. status = -ENXIO;
  8904. goto out;
  8905. }
  8906. wq->type = LPFC_WQ;
  8907. wq->subtype = subtype;
  8908. wq->host_index = 0;
  8909. wq->hba_index = 0;
  8910. /* link the wq onto the parent cq child list */
  8911. list_add_tail(&wq->list, &cq->child_list);
  8912. out:
  8913. mempool_free(mbox, phba->mbox_mem_pool);
  8914. return status;
  8915. }
  8916. /**
  8917. * lpfc_rq_create - Create a Receive Queue on the HBA
  8918. * @phba: HBA structure that indicates port to create a queue on.
  8919. * @hrq: The queue structure to use to create the header receive queue.
  8920. * @drq: The queue structure to use to create the data receive queue.
  8921. * @cq: The completion queue to bind this work queue to.
  8922. *
  8923. * This function creates a receive buffer queue pair , as detailed in @hrq and
  8924. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  8925. * to the HBA.
  8926. *
  8927. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  8928. * struct is used to get the entry count that is necessary to determine the
  8929. * number of pages to use for this queue. The @cq is used to indicate which
  8930. * completion queue to bind received buffers that are posted to these queues to.
  8931. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  8932. * receive queue pair. This function is asynchronous and will wait for the
  8933. * mailbox command to finish before continuing.
  8934. *
  8935. * On success this function will return a zero. If unable to allocate enough
  8936. * memory this function will return ENOMEM. If the queue create mailbox command
  8937. * fails this function will return ENXIO.
  8938. **/
  8939. uint32_t
  8940. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  8941. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  8942. {
  8943. struct lpfc_mbx_rq_create *rq_create;
  8944. struct lpfc_dmabuf *dmabuf;
  8945. LPFC_MBOXQ_t *mbox;
  8946. int rc, length, status = 0;
  8947. uint32_t shdr_status, shdr_add_status;
  8948. union lpfc_sli4_cfg_shdr *shdr;
  8949. if (hrq->entry_count != drq->entry_count)
  8950. return -EINVAL;
  8951. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8952. if (!mbox)
  8953. return -ENOMEM;
  8954. length = (sizeof(struct lpfc_mbx_rq_create) -
  8955. sizeof(struct lpfc_sli4_cfg_mhdr));
  8956. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  8957. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  8958. length, LPFC_SLI4_MBX_EMBED);
  8959. rq_create = &mbox->u.mqe.un.rq_create;
  8960. switch (hrq->entry_count) {
  8961. default:
  8962. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8963. "2535 Unsupported RQ count. (%d)\n",
  8964. hrq->entry_count);
  8965. if (hrq->entry_count < 512)
  8966. return -EINVAL;
  8967. /* otherwise default to smallest count (drop through) */
  8968. case 512:
  8969. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8970. LPFC_RQ_RING_SIZE_512);
  8971. break;
  8972. case 1024:
  8973. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8974. LPFC_RQ_RING_SIZE_1024);
  8975. break;
  8976. case 2048:
  8977. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8978. LPFC_RQ_RING_SIZE_2048);
  8979. break;
  8980. case 4096:
  8981. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  8982. LPFC_RQ_RING_SIZE_4096);
  8983. break;
  8984. }
  8985. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  8986. cq->queue_id);
  8987. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  8988. hrq->page_count);
  8989. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  8990. LPFC_HDR_BUF_SIZE);
  8991. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  8992. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  8993. putPaddrLow(dmabuf->phys);
  8994. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  8995. putPaddrHigh(dmabuf->phys);
  8996. }
  8997. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  8998. /* The IOCTL status is embedded in the mailbox subheader. */
  8999. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  9000. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9001. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9002. if (shdr_status || shdr_add_status || rc) {
  9003. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9004. "2504 RQ_CREATE mailbox failed with "
  9005. "status x%x add_status x%x, mbx status x%x\n",
  9006. shdr_status, shdr_add_status, rc);
  9007. status = -ENXIO;
  9008. goto out;
  9009. }
  9010. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  9011. if (hrq->queue_id == 0xFFFF) {
  9012. status = -ENXIO;
  9013. goto out;
  9014. }
  9015. hrq->type = LPFC_HRQ;
  9016. hrq->subtype = subtype;
  9017. hrq->host_index = 0;
  9018. hrq->hba_index = 0;
  9019. /* now create the data queue */
  9020. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9021. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  9022. length, LPFC_SLI4_MBX_EMBED);
  9023. switch (drq->entry_count) {
  9024. default:
  9025. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9026. "2536 Unsupported RQ count. (%d)\n",
  9027. drq->entry_count);
  9028. if (drq->entry_count < 512)
  9029. return -EINVAL;
  9030. /* otherwise default to smallest count (drop through) */
  9031. case 512:
  9032. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9033. LPFC_RQ_RING_SIZE_512);
  9034. break;
  9035. case 1024:
  9036. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9037. LPFC_RQ_RING_SIZE_1024);
  9038. break;
  9039. case 2048:
  9040. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9041. LPFC_RQ_RING_SIZE_2048);
  9042. break;
  9043. case 4096:
  9044. bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
  9045. LPFC_RQ_RING_SIZE_4096);
  9046. break;
  9047. }
  9048. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  9049. cq->queue_id);
  9050. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  9051. drq->page_count);
  9052. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  9053. LPFC_DATA_BUF_SIZE);
  9054. list_for_each_entry(dmabuf, &drq->page_list, list) {
  9055. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  9056. putPaddrLow(dmabuf->phys);
  9057. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  9058. putPaddrHigh(dmabuf->phys);
  9059. }
  9060. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9061. /* The IOCTL status is embedded in the mailbox subheader. */
  9062. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  9063. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9064. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9065. if (shdr_status || shdr_add_status || rc) {
  9066. status = -ENXIO;
  9067. goto out;
  9068. }
  9069. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  9070. if (drq->queue_id == 0xFFFF) {
  9071. status = -ENXIO;
  9072. goto out;
  9073. }
  9074. drq->type = LPFC_DRQ;
  9075. drq->subtype = subtype;
  9076. drq->host_index = 0;
  9077. drq->hba_index = 0;
  9078. /* link the header and data RQs onto the parent cq child list */
  9079. list_add_tail(&hrq->list, &cq->child_list);
  9080. list_add_tail(&drq->list, &cq->child_list);
  9081. out:
  9082. mempool_free(mbox, phba->mbox_mem_pool);
  9083. return status;
  9084. }
  9085. /**
  9086. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  9087. * @eq: The queue structure associated with the queue to destroy.
  9088. *
  9089. * This function destroys a queue, as detailed in @eq by sending an mailbox
  9090. * command, specific to the type of queue, to the HBA.
  9091. *
  9092. * The @eq struct is used to get the queue ID of the queue to destroy.
  9093. *
  9094. * On success this function will return a zero. If the queue destroy mailbox
  9095. * command fails this function will return ENXIO.
  9096. **/
  9097. uint32_t
  9098. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  9099. {
  9100. LPFC_MBOXQ_t *mbox;
  9101. int rc, length, status = 0;
  9102. uint32_t shdr_status, shdr_add_status;
  9103. union lpfc_sli4_cfg_shdr *shdr;
  9104. if (!eq)
  9105. return -ENODEV;
  9106. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  9107. if (!mbox)
  9108. return -ENOMEM;
  9109. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  9110. sizeof(struct lpfc_sli4_cfg_mhdr));
  9111. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9112. LPFC_MBOX_OPCODE_EQ_DESTROY,
  9113. length, LPFC_SLI4_MBX_EMBED);
  9114. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  9115. eq->queue_id);
  9116. mbox->vport = eq->phba->pport;
  9117. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9118. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  9119. /* The IOCTL status is embedded in the mailbox subheader. */
  9120. shdr = (union lpfc_sli4_cfg_shdr *)
  9121. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  9122. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9123. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9124. if (shdr_status || shdr_add_status || rc) {
  9125. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9126. "2505 EQ_DESTROY mailbox failed with "
  9127. "status x%x add_status x%x, mbx status x%x\n",
  9128. shdr_status, shdr_add_status, rc);
  9129. status = -ENXIO;
  9130. }
  9131. /* Remove eq from any list */
  9132. list_del_init(&eq->list);
  9133. mempool_free(mbox, eq->phba->mbox_mem_pool);
  9134. return status;
  9135. }
  9136. /**
  9137. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  9138. * @cq: The queue structure associated with the queue to destroy.
  9139. *
  9140. * This function destroys a queue, as detailed in @cq by sending an mailbox
  9141. * command, specific to the type of queue, to the HBA.
  9142. *
  9143. * The @cq struct is used to get the queue ID of the queue to destroy.
  9144. *
  9145. * On success this function will return a zero. If the queue destroy mailbox
  9146. * command fails this function will return ENXIO.
  9147. **/
  9148. uint32_t
  9149. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  9150. {
  9151. LPFC_MBOXQ_t *mbox;
  9152. int rc, length, status = 0;
  9153. uint32_t shdr_status, shdr_add_status;
  9154. union lpfc_sli4_cfg_shdr *shdr;
  9155. if (!cq)
  9156. return -ENODEV;
  9157. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  9158. if (!mbox)
  9159. return -ENOMEM;
  9160. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  9161. sizeof(struct lpfc_sli4_cfg_mhdr));
  9162. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9163. LPFC_MBOX_OPCODE_CQ_DESTROY,
  9164. length, LPFC_SLI4_MBX_EMBED);
  9165. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  9166. cq->queue_id);
  9167. mbox->vport = cq->phba->pport;
  9168. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9169. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  9170. /* The IOCTL status is embedded in the mailbox subheader. */
  9171. shdr = (union lpfc_sli4_cfg_shdr *)
  9172. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  9173. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9174. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9175. if (shdr_status || shdr_add_status || rc) {
  9176. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9177. "2506 CQ_DESTROY mailbox failed with "
  9178. "status x%x add_status x%x, mbx status x%x\n",
  9179. shdr_status, shdr_add_status, rc);
  9180. status = -ENXIO;
  9181. }
  9182. /* Remove cq from any list */
  9183. list_del_init(&cq->list);
  9184. mempool_free(mbox, cq->phba->mbox_mem_pool);
  9185. return status;
  9186. }
  9187. /**
  9188. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  9189. * @qm: The queue structure associated with the queue to destroy.
  9190. *
  9191. * This function destroys a queue, as detailed in @mq by sending an mailbox
  9192. * command, specific to the type of queue, to the HBA.
  9193. *
  9194. * The @mq struct is used to get the queue ID of the queue to destroy.
  9195. *
  9196. * On success this function will return a zero. If the queue destroy mailbox
  9197. * command fails this function will return ENXIO.
  9198. **/
  9199. uint32_t
  9200. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  9201. {
  9202. LPFC_MBOXQ_t *mbox;
  9203. int rc, length, status = 0;
  9204. uint32_t shdr_status, shdr_add_status;
  9205. union lpfc_sli4_cfg_shdr *shdr;
  9206. if (!mq)
  9207. return -ENODEV;
  9208. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  9209. if (!mbox)
  9210. return -ENOMEM;
  9211. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  9212. sizeof(struct lpfc_sli4_cfg_mhdr));
  9213. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  9214. LPFC_MBOX_OPCODE_MQ_DESTROY,
  9215. length, LPFC_SLI4_MBX_EMBED);
  9216. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  9217. mq->queue_id);
  9218. mbox->vport = mq->phba->pport;
  9219. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9220. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  9221. /* The IOCTL status is embedded in the mailbox subheader. */
  9222. shdr = (union lpfc_sli4_cfg_shdr *)
  9223. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  9224. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9225. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9226. if (shdr_status || shdr_add_status || rc) {
  9227. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9228. "2507 MQ_DESTROY mailbox failed with "
  9229. "status x%x add_status x%x, mbx status x%x\n",
  9230. shdr_status, shdr_add_status, rc);
  9231. status = -ENXIO;
  9232. }
  9233. /* Remove mq from any list */
  9234. list_del_init(&mq->list);
  9235. mempool_free(mbox, mq->phba->mbox_mem_pool);
  9236. return status;
  9237. }
  9238. /**
  9239. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  9240. * @wq: The queue structure associated with the queue to destroy.
  9241. *
  9242. * This function destroys a queue, as detailed in @wq by sending an mailbox
  9243. * command, specific to the type of queue, to the HBA.
  9244. *
  9245. * The @wq struct is used to get the queue ID of the queue to destroy.
  9246. *
  9247. * On success this function will return a zero. If the queue destroy mailbox
  9248. * command fails this function will return ENXIO.
  9249. **/
  9250. uint32_t
  9251. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  9252. {
  9253. LPFC_MBOXQ_t *mbox;
  9254. int rc, length, status = 0;
  9255. uint32_t shdr_status, shdr_add_status;
  9256. union lpfc_sli4_cfg_shdr *shdr;
  9257. if (!wq)
  9258. return -ENODEV;
  9259. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  9260. if (!mbox)
  9261. return -ENOMEM;
  9262. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  9263. sizeof(struct lpfc_sli4_cfg_mhdr));
  9264. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9265. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  9266. length, LPFC_SLI4_MBX_EMBED);
  9267. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  9268. wq->queue_id);
  9269. mbox->vport = wq->phba->pport;
  9270. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9271. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  9272. shdr = (union lpfc_sli4_cfg_shdr *)
  9273. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  9274. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9275. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9276. if (shdr_status || shdr_add_status || rc) {
  9277. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9278. "2508 WQ_DESTROY mailbox failed with "
  9279. "status x%x add_status x%x, mbx status x%x\n",
  9280. shdr_status, shdr_add_status, rc);
  9281. status = -ENXIO;
  9282. }
  9283. /* Remove wq from any list */
  9284. list_del_init(&wq->list);
  9285. mempool_free(mbox, wq->phba->mbox_mem_pool);
  9286. return status;
  9287. }
  9288. /**
  9289. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  9290. * @rq: The queue structure associated with the queue to destroy.
  9291. *
  9292. * This function destroys a queue, as detailed in @rq by sending an mailbox
  9293. * command, specific to the type of queue, to the HBA.
  9294. *
  9295. * The @rq struct is used to get the queue ID of the queue to destroy.
  9296. *
  9297. * On success this function will return a zero. If the queue destroy mailbox
  9298. * command fails this function will return ENXIO.
  9299. **/
  9300. uint32_t
  9301. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  9302. struct lpfc_queue *drq)
  9303. {
  9304. LPFC_MBOXQ_t *mbox;
  9305. int rc, length, status = 0;
  9306. uint32_t shdr_status, shdr_add_status;
  9307. union lpfc_sli4_cfg_shdr *shdr;
  9308. if (!hrq || !drq)
  9309. return -ENODEV;
  9310. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  9311. if (!mbox)
  9312. return -ENOMEM;
  9313. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  9314. sizeof(struct mbox_header));
  9315. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9316. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  9317. length, LPFC_SLI4_MBX_EMBED);
  9318. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  9319. hrq->queue_id);
  9320. mbox->vport = hrq->phba->pport;
  9321. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9322. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  9323. /* The IOCTL status is embedded in the mailbox subheader. */
  9324. shdr = (union lpfc_sli4_cfg_shdr *)
  9325. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  9326. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9327. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9328. if (shdr_status || shdr_add_status || rc) {
  9329. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9330. "2509 RQ_DESTROY mailbox failed with "
  9331. "status x%x add_status x%x, mbx status x%x\n",
  9332. shdr_status, shdr_add_status, rc);
  9333. if (rc != MBX_TIMEOUT)
  9334. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  9335. return -ENXIO;
  9336. }
  9337. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  9338. drq->queue_id);
  9339. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  9340. shdr = (union lpfc_sli4_cfg_shdr *)
  9341. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  9342. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9343. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9344. if (shdr_status || shdr_add_status || rc) {
  9345. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9346. "2510 RQ_DESTROY mailbox failed with "
  9347. "status x%x add_status x%x, mbx status x%x\n",
  9348. shdr_status, shdr_add_status, rc);
  9349. status = -ENXIO;
  9350. }
  9351. list_del_init(&hrq->list);
  9352. list_del_init(&drq->list);
  9353. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  9354. return status;
  9355. }
  9356. /**
  9357. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  9358. * @phba: The virtual port for which this call being executed.
  9359. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  9360. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  9361. * @xritag: the xritag that ties this io to the SGL pages.
  9362. *
  9363. * This routine will post the sgl pages for the IO that has the xritag
  9364. * that is in the iocbq structure. The xritag is assigned during iocbq
  9365. * creation and persists for as long as the driver is loaded.
  9366. * if the caller has fewer than 256 scatter gather segments to map then
  9367. * pdma_phys_addr1 should be 0.
  9368. * If the caller needs to map more than 256 scatter gather segment then
  9369. * pdma_phys_addr1 should be a valid physical address.
  9370. * physical address for SGLs must be 64 byte aligned.
  9371. * If you are going to map 2 SGL's then the first one must have 256 entries
  9372. * the second sgl can have between 1 and 256 entries.
  9373. *
  9374. * Return codes:
  9375. * 0 - Success
  9376. * -ENXIO, -ENOMEM - Failure
  9377. **/
  9378. int
  9379. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  9380. dma_addr_t pdma_phys_addr0,
  9381. dma_addr_t pdma_phys_addr1,
  9382. uint16_t xritag)
  9383. {
  9384. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  9385. LPFC_MBOXQ_t *mbox;
  9386. int rc;
  9387. uint32_t shdr_status, shdr_add_status;
  9388. union lpfc_sli4_cfg_shdr *shdr;
  9389. if (xritag == NO_XRI) {
  9390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9391. "0364 Invalid param:\n");
  9392. return -EINVAL;
  9393. }
  9394. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9395. if (!mbox)
  9396. return -ENOMEM;
  9397. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9398. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  9399. sizeof(struct lpfc_mbx_post_sgl_pages) -
  9400. sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
  9401. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  9402. &mbox->u.mqe.un.post_sgl_pages;
  9403. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  9404. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  9405. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  9406. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  9407. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  9408. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  9409. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  9410. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  9411. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  9412. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  9413. if (!phba->sli4_hba.intr_enable)
  9414. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9415. else
  9416. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  9417. /* The IOCTL status is embedded in the mailbox subheader. */
  9418. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  9419. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9420. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9421. if (rc != MBX_TIMEOUT)
  9422. mempool_free(mbox, phba->mbox_mem_pool);
  9423. if (shdr_status || shdr_add_status || rc) {
  9424. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9425. "2511 POST_SGL mailbox failed with "
  9426. "status x%x add_status x%x, mbx status x%x\n",
  9427. shdr_status, shdr_add_status, rc);
  9428. rc = -ENXIO;
  9429. }
  9430. return 0;
  9431. }
  9432. /**
  9433. * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
  9434. * @phba: The virtual port for which this call being executed.
  9435. *
  9436. * This routine will remove all of the sgl pages registered with the hba.
  9437. *
  9438. * Return codes:
  9439. * 0 - Success
  9440. * -ENXIO, -ENOMEM - Failure
  9441. **/
  9442. int
  9443. lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
  9444. {
  9445. LPFC_MBOXQ_t *mbox;
  9446. int rc;
  9447. uint32_t shdr_status, shdr_add_status;
  9448. union lpfc_sli4_cfg_shdr *shdr;
  9449. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9450. if (!mbox)
  9451. return -ENOMEM;
  9452. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9453. LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
  9454. LPFC_SLI4_MBX_EMBED);
  9455. if (!phba->sli4_hba.intr_enable)
  9456. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9457. else
  9458. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  9459. /* The IOCTL status is embedded in the mailbox subheader. */
  9460. shdr = (union lpfc_sli4_cfg_shdr *)
  9461. &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
  9462. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9463. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9464. if (rc != MBX_TIMEOUT)
  9465. mempool_free(mbox, phba->mbox_mem_pool);
  9466. if (shdr_status || shdr_add_status || rc) {
  9467. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9468. "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
  9469. "status x%x add_status x%x, mbx status x%x\n",
  9470. shdr_status, shdr_add_status, rc);
  9471. rc = -ENXIO;
  9472. }
  9473. return rc;
  9474. }
  9475. /**
  9476. * lpfc_sli4_next_xritag - Get an xritag for the io
  9477. * @phba: Pointer to HBA context object.
  9478. *
  9479. * This function gets an xritag for the iocb. If there is no unused xritag
  9480. * it will return 0xffff.
  9481. * The function returns the allocated xritag if successful, else returns zero.
  9482. * Zero is not a valid xritag.
  9483. * The caller is not required to hold any lock.
  9484. **/
  9485. uint16_t
  9486. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  9487. {
  9488. uint16_t xritag;
  9489. spin_lock_irq(&phba->hbalock);
  9490. xritag = phba->sli4_hba.next_xri;
  9491. if ((xritag != (uint16_t) -1) && xritag <
  9492. (phba->sli4_hba.max_cfg_param.max_xri
  9493. + phba->sli4_hba.max_cfg_param.xri_base)) {
  9494. phba->sli4_hba.next_xri++;
  9495. phba->sli4_hba.max_cfg_param.xri_used++;
  9496. spin_unlock_irq(&phba->hbalock);
  9497. return xritag;
  9498. }
  9499. spin_unlock_irq(&phba->hbalock);
  9500. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9501. "2004 Failed to allocate XRI.last XRITAG is %d"
  9502. " Max XRI is %d, Used XRI is %d\n",
  9503. phba->sli4_hba.next_xri,
  9504. phba->sli4_hba.max_cfg_param.max_xri,
  9505. phba->sli4_hba.max_cfg_param.xri_used);
  9506. return -1;
  9507. }
  9508. /**
  9509. * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
  9510. * @phba: pointer to lpfc hba data structure.
  9511. *
  9512. * This routine is invoked to post a block of driver's sgl pages to the
  9513. * HBA using non-embedded mailbox command. No Lock is held. This routine
  9514. * is only called when the driver is loading and after all IO has been
  9515. * stopped.
  9516. **/
  9517. int
  9518. lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
  9519. {
  9520. struct lpfc_sglq *sglq_entry;
  9521. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  9522. struct sgl_page_pairs *sgl_pg_pairs;
  9523. void *viraddr;
  9524. LPFC_MBOXQ_t *mbox;
  9525. uint32_t reqlen, alloclen, pg_pairs;
  9526. uint32_t mbox_tmo;
  9527. uint16_t xritag_start = 0;
  9528. int els_xri_cnt, rc = 0;
  9529. uint32_t shdr_status, shdr_add_status;
  9530. union lpfc_sli4_cfg_shdr *shdr;
  9531. /* The number of sgls to be posted */
  9532. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  9533. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  9534. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  9535. if (reqlen > PAGE_SIZE) {
  9536. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9537. "2559 Block sgl registration required DMA "
  9538. "size (%d) great than a page\n", reqlen);
  9539. return -ENOMEM;
  9540. }
  9541. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9542. if (!mbox) {
  9543. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9544. "2560 Failed to allocate mbox cmd memory\n");
  9545. return -ENOMEM;
  9546. }
  9547. /* Allocate DMA memory and set up the non-embedded mailbox command */
  9548. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9549. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  9550. LPFC_SLI4_MBX_NEMBED);
  9551. if (alloclen < reqlen) {
  9552. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9553. "0285 Allocated DMA memory size (%d) is "
  9554. "less than the requested DMA memory "
  9555. "size (%d)\n", alloclen, reqlen);
  9556. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9557. return -ENOMEM;
  9558. }
  9559. /* Get the first SGE entry from the non-embedded DMA memory */
  9560. if (unlikely(!mbox->sge_array)) {
  9561. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9562. "2525 Failed to get the non-embedded SGE "
  9563. "virtual address\n");
  9564. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9565. return -ENOMEM;
  9566. }
  9567. viraddr = mbox->sge_array->addr[0];
  9568. /* Set up the SGL pages in the non-embedded DMA pages */
  9569. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  9570. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  9571. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  9572. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  9573. /* Set up the sge entry */
  9574. sgl_pg_pairs->sgl_pg0_addr_lo =
  9575. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  9576. sgl_pg_pairs->sgl_pg0_addr_hi =
  9577. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  9578. sgl_pg_pairs->sgl_pg1_addr_lo =
  9579. cpu_to_le32(putPaddrLow(0));
  9580. sgl_pg_pairs->sgl_pg1_addr_hi =
  9581. cpu_to_le32(putPaddrHigh(0));
  9582. /* Keep the first xritag on the list */
  9583. if (pg_pairs == 0)
  9584. xritag_start = sglq_entry->sli4_xritag;
  9585. sgl_pg_pairs++;
  9586. }
  9587. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  9588. pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
  9589. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  9590. /* Perform endian conversion if necessary */
  9591. sgl->word0 = cpu_to_le32(sgl->word0);
  9592. if (!phba->sli4_hba.intr_enable)
  9593. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9594. else {
  9595. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  9596. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  9597. }
  9598. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  9599. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9600. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9601. if (rc != MBX_TIMEOUT)
  9602. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9603. if (shdr_status || shdr_add_status || rc) {
  9604. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9605. "2513 POST_SGL_BLOCK mailbox command failed "
  9606. "status x%x add_status x%x mbx status x%x\n",
  9607. shdr_status, shdr_add_status, rc);
  9608. rc = -ENXIO;
  9609. }
  9610. return rc;
  9611. }
  9612. /**
  9613. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  9614. * @phba: pointer to lpfc hba data structure.
  9615. * @sblist: pointer to scsi buffer list.
  9616. * @count: number of scsi buffers on the list.
  9617. *
  9618. * This routine is invoked to post a block of @count scsi sgl pages from a
  9619. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  9620. * No Lock is held.
  9621. *
  9622. **/
  9623. int
  9624. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  9625. int cnt)
  9626. {
  9627. struct lpfc_scsi_buf *psb;
  9628. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  9629. struct sgl_page_pairs *sgl_pg_pairs;
  9630. void *viraddr;
  9631. LPFC_MBOXQ_t *mbox;
  9632. uint32_t reqlen, alloclen, pg_pairs;
  9633. uint32_t mbox_tmo;
  9634. uint16_t xritag_start = 0;
  9635. int rc = 0;
  9636. uint32_t shdr_status, shdr_add_status;
  9637. dma_addr_t pdma_phys_bpl1;
  9638. union lpfc_sli4_cfg_shdr *shdr;
  9639. /* Calculate the requested length of the dma memory */
  9640. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  9641. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  9642. if (reqlen > PAGE_SIZE) {
  9643. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9644. "0217 Block sgl registration required DMA "
  9645. "size (%d) great than a page\n", reqlen);
  9646. return -ENOMEM;
  9647. }
  9648. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9649. if (!mbox) {
  9650. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9651. "0283 Failed to allocate mbox cmd memory\n");
  9652. return -ENOMEM;
  9653. }
  9654. /* Allocate DMA memory and set up the non-embedded mailbox command */
  9655. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  9656. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  9657. LPFC_SLI4_MBX_NEMBED);
  9658. if (alloclen < reqlen) {
  9659. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9660. "2561 Allocated DMA memory size (%d) is "
  9661. "less than the requested DMA memory "
  9662. "size (%d)\n", alloclen, reqlen);
  9663. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9664. return -ENOMEM;
  9665. }
  9666. /* Get the first SGE entry from the non-embedded DMA memory */
  9667. if (unlikely(!mbox->sge_array)) {
  9668. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9669. "2565 Failed to get the non-embedded SGE "
  9670. "virtual address\n");
  9671. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9672. return -ENOMEM;
  9673. }
  9674. viraddr = mbox->sge_array->addr[0];
  9675. /* Set up the SGL pages in the non-embedded DMA pages */
  9676. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  9677. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  9678. pg_pairs = 0;
  9679. list_for_each_entry(psb, sblist, list) {
  9680. /* Set up the sge entry */
  9681. sgl_pg_pairs->sgl_pg0_addr_lo =
  9682. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  9683. sgl_pg_pairs->sgl_pg0_addr_hi =
  9684. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  9685. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  9686. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  9687. else
  9688. pdma_phys_bpl1 = 0;
  9689. sgl_pg_pairs->sgl_pg1_addr_lo =
  9690. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  9691. sgl_pg_pairs->sgl_pg1_addr_hi =
  9692. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  9693. /* Keep the first xritag on the list */
  9694. if (pg_pairs == 0)
  9695. xritag_start = psb->cur_iocbq.sli4_xritag;
  9696. sgl_pg_pairs++;
  9697. pg_pairs++;
  9698. }
  9699. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  9700. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  9701. /* Perform endian conversion if necessary */
  9702. sgl->word0 = cpu_to_le32(sgl->word0);
  9703. if (!phba->sli4_hba.intr_enable)
  9704. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  9705. else {
  9706. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  9707. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  9708. }
  9709. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  9710. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9711. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9712. if (rc != MBX_TIMEOUT)
  9713. lpfc_sli4_mbox_cmd_free(phba, mbox);
  9714. if (shdr_status || shdr_add_status || rc) {
  9715. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9716. "2564 POST_SGL_BLOCK mailbox command failed "
  9717. "status x%x add_status x%x mbx status x%x\n",
  9718. shdr_status, shdr_add_status, rc);
  9719. rc = -ENXIO;
  9720. }
  9721. return rc;
  9722. }
  9723. /**
  9724. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  9725. * @phba: pointer to lpfc_hba struct that the frame was received on
  9726. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9727. *
  9728. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  9729. * valid type of frame that the LPFC driver will handle. This function will
  9730. * return a zero if the frame is a valid frame or a non zero value when the
  9731. * frame does not pass the check.
  9732. **/
  9733. static int
  9734. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  9735. {
  9736. char *rctl_names[] = FC_RCTL_NAMES_INIT;
  9737. char *type_names[] = FC_TYPE_NAMES_INIT;
  9738. struct fc_vft_header *fc_vft_hdr;
  9739. switch (fc_hdr->fh_r_ctl) {
  9740. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  9741. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  9742. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  9743. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  9744. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  9745. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  9746. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  9747. case FC_RCTL_DD_CMD_STATUS: /* command status */
  9748. case FC_RCTL_ELS_REQ: /* extended link services request */
  9749. case FC_RCTL_ELS_REP: /* extended link services reply */
  9750. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  9751. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  9752. case FC_RCTL_BA_NOP: /* basic link service NOP */
  9753. case FC_RCTL_BA_ABTS: /* basic link service abort */
  9754. case FC_RCTL_BA_RMC: /* remove connection */
  9755. case FC_RCTL_BA_ACC: /* basic accept */
  9756. case FC_RCTL_BA_RJT: /* basic reject */
  9757. case FC_RCTL_BA_PRMT:
  9758. case FC_RCTL_ACK_1: /* acknowledge_1 */
  9759. case FC_RCTL_ACK_0: /* acknowledge_0 */
  9760. case FC_RCTL_P_RJT: /* port reject */
  9761. case FC_RCTL_F_RJT: /* fabric reject */
  9762. case FC_RCTL_P_BSY: /* port busy */
  9763. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  9764. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  9765. case FC_RCTL_LCR: /* link credit reset */
  9766. case FC_RCTL_END: /* end */
  9767. break;
  9768. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  9769. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  9770. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  9771. return lpfc_fc_frame_check(phba, fc_hdr);
  9772. default:
  9773. goto drop;
  9774. }
  9775. switch (fc_hdr->fh_type) {
  9776. case FC_TYPE_BLS:
  9777. case FC_TYPE_ELS:
  9778. case FC_TYPE_FCP:
  9779. case FC_TYPE_CT:
  9780. break;
  9781. case FC_TYPE_IP:
  9782. case FC_TYPE_ILS:
  9783. default:
  9784. goto drop;
  9785. }
  9786. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  9787. "2538 Received frame rctl:%s type:%s\n",
  9788. rctl_names[fc_hdr->fh_r_ctl],
  9789. type_names[fc_hdr->fh_type]);
  9790. return 0;
  9791. drop:
  9792. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  9793. "2539 Dropped frame rctl:%s type:%s\n",
  9794. rctl_names[fc_hdr->fh_r_ctl],
  9795. type_names[fc_hdr->fh_type]);
  9796. return 1;
  9797. }
  9798. /**
  9799. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  9800. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9801. *
  9802. * This function processes the FC header to retrieve the VFI from the VF
  9803. * header, if one exists. This function will return the VFI if one exists
  9804. * or 0 if no VSAN Header exists.
  9805. **/
  9806. static uint32_t
  9807. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  9808. {
  9809. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  9810. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  9811. return 0;
  9812. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  9813. }
  9814. /**
  9815. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  9816. * @phba: Pointer to the HBA structure to search for the vport on
  9817. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  9818. * @fcfi: The FC Fabric ID that the frame came from
  9819. *
  9820. * This function searches the @phba for a vport that matches the content of the
  9821. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  9822. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  9823. * returns the matching vport pointer or NULL if unable to match frame to a
  9824. * vport.
  9825. **/
  9826. static struct lpfc_vport *
  9827. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  9828. uint16_t fcfi)
  9829. {
  9830. struct lpfc_vport **vports;
  9831. struct lpfc_vport *vport = NULL;
  9832. int i;
  9833. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  9834. fc_hdr->fh_d_id[1] << 8 |
  9835. fc_hdr->fh_d_id[2]);
  9836. vports = lpfc_create_vport_work_array(phba);
  9837. if (vports != NULL)
  9838. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  9839. if (phba->fcf.fcfi == fcfi &&
  9840. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  9841. vports[i]->fc_myDID == did) {
  9842. vport = vports[i];
  9843. break;
  9844. }
  9845. }
  9846. lpfc_destroy_vport_work_array(phba, vports);
  9847. return vport;
  9848. }
  9849. /**
  9850. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  9851. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  9852. *
  9853. * This function searches through the existing incomplete sequences that have
  9854. * been sent to this @vport. If the frame matches one of the incomplete
  9855. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  9856. * make up that sequence. If no sequence is found that matches this frame then
  9857. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  9858. * This function returns a pointer to the first dmabuf in the sequence list that
  9859. * the frame was linked to.
  9860. **/
  9861. static struct hbq_dmabuf *
  9862. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  9863. {
  9864. struct fc_frame_header *new_hdr;
  9865. struct fc_frame_header *temp_hdr;
  9866. struct lpfc_dmabuf *d_buf;
  9867. struct lpfc_dmabuf *h_buf;
  9868. struct hbq_dmabuf *seq_dmabuf = NULL;
  9869. struct hbq_dmabuf *temp_dmabuf = NULL;
  9870. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  9871. /* Use the hdr_buf to find the sequence that this frame belongs to */
  9872. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  9873. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  9874. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  9875. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  9876. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  9877. continue;
  9878. /* found a pending sequence that matches this frame */
  9879. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  9880. break;
  9881. }
  9882. if (!seq_dmabuf) {
  9883. /*
  9884. * This indicates first frame received for this sequence.
  9885. * Queue the buffer on the vport's rcv_buffer_list.
  9886. */
  9887. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  9888. return dmabuf;
  9889. }
  9890. temp_hdr = seq_dmabuf->hbuf.virt;
  9891. if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
  9892. list_add(&seq_dmabuf->dbuf.list, &dmabuf->dbuf.list);
  9893. return dmabuf;
  9894. }
  9895. /* find the correct place in the sequence to insert this frame */
  9896. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  9897. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  9898. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  9899. /*
  9900. * If the frame's sequence count is greater than the frame on
  9901. * the list then insert the frame right after this frame
  9902. */
  9903. if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
  9904. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  9905. return seq_dmabuf;
  9906. }
  9907. }
  9908. return NULL;
  9909. }
  9910. /**
  9911. * lpfc_seq_complete - Indicates if a sequence is complete
  9912. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  9913. *
  9914. * This function checks the sequence, starting with the frame described by
  9915. * @dmabuf, to see if all the frames associated with this sequence are present.
  9916. * the frames associated with this sequence are linked to the @dmabuf using the
  9917. * dbuf list. This function looks for two major things. 1) That the first frame
  9918. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  9919. * set. 3) That there are no holes in the sequence count. The function will
  9920. * return 1 when the sequence is complete, otherwise it will return 0.
  9921. **/
  9922. static int
  9923. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  9924. {
  9925. struct fc_frame_header *hdr;
  9926. struct lpfc_dmabuf *d_buf;
  9927. struct hbq_dmabuf *seq_dmabuf;
  9928. uint32_t fctl;
  9929. int seq_count = 0;
  9930. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  9931. /* make sure first fame of sequence has a sequence count of zero */
  9932. if (hdr->fh_seq_cnt != seq_count)
  9933. return 0;
  9934. fctl = (hdr->fh_f_ctl[0] << 16 |
  9935. hdr->fh_f_ctl[1] << 8 |
  9936. hdr->fh_f_ctl[2]);
  9937. /* If last frame of sequence we can return success. */
  9938. if (fctl & FC_FC_END_SEQ)
  9939. return 1;
  9940. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  9941. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  9942. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  9943. /* If there is a hole in the sequence count then fail. */
  9944. if (++seq_count != hdr->fh_seq_cnt)
  9945. return 0;
  9946. fctl = (hdr->fh_f_ctl[0] << 16 |
  9947. hdr->fh_f_ctl[1] << 8 |
  9948. hdr->fh_f_ctl[2]);
  9949. /* If last frame of sequence we can return success. */
  9950. if (fctl & FC_FC_END_SEQ)
  9951. return 1;
  9952. }
  9953. return 0;
  9954. }
  9955. /**
  9956. * lpfc_prep_seq - Prep sequence for ULP processing
  9957. * @vport: Pointer to the vport on which this sequence was received
  9958. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  9959. *
  9960. * This function takes a sequence, described by a list of frames, and creates
  9961. * a list of iocbq structures to describe the sequence. This iocbq list will be
  9962. * used to issue to the generic unsolicited sequence handler. This routine
  9963. * returns a pointer to the first iocbq in the list. If the function is unable
  9964. * to allocate an iocbq then it throw out the received frames that were not
  9965. * able to be described and return a pointer to the first iocbq. If unable to
  9966. * allocate any iocbqs (including the first) this function will return NULL.
  9967. **/
  9968. static struct lpfc_iocbq *
  9969. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  9970. {
  9971. struct lpfc_dmabuf *d_buf, *n_buf;
  9972. struct lpfc_iocbq *first_iocbq, *iocbq;
  9973. struct fc_frame_header *fc_hdr;
  9974. uint32_t sid;
  9975. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  9976. /* remove from receive buffer list */
  9977. list_del_init(&seq_dmabuf->hbuf.list);
  9978. /* get the Remote Port's SID */
  9979. sid = (fc_hdr->fh_s_id[0] << 16 |
  9980. fc_hdr->fh_s_id[1] << 8 |
  9981. fc_hdr->fh_s_id[2]);
  9982. /* Get an iocbq struct to fill in. */
  9983. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  9984. if (first_iocbq) {
  9985. /* Initialize the first IOCB. */
  9986. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  9987. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  9988. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  9989. first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
  9990. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  9991. vport->vpi + vport->phba->vpi_base;
  9992. /* put the first buffer into the first IOCBq */
  9993. first_iocbq->context2 = &seq_dmabuf->dbuf;
  9994. first_iocbq->context3 = NULL;
  9995. first_iocbq->iocb.ulpBdeCount = 1;
  9996. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  9997. LPFC_DATA_BUF_SIZE;
  9998. first_iocbq->iocb.un.rcvels.remoteID = sid;
  9999. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  10000. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  10001. }
  10002. iocbq = first_iocbq;
  10003. /*
  10004. * Each IOCBq can have two Buffers assigned, so go through the list
  10005. * of buffers for this sequence and save two buffers in each IOCBq
  10006. */
  10007. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  10008. if (!iocbq) {
  10009. lpfc_in_buf_free(vport->phba, d_buf);
  10010. continue;
  10011. }
  10012. if (!iocbq->context3) {
  10013. iocbq->context3 = d_buf;
  10014. iocbq->iocb.ulpBdeCount++;
  10015. iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
  10016. LPFC_DATA_BUF_SIZE;
  10017. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  10018. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  10019. } else {
  10020. iocbq = lpfc_sli_get_iocbq(vport->phba);
  10021. if (!iocbq) {
  10022. if (first_iocbq) {
  10023. first_iocbq->iocb.ulpStatus =
  10024. IOSTAT_FCP_RSP_ERROR;
  10025. first_iocbq->iocb.un.ulpWord[4] =
  10026. IOERR_NO_RESOURCES;
  10027. }
  10028. lpfc_in_buf_free(vport->phba, d_buf);
  10029. continue;
  10030. }
  10031. iocbq->context2 = d_buf;
  10032. iocbq->context3 = NULL;
  10033. iocbq->iocb.ulpBdeCount = 1;
  10034. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  10035. LPFC_DATA_BUF_SIZE;
  10036. first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
  10037. bf_get(lpfc_rcqe_length, &seq_dmabuf->rcqe);
  10038. iocbq->iocb.un.rcvels.remoteID = sid;
  10039. list_add_tail(&iocbq->list, &first_iocbq->list);
  10040. }
  10041. }
  10042. return first_iocbq;
  10043. }
  10044. /**
  10045. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  10046. * @phba: Pointer to HBA context object.
  10047. *
  10048. * This function is called with no lock held. This function processes all
  10049. * the received buffers and gives it to upper layers when a received buffer
  10050. * indicates that it is the final frame in the sequence. The interrupt
  10051. * service routine processes received buffers at interrupt contexts and adds
  10052. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  10053. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  10054. * appropriate receive function when the final frame in a sequence is received.
  10055. **/
  10056. int
  10057. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba)
  10058. {
  10059. LIST_HEAD(cmplq);
  10060. struct hbq_dmabuf *dmabuf, *seq_dmabuf;
  10061. struct fc_frame_header *fc_hdr;
  10062. struct lpfc_vport *vport;
  10063. uint32_t fcfi;
  10064. struct lpfc_iocbq *iocbq;
  10065. /* Clear hba flag and get all received buffers into the cmplq */
  10066. spin_lock_irq(&phba->hbalock);
  10067. phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
  10068. list_splice_init(&phba->rb_pend_list, &cmplq);
  10069. spin_unlock_irq(&phba->hbalock);
  10070. /* Process each received buffer */
  10071. while ((dmabuf = lpfc_sli_hbqbuf_get(&cmplq)) != NULL) {
  10072. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  10073. /* check to see if this a valid type of frame */
  10074. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  10075. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10076. continue;
  10077. }
  10078. fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->rcqe);
  10079. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  10080. if (!vport) {
  10081. /* throw out the frame */
  10082. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10083. continue;
  10084. }
  10085. /* Link this frame */
  10086. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  10087. if (!seq_dmabuf) {
  10088. /* unable to add frame to vport - throw it out */
  10089. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  10090. continue;
  10091. }
  10092. /* If not last frame in sequence continue processing frames. */
  10093. if (!lpfc_seq_complete(seq_dmabuf)) {
  10094. /*
  10095. * When saving off frames post a new one and mark this
  10096. * frame to be freed when it is finished.
  10097. **/
  10098. lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
  10099. dmabuf->tag = -1;
  10100. continue;
  10101. }
  10102. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  10103. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  10104. if (!lpfc_complete_unsol_iocb(phba,
  10105. &phba->sli.ring[LPFC_ELS_RING],
  10106. iocbq, fc_hdr->fh_r_ctl,
  10107. fc_hdr->fh_type))
  10108. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10109. "2540 Ring %d handler: unexpected Rctl "
  10110. "x%x Type x%x received\n",
  10111. LPFC_ELS_RING,
  10112. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  10113. };
  10114. return 0;
  10115. }
  10116. /**
  10117. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  10118. * @phba: pointer to lpfc hba data structure.
  10119. *
  10120. * This routine is invoked to post rpi header templates to the
  10121. * HBA consistent with the SLI-4 interface spec. This routine
  10122. * posts a PAGE_SIZE memory region to the port to hold up to
  10123. * PAGE_SIZE modulo 64 rpi context headers.
  10124. *
  10125. * This routine does not require any locks. It's usage is expected
  10126. * to be driver load or reset recovery when the driver is
  10127. * sequential.
  10128. *
  10129. * Return codes
  10130. * 0 - sucessful
  10131. * EIO - The mailbox failed to complete successfully.
  10132. * When this error occurs, the driver is not guaranteed
  10133. * to have any rpi regions posted to the device and
  10134. * must either attempt to repost the regions or take a
  10135. * fatal error.
  10136. **/
  10137. int
  10138. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  10139. {
  10140. struct lpfc_rpi_hdr *rpi_page;
  10141. uint32_t rc = 0;
  10142. /* Post all rpi memory regions to the port. */
  10143. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  10144. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  10145. if (rc != MBX_SUCCESS) {
  10146. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10147. "2008 Error %d posting all rpi "
  10148. "headers\n", rc);
  10149. rc = -EIO;
  10150. break;
  10151. }
  10152. }
  10153. return rc;
  10154. }
  10155. /**
  10156. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  10157. * @phba: pointer to lpfc hba data structure.
  10158. * @rpi_page: pointer to the rpi memory region.
  10159. *
  10160. * This routine is invoked to post a single rpi header to the
  10161. * HBA consistent with the SLI-4 interface spec. This memory region
  10162. * maps up to 64 rpi context regions.
  10163. *
  10164. * Return codes
  10165. * 0 - sucessful
  10166. * ENOMEM - No available memory
  10167. * EIO - The mailbox failed to complete successfully.
  10168. **/
  10169. int
  10170. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  10171. {
  10172. LPFC_MBOXQ_t *mboxq;
  10173. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  10174. uint32_t rc = 0;
  10175. uint32_t mbox_tmo;
  10176. uint32_t shdr_status, shdr_add_status;
  10177. union lpfc_sli4_cfg_shdr *shdr;
  10178. /* The port is notified of the header region via a mailbox command. */
  10179. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10180. if (!mboxq) {
  10181. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10182. "2001 Unable to allocate memory for issuing "
  10183. "SLI_CONFIG_SPECIAL mailbox command\n");
  10184. return -ENOMEM;
  10185. }
  10186. /* Post all rpi memory regions to the port. */
  10187. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  10188. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
  10189. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10190. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  10191. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  10192. sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
  10193. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  10194. hdr_tmpl, rpi_page->page_count);
  10195. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  10196. rpi_page->start_rpi);
  10197. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  10198. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  10199. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10200. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  10201. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10202. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10203. if (rc != MBX_TIMEOUT)
  10204. mempool_free(mboxq, phba->mbox_mem_pool);
  10205. if (shdr_status || shdr_add_status || rc) {
  10206. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10207. "2514 POST_RPI_HDR mailbox failed with "
  10208. "status x%x add_status x%x, mbx status x%x\n",
  10209. shdr_status, shdr_add_status, rc);
  10210. rc = -ENXIO;
  10211. }
  10212. return rc;
  10213. }
  10214. /**
  10215. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  10216. * @phba: pointer to lpfc hba data structure.
  10217. *
  10218. * This routine is invoked to post rpi header templates to the
  10219. * HBA consistent with the SLI-4 interface spec. This routine
  10220. * posts a PAGE_SIZE memory region to the port to hold up to
  10221. * PAGE_SIZE modulo 64 rpi context headers.
  10222. *
  10223. * Returns
  10224. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
  10225. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  10226. **/
  10227. int
  10228. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  10229. {
  10230. int rpi;
  10231. uint16_t max_rpi, rpi_base, rpi_limit;
  10232. uint16_t rpi_remaining;
  10233. struct lpfc_rpi_hdr *rpi_hdr;
  10234. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  10235. rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
  10236. rpi_limit = phba->sli4_hba.next_rpi;
  10237. /*
  10238. * The valid rpi range is not guaranteed to be zero-based. Start
  10239. * the search at the rpi_base as reported by the port.
  10240. */
  10241. spin_lock_irq(&phba->hbalock);
  10242. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
  10243. if (rpi >= rpi_limit || rpi < rpi_base)
  10244. rpi = LPFC_RPI_ALLOC_ERROR;
  10245. else {
  10246. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  10247. phba->sli4_hba.max_cfg_param.rpi_used++;
  10248. phba->sli4_hba.rpi_count++;
  10249. }
  10250. /*
  10251. * Don't try to allocate more rpi header regions if the device limit
  10252. * on available rpis max has been exhausted.
  10253. */
  10254. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  10255. (phba->sli4_hba.rpi_count >= max_rpi)) {
  10256. spin_unlock_irq(&phba->hbalock);
  10257. return rpi;
  10258. }
  10259. /*
  10260. * If the driver is running low on rpi resources, allocate another
  10261. * page now. Note that the next_rpi value is used because
  10262. * it represents how many are actually in use whereas max_rpi notes
  10263. * how many are supported max by the device.
  10264. */
  10265. rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
  10266. phba->sli4_hba.rpi_count;
  10267. spin_unlock_irq(&phba->hbalock);
  10268. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  10269. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  10270. if (!rpi_hdr) {
  10271. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10272. "2002 Error Could not grow rpi "
  10273. "count\n");
  10274. } else {
  10275. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  10276. }
  10277. }
  10278. return rpi;
  10279. }
  10280. /**
  10281. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  10282. * @phba: pointer to lpfc hba data structure.
  10283. *
  10284. * This routine is invoked to release an rpi to the pool of
  10285. * available rpis maintained by the driver.
  10286. **/
  10287. void
  10288. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  10289. {
  10290. spin_lock_irq(&phba->hbalock);
  10291. clear_bit(rpi, phba->sli4_hba.rpi_bmask);
  10292. phba->sli4_hba.rpi_count--;
  10293. phba->sli4_hba.max_cfg_param.rpi_used--;
  10294. spin_unlock_irq(&phba->hbalock);
  10295. }
  10296. /**
  10297. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  10298. * @phba: pointer to lpfc hba data structure.
  10299. *
  10300. * This routine is invoked to remove the memory region that
  10301. * provided rpi via a bitmask.
  10302. **/
  10303. void
  10304. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  10305. {
  10306. kfree(phba->sli4_hba.rpi_bmask);
  10307. }
  10308. /**
  10309. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  10310. * @phba: pointer to lpfc hba data structure.
  10311. *
  10312. * This routine is invoked to remove the memory region that
  10313. * provided rpi via a bitmask.
  10314. **/
  10315. int
  10316. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
  10317. {
  10318. LPFC_MBOXQ_t *mboxq;
  10319. struct lpfc_hba *phba = ndlp->phba;
  10320. int rc;
  10321. /* The port is notified of the header region via a mailbox command. */
  10322. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10323. if (!mboxq)
  10324. return -ENOMEM;
  10325. /* Post all rpi memory regions to the port. */
  10326. lpfc_resume_rpi(mboxq, ndlp);
  10327. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10328. if (rc == MBX_NOT_FINISHED) {
  10329. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10330. "2010 Resume RPI Mailbox failed "
  10331. "status %d, mbxStatus x%x\n", rc,
  10332. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  10333. mempool_free(mboxq, phba->mbox_mem_pool);
  10334. return -EIO;
  10335. }
  10336. return 0;
  10337. }
  10338. /**
  10339. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  10340. * @phba: pointer to lpfc hba data structure.
  10341. * @vpi: vpi value to activate with the port.
  10342. *
  10343. * This routine is invoked to activate a vpi with the
  10344. * port when the host intends to use vports with a
  10345. * nonzero vpi.
  10346. *
  10347. * Returns:
  10348. * 0 success
  10349. * -Evalue otherwise
  10350. **/
  10351. int
  10352. lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
  10353. {
  10354. LPFC_MBOXQ_t *mboxq;
  10355. int rc = 0;
  10356. uint32_t mbox_tmo;
  10357. if (vpi == 0)
  10358. return -EINVAL;
  10359. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10360. if (!mboxq)
  10361. return -ENOMEM;
  10362. lpfc_init_vpi(phba, mboxq, vpi);
  10363. mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
  10364. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  10365. if (rc != MBX_TIMEOUT)
  10366. mempool_free(mboxq, phba->mbox_mem_pool);
  10367. if (rc != MBX_SUCCESS) {
  10368. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10369. "2022 INIT VPI Mailbox failed "
  10370. "status %d, mbxStatus x%x\n", rc,
  10371. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  10372. rc = -EIO;
  10373. }
  10374. return rc;
  10375. }
  10376. /**
  10377. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  10378. * @phba: pointer to lpfc hba data structure.
  10379. * @mboxq: Pointer to mailbox object.
  10380. *
  10381. * This routine is invoked to manually add a single FCF record. The caller
  10382. * must pass a completely initialized FCF_Record. This routine takes
  10383. * care of the nonembedded mailbox operations.
  10384. **/
  10385. static void
  10386. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  10387. {
  10388. void *virt_addr;
  10389. union lpfc_sli4_cfg_shdr *shdr;
  10390. uint32_t shdr_status, shdr_add_status;
  10391. virt_addr = mboxq->sge_array->addr[0];
  10392. /* The IOCTL status is embedded in the mailbox subheader. */
  10393. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  10394. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10395. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10396. if ((shdr_status || shdr_add_status) &&
  10397. (shdr_status != STATUS_FCF_IN_USE))
  10398. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10399. "2558 ADD_FCF_RECORD mailbox failed with "
  10400. "status x%x add_status x%x\n",
  10401. shdr_status, shdr_add_status);
  10402. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10403. }
  10404. /**
  10405. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  10406. * @phba: pointer to lpfc hba data structure.
  10407. * @fcf_record: pointer to the initialized fcf record to add.
  10408. *
  10409. * This routine is invoked to manually add a single FCF record. The caller
  10410. * must pass a completely initialized FCF_Record. This routine takes
  10411. * care of the nonembedded mailbox operations.
  10412. **/
  10413. int
  10414. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  10415. {
  10416. int rc = 0;
  10417. LPFC_MBOXQ_t *mboxq;
  10418. uint8_t *bytep;
  10419. void *virt_addr;
  10420. dma_addr_t phys_addr;
  10421. struct lpfc_mbx_sge sge;
  10422. uint32_t alloc_len, req_len;
  10423. uint32_t fcfindex;
  10424. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10425. if (!mboxq) {
  10426. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10427. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  10428. return -ENOMEM;
  10429. }
  10430. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  10431. sizeof(uint32_t);
  10432. /* Allocate DMA memory and set up the non-embedded mailbox command */
  10433. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10434. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  10435. req_len, LPFC_SLI4_MBX_NEMBED);
  10436. if (alloc_len < req_len) {
  10437. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10438. "2523 Allocated DMA memory size (x%x) is "
  10439. "less than the requested DMA memory "
  10440. "size (x%x)\n", alloc_len, req_len);
  10441. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10442. return -ENOMEM;
  10443. }
  10444. /*
  10445. * Get the first SGE entry from the non-embedded DMA memory. This
  10446. * routine only uses a single SGE.
  10447. */
  10448. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  10449. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  10450. if (unlikely(!mboxq->sge_array)) {
  10451. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10452. "2526 Failed to get the non-embedded SGE "
  10453. "virtual address\n");
  10454. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10455. return -ENOMEM;
  10456. }
  10457. virt_addr = mboxq->sge_array->addr[0];
  10458. /*
  10459. * Configure the FCF record for FCFI 0. This is the driver's
  10460. * hardcoded default and gets used in nonFIP mode.
  10461. */
  10462. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  10463. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  10464. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  10465. /*
  10466. * Copy the fcf_index and the FCF Record Data. The data starts after
  10467. * the FCoE header plus word10. The data copy needs to be endian
  10468. * correct.
  10469. */
  10470. bytep += sizeof(uint32_t);
  10471. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  10472. mboxq->vport = phba->pport;
  10473. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  10474. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10475. if (rc == MBX_NOT_FINISHED) {
  10476. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10477. "2515 ADD_FCF_RECORD mailbox failed with "
  10478. "status 0x%x\n", rc);
  10479. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10480. rc = -EIO;
  10481. } else
  10482. rc = 0;
  10483. return rc;
  10484. }
  10485. /**
  10486. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  10487. * @phba: pointer to lpfc hba data structure.
  10488. * @fcf_record: pointer to the fcf record to write the default data.
  10489. * @fcf_index: FCF table entry index.
  10490. *
  10491. * This routine is invoked to build the driver's default FCF record. The
  10492. * values used are hardcoded. This routine handles memory initialization.
  10493. *
  10494. **/
  10495. void
  10496. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  10497. struct fcf_record *fcf_record,
  10498. uint16_t fcf_index)
  10499. {
  10500. memset(fcf_record, 0, sizeof(struct fcf_record));
  10501. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  10502. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  10503. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  10504. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  10505. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  10506. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  10507. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  10508. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  10509. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  10510. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  10511. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  10512. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  10513. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  10514. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  10515. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  10516. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  10517. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  10518. /* Set the VLAN bit map */
  10519. if (phba->valid_vlan) {
  10520. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  10521. = 1 << (phba->vlan_id % 8);
  10522. }
  10523. }
  10524. /**
  10525. * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
  10526. * @phba: pointer to lpfc hba data structure.
  10527. * @fcf_index: FCF table entry offset.
  10528. *
  10529. * This routine is invoked to read up to @fcf_num of FCF record from the
  10530. * device starting with the given @fcf_index.
  10531. **/
  10532. int
  10533. lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
  10534. {
  10535. int rc = 0, error;
  10536. LPFC_MBOXQ_t *mboxq;
  10537. void *virt_addr;
  10538. dma_addr_t phys_addr;
  10539. uint8_t *bytep;
  10540. struct lpfc_mbx_sge sge;
  10541. uint32_t alloc_len, req_len;
  10542. struct lpfc_mbx_read_fcf_tbl *read_fcf;
  10543. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  10544. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10545. if (!mboxq) {
  10546. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10547. "2000 Failed to allocate mbox for "
  10548. "READ_FCF cmd\n");
  10549. return -ENOMEM;
  10550. }
  10551. req_len = sizeof(struct fcf_record) +
  10552. sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
  10553. /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
  10554. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  10555. LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
  10556. LPFC_SLI4_MBX_NEMBED);
  10557. if (alloc_len < req_len) {
  10558. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10559. "0291 Allocated DMA memory size (x%x) is "
  10560. "less than the requested DMA memory "
  10561. "size (x%x)\n", alloc_len, req_len);
  10562. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10563. return -ENOMEM;
  10564. }
  10565. /* Get the first SGE entry from the non-embedded DMA memory. This
  10566. * routine only uses a single SGE.
  10567. */
  10568. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  10569. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  10570. if (unlikely(!mboxq->sge_array)) {
  10571. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10572. "2527 Failed to get the non-embedded SGE "
  10573. "virtual address\n");
  10574. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10575. return -ENOMEM;
  10576. }
  10577. virt_addr = mboxq->sge_array->addr[0];
  10578. read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
  10579. /* Set up command fields */
  10580. bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
  10581. /* Perform necessary endian conversion */
  10582. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  10583. lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
  10584. mboxq->vport = phba->pport;
  10585. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
  10586. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  10587. if (rc == MBX_NOT_FINISHED) {
  10588. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  10589. error = -EIO;
  10590. } else {
  10591. spin_lock_irq(&phba->hbalock);
  10592. phba->hba_flag |= FCF_DISC_INPROGRESS;
  10593. spin_unlock_irq(&phba->hbalock);
  10594. error = 0;
  10595. }
  10596. return error;
  10597. }
  10598. /**
  10599. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  10600. * @phba: pointer to lpfc hba data structure.
  10601. *
  10602. * This function read region 23 and parse TLV for port status to
  10603. * decide if the user disaled the port. If the TLV indicates the
  10604. * port is disabled, the hba_flag is set accordingly.
  10605. **/
  10606. void
  10607. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  10608. {
  10609. LPFC_MBOXQ_t *pmb = NULL;
  10610. MAILBOX_t *mb;
  10611. uint8_t *rgn23_data = NULL;
  10612. uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
  10613. int rc;
  10614. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10615. if (!pmb) {
  10616. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10617. "2600 lpfc_sli_read_serdes_param failed to"
  10618. " allocate mailbox memory\n");
  10619. goto out;
  10620. }
  10621. mb = &pmb->u.mb;
  10622. /* Get adapter Region 23 data */
  10623. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  10624. if (!rgn23_data)
  10625. goto out;
  10626. do {
  10627. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  10628. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  10629. if (rc != MBX_SUCCESS) {
  10630. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10631. "2601 lpfc_sli_read_link_ste failed to"
  10632. " read config region 23 rc 0x%x Status 0x%x\n",
  10633. rc, mb->mbxStatus);
  10634. mb->un.varDmp.word_cnt = 0;
  10635. }
  10636. /*
  10637. * dump mem may return a zero when finished or we got a
  10638. * mailbox error, either way we are done.
  10639. */
  10640. if (mb->un.varDmp.word_cnt == 0)
  10641. break;
  10642. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  10643. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  10644. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  10645. rgn23_data + offset,
  10646. mb->un.varDmp.word_cnt);
  10647. offset += mb->un.varDmp.word_cnt;
  10648. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  10649. data_size = offset;
  10650. offset = 0;
  10651. if (!data_size)
  10652. goto out;
  10653. /* Check the region signature first */
  10654. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  10655. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10656. "2619 Config region 23 has bad signature\n");
  10657. goto out;
  10658. }
  10659. offset += 4;
  10660. /* Check the data structure version */
  10661. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  10662. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10663. "2620 Config region 23 has bad version\n");
  10664. goto out;
  10665. }
  10666. offset += 4;
  10667. /* Parse TLV entries in the region */
  10668. while (offset < data_size) {
  10669. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  10670. break;
  10671. /*
  10672. * If the TLV is not driver specific TLV or driver id is
  10673. * not linux driver id, skip the record.
  10674. */
  10675. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  10676. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  10677. (rgn23_data[offset + 3] != 0)) {
  10678. offset += rgn23_data[offset + 1] * 4 + 4;
  10679. continue;
  10680. }
  10681. /* Driver found a driver specific TLV in the config region */
  10682. sub_tlv_len = rgn23_data[offset + 1] * 4;
  10683. offset += 4;
  10684. tlv_offset = 0;
  10685. /*
  10686. * Search for configured port state sub-TLV.
  10687. */
  10688. while ((offset < data_size) &&
  10689. (tlv_offset < sub_tlv_len)) {
  10690. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  10691. offset += 4;
  10692. tlv_offset += 4;
  10693. break;
  10694. }
  10695. if (rgn23_data[offset] != PORT_STE_TYPE) {
  10696. offset += rgn23_data[offset + 1] * 4 + 4;
  10697. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  10698. continue;
  10699. }
  10700. /* This HBA contains PORT_STE configured */
  10701. if (!rgn23_data[offset + 2])
  10702. phba->hba_flag |= LINK_DISABLED;
  10703. goto out;
  10704. }
  10705. }
  10706. out:
  10707. if (pmb)
  10708. mempool_free(pmb, phba->mbox_mem_pool);
  10709. kfree(rgn23_data);
  10710. return;
  10711. }