ib_isert.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  1. /*******************************************************************************
  2. * This file contains iSCSI extentions for RDMA (iSER) Verbs
  3. *
  4. * (c) Copyright 2013 RisingTide Systems LLC.
  5. *
  6. * Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ****************************************************************************/
  18. #include <linux/string.h>
  19. #include <linux/module.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/socket.h>
  22. #include <linux/in.h>
  23. #include <linux/in6.h>
  24. #include <rdma/ib_verbs.h>
  25. #include <rdma/rdma_cm.h>
  26. #include <target/target_core_base.h>
  27. #include <target/target_core_fabric.h>
  28. #include <target/iscsi/iscsi_transport.h>
  29. #include "isert_proto.h"
  30. #include "ib_isert.h"
  31. #define ISERT_MAX_CONN 8
  32. #define ISER_MAX_RX_CQ_LEN (ISERT_QP_MAX_RECV_DTOS * ISERT_MAX_CONN)
  33. #define ISER_MAX_TX_CQ_LEN (ISERT_QP_MAX_REQ_DTOS * ISERT_MAX_CONN)
  34. static DEFINE_MUTEX(device_list_mutex);
  35. static LIST_HEAD(device_list);
  36. static struct workqueue_struct *isert_rx_wq;
  37. static struct workqueue_struct *isert_comp_wq;
  38. static struct kmem_cache *isert_cmd_cache;
  39. static void
  40. isert_qp_event_callback(struct ib_event *e, void *context)
  41. {
  42. struct isert_conn *isert_conn = (struct isert_conn *)context;
  43. pr_err("isert_qp_event_callback event: %d\n", e->event);
  44. switch (e->event) {
  45. case IB_EVENT_COMM_EST:
  46. rdma_notify(isert_conn->conn_cm_id, IB_EVENT_COMM_EST);
  47. break;
  48. case IB_EVENT_QP_LAST_WQE_REACHED:
  49. pr_warn("Reached TX IB_EVENT_QP_LAST_WQE_REACHED:\n");
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. static int
  56. isert_query_device(struct ib_device *ib_dev, struct ib_device_attr *devattr)
  57. {
  58. int ret;
  59. ret = ib_query_device(ib_dev, devattr);
  60. if (ret) {
  61. pr_err("ib_query_device() failed: %d\n", ret);
  62. return ret;
  63. }
  64. pr_debug("devattr->max_sge: %d\n", devattr->max_sge);
  65. pr_debug("devattr->max_sge_rd: %d\n", devattr->max_sge_rd);
  66. return 0;
  67. }
  68. static int
  69. isert_conn_setup_qp(struct isert_conn *isert_conn, struct rdma_cm_id *cma_id)
  70. {
  71. struct isert_device *device = isert_conn->conn_device;
  72. struct ib_qp_init_attr attr;
  73. struct ib_device_attr devattr;
  74. int ret, index, min_index = 0;
  75. memset(&devattr, 0, sizeof(struct ib_device_attr));
  76. ret = isert_query_device(cma_id->device, &devattr);
  77. if (ret)
  78. return ret;
  79. mutex_lock(&device_list_mutex);
  80. for (index = 0; index < device->cqs_used; index++)
  81. if (device->cq_active_qps[index] <
  82. device->cq_active_qps[min_index])
  83. min_index = index;
  84. device->cq_active_qps[min_index]++;
  85. pr_debug("isert_conn_setup_qp: Using min_index: %d\n", min_index);
  86. mutex_unlock(&device_list_mutex);
  87. memset(&attr, 0, sizeof(struct ib_qp_init_attr));
  88. attr.event_handler = isert_qp_event_callback;
  89. attr.qp_context = isert_conn;
  90. attr.send_cq = device->dev_tx_cq[min_index];
  91. attr.recv_cq = device->dev_rx_cq[min_index];
  92. attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
  93. attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS;
  94. /*
  95. * FIXME: Use devattr.max_sge - 2 for max_send_sge as
  96. * work-around for RDMA_READ..
  97. */
  98. attr.cap.max_send_sge = devattr.max_sge - 2;
  99. isert_conn->max_sge = attr.cap.max_send_sge;
  100. attr.cap.max_recv_sge = 1;
  101. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  102. attr.qp_type = IB_QPT_RC;
  103. pr_debug("isert_conn_setup_qp cma_id->device: %p\n",
  104. cma_id->device);
  105. pr_debug("isert_conn_setup_qp conn_pd->device: %p\n",
  106. isert_conn->conn_pd->device);
  107. ret = rdma_create_qp(cma_id, isert_conn->conn_pd, &attr);
  108. if (ret) {
  109. pr_err("rdma_create_qp failed for cma_id %d\n", ret);
  110. return ret;
  111. }
  112. isert_conn->conn_qp = cma_id->qp;
  113. pr_debug("rdma_create_qp() returned success >>>>>>>>>>>>>>>>>>>>>>>>>.\n");
  114. return 0;
  115. }
  116. static void
  117. isert_cq_event_callback(struct ib_event *e, void *context)
  118. {
  119. pr_debug("isert_cq_event_callback event: %d\n", e->event);
  120. }
  121. static int
  122. isert_alloc_rx_descriptors(struct isert_conn *isert_conn)
  123. {
  124. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  125. struct iser_rx_desc *rx_desc;
  126. struct ib_sge *rx_sg;
  127. u64 dma_addr;
  128. int i, j;
  129. isert_conn->conn_rx_descs = kzalloc(ISERT_QP_MAX_RECV_DTOS *
  130. sizeof(struct iser_rx_desc), GFP_KERNEL);
  131. if (!isert_conn->conn_rx_descs)
  132. goto fail;
  133. rx_desc = isert_conn->conn_rx_descs;
  134. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  135. dma_addr = ib_dma_map_single(ib_dev, (void *)rx_desc,
  136. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  137. if (ib_dma_mapping_error(ib_dev, dma_addr))
  138. goto dma_map_fail;
  139. rx_desc->dma_addr = dma_addr;
  140. rx_sg = &rx_desc->rx_sg;
  141. rx_sg->addr = rx_desc->dma_addr;
  142. rx_sg->length = ISER_RX_PAYLOAD_SIZE;
  143. rx_sg->lkey = isert_conn->conn_mr->lkey;
  144. }
  145. isert_conn->conn_rx_desc_head = 0;
  146. return 0;
  147. dma_map_fail:
  148. rx_desc = isert_conn->conn_rx_descs;
  149. for (j = 0; j < i; j++, rx_desc++) {
  150. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  151. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  152. }
  153. kfree(isert_conn->conn_rx_descs);
  154. isert_conn->conn_rx_descs = NULL;
  155. fail:
  156. return -ENOMEM;
  157. }
  158. static void
  159. isert_free_rx_descriptors(struct isert_conn *isert_conn)
  160. {
  161. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  162. struct iser_rx_desc *rx_desc;
  163. int i;
  164. if (!isert_conn->conn_rx_descs)
  165. return;
  166. rx_desc = isert_conn->conn_rx_descs;
  167. for (i = 0; i < ISERT_QP_MAX_RECV_DTOS; i++, rx_desc++) {
  168. ib_dma_unmap_single(ib_dev, rx_desc->dma_addr,
  169. ISER_RX_PAYLOAD_SIZE, DMA_FROM_DEVICE);
  170. }
  171. kfree(isert_conn->conn_rx_descs);
  172. isert_conn->conn_rx_descs = NULL;
  173. }
  174. static void isert_cq_tx_callback(struct ib_cq *, void *);
  175. static void isert_cq_rx_callback(struct ib_cq *, void *);
  176. static int
  177. isert_create_device_ib_res(struct isert_device *device)
  178. {
  179. struct ib_device *ib_dev = device->ib_device;
  180. struct isert_cq_desc *cq_desc;
  181. int ret = 0, i, j;
  182. device->cqs_used = min_t(int, num_online_cpus(),
  183. device->ib_device->num_comp_vectors);
  184. device->cqs_used = min(ISERT_MAX_CQ, device->cqs_used);
  185. pr_debug("Using %d CQs, device %s supports %d vectors\n",
  186. device->cqs_used, device->ib_device->name,
  187. device->ib_device->num_comp_vectors);
  188. device->cq_desc = kzalloc(sizeof(struct isert_cq_desc) *
  189. device->cqs_used, GFP_KERNEL);
  190. if (!device->cq_desc) {
  191. pr_err("Unable to allocate device->cq_desc\n");
  192. return -ENOMEM;
  193. }
  194. cq_desc = device->cq_desc;
  195. device->dev_pd = ib_alloc_pd(ib_dev);
  196. if (IS_ERR(device->dev_pd)) {
  197. ret = PTR_ERR(device->dev_pd);
  198. pr_err("ib_alloc_pd failed for dev_pd: %d\n", ret);
  199. goto out_cq_desc;
  200. }
  201. for (i = 0; i < device->cqs_used; i++) {
  202. cq_desc[i].device = device;
  203. cq_desc[i].cq_index = i;
  204. device->dev_rx_cq[i] = ib_create_cq(device->ib_device,
  205. isert_cq_rx_callback,
  206. isert_cq_event_callback,
  207. (void *)&cq_desc[i],
  208. ISER_MAX_RX_CQ_LEN, i);
  209. if (IS_ERR(device->dev_rx_cq[i]))
  210. goto out_cq;
  211. device->dev_tx_cq[i] = ib_create_cq(device->ib_device,
  212. isert_cq_tx_callback,
  213. isert_cq_event_callback,
  214. (void *)&cq_desc[i],
  215. ISER_MAX_TX_CQ_LEN, i);
  216. if (IS_ERR(device->dev_tx_cq[i]))
  217. goto out_cq;
  218. if (ib_req_notify_cq(device->dev_rx_cq[i], IB_CQ_NEXT_COMP))
  219. goto out_cq;
  220. if (ib_req_notify_cq(device->dev_tx_cq[i], IB_CQ_NEXT_COMP))
  221. goto out_cq;
  222. }
  223. device->dev_mr = ib_get_dma_mr(device->dev_pd, IB_ACCESS_LOCAL_WRITE);
  224. if (IS_ERR(device->dev_mr)) {
  225. ret = PTR_ERR(device->dev_mr);
  226. pr_err("ib_get_dma_mr failed for dev_mr: %d\n", ret);
  227. goto out_cq;
  228. }
  229. return 0;
  230. out_cq:
  231. for (j = 0; j < i; j++) {
  232. cq_desc = &device->cq_desc[j];
  233. if (device->dev_rx_cq[j]) {
  234. cancel_work_sync(&cq_desc->cq_rx_work);
  235. ib_destroy_cq(device->dev_rx_cq[j]);
  236. }
  237. if (device->dev_tx_cq[j]) {
  238. cancel_work_sync(&cq_desc->cq_tx_work);
  239. ib_destroy_cq(device->dev_tx_cq[j]);
  240. }
  241. }
  242. ib_dealloc_pd(device->dev_pd);
  243. out_cq_desc:
  244. kfree(device->cq_desc);
  245. return ret;
  246. }
  247. static void
  248. isert_free_device_ib_res(struct isert_device *device)
  249. {
  250. struct isert_cq_desc *cq_desc;
  251. int i;
  252. for (i = 0; i < device->cqs_used; i++) {
  253. cq_desc = &device->cq_desc[i];
  254. cancel_work_sync(&cq_desc->cq_rx_work);
  255. cancel_work_sync(&cq_desc->cq_tx_work);
  256. ib_destroy_cq(device->dev_rx_cq[i]);
  257. ib_destroy_cq(device->dev_tx_cq[i]);
  258. device->dev_rx_cq[i] = NULL;
  259. device->dev_tx_cq[i] = NULL;
  260. }
  261. ib_dereg_mr(device->dev_mr);
  262. ib_dealloc_pd(device->dev_pd);
  263. kfree(device->cq_desc);
  264. }
  265. static void
  266. isert_device_try_release(struct isert_device *device)
  267. {
  268. mutex_lock(&device_list_mutex);
  269. device->refcount--;
  270. if (!device->refcount) {
  271. isert_free_device_ib_res(device);
  272. list_del(&device->dev_node);
  273. kfree(device);
  274. }
  275. mutex_unlock(&device_list_mutex);
  276. }
  277. static struct isert_device *
  278. isert_device_find_by_ib_dev(struct rdma_cm_id *cma_id)
  279. {
  280. struct isert_device *device;
  281. int ret;
  282. mutex_lock(&device_list_mutex);
  283. list_for_each_entry(device, &device_list, dev_node) {
  284. if (device->ib_device->node_guid == cma_id->device->node_guid) {
  285. device->refcount++;
  286. mutex_unlock(&device_list_mutex);
  287. return device;
  288. }
  289. }
  290. device = kzalloc(sizeof(struct isert_device), GFP_KERNEL);
  291. if (!device) {
  292. mutex_unlock(&device_list_mutex);
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. INIT_LIST_HEAD(&device->dev_node);
  296. device->ib_device = cma_id->device;
  297. ret = isert_create_device_ib_res(device);
  298. if (ret) {
  299. kfree(device);
  300. mutex_unlock(&device_list_mutex);
  301. return ERR_PTR(ret);
  302. }
  303. device->refcount++;
  304. list_add_tail(&device->dev_node, &device_list);
  305. mutex_unlock(&device_list_mutex);
  306. return device;
  307. }
  308. static int
  309. isert_connect_request(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  310. {
  311. struct iscsi_np *np = cma_id->context;
  312. struct isert_np *isert_np = np->np_context;
  313. struct isert_conn *isert_conn;
  314. struct isert_device *device;
  315. struct ib_device *ib_dev = cma_id->device;
  316. int ret = 0;
  317. pr_debug("Entering isert_connect_request cma_id: %p, context: %p\n",
  318. cma_id, cma_id->context);
  319. isert_conn = kzalloc(sizeof(struct isert_conn), GFP_KERNEL);
  320. if (!isert_conn) {
  321. pr_err("Unable to allocate isert_conn\n");
  322. return -ENOMEM;
  323. }
  324. isert_conn->state = ISER_CONN_INIT;
  325. INIT_LIST_HEAD(&isert_conn->conn_accept_node);
  326. init_completion(&isert_conn->conn_login_comp);
  327. init_waitqueue_head(&isert_conn->conn_wait);
  328. init_waitqueue_head(&isert_conn->conn_wait_comp_err);
  329. kref_init(&isert_conn->conn_kref);
  330. kref_get(&isert_conn->conn_kref);
  331. cma_id->context = isert_conn;
  332. isert_conn->conn_cm_id = cma_id;
  333. isert_conn->responder_resources = event->param.conn.responder_resources;
  334. isert_conn->initiator_depth = event->param.conn.initiator_depth;
  335. pr_debug("Using responder_resources: %u initiator_depth: %u\n",
  336. isert_conn->responder_resources, isert_conn->initiator_depth);
  337. isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
  338. ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  339. if (!isert_conn->login_buf) {
  340. pr_err("Unable to allocate isert_conn->login_buf\n");
  341. ret = -ENOMEM;
  342. goto out;
  343. }
  344. isert_conn->login_req_buf = isert_conn->login_buf;
  345. isert_conn->login_rsp_buf = isert_conn->login_buf +
  346. ISCSI_DEF_MAX_RECV_SEG_LEN;
  347. pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
  348. isert_conn->login_buf, isert_conn->login_req_buf,
  349. isert_conn->login_rsp_buf);
  350. isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
  351. (void *)isert_conn->login_req_buf,
  352. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  353. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
  354. if (ret) {
  355. pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
  356. ret);
  357. isert_conn->login_req_dma = 0;
  358. goto out_login_buf;
  359. }
  360. isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
  361. (void *)isert_conn->login_rsp_buf,
  362. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  363. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
  364. if (ret) {
  365. pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
  366. ret);
  367. isert_conn->login_rsp_dma = 0;
  368. goto out_req_dma_map;
  369. }
  370. device = isert_device_find_by_ib_dev(cma_id);
  371. if (IS_ERR(device)) {
  372. ret = PTR_ERR(device);
  373. goto out_rsp_dma_map;
  374. }
  375. isert_conn->conn_device = device;
  376. isert_conn->conn_pd = device->dev_pd;
  377. isert_conn->conn_mr = device->dev_mr;
  378. ret = isert_conn_setup_qp(isert_conn, cma_id);
  379. if (ret)
  380. goto out_conn_dev;
  381. mutex_lock(&isert_np->np_accept_mutex);
  382. list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
  383. mutex_unlock(&isert_np->np_accept_mutex);
  384. pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
  385. wake_up(&isert_np->np_accept_wq);
  386. return 0;
  387. out_conn_dev:
  388. isert_device_try_release(device);
  389. out_rsp_dma_map:
  390. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  391. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  392. out_req_dma_map:
  393. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  394. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  395. out_login_buf:
  396. kfree(isert_conn->login_buf);
  397. out:
  398. kfree(isert_conn);
  399. return ret;
  400. }
  401. static void
  402. isert_connect_release(struct isert_conn *isert_conn)
  403. {
  404. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  405. struct isert_device *device = isert_conn->conn_device;
  406. int cq_index;
  407. pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  408. if (isert_conn->conn_qp) {
  409. cq_index = ((struct isert_cq_desc *)
  410. isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
  411. pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
  412. isert_conn->conn_device->cq_active_qps[cq_index]--;
  413. rdma_destroy_qp(isert_conn->conn_cm_id);
  414. }
  415. isert_free_rx_descriptors(isert_conn);
  416. rdma_destroy_id(isert_conn->conn_cm_id);
  417. if (isert_conn->login_buf) {
  418. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  419. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  420. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  421. ISCSI_DEF_MAX_RECV_SEG_LEN,
  422. DMA_FROM_DEVICE);
  423. kfree(isert_conn->login_buf);
  424. }
  425. kfree(isert_conn);
  426. if (device)
  427. isert_device_try_release(device);
  428. pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
  429. }
  430. static void
  431. isert_connected_handler(struct rdma_cm_id *cma_id)
  432. {
  433. return;
  434. }
  435. static void
  436. isert_release_conn_kref(struct kref *kref)
  437. {
  438. struct isert_conn *isert_conn = container_of(kref,
  439. struct isert_conn, conn_kref);
  440. pr_debug("Calling isert_connect_release for final kref %s/%d\n",
  441. current->comm, current->pid);
  442. isert_connect_release(isert_conn);
  443. }
  444. static void
  445. isert_put_conn(struct isert_conn *isert_conn)
  446. {
  447. kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
  448. }
  449. static void
  450. isert_disconnect_work(struct work_struct *work)
  451. {
  452. struct isert_conn *isert_conn = container_of(work,
  453. struct isert_conn, conn_logout_work);
  454. pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  455. isert_conn->state = ISER_CONN_DOWN;
  456. if (isert_conn->post_recv_buf_count == 0 &&
  457. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  458. pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
  459. wake_up(&isert_conn->conn_wait);
  460. }
  461. isert_put_conn(isert_conn);
  462. }
  463. static void
  464. isert_disconnected_handler(struct rdma_cm_id *cma_id)
  465. {
  466. struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
  467. INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
  468. schedule_work(&isert_conn->conn_logout_work);
  469. }
  470. static int
  471. isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  472. {
  473. int ret = 0;
  474. pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
  475. event->event, event->status, cma_id->context, cma_id);
  476. switch (event->event) {
  477. case RDMA_CM_EVENT_CONNECT_REQUEST:
  478. pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
  479. ret = isert_connect_request(cma_id, event);
  480. break;
  481. case RDMA_CM_EVENT_ESTABLISHED:
  482. pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
  483. isert_connected_handler(cma_id);
  484. break;
  485. case RDMA_CM_EVENT_DISCONNECTED:
  486. pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
  487. isert_disconnected_handler(cma_id);
  488. break;
  489. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  490. case RDMA_CM_EVENT_ADDR_CHANGE:
  491. break;
  492. case RDMA_CM_EVENT_CONNECT_ERROR:
  493. default:
  494. pr_err("Unknown RDMA CMA event: %d\n", event->event);
  495. break;
  496. }
  497. if (ret != 0) {
  498. pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
  499. event->event, ret);
  500. dump_stack();
  501. }
  502. return ret;
  503. }
  504. static int
  505. isert_post_recv(struct isert_conn *isert_conn, u32 count)
  506. {
  507. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  508. int i, ret;
  509. unsigned int rx_head = isert_conn->conn_rx_desc_head;
  510. struct iser_rx_desc *rx_desc;
  511. for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
  512. rx_desc = &isert_conn->conn_rx_descs[rx_head];
  513. rx_wr->wr_id = (unsigned long)rx_desc;
  514. rx_wr->sg_list = &rx_desc->rx_sg;
  515. rx_wr->num_sge = 1;
  516. rx_wr->next = rx_wr + 1;
  517. rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
  518. }
  519. rx_wr--;
  520. rx_wr->next = NULL; /* mark end of work requests list */
  521. isert_conn->post_recv_buf_count += count;
  522. ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
  523. &rx_wr_failed);
  524. if (ret) {
  525. pr_err("ib_post_recv() failed with ret: %d\n", ret);
  526. isert_conn->post_recv_buf_count -= count;
  527. } else {
  528. pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
  529. isert_conn->conn_rx_desc_head = rx_head;
  530. }
  531. return ret;
  532. }
  533. static int
  534. isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
  535. {
  536. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  537. struct ib_send_wr send_wr, *send_wr_failed;
  538. int ret;
  539. ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
  540. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  541. send_wr.next = NULL;
  542. send_wr.wr_id = (unsigned long)tx_desc;
  543. send_wr.sg_list = tx_desc->tx_sg;
  544. send_wr.num_sge = tx_desc->num_sge;
  545. send_wr.opcode = IB_WR_SEND;
  546. send_wr.send_flags = IB_SEND_SIGNALED;
  547. atomic_inc(&isert_conn->post_send_buf_count);
  548. ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
  549. if (ret) {
  550. pr_err("ib_post_send() failed, ret: %d\n", ret);
  551. atomic_dec(&isert_conn->post_send_buf_count);
  552. }
  553. return ret;
  554. }
  555. static void
  556. isert_create_send_desc(struct isert_conn *isert_conn,
  557. struct isert_cmd *isert_cmd,
  558. struct iser_tx_desc *tx_desc)
  559. {
  560. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  561. ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
  562. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  563. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  564. tx_desc->iser_header.flags = ISER_VER;
  565. tx_desc->num_sge = 1;
  566. tx_desc->isert_cmd = isert_cmd;
  567. if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
  568. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  569. pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
  570. }
  571. }
  572. static int
  573. isert_init_tx_hdrs(struct isert_conn *isert_conn,
  574. struct iser_tx_desc *tx_desc)
  575. {
  576. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  577. u64 dma_addr;
  578. dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
  579. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  580. if (ib_dma_mapping_error(ib_dev, dma_addr)) {
  581. pr_err("ib_dma_mapping_error() failed\n");
  582. return -ENOMEM;
  583. }
  584. tx_desc->dma_addr = dma_addr;
  585. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  586. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  587. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  588. pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
  589. " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
  590. tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
  591. return 0;
  592. }
  593. static void
  594. isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
  595. {
  596. isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
  597. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  598. send_wr->opcode = IB_WR_SEND;
  599. send_wr->send_flags = IB_SEND_SIGNALED;
  600. send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
  601. send_wr->num_sge = isert_cmd->tx_desc.num_sge;
  602. }
  603. static int
  604. isert_rdma_post_recvl(struct isert_conn *isert_conn)
  605. {
  606. struct ib_recv_wr rx_wr, *rx_wr_fail;
  607. struct ib_sge sge;
  608. int ret;
  609. memset(&sge, 0, sizeof(struct ib_sge));
  610. sge.addr = isert_conn->login_req_dma;
  611. sge.length = ISER_RX_LOGIN_SIZE;
  612. sge.lkey = isert_conn->conn_mr->lkey;
  613. pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
  614. sge.addr, sge.length, sge.lkey);
  615. memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
  616. rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
  617. rx_wr.sg_list = &sge;
  618. rx_wr.num_sge = 1;
  619. isert_conn->post_recv_buf_count++;
  620. ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
  621. if (ret) {
  622. pr_err("ib_post_recv() failed: %d\n", ret);
  623. isert_conn->post_recv_buf_count--;
  624. }
  625. pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
  626. return ret;
  627. }
  628. static int
  629. isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
  630. u32 length)
  631. {
  632. struct isert_conn *isert_conn = conn->context;
  633. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  634. struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
  635. int ret;
  636. isert_create_send_desc(isert_conn, NULL, tx_desc);
  637. memcpy(&tx_desc->iscsi_header, &login->rsp[0],
  638. sizeof(struct iscsi_hdr));
  639. isert_init_tx_hdrs(isert_conn, tx_desc);
  640. if (length > 0) {
  641. struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
  642. ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
  643. length, DMA_TO_DEVICE);
  644. memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
  645. ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
  646. length, DMA_TO_DEVICE);
  647. tx_dsg->addr = isert_conn->login_rsp_dma;
  648. tx_dsg->length = length;
  649. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  650. tx_desc->num_sge = 2;
  651. }
  652. if (!login->login_failed) {
  653. if (login->login_complete) {
  654. ret = isert_alloc_rx_descriptors(isert_conn);
  655. if (ret)
  656. return ret;
  657. ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
  658. if (ret)
  659. return ret;
  660. isert_conn->state = ISER_CONN_UP;
  661. goto post_send;
  662. }
  663. ret = isert_rdma_post_recvl(isert_conn);
  664. if (ret)
  665. return ret;
  666. }
  667. post_send:
  668. ret = isert_post_send(isert_conn, tx_desc);
  669. if (ret)
  670. return ret;
  671. return 0;
  672. }
  673. static void
  674. isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
  675. struct isert_conn *isert_conn)
  676. {
  677. struct iscsi_conn *conn = isert_conn->conn;
  678. struct iscsi_login *login = conn->conn_login;
  679. int size;
  680. if (!login) {
  681. pr_err("conn->conn_login is NULL\n");
  682. dump_stack();
  683. return;
  684. }
  685. if (login->first_request) {
  686. struct iscsi_login_req *login_req =
  687. (struct iscsi_login_req *)&rx_desc->iscsi_header;
  688. /*
  689. * Setup the initial iscsi_login values from the leading
  690. * login request PDU.
  691. */
  692. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  693. login->current_stage =
  694. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  695. >> 2;
  696. login->version_min = login_req->min_version;
  697. login->version_max = login_req->max_version;
  698. memcpy(login->isid, login_req->isid, 6);
  699. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  700. login->init_task_tag = login_req->itt;
  701. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  702. login->cid = be16_to_cpu(login_req->cid);
  703. login->tsih = be16_to_cpu(login_req->tsih);
  704. }
  705. memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
  706. size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
  707. pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
  708. size, rx_buflen, MAX_KEY_VALUE_PAIRS);
  709. memcpy(login->req_buf, &rx_desc->data[0], size);
  710. complete(&isert_conn->conn_login_comp);
  711. }
  712. static void
  713. isert_release_cmd(struct iscsi_cmd *cmd)
  714. {
  715. struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
  716. iscsi_cmd);
  717. pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
  718. kfree(cmd->buf_ptr);
  719. kfree(cmd->tmr_req);
  720. kmem_cache_free(isert_cmd_cache, isert_cmd);
  721. }
  722. static struct iscsi_cmd
  723. *isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
  724. {
  725. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  726. struct isert_cmd *isert_cmd;
  727. isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
  728. if (!isert_cmd) {
  729. pr_err("Unable to allocate isert_cmd\n");
  730. return NULL;
  731. }
  732. isert_cmd->conn = isert_conn;
  733. isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
  734. return &isert_cmd->iscsi_cmd;
  735. }
  736. static int
  737. isert_handle_scsi_cmd(struct isert_conn *isert_conn,
  738. struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
  739. unsigned char *buf)
  740. {
  741. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  742. struct iscsi_conn *conn = isert_conn->conn;
  743. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
  744. struct scatterlist *sg;
  745. int imm_data, imm_data_len, unsol_data, sg_nents, rc;
  746. bool dump_payload = false;
  747. rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
  748. if (rc < 0)
  749. return rc;
  750. imm_data = cmd->immediate_data;
  751. imm_data_len = cmd->first_burst_len;
  752. unsol_data = cmd->unsolicited_data;
  753. rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
  754. if (rc < 0) {
  755. return 0;
  756. } else if (rc > 0) {
  757. dump_payload = true;
  758. goto sequence_cmd;
  759. }
  760. if (!imm_data)
  761. return 0;
  762. sg = &cmd->se_cmd.t_data_sg[0];
  763. sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
  764. pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
  765. sg, sg_nents, &rx_desc->data[0], imm_data_len);
  766. sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
  767. cmd->write_data_done += imm_data_len;
  768. if (cmd->write_data_done == cmd->se_cmd.data_length) {
  769. spin_lock_bh(&cmd->istate_lock);
  770. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  771. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  772. spin_unlock_bh(&cmd->istate_lock);
  773. }
  774. sequence_cmd:
  775. rc = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
  776. if (!rc && dump_payload == false && unsol_data)
  777. iscsit_set_unsoliticed_dataout(cmd);
  778. if (rc == CMDSN_ERROR_CANNOT_RECOVER)
  779. return iscsit_add_reject_from_cmd(
  780. ISCSI_REASON_PROTOCOL_ERROR,
  781. 1, 0, (unsigned char *)hdr, cmd);
  782. return 0;
  783. }
  784. static int
  785. isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
  786. struct iser_rx_desc *rx_desc, unsigned char *buf)
  787. {
  788. struct scatterlist *sg_start;
  789. struct iscsi_conn *conn = isert_conn->conn;
  790. struct iscsi_cmd *cmd = NULL;
  791. struct iscsi_data *hdr = (struct iscsi_data *)buf;
  792. u32 unsol_data_len = ntoh24(hdr->dlength);
  793. int rc, sg_nents, sg_off, page_off;
  794. rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
  795. if (rc < 0)
  796. return rc;
  797. else if (!cmd)
  798. return 0;
  799. /*
  800. * FIXME: Unexpected unsolicited_data out
  801. */
  802. if (!cmd->unsolicited_data) {
  803. pr_err("Received unexpected solicited data payload\n");
  804. dump_stack();
  805. return -1;
  806. }
  807. pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
  808. unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
  809. sg_off = cmd->write_data_done / PAGE_SIZE;
  810. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  811. sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
  812. page_off = cmd->write_data_done % PAGE_SIZE;
  813. /*
  814. * FIXME: Non page-aligned unsolicited_data out
  815. */
  816. if (page_off) {
  817. pr_err("Received unexpected non-page aligned data payload\n");
  818. dump_stack();
  819. return -1;
  820. }
  821. pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
  822. sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
  823. sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
  824. unsol_data_len);
  825. rc = iscsit_check_dataout_payload(cmd, hdr, false);
  826. if (rc < 0)
  827. return rc;
  828. return 0;
  829. }
  830. static int
  831. isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
  832. uint32_t read_stag, uint64_t read_va,
  833. uint32_t write_stag, uint64_t write_va)
  834. {
  835. struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
  836. struct iscsi_conn *conn = isert_conn->conn;
  837. struct iscsi_cmd *cmd;
  838. struct isert_cmd *isert_cmd;
  839. int ret = -EINVAL;
  840. u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
  841. switch (opcode) {
  842. case ISCSI_OP_SCSI_CMD:
  843. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  844. if (!cmd)
  845. break;
  846. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  847. isert_cmd->read_stag = read_stag;
  848. isert_cmd->read_va = read_va;
  849. isert_cmd->write_stag = write_stag;
  850. isert_cmd->write_va = write_va;
  851. ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
  852. rx_desc, (unsigned char *)hdr);
  853. break;
  854. case ISCSI_OP_NOOP_OUT:
  855. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  856. if (!cmd)
  857. break;
  858. ret = iscsit_handle_nop_out(conn, cmd, (unsigned char *)hdr);
  859. break;
  860. case ISCSI_OP_SCSI_DATA_OUT:
  861. ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
  862. (unsigned char *)hdr);
  863. break;
  864. case ISCSI_OP_SCSI_TMFUNC:
  865. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  866. if (!cmd)
  867. break;
  868. ret = iscsit_handle_task_mgt_cmd(conn, cmd,
  869. (unsigned char *)hdr);
  870. break;
  871. case ISCSI_OP_LOGOUT:
  872. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  873. if (!cmd)
  874. break;
  875. ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
  876. if (ret > 0)
  877. wait_for_completion_timeout(&conn->conn_logout_comp,
  878. SECONDS_FOR_LOGOUT_COMP *
  879. HZ);
  880. break;
  881. default:
  882. pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
  883. dump_stack();
  884. break;
  885. }
  886. return ret;
  887. }
  888. static void
  889. isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
  890. {
  891. struct iser_hdr *iser_hdr = &rx_desc->iser_header;
  892. uint64_t read_va = 0, write_va = 0;
  893. uint32_t read_stag = 0, write_stag = 0;
  894. int rc;
  895. switch (iser_hdr->flags & 0xF0) {
  896. case ISCSI_CTRL:
  897. if (iser_hdr->flags & ISER_RSV) {
  898. read_stag = be32_to_cpu(iser_hdr->read_stag);
  899. read_va = be64_to_cpu(iser_hdr->read_va);
  900. pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
  901. read_stag, (unsigned long long)read_va);
  902. }
  903. if (iser_hdr->flags & ISER_WSV) {
  904. write_stag = be32_to_cpu(iser_hdr->write_stag);
  905. write_va = be64_to_cpu(iser_hdr->write_va);
  906. pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
  907. write_stag, (unsigned long long)write_va);
  908. }
  909. pr_debug("ISER ISCSI_CTRL PDU\n");
  910. break;
  911. case ISER_HELLO:
  912. pr_err("iSER Hello message\n");
  913. break;
  914. default:
  915. pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
  916. break;
  917. }
  918. rc = isert_rx_opcode(isert_conn, rx_desc,
  919. read_stag, read_va, write_stag, write_va);
  920. }
  921. static void
  922. isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
  923. unsigned long xfer_len)
  924. {
  925. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  926. struct iscsi_hdr *hdr;
  927. u64 rx_dma;
  928. int rx_buflen, outstanding;
  929. if ((char *)desc == isert_conn->login_req_buf) {
  930. rx_dma = isert_conn->login_req_dma;
  931. rx_buflen = ISER_RX_LOGIN_SIZE;
  932. pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  933. rx_dma, rx_buflen);
  934. } else {
  935. rx_dma = desc->dma_addr;
  936. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  937. pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  938. rx_dma, rx_buflen);
  939. }
  940. ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
  941. hdr = &desc->iscsi_header;
  942. pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
  943. hdr->opcode, hdr->itt, hdr->flags,
  944. (int)(xfer_len - ISER_HEADERS_LEN));
  945. if ((char *)desc == isert_conn->login_req_buf)
  946. isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
  947. isert_conn);
  948. else
  949. isert_rx_do_work(desc, isert_conn);
  950. ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
  951. DMA_FROM_DEVICE);
  952. isert_conn->post_recv_buf_count--;
  953. pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
  954. isert_conn->post_recv_buf_count);
  955. if ((char *)desc == isert_conn->login_req_buf)
  956. return;
  957. outstanding = isert_conn->post_recv_buf_count;
  958. if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
  959. int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
  960. ISERT_MIN_POSTED_RX);
  961. err = isert_post_recv(isert_conn, count);
  962. if (err) {
  963. pr_err("isert_post_recv() count: %d failed, %d\n",
  964. count, err);
  965. }
  966. }
  967. }
  968. static void
  969. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  970. {
  971. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  972. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  973. pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
  974. if (wr->sge) {
  975. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  976. wr->sge = NULL;
  977. }
  978. kfree(wr->send_wr);
  979. wr->send_wr = NULL;
  980. kfree(isert_cmd->ib_sge);
  981. isert_cmd->ib_sge = NULL;
  982. }
  983. static void
  984. isert_put_cmd(struct isert_cmd *isert_cmd)
  985. {
  986. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  987. struct isert_conn *isert_conn = isert_cmd->conn;
  988. struct iscsi_conn *conn;
  989. pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
  990. switch (cmd->iscsi_opcode) {
  991. case ISCSI_OP_SCSI_CMD:
  992. conn = isert_conn->conn;
  993. spin_lock_bh(&conn->cmd_lock);
  994. if (!list_empty(&cmd->i_conn_node))
  995. list_del(&cmd->i_conn_node);
  996. spin_unlock_bh(&conn->cmd_lock);
  997. if (cmd->data_direction == DMA_TO_DEVICE)
  998. iscsit_stop_dataout_timer(cmd);
  999. isert_unmap_cmd(isert_cmd, isert_conn);
  1000. /*
  1001. * Fall-through
  1002. */
  1003. case ISCSI_OP_SCSI_TMFUNC:
  1004. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1005. break;
  1006. case ISCSI_OP_REJECT:
  1007. case ISCSI_OP_NOOP_OUT:
  1008. conn = isert_conn->conn;
  1009. spin_lock_bh(&conn->cmd_lock);
  1010. if (!list_empty(&cmd->i_conn_node))
  1011. list_del(&cmd->i_conn_node);
  1012. spin_unlock_bh(&conn->cmd_lock);
  1013. /*
  1014. * Handle special case for REJECT when iscsi_add_reject*() has
  1015. * overwritten the original iscsi_opcode assignment, and the
  1016. * associated cmd->se_cmd needs to be released.
  1017. */
  1018. if (cmd->se_cmd.se_tfo != NULL) {
  1019. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1020. break;
  1021. }
  1022. /*
  1023. * Fall-through
  1024. */
  1025. default:
  1026. isert_release_cmd(cmd);
  1027. break;
  1028. }
  1029. }
  1030. static void
  1031. isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
  1032. {
  1033. if (tx_desc->dma_addr != 0) {
  1034. pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
  1035. ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
  1036. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  1037. tx_desc->dma_addr = 0;
  1038. }
  1039. }
  1040. static void
  1041. isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
  1042. struct ib_device *ib_dev)
  1043. {
  1044. if (isert_cmd->sense_buf_dma != 0) {
  1045. pr_debug("Calling ib_dma_unmap_single for isert_cmd->sense_buf_dma\n");
  1046. ib_dma_unmap_single(ib_dev, isert_cmd->sense_buf_dma,
  1047. isert_cmd->sense_buf_len, DMA_TO_DEVICE);
  1048. isert_cmd->sense_buf_dma = 0;
  1049. }
  1050. isert_unmap_tx_desc(tx_desc, ib_dev);
  1051. isert_put_cmd(isert_cmd);
  1052. }
  1053. static void
  1054. isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
  1055. struct isert_cmd *isert_cmd)
  1056. {
  1057. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1058. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1059. struct se_cmd *se_cmd = &cmd->se_cmd;
  1060. struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
  1061. iscsit_stop_dataout_timer(cmd);
  1062. if (wr->sge) {
  1063. pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
  1064. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  1065. wr->sge = NULL;
  1066. }
  1067. if (isert_cmd->ib_sge) {
  1068. pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
  1069. kfree(isert_cmd->ib_sge);
  1070. isert_cmd->ib_sge = NULL;
  1071. }
  1072. cmd->write_data_done = se_cmd->data_length;
  1073. pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
  1074. spin_lock_bh(&cmd->istate_lock);
  1075. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1076. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1077. spin_unlock_bh(&cmd->istate_lock);
  1078. target_execute_cmd(se_cmd);
  1079. }
  1080. static void
  1081. isert_do_control_comp(struct work_struct *work)
  1082. {
  1083. struct isert_cmd *isert_cmd = container_of(work,
  1084. struct isert_cmd, comp_work);
  1085. struct isert_conn *isert_conn = isert_cmd->conn;
  1086. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1087. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1088. switch (cmd->i_state) {
  1089. case ISTATE_SEND_TASKMGTRSP:
  1090. pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
  1091. atomic_dec(&isert_conn->post_send_buf_count);
  1092. iscsit_tmr_post_handler(cmd, cmd->conn);
  1093. cmd->i_state = ISTATE_SENT_STATUS;
  1094. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1095. break;
  1096. case ISTATE_SEND_REJECT:
  1097. pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
  1098. atomic_dec(&isert_conn->post_send_buf_count);
  1099. cmd->i_state = ISTATE_SENT_STATUS;
  1100. complete(&cmd->reject_comp);
  1101. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1102. case ISTATE_SEND_LOGOUTRSP:
  1103. pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
  1104. /*
  1105. * Call atomic_dec(&isert_conn->post_send_buf_count)
  1106. * from isert_free_conn()
  1107. */
  1108. isert_conn->logout_posted = true;
  1109. iscsit_logout_post_handler(cmd, cmd->conn);
  1110. break;
  1111. default:
  1112. pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
  1113. dump_stack();
  1114. break;
  1115. }
  1116. }
  1117. static void
  1118. isert_response_completion(struct iser_tx_desc *tx_desc,
  1119. struct isert_cmd *isert_cmd,
  1120. struct isert_conn *isert_conn,
  1121. struct ib_device *ib_dev)
  1122. {
  1123. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1124. if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
  1125. cmd->i_state == ISTATE_SEND_LOGOUTRSP) {
  1126. isert_unmap_tx_desc(tx_desc, ib_dev);
  1127. INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
  1128. queue_work(isert_comp_wq, &isert_cmd->comp_work);
  1129. return;
  1130. }
  1131. atomic_dec(&isert_conn->post_send_buf_count);
  1132. cmd->i_state = ISTATE_SENT_STATUS;
  1133. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1134. }
  1135. static void
  1136. isert_send_completion(struct iser_tx_desc *tx_desc,
  1137. struct isert_conn *isert_conn)
  1138. {
  1139. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1140. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1141. struct isert_rdma_wr *wr;
  1142. if (!isert_cmd) {
  1143. atomic_dec(&isert_conn->post_send_buf_count);
  1144. isert_unmap_tx_desc(tx_desc, ib_dev);
  1145. return;
  1146. }
  1147. wr = &isert_cmd->rdma_wr;
  1148. switch (wr->iser_ib_op) {
  1149. case ISER_IB_RECV:
  1150. pr_err("isert_send_completion: Got ISER_IB_RECV\n");
  1151. dump_stack();
  1152. break;
  1153. case ISER_IB_SEND:
  1154. pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
  1155. isert_response_completion(tx_desc, isert_cmd,
  1156. isert_conn, ib_dev);
  1157. break;
  1158. case ISER_IB_RDMA_WRITE:
  1159. pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
  1160. dump_stack();
  1161. break;
  1162. case ISER_IB_RDMA_READ:
  1163. pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
  1164. atomic_dec(&isert_conn->post_send_buf_count);
  1165. isert_completion_rdma_read(tx_desc, isert_cmd);
  1166. break;
  1167. default:
  1168. pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
  1169. dump_stack();
  1170. break;
  1171. }
  1172. }
  1173. static void
  1174. isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
  1175. {
  1176. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1177. if (tx_desc) {
  1178. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1179. if (!isert_cmd)
  1180. isert_unmap_tx_desc(tx_desc, ib_dev);
  1181. else
  1182. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1183. }
  1184. if (isert_conn->post_recv_buf_count == 0 &&
  1185. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  1186. pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  1187. pr_debug("Calling wake_up from isert_cq_comp_err\n");
  1188. isert_conn->state = ISER_CONN_TERMINATING;
  1189. wake_up(&isert_conn->conn_wait_comp_err);
  1190. }
  1191. }
  1192. static void
  1193. isert_cq_tx_work(struct work_struct *work)
  1194. {
  1195. struct isert_cq_desc *cq_desc = container_of(work,
  1196. struct isert_cq_desc, cq_tx_work);
  1197. struct isert_device *device = cq_desc->device;
  1198. int cq_index = cq_desc->cq_index;
  1199. struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
  1200. struct isert_conn *isert_conn;
  1201. struct iser_tx_desc *tx_desc;
  1202. struct ib_wc wc;
  1203. while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
  1204. tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
  1205. isert_conn = wc.qp->qp_context;
  1206. if (wc.status == IB_WC_SUCCESS) {
  1207. isert_send_completion(tx_desc, isert_conn);
  1208. } else {
  1209. pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1210. pr_debug("TX wc.status: 0x%08x\n", wc.status);
  1211. atomic_dec(&isert_conn->post_send_buf_count);
  1212. isert_cq_comp_err(tx_desc, isert_conn);
  1213. }
  1214. }
  1215. ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
  1216. }
  1217. static void
  1218. isert_cq_tx_callback(struct ib_cq *cq, void *context)
  1219. {
  1220. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1221. INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
  1222. queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
  1223. }
  1224. static void
  1225. isert_cq_rx_work(struct work_struct *work)
  1226. {
  1227. struct isert_cq_desc *cq_desc = container_of(work,
  1228. struct isert_cq_desc, cq_rx_work);
  1229. struct isert_device *device = cq_desc->device;
  1230. int cq_index = cq_desc->cq_index;
  1231. struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
  1232. struct isert_conn *isert_conn;
  1233. struct iser_rx_desc *rx_desc;
  1234. struct ib_wc wc;
  1235. unsigned long xfer_len;
  1236. while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
  1237. rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
  1238. isert_conn = wc.qp->qp_context;
  1239. if (wc.status == IB_WC_SUCCESS) {
  1240. xfer_len = (unsigned long)wc.byte_len;
  1241. isert_rx_completion(rx_desc, isert_conn, xfer_len);
  1242. } else {
  1243. pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1244. if (wc.status != IB_WC_WR_FLUSH_ERR)
  1245. pr_debug("RX wc.status: 0x%08x\n", wc.status);
  1246. isert_conn->post_recv_buf_count--;
  1247. isert_cq_comp_err(NULL, isert_conn);
  1248. }
  1249. }
  1250. ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
  1251. }
  1252. static void
  1253. isert_cq_rx_callback(struct ib_cq *cq, void *context)
  1254. {
  1255. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1256. INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
  1257. queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
  1258. }
  1259. static int
  1260. isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
  1261. {
  1262. struct ib_send_wr *wr_failed;
  1263. int ret;
  1264. atomic_inc(&isert_conn->post_send_buf_count);
  1265. ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
  1266. &wr_failed);
  1267. if (ret) {
  1268. pr_err("ib_post_send failed with %d\n", ret);
  1269. atomic_dec(&isert_conn->post_send_buf_count);
  1270. return ret;
  1271. }
  1272. return ret;
  1273. }
  1274. static int
  1275. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1276. {
  1277. struct isert_cmd *isert_cmd = container_of(cmd,
  1278. struct isert_cmd, iscsi_cmd);
  1279. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1280. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1281. struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
  1282. &isert_cmd->tx_desc.iscsi_header;
  1283. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1284. iscsit_build_rsp_pdu(cmd, conn, true, hdr);
  1285. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1286. /*
  1287. * Attach SENSE DATA payload to iSCSI Response PDU
  1288. */
  1289. if (cmd->se_cmd.sense_buffer &&
  1290. ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1291. (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  1292. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1293. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1294. u32 padding, sense_len;
  1295. put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
  1296. cmd->sense_buffer);
  1297. cmd->se_cmd.scsi_sense_length += sizeof(__be16);
  1298. padding = -(cmd->se_cmd.scsi_sense_length) & 3;
  1299. hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
  1300. sense_len = cmd->se_cmd.scsi_sense_length + padding;
  1301. isert_cmd->sense_buf_dma = ib_dma_map_single(ib_dev,
  1302. (void *)cmd->sense_buffer, sense_len,
  1303. DMA_TO_DEVICE);
  1304. isert_cmd->sense_buf_len = sense_len;
  1305. tx_dsg->addr = isert_cmd->sense_buf_dma;
  1306. tx_dsg->length = sense_len;
  1307. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1308. isert_cmd->tx_desc.num_sge = 2;
  1309. }
  1310. isert_init_send_wr(isert_cmd, send_wr);
  1311. pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1312. return isert_post_response(isert_conn, isert_cmd);
  1313. }
  1314. static int
  1315. isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
  1316. bool nopout_response)
  1317. {
  1318. struct isert_cmd *isert_cmd = container_of(cmd,
  1319. struct isert_cmd, iscsi_cmd);
  1320. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1321. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1322. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1323. iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
  1324. &isert_cmd->tx_desc.iscsi_header,
  1325. nopout_response);
  1326. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1327. isert_init_send_wr(isert_cmd, send_wr);
  1328. pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1329. return isert_post_response(isert_conn, isert_cmd);
  1330. }
  1331. static int
  1332. isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1333. {
  1334. struct isert_cmd *isert_cmd = container_of(cmd,
  1335. struct isert_cmd, iscsi_cmd);
  1336. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1337. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1338. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1339. iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
  1340. &isert_cmd->tx_desc.iscsi_header);
  1341. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1342. isert_init_send_wr(isert_cmd, send_wr);
  1343. pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1344. return isert_post_response(isert_conn, isert_cmd);
  1345. }
  1346. static int
  1347. isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1348. {
  1349. struct isert_cmd *isert_cmd = container_of(cmd,
  1350. struct isert_cmd, iscsi_cmd);
  1351. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1352. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1353. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1354. iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
  1355. &isert_cmd->tx_desc.iscsi_header);
  1356. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1357. isert_init_send_wr(isert_cmd, send_wr);
  1358. pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1359. return isert_post_response(isert_conn, isert_cmd);
  1360. }
  1361. static int
  1362. isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1363. {
  1364. struct isert_cmd *isert_cmd = container_of(cmd,
  1365. struct isert_cmd, iscsi_cmd);
  1366. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1367. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1368. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1369. iscsit_build_reject(cmd, conn, (struct iscsi_reject *)
  1370. &isert_cmd->tx_desc.iscsi_header);
  1371. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1372. isert_init_send_wr(isert_cmd, send_wr);
  1373. pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1374. return isert_post_response(isert_conn, isert_cmd);
  1375. }
  1376. static int
  1377. isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1378. struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
  1379. u32 data_left, u32 offset)
  1380. {
  1381. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1382. struct scatterlist *sg_start, *tmp_sg;
  1383. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1384. u32 sg_off, page_off;
  1385. int i = 0, sg_nents;
  1386. sg_off = offset / PAGE_SIZE;
  1387. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1388. sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
  1389. page_off = offset % PAGE_SIZE;
  1390. send_wr->sg_list = ib_sge;
  1391. send_wr->num_sge = sg_nents;
  1392. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  1393. /*
  1394. * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
  1395. */
  1396. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1397. pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
  1398. (unsigned long long)tmp_sg->dma_address,
  1399. tmp_sg->length, page_off);
  1400. ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
  1401. ib_sge->length = min_t(u32, data_left,
  1402. ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
  1403. ib_sge->lkey = isert_conn->conn_mr->lkey;
  1404. pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
  1405. ib_sge->addr, ib_sge->length);
  1406. page_off = 0;
  1407. data_left -= ib_sge->length;
  1408. ib_sge++;
  1409. pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
  1410. }
  1411. pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
  1412. send_wr->sg_list, send_wr->num_sge);
  1413. return sg_nents;
  1414. }
  1415. static int
  1416. isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1417. {
  1418. struct se_cmd *se_cmd = &cmd->se_cmd;
  1419. struct isert_cmd *isert_cmd = container_of(cmd,
  1420. struct isert_cmd, iscsi_cmd);
  1421. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1422. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1423. struct ib_send_wr *wr_failed, *send_wr;
  1424. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1425. struct ib_sge *ib_sge;
  1426. struct scatterlist *sg;
  1427. u32 offset = 0, data_len, data_left, rdma_write_max;
  1428. int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
  1429. pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
  1430. sg = &se_cmd->t_data_sg[0];
  1431. sg_nents = se_cmd->t_data_nents;
  1432. count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1433. if (unlikely(!count)) {
  1434. pr_err("Unable to map put_datain SGs\n");
  1435. return -EINVAL;
  1436. }
  1437. wr->sge = sg;
  1438. wr->num_sge = sg_nents;
  1439. pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
  1440. count, sg, sg_nents);
  1441. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1442. if (!ib_sge) {
  1443. pr_warn("Unable to allocate datain ib_sge\n");
  1444. ret = -ENOMEM;
  1445. goto unmap_sg;
  1446. }
  1447. isert_cmd->ib_sge = ib_sge;
  1448. pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
  1449. ib_sge, se_cmd->t_data_nents);
  1450. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1451. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1452. GFP_KERNEL);
  1453. if (!wr->send_wr) {
  1454. pr_err("Unable to allocate wr->send_wr\n");
  1455. ret = -ENOMEM;
  1456. goto unmap_sg;
  1457. }
  1458. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1459. wr->send_wr, wr->send_wr_num);
  1460. iscsit_increment_maxcmdsn(cmd, conn->sess);
  1461. cmd->stat_sn = conn->stat_sn++;
  1462. wr->isert_cmd = isert_cmd;
  1463. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1464. data_left = se_cmd->data_length;
  1465. for (i = 0; i < wr->send_wr_num; i++) {
  1466. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1467. data_len = min(data_left, rdma_write_max);
  1468. send_wr->opcode = IB_WR_RDMA_WRITE;
  1469. send_wr->send_flags = 0;
  1470. send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
  1471. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  1472. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1473. send_wr, data_len, offset);
  1474. ib_sge += ib_sge_cnt;
  1475. if (i + 1 == wr->send_wr_num)
  1476. send_wr->next = &isert_cmd->tx_desc.send_wr;
  1477. else
  1478. send_wr->next = &wr->send_wr[i + 1];
  1479. offset += data_len;
  1480. data_left -= data_len;
  1481. }
  1482. /*
  1483. * Build isert_conn->tx_desc for iSCSI response PDU and attach
  1484. */
  1485. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1486. iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
  1487. &isert_cmd->tx_desc.iscsi_header);
  1488. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1489. isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
  1490. atomic_inc(&isert_conn->post_send_buf_count);
  1491. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1492. if (rc) {
  1493. pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
  1494. atomic_dec(&isert_conn->post_send_buf_count);
  1495. }
  1496. pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
  1497. return 1;
  1498. unmap_sg:
  1499. ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1500. return ret;
  1501. }
  1502. static int
  1503. isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
  1504. {
  1505. struct se_cmd *se_cmd = &cmd->se_cmd;
  1506. struct isert_cmd *isert_cmd = container_of(cmd,
  1507. struct isert_cmd, iscsi_cmd);
  1508. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1509. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1510. struct ib_send_wr *wr_failed, *send_wr;
  1511. struct ib_sge *ib_sge;
  1512. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1513. struct scatterlist *sg_start;
  1514. u32 sg_off, sg_nents, page_off, va_offset = 0;
  1515. u32 offset = 0, data_len, data_left, rdma_write_max;
  1516. int rc, ret = 0, count, i, ib_sge_cnt;
  1517. pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
  1518. se_cmd->data_length, cmd->write_data_done);
  1519. sg_off = cmd->write_data_done / PAGE_SIZE;
  1520. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1521. page_off = cmd->write_data_done % PAGE_SIZE;
  1522. pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
  1523. sg_off, sg_start, page_off);
  1524. data_left = se_cmd->data_length - cmd->write_data_done;
  1525. sg_nents = se_cmd->t_data_nents - sg_off;
  1526. pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
  1527. data_left, sg_nents);
  1528. count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1529. if (unlikely(!count)) {
  1530. pr_err("Unable to map get_dataout SGs\n");
  1531. return -EINVAL;
  1532. }
  1533. wr->sge = sg_start;
  1534. wr->num_sge = sg_nents;
  1535. pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
  1536. count, sg_start, sg_nents);
  1537. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1538. if (!ib_sge) {
  1539. pr_warn("Unable to allocate dataout ib_sge\n");
  1540. ret = -ENOMEM;
  1541. goto unmap_sg;
  1542. }
  1543. isert_cmd->ib_sge = ib_sge;
  1544. pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
  1545. ib_sge, sg_nents);
  1546. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1547. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1548. GFP_KERNEL);
  1549. if (!wr->send_wr) {
  1550. pr_debug("Unable to allocate wr->send_wr\n");
  1551. ret = -ENOMEM;
  1552. goto unmap_sg;
  1553. }
  1554. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1555. wr->send_wr, wr->send_wr_num);
  1556. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1557. wr->iser_ib_op = ISER_IB_RDMA_READ;
  1558. wr->isert_cmd = isert_cmd;
  1559. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1560. offset = cmd->write_data_done;
  1561. for (i = 0; i < wr->send_wr_num; i++) {
  1562. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1563. data_len = min(data_left, rdma_write_max);
  1564. send_wr->opcode = IB_WR_RDMA_READ;
  1565. send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
  1566. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  1567. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1568. send_wr, data_len, offset);
  1569. ib_sge += ib_sge_cnt;
  1570. if (i + 1 == wr->send_wr_num)
  1571. send_wr->send_flags = IB_SEND_SIGNALED;
  1572. else
  1573. send_wr->next = &wr->send_wr[i + 1];
  1574. offset += data_len;
  1575. va_offset += data_len;
  1576. data_left -= data_len;
  1577. }
  1578. atomic_inc(&isert_conn->post_send_buf_count);
  1579. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1580. if (rc) {
  1581. pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
  1582. atomic_dec(&isert_conn->post_send_buf_count);
  1583. }
  1584. pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
  1585. return 0;
  1586. unmap_sg:
  1587. ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1588. return ret;
  1589. }
  1590. static int
  1591. isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1592. {
  1593. int ret;
  1594. switch (state) {
  1595. case ISTATE_SEND_NOPIN_WANT_RESPONSE:
  1596. ret = isert_put_nopin(cmd, conn, false);
  1597. break;
  1598. default:
  1599. pr_err("Unknown immediate state: 0x%02x\n", state);
  1600. ret = -EINVAL;
  1601. break;
  1602. }
  1603. return ret;
  1604. }
  1605. static int
  1606. isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1607. {
  1608. int ret;
  1609. switch (state) {
  1610. case ISTATE_SEND_LOGOUTRSP:
  1611. ret = isert_put_logout_rsp(cmd, conn);
  1612. if (!ret) {
  1613. pr_debug("Returning iSER Logout -EAGAIN\n");
  1614. ret = -EAGAIN;
  1615. }
  1616. break;
  1617. case ISTATE_SEND_NOPIN:
  1618. ret = isert_put_nopin(cmd, conn, true);
  1619. break;
  1620. case ISTATE_SEND_TASKMGTRSP:
  1621. ret = isert_put_tm_rsp(cmd, conn);
  1622. break;
  1623. case ISTATE_SEND_REJECT:
  1624. ret = isert_put_reject(cmd, conn);
  1625. break;
  1626. case ISTATE_SEND_STATUS:
  1627. /*
  1628. * Special case for sending non GOOD SCSI status from TX thread
  1629. * context during pre se_cmd excecution failure.
  1630. */
  1631. ret = isert_put_response(conn, cmd);
  1632. break;
  1633. default:
  1634. pr_err("Unknown response state: 0x%02x\n", state);
  1635. ret = -EINVAL;
  1636. break;
  1637. }
  1638. return ret;
  1639. }
  1640. static int
  1641. isert_setup_np(struct iscsi_np *np,
  1642. struct __kernel_sockaddr_storage *ksockaddr)
  1643. {
  1644. struct isert_np *isert_np;
  1645. struct rdma_cm_id *isert_lid;
  1646. struct sockaddr *sa;
  1647. int ret;
  1648. isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
  1649. if (!isert_np) {
  1650. pr_err("Unable to allocate struct isert_np\n");
  1651. return -ENOMEM;
  1652. }
  1653. init_waitqueue_head(&isert_np->np_accept_wq);
  1654. mutex_init(&isert_np->np_accept_mutex);
  1655. INIT_LIST_HEAD(&isert_np->np_accept_list);
  1656. init_completion(&isert_np->np_login_comp);
  1657. sa = (struct sockaddr *)ksockaddr;
  1658. pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
  1659. /*
  1660. * Setup the np->np_sockaddr from the passed sockaddr setup
  1661. * in iscsi_target_configfs.c code..
  1662. */
  1663. memcpy(&np->np_sockaddr, ksockaddr,
  1664. sizeof(struct __kernel_sockaddr_storage));
  1665. isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
  1666. IB_QPT_RC);
  1667. if (IS_ERR(isert_lid)) {
  1668. pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
  1669. PTR_ERR(isert_lid));
  1670. ret = PTR_ERR(isert_lid);
  1671. goto out;
  1672. }
  1673. ret = rdma_bind_addr(isert_lid, sa);
  1674. if (ret) {
  1675. pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
  1676. goto out_lid;
  1677. }
  1678. ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
  1679. if (ret) {
  1680. pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
  1681. goto out_lid;
  1682. }
  1683. isert_np->np_cm_id = isert_lid;
  1684. np->np_context = isert_np;
  1685. pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
  1686. return 0;
  1687. out_lid:
  1688. rdma_destroy_id(isert_lid);
  1689. out:
  1690. kfree(isert_np);
  1691. return ret;
  1692. }
  1693. static int
  1694. isert_check_accept_queue(struct isert_np *isert_np)
  1695. {
  1696. int empty;
  1697. mutex_lock(&isert_np->np_accept_mutex);
  1698. empty = list_empty(&isert_np->np_accept_list);
  1699. mutex_unlock(&isert_np->np_accept_mutex);
  1700. return empty;
  1701. }
  1702. static int
  1703. isert_rdma_accept(struct isert_conn *isert_conn)
  1704. {
  1705. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1706. struct rdma_conn_param cp;
  1707. int ret;
  1708. memset(&cp, 0, sizeof(struct rdma_conn_param));
  1709. cp.responder_resources = isert_conn->responder_resources;
  1710. cp.initiator_depth = isert_conn->initiator_depth;
  1711. cp.retry_count = 7;
  1712. cp.rnr_retry_count = 7;
  1713. pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
  1714. ret = rdma_accept(cm_id, &cp);
  1715. if (ret) {
  1716. pr_err("rdma_accept() failed with: %d\n", ret);
  1717. return ret;
  1718. }
  1719. pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
  1720. return 0;
  1721. }
  1722. static int
  1723. isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  1724. {
  1725. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1726. int ret;
  1727. pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
  1728. ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
  1729. if (ret)
  1730. return ret;
  1731. pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
  1732. return 0;
  1733. }
  1734. static void
  1735. isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  1736. struct isert_conn *isert_conn)
  1737. {
  1738. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1739. struct rdma_route *cm_route = &cm_id->route;
  1740. struct sockaddr_in *sock_in;
  1741. struct sockaddr_in6 *sock_in6;
  1742. conn->login_family = np->np_sockaddr.ss_family;
  1743. if (np->np_sockaddr.ss_family == AF_INET6) {
  1744. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
  1745. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  1746. &sock_in6->sin6_addr.in6_u);
  1747. conn->login_port = ntohs(sock_in6->sin6_port);
  1748. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
  1749. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  1750. &sock_in6->sin6_addr.in6_u);
  1751. conn->local_port = ntohs(sock_in6->sin6_port);
  1752. } else {
  1753. sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
  1754. sprintf(conn->login_ip, "%pI4",
  1755. &sock_in->sin_addr.s_addr);
  1756. conn->login_port = ntohs(sock_in->sin_port);
  1757. sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
  1758. sprintf(conn->local_ip, "%pI4",
  1759. &sock_in->sin_addr.s_addr);
  1760. conn->local_port = ntohs(sock_in->sin_port);
  1761. }
  1762. }
  1763. static int
  1764. isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  1765. {
  1766. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1767. struct isert_conn *isert_conn;
  1768. int max_accept = 0, ret;
  1769. accept_wait:
  1770. ret = wait_event_interruptible(isert_np->np_accept_wq,
  1771. !isert_check_accept_queue(isert_np) ||
  1772. np->np_thread_state == ISCSI_NP_THREAD_RESET);
  1773. if (max_accept > 5)
  1774. return -ENODEV;
  1775. spin_lock_bh(&np->np_thread_lock);
  1776. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  1777. spin_unlock_bh(&np->np_thread_lock);
  1778. pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
  1779. return -ENODEV;
  1780. }
  1781. spin_unlock_bh(&np->np_thread_lock);
  1782. mutex_lock(&isert_np->np_accept_mutex);
  1783. if (list_empty(&isert_np->np_accept_list)) {
  1784. mutex_unlock(&isert_np->np_accept_mutex);
  1785. max_accept++;
  1786. goto accept_wait;
  1787. }
  1788. isert_conn = list_first_entry(&isert_np->np_accept_list,
  1789. struct isert_conn, conn_accept_node);
  1790. list_del_init(&isert_conn->conn_accept_node);
  1791. mutex_unlock(&isert_np->np_accept_mutex);
  1792. conn->context = isert_conn;
  1793. isert_conn->conn = conn;
  1794. max_accept = 0;
  1795. ret = isert_rdma_post_recvl(isert_conn);
  1796. if (ret)
  1797. return ret;
  1798. ret = isert_rdma_accept(isert_conn);
  1799. if (ret)
  1800. return ret;
  1801. isert_set_conn_info(np, conn, isert_conn);
  1802. pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
  1803. return 0;
  1804. }
  1805. static void
  1806. isert_free_np(struct iscsi_np *np)
  1807. {
  1808. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1809. rdma_destroy_id(isert_np->np_cm_id);
  1810. np->np_context = NULL;
  1811. kfree(isert_np);
  1812. }
  1813. static void isert_free_conn(struct iscsi_conn *conn)
  1814. {
  1815. struct isert_conn *isert_conn = conn->context;
  1816. pr_debug("isert_free_conn: Starting \n");
  1817. /*
  1818. * Decrement post_send_buf_count for special case when called
  1819. * from isert_do_control_comp() -> iscsit_logout_post_handler()
  1820. */
  1821. if (isert_conn->logout_posted)
  1822. atomic_dec(&isert_conn->post_send_buf_count);
  1823. if (isert_conn->conn_cm_id)
  1824. rdma_disconnect(isert_conn->conn_cm_id);
  1825. /*
  1826. * Only wait for conn_wait_comp_err if the isert_conn made it
  1827. * into full feature phase..
  1828. */
  1829. if (isert_conn->state > ISER_CONN_INIT) {
  1830. pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
  1831. isert_conn->state);
  1832. wait_event(isert_conn->conn_wait_comp_err,
  1833. isert_conn->state == ISER_CONN_TERMINATING);
  1834. pr_debug("isert_free_conn: After wait_event #1 >>>>>>>>>>>>\n");
  1835. }
  1836. pr_debug("isert_free_conn: wait_event conn_wait %d\n", isert_conn->state);
  1837. wait_event(isert_conn->conn_wait, isert_conn->state == ISER_CONN_DOWN);
  1838. pr_debug("isert_free_conn: After wait_event #2 >>>>>>>>>>>>>>>>>>>>\n");
  1839. isert_put_conn(isert_conn);
  1840. }
  1841. static struct iscsit_transport iser_target_transport = {
  1842. .name = "IB/iSER",
  1843. .transport_type = ISCSI_INFINIBAND,
  1844. .owner = THIS_MODULE,
  1845. .iscsit_setup_np = isert_setup_np,
  1846. .iscsit_accept_np = isert_accept_np,
  1847. .iscsit_free_np = isert_free_np,
  1848. .iscsit_free_conn = isert_free_conn,
  1849. .iscsit_alloc_cmd = isert_alloc_cmd,
  1850. .iscsit_get_login_rx = isert_get_login_rx,
  1851. .iscsit_put_login_tx = isert_put_login_tx,
  1852. .iscsit_immediate_queue = isert_immediate_queue,
  1853. .iscsit_response_queue = isert_response_queue,
  1854. .iscsit_get_dataout = isert_get_dataout,
  1855. .iscsit_queue_data_in = isert_put_datain,
  1856. .iscsit_queue_status = isert_put_response,
  1857. };
  1858. static int __init isert_init(void)
  1859. {
  1860. int ret;
  1861. isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
  1862. if (!isert_rx_wq) {
  1863. pr_err("Unable to allocate isert_rx_wq\n");
  1864. return -ENOMEM;
  1865. }
  1866. isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
  1867. if (!isert_comp_wq) {
  1868. pr_err("Unable to allocate isert_comp_wq\n");
  1869. ret = -ENOMEM;
  1870. goto destroy_rx_wq;
  1871. }
  1872. isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
  1873. sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
  1874. 0, NULL);
  1875. if (!isert_cmd_cache) {
  1876. pr_err("Unable to create isert_cmd_cache\n");
  1877. ret = -ENOMEM;
  1878. goto destroy_tx_cq;
  1879. }
  1880. iscsit_register_transport(&iser_target_transport);
  1881. pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
  1882. return 0;
  1883. destroy_tx_cq:
  1884. destroy_workqueue(isert_comp_wq);
  1885. destroy_rx_wq:
  1886. destroy_workqueue(isert_rx_wq);
  1887. return ret;
  1888. }
  1889. static void __exit isert_exit(void)
  1890. {
  1891. kmem_cache_destroy(isert_cmd_cache);
  1892. destroy_workqueue(isert_comp_wq);
  1893. destroy_workqueue(isert_rx_wq);
  1894. iscsit_unregister_transport(&iser_target_transport);
  1895. pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
  1896. }
  1897. MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
  1898. MODULE_VERSION("0.1");
  1899. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  1900. MODULE_LICENSE("GPL");
  1901. module_init(isert_init);
  1902. module_exit(isert_exit);