1 /*
2  * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/scatterlist.h>
37 #include <linux/kfifo.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_host.h>
40 
41 #include "iscsi_iser.h"
42 
43 /* Register user buffer memory and initialize passive rdma
44  *  dto descriptor. Data size is stored in
45  *  task->data[ISER_DIR_IN].data_len, Protection size
46  *  os stored in task->prot[ISER_DIR_IN].data_len
47  */
48 static int iser_prepare_read_cmd(struct iscsi_task *task)
49 
50 {
51 	struct iscsi_iser_task *iser_task = task->dd_data;
52 	struct iser_mem_reg *mem_reg;
53 	int err;
54 	struct iser_ctrl *hdr = &iser_task->desc.iser_header;
55 	struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN];
56 
57 	err = iser_dma_map_task_data(iser_task,
58 				     buf_in,
59 				     ISER_DIR_IN,
60 				     DMA_FROM_DEVICE);
61 	if (err)
62 		return err;
63 
64 	if (scsi_prot_sg_count(iser_task->sc)) {
65 		struct iser_data_buf *pbuf_in = &iser_task->prot[ISER_DIR_IN];
66 
67 		err = iser_dma_map_task_data(iser_task,
68 					     pbuf_in,
69 					     ISER_DIR_IN,
70 					     DMA_FROM_DEVICE);
71 		if (err)
72 			return err;
73 	}
74 
75 	err = iser_reg_rdma_mem(iser_task, ISER_DIR_IN, false);
76 	if (err) {
77 		iser_err("Failed to set up Data-IN RDMA\n");
78 		return err;
79 	}
80 	mem_reg = &iser_task->rdma_reg[ISER_DIR_IN];
81 
82 	hdr->flags    |= ISER_RSV;
83 	hdr->read_stag = cpu_to_be32(mem_reg->rkey);
84 	hdr->read_va   = cpu_to_be64(mem_reg->sge.addr);
85 
86 	iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
87 		 task->itt, mem_reg->rkey,
88 		 (unsigned long long)mem_reg->sge.addr);
89 
90 	return 0;
91 }
92 
93 /* Register user buffer memory and initialize passive rdma
94  *  dto descriptor. Data size is stored in
95  *  task->data[ISER_DIR_OUT].data_len, Protection size
96  *  is stored at task->prot[ISER_DIR_OUT].data_len
97  */
98 static int
99 iser_prepare_write_cmd(struct iscsi_task *task,
100 		       unsigned int imm_sz,
101 		       unsigned int unsol_sz,
102 		       unsigned int edtl)
103 {
104 	struct iscsi_iser_task *iser_task = task->dd_data;
105 	struct iser_mem_reg *mem_reg;
106 	int err;
107 	struct iser_ctrl *hdr = &iser_task->desc.iser_header;
108 	struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT];
109 	struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1];
110 
111 	err = iser_dma_map_task_data(iser_task,
112 				     buf_out,
113 				     ISER_DIR_OUT,
114 				     DMA_TO_DEVICE);
115 	if (err)
116 		return err;
117 
118 	if (scsi_prot_sg_count(iser_task->sc)) {
119 		struct iser_data_buf *pbuf_out = &iser_task->prot[ISER_DIR_OUT];
120 
121 		err = iser_dma_map_task_data(iser_task,
122 					     pbuf_out,
123 					     ISER_DIR_OUT,
124 					     DMA_TO_DEVICE);
125 		if (err)
126 			return err;
127 	}
128 
129 	err = iser_reg_rdma_mem(iser_task, ISER_DIR_OUT,
130 				buf_out->data_len == imm_sz);
131 	if (err != 0) {
132 		iser_err("Failed to register write cmd RDMA mem\n");
133 		return err;
134 	}
135 
136 	mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT];
137 
138 	if (unsol_sz < edtl) {
139 		hdr->flags     |= ISER_WSV;
140 		if (buf_out->data_len > imm_sz) {
141 			hdr->write_stag = cpu_to_be32(mem_reg->rkey);
142 			hdr->write_va = cpu_to_be64(mem_reg->sge.addr + unsol_sz);
143 		}
144 
145 		iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X VA:%#llX + unsol:%d\n",
146 			 task->itt, mem_reg->rkey,
147 			 (unsigned long long)mem_reg->sge.addr, unsol_sz);
148 	}
149 
150 	if (imm_sz > 0) {
151 		iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
152 			 task->itt, imm_sz);
153 		tx_dsg->addr = mem_reg->sge.addr;
154 		tx_dsg->length = imm_sz;
155 		tx_dsg->lkey = mem_reg->sge.lkey;
156 		iser_task->desc.num_sge = 2;
157 	}
158 
159 	return 0;
160 }
161 
162 /* creates a new tx descriptor and adds header regd buffer */
163 static void iser_create_send_desc(struct iser_conn	*iser_conn,
164 				  struct iser_tx_desc	*tx_desc)
165 {
166 	struct iser_device *device = iser_conn->ib_conn.device;
167 
168 	ib_dma_sync_single_for_cpu(device->ib_device,
169 		tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
170 
171 	memset(&tx_desc->iser_header, 0, sizeof(struct iser_ctrl));
172 	tx_desc->iser_header.flags = ISER_VER;
173 	tx_desc->num_sge = 1;
174 }
175 
176 static void iser_free_login_buf(struct iser_conn *iser_conn)
177 {
178 	struct iser_device *device = iser_conn->ib_conn.device;
179 	struct iser_login_desc *desc = &iser_conn->login_desc;
180 
181 	if (!desc->req)
182 		return;
183 
184 	ib_dma_unmap_single(device->ib_device, desc->req_dma,
185 			    ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE);
186 
187 	ib_dma_unmap_single(device->ib_device, desc->rsp_dma,
188 			    ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
189 
190 	kfree(desc->req);
191 	kfree(desc->rsp);
192 
193 	/* make sure we never redo any unmapping */
194 	desc->req = NULL;
195 	desc->rsp = NULL;
196 }
197 
198 static int iser_alloc_login_buf(struct iser_conn *iser_conn)
199 {
200 	struct iser_device *device = iser_conn->ib_conn.device;
201 	struct iser_login_desc *desc = &iser_conn->login_desc;
202 
203 	desc->req = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
204 	if (!desc->req)
205 		return -ENOMEM;
206 
207 	desc->req_dma = ib_dma_map_single(device->ib_device, desc->req,
208 					  ISCSI_DEF_MAX_RECV_SEG_LEN,
209 					  DMA_TO_DEVICE);
210 	if (ib_dma_mapping_error(device->ib_device,
211 				desc->req_dma))
212 		goto free_req;
213 
214 	desc->rsp = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
215 	if (!desc->rsp)
216 		goto unmap_req;
217 
218 	desc->rsp_dma = ib_dma_map_single(device->ib_device, desc->rsp,
219 					   ISER_RX_LOGIN_SIZE,
220 					   DMA_FROM_DEVICE);
221 	if (ib_dma_mapping_error(device->ib_device,
222 				desc->rsp_dma))
223 		goto free_rsp;
224 
225 	return 0;
226 
227 free_rsp:
228 	kfree(desc->rsp);
229 unmap_req:
230 	ib_dma_unmap_single(device->ib_device, desc->req_dma,
231 			    ISCSI_DEF_MAX_RECV_SEG_LEN,
232 			    DMA_TO_DEVICE);
233 free_req:
234 	kfree(desc->req);
235 
236 	return -ENOMEM;
237 }
238 
239 int iser_alloc_rx_descriptors(struct iser_conn *iser_conn,
240 			      struct iscsi_session *session)
241 {
242 	int i, j;
243 	u64 dma_addr;
244 	struct iser_rx_desc *rx_desc;
245 	struct ib_sge       *rx_sg;
246 	struct ib_conn *ib_conn = &iser_conn->ib_conn;
247 	struct iser_device *device = ib_conn->device;
248 
249 	iser_conn->qp_max_recv_dtos = session->cmds_max;
250 	iser_conn->qp_max_recv_dtos_mask = session->cmds_max - 1; /* cmds_max is 2^N */
251 	iser_conn->min_posted_rx = iser_conn->qp_max_recv_dtos >> 2;
252 
253 	if (device->reg_ops->alloc_reg_res(ib_conn, session->scsi_cmds_max,
254 					   iser_conn->pages_per_mr))
255 		goto create_rdma_reg_res_failed;
256 
257 	if (iser_alloc_login_buf(iser_conn))
258 		goto alloc_login_buf_fail;
259 
260 	iser_conn->num_rx_descs = session->cmds_max;
261 	iser_conn->rx_descs = kmalloc_array(iser_conn->num_rx_descs,
262 					    sizeof(struct iser_rx_desc),
263 					    GFP_KERNEL);
264 	if (!iser_conn->rx_descs)
265 		goto rx_desc_alloc_fail;
266 
267 	rx_desc = iser_conn->rx_descs;
268 
269 	for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)  {
270 		dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
271 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
272 		if (ib_dma_mapping_error(device->ib_device, dma_addr))
273 			goto rx_desc_dma_map_failed;
274 
275 		rx_desc->dma_addr = dma_addr;
276 		rx_desc->cqe.done = iser_task_rsp;
277 		rx_sg = &rx_desc->rx_sg;
278 		rx_sg->addr = rx_desc->dma_addr;
279 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
280 		rx_sg->lkey = device->pd->local_dma_lkey;
281 	}
282 
283 	iser_conn->rx_desc_head = 0;
284 	return 0;
285 
286 rx_desc_dma_map_failed:
287 	rx_desc = iser_conn->rx_descs;
288 	for (j = 0; j < i; j++, rx_desc++)
289 		ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
290 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
291 	kfree(iser_conn->rx_descs);
292 	iser_conn->rx_descs = NULL;
293 rx_desc_alloc_fail:
294 	iser_free_login_buf(iser_conn);
295 alloc_login_buf_fail:
296 	device->reg_ops->free_reg_res(ib_conn);
297 create_rdma_reg_res_failed:
298 	iser_err("failed allocating rx descriptors / data buffers\n");
299 	return -ENOMEM;
300 }
301 
302 void iser_free_rx_descriptors(struct iser_conn *iser_conn)
303 {
304 	int i;
305 	struct iser_rx_desc *rx_desc;
306 	struct ib_conn *ib_conn = &iser_conn->ib_conn;
307 	struct iser_device *device = ib_conn->device;
308 
309 	if (device->reg_ops->free_reg_res)
310 		device->reg_ops->free_reg_res(ib_conn);
311 
312 	rx_desc = iser_conn->rx_descs;
313 	for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)
314 		ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
315 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
316 	kfree(iser_conn->rx_descs);
317 	/* make sure we never redo any unmapping */
318 	iser_conn->rx_descs = NULL;
319 
320 	iser_free_login_buf(iser_conn);
321 }
322 
323 static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req)
324 {
325 	struct iser_conn *iser_conn = conn->dd_data;
326 	struct ib_conn *ib_conn = &iser_conn->ib_conn;
327 	struct iscsi_session *session = conn->session;
328 
329 	iser_dbg("req op %x flags %x\n", req->opcode, req->flags);
330 	/* check if this is the last login - going to full feature phase */
331 	if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE)
332 		return 0;
333 
334 	/*
335 	 * Check that there is one posted recv buffer
336 	 * (for the last login response).
337 	 */
338 	WARN_ON(ib_conn->post_recv_buf_count != 1);
339 
340 	if (session->discovery_sess) {
341 		iser_info("Discovery session, re-using login RX buffer\n");
342 		return 0;
343 	} else
344 		iser_info("Normal session, posting batch of RX %d buffers\n",
345 			  iser_conn->min_posted_rx);
346 
347 	/* Initial post receive buffers */
348 	if (iser_post_recvm(iser_conn, iser_conn->min_posted_rx))
349 		return -ENOMEM;
350 
351 	return 0;
352 }
353 
354 static inline bool iser_signal_comp(u8 sig_count)
355 {
356 	return ((sig_count % ISER_SIGNAL_CMD_COUNT) == 0);
357 }
358 
359 /**
360  * iser_send_command - send command PDU
361  */
362 int iser_send_command(struct iscsi_conn *conn,
363 		      struct iscsi_task *task)
364 {
365 	struct iser_conn *iser_conn = conn->dd_data;
366 	struct iscsi_iser_task *iser_task = task->dd_data;
367 	unsigned long edtl;
368 	int err;
369 	struct iser_data_buf *data_buf, *prot_buf;
370 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
371 	struct scsi_cmnd *sc  =  task->sc;
372 	struct iser_tx_desc *tx_desc = &iser_task->desc;
373 	u8 sig_count = ++iser_conn->ib_conn.sig_count;
374 
375 	edtl = ntohl(hdr->data_length);
376 
377 	/* build the tx desc regd header and add it to the tx desc dto */
378 	tx_desc->type = ISCSI_TX_SCSI_COMMAND;
379 	tx_desc->cqe.done = iser_cmd_comp;
380 	iser_create_send_desc(iser_conn, tx_desc);
381 
382 	if (hdr->flags & ISCSI_FLAG_CMD_READ) {
383 		data_buf = &iser_task->data[ISER_DIR_IN];
384 		prot_buf = &iser_task->prot[ISER_DIR_IN];
385 	} else {
386 		data_buf = &iser_task->data[ISER_DIR_OUT];
387 		prot_buf = &iser_task->prot[ISER_DIR_OUT];
388 	}
389 
390 	if (scsi_sg_count(sc)) { /* using a scatter list */
391 		data_buf->sg = scsi_sglist(sc);
392 		data_buf->size = scsi_sg_count(sc);
393 	}
394 	data_buf->data_len = scsi_bufflen(sc);
395 
396 	if (scsi_prot_sg_count(sc)) {
397 		prot_buf->sg  = scsi_prot_sglist(sc);
398 		prot_buf->size = scsi_prot_sg_count(sc);
399 		prot_buf->data_len = (data_buf->data_len >>
400 				     ilog2(sc->device->sector_size)) * 8;
401 	}
402 
403 	if (hdr->flags & ISCSI_FLAG_CMD_READ) {
404 		err = iser_prepare_read_cmd(task);
405 		if (err)
406 			goto send_command_error;
407 	}
408 	if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
409 		err = iser_prepare_write_cmd(task,
410 					     task->imm_count,
411 				             task->imm_count +
412 					     task->unsol_r2t.data_length,
413 					     edtl);
414 		if (err)
415 			goto send_command_error;
416 	}
417 
418 	iser_task->status = ISER_TASK_STATUS_STARTED;
419 
420 	err = iser_post_send(&iser_conn->ib_conn, tx_desc,
421 			     iser_signal_comp(sig_count));
422 	if (!err)
423 		return 0;
424 
425 send_command_error:
426 	iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
427 	return err;
428 }
429 
430 /**
431  * iser_send_data_out - send data out PDU
432  */
433 int iser_send_data_out(struct iscsi_conn *conn,
434 		       struct iscsi_task *task,
435 		       struct iscsi_data *hdr)
436 {
437 	struct iser_conn *iser_conn = conn->dd_data;
438 	struct iscsi_iser_task *iser_task = task->dd_data;
439 	struct iser_tx_desc *tx_desc;
440 	struct iser_mem_reg *mem_reg;
441 	unsigned long buf_offset;
442 	unsigned long data_seg_len;
443 	uint32_t itt;
444 	int err;
445 	struct ib_sge *tx_dsg;
446 
447 	itt = (__force uint32_t)hdr->itt;
448 	data_seg_len = ntoh24(hdr->dlength);
449 	buf_offset   = ntohl(hdr->offset);
450 
451 	iser_dbg("%s itt %d dseg_len %d offset %d\n",
452 		 __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
453 
454 	tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
455 	if (!tx_desc)
456 		return -ENOMEM;
457 
458 	tx_desc->type = ISCSI_TX_DATAOUT;
459 	tx_desc->cqe.done = iser_dataout_comp;
460 	tx_desc->iser_header.flags = ISER_VER;
461 	memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
462 
463 	/* build the tx desc */
464 	err = iser_initialize_task_headers(task, tx_desc);
465 	if (err)
466 		goto send_data_out_error;
467 
468 	mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT];
469 	tx_dsg = &tx_desc->tx_sg[1];
470 	tx_dsg->addr = mem_reg->sge.addr + buf_offset;
471 	tx_dsg->length = data_seg_len;
472 	tx_dsg->lkey = mem_reg->sge.lkey;
473 	tx_desc->num_sge = 2;
474 
475 	if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
476 		iser_err("Offset:%ld & DSL:%ld in Data-Out inconsistent with total len:%ld, itt:%d\n",
477 			 buf_offset, data_seg_len,
478 			 iser_task->data[ISER_DIR_OUT].data_len, itt);
479 		err = -EINVAL;
480 		goto send_data_out_error;
481 	}
482 	iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
483 		 itt, buf_offset, data_seg_len);
484 
485 
486 	err = iser_post_send(&iser_conn->ib_conn, tx_desc, true);
487 	if (!err)
488 		return 0;
489 
490 send_data_out_error:
491 	kmem_cache_free(ig.desc_cache, tx_desc);
492 	iser_err("conn %p failed err %d\n", conn, err);
493 	return err;
494 }
495 
496 int iser_send_control(struct iscsi_conn *conn,
497 		      struct iscsi_task *task)
498 {
499 	struct iser_conn *iser_conn = conn->dd_data;
500 	struct iscsi_iser_task *iser_task = task->dd_data;
501 	struct iser_tx_desc *mdesc = &iser_task->desc;
502 	unsigned long data_seg_len;
503 	int err = 0;
504 	struct iser_device *device;
505 
506 	/* build the tx desc regd header and add it to the tx desc dto */
507 	mdesc->type = ISCSI_TX_CONTROL;
508 	mdesc->cqe.done = iser_ctrl_comp;
509 	iser_create_send_desc(iser_conn, mdesc);
510 
511 	device = iser_conn->ib_conn.device;
512 
513 	data_seg_len = ntoh24(task->hdr->dlength);
514 
515 	if (data_seg_len > 0) {
516 		struct iser_login_desc *desc = &iser_conn->login_desc;
517 		struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
518 
519 		if (task != conn->login_task) {
520 			iser_err("data present on non login task!!!\n");
521 			goto send_control_error;
522 		}
523 
524 		ib_dma_sync_single_for_cpu(device->ib_device, desc->req_dma,
525 					   task->data_count, DMA_TO_DEVICE);
526 
527 		memcpy(desc->req, task->data, task->data_count);
528 
529 		ib_dma_sync_single_for_device(device->ib_device, desc->req_dma,
530 					      task->data_count, DMA_TO_DEVICE);
531 
532 		tx_dsg->addr = desc->req_dma;
533 		tx_dsg->length = task->data_count;
534 		tx_dsg->lkey = device->pd->local_dma_lkey;
535 		mdesc->num_sge = 2;
536 	}
537 
538 	if (task == conn->login_task) {
539 		iser_dbg("op %x dsl %lx, posting login rx buffer\n",
540 			 task->hdr->opcode, data_seg_len);
541 		err = iser_post_recvl(iser_conn);
542 		if (err)
543 			goto send_control_error;
544 		err = iser_post_rx_bufs(conn, task->hdr);
545 		if (err)
546 			goto send_control_error;
547 	}
548 
549 	err = iser_post_send(&iser_conn->ib_conn, mdesc, true);
550 	if (!err)
551 		return 0;
552 
553 send_control_error:
554 	iser_err("conn %p failed err %d\n",conn, err);
555 	return err;
556 }
557 
558 void iser_login_rsp(struct ib_cq *cq, struct ib_wc *wc)
559 {
560 	struct ib_conn *ib_conn = wc->qp->qp_context;
561 	struct iser_conn *iser_conn = to_iser_conn(ib_conn);
562 	struct iser_login_desc *desc = iser_login(wc->wr_cqe);
563 	struct iscsi_hdr *hdr;
564 	char *data;
565 	int length;
566 
567 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
568 		iser_err_comp(wc, "login_rsp");
569 		return;
570 	}
571 
572 	ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
573 				   desc->rsp_dma, ISER_RX_LOGIN_SIZE,
574 				   DMA_FROM_DEVICE);
575 
576 	hdr = desc->rsp + sizeof(struct iser_ctrl);
577 	data = desc->rsp + ISER_HEADERS_LEN;
578 	length = wc->byte_len - ISER_HEADERS_LEN;
579 
580 	iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
581 		 hdr->itt, length);
582 
583 	iscsi_iser_recv(iser_conn->iscsi_conn, hdr, data, length);
584 
585 	ib_dma_sync_single_for_device(ib_conn->device->ib_device,
586 				      desc->rsp_dma, ISER_RX_LOGIN_SIZE,
587 				      DMA_FROM_DEVICE);
588 
589 	ib_conn->post_recv_buf_count--;
590 }
591 
592 static inline int
593 iser_inv_desc(struct iser_fr_desc *desc, u32 rkey)
594 {
595 	if (likely(rkey == desc->rsc.mr->rkey)) {
596 		desc->rsc.mr_valid = 0;
597 	} else if (likely(desc->pi_ctx && rkey == desc->pi_ctx->sig_mr->rkey)) {
598 		desc->pi_ctx->sig_mr_valid = 0;
599 	} else {
600 		iser_err("Bogus remote invalidation for rkey %#x\n", rkey);
601 		return -EINVAL;
602 	}
603 
604 	return 0;
605 }
606 
607 static int
608 iser_check_remote_inv(struct iser_conn *iser_conn,
609 		      struct ib_wc *wc,
610 		      struct iscsi_hdr *hdr)
611 {
612 	if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
613 		struct iscsi_task *task;
614 		u32 rkey = wc->ex.invalidate_rkey;
615 
616 		iser_dbg("conn %p: remote invalidation for rkey %#x\n",
617 			 iser_conn, rkey);
618 
619 		if (unlikely(!iser_conn->snd_w_inv)) {
620 			iser_err("conn %p: unexpected remote invalidation, terminating connection\n",
621 				 iser_conn);
622 			return -EPROTO;
623 		}
624 
625 		task = iscsi_itt_to_ctask(iser_conn->iscsi_conn, hdr->itt);
626 		if (likely(task)) {
627 			struct iscsi_iser_task *iser_task = task->dd_data;
628 			struct iser_fr_desc *desc;
629 
630 			if (iser_task->dir[ISER_DIR_IN]) {
631 				desc = iser_task->rdma_reg[ISER_DIR_IN].mem_h;
632 				if (unlikely(iser_inv_desc(desc, rkey)))
633 					return -EINVAL;
634 			}
635 
636 			if (iser_task->dir[ISER_DIR_OUT]) {
637 				desc = iser_task->rdma_reg[ISER_DIR_OUT].mem_h;
638 				if (unlikely(iser_inv_desc(desc, rkey)))
639 					return -EINVAL;
640 			}
641 		} else {
642 			iser_err("failed to get task for itt=%d\n", hdr->itt);
643 			return -EINVAL;
644 		}
645 	}
646 
647 	return 0;
648 }
649 
650 
651 void iser_task_rsp(struct ib_cq *cq, struct ib_wc *wc)
652 {
653 	struct ib_conn *ib_conn = wc->qp->qp_context;
654 	struct iser_conn *iser_conn = to_iser_conn(ib_conn);
655 	struct iser_rx_desc *desc = iser_rx(wc->wr_cqe);
656 	struct iscsi_hdr *hdr;
657 	int length;
658 	int outstanding, count, err;
659 
660 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
661 		iser_err_comp(wc, "task_rsp");
662 		return;
663 	}
664 
665 	ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
666 				   desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
667 				   DMA_FROM_DEVICE);
668 
669 	hdr = &desc->iscsi_header;
670 	length = wc->byte_len - ISER_HEADERS_LEN;
671 
672 	iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
673 		 hdr->itt, length);
674 
675 	if (iser_check_remote_inv(iser_conn, wc, hdr)) {
676 		iscsi_conn_failure(iser_conn->iscsi_conn,
677 				   ISCSI_ERR_CONN_FAILED);
678 		return;
679 	}
680 
681 	iscsi_iser_recv(iser_conn->iscsi_conn, hdr, desc->data, length);
682 
683 	ib_dma_sync_single_for_device(ib_conn->device->ib_device,
684 				      desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
685 				      DMA_FROM_DEVICE);
686 
687 	/* decrementing conn->post_recv_buf_count only --after-- freeing the   *
688 	 * task eliminates the need to worry on tasks which are completed in   *
689 	 * parallel to the execution of iser_conn_term. So the code that waits *
690 	 * for the posted rx bufs refcount to become zero handles everything   */
691 	ib_conn->post_recv_buf_count--;
692 
693 	outstanding = ib_conn->post_recv_buf_count;
694 	if (outstanding + iser_conn->min_posted_rx <= iser_conn->qp_max_recv_dtos) {
695 		count = min(iser_conn->qp_max_recv_dtos - outstanding,
696 			    iser_conn->min_posted_rx);
697 		err = iser_post_recvm(iser_conn, count);
698 		if (err)
699 			iser_err("posting %d rx bufs err %d\n", count, err);
700 	}
701 }
702 
703 void iser_cmd_comp(struct ib_cq *cq, struct ib_wc *wc)
704 {
705 	if (unlikely(wc->status != IB_WC_SUCCESS))
706 		iser_err_comp(wc, "command");
707 }
708 
709 void iser_ctrl_comp(struct ib_cq *cq, struct ib_wc *wc)
710 {
711 	struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
712 	struct iscsi_task *task;
713 
714 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
715 		iser_err_comp(wc, "control");
716 		return;
717 	}
718 
719 	/* this arithmetic is legal by libiscsi dd_data allocation */
720 	task = (void *)desc - sizeof(struct iscsi_task);
721 	if (task->hdr->itt == RESERVED_ITT)
722 		iscsi_put_task(task);
723 }
724 
725 void iser_dataout_comp(struct ib_cq *cq, struct ib_wc *wc)
726 {
727 	struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
728 	struct ib_conn *ib_conn = wc->qp->qp_context;
729 	struct iser_device *device = ib_conn->device;
730 
731 	if (unlikely(wc->status != IB_WC_SUCCESS))
732 		iser_err_comp(wc, "dataout");
733 
734 	ib_dma_unmap_single(device->ib_device, desc->dma_addr,
735 			    ISER_HEADERS_LEN, DMA_TO_DEVICE);
736 	kmem_cache_free(ig.desc_cache, desc);
737 }
738 
739 void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
740 
741 {
742 	iser_task->status = ISER_TASK_STATUS_INIT;
743 
744 	iser_task->dir[ISER_DIR_IN] = 0;
745 	iser_task->dir[ISER_DIR_OUT] = 0;
746 
747 	iser_task->data[ISER_DIR_IN].data_len  = 0;
748 	iser_task->data[ISER_DIR_OUT].data_len = 0;
749 
750 	iser_task->prot[ISER_DIR_IN].data_len  = 0;
751 	iser_task->prot[ISER_DIR_OUT].data_len = 0;
752 
753 	memset(&iser_task->rdma_reg[ISER_DIR_IN], 0,
754 	       sizeof(struct iser_mem_reg));
755 	memset(&iser_task->rdma_reg[ISER_DIR_OUT], 0,
756 	       sizeof(struct iser_mem_reg));
757 }
758 
759 void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
760 {
761 	int prot_count = scsi_prot_sg_count(iser_task->sc);
762 
763 	if (iser_task->dir[ISER_DIR_IN]) {
764 		iser_unreg_rdma_mem(iser_task, ISER_DIR_IN);
765 		iser_dma_unmap_task_data(iser_task,
766 					 &iser_task->data[ISER_DIR_IN],
767 					 DMA_FROM_DEVICE);
768 		if (prot_count)
769 			iser_dma_unmap_task_data(iser_task,
770 						 &iser_task->prot[ISER_DIR_IN],
771 						 DMA_FROM_DEVICE);
772 	}
773 
774 	if (iser_task->dir[ISER_DIR_OUT]) {
775 		iser_unreg_rdma_mem(iser_task, ISER_DIR_OUT);
776 		iser_dma_unmap_task_data(iser_task,
777 					 &iser_task->data[ISER_DIR_OUT],
778 					 DMA_TO_DEVICE);
779 		if (prot_count)
780 			iser_dma_unmap_task_data(iser_task,
781 						 &iser_task->prot[ISER_DIR_OUT],
782 						 DMA_TO_DEVICE);
783 	}
784 }
785