iser_initiator.c 20 KB

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