1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic iSCSI HBA Driver
4  * Copyright (c)  2003-2013 QLogic Corporation
5  */
6 
7 #include "ql4_def.h"
8 #include "ql4_glbl.h"
9 #include "ql4_dbg.h"
10 #include "ql4_inline.h"
11 
12 #include <scsi/scsi_tcq.h>
13 
14 static int
qla4xxx_space_in_req_ring(struct scsi_qla_host * ha,uint16_t req_cnt)15 qla4xxx_space_in_req_ring(struct scsi_qla_host *ha, uint16_t req_cnt)
16 {
17 	uint16_t cnt;
18 
19 	/* Calculate number of free request entries. */
20 	if ((req_cnt + 2) >= ha->req_q_count) {
21 		cnt = (uint16_t) ha->isp_ops->rd_shdw_req_q_out(ha);
22 		if (ha->request_in < cnt)
23 			ha->req_q_count = cnt - ha->request_in;
24 		else
25 			ha->req_q_count = REQUEST_QUEUE_DEPTH -
26 						(ha->request_in - cnt);
27 	}
28 
29 	/* Check if room for request in request ring. */
30 	if ((req_cnt + 2) < ha->req_q_count)
31 		return 1;
32 	else
33 		return 0;
34 }
35 
qla4xxx_advance_req_ring_ptr(struct scsi_qla_host * ha)36 static void qla4xxx_advance_req_ring_ptr(struct scsi_qla_host *ha)
37 {
38 	/* Advance request queue pointer */
39 	if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) {
40 		ha->request_in = 0;
41 		ha->request_ptr = ha->request_ring;
42 	} else {
43 		ha->request_in++;
44 		ha->request_ptr++;
45 	}
46 }
47 
48 /**
49  * qla4xxx_get_req_pkt - returns a valid entry in request queue.
50  * @ha: Pointer to host adapter structure.
51  * @queue_entry: Pointer to pointer to queue entry structure
52  *
53  * This routine performs the following tasks:
54  *	- returns the current request_in pointer (if queue not full)
55  *	- advances the request_in pointer
56  *	- checks for queue full
57  **/
qla4xxx_get_req_pkt(struct scsi_qla_host * ha,struct queue_entry ** queue_entry)58 static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha,
59 			       struct queue_entry **queue_entry)
60 {
61 	uint16_t req_cnt = 1;
62 
63 	if (qla4xxx_space_in_req_ring(ha, req_cnt)) {
64 		*queue_entry = ha->request_ptr;
65 		memset(*queue_entry, 0, sizeof(**queue_entry));
66 
67 		qla4xxx_advance_req_ring_ptr(ha);
68 		ha->req_q_count -= req_cnt;
69 		return QLA_SUCCESS;
70 	}
71 
72 	return QLA_ERROR;
73 }
74 
75 /**
76  * qla4xxx_send_marker_iocb - issues marker iocb to HBA
77  * @ha: Pointer to host adapter structure.
78  * @ddb_entry: Pointer to device database entry
79  * @lun: SCSI LUN
80  * @mrkr_mod: marker identifier
81  *
82  * This routine issues a marker IOCB.
83  **/
qla4xxx_send_marker_iocb(struct scsi_qla_host * ha,struct ddb_entry * ddb_entry,uint64_t lun,uint16_t mrkr_mod)84 int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha,
85 	struct ddb_entry *ddb_entry, uint64_t lun, uint16_t mrkr_mod)
86 {
87 	struct qla4_marker_entry *marker_entry;
88 	unsigned long flags = 0;
89 	uint8_t status = QLA_SUCCESS;
90 
91 	/* Acquire hardware specific lock */
92 	spin_lock_irqsave(&ha->hardware_lock, flags);
93 
94 	/* Get pointer to the queue entry for the marker */
95 	if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) !=
96 	    QLA_SUCCESS) {
97 		status = QLA_ERROR;
98 		goto exit_send_marker;
99 	}
100 
101 	/* Put the marker in the request queue */
102 	marker_entry->hdr.entryType = ET_MARKER;
103 	marker_entry->hdr.entryCount = 1;
104 	marker_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
105 	marker_entry->modifier = cpu_to_le16(mrkr_mod);
106 	int_to_scsilun(lun, &marker_entry->lun);
107 	wmb();
108 
109 	/* Tell ISP it's got a new I/O request */
110 	ha->isp_ops->queue_iocb(ha);
111 
112 exit_send_marker:
113 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
114 	return status;
115 }
116 
117 static struct continuation_t1_entry *
qla4xxx_alloc_cont_entry(struct scsi_qla_host * ha)118 qla4xxx_alloc_cont_entry(struct scsi_qla_host *ha)
119 {
120 	struct continuation_t1_entry *cont_entry;
121 
122 	cont_entry = (struct continuation_t1_entry *)ha->request_ptr;
123 
124 	qla4xxx_advance_req_ring_ptr(ha);
125 
126 	/* Load packet defaults */
127 	cont_entry->hdr.entryType = ET_CONTINUE;
128 	cont_entry->hdr.entryCount = 1;
129 	cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in);
130 
131 	return cont_entry;
132 }
133 
qla4xxx_calc_request_entries(uint16_t dsds)134 static uint16_t qla4xxx_calc_request_entries(uint16_t dsds)
135 {
136 	uint16_t iocbs;
137 
138 	iocbs = 1;
139 	if (dsds > COMMAND_SEG) {
140 		iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG;
141 		if ((dsds - COMMAND_SEG) % CONTINUE_SEG)
142 			iocbs++;
143 	}
144 	return iocbs;
145 }
146 
qla4xxx_build_scsi_iocbs(struct srb * srb,struct command_t3_entry * cmd_entry,uint16_t tot_dsds)147 static void qla4xxx_build_scsi_iocbs(struct srb *srb,
148 				     struct command_t3_entry *cmd_entry,
149 				     uint16_t tot_dsds)
150 {
151 	struct scsi_qla_host *ha;
152 	uint16_t avail_dsds;
153 	struct data_seg_a64 *cur_dsd;
154 	struct scsi_cmnd *cmd;
155 	struct scatterlist *sg;
156 	int i;
157 
158 	cmd = srb->cmd;
159 	ha = srb->ha;
160 
161 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
162 		/* No data being transferred */
163 		cmd_entry->ttlByteCnt = cpu_to_le32(0);
164 		return;
165 	}
166 
167 	avail_dsds = COMMAND_SEG;
168 	cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
169 
170 	scsi_for_each_sg(cmd, sg, tot_dsds, i) {
171 		dma_addr_t sle_dma;
172 
173 		/* Allocate additional continuation packets? */
174 		if (avail_dsds == 0) {
175 			struct continuation_t1_entry *cont_entry;
176 
177 			cont_entry = qla4xxx_alloc_cont_entry(ha);
178 			cur_dsd =
179 				(struct data_seg_a64 *)
180 				&cont_entry->dataseg[0];
181 			avail_dsds = CONTINUE_SEG;
182 		}
183 
184 		sle_dma = sg_dma_address(sg);
185 		cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma));
186 		cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma));
187 		cur_dsd->count = cpu_to_le32(sg_dma_len(sg));
188 		avail_dsds--;
189 
190 		cur_dsd++;
191 	}
192 }
193 
qla4_83xx_queue_iocb(struct scsi_qla_host * ha)194 void qla4_83xx_queue_iocb(struct scsi_qla_host *ha)
195 {
196 	writel(ha->request_in, &ha->qla4_83xx_reg->req_q_in);
197 	readl(&ha->qla4_83xx_reg->req_q_in);
198 }
199 
qla4_83xx_complete_iocb(struct scsi_qla_host * ha)200 void qla4_83xx_complete_iocb(struct scsi_qla_host *ha)
201 {
202 	writel(ha->response_out, &ha->qla4_83xx_reg->rsp_q_out);
203 	readl(&ha->qla4_83xx_reg->rsp_q_out);
204 }
205 
206 /**
207  * qla4_82xx_queue_iocb - Tell ISP it's got new request(s)
208  * @ha: pointer to host adapter structure.
209  *
210  * This routine notifies the ISP that one or more new request
211  * queue entries have been placed on the request queue.
212  **/
qla4_82xx_queue_iocb(struct scsi_qla_host * ha)213 void qla4_82xx_queue_iocb(struct scsi_qla_host *ha)
214 {
215 	uint32_t dbval = 0;
216 
217 	dbval = 0x14 | (ha->func_num << 5);
218 	dbval = dbval | (0 << 8) | (ha->request_in << 16);
219 
220 	qla4_82xx_wr_32(ha, ha->nx_db_wr_ptr, ha->request_in);
221 }
222 
223 /**
224  * qla4_82xx_complete_iocb - Tell ISP we're done with response(s)
225  * @ha: pointer to host adapter structure.
226  *
227  * This routine notifies the ISP that one or more response/completion
228  * queue entries have been processed by the driver.
229  * This also clears the interrupt.
230  **/
qla4_82xx_complete_iocb(struct scsi_qla_host * ha)231 void qla4_82xx_complete_iocb(struct scsi_qla_host *ha)
232 {
233 	writel(ha->response_out, &ha->qla4_82xx_reg->rsp_q_out);
234 	readl(&ha->qla4_82xx_reg->rsp_q_out);
235 }
236 
237 /**
238  * qla4xxx_queue_iocb - Tell ISP it's got new request(s)
239  * @ha: pointer to host adapter structure.
240  *
241  * This routine is notifies the ISP that one or more new request
242  * queue entries have been placed on the request queue.
243  **/
qla4xxx_queue_iocb(struct scsi_qla_host * ha)244 void qla4xxx_queue_iocb(struct scsi_qla_host *ha)
245 {
246 	writel(ha->request_in, &ha->reg->req_q_in);
247 	readl(&ha->reg->req_q_in);
248 }
249 
250 /**
251  * qla4xxx_complete_iocb - Tell ISP we're done with response(s)
252  * @ha: pointer to host adapter structure.
253  *
254  * This routine is notifies the ISP that one or more response/completion
255  * queue entries have been processed by the driver.
256  * This also clears the interrupt.
257  **/
qla4xxx_complete_iocb(struct scsi_qla_host * ha)258 void qla4xxx_complete_iocb(struct scsi_qla_host *ha)
259 {
260 	writel(ha->response_out, &ha->reg->rsp_q_out);
261 	readl(&ha->reg->rsp_q_out);
262 }
263 
264 /**
265  * qla4xxx_send_command_to_isp - issues command to HBA
266  * @ha: pointer to host adapter structure.
267  * @srb: pointer to SCSI Request Block to be sent to ISP
268  *
269  * This routine is called by qla4xxx_queuecommand to build an ISP
270  * command and pass it to the ISP for execution.
271  **/
qla4xxx_send_command_to_isp(struct scsi_qla_host * ha,struct srb * srb)272 int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
273 {
274 	struct scsi_cmnd *cmd = srb->cmd;
275 	struct ddb_entry *ddb_entry;
276 	struct command_t3_entry *cmd_entry;
277 	int nseg;
278 	uint16_t tot_dsds;
279 	uint16_t req_cnt;
280 	unsigned long flags;
281 	uint32_t index;
282 
283 	/* Get real lun and adapter */
284 	ddb_entry = srb->ddb;
285 
286 	tot_dsds = 0;
287 
288 	/* Acquire hardware specific lock */
289 	spin_lock_irqsave(&ha->hardware_lock, flags);
290 
291 	index = scsi_cmd_to_rq(cmd)->tag;
292 
293 	/*
294 	 * Check to see if adapter is online before placing request on
295 	 * request queue.  If a reset occurs and a request is in the queue,
296 	 * the firmware will still attempt to process the request, retrieving
297 	 * garbage for pointers.
298 	 */
299 	if (!test_bit(AF_ONLINE, &ha->flags)) {
300 		DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! "
301 			      "Do not issue command.\n",
302 			      ha->host_no, __func__));
303 		goto queuing_error;
304 	}
305 
306 	/* Calculate the number of request entries needed. */
307 	nseg = scsi_dma_map(cmd);
308 	if (nseg < 0)
309 		goto queuing_error;
310 	tot_dsds = nseg;
311 
312 	req_cnt = qla4xxx_calc_request_entries(tot_dsds);
313 	if (!qla4xxx_space_in_req_ring(ha, req_cnt))
314 		goto queuing_error;
315 
316 	/* total iocbs active */
317 	if ((ha->iocb_cnt + req_cnt) >= ha->iocb_hiwat)
318 		goto queuing_error;
319 
320 	/* Build command packet */
321 	cmd_entry = (struct command_t3_entry *) ha->request_ptr;
322 	memset(cmd_entry, 0, sizeof(struct command_t3_entry));
323 	cmd_entry->hdr.entryType = ET_COMMAND;
324 	cmd_entry->handle = cpu_to_le32(index);
325 	cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
326 
327 	int_to_scsilun(cmd->device->lun, &cmd_entry->lun);
328 	cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd));
329 	memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len);
330 	cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds);
331 	cmd_entry->hdr.entryCount = req_cnt;
332 
333 	/* Set data transfer direction control flags
334 	 * NOTE: Look at data_direction bits iff there is data to be
335 	 *	 transferred, as the data direction bit is sometimed filled
336 	 *	 in when there is no data to be transferred */
337 	cmd_entry->control_flags = CF_NO_DATA;
338 	if (scsi_bufflen(cmd)) {
339 		if (cmd->sc_data_direction == DMA_TO_DEVICE)
340 			cmd_entry->control_flags = CF_WRITE;
341 		else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
342 			cmd_entry->control_flags = CF_READ;
343 
344 		ha->bytes_xfered += scsi_bufflen(cmd);
345 		if (ha->bytes_xfered & ~0xFFFFF){
346 			ha->total_mbytes_xferred += ha->bytes_xfered >> 20;
347 			ha->bytes_xfered &= 0xFFFFF;
348 		}
349 	}
350 
351 	/* Set tagged queueing control flags */
352 	cmd_entry->control_flags |= CF_SIMPLE_TAG;
353 
354 	qla4xxx_advance_req_ring_ptr(ha);
355 	qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
356 	wmb();
357 
358 	srb->cmd->host_scribble = (unsigned char *)(unsigned long)index;
359 
360 	/* update counters */
361 	srb->state = SRB_ACTIVE_STATE;
362 	srb->flags |= SRB_DMA_VALID;
363 
364 	/* Track IOCB used */
365 	ha->iocb_cnt += req_cnt;
366 	srb->iocb_cnt = req_cnt;
367 	ha->req_q_count -= req_cnt;
368 
369 	ha->isp_ops->queue_iocb(ha);
370 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
371 
372 	return QLA_SUCCESS;
373 
374 queuing_error:
375 	if (tot_dsds)
376 		scsi_dma_unmap(cmd);
377 
378 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
379 
380 	return QLA_ERROR;
381 }
382 
qla4xxx_send_passthru0(struct iscsi_task * task)383 int qla4xxx_send_passthru0(struct iscsi_task *task)
384 {
385 	struct passthru0 *passthru_iocb;
386 	struct iscsi_session *sess = task->conn->session;
387 	struct ddb_entry *ddb_entry = sess->dd_data;
388 	struct scsi_qla_host *ha = ddb_entry->ha;
389 	struct ql4_task_data *task_data = task->dd_data;
390 	uint16_t ctrl_flags = 0;
391 	unsigned long flags;
392 	int ret = QLA_ERROR;
393 
394 	spin_lock_irqsave(&ha->hardware_lock, flags);
395 	task_data->iocb_req_cnt = 1;
396 	/* Put the IOCB on the request queue */
397 	if (!qla4xxx_space_in_req_ring(ha, task_data->iocb_req_cnt))
398 		goto queuing_error;
399 
400 	passthru_iocb = (struct passthru0 *) ha->request_ptr;
401 
402 	memset(passthru_iocb, 0, sizeof(struct passthru0));
403 	passthru_iocb->hdr.entryType = ET_PASSTHRU0;
404 	passthru_iocb->hdr.systemDefined = SD_ISCSI_PDU;
405 	passthru_iocb->hdr.entryCount = task_data->iocb_req_cnt;
406 	passthru_iocb->handle = task->itt;
407 	passthru_iocb->target = cpu_to_le16(ddb_entry->fw_ddb_index);
408 	passthru_iocb->timeout = cpu_to_le16(PT_DEFAULT_TIMEOUT);
409 
410 	/* Setup the out & in DSDs */
411 	if (task_data->req_len) {
412 		memcpy((uint8_t *)task_data->req_buffer +
413 		       sizeof(struct iscsi_hdr), task->data, task->data_count);
414 		ctrl_flags |= PT_FLAG_SEND_BUFFER;
415 		passthru_iocb->out_dsd.base.addrLow =
416 					cpu_to_le32(LSDW(task_data->req_dma));
417 		passthru_iocb->out_dsd.base.addrHigh =
418 					cpu_to_le32(MSDW(task_data->req_dma));
419 		passthru_iocb->out_dsd.count =
420 					cpu_to_le32(task->data_count +
421 						    sizeof(struct iscsi_hdr));
422 	}
423 	if (task_data->resp_len) {
424 		passthru_iocb->in_dsd.base.addrLow =
425 					cpu_to_le32(LSDW(task_data->resp_dma));
426 		passthru_iocb->in_dsd.base.addrHigh =
427 					cpu_to_le32(MSDW(task_data->resp_dma));
428 		passthru_iocb->in_dsd.count =
429 			cpu_to_le32(task_data->resp_len);
430 	}
431 
432 	ctrl_flags |= (PT_FLAG_ISCSI_PDU | PT_FLAG_WAIT_4_RESPONSE);
433 	passthru_iocb->control_flags = cpu_to_le16(ctrl_flags);
434 
435 	/* Update the request pointer */
436 	qla4xxx_advance_req_ring_ptr(ha);
437 	wmb();
438 
439 	/* Track IOCB used */
440 	ha->iocb_cnt += task_data->iocb_req_cnt;
441 	ha->req_q_count -= task_data->iocb_req_cnt;
442 	ha->isp_ops->queue_iocb(ha);
443 	ret = QLA_SUCCESS;
444 
445 queuing_error:
446 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
447 	return ret;
448 }
449 
qla4xxx_get_new_mrb(struct scsi_qla_host * ha)450 static struct mrb *qla4xxx_get_new_mrb(struct scsi_qla_host *ha)
451 {
452 	struct mrb *mrb;
453 
454 	mrb = kzalloc(sizeof(*mrb), GFP_KERNEL);
455 	if (!mrb)
456 		return mrb;
457 
458 	mrb->ha = ha;
459 	return mrb;
460 }
461 
qla4xxx_send_mbox_iocb(struct scsi_qla_host * ha,struct mrb * mrb,uint32_t * in_mbox)462 static int qla4xxx_send_mbox_iocb(struct scsi_qla_host *ha, struct mrb *mrb,
463 				  uint32_t *in_mbox)
464 {
465 	int rval = QLA_SUCCESS;
466 	uint32_t i;
467 	unsigned long flags;
468 	uint32_t index = 0;
469 
470 	/* Acquire hardware specific lock */
471 	spin_lock_irqsave(&ha->hardware_lock, flags);
472 
473 	/* Get pointer to the queue entry for the marker */
474 	rval = qla4xxx_get_req_pkt(ha, (struct queue_entry **) &(mrb->mbox));
475 	if (rval != QLA_SUCCESS)
476 		goto exit_mbox_iocb;
477 
478 	index = ha->mrb_index;
479 	/* get valid mrb index*/
480 	for (i = 0; i < MAX_MRB; i++) {
481 		index++;
482 		if (index == MAX_MRB)
483 			index = 1;
484 		if (ha->active_mrb_array[index] == NULL) {
485 			ha->mrb_index = index;
486 			break;
487 		}
488 	}
489 
490 	mrb->iocb_cnt = 1;
491 	ha->active_mrb_array[index] = mrb;
492 	mrb->mbox->handle = index;
493 	mrb->mbox->hdr.entryType = ET_MBOX_CMD;
494 	mrb->mbox->hdr.entryCount = mrb->iocb_cnt;
495 	memcpy(mrb->mbox->in_mbox, in_mbox, 32);
496 	mrb->mbox_cmd = in_mbox[0];
497 	wmb();
498 
499 	ha->iocb_cnt += mrb->iocb_cnt;
500 	ha->isp_ops->queue_iocb(ha);
501 exit_mbox_iocb:
502 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
503 	return rval;
504 }
505 
qla4xxx_ping_iocb(struct scsi_qla_host * ha,uint32_t options,uint32_t payload_size,uint32_t pid,uint8_t * ipaddr)506 int qla4xxx_ping_iocb(struct scsi_qla_host *ha, uint32_t options,
507 		      uint32_t payload_size, uint32_t pid, uint8_t *ipaddr)
508 {
509 	uint32_t in_mbox[8];
510 	struct mrb *mrb = NULL;
511 	int rval = QLA_SUCCESS;
512 
513 	memset(in_mbox, 0, sizeof(in_mbox));
514 
515 	mrb = qla4xxx_get_new_mrb(ha);
516 	if (!mrb) {
517 		DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: fail to get new mrb\n",
518 				  __func__));
519 		rval = QLA_ERROR;
520 		goto exit_ping;
521 	}
522 
523 	in_mbox[0] = MBOX_CMD_PING;
524 	in_mbox[1] = options;
525 	memcpy(&in_mbox[2], &ipaddr[0], 4);
526 	memcpy(&in_mbox[3], &ipaddr[4], 4);
527 	memcpy(&in_mbox[4], &ipaddr[8], 4);
528 	memcpy(&in_mbox[5], &ipaddr[12], 4);
529 	in_mbox[6] = payload_size;
530 
531 	mrb->pid = pid;
532 	rval = qla4xxx_send_mbox_iocb(ha, mrb, in_mbox);
533 
534 	if (rval != QLA_SUCCESS)
535 		goto exit_ping;
536 
537 	return rval;
538 exit_ping:
539 	kfree(mrb);
540 	return rval;
541 }
542