qla_inline.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2014 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_target.h"
  8. /**
  9. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  10. * Continuation Type 1 IOCBs to allocate.
  11. *
  12. * @dsds: number of data segment decriptors needed
  13. *
  14. * Returns the number of IOCB entries needed to store @dsds.
  15. */
  16. static inline uint16_t
  17. qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
  18. {
  19. uint16_t iocbs;
  20. iocbs = 1;
  21. if (dsds > 1) {
  22. iocbs += (dsds - 1) / 5;
  23. if ((dsds - 1) % 5)
  24. iocbs++;
  25. }
  26. return iocbs;
  27. }
  28. /*
  29. * qla2x00_debounce_register
  30. * Debounce register.
  31. *
  32. * Input:
  33. * port = register address.
  34. *
  35. * Returns:
  36. * register value.
  37. */
  38. static __inline__ uint16_t
  39. qla2x00_debounce_register(volatile uint16_t __iomem *addr)
  40. {
  41. volatile uint16_t first;
  42. volatile uint16_t second;
  43. do {
  44. first = RD_REG_WORD(addr);
  45. barrier();
  46. cpu_relax();
  47. second = RD_REG_WORD(addr);
  48. } while (first != second);
  49. return (first);
  50. }
  51. static inline void
  52. qla2x00_poll(struct rsp_que *rsp)
  53. {
  54. unsigned long flags;
  55. struct qla_hw_data *ha = rsp->hw;
  56. local_irq_save(flags);
  57. if (IS_P3P_TYPE(ha))
  58. qla82xx_poll(0, rsp);
  59. else
  60. ha->isp_ops->intr_handler(0, rsp);
  61. local_irq_restore(flags);
  62. }
  63. static inline uint8_t *
  64. host_to_fcp_swap(uint8_t *fcp, uint32_t bsize)
  65. {
  66. uint32_t *ifcp = (uint32_t *) fcp;
  67. uint32_t *ofcp = (uint32_t *) fcp;
  68. uint32_t iter = bsize >> 2;
  69. for (; iter ; iter--)
  70. *ofcp++ = swab32(*ifcp++);
  71. return fcp;
  72. }
  73. static inline void
  74. host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
  75. {
  76. uint32_t *isrc = (uint32_t *) src;
  77. __le32 *odest = (__le32 *) dst;
  78. uint32_t iter = bsize >> 2;
  79. for (; iter ; iter--)
  80. *odest++ = cpu_to_le32(*isrc++);
  81. }
  82. static inline void
  83. qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
  84. {
  85. int i;
  86. if (IS_FWI2_CAPABLE(ha))
  87. return;
  88. for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
  89. set_bit(i, ha->loop_id_map);
  90. set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
  91. set_bit(BROADCAST, ha->loop_id_map);
  92. }
  93. static inline int
  94. qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
  95. {
  96. struct qla_hw_data *ha = vha->hw;
  97. if (IS_FWI2_CAPABLE(ha))
  98. return (loop_id > NPH_LAST_HANDLE);
  99. return ((loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
  100. loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST);
  101. }
  102. static inline void
  103. qla2x00_clear_loop_id(fc_port_t *fcport) {
  104. struct qla_hw_data *ha = fcport->vha->hw;
  105. if (fcport->loop_id == FC_NO_LOOP_ID ||
  106. qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
  107. return;
  108. clear_bit(fcport->loop_id, ha->loop_id_map);
  109. fcport->loop_id = FC_NO_LOOP_ID;
  110. }
  111. static inline void
  112. qla2x00_clean_dsd_pool(struct qla_hw_data *ha, srb_t *sp,
  113. struct qla_tgt_cmd *tc)
  114. {
  115. struct dsd_dma *dsd_ptr, *tdsd_ptr;
  116. struct crc_context *ctx;
  117. if (sp)
  118. ctx = (struct crc_context *)GET_CMD_CTX_SP(sp);
  119. else if (tc)
  120. ctx = (struct crc_context *)tc->ctx;
  121. else {
  122. BUG();
  123. return;
  124. }
  125. /* clean up allocated prev pool */
  126. list_for_each_entry_safe(dsd_ptr, tdsd_ptr,
  127. &ctx->dsd_list, list) {
  128. dma_pool_free(ha->dl_dma_pool, dsd_ptr->dsd_addr,
  129. dsd_ptr->dsd_list_dma);
  130. list_del(&dsd_ptr->list);
  131. kfree(dsd_ptr);
  132. }
  133. INIT_LIST_HEAD(&ctx->dsd_list);
  134. }
  135. static inline void
  136. qla2x00_set_fcport_state(fc_port_t *fcport, int state)
  137. {
  138. int old_state;
  139. old_state = atomic_read(&fcport->state);
  140. atomic_set(&fcport->state, state);
  141. /* Don't print state transitions during initial allocation of fcport */
  142. if (old_state && old_state != state) {
  143. ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
  144. "FCPort state transitioned from %s to %s - "
  145. "portid=%02x%02x%02x.\n",
  146. port_state_str[old_state], port_state_str[state],
  147. fcport->d_id.b.domain, fcport->d_id.b.area,
  148. fcport->d_id.b.al_pa);
  149. }
  150. }
  151. static inline int
  152. qla2x00_hba_err_chk_enabled(srb_t *sp)
  153. {
  154. /*
  155. * Uncomment when corresponding SCSI changes are done.
  156. *
  157. if (!sp->cmd->prot_chk)
  158. return 0;
  159. *
  160. */
  161. switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
  162. case SCSI_PROT_READ_STRIP:
  163. case SCSI_PROT_WRITE_INSERT:
  164. if (ql2xenablehba_err_chk >= 1)
  165. return 1;
  166. break;
  167. case SCSI_PROT_READ_PASS:
  168. case SCSI_PROT_WRITE_PASS:
  169. if (ql2xenablehba_err_chk >= 2)
  170. return 1;
  171. break;
  172. case SCSI_PROT_READ_INSERT:
  173. case SCSI_PROT_WRITE_STRIP:
  174. return 1;
  175. }
  176. return 0;
  177. }
  178. static inline int
  179. qla2x00_reset_active(scsi_qla_host_t *vha)
  180. {
  181. scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
  182. /* Test appropriate base-vha and vha flags. */
  183. return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
  184. test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
  185. test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
  186. test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  187. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
  188. }
  189. static inline srb_t *
  190. qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
  191. {
  192. srb_t *sp = NULL;
  193. struct qla_hw_data *ha = vha->hw;
  194. uint8_t bail;
  195. QLA_VHA_MARK_BUSY(vha, bail);
  196. if (unlikely(bail))
  197. return NULL;
  198. sp = mempool_alloc(ha->srb_mempool, flag);
  199. if (!sp)
  200. goto done;
  201. memset(sp, 0, sizeof(*sp));
  202. sp->fcport = fcport;
  203. sp->iocbs = 1;
  204. done:
  205. if (!sp)
  206. QLA_VHA_MARK_NOT_BUSY(vha);
  207. return sp;
  208. }
  209. static inline void
  210. qla2x00_rel_sp(scsi_qla_host_t *vha, srb_t *sp)
  211. {
  212. mempool_free(sp, vha->hw->srb_mempool);
  213. QLA_VHA_MARK_NOT_BUSY(vha);
  214. }
  215. static inline void
  216. qla2x00_init_timer(srb_t *sp, unsigned long tmo)
  217. {
  218. init_timer(&sp->u.iocb_cmd.timer);
  219. sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
  220. sp->u.iocb_cmd.timer.data = (unsigned long)sp;
  221. sp->u.iocb_cmd.timer.function = qla2x00_sp_timeout;
  222. add_timer(&sp->u.iocb_cmd.timer);
  223. sp->free = qla2x00_sp_free;
  224. if ((IS_QLAFX00(sp->fcport->vha->hw)) &&
  225. (sp->type == SRB_FXIOCB_DCMD))
  226. init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
  227. }
  228. static inline int
  229. qla2x00_gid_list_size(struct qla_hw_data *ha)
  230. {
  231. if (IS_QLAFX00(ha))
  232. return sizeof(uint32_t) * 32;
  233. else
  234. return sizeof(struct gid_list_info) * ha->max_fibre_devices;
  235. }
  236. static inline void
  237. qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
  238. {
  239. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  240. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  241. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  242. clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
  243. complete(&ha->mbx_intr_comp);
  244. }
  245. }
  246. static inline void
  247. qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
  248. {
  249. if (retry_delay)
  250. fcport->retry_delay_timestamp = jiffies +
  251. (retry_delay * HZ / 10);
  252. }