qla_inline.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. * @vha: HA context
  13. * @dsds: number of data segment decriptors needed
  14. *
  15. * Returns the number of IOCB entries needed to store @dsds.
  16. */
  17. static inline uint16_t
  18. qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
  19. {
  20. uint16_t iocbs;
  21. iocbs = 1;
  22. if (dsds > 1) {
  23. iocbs += (dsds - 1) / 5;
  24. if ((dsds - 1) % 5)
  25. iocbs++;
  26. }
  27. return iocbs;
  28. }
  29. /*
  30. * qla2x00_debounce_register
  31. * Debounce register.
  32. *
  33. * Input:
  34. * port = register address.
  35. *
  36. * Returns:
  37. * register value.
  38. */
  39. static __inline__ uint16_t
  40. qla2x00_debounce_register(volatile uint16_t __iomem *addr)
  41. {
  42. volatile uint16_t first;
  43. volatile uint16_t second;
  44. do {
  45. first = RD_REG_WORD(addr);
  46. barrier();
  47. cpu_relax();
  48. second = RD_REG_WORD(addr);
  49. } while (first != second);
  50. return (first);
  51. }
  52. static inline void
  53. qla2x00_poll(struct rsp_que *rsp)
  54. {
  55. struct qla_hw_data *ha = rsp->hw;
  56. if (IS_P3P_TYPE(ha))
  57. qla82xx_poll(0, rsp);
  58. else
  59. ha->isp_ops->intr_handler(0, rsp);
  60. }
  61. static inline uint8_t *
  62. host_to_fcp_swap(uint8_t *fcp, uint32_t bsize)
  63. {
  64. uint32_t *ifcp = (uint32_t *) fcp;
  65. uint32_t *ofcp = (uint32_t *) fcp;
  66. uint32_t iter = bsize >> 2;
  67. for (; iter ; iter--)
  68. *ofcp++ = swab32(*ifcp++);
  69. return fcp;
  70. }
  71. static inline void
  72. host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
  73. {
  74. uint32_t *isrc = (uint32_t *) src;
  75. __le32 *odest = (__le32 *) dst;
  76. uint32_t iter = bsize >> 2;
  77. for ( ; iter--; isrc++)
  78. *odest++ = cpu_to_le32(*isrc);
  79. }
  80. static inline void
  81. qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
  82. {
  83. int i;
  84. if (IS_FWI2_CAPABLE(ha))
  85. return;
  86. for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
  87. set_bit(i, ha->loop_id_map);
  88. set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
  89. set_bit(BROADCAST, ha->loop_id_map);
  90. }
  91. static inline int
  92. qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
  93. {
  94. struct qla_hw_data *ha = vha->hw;
  95. if (IS_FWI2_CAPABLE(ha))
  96. return (loop_id > NPH_LAST_HANDLE);
  97. return ((loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
  98. loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST);
  99. }
  100. static inline void
  101. qla2x00_clear_loop_id(fc_port_t *fcport) {
  102. struct qla_hw_data *ha = fcport->vha->hw;
  103. if (fcport->loop_id == FC_NO_LOOP_ID ||
  104. qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
  105. return;
  106. clear_bit(fcport->loop_id, ha->loop_id_map);
  107. fcport->loop_id = FC_NO_LOOP_ID;
  108. }
  109. static inline void
  110. qla2x00_clean_dsd_pool(struct qla_hw_data *ha, struct crc_context *ctx)
  111. {
  112. struct dsd_dma *dsd, *tdsd;
  113. /* clean up allocated prev pool */
  114. list_for_each_entry_safe(dsd, tdsd, &ctx->dsd_list, list) {
  115. dma_pool_free(ha->dl_dma_pool, dsd->dsd_addr,
  116. dsd->dsd_list_dma);
  117. list_del(&dsd->list);
  118. kfree(dsd);
  119. }
  120. INIT_LIST_HEAD(&ctx->dsd_list);
  121. }
  122. static inline void
  123. qla2x00_set_fcport_state(fc_port_t *fcport, int state)
  124. {
  125. int old_state;
  126. old_state = atomic_read(&fcport->state);
  127. atomic_set(&fcport->state, state);
  128. /* Don't print state transitions during initial allocation of fcport */
  129. if (old_state && old_state != state) {
  130. ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
  131. "FCPort %8phC state transitioned from %s to %s - "
  132. "portid=%02x%02x%02x.\n", fcport->port_name,
  133. port_state_str[old_state], port_state_str[state],
  134. fcport->d_id.b.domain, fcport->d_id.b.area,
  135. fcport->d_id.b.al_pa);
  136. }
  137. }
  138. static inline int
  139. qla2x00_hba_err_chk_enabled(srb_t *sp)
  140. {
  141. /*
  142. * Uncomment when corresponding SCSI changes are done.
  143. *
  144. if (!sp->cmd->prot_chk)
  145. return 0;
  146. *
  147. */
  148. switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
  149. case SCSI_PROT_READ_STRIP:
  150. case SCSI_PROT_WRITE_INSERT:
  151. if (ql2xenablehba_err_chk >= 1)
  152. return 1;
  153. break;
  154. case SCSI_PROT_READ_PASS:
  155. case SCSI_PROT_WRITE_PASS:
  156. if (ql2xenablehba_err_chk >= 2)
  157. return 1;
  158. break;
  159. case SCSI_PROT_READ_INSERT:
  160. case SCSI_PROT_WRITE_STRIP:
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static inline int
  166. qla2x00_reset_active(scsi_qla_host_t *vha)
  167. {
  168. scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
  169. /* Test appropriate base-vha and vha flags. */
  170. return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
  171. test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
  172. test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
  173. test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
  174. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
  175. }
  176. static inline int
  177. qla2x00_chip_is_down(scsi_qla_host_t *vha)
  178. {
  179. return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
  180. }
  181. static inline srb_t *
  182. qla2xxx_get_qpair_sp(struct qla_qpair *qpair, fc_port_t *fcport, gfp_t flag)
  183. {
  184. srb_t *sp = NULL;
  185. uint8_t bail;
  186. QLA_QPAIR_MARK_BUSY(qpair, bail);
  187. if (unlikely(bail))
  188. return NULL;
  189. sp = mempool_alloc(qpair->srb_mempool, flag);
  190. if (!sp)
  191. goto done;
  192. memset(sp, 0, sizeof(*sp));
  193. sp->fcport = fcport;
  194. sp->iocbs = 1;
  195. sp->vha = qpair->vha;
  196. INIT_LIST_HEAD(&sp->elem);
  197. done:
  198. if (!sp)
  199. QLA_QPAIR_MARK_NOT_BUSY(qpair);
  200. return sp;
  201. }
  202. static inline void
  203. qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
  204. {
  205. mempool_free(sp, qpair->srb_mempool);
  206. QLA_QPAIR_MARK_NOT_BUSY(qpair);
  207. }
  208. static inline srb_t *
  209. qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
  210. {
  211. srb_t *sp = NULL;
  212. uint8_t bail;
  213. QLA_VHA_MARK_BUSY(vha, bail);
  214. if (unlikely(bail))
  215. return NULL;
  216. sp = mempool_alloc(vha->hw->srb_mempool, flag);
  217. if (!sp)
  218. goto done;
  219. memset(sp, 0, sizeof(*sp));
  220. sp->fcport = fcport;
  221. sp->cmd_type = TYPE_SRB;
  222. sp->iocbs = 1;
  223. sp->vha = vha;
  224. done:
  225. if (!sp)
  226. QLA_VHA_MARK_NOT_BUSY(vha);
  227. return sp;
  228. }
  229. static inline void
  230. qla2x00_rel_sp(srb_t *sp)
  231. {
  232. QLA_VHA_MARK_NOT_BUSY(sp->vha);
  233. mempool_free(sp, sp->vha->hw->srb_mempool);
  234. }
  235. static inline void
  236. qla2x00_init_timer(srb_t *sp, unsigned long tmo)
  237. {
  238. timer_setup(&sp->u.iocb_cmd.timer, qla2x00_sp_timeout, 0);
  239. sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
  240. sp->free = qla2x00_sp_free;
  241. init_completion(&sp->comp);
  242. if (IS_QLAFX00(sp->vha->hw) && (sp->type == SRB_FXIOCB_DCMD))
  243. init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
  244. add_timer(&sp->u.iocb_cmd.timer);
  245. }
  246. static inline int
  247. qla2x00_gid_list_size(struct qla_hw_data *ha)
  248. {
  249. if (IS_QLAFX00(ha))
  250. return sizeof(uint32_t) * 32;
  251. else
  252. return sizeof(struct gid_list_info) * ha->max_fibre_devices;
  253. }
  254. static inline void
  255. qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
  256. {
  257. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  258. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  259. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  260. clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
  261. complete(&ha->mbx_intr_comp);
  262. }
  263. }
  264. static inline void
  265. qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
  266. {
  267. if (retry_delay)
  268. fcport->retry_delay_timestamp = jiffies +
  269. (retry_delay * HZ / 10);
  270. }
  271. static inline bool
  272. qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
  273. {
  274. if (qla_ini_mode_enabled(vha) &&
  275. (ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
  276. return true;
  277. else if (qla_tgt_mode_enabled(vha) &&
  278. (ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
  279. return true;
  280. else if (qla_dual_mode_enabled(vha) &&
  281. ((ql2xiniexchg + ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
  282. return true;
  283. else
  284. return false;
  285. }
  286. static inline void
  287. qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
  288. {
  289. qpair->cpuid = cpuid;
  290. if (!list_empty(&qpair->hints_list)) {
  291. struct qla_qpair_hint *h;
  292. list_for_each_entry(h, &qpair->hints_list, hint_elem)
  293. h->cpuid = qpair->cpuid;
  294. }
  295. }
  296. static inline struct qla_qpair_hint *
  297. qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
  298. {
  299. struct qla_qpair_hint *h;
  300. u16 i;
  301. for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
  302. h = &tgt->qphints[i];
  303. if (h->qpair == qpair)
  304. return h;
  305. }
  306. return NULL;
  307. }
  308. static inline void
  309. qla_83xx_start_iocbs(struct qla_qpair *qpair)
  310. {
  311. struct req_que *req = qpair->req;
  312. req->ring_index++;
  313. if (req->ring_index == req->length) {
  314. req->ring_index = 0;
  315. req->ring_ptr = req->ring;
  316. } else
  317. req->ring_ptr++;
  318. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  319. }