iser_initiator.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/scatterlist.h>
  36. #include <linux/kfifo.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <scsi/scsi_host.h>
  39. #include "iscsi_iser.h"
  40. /* Register user buffer memory and initialize passive rdma
  41. * dto descriptor. Total data size is stored in
  42. * iser_task->data[ISER_DIR_IN].data_len
  43. */
  44. static int iser_prepare_read_cmd(struct iscsi_task *task,
  45. unsigned int edtl)
  46. {
  47. struct iscsi_iser_task *iser_task = task->dd_data;
  48. struct iser_regd_buf *regd_buf;
  49. int err;
  50. struct iser_hdr *hdr = &iser_task->desc.iser_header;
  51. struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN];
  52. err = iser_dma_map_task_data(iser_task,
  53. buf_in,
  54. ISER_DIR_IN,
  55. DMA_FROM_DEVICE);
  56. if (err)
  57. return err;
  58. if (edtl > iser_task->data[ISER_DIR_IN].data_len) {
  59. iser_err("Total data length: %ld, less than EDTL: "
  60. "%d, in READ cmd BHS itt: %d, conn: 0x%p\n",
  61. iser_task->data[ISER_DIR_IN].data_len, edtl,
  62. task->itt, iser_task->iser_conn);
  63. return -EINVAL;
  64. }
  65. err = iser_reg_rdma_mem(iser_task,ISER_DIR_IN);
  66. if (err) {
  67. iser_err("Failed to set up Data-IN RDMA\n");
  68. return err;
  69. }
  70. regd_buf = &iser_task->rdma_regd[ISER_DIR_IN];
  71. hdr->flags |= ISER_RSV;
  72. hdr->read_stag = cpu_to_be32(regd_buf->reg.rkey);
  73. hdr->read_va = cpu_to_be64(regd_buf->reg.va);
  74. iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
  75. task->itt, regd_buf->reg.rkey,
  76. (unsigned long long)regd_buf->reg.va);
  77. return 0;
  78. }
  79. /* Register user buffer memory and initialize passive rdma
  80. * dto descriptor. Total data size is stored in
  81. * task->data[ISER_DIR_OUT].data_len
  82. */
  83. static int
  84. iser_prepare_write_cmd(struct iscsi_task *task,
  85. unsigned int imm_sz,
  86. unsigned int unsol_sz,
  87. unsigned int edtl)
  88. {
  89. struct iscsi_iser_task *iser_task = task->dd_data;
  90. struct iser_regd_buf *regd_buf;
  91. int err;
  92. struct iser_hdr *hdr = &iser_task->desc.iser_header;
  93. struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT];
  94. struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1];
  95. err = iser_dma_map_task_data(iser_task,
  96. buf_out,
  97. ISER_DIR_OUT,
  98. DMA_TO_DEVICE);
  99. if (err)
  100. return err;
  101. if (edtl > iser_task->data[ISER_DIR_OUT].data_len) {
  102. iser_err("Total data length: %ld, less than EDTL: %d, "
  103. "in WRITE cmd BHS itt: %d, conn: 0x%p\n",
  104. iser_task->data[ISER_DIR_OUT].data_len,
  105. edtl, task->itt, task->conn);
  106. return -EINVAL;
  107. }
  108. err = iser_reg_rdma_mem(iser_task,ISER_DIR_OUT);
  109. if (err != 0) {
  110. iser_err("Failed to register write cmd RDMA mem\n");
  111. return err;
  112. }
  113. regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
  114. if (unsol_sz < edtl) {
  115. hdr->flags |= ISER_WSV;
  116. hdr->write_stag = cpu_to_be32(regd_buf->reg.rkey);
  117. hdr->write_va = cpu_to_be64(regd_buf->reg.va + unsol_sz);
  118. iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X "
  119. "VA:%#llX + unsol:%d\n",
  120. task->itt, regd_buf->reg.rkey,
  121. (unsigned long long)regd_buf->reg.va, unsol_sz);
  122. }
  123. if (imm_sz > 0) {
  124. iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
  125. task->itt, imm_sz);
  126. tx_dsg->addr = regd_buf->reg.va;
  127. tx_dsg->length = imm_sz;
  128. tx_dsg->lkey = regd_buf->reg.lkey;
  129. iser_task->desc.num_sge = 2;
  130. }
  131. return 0;
  132. }
  133. /* creates a new tx descriptor and adds header regd buffer */
  134. static void iser_create_send_desc(struct iser_conn *ib_conn,
  135. struct iser_tx_desc *tx_desc)
  136. {
  137. struct iser_device *device = ib_conn->device;
  138. ib_dma_sync_single_for_cpu(device->ib_device,
  139. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  140. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  141. tx_desc->iser_header.flags = ISER_VER;
  142. tx_desc->num_sge = 1;
  143. if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
  144. tx_desc->tx_sg[0].lkey = device->mr->lkey;
  145. iser_dbg("sdesc %p lkey mismatch, fixing\n", tx_desc);
  146. }
  147. }
  148. int iser_alloc_rx_descriptors(struct iser_conn *ib_conn)
  149. {
  150. int i, j;
  151. u64 dma_addr;
  152. struct iser_rx_desc *rx_desc;
  153. struct ib_sge *rx_sg;
  154. struct iser_device *device = ib_conn->device;
  155. ib_conn->rx_descs = kmalloc(ISER_QP_MAX_RECV_DTOS *
  156. sizeof(struct iser_rx_desc), GFP_KERNEL);
  157. if (!ib_conn->rx_descs)
  158. goto rx_desc_alloc_fail;
  159. rx_desc = ib_conn->rx_descs;
  160. for (i = 0; i < ISER_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  161. dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
  162. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  163. if (ib_dma_mapping_error(device->ib_device, dma_addr))
  164. goto rx_desc_dma_map_failed;
  165. rx_desc->dma_addr = dma_addr;
  166. rx_sg = &rx_desc->rx_sg;
  167. rx_sg->addr = rx_desc->dma_addr;
  168. rx_sg->length = ISER_RX_PAYLOAD_SIZE;
  169. rx_sg->lkey = device->mr->lkey;
  170. }
  171. ib_conn->rx_desc_head = 0;
  172. return 0;
  173. rx_desc_dma_map_failed:
  174. rx_desc = ib_conn->rx_descs;
  175. for (j = 0; j < i; j++, rx_desc++)
  176. ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
  177. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  178. kfree(ib_conn->rx_descs);
  179. ib_conn->rx_descs = NULL;
  180. rx_desc_alloc_fail:
  181. iser_err("failed allocating rx descriptors / data buffers\n");
  182. return -ENOMEM;
  183. }
  184. void iser_free_rx_descriptors(struct iser_conn *ib_conn)
  185. {
  186. int i;
  187. struct iser_rx_desc *rx_desc;
  188. struct iser_device *device = ib_conn->device;
  189. if (ib_conn->login_buf) {
  190. ib_dma_unmap_single(device->ib_device, ib_conn->login_dma,
  191. ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
  192. kfree(ib_conn->login_buf);
  193. }
  194. if (!ib_conn->rx_descs)
  195. return;
  196. rx_desc = ib_conn->rx_descs;
  197. for (i = 0; i < ISER_QP_MAX_RECV_DTOS; i++, rx_desc++)
  198. ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
  199. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  200. kfree(ib_conn->rx_descs);
  201. }
  202. /**
  203. * iser_conn_set_full_featured_mode - (iSER API)
  204. */
  205. int iser_conn_set_full_featured_mode(struct iscsi_conn *conn)
  206. {
  207. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  208. iser_dbg("Initially post: %d\n", ISER_MIN_POSTED_RX);
  209. /* Check that there is no posted recv or send buffers left - */
  210. /* they must be consumed during the login phase */
  211. BUG_ON(iser_conn->ib_conn->post_recv_buf_count != 0);
  212. BUG_ON(atomic_read(&iser_conn->ib_conn->post_send_buf_count) != 0);
  213. if (iser_alloc_rx_descriptors(iser_conn->ib_conn))
  214. return -ENOMEM;
  215. /* Initial post receive buffers */
  216. if (iser_post_recvm(iser_conn->ib_conn, ISER_MIN_POSTED_RX))
  217. return -ENOMEM;
  218. return 0;
  219. }
  220. static int
  221. iser_check_xmit(struct iscsi_conn *conn, void *task)
  222. {
  223. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  224. if (atomic_read(&iser_conn->ib_conn->post_send_buf_count) ==
  225. ISER_QP_MAX_REQ_DTOS) {
  226. iser_dbg("%ld can't xmit task %p\n",jiffies,task);
  227. return -ENOBUFS;
  228. }
  229. return 0;
  230. }
  231. /**
  232. * iser_send_command - send command PDU
  233. */
  234. int iser_send_command(struct iscsi_conn *conn,
  235. struct iscsi_task *task)
  236. {
  237. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  238. struct iscsi_iser_task *iser_task = task->dd_data;
  239. unsigned long edtl;
  240. int err;
  241. struct iser_data_buf *data_buf;
  242. struct iscsi_cmd *hdr = (struct iscsi_cmd *)task->hdr;
  243. struct scsi_cmnd *sc = task->sc;
  244. struct iser_tx_desc *tx_desc = &iser_task->desc;
  245. if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
  246. iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
  247. return -EPERM;
  248. }
  249. if (iser_check_xmit(conn, task))
  250. return -ENOBUFS;
  251. edtl = ntohl(hdr->data_length);
  252. /* build the tx desc regd header and add it to the tx desc dto */
  253. tx_desc->type = ISCSI_TX_SCSI_COMMAND;
  254. iser_create_send_desc(iser_conn->ib_conn, tx_desc);
  255. if (hdr->flags & ISCSI_FLAG_CMD_READ)
  256. data_buf = &iser_task->data[ISER_DIR_IN];
  257. else
  258. data_buf = &iser_task->data[ISER_DIR_OUT];
  259. if (scsi_sg_count(sc)) { /* using a scatter list */
  260. data_buf->buf = scsi_sglist(sc);
  261. data_buf->size = scsi_sg_count(sc);
  262. }
  263. data_buf->data_len = scsi_bufflen(sc);
  264. if (hdr->flags & ISCSI_FLAG_CMD_READ) {
  265. err = iser_prepare_read_cmd(task, edtl);
  266. if (err)
  267. goto send_command_error;
  268. }
  269. if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
  270. err = iser_prepare_write_cmd(task,
  271. task->imm_count,
  272. task->imm_count +
  273. task->unsol_r2t.data_length,
  274. edtl);
  275. if (err)
  276. goto send_command_error;
  277. }
  278. iser_task->status = ISER_TASK_STATUS_STARTED;
  279. err = iser_post_send(iser_conn->ib_conn, tx_desc);
  280. if (!err)
  281. return 0;
  282. send_command_error:
  283. iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
  284. return err;
  285. }
  286. /**
  287. * iser_send_data_out - send data out PDU
  288. */
  289. int iser_send_data_out(struct iscsi_conn *conn,
  290. struct iscsi_task *task,
  291. struct iscsi_data *hdr)
  292. {
  293. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  294. struct iscsi_iser_task *iser_task = task->dd_data;
  295. struct iser_tx_desc *tx_desc = NULL;
  296. struct iser_regd_buf *regd_buf;
  297. unsigned long buf_offset;
  298. unsigned long data_seg_len;
  299. uint32_t itt;
  300. int err = 0;
  301. struct ib_sge *tx_dsg;
  302. if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
  303. iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
  304. return -EPERM;
  305. }
  306. if (iser_check_xmit(conn, task))
  307. return -ENOBUFS;
  308. itt = (__force uint32_t)hdr->itt;
  309. data_seg_len = ntoh24(hdr->dlength);
  310. buf_offset = ntohl(hdr->offset);
  311. iser_dbg("%s itt %d dseg_len %d offset %d\n",
  312. __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
  313. tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
  314. if (tx_desc == NULL) {
  315. iser_err("Failed to alloc desc for post dataout\n");
  316. return -ENOMEM;
  317. }
  318. tx_desc->type = ISCSI_TX_DATAOUT;
  319. tx_desc->iser_header.flags = ISER_VER;
  320. memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
  321. /* build the tx desc */
  322. iser_initialize_task_headers(task, tx_desc);
  323. regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
  324. tx_dsg = &tx_desc->tx_sg[1];
  325. tx_dsg->addr = regd_buf->reg.va + buf_offset;
  326. tx_dsg->length = data_seg_len;
  327. tx_dsg->lkey = regd_buf->reg.lkey;
  328. tx_desc->num_sge = 2;
  329. if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
  330. iser_err("Offset:%ld & DSL:%ld in Data-Out "
  331. "inconsistent with total len:%ld, itt:%d\n",
  332. buf_offset, data_seg_len,
  333. iser_task->data[ISER_DIR_OUT].data_len, itt);
  334. err = -EINVAL;
  335. goto send_data_out_error;
  336. }
  337. iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
  338. itt, buf_offset, data_seg_len);
  339. err = iser_post_send(iser_conn->ib_conn, tx_desc);
  340. if (!err)
  341. return 0;
  342. send_data_out_error:
  343. kmem_cache_free(ig.desc_cache, tx_desc);
  344. iser_err("conn %p failed err %d\n",conn, err);
  345. return err;
  346. }
  347. int iser_send_control(struct iscsi_conn *conn,
  348. struct iscsi_task *task)
  349. {
  350. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  351. struct iscsi_iser_task *iser_task = task->dd_data;
  352. struct iser_tx_desc *mdesc = &iser_task->desc;
  353. unsigned long data_seg_len;
  354. int err = 0;
  355. struct iser_device *device;
  356. if (!iser_conn_state_comp(iser_conn->ib_conn, ISER_CONN_UP)) {
  357. iser_err("Failed to send, conn: 0x%p is not up\n", iser_conn->ib_conn);
  358. return -EPERM;
  359. }
  360. if (iser_check_xmit(conn, task))
  361. return -ENOBUFS;
  362. /* build the tx desc regd header and add it to the tx desc dto */
  363. mdesc->type = ISCSI_TX_CONTROL;
  364. iser_create_send_desc(iser_conn->ib_conn, mdesc);
  365. device = iser_conn->ib_conn->device;
  366. data_seg_len = ntoh24(task->hdr->dlength);
  367. if (data_seg_len > 0) {
  368. struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
  369. if (task != conn->login_task) {
  370. iser_err("data present on non login task!!!\n");
  371. goto send_control_error;
  372. }
  373. memcpy(iser_conn->ib_conn->login_buf, task->data,
  374. task->data_count);
  375. tx_dsg->addr = iser_conn->ib_conn->login_dma;
  376. tx_dsg->length = data_seg_len;
  377. tx_dsg->lkey = device->mr->lkey;
  378. mdesc->num_sge = 2;
  379. }
  380. if (task == conn->login_task) {
  381. err = iser_post_recvl(iser_conn->ib_conn);
  382. if (err)
  383. goto send_control_error;
  384. }
  385. err = iser_post_send(iser_conn->ib_conn, mdesc);
  386. if (!err)
  387. return 0;
  388. send_control_error:
  389. iser_err("conn %p failed err %d\n",conn, err);
  390. return err;
  391. }
  392. /**
  393. * iser_rcv_dto_completion - recv DTO completion
  394. */
  395. void iser_rcv_completion(struct iser_rx_desc *rx_desc,
  396. unsigned long rx_xfer_len,
  397. struct iser_conn *ib_conn)
  398. {
  399. struct iscsi_iser_conn *conn = ib_conn->iser_conn;
  400. struct iscsi_task *task;
  401. struct iscsi_iser_task *iser_task;
  402. struct iscsi_hdr *hdr;
  403. unsigned char opcode;
  404. u64 rx_dma;
  405. int rx_buflen, outstanding, count, err;
  406. /* differentiate between login to all other PDUs */
  407. if ((char *)rx_desc == ib_conn->login_buf) {
  408. rx_dma = ib_conn->login_dma;
  409. rx_buflen = ISER_RX_LOGIN_SIZE;
  410. } else {
  411. rx_dma = rx_desc->dma_addr;
  412. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  413. }
  414. ib_dma_sync_single_for_cpu(ib_conn->device->ib_device, rx_dma,
  415. rx_buflen, DMA_FROM_DEVICE);
  416. hdr = &rx_desc->iscsi_header;
  417. iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
  418. hdr->itt, (int)(rx_xfer_len - ISER_HEADERS_LEN));
  419. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  420. if (opcode == ISCSI_OP_SCSI_CMD_RSP) {
  421. spin_lock(&conn->iscsi_conn->session->lock);
  422. task = iscsi_itt_to_ctask(conn->iscsi_conn, hdr->itt);
  423. if (task)
  424. __iscsi_get_task(task);
  425. spin_unlock(&conn->iscsi_conn->session->lock);
  426. if (!task)
  427. iser_err("itt can't be matched to task!!! "
  428. "conn %p opcode %d itt %d\n",
  429. conn->iscsi_conn, opcode, hdr->itt);
  430. else {
  431. iser_task = task->dd_data;
  432. iser_dbg("itt %d task %p\n",hdr->itt, task);
  433. iser_task->status = ISER_TASK_STATUS_COMPLETED;
  434. iser_task_rdma_finalize(iser_task);
  435. iscsi_put_task(task);
  436. }
  437. }
  438. iscsi_iser_recv(conn->iscsi_conn, hdr,
  439. rx_desc->data, rx_xfer_len - ISER_HEADERS_LEN);
  440. ib_dma_sync_single_for_device(ib_conn->device->ib_device, rx_dma,
  441. rx_buflen, DMA_FROM_DEVICE);
  442. /* decrementing conn->post_recv_buf_count only --after-- freeing the *
  443. * task eliminates the need to worry on tasks which are completed in *
  444. * parallel to the execution of iser_conn_term. So the code that waits *
  445. * for the posted rx bufs refcount to become zero handles everything */
  446. conn->ib_conn->post_recv_buf_count--;
  447. if (rx_dma == ib_conn->login_dma)
  448. return;
  449. outstanding = ib_conn->post_recv_buf_count;
  450. if (outstanding + ISER_MIN_POSTED_RX <= ISER_QP_MAX_RECV_DTOS) {
  451. count = min(ISER_QP_MAX_RECV_DTOS - outstanding,
  452. ISER_MIN_POSTED_RX);
  453. err = iser_post_recvm(ib_conn, count);
  454. if (err)
  455. iser_err("posting %d rx bufs err %d\n", count, err);
  456. }
  457. }
  458. void iser_snd_completion(struct iser_tx_desc *tx_desc,
  459. struct iser_conn *ib_conn)
  460. {
  461. struct iscsi_iser_conn *iser_conn = ib_conn->iser_conn;
  462. struct iscsi_conn *conn = iser_conn->iscsi_conn;
  463. struct iscsi_task *task;
  464. int resume_tx = 0;
  465. struct iser_device *device = ib_conn->device;
  466. if (tx_desc->type == ISCSI_TX_DATAOUT) {
  467. ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
  468. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  469. kmem_cache_free(ig.desc_cache, tx_desc);
  470. }
  471. if (atomic_read(&iser_conn->ib_conn->post_send_buf_count) ==
  472. ISER_QP_MAX_REQ_DTOS)
  473. resume_tx = 1;
  474. atomic_dec(&ib_conn->post_send_buf_count);
  475. if (resume_tx) {
  476. iser_dbg("%ld resuming tx\n",jiffies);
  477. iscsi_conn_queue_work(conn);
  478. }
  479. if (tx_desc->type == ISCSI_TX_CONTROL) {
  480. /* this arithmetic is legal by libiscsi dd_data allocation */
  481. task = (void *) ((long)(void *)tx_desc -
  482. sizeof(struct iscsi_task));
  483. if (task->hdr->itt == RESERVED_ITT)
  484. iscsi_put_task(task);
  485. }
  486. }
  487. void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
  488. {
  489. iser_task->status = ISER_TASK_STATUS_INIT;
  490. iser_task->dir[ISER_DIR_IN] = 0;
  491. iser_task->dir[ISER_DIR_OUT] = 0;
  492. iser_task->data[ISER_DIR_IN].data_len = 0;
  493. iser_task->data[ISER_DIR_OUT].data_len = 0;
  494. memset(&iser_task->rdma_regd[ISER_DIR_IN], 0,
  495. sizeof(struct iser_regd_buf));
  496. memset(&iser_task->rdma_regd[ISER_DIR_OUT], 0,
  497. sizeof(struct iser_regd_buf));
  498. }
  499. void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
  500. {
  501. int is_rdma_aligned = 1;
  502. struct iser_regd_buf *regd;
  503. /* if we were reading, copy back to unaligned sglist,
  504. * anyway dma_unmap and free the copy
  505. */
  506. if (iser_task->data_copy[ISER_DIR_IN].copy_buf != NULL) {
  507. is_rdma_aligned = 0;
  508. iser_finalize_rdma_unaligned_sg(iser_task, ISER_DIR_IN);
  509. }
  510. if (iser_task->data_copy[ISER_DIR_OUT].copy_buf != NULL) {
  511. is_rdma_aligned = 0;
  512. iser_finalize_rdma_unaligned_sg(iser_task, ISER_DIR_OUT);
  513. }
  514. if (iser_task->dir[ISER_DIR_IN]) {
  515. regd = &iser_task->rdma_regd[ISER_DIR_IN];
  516. if (regd->reg.is_fmr)
  517. iser_unreg_mem(&regd->reg);
  518. }
  519. if (iser_task->dir[ISER_DIR_OUT]) {
  520. regd = &iser_task->rdma_regd[ISER_DIR_OUT];
  521. if (regd->reg.is_fmr)
  522. iser_unreg_mem(&regd->reg);
  523. }
  524. /* if the data was unaligned, it was already unmapped and then copied */
  525. if (is_rdma_aligned)
  526. iser_dma_unmap_task_data(iser_task);
  527. }