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->scsi_sg_tablesize))
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(iser_conn->num_rx_descs *
262 				sizeof(struct iser_rx_desc), GFP_KERNEL);
263 	if (!iser_conn->rx_descs)
264 		goto rx_desc_alloc_fail;
265 
266 	rx_desc = iser_conn->rx_descs;
267 
268 	for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)  {
269 		dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
270 					ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
271 		if (ib_dma_mapping_error(device->ib_device, dma_addr))
272 			goto rx_desc_dma_map_failed;
273 
274 		rx_desc->dma_addr = dma_addr;
275 		rx_desc->cqe.done = iser_task_rsp;
276 		rx_sg = &rx_desc->rx_sg;
277 		rx_sg->addr = rx_desc->dma_addr;
278 		rx_sg->length = ISER_RX_PAYLOAD_SIZE;
279 		rx_sg->lkey = device->pd->local_dma_lkey;
280 	}
281 
282 	iser_conn->rx_desc_head = 0;
283 	return 0;
284 
285 rx_desc_dma_map_failed:
286 	rx_desc = iser_conn->rx_descs;
287 	for (j = 0; j < i; j++, rx_desc++)
288 		ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
289 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
290 	kfree(iser_conn->rx_descs);
291 	iser_conn->rx_descs = NULL;
292 rx_desc_alloc_fail:
293 	iser_free_login_buf(iser_conn);
294 alloc_login_buf_fail:
295 	device->reg_ops->free_reg_res(ib_conn);
296 create_rdma_reg_res_failed:
297 	iser_err("failed allocating rx descriptors / data buffers\n");
298 	return -ENOMEM;
299 }
300 
301 void iser_free_rx_descriptors(struct iser_conn *iser_conn)
302 {
303 	int i;
304 	struct iser_rx_desc *rx_desc;
305 	struct ib_conn *ib_conn = &iser_conn->ib_conn;
306 	struct iser_device *device = ib_conn->device;
307 
308 	if (device->reg_ops->free_reg_res)
309 		device->reg_ops->free_reg_res(ib_conn);
310 
311 	rx_desc = iser_conn->rx_descs;
312 	for (i = 0; i < iser_conn->qp_max_recv_dtos; i++, rx_desc++)
313 		ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
314 				    ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
315 	kfree(iser_conn->rx_descs);
316 	/* make sure we never redo any unmapping */
317 	iser_conn->rx_descs = NULL;
318 
319 	iser_free_login_buf(iser_conn);
320 }
321 
322 static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req)
323 {
324 	struct iser_conn *iser_conn = conn->dd_data;
325 	struct ib_conn *ib_conn = &iser_conn->ib_conn;
326 	struct iscsi_session *session = conn->session;
327 
328 	iser_dbg("req op %x flags %x\n", req->opcode, req->flags);
329 	/* check if this is the last login - going to full feature phase */
330 	if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE)
331 		return 0;
332 
333 	/*
334 	 * Check that there is one posted recv buffer
335 	 * (for the last login response).
336 	 */
337 	WARN_ON(ib_conn->post_recv_buf_count != 1);
338 
339 	if (session->discovery_sess) {
340 		iser_info("Discovery session, re-using login RX buffer\n");
341 		return 0;
342 	} else
343 		iser_info("Normal session, posting batch of RX %d buffers\n",
344 			  iser_conn->min_posted_rx);
345 
346 	/* Initial post receive buffers */
347 	if (iser_post_recvm(iser_conn, iser_conn->min_posted_rx))
348 		return -ENOMEM;
349 
350 	return 0;
351 }
352 
353 static inline bool iser_signal_comp(u8 sig_count)
354 {
355 	return ((sig_count % ISER_SIGNAL_CMD_COUNT) == 0);
356 }
357 
358 /**
359  * iser_send_command - send command PDU
360  */
361 int iser_send_command(struct iscsi_conn *conn,
362 		      struct iscsi_task *task)
363 {
364 	struct iser_conn *iser_conn = conn->dd_data;
365 	struct iscsi_iser_task *iser_task = task->dd_data;
366 	unsigned long edtl;
367 	int err;
368 	struct iser_data_buf *data_buf, *prot_buf;
369 	struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
370 	struct scsi_cmnd *sc  =  task->sc;
371 	struct iser_tx_desc *tx_desc = &iser_task->desc;
372 	u8 sig_count = ++iser_conn->ib_conn.sig_count;
373 
374 	edtl = ntohl(hdr->data_length);
375 
376 	/* build the tx desc regd header and add it to the tx desc dto */
377 	tx_desc->type = ISCSI_TX_SCSI_COMMAND;
378 	tx_desc->cqe.done = iser_cmd_comp;
379 	iser_create_send_desc(iser_conn, tx_desc);
380 
381 	if (hdr->flags & ISCSI_FLAG_CMD_READ) {
382 		data_buf = &iser_task->data[ISER_DIR_IN];
383 		prot_buf = &iser_task->prot[ISER_DIR_IN];
384 	} else {
385 		data_buf = &iser_task->data[ISER_DIR_OUT];
386 		prot_buf = &iser_task->prot[ISER_DIR_OUT];
387 	}
388 
389 	if (scsi_sg_count(sc)) { /* using a scatter list */
390 		data_buf->sg = scsi_sglist(sc);
391 		data_buf->size = scsi_sg_count(sc);
392 	}
393 	data_buf->data_len = scsi_bufflen(sc);
394 
395 	if (scsi_prot_sg_count(sc)) {
396 		prot_buf->sg  = scsi_prot_sglist(sc);
397 		prot_buf->size = scsi_prot_sg_count(sc);
398 		prot_buf->data_len = (data_buf->data_len >>
399 				     ilog2(sc->device->sector_size)) * 8;
400 	}
401 
402 	if (hdr->flags & ISCSI_FLAG_CMD_READ) {
403 		err = iser_prepare_read_cmd(task);
404 		if (err)
405 			goto send_command_error;
406 	}
407 	if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
408 		err = iser_prepare_write_cmd(task,
409 					     task->imm_count,
410 				             task->imm_count +
411 					     task->unsol_r2t.data_length,
412 					     edtl);
413 		if (err)
414 			goto send_command_error;
415 	}
416 
417 	iser_task->status = ISER_TASK_STATUS_STARTED;
418 
419 	err = iser_post_send(&iser_conn->ib_conn, tx_desc,
420 			     iser_signal_comp(sig_count));
421 	if (!err)
422 		return 0;
423 
424 send_command_error:
425 	iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
426 	return err;
427 }
428 
429 /**
430  * iser_send_data_out - send data out PDU
431  */
432 int iser_send_data_out(struct iscsi_conn *conn,
433 		       struct iscsi_task *task,
434 		       struct iscsi_data *hdr)
435 {
436 	struct iser_conn *iser_conn = conn->dd_data;
437 	struct iscsi_iser_task *iser_task = task->dd_data;
438 	struct iser_tx_desc *tx_desc;
439 	struct iser_mem_reg *mem_reg;
440 	unsigned long buf_offset;
441 	unsigned long data_seg_len;
442 	uint32_t itt;
443 	int err;
444 	struct ib_sge *tx_dsg;
445 
446 	itt = (__force uint32_t)hdr->itt;
447 	data_seg_len = ntoh24(hdr->dlength);
448 	buf_offset   = ntohl(hdr->offset);
449 
450 	iser_dbg("%s itt %d dseg_len %d offset %d\n",
451 		 __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
452 
453 	tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
454 	if (!tx_desc)
455 		return -ENOMEM;
456 
457 	tx_desc->type = ISCSI_TX_DATAOUT;
458 	tx_desc->cqe.done = iser_dataout_comp;
459 	tx_desc->iser_header.flags = ISER_VER;
460 	memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
461 
462 	/* build the tx desc */
463 	err = iser_initialize_task_headers(task, tx_desc);
464 	if (err)
465 		goto send_data_out_error;
466 
467 	mem_reg = &iser_task->rdma_reg[ISER_DIR_OUT];
468 	tx_dsg = &tx_desc->tx_sg[1];
469 	tx_dsg->addr = mem_reg->sge.addr + buf_offset;
470 	tx_dsg->length = data_seg_len;
471 	tx_dsg->lkey = mem_reg->sge.lkey;
472 	tx_desc->num_sge = 2;
473 
474 	if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
475 		iser_err("Offset:%ld & DSL:%ld in Data-Out inconsistent with total len:%ld, itt:%d\n",
476 			 buf_offset, data_seg_len,
477 			 iser_task->data[ISER_DIR_OUT].data_len, itt);
478 		err = -EINVAL;
479 		goto send_data_out_error;
480 	}
481 	iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
482 		 itt, buf_offset, data_seg_len);
483 
484 
485 	err = iser_post_send(&iser_conn->ib_conn, tx_desc, true);
486 	if (!err)
487 		return 0;
488 
489 send_data_out_error:
490 	kmem_cache_free(ig.desc_cache, tx_desc);
491 	iser_err("conn %p failed err %d\n", conn, err);
492 	return err;
493 }
494 
495 int iser_send_control(struct iscsi_conn *conn,
496 		      struct iscsi_task *task)
497 {
498 	struct iser_conn *iser_conn = conn->dd_data;
499 	struct iscsi_iser_task *iser_task = task->dd_data;
500 	struct iser_tx_desc *mdesc = &iser_task->desc;
501 	unsigned long data_seg_len;
502 	int err = 0;
503 	struct iser_device *device;
504 
505 	/* build the tx desc regd header and add it to the tx desc dto */
506 	mdesc->type = ISCSI_TX_CONTROL;
507 	mdesc->cqe.done = iser_ctrl_comp;
508 	iser_create_send_desc(iser_conn, mdesc);
509 
510 	device = iser_conn->ib_conn.device;
511 
512 	data_seg_len = ntoh24(task->hdr->dlength);
513 
514 	if (data_seg_len > 0) {
515 		struct iser_login_desc *desc = &iser_conn->login_desc;
516 		struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
517 
518 		if (task != conn->login_task) {
519 			iser_err("data present on non login task!!!\n");
520 			goto send_control_error;
521 		}
522 
523 		ib_dma_sync_single_for_cpu(device->ib_device, desc->req_dma,
524 					   task->data_count, DMA_TO_DEVICE);
525 
526 		memcpy(desc->req, task->data, task->data_count);
527 
528 		ib_dma_sync_single_for_device(device->ib_device, desc->req_dma,
529 					      task->data_count, DMA_TO_DEVICE);
530 
531 		tx_dsg->addr = desc->req_dma;
532 		tx_dsg->length = task->data_count;
533 		tx_dsg->lkey = device->pd->local_dma_lkey;
534 		mdesc->num_sge = 2;
535 	}
536 
537 	if (task == conn->login_task) {
538 		iser_dbg("op %x dsl %lx, posting login rx buffer\n",
539 			 task->hdr->opcode, data_seg_len);
540 		err = iser_post_recvl(iser_conn);
541 		if (err)
542 			goto send_control_error;
543 		err = iser_post_rx_bufs(conn, task->hdr);
544 		if (err)
545 			goto send_control_error;
546 	}
547 
548 	err = iser_post_send(&iser_conn->ib_conn, mdesc, true);
549 	if (!err)
550 		return 0;
551 
552 send_control_error:
553 	iser_err("conn %p failed err %d\n",conn, err);
554 	return err;
555 }
556 
557 void iser_login_rsp(struct ib_cq *cq, struct ib_wc *wc)
558 {
559 	struct ib_conn *ib_conn = wc->qp->qp_context;
560 	struct iser_conn *iser_conn = to_iser_conn(ib_conn);
561 	struct iser_login_desc *desc = iser_login(wc->wr_cqe);
562 	struct iscsi_hdr *hdr;
563 	char *data;
564 	int length;
565 
566 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
567 		iser_err_comp(wc, "login_rsp");
568 		return;
569 	}
570 
571 	ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
572 				   desc->rsp_dma, ISER_RX_LOGIN_SIZE,
573 				   DMA_FROM_DEVICE);
574 
575 	hdr = desc->rsp + sizeof(struct iser_ctrl);
576 	data = desc->rsp + ISER_HEADERS_LEN;
577 	length = wc->byte_len - ISER_HEADERS_LEN;
578 
579 	iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
580 		 hdr->itt, length);
581 
582 	iscsi_iser_recv(iser_conn->iscsi_conn, hdr, data, length);
583 
584 	ib_dma_sync_single_for_device(ib_conn->device->ib_device,
585 				      desc->rsp_dma, ISER_RX_LOGIN_SIZE,
586 				      DMA_FROM_DEVICE);
587 
588 	ib_conn->post_recv_buf_count--;
589 }
590 
591 static inline void
592 iser_inv_desc(struct iser_fr_desc *desc, u32 rkey)
593 {
594 	if (likely(rkey == desc->rsc.mr->rkey))
595 		desc->rsc.mr_valid = 0;
596 	else if (likely(rkey == desc->pi_ctx->sig_mr->rkey))
597 		desc->pi_ctx->sig_mr_valid = 0;
598 }
599 
600 static int
601 iser_check_remote_inv(struct iser_conn *iser_conn,
602 		      struct ib_wc *wc,
603 		      struct iscsi_hdr *hdr)
604 {
605 	if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
606 		struct iscsi_task *task;
607 		u32 rkey = wc->ex.invalidate_rkey;
608 
609 		iser_dbg("conn %p: remote invalidation for rkey %#x\n",
610 			 iser_conn, rkey);
611 
612 		if (unlikely(!iser_conn->snd_w_inv)) {
613 			iser_err("conn %p: unexpected remote invalidation, terminating connection\n",
614 				 iser_conn);
615 			return -EPROTO;
616 		}
617 
618 		task = iscsi_itt_to_ctask(iser_conn->iscsi_conn, hdr->itt);
619 		if (likely(task)) {
620 			struct iscsi_iser_task *iser_task = task->dd_data;
621 			struct iser_fr_desc *desc;
622 
623 			if (iser_task->dir[ISER_DIR_IN]) {
624 				desc = iser_task->rdma_reg[ISER_DIR_IN].mem_h;
625 				iser_inv_desc(desc, rkey);
626 			}
627 
628 			if (iser_task->dir[ISER_DIR_OUT]) {
629 				desc = iser_task->rdma_reg[ISER_DIR_OUT].mem_h;
630 				iser_inv_desc(desc, rkey);
631 			}
632 		} else {
633 			iser_err("failed to get task for itt=%d\n", hdr->itt);
634 			return -EINVAL;
635 		}
636 	}
637 
638 	return 0;
639 }
640 
641 
642 void iser_task_rsp(struct ib_cq *cq, struct ib_wc *wc)
643 {
644 	struct ib_conn *ib_conn = wc->qp->qp_context;
645 	struct iser_conn *iser_conn = to_iser_conn(ib_conn);
646 	struct iser_rx_desc *desc = iser_rx(wc->wr_cqe);
647 	struct iscsi_hdr *hdr;
648 	int length;
649 	int outstanding, count, err;
650 
651 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
652 		iser_err_comp(wc, "task_rsp");
653 		return;
654 	}
655 
656 	ib_dma_sync_single_for_cpu(ib_conn->device->ib_device,
657 				   desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
658 				   DMA_FROM_DEVICE);
659 
660 	hdr = &desc->iscsi_header;
661 	length = wc->byte_len - ISER_HEADERS_LEN;
662 
663 	iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
664 		 hdr->itt, length);
665 
666 	if (iser_check_remote_inv(iser_conn, wc, hdr)) {
667 		iscsi_conn_failure(iser_conn->iscsi_conn,
668 				   ISCSI_ERR_CONN_FAILED);
669 		return;
670 	}
671 
672 	iscsi_iser_recv(iser_conn->iscsi_conn, hdr, desc->data, length);
673 
674 	ib_dma_sync_single_for_device(ib_conn->device->ib_device,
675 				      desc->dma_addr, ISER_RX_PAYLOAD_SIZE,
676 				      DMA_FROM_DEVICE);
677 
678 	/* decrementing conn->post_recv_buf_count only --after-- freeing the   *
679 	 * task eliminates the need to worry on tasks which are completed in   *
680 	 * parallel to the execution of iser_conn_term. So the code that waits *
681 	 * for the posted rx bufs refcount to become zero handles everything   */
682 	ib_conn->post_recv_buf_count--;
683 
684 	outstanding = ib_conn->post_recv_buf_count;
685 	if (outstanding + iser_conn->min_posted_rx <= iser_conn->qp_max_recv_dtos) {
686 		count = min(iser_conn->qp_max_recv_dtos - outstanding,
687 			    iser_conn->min_posted_rx);
688 		err = iser_post_recvm(iser_conn, count);
689 		if (err)
690 			iser_err("posting %d rx bufs err %d\n", count, err);
691 	}
692 }
693 
694 void iser_cmd_comp(struct ib_cq *cq, struct ib_wc *wc)
695 {
696 	if (unlikely(wc->status != IB_WC_SUCCESS))
697 		iser_err_comp(wc, "command");
698 }
699 
700 void iser_ctrl_comp(struct ib_cq *cq, struct ib_wc *wc)
701 {
702 	struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
703 	struct iscsi_task *task;
704 
705 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
706 		iser_err_comp(wc, "control");
707 		return;
708 	}
709 
710 	/* this arithmetic is legal by libiscsi dd_data allocation */
711 	task = (void *)desc - sizeof(struct iscsi_task);
712 	if (task->hdr->itt == RESERVED_ITT)
713 		iscsi_put_task(task);
714 }
715 
716 void iser_dataout_comp(struct ib_cq *cq, struct ib_wc *wc)
717 {
718 	struct iser_tx_desc *desc = iser_tx(wc->wr_cqe);
719 	struct ib_conn *ib_conn = wc->qp->qp_context;
720 	struct iser_device *device = ib_conn->device;
721 
722 	if (unlikely(wc->status != IB_WC_SUCCESS))
723 		iser_err_comp(wc, "dataout");
724 
725 	ib_dma_unmap_single(device->ib_device, desc->dma_addr,
726 			    ISER_HEADERS_LEN, DMA_TO_DEVICE);
727 	kmem_cache_free(ig.desc_cache, desc);
728 }
729 
730 void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
731 
732 {
733 	iser_task->status = ISER_TASK_STATUS_INIT;
734 
735 	iser_task->dir[ISER_DIR_IN] = 0;
736 	iser_task->dir[ISER_DIR_OUT] = 0;
737 
738 	iser_task->data[ISER_DIR_IN].data_len  = 0;
739 	iser_task->data[ISER_DIR_OUT].data_len = 0;
740 
741 	iser_task->prot[ISER_DIR_IN].data_len  = 0;
742 	iser_task->prot[ISER_DIR_OUT].data_len = 0;
743 
744 	memset(&iser_task->rdma_reg[ISER_DIR_IN], 0,
745 	       sizeof(struct iser_mem_reg));
746 	memset(&iser_task->rdma_reg[ISER_DIR_OUT], 0,
747 	       sizeof(struct iser_mem_reg));
748 }
749 
750 void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
751 {
752 	int prot_count = scsi_prot_sg_count(iser_task->sc);
753 
754 	if (iser_task->dir[ISER_DIR_IN]) {
755 		iser_unreg_rdma_mem(iser_task, ISER_DIR_IN);
756 		iser_dma_unmap_task_data(iser_task,
757 					 &iser_task->data[ISER_DIR_IN],
758 					 DMA_FROM_DEVICE);
759 		if (prot_count)
760 			iser_dma_unmap_task_data(iser_task,
761 						 &iser_task->prot[ISER_DIR_IN],
762 						 DMA_FROM_DEVICE);
763 	}
764 
765 	if (iser_task->dir[ISER_DIR_OUT]) {
766 		iser_unreg_rdma_mem(iser_task, ISER_DIR_OUT);
767 		iser_dma_unmap_task_data(iser_task,
768 					 &iser_task->data[ISER_DIR_OUT],
769 					 DMA_TO_DEVICE);
770 		if (prot_count)
771 			iser_dma_unmap_task_data(iser_task,
772 						 &iser_task->prot[ISER_DIR_OUT],
773 						 DMA_TO_DEVICE);
774 	}
775 }
776