iser_initiator.c 21 KB

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