blob: 4351736b2426da9848b9fd3a81d7fb9ed745c39f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Quinn Tranf83adb62014-04-11 16:54:43 -04008#include "qla_target.h"
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04009/**
10 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
11 * Continuation Type 1 IOCBs to allocate.
12 *
Bart Van Assche2db62282018-01-23 16:33:51 -080013 * @vha: HA context
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040014 * @dsds: number of data segment decriptors needed
15 *
16 * Returns the number of IOCB entries needed to store @dsds.
17 */
18static inline uint16_t
19qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
20{
21 uint16_t iocbs;
22
23 iocbs = 1;
24 if (dsds > 1) {
25 iocbs += (dsds - 1) / 5;
26 if ((dsds - 1) % 5)
27 iocbs++;
28 }
29 return iocbs;
30}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * qla2x00_debounce_register
34 * Debounce register.
35 *
36 * Input:
37 * port = register address.
38 *
39 * Returns:
40 * register value.
41 */
42static __inline__ uint16_t
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070043qla2x00_debounce_register(volatile uint16_t __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 volatile uint16_t first;
46 volatile uint16_t second;
47
48 do {
49 first = RD_REG_WORD(addr);
50 barrier();
51 cpu_relax();
52 second = RD_REG_WORD(addr);
53 } while (first != second);
54
55 return (first);
56}
57
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070058static inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080059qla2x00_poll(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080061 struct qla_hw_data *ha = rsp->hw;
Sebastian Andrzej Siewiorb3a8aa902018-06-11 16:40:53 +020062
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -040063 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -070064 qla82xx_poll(0, rsp);
65 else
66 ha->isp_ops->intr_handler(0, rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -070069static inline uint8_t *
70host_to_fcp_swap(uint8_t *fcp, uint32_t bsize)
71{
72 uint32_t *ifcp = (uint32_t *) fcp;
73 uint32_t *ofcp = (uint32_t *) fcp;
74 uint32_t iter = bsize >> 2;
75
76 for (; iter ; iter--)
77 *ofcp++ = swab32(*ifcp++);
78
79 return fcp;
80}
Andrew Vasquez3d716442005-07-06 10:30:26 -070081
Chad Dupuis5f16b332012-08-22 14:21:00 -040082static inline void
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040083host_to_adap(uint8_t *src, uint8_t *dst, uint32_t bsize)
84{
85 uint32_t *isrc = (uint32_t *) src;
Saurav Kashyap1f8deef2013-06-25 11:27:21 -040086 __le32 *odest = (__le32 *) dst;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040087 uint32_t iter = bsize >> 2;
88
Joe Carnuccioda08ef52016-01-27 12:03:34 -050089 for ( ; iter--; isrc++)
90 *odest++ = cpu_to_le32(*isrc);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040091}
92
93static inline void
Chad Dupuis5f16b332012-08-22 14:21:00 -040094qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
95{
96 int i;
97
98 if (IS_FWI2_CAPABLE(ha))
99 return;
100
101 for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
102 set_bit(i, ha->loop_id_map);
103 set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
104 set_bit(BROADCAST, ha->loop_id_map);
105}
106
Andrew Vasquez3d716442005-07-06 10:30:26 -0700107static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800108qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
Andrew Vasquez3d716442005-07-06 10:30:26 -0700109{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800110 struct qla_hw_data *ha = vha->hw;
Andrew Vasqueze4289242007-07-19 15:05:56 -0700111 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez3d716442005-07-06 10:30:26 -0700112 return (loop_id > NPH_LAST_HANDLE);
113
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800114 return ((loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
Andrew Vasquez3d716442005-07-06 10:30:26 -0700115 loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST);
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800116}
Arun Easibad75002010-05-04 15:01:30 -0700117
118static inline void
Chad Dupuis5f16b332012-08-22 14:21:00 -0400119qla2x00_clear_loop_id(fc_port_t *fcport) {
120 struct qla_hw_data *ha = fcport->vha->hw;
121
122 if (fcport->loop_id == FC_NO_LOOP_ID ||
123 qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
124 return;
125
126 clear_bit(fcport->loop_id, ha->loop_id_map);
127 fcport->loop_id = FC_NO_LOOP_ID;
128}
129
130static inline void
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -0700131qla2x00_clean_dsd_pool(struct qla_hw_data *ha, struct crc_context *ctx)
Arun Easibad75002010-05-04 15:01:30 -0700132{
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -0700133 struct dsd_dma *dsd, *tdsd;
Arun Easibad75002010-05-04 15:01:30 -0700134
135 /* clean up allocated prev pool */
Joe Carnucciod5ff0ee2017-05-24 18:06:24 -0700136 list_for_each_entry_safe(dsd, tdsd, &ctx->dsd_list, list) {
137 dma_pool_free(ha->dl_dma_pool, dsd->dsd_addr,
138 dsd->dsd_list_dma);
139 list_del(&dsd->list);
140 kfree(dsd);
Arun Easibad75002010-05-04 15:01:30 -0700141 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800142 INIT_LIST_HEAD(&ctx->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -0700143}
Chad Dupuisec426e12011-03-30 11:46:32 -0700144
145static inline void
146qla2x00_set_fcport_state(fc_port_t *fcport, int state)
147{
148 int old_state;
149
150 old_state = atomic_read(&fcport->state);
151 atomic_set(&fcport->state, state);
152
153 /* Don't print state transitions during initial allocation of fcport */
154 if (old_state && old_state != state) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700155 ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
Quinn Tran726b8542017-01-19 22:28:00 -0800156 "FCPort %8phC state transitioned from %s to %s - "
157 "portid=%02x%02x%02x.\n", fcport->port_name,
Chad Dupuisec426e12011-03-30 11:46:32 -0700158 port_state_str[old_state], port_state_str[state],
159 fcport->d_id.b.domain, fcport->d_id.b.area,
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700160 fcport->d_id.b.al_pa);
Chad Dupuisec426e12011-03-30 11:46:32 -0700161 }
162}
Arun Easi8cb20492011-08-16 11:29:22 -0700163
164static inline int
Arun Easie02587d2011-08-16 11:29:23 -0700165qla2x00_hba_err_chk_enabled(srb_t *sp)
Arun Easi8cb20492011-08-16 11:29:22 -0700166{
Arun Easie02587d2011-08-16 11:29:23 -0700167 /*
168 * Uncomment when corresponding SCSI changes are done.
169 *
170 if (!sp->cmd->prot_chk)
171 return 0;
172 *
173 */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800174 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
Arun Easi8cb20492011-08-16 11:29:22 -0700175 case SCSI_PROT_READ_STRIP:
176 case SCSI_PROT_WRITE_INSERT:
177 if (ql2xenablehba_err_chk >= 1)
178 return 1;
179 break;
180 case SCSI_PROT_READ_PASS:
181 case SCSI_PROT_WRITE_PASS:
182 if (ql2xenablehba_err_chk >= 2)
183 return 1;
184 break;
185 case SCSI_PROT_READ_INSERT:
186 case SCSI_PROT_WRITE_STRIP:
187 return 1;
188 }
189 return 0;
190}
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -0800191
192static inline int
193qla2x00_reset_active(scsi_qla_host_t *vha)
194{
195 scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
196
197 /* Test appropriate base-vha and vha flags. */
198 return test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) ||
199 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
200 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
201 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
202 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
203}
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800204
Quinn Tran22ebde12018-08-02 13:16:47 -0700205static inline int
206qla2x00_chip_is_down(scsi_qla_host_t *vha)
207{
208 return (qla2x00_reset_active(vha) || !vha->hw->flags.fw_started);
209}
210
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800211static inline srb_t *
Michael Hernandezd7459522016-12-12 14:40:07 -0800212qla2xxx_get_qpair_sp(struct qla_qpair *qpair, fc_port_t *fcport, gfp_t flag)
213{
214 srb_t *sp = NULL;
215 uint8_t bail;
216
217 QLA_QPAIR_MARK_BUSY(qpair, bail);
218 if (unlikely(bail))
219 return NULL;
220
221 sp = mempool_alloc(qpair->srb_mempool, flag);
222 if (!sp)
223 goto done;
224
225 memset(sp, 0, sizeof(*sp));
226 sp->fcport = fcport;
227 sp->iocbs = 1;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800228 sp->vha = qpair->vha;
Quinn Trane3dde082018-07-18 14:29:51 -0700229 INIT_LIST_HEAD(&sp->elem);
230
Michael Hernandezd7459522016-12-12 14:40:07 -0800231done:
232 if (!sp)
233 QLA_QPAIR_MARK_NOT_BUSY(qpair);
234 return sp;
235}
236
237static inline void
238qla2xxx_rel_qpair_sp(struct qla_qpair *qpair, srb_t *sp)
239{
240 mempool_free(sp, qpair->srb_mempool);
241 QLA_QPAIR_MARK_NOT_BUSY(qpair);
242}
243
244static inline srb_t *
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800245qla2x00_get_sp(scsi_qla_host_t *vha, fc_port_t *fcport, gfp_t flag)
246{
247 srb_t *sp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800248 uint8_t bail;
249
250 QLA_VHA_MARK_BUSY(vha, bail);
251 if (unlikely(bail))
252 return NULL;
253
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800254 sp = mempool_alloc(vha->hw->srb_mempool, flag);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800255 if (!sp)
256 goto done;
257
258 memset(sp, 0, sizeof(*sp));
259 sp->fcport = fcport;
Quinn Tranc5419e22017-06-13 20:47:16 -0700260 sp->cmd_type = TYPE_SRB;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800261 sp->iocbs = 1;
Quinn Tran726b8542017-01-19 22:28:00 -0800262 sp->vha = vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800263done:
264 if (!sp)
265 QLA_VHA_MARK_NOT_BUSY(vha);
266 return sp;
267}
268
269static inline void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800270qla2x00_rel_sp(srb_t *sp)
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500271{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800272 QLA_VHA_MARK_NOT_BUSY(sp->vha);
273 mempool_free(sp, sp->vha->hw->srb_mempool);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500274}
275
276static inline void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800277qla2x00_init_timer(srb_t *sp, unsigned long tmo)
278{
Kees Cook8e5f4ba2017-09-03 13:23:32 -0700279 timer_setup(&sp->u.iocb_cmd.timer, qla2x00_sp_timeout, 0);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800280 sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800281 sp->free = qla2x00_sp_free;
Quinn Tran28531922017-12-28 12:33:10 -0800282 init_completion(&sp->comp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800283 if (IS_QLAFX00(sp->vha->hw) && (sp->type == SRB_FXIOCB_DCMD))
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400284 init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000285 add_timer(&sp->u.iocb_cmd.timer);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800286}
Chad Dupuis642ef982012-02-09 11:15:57 -0800287
288static inline int
289qla2x00_gid_list_size(struct qla_hw_data *ha)
290{
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400291 if (IS_QLAFX00(ha))
292 return sizeof(uint32_t) * 32;
293 else
294 return sizeof(struct gid_list_info) * ha->max_fibre_devices;
Chad Dupuis642ef982012-02-09 11:15:57 -0800295}
Chad Dupuis3c290d02013-01-30 03:34:38 -0500296
297static inline void
gurinder.shergill@hp.com36439832013-04-23 10:13:17 -0700298qla2x00_handle_mbx_completion(struct qla_hw_data *ha, int status)
299{
300 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
301 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
302 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
303 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
304 complete(&ha->mbx_intr_comp);
305 }
306}
Chad Dupuise05fe292014-09-25 05:16:59 -0400307
308static inline void
309qla2x00_set_retry_delay_timestamp(fc_port_t *fcport, uint16_t retry_delay)
310{
311 if (retry_delay)
312 fcport->retry_delay_timestamp = jiffies +
313 (retry_delay * HZ / 10);
314}
Quinn Tran99e1b682017-06-02 09:12:03 -0700315
316static inline bool
317qla_is_exch_offld_enabled(struct scsi_qla_host *vha)
318{
319 if (qla_ini_mode_enabled(vha) &&
320 (ql2xiniexchg > FW_DEF_EXCHANGES_CNT))
321 return true;
322 else if (qla_tgt_mode_enabled(vha) &&
323 (ql2xexchoffld > FW_DEF_EXCHANGES_CNT))
324 return true;
325 else if (qla_dual_mode_enabled(vha) &&
326 ((ql2xiniexchg + ql2xexchoffld) > FW_DEF_EXCHANGES_CNT))
327 return true;
328 else
329 return false;
330}
Quinn Trane326d222017-06-13 20:47:18 -0700331
332static inline void
333qla_cpu_update(struct qla_qpair *qpair, uint16_t cpuid)
334{
335 qpair->cpuid = cpuid;
336
337 if (!list_empty(&qpair->hints_list)) {
338 struct qla_qpair_hint *h;
339
340 list_for_each_entry(h, &qpair->hints_list, hint_elem)
341 h->cpuid = qpair->cpuid;
342 }
343}
344
345static inline struct qla_qpair_hint *
346qla_qpair_to_hint(struct qla_tgt *tgt, struct qla_qpair *qpair)
347{
348 struct qla_qpair_hint *h;
349 u16 i;
350
351 for (i = 0; i < tgt->ha->max_qpairs + 1; i++) {
352 h = &tgt->qphints[i];
353 if (h->qpair == qpair)
354 return h;
355 }
356
357 return NULL;
358}
Quinn Tran8abfa9e2017-06-13 20:47:24 -0700359
360static inline void
361qla_83xx_start_iocbs(struct qla_qpair *qpair)
362{
363 struct req_que *req = qpair->req;
364
365 req->ring_index++;
366 if (req->ring_index == req->length) {
367 req->ring_index = 0;
368 req->ring_ptr = req->ring;
369 } else
370 req->ring_ptr++;
371
372 WRT_REG_DWORD(req->req_q_in, req->ring_index);
373}