2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2014 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
8 #include "qla_target.h"
10 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
11 * Continuation Type 1 IOCBs to allocate.
13 * @dsds: number of data segment decriptors needed
15 * Returns the number of IOCB entries needed to store @dsds.
17 static inline uint16_t
18 qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
24 iocbs += (dsds - 1) / 5;
32 * qla2x00_debounce_register
36 * port = register address.
41 static __inline__ uint16_t
42 qla2x00_debounce_register(volatile uint16_t __iomem *addr)
44 volatile uint16_t first;
45 volatile uint16_t second;
48 first = RD_REG_WORD(addr);
51 second = RD_REG_WORD(addr);
52 } while (first != second);
58 qla2x00_poll(struct rsp_que *rsp)
61 struct qla_hw_data *ha = rsp->hw;
62 local_irq_save(flags);
66 ha->isp_ops->intr_handler(0, rsp);
67 local_irq_restore(flags);
70 static inline uint8_t *
71 host_to_fcp_swap(uint8_t *fcp, uint32_t bsize)
73 uint32_t *ifcp = (uint32_t *) fcp;
74 uint32_t *ofcp = (uint32_t *) fcp;
75 uint32_t iter = bsize >> 2;
78 *ofcp++ = swab32(*ifcp++);
84 host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
86 uint32_t *isrc = (uint32_t *) src;
87 __le32 *odest = (__le32 *) dst;
88 uint32_t iter = bsize >> 2;
90 for ( ; iter--; isrc++)
91 *odest++ = cpu_to_le32(*isrc);
95 qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
99 if (IS_FWI2_CAPABLE(ha))
102 for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
103 set_bit(i, ha->loop_id_map);
104 set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
105 set_bit(BROADCAST, ha->loop_id_map);
109 qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
111 struct qla_hw_data *ha = vha->hw;
112 if (IS_FWI2_CAPABLE(ha))
113 return (loop_id > NPH_LAST_HANDLE);
115 return ((loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
116 loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST);
120 qla2x00_clear_loop_id(fc_port_t *fcport) {
121 struct qla_hw_data *ha = fcport->vha->hw;
123 if (fcport->loop_id == FC_NO_LOOP_ID ||
124 qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
127 clear_bit(fcport->loop_id, ha->loop_id_map);
128 fcport->loop_id = FC_NO_LOOP_ID;
132 qla2x00_clean_dsd_pool(struct qla_hw_data *ha, struct crc_context *ctx)
134 struct dsd_dma *dsd, *tdsd;
136 /* clean up allocated prev pool */
137 list_for_each_entry_safe(dsd, tdsd, &ctx->dsd_list, list) {
138 dma_pool_free(ha->dl_dma_pool, dsd->dsd_addr,
140 list_del(&dsd->list);
143 INIT_LIST_HEAD(&ctx->dsd_list);
147 qla2x00_set_fcport_state(fc_port_t *fcport, int state)
151 old_state = atomic_read(&fcport->state);
152 atomic_set(&fcport->state, state);
154 /* Don't print state transitions during initial allocation of fcport */
155 if (old_state && old_state != state) {
156 ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
157 "FCPort %8phC state transitioned from %s to %s - "
158 "portid=%02x%02x%02x.\n", fcport->port_name,
159 port_state_str[old_state], port_state_str[state],
160 fcport->d_id.b.domain, fcport->d_id.b.area,
161 fcport->d_id.b.al_pa);
166 qla2x00_hba_err_chk_enabled(srb_t *sp)
169 * Uncomment when corresponding SCSI changes are done.
171 if (!sp->cmd->prot_chk)
175 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
176 case SCSI_PROT_READ_STRIP:
177 case SCSI_PROT_WRITE_INSERT:
178 if (ql2xenablehba_err_chk >= 1)
181 case SCSI_PROT_READ_PASS:
182 case SCSI_PROT_WRITE_PASS:
183 if (ql2xenablehba_err_chk >= 2)
186 case SCSI_PROT_READ_INSERT:
187 case SCSI_PROT_WRITE_STRIP:
194 qla2x00_reset_active(scsi_qla_host_t *vha)
196 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
198 /* Test appropriate base-vha and vha flags. */
199 return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
200 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
201 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
202 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
203 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
206 static inline srb_t *
207 qla2xxx_get_qpair_sp(struct qla_qpair *qpair, fc_port_t *fcport, gfp_t flag)
212 QLA_QPAIR_MARK_BUSY(qpair, bail);
216 sp = mempool_alloc(qpair->srb_mempool, flag);
220 memset(sp, 0, sizeof(*sp));
223 sp->vha = qpair->vha;
224 INIT_LIST_HEAD(&sp->elem);
228 QLA_QPAIR_MARK_NOT_BUSY(qpair);
233 qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
235 mempool_free(sp, qpair->srb_mempool);
236 QLA_QPAIR_MARK_NOT_BUSY(qpair);
239 static inline srb_t *
240 qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
245 QLA_VHA_MARK_BUSY(vha, bail);
249 sp = mempool_alloc(vha->hw->srb_mempool, flag);
253 memset(sp, 0, sizeof(*sp));
255 sp->cmd_type = TYPE_SRB;
260 QLA_VHA_MARK_NOT_BUSY(vha);
265 qla2x00_rel_sp(srb_t *sp)
267 QLA_VHA_MARK_NOT_BUSY(sp->vha);
268 mempool_free(sp, sp->vha->hw->srb_mempool);
272 qla2x00_init_timer(srb_t *sp, unsigned long tmo)
274 init_timer(&sp->u.iocb_cmd.timer);
275 sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
276 sp->u.iocb_cmd.timer.data = (unsigned long)sp;
277 sp->u.iocb_cmd.timer.function = qla2x00_sp_timeout;
278 add_timer(&sp->u.iocb_cmd.timer);
279 sp->free = qla2x00_sp_free;
280 if (IS_QLAFX00(sp->vha->hw) && (sp->type == SRB_FXIOCB_DCMD))
281 init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
282 if (sp->type == SRB_ELS_DCMD)
283 init_completion(&sp->u.iocb_cmd.u.els_logo.comp);
287 qla2x00_gid_list_size(struct qla_hw_data *ha)
290 return sizeof(uint32_t) * 32;
292 return sizeof(struct gid_list_info) * ha->max_fibre_devices;
296 qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
298 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
299 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
300 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
301 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
302 complete(&ha->mbx_intr_comp);
307 qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
310 fcport->retry_delay_timestamp = jiffies +
311 (retry_delay * HZ / 10);
315 qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
317 if (qla_ini_mode_enabled(vha) &&
318 (ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
320 else if (qla_tgt_mode_enabled(vha) &&
321 (ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
323 else if (qla_dual_mode_enabled(vha) &&
324 ((ql2xiniexchg + ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
331 qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
333 qpair->cpuid = cpuid;
335 if (!list_empty(&qpair->hints_list)) {
336 struct qla_qpair_hint *h;
338 list_for_each_entry(h, &qpair->hints_list, hint_elem)
339 h->cpuid = qpair->cpuid;
343 static inline struct qla_qpair_hint *
344 qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
346 struct qla_qpair_hint *h;
349 for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
350 h = &tgt->qphints[i];
351 if (h->qpair == qpair)
359 qla_83xx_start_iocbs(struct qla_qpair *qpair)
361 struct req_que *req = qpair->req;
364 if (req->ring_index == req->length) {
366 req->ring_ptr = req->ring;
370 WRT_REG_DWORD(req->req_q_in, req->ring_index);