iser_initiator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2013 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. #include "iscsi_iser.h"
  41. /* Register user buffer memory and initialize passive rdma
  42. * dto descriptor. Total data size is stored in
  43. * iser_task->data[ISER_DIR_IN].data_len
  44. */
  45. static int iser_prepare_read_cmd(struct iscsi_task *task,
  46. unsigned int edtl)
  47. {
  48. struct iscsi_iser_task *iser_task = task->dd_data;
  49. struct iser_regd_buf *regd_buf;
  50. int err;
  51. struct iser_hdr *hdr = &iser_task->desc.iser_header;
  52. struct iser_data_buf *buf_in = &iser_task->data[ISER_DIR_IN];
  53. err = iser_dma_map_task_data(iser_task,
  54. buf_in,
  55. ISER_DIR_IN,
  56. DMA_FROM_DEVICE);
  57. if (err)
  58. return err;
  59. if (edtl > iser_task->data[ISER_DIR_IN].data_len) {
  60. iser_err("Total data length: %ld, less than EDTL: "
  61. "%d, in READ cmd BHS itt: %d, conn: 0x%p\n",
  62. iser_task->data[ISER_DIR_IN].data_len, edtl,
  63. task->itt, iser_task->iser_conn);
  64. return -EINVAL;
  65. }
  66. err = iser_reg_rdma_mem(iser_task,ISER_DIR_IN);
  67. if (err) {
  68. iser_err("Failed to set up Data-IN RDMA\n");
  69. return err;
  70. }
  71. regd_buf = &iser_task->rdma_regd[ISER_DIR_IN];
  72. hdr->flags |= ISER_RSV;
  73. hdr->read_stag = cpu_to_be32(regd_buf->reg.rkey);
  74. hdr->read_va = cpu_to_be64(regd_buf->reg.va);
  75. iser_dbg("Cmd itt:%d READ tags RKEY:%#.4X VA:%#llX\n",
  76. task->itt, regd_buf->reg.rkey,
  77. (unsigned long long)regd_buf->reg.va);
  78. return 0;
  79. }
  80. /* Register user buffer memory and initialize passive rdma
  81. * dto descriptor. Total data size is stored in
  82. * task->data[ISER_DIR_OUT].data_len
  83. */
  84. static int
  85. iser_prepare_write_cmd(struct iscsi_task *task,
  86. unsigned int imm_sz,
  87. unsigned int unsol_sz,
  88. unsigned int edtl)
  89. {
  90. struct iscsi_iser_task *iser_task = task->dd_data;
  91. struct iser_regd_buf *regd_buf;
  92. int err;
  93. struct iser_hdr *hdr = &iser_task->desc.iser_header;
  94. struct iser_data_buf *buf_out = &iser_task->data[ISER_DIR_OUT];
  95. struct ib_sge *tx_dsg = &iser_task->desc.tx_sg[1];
  96. err = iser_dma_map_task_data(iser_task,
  97. buf_out,
  98. ISER_DIR_OUT,
  99. DMA_TO_DEVICE);
  100. if (err)
  101. return err;
  102. if (edtl > iser_task->data[ISER_DIR_OUT].data_len) {
  103. iser_err("Total data length: %ld, less than EDTL: %d, "
  104. "in WRITE cmd BHS itt: %d, conn: 0x%p\n",
  105. iser_task->data[ISER_DIR_OUT].data_len,
  106. edtl, task->itt, task->conn);
  107. return -EINVAL;
  108. }
  109. err = iser_reg_rdma_mem(iser_task,ISER_DIR_OUT);
  110. if (err != 0) {
  111. iser_err("Failed to register write cmd RDMA mem\n");
  112. return err;
  113. }
  114. regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
  115. if (unsol_sz < edtl) {
  116. hdr->flags |= ISER_WSV;
  117. hdr->write_stag = cpu_to_be32(regd_buf->reg.rkey);
  118. hdr->write_va = cpu_to_be64(regd_buf->reg.va + unsol_sz);
  119. iser_dbg("Cmd itt:%d, WRITE tags, RKEY:%#.4X "
  120. "VA:%#llX + unsol:%d\n",
  121. task->itt, regd_buf->reg.rkey,
  122. (unsigned long long)regd_buf->reg.va, unsol_sz);
  123. }
  124. if (imm_sz > 0) {
  125. iser_dbg("Cmd itt:%d, WRITE, adding imm.data sz: %d\n",
  126. task->itt, imm_sz);
  127. tx_dsg->addr = regd_buf->reg.va;
  128. tx_dsg->length = imm_sz;
  129. tx_dsg->lkey = regd_buf->reg.lkey;
  130. iser_task->desc.num_sge = 2;
  131. }
  132. return 0;
  133. }
  134. /* creates a new tx descriptor and adds header regd buffer */
  135. static void iser_create_send_desc(struct iser_conn *ib_conn,
  136. struct iser_tx_desc *tx_desc)
  137. {
  138. struct iser_device *device = ib_conn->device;
  139. ib_dma_sync_single_for_cpu(device->ib_device,
  140. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  141. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  142. tx_desc->iser_header.flags = ISER_VER;
  143. tx_desc->num_sge = 1;
  144. if (tx_desc->tx_sg[0].lkey != device->mr->lkey) {
  145. tx_desc->tx_sg[0].lkey = device->mr->lkey;
  146. iser_dbg("sdesc %p lkey mismatch, fixing\n", tx_desc);
  147. }
  148. }
  149. static void iser_free_login_buf(struct iser_conn *ib_conn)
  150. {
  151. if (!ib_conn->login_buf)
  152. return;
  153. if (ib_conn->login_req_dma)
  154. ib_dma_unmap_single(ib_conn->device->ib_device,
  155. ib_conn->login_req_dma,
  156. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE);
  157. if (ib_conn->login_resp_dma)
  158. ib_dma_unmap_single(ib_conn->device->ib_device,
  159. ib_conn->login_resp_dma,
  160. ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
  161. kfree(ib_conn->login_buf);
  162. /* make sure we never redo any unmapping */
  163. ib_conn->login_req_dma = 0;
  164. ib_conn->login_resp_dma = 0;
  165. ib_conn->login_buf = NULL;
  166. }
  167. static int iser_alloc_login_buf(struct iser_conn *ib_conn)
  168. {
  169. struct iser_device *device;
  170. int req_err, resp_err;
  171. BUG_ON(ib_conn->device == NULL);
  172. device = ib_conn->device;
  173. ib_conn->login_buf = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
  174. ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  175. if (!ib_conn->login_buf)
  176. goto out_err;
  177. ib_conn->login_req_buf = ib_conn->login_buf;
  178. ib_conn->login_resp_buf = ib_conn->login_buf +
  179. ISCSI_DEF_MAX_RECV_SEG_LEN;
  180. ib_conn->login_req_dma = ib_dma_map_single(ib_conn->device->ib_device,
  181. (void *)ib_conn->login_req_buf,
  182. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_TO_DEVICE);
  183. ib_conn->login_resp_dma = ib_dma_map_single(ib_conn->device->ib_device,
  184. (void *)ib_conn->login_resp_buf,
  185. ISER_RX_LOGIN_SIZE, DMA_FROM_DEVICE);
  186. req_err = ib_dma_mapping_error(device->ib_device,
  187. ib_conn->login_req_dma);
  188. resp_err = ib_dma_mapping_error(device->ib_device,
  189. ib_conn->login_resp_dma);
  190. if (req_err || resp_err) {
  191. if (req_err)
  192. ib_conn->login_req_dma = 0;
  193. if (resp_err)
  194. ib_conn->login_resp_dma = 0;
  195. goto free_login_buf;
  196. }
  197. return 0;
  198. free_login_buf:
  199. iser_free_login_buf(ib_conn);
  200. out_err:
  201. iser_err("unable to alloc or map login buf\n");
  202. return -ENOMEM;
  203. }
  204. int iser_alloc_rx_descriptors(struct iser_conn *ib_conn, struct iscsi_session *session)
  205. {
  206. int i, j;
  207. u64 dma_addr;
  208. struct iser_rx_desc *rx_desc;
  209. struct ib_sge *rx_sg;
  210. struct iser_device *device = ib_conn->device;
  211. ib_conn->qp_max_recv_dtos = session->cmds_max;
  212. ib_conn->qp_max_recv_dtos_mask = session->cmds_max - 1; /* cmds_max is 2^N */
  213. ib_conn->min_posted_rx = ib_conn->qp_max_recv_dtos >> 2;
  214. if (iser_create_fmr_pool(ib_conn, session->scsi_cmds_max))
  215. goto create_fmr_pool_failed;
  216. if (iser_alloc_login_buf(ib_conn))
  217. goto alloc_login_buf_fail;
  218. ib_conn->rx_descs = kmalloc(session->cmds_max *
  219. sizeof(struct iser_rx_desc), GFP_KERNEL);
  220. if (!ib_conn->rx_descs)
  221. goto rx_desc_alloc_fail;
  222. rx_desc = ib_conn->rx_descs;
  223. for (i = 0; i < ib_conn->qp_max_recv_dtos; i++, rx_desc++) {
  224. dma_addr = ib_dma_map_single(device->ib_device, (void *)rx_desc,
  225. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  226. if (ib_dma_mapping_error(device->ib_device, dma_addr))
  227. goto rx_desc_dma_map_failed;
  228. rx_desc->dma_addr = dma_addr;
  229. rx_sg = &rx_desc->rx_sg;
  230. rx_sg->addr = rx_desc->dma_addr;
  231. rx_sg->length = ISER_RX_PAYLOAD_SIZE;
  232. rx_sg->lkey = device->mr->lkey;
  233. }
  234. ib_conn->rx_desc_head = 0;
  235. return 0;
  236. rx_desc_dma_map_failed:
  237. rx_desc = ib_conn->rx_descs;
  238. for (j = 0; j < i; j++, rx_desc++)
  239. ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
  240. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  241. kfree(ib_conn->rx_descs);
  242. ib_conn->rx_descs = NULL;
  243. rx_desc_alloc_fail:
  244. iser_free_login_buf(ib_conn);
  245. alloc_login_buf_fail:
  246. iser_free_fmr_pool(ib_conn);
  247. create_fmr_pool_failed:
  248. iser_err("failed allocating rx descriptors / data buffers\n");
  249. return -ENOMEM;
  250. }
  251. void iser_free_rx_descriptors(struct iser_conn *ib_conn)
  252. {
  253. int i;
  254. struct iser_rx_desc *rx_desc;
  255. struct iser_device *device = ib_conn->device;
  256. if (!ib_conn->rx_descs)
  257. goto free_login_buf;
  258. rx_desc = ib_conn->rx_descs;
  259. for (i = 0; i < ib_conn->qp_max_recv_dtos; i++, rx_desc++)
  260. ib_dma_unmap_single(device->ib_device, rx_desc->dma_addr,
  261. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  262. kfree(ib_conn->rx_descs);
  263. /* make sure we never redo any unmapping */
  264. ib_conn->rx_descs = NULL;
  265. free_login_buf:
  266. iser_free_login_buf(ib_conn);
  267. iser_free_fmr_pool(ib_conn);
  268. }
  269. static int iser_post_rx_bufs(struct iscsi_conn *conn, struct iscsi_hdr *req)
  270. {
  271. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  272. iser_dbg("req op %x flags %x\n", req->opcode, req->flags);
  273. /* check if this is the last login - going to full feature phase */
  274. if ((req->flags & ISCSI_FULL_FEATURE_PHASE) != ISCSI_FULL_FEATURE_PHASE)
  275. return 0;
  276. /*
  277. * Check that there is one posted recv buffer (for the last login
  278. * response) and no posted send buffers left - they must have been
  279. * consumed during previous login phases.
  280. */
  281. WARN_ON(iser_conn->ib_conn->post_recv_buf_count != 1);
  282. WARN_ON(atomic_read(&iser_conn->ib_conn->post_send_buf_count) != 0);
  283. iser_dbg("Initially post: %d\n", iser_conn->ib_conn->min_posted_rx);
  284. /* Initial post receive buffers */
  285. if (iser_post_recvm(iser_conn->ib_conn,
  286. iser_conn->ib_conn->min_posted_rx))
  287. return -ENOMEM;
  288. return 0;
  289. }
  290. /**
  291. * iser_send_command - send command PDU
  292. */
  293. int iser_send_command(struct iscsi_conn *conn,
  294. struct iscsi_task *task)
  295. {
  296. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  297. struct iscsi_iser_task *iser_task = task->dd_data;
  298. unsigned long edtl;
  299. int err;
  300. struct iser_data_buf *data_buf;
  301. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
  302. struct scsi_cmnd *sc = task->sc;
  303. struct iser_tx_desc *tx_desc = &iser_task->desc;
  304. edtl = ntohl(hdr->data_length);
  305. /* build the tx desc regd header and add it to the tx desc dto */
  306. tx_desc->type = ISCSI_TX_SCSI_COMMAND;
  307. iser_create_send_desc(iser_conn->ib_conn, tx_desc);
  308. if (hdr->flags & ISCSI_FLAG_CMD_READ)
  309. data_buf = &iser_task->data[ISER_DIR_IN];
  310. else
  311. data_buf = &iser_task->data[ISER_DIR_OUT];
  312. if (scsi_sg_count(sc)) { /* using a scatter list */
  313. data_buf->buf = scsi_sglist(sc);
  314. data_buf->size = scsi_sg_count(sc);
  315. }
  316. data_buf->data_len = scsi_bufflen(sc);
  317. if (hdr->flags & ISCSI_FLAG_CMD_READ) {
  318. err = iser_prepare_read_cmd(task, edtl);
  319. if (err)
  320. goto send_command_error;
  321. }
  322. if (hdr->flags & ISCSI_FLAG_CMD_WRITE) {
  323. err = iser_prepare_write_cmd(task,
  324. task->imm_count,
  325. task->imm_count +
  326. task->unsol_r2t.data_length,
  327. edtl);
  328. if (err)
  329. goto send_command_error;
  330. }
  331. iser_task->status = ISER_TASK_STATUS_STARTED;
  332. err = iser_post_send(iser_conn->ib_conn, tx_desc);
  333. if (!err)
  334. return 0;
  335. send_command_error:
  336. iser_err("conn %p failed task->itt %d err %d\n",conn, task->itt, err);
  337. return err;
  338. }
  339. /**
  340. * iser_send_data_out - send data out PDU
  341. */
  342. int iser_send_data_out(struct iscsi_conn *conn,
  343. struct iscsi_task *task,
  344. struct iscsi_data *hdr)
  345. {
  346. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  347. struct iscsi_iser_task *iser_task = task->dd_data;
  348. struct iser_tx_desc *tx_desc = NULL;
  349. struct iser_regd_buf *regd_buf;
  350. unsigned long buf_offset;
  351. unsigned long data_seg_len;
  352. uint32_t itt;
  353. int err = 0;
  354. struct ib_sge *tx_dsg;
  355. itt = (__force uint32_t)hdr->itt;
  356. data_seg_len = ntoh24(hdr->dlength);
  357. buf_offset = ntohl(hdr->offset);
  358. iser_dbg("%s itt %d dseg_len %d offset %d\n",
  359. __func__,(int)itt,(int)data_seg_len,(int)buf_offset);
  360. tx_desc = kmem_cache_zalloc(ig.desc_cache, GFP_ATOMIC);
  361. if (tx_desc == NULL) {
  362. iser_err("Failed to alloc desc for post dataout\n");
  363. return -ENOMEM;
  364. }
  365. tx_desc->type = ISCSI_TX_DATAOUT;
  366. tx_desc->iser_header.flags = ISER_VER;
  367. memcpy(&tx_desc->iscsi_header, hdr, sizeof(struct iscsi_hdr));
  368. /* build the tx desc */
  369. iser_initialize_task_headers(task, tx_desc);
  370. regd_buf = &iser_task->rdma_regd[ISER_DIR_OUT];
  371. tx_dsg = &tx_desc->tx_sg[1];
  372. tx_dsg->addr = regd_buf->reg.va + buf_offset;
  373. tx_dsg->length = data_seg_len;
  374. tx_dsg->lkey = regd_buf->reg.lkey;
  375. tx_desc->num_sge = 2;
  376. if (buf_offset + data_seg_len > iser_task->data[ISER_DIR_OUT].data_len) {
  377. iser_err("Offset:%ld & DSL:%ld in Data-Out "
  378. "inconsistent with total len:%ld, itt:%d\n",
  379. buf_offset, data_seg_len,
  380. iser_task->data[ISER_DIR_OUT].data_len, itt);
  381. err = -EINVAL;
  382. goto send_data_out_error;
  383. }
  384. iser_dbg("data-out itt: %d, offset: %ld, sz: %ld\n",
  385. itt, buf_offset, data_seg_len);
  386. err = iser_post_send(iser_conn->ib_conn, tx_desc);
  387. if (!err)
  388. return 0;
  389. send_data_out_error:
  390. kmem_cache_free(ig.desc_cache, tx_desc);
  391. iser_err("conn %p failed err %d\n",conn, err);
  392. return err;
  393. }
  394. int iser_send_control(struct iscsi_conn *conn,
  395. struct iscsi_task *task)
  396. {
  397. struct iscsi_iser_conn *iser_conn = conn->dd_data;
  398. struct iscsi_iser_task *iser_task = task->dd_data;
  399. struct iser_tx_desc *mdesc = &iser_task->desc;
  400. unsigned long data_seg_len;
  401. int err = 0;
  402. struct iser_device *device;
  403. struct iser_conn *ib_conn = iser_conn->ib_conn;
  404. /* build the tx desc regd header and add it to the tx desc dto */
  405. mdesc->type = ISCSI_TX_CONTROL;
  406. iser_create_send_desc(iser_conn->ib_conn, mdesc);
  407. device = iser_conn->ib_conn->device;
  408. data_seg_len = ntoh24(task->hdr->dlength);
  409. if (data_seg_len > 0) {
  410. struct ib_sge *tx_dsg = &mdesc->tx_sg[1];
  411. if (task != conn->login_task) {
  412. iser_err("data present on non login task!!!\n");
  413. goto send_control_error;
  414. }
  415. ib_dma_sync_single_for_cpu(device->ib_device,
  416. ib_conn->login_req_dma, task->data_count,
  417. DMA_TO_DEVICE);
  418. memcpy(iser_conn->ib_conn->login_req_buf, task->data,
  419. task->data_count);
  420. ib_dma_sync_single_for_device(device->ib_device,
  421. ib_conn->login_req_dma, task->data_count,
  422. DMA_TO_DEVICE);
  423. tx_dsg->addr = iser_conn->ib_conn->login_req_dma;
  424. tx_dsg->length = task->data_count;
  425. tx_dsg->lkey = device->mr->lkey;
  426. mdesc->num_sge = 2;
  427. }
  428. if (task == conn->login_task) {
  429. err = iser_post_recvl(iser_conn->ib_conn);
  430. if (err)
  431. goto send_control_error;
  432. err = iser_post_rx_bufs(conn, task->hdr);
  433. if (err)
  434. goto send_control_error;
  435. }
  436. err = iser_post_send(iser_conn->ib_conn, mdesc);
  437. if (!err)
  438. return 0;
  439. send_control_error:
  440. iser_err("conn %p failed err %d\n",conn, err);
  441. return err;
  442. }
  443. /**
  444. * iser_rcv_dto_completion - recv DTO completion
  445. */
  446. void iser_rcv_completion(struct iser_rx_desc *rx_desc,
  447. unsigned long rx_xfer_len,
  448. struct iser_conn *ib_conn)
  449. {
  450. struct iscsi_iser_conn *conn = ib_conn->iser_conn;
  451. struct iscsi_hdr *hdr;
  452. u64 rx_dma;
  453. int rx_buflen, outstanding, count, err;
  454. /* differentiate between login to all other PDUs */
  455. if ((char *)rx_desc == ib_conn->login_resp_buf) {
  456. rx_dma = ib_conn->login_resp_dma;
  457. rx_buflen = ISER_RX_LOGIN_SIZE;
  458. } else {
  459. rx_dma = rx_desc->dma_addr;
  460. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  461. }
  462. ib_dma_sync_single_for_cpu(ib_conn->device->ib_device, rx_dma,
  463. rx_buflen, DMA_FROM_DEVICE);
  464. hdr = &rx_desc->iscsi_header;
  465. iser_dbg("op 0x%x itt 0x%x dlen %d\n", hdr->opcode,
  466. hdr->itt, (int)(rx_xfer_len - ISER_HEADERS_LEN));
  467. iscsi_iser_recv(conn->iscsi_conn, hdr,
  468. rx_desc->data, rx_xfer_len - ISER_HEADERS_LEN);
  469. ib_dma_sync_single_for_device(ib_conn->device->ib_device, rx_dma,
  470. rx_buflen, DMA_FROM_DEVICE);
  471. /* decrementing conn->post_recv_buf_count only --after-- freeing the *
  472. * task eliminates the need to worry on tasks which are completed in *
  473. * parallel to the execution of iser_conn_term. So the code that waits *
  474. * for the posted rx bufs refcount to become zero handles everything */
  475. conn->ib_conn->post_recv_buf_count--;
  476. if (rx_dma == ib_conn->login_resp_dma)
  477. return;
  478. outstanding = ib_conn->post_recv_buf_count;
  479. if (outstanding + ib_conn->min_posted_rx <= ib_conn->qp_max_recv_dtos) {
  480. count = min(ib_conn->qp_max_recv_dtos - outstanding,
  481. ib_conn->min_posted_rx);
  482. err = iser_post_recvm(ib_conn, count);
  483. if (err)
  484. iser_err("posting %d rx bufs err %d\n", count, err);
  485. }
  486. }
  487. void iser_snd_completion(struct iser_tx_desc *tx_desc,
  488. struct iser_conn *ib_conn)
  489. {
  490. struct iscsi_task *task;
  491. struct iser_device *device = ib_conn->device;
  492. if (tx_desc->type == ISCSI_TX_DATAOUT) {
  493. ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
  494. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  495. kmem_cache_free(ig.desc_cache, tx_desc);
  496. }
  497. atomic_dec(&ib_conn->post_send_buf_count);
  498. if (tx_desc->type == ISCSI_TX_CONTROL) {
  499. /* this arithmetic is legal by libiscsi dd_data allocation */
  500. task = (void *) ((long)(void *)tx_desc -
  501. sizeof(struct iscsi_task));
  502. if (task->hdr->itt == RESERVED_ITT)
  503. iscsi_put_task(task);
  504. }
  505. }
  506. void iser_task_rdma_init(struct iscsi_iser_task *iser_task)
  507. {
  508. iser_task->status = ISER_TASK_STATUS_INIT;
  509. iser_task->dir[ISER_DIR_IN] = 0;
  510. iser_task->dir[ISER_DIR_OUT] = 0;
  511. iser_task->data[ISER_DIR_IN].data_len = 0;
  512. iser_task->data[ISER_DIR_OUT].data_len = 0;
  513. memset(&iser_task->rdma_regd[ISER_DIR_IN], 0,
  514. sizeof(struct iser_regd_buf));
  515. memset(&iser_task->rdma_regd[ISER_DIR_OUT], 0,
  516. sizeof(struct iser_regd_buf));
  517. }
  518. void iser_task_rdma_finalize(struct iscsi_iser_task *iser_task)
  519. {
  520. int is_rdma_aligned = 1;
  521. struct iser_regd_buf *regd;
  522. /* if we were reading, copy back to unaligned sglist,
  523. * anyway dma_unmap and free the copy
  524. */
  525. if (iser_task->data_copy[ISER_DIR_IN].copy_buf != NULL) {
  526. is_rdma_aligned = 0;
  527. iser_finalize_rdma_unaligned_sg(iser_task, ISER_DIR_IN);
  528. }
  529. if (iser_task->data_copy[ISER_DIR_OUT].copy_buf != NULL) {
  530. is_rdma_aligned = 0;
  531. iser_finalize_rdma_unaligned_sg(iser_task, ISER_DIR_OUT);
  532. }
  533. if (iser_task->dir[ISER_DIR_IN]) {
  534. regd = &iser_task->rdma_regd[ISER_DIR_IN];
  535. if (regd->reg.is_fmr)
  536. iser_unreg_mem(&regd->reg);
  537. }
  538. if (iser_task->dir[ISER_DIR_OUT]) {
  539. regd = &iser_task->rdma_regd[ISER_DIR_OUT];
  540. if (regd->reg.is_fmr)
  541. iser_unreg_mem(&regd->reg);
  542. }
  543. /* if the data was unaligned, it was already unmapped and then copied */
  544. if (is_rdma_aligned)
  545. iser_dma_unmap_task_data(iser_task);
  546. }