iser_initiator.c 19 KB

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