ib_isert.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  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. mutex_init(&isert_conn->conn_mutex);
  332. cma_id->context = isert_conn;
  333. isert_conn->conn_cm_id = cma_id;
  334. isert_conn->responder_resources = event->param.conn.responder_resources;
  335. isert_conn->initiator_depth = event->param.conn.initiator_depth;
  336. pr_debug("Using responder_resources: %u initiator_depth: %u\n",
  337. isert_conn->responder_resources, isert_conn->initiator_depth);
  338. isert_conn->login_buf = kzalloc(ISCSI_DEF_MAX_RECV_SEG_LEN +
  339. ISER_RX_LOGIN_SIZE, GFP_KERNEL);
  340. if (!isert_conn->login_buf) {
  341. pr_err("Unable to allocate isert_conn->login_buf\n");
  342. ret = -ENOMEM;
  343. goto out;
  344. }
  345. isert_conn->login_req_buf = isert_conn->login_buf;
  346. isert_conn->login_rsp_buf = isert_conn->login_buf +
  347. ISCSI_DEF_MAX_RECV_SEG_LEN;
  348. pr_debug("Set login_buf: %p login_req_buf: %p login_rsp_buf: %p\n",
  349. isert_conn->login_buf, isert_conn->login_req_buf,
  350. isert_conn->login_rsp_buf);
  351. isert_conn->login_req_dma = ib_dma_map_single(ib_dev,
  352. (void *)isert_conn->login_req_buf,
  353. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  354. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_req_dma);
  355. if (ret) {
  356. pr_err("ib_dma_mapping_error failed for login_req_dma: %d\n",
  357. ret);
  358. isert_conn->login_req_dma = 0;
  359. goto out_login_buf;
  360. }
  361. isert_conn->login_rsp_dma = ib_dma_map_single(ib_dev,
  362. (void *)isert_conn->login_rsp_buf,
  363. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  364. ret = ib_dma_mapping_error(ib_dev, isert_conn->login_rsp_dma);
  365. if (ret) {
  366. pr_err("ib_dma_mapping_error failed for login_rsp_dma: %d\n",
  367. ret);
  368. isert_conn->login_rsp_dma = 0;
  369. goto out_req_dma_map;
  370. }
  371. device = isert_device_find_by_ib_dev(cma_id);
  372. if (IS_ERR(device)) {
  373. ret = PTR_ERR(device);
  374. goto out_rsp_dma_map;
  375. }
  376. isert_conn->conn_device = device;
  377. isert_conn->conn_pd = device->dev_pd;
  378. isert_conn->conn_mr = device->dev_mr;
  379. ret = isert_conn_setup_qp(isert_conn, cma_id);
  380. if (ret)
  381. goto out_conn_dev;
  382. mutex_lock(&isert_np->np_accept_mutex);
  383. list_add_tail(&isert_np->np_accept_list, &isert_conn->conn_accept_node);
  384. mutex_unlock(&isert_np->np_accept_mutex);
  385. pr_debug("isert_connect_request() waking up np_accept_wq: %p\n", np);
  386. wake_up(&isert_np->np_accept_wq);
  387. return 0;
  388. out_conn_dev:
  389. isert_device_try_release(device);
  390. out_rsp_dma_map:
  391. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  392. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  393. out_req_dma_map:
  394. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  395. ISCSI_DEF_MAX_RECV_SEG_LEN, DMA_FROM_DEVICE);
  396. out_login_buf:
  397. kfree(isert_conn->login_buf);
  398. out:
  399. kfree(isert_conn);
  400. return ret;
  401. }
  402. static void
  403. isert_connect_release(struct isert_conn *isert_conn)
  404. {
  405. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  406. struct isert_device *device = isert_conn->conn_device;
  407. int cq_index;
  408. pr_debug("Entering isert_connect_release(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  409. if (isert_conn->conn_qp) {
  410. cq_index = ((struct isert_cq_desc *)
  411. isert_conn->conn_qp->recv_cq->cq_context)->cq_index;
  412. pr_debug("isert_connect_release: cq_index: %d\n", cq_index);
  413. isert_conn->conn_device->cq_active_qps[cq_index]--;
  414. rdma_destroy_qp(isert_conn->conn_cm_id);
  415. }
  416. isert_free_rx_descriptors(isert_conn);
  417. rdma_destroy_id(isert_conn->conn_cm_id);
  418. if (isert_conn->login_buf) {
  419. ib_dma_unmap_single(ib_dev, isert_conn->login_rsp_dma,
  420. ISER_RX_LOGIN_SIZE, DMA_TO_DEVICE);
  421. ib_dma_unmap_single(ib_dev, isert_conn->login_req_dma,
  422. ISCSI_DEF_MAX_RECV_SEG_LEN,
  423. DMA_FROM_DEVICE);
  424. kfree(isert_conn->login_buf);
  425. }
  426. kfree(isert_conn);
  427. if (device)
  428. isert_device_try_release(device);
  429. pr_debug("Leaving isert_connect_release >>>>>>>>>>>>\n");
  430. }
  431. static void
  432. isert_connected_handler(struct rdma_cm_id *cma_id)
  433. {
  434. return;
  435. }
  436. static void
  437. isert_release_conn_kref(struct kref *kref)
  438. {
  439. struct isert_conn *isert_conn = container_of(kref,
  440. struct isert_conn, conn_kref);
  441. pr_debug("Calling isert_connect_release for final kref %s/%d\n",
  442. current->comm, current->pid);
  443. isert_connect_release(isert_conn);
  444. }
  445. static void
  446. isert_put_conn(struct isert_conn *isert_conn)
  447. {
  448. kref_put(&isert_conn->conn_kref, isert_release_conn_kref);
  449. }
  450. static void
  451. isert_disconnect_work(struct work_struct *work)
  452. {
  453. struct isert_conn *isert_conn = container_of(work,
  454. struct isert_conn, conn_logout_work);
  455. pr_debug("isert_disconnect_work(): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  456. mutex_lock(&isert_conn->conn_mutex);
  457. isert_conn->state = ISER_CONN_DOWN;
  458. if (isert_conn->post_recv_buf_count == 0 &&
  459. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  460. pr_debug("Calling wake_up(&isert_conn->conn_wait);\n");
  461. mutex_unlock(&isert_conn->conn_mutex);
  462. goto wake_up;
  463. }
  464. if (!isert_conn->conn_cm_id) {
  465. mutex_unlock(&isert_conn->conn_mutex);
  466. isert_put_conn(isert_conn);
  467. return;
  468. }
  469. if (!isert_conn->logout_posted) {
  470. pr_debug("Calling rdma_disconnect for !logout_posted from"
  471. " isert_disconnect_work\n");
  472. rdma_disconnect(isert_conn->conn_cm_id);
  473. mutex_unlock(&isert_conn->conn_mutex);
  474. iscsit_cause_connection_reinstatement(isert_conn->conn, 0);
  475. goto wake_up;
  476. }
  477. mutex_unlock(&isert_conn->conn_mutex);
  478. wake_up:
  479. wake_up(&isert_conn->conn_wait);
  480. isert_put_conn(isert_conn);
  481. }
  482. static void
  483. isert_disconnected_handler(struct rdma_cm_id *cma_id)
  484. {
  485. struct isert_conn *isert_conn = (struct isert_conn *)cma_id->context;
  486. INIT_WORK(&isert_conn->conn_logout_work, isert_disconnect_work);
  487. schedule_work(&isert_conn->conn_logout_work);
  488. }
  489. static int
  490. isert_cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
  491. {
  492. int ret = 0;
  493. pr_debug("isert_cma_handler: event %d status %d conn %p id %p\n",
  494. event->event, event->status, cma_id->context, cma_id);
  495. switch (event->event) {
  496. case RDMA_CM_EVENT_CONNECT_REQUEST:
  497. pr_debug("RDMA_CM_EVENT_CONNECT_REQUEST: >>>>>>>>>>>>>>>\n");
  498. ret = isert_connect_request(cma_id, event);
  499. break;
  500. case RDMA_CM_EVENT_ESTABLISHED:
  501. pr_debug("RDMA_CM_EVENT_ESTABLISHED >>>>>>>>>>>>>>\n");
  502. isert_connected_handler(cma_id);
  503. break;
  504. case RDMA_CM_EVENT_DISCONNECTED:
  505. pr_debug("RDMA_CM_EVENT_DISCONNECTED: >>>>>>>>>>>>>>\n");
  506. isert_disconnected_handler(cma_id);
  507. break;
  508. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  509. case RDMA_CM_EVENT_ADDR_CHANGE:
  510. break;
  511. case RDMA_CM_EVENT_CONNECT_ERROR:
  512. default:
  513. pr_err("Unknown RDMA CMA event: %d\n", event->event);
  514. break;
  515. }
  516. if (ret != 0) {
  517. pr_err("isert_cma_handler failed RDMA_CM_EVENT: 0x%08x %d\n",
  518. event->event, ret);
  519. dump_stack();
  520. }
  521. return ret;
  522. }
  523. static int
  524. isert_post_recv(struct isert_conn *isert_conn, u32 count)
  525. {
  526. struct ib_recv_wr *rx_wr, *rx_wr_failed;
  527. int i, ret;
  528. unsigned int rx_head = isert_conn->conn_rx_desc_head;
  529. struct iser_rx_desc *rx_desc;
  530. for (rx_wr = isert_conn->conn_rx_wr, i = 0; i < count; i++, rx_wr++) {
  531. rx_desc = &isert_conn->conn_rx_descs[rx_head];
  532. rx_wr->wr_id = (unsigned long)rx_desc;
  533. rx_wr->sg_list = &rx_desc->rx_sg;
  534. rx_wr->num_sge = 1;
  535. rx_wr->next = rx_wr + 1;
  536. rx_head = (rx_head + 1) & (ISERT_QP_MAX_RECV_DTOS - 1);
  537. }
  538. rx_wr--;
  539. rx_wr->next = NULL; /* mark end of work requests list */
  540. isert_conn->post_recv_buf_count += count;
  541. ret = ib_post_recv(isert_conn->conn_qp, isert_conn->conn_rx_wr,
  542. &rx_wr_failed);
  543. if (ret) {
  544. pr_err("ib_post_recv() failed with ret: %d\n", ret);
  545. isert_conn->post_recv_buf_count -= count;
  546. } else {
  547. pr_debug("isert_post_recv(): Posted %d RX buffers\n", count);
  548. isert_conn->conn_rx_desc_head = rx_head;
  549. }
  550. return ret;
  551. }
  552. static int
  553. isert_post_send(struct isert_conn *isert_conn, struct iser_tx_desc *tx_desc)
  554. {
  555. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  556. struct ib_send_wr send_wr, *send_wr_failed;
  557. int ret;
  558. ib_dma_sync_single_for_device(ib_dev, tx_desc->dma_addr,
  559. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  560. send_wr.next = NULL;
  561. send_wr.wr_id = (unsigned long)tx_desc;
  562. send_wr.sg_list = tx_desc->tx_sg;
  563. send_wr.num_sge = tx_desc->num_sge;
  564. send_wr.opcode = IB_WR_SEND;
  565. send_wr.send_flags = IB_SEND_SIGNALED;
  566. atomic_inc(&isert_conn->post_send_buf_count);
  567. ret = ib_post_send(isert_conn->conn_qp, &send_wr, &send_wr_failed);
  568. if (ret) {
  569. pr_err("ib_post_send() failed, ret: %d\n", ret);
  570. atomic_dec(&isert_conn->post_send_buf_count);
  571. }
  572. return ret;
  573. }
  574. static void
  575. isert_create_send_desc(struct isert_conn *isert_conn,
  576. struct isert_cmd *isert_cmd,
  577. struct iser_tx_desc *tx_desc)
  578. {
  579. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  580. ib_dma_sync_single_for_cpu(ib_dev, tx_desc->dma_addr,
  581. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  582. memset(&tx_desc->iser_header, 0, sizeof(struct iser_hdr));
  583. tx_desc->iser_header.flags = ISER_VER;
  584. tx_desc->num_sge = 1;
  585. tx_desc->isert_cmd = isert_cmd;
  586. if (tx_desc->tx_sg[0].lkey != isert_conn->conn_mr->lkey) {
  587. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  588. pr_debug("tx_desc %p lkey mismatch, fixing\n", tx_desc);
  589. }
  590. }
  591. static int
  592. isert_init_tx_hdrs(struct isert_conn *isert_conn,
  593. struct iser_tx_desc *tx_desc)
  594. {
  595. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  596. u64 dma_addr;
  597. dma_addr = ib_dma_map_single(ib_dev, (void *)tx_desc,
  598. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  599. if (ib_dma_mapping_error(ib_dev, dma_addr)) {
  600. pr_err("ib_dma_mapping_error() failed\n");
  601. return -ENOMEM;
  602. }
  603. tx_desc->dma_addr = dma_addr;
  604. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  605. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  606. tx_desc->tx_sg[0].lkey = isert_conn->conn_mr->lkey;
  607. pr_debug("isert_init_tx_hdrs: Setup tx_sg[0].addr: 0x%llx length: %u"
  608. " lkey: 0x%08x\n", tx_desc->tx_sg[0].addr,
  609. tx_desc->tx_sg[0].length, tx_desc->tx_sg[0].lkey);
  610. return 0;
  611. }
  612. static void
  613. isert_init_send_wr(struct isert_cmd *isert_cmd, struct ib_send_wr *send_wr)
  614. {
  615. isert_cmd->rdma_wr.iser_ib_op = ISER_IB_SEND;
  616. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  617. send_wr->opcode = IB_WR_SEND;
  618. send_wr->send_flags = IB_SEND_SIGNALED;
  619. send_wr->sg_list = &isert_cmd->tx_desc.tx_sg[0];
  620. send_wr->num_sge = isert_cmd->tx_desc.num_sge;
  621. }
  622. static int
  623. isert_rdma_post_recvl(struct isert_conn *isert_conn)
  624. {
  625. struct ib_recv_wr rx_wr, *rx_wr_fail;
  626. struct ib_sge sge;
  627. int ret;
  628. memset(&sge, 0, sizeof(struct ib_sge));
  629. sge.addr = isert_conn->login_req_dma;
  630. sge.length = ISER_RX_LOGIN_SIZE;
  631. sge.lkey = isert_conn->conn_mr->lkey;
  632. pr_debug("Setup sge: addr: %llx length: %d 0x%08x\n",
  633. sge.addr, sge.length, sge.lkey);
  634. memset(&rx_wr, 0, sizeof(struct ib_recv_wr));
  635. rx_wr.wr_id = (unsigned long)isert_conn->login_req_buf;
  636. rx_wr.sg_list = &sge;
  637. rx_wr.num_sge = 1;
  638. isert_conn->post_recv_buf_count++;
  639. ret = ib_post_recv(isert_conn->conn_qp, &rx_wr, &rx_wr_fail);
  640. if (ret) {
  641. pr_err("ib_post_recv() failed: %d\n", ret);
  642. isert_conn->post_recv_buf_count--;
  643. }
  644. pr_debug("ib_post_recv(): returned success >>>>>>>>>>>>>>>>>>>>>>>>\n");
  645. return ret;
  646. }
  647. static int
  648. isert_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
  649. u32 length)
  650. {
  651. struct isert_conn *isert_conn = conn->context;
  652. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  653. struct iser_tx_desc *tx_desc = &isert_conn->conn_login_tx_desc;
  654. int ret;
  655. isert_create_send_desc(isert_conn, NULL, tx_desc);
  656. memcpy(&tx_desc->iscsi_header, &login->rsp[0],
  657. sizeof(struct iscsi_hdr));
  658. isert_init_tx_hdrs(isert_conn, tx_desc);
  659. if (length > 0) {
  660. struct ib_sge *tx_dsg = &tx_desc->tx_sg[1];
  661. ib_dma_sync_single_for_cpu(ib_dev, isert_conn->login_rsp_dma,
  662. length, DMA_TO_DEVICE);
  663. memcpy(isert_conn->login_rsp_buf, login->rsp_buf, length);
  664. ib_dma_sync_single_for_device(ib_dev, isert_conn->login_rsp_dma,
  665. length, DMA_TO_DEVICE);
  666. tx_dsg->addr = isert_conn->login_rsp_dma;
  667. tx_dsg->length = length;
  668. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  669. tx_desc->num_sge = 2;
  670. }
  671. if (!login->login_failed) {
  672. if (login->login_complete) {
  673. ret = isert_alloc_rx_descriptors(isert_conn);
  674. if (ret)
  675. return ret;
  676. ret = isert_post_recv(isert_conn, ISERT_MIN_POSTED_RX);
  677. if (ret)
  678. return ret;
  679. isert_conn->state = ISER_CONN_UP;
  680. goto post_send;
  681. }
  682. ret = isert_rdma_post_recvl(isert_conn);
  683. if (ret)
  684. return ret;
  685. }
  686. post_send:
  687. ret = isert_post_send(isert_conn, tx_desc);
  688. if (ret)
  689. return ret;
  690. return 0;
  691. }
  692. static void
  693. isert_rx_login_req(struct iser_rx_desc *rx_desc, int rx_buflen,
  694. struct isert_conn *isert_conn)
  695. {
  696. struct iscsi_conn *conn = isert_conn->conn;
  697. struct iscsi_login *login = conn->conn_login;
  698. int size;
  699. if (!login) {
  700. pr_err("conn->conn_login is NULL\n");
  701. dump_stack();
  702. return;
  703. }
  704. if (login->first_request) {
  705. struct iscsi_login_req *login_req =
  706. (struct iscsi_login_req *)&rx_desc->iscsi_header;
  707. /*
  708. * Setup the initial iscsi_login values from the leading
  709. * login request PDU.
  710. */
  711. login->leading_connection = (!login_req->tsih) ? 1 : 0;
  712. login->current_stage =
  713. (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
  714. >> 2;
  715. login->version_min = login_req->min_version;
  716. login->version_max = login_req->max_version;
  717. memcpy(login->isid, login_req->isid, 6);
  718. login->cmd_sn = be32_to_cpu(login_req->cmdsn);
  719. login->init_task_tag = login_req->itt;
  720. login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
  721. login->cid = be16_to_cpu(login_req->cid);
  722. login->tsih = be16_to_cpu(login_req->tsih);
  723. }
  724. memcpy(&login->req[0], (void *)&rx_desc->iscsi_header, ISCSI_HDR_LEN);
  725. size = min(rx_buflen, MAX_KEY_VALUE_PAIRS);
  726. pr_debug("Using login payload size: %d, rx_buflen: %d MAX_KEY_VALUE_PAIRS: %d\n",
  727. size, rx_buflen, MAX_KEY_VALUE_PAIRS);
  728. memcpy(login->req_buf, &rx_desc->data[0], size);
  729. complete(&isert_conn->conn_login_comp);
  730. }
  731. static void
  732. isert_release_cmd(struct iscsi_cmd *cmd)
  733. {
  734. struct isert_cmd *isert_cmd = container_of(cmd, struct isert_cmd,
  735. iscsi_cmd);
  736. pr_debug("Entering isert_release_cmd %p >>>>>>>>>>>>>>>.\n", isert_cmd);
  737. kfree(cmd->buf_ptr);
  738. kfree(cmd->tmr_req);
  739. kmem_cache_free(isert_cmd_cache, isert_cmd);
  740. }
  741. static struct iscsi_cmd
  742. *isert_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp)
  743. {
  744. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  745. struct isert_cmd *isert_cmd;
  746. isert_cmd = kmem_cache_zalloc(isert_cmd_cache, gfp);
  747. if (!isert_cmd) {
  748. pr_err("Unable to allocate isert_cmd\n");
  749. return NULL;
  750. }
  751. isert_cmd->conn = isert_conn;
  752. isert_cmd->iscsi_cmd.release_cmd = &isert_release_cmd;
  753. return &isert_cmd->iscsi_cmd;
  754. }
  755. static int
  756. isert_handle_scsi_cmd(struct isert_conn *isert_conn,
  757. struct isert_cmd *isert_cmd, struct iser_rx_desc *rx_desc,
  758. unsigned char *buf)
  759. {
  760. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  761. struct iscsi_conn *conn = isert_conn->conn;
  762. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
  763. struct scatterlist *sg;
  764. int imm_data, imm_data_len, unsol_data, sg_nents, rc;
  765. bool dump_payload = false;
  766. rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
  767. if (rc < 0)
  768. return rc;
  769. imm_data = cmd->immediate_data;
  770. imm_data_len = cmd->first_burst_len;
  771. unsol_data = cmd->unsolicited_data;
  772. rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
  773. if (rc < 0) {
  774. return 0;
  775. } else if (rc > 0) {
  776. dump_payload = true;
  777. goto sequence_cmd;
  778. }
  779. if (!imm_data)
  780. return 0;
  781. sg = &cmd->se_cmd.t_data_sg[0];
  782. sg_nents = max(1UL, DIV_ROUND_UP(imm_data_len, PAGE_SIZE));
  783. pr_debug("Copying Immediate SG: %p sg_nents: %u from %p imm_data_len: %d\n",
  784. sg, sg_nents, &rx_desc->data[0], imm_data_len);
  785. sg_copy_from_buffer(sg, sg_nents, &rx_desc->data[0], imm_data_len);
  786. cmd->write_data_done += imm_data_len;
  787. if (cmd->write_data_done == cmd->se_cmd.data_length) {
  788. spin_lock_bh(&cmd->istate_lock);
  789. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  790. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  791. spin_unlock_bh(&cmd->istate_lock);
  792. }
  793. sequence_cmd:
  794. rc = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
  795. if (!rc && dump_payload == false && unsol_data)
  796. iscsit_set_unsoliticed_dataout(cmd);
  797. return 0;
  798. }
  799. static int
  800. isert_handle_iscsi_dataout(struct isert_conn *isert_conn,
  801. struct iser_rx_desc *rx_desc, unsigned char *buf)
  802. {
  803. struct scatterlist *sg_start;
  804. struct iscsi_conn *conn = isert_conn->conn;
  805. struct iscsi_cmd *cmd = NULL;
  806. struct iscsi_data *hdr = (struct iscsi_data *)buf;
  807. u32 unsol_data_len = ntoh24(hdr->dlength);
  808. int rc, sg_nents, sg_off, page_off;
  809. rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
  810. if (rc < 0)
  811. return rc;
  812. else if (!cmd)
  813. return 0;
  814. /*
  815. * FIXME: Unexpected unsolicited_data out
  816. */
  817. if (!cmd->unsolicited_data) {
  818. pr_err("Received unexpected solicited data payload\n");
  819. dump_stack();
  820. return -1;
  821. }
  822. pr_debug("Unsolicited DataOut unsol_data_len: %u, write_data_done: %u, data_length: %u\n",
  823. unsol_data_len, cmd->write_data_done, cmd->se_cmd.data_length);
  824. sg_off = cmd->write_data_done / PAGE_SIZE;
  825. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  826. sg_nents = max(1UL, DIV_ROUND_UP(unsol_data_len, PAGE_SIZE));
  827. page_off = cmd->write_data_done % PAGE_SIZE;
  828. /*
  829. * FIXME: Non page-aligned unsolicited_data out
  830. */
  831. if (page_off) {
  832. pr_err("Received unexpected non-page aligned data payload\n");
  833. dump_stack();
  834. return -1;
  835. }
  836. pr_debug("Copying DataOut: sg_start: %p, sg_off: %u sg_nents: %u from %p %u\n",
  837. sg_start, sg_off, sg_nents, &rx_desc->data[0], unsol_data_len);
  838. sg_copy_from_buffer(sg_start, sg_nents, &rx_desc->data[0],
  839. unsol_data_len);
  840. rc = iscsit_check_dataout_payload(cmd, hdr, false);
  841. if (rc < 0)
  842. return rc;
  843. return 0;
  844. }
  845. static int
  846. isert_handle_nop_out(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  847. struct iser_rx_desc *rx_desc, unsigned char *buf)
  848. {
  849. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  850. struct iscsi_conn *conn = isert_conn->conn;
  851. struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
  852. int rc;
  853. rc = iscsit_setup_nop_out(conn, cmd, hdr);
  854. if (rc < 0)
  855. return rc;
  856. /*
  857. * FIXME: Add support for NOPOUT payload using unsolicited RDMA payload
  858. */
  859. return iscsit_process_nop_out(conn, cmd, hdr);
  860. }
  861. static int
  862. isert_handle_text_cmd(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  863. struct iser_rx_desc *rx_desc, struct iscsi_text *hdr)
  864. {
  865. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  866. struct iscsi_conn *conn = isert_conn->conn;
  867. u32 payload_length = ntoh24(hdr->dlength);
  868. int rc;
  869. unsigned char *text_in;
  870. rc = iscsit_setup_text_cmd(conn, cmd, hdr);
  871. if (rc < 0)
  872. return rc;
  873. text_in = kzalloc(payload_length, GFP_KERNEL);
  874. if (!text_in) {
  875. pr_err("Unable to allocate text_in of payload_length: %u\n",
  876. payload_length);
  877. return -ENOMEM;
  878. }
  879. cmd->text_in_ptr = text_in;
  880. memcpy(cmd->text_in_ptr, &rx_desc->data[0], payload_length);
  881. return iscsit_process_text_cmd(conn, cmd, hdr);
  882. }
  883. static int
  884. isert_rx_opcode(struct isert_conn *isert_conn, struct iser_rx_desc *rx_desc,
  885. uint32_t read_stag, uint64_t read_va,
  886. uint32_t write_stag, uint64_t write_va)
  887. {
  888. struct iscsi_hdr *hdr = &rx_desc->iscsi_header;
  889. struct iscsi_conn *conn = isert_conn->conn;
  890. struct iscsi_cmd *cmd;
  891. struct isert_cmd *isert_cmd;
  892. int ret = -EINVAL;
  893. u8 opcode = (hdr->opcode & ISCSI_OPCODE_MASK);
  894. switch (opcode) {
  895. case ISCSI_OP_SCSI_CMD:
  896. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  897. if (!cmd)
  898. break;
  899. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  900. isert_cmd->read_stag = read_stag;
  901. isert_cmd->read_va = read_va;
  902. isert_cmd->write_stag = write_stag;
  903. isert_cmd->write_va = write_va;
  904. ret = isert_handle_scsi_cmd(isert_conn, isert_cmd,
  905. rx_desc, (unsigned char *)hdr);
  906. break;
  907. case ISCSI_OP_NOOP_OUT:
  908. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  909. if (!cmd)
  910. break;
  911. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  912. ret = isert_handle_nop_out(isert_conn, isert_cmd,
  913. rx_desc, (unsigned char *)hdr);
  914. break;
  915. case ISCSI_OP_SCSI_DATA_OUT:
  916. ret = isert_handle_iscsi_dataout(isert_conn, rx_desc,
  917. (unsigned char *)hdr);
  918. break;
  919. case ISCSI_OP_SCSI_TMFUNC:
  920. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  921. if (!cmd)
  922. break;
  923. ret = iscsit_handle_task_mgt_cmd(conn, cmd,
  924. (unsigned char *)hdr);
  925. break;
  926. case ISCSI_OP_LOGOUT:
  927. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  928. if (!cmd)
  929. break;
  930. ret = iscsit_handle_logout_cmd(conn, cmd, (unsigned char *)hdr);
  931. if (ret > 0)
  932. wait_for_completion_timeout(&conn->conn_logout_comp,
  933. SECONDS_FOR_LOGOUT_COMP *
  934. HZ);
  935. break;
  936. case ISCSI_OP_TEXT:
  937. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  938. if (!cmd)
  939. break;
  940. isert_cmd = container_of(cmd, struct isert_cmd, iscsi_cmd);
  941. ret = isert_handle_text_cmd(isert_conn, isert_cmd,
  942. rx_desc, (struct iscsi_text *)hdr);
  943. break;
  944. default:
  945. pr_err("Got unknown iSCSI OpCode: 0x%02x\n", opcode);
  946. dump_stack();
  947. break;
  948. }
  949. return ret;
  950. }
  951. static void
  952. isert_rx_do_work(struct iser_rx_desc *rx_desc, struct isert_conn *isert_conn)
  953. {
  954. struct iser_hdr *iser_hdr = &rx_desc->iser_header;
  955. uint64_t read_va = 0, write_va = 0;
  956. uint32_t read_stag = 0, write_stag = 0;
  957. int rc;
  958. switch (iser_hdr->flags & 0xF0) {
  959. case ISCSI_CTRL:
  960. if (iser_hdr->flags & ISER_RSV) {
  961. read_stag = be32_to_cpu(iser_hdr->read_stag);
  962. read_va = be64_to_cpu(iser_hdr->read_va);
  963. pr_debug("ISER_RSV: read_stag: 0x%08x read_va: 0x%16llx\n",
  964. read_stag, (unsigned long long)read_va);
  965. }
  966. if (iser_hdr->flags & ISER_WSV) {
  967. write_stag = be32_to_cpu(iser_hdr->write_stag);
  968. write_va = be64_to_cpu(iser_hdr->write_va);
  969. pr_debug("ISER_WSV: write__stag: 0x%08x write_va: 0x%16llx\n",
  970. write_stag, (unsigned long long)write_va);
  971. }
  972. pr_debug("ISER ISCSI_CTRL PDU\n");
  973. break;
  974. case ISER_HELLO:
  975. pr_err("iSER Hello message\n");
  976. break;
  977. default:
  978. pr_warn("Unknown iSER hdr flags: 0x%02x\n", iser_hdr->flags);
  979. break;
  980. }
  981. rc = isert_rx_opcode(isert_conn, rx_desc,
  982. read_stag, read_va, write_stag, write_va);
  983. }
  984. static void
  985. isert_rx_completion(struct iser_rx_desc *desc, struct isert_conn *isert_conn,
  986. unsigned long xfer_len)
  987. {
  988. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  989. struct iscsi_hdr *hdr;
  990. u64 rx_dma;
  991. int rx_buflen, outstanding;
  992. if ((char *)desc == isert_conn->login_req_buf) {
  993. rx_dma = isert_conn->login_req_dma;
  994. rx_buflen = ISER_RX_LOGIN_SIZE;
  995. pr_debug("ISER login_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  996. rx_dma, rx_buflen);
  997. } else {
  998. rx_dma = desc->dma_addr;
  999. rx_buflen = ISER_RX_PAYLOAD_SIZE;
  1000. pr_debug("ISER req_buf: Using rx_dma: 0x%llx, rx_buflen: %d\n",
  1001. rx_dma, rx_buflen);
  1002. }
  1003. ib_dma_sync_single_for_cpu(ib_dev, rx_dma, rx_buflen, DMA_FROM_DEVICE);
  1004. hdr = &desc->iscsi_header;
  1005. pr_debug("iSCSI opcode: 0x%02x, ITT: 0x%08x, flags: 0x%02x dlen: %d\n",
  1006. hdr->opcode, hdr->itt, hdr->flags,
  1007. (int)(xfer_len - ISER_HEADERS_LEN));
  1008. if ((char *)desc == isert_conn->login_req_buf)
  1009. isert_rx_login_req(desc, xfer_len - ISER_HEADERS_LEN,
  1010. isert_conn);
  1011. else
  1012. isert_rx_do_work(desc, isert_conn);
  1013. ib_dma_sync_single_for_device(ib_dev, rx_dma, rx_buflen,
  1014. DMA_FROM_DEVICE);
  1015. isert_conn->post_recv_buf_count--;
  1016. pr_debug("iSERT: Decremented post_recv_buf_count: %d\n",
  1017. isert_conn->post_recv_buf_count);
  1018. if ((char *)desc == isert_conn->login_req_buf)
  1019. return;
  1020. outstanding = isert_conn->post_recv_buf_count;
  1021. if (outstanding + ISERT_MIN_POSTED_RX <= ISERT_QP_MAX_RECV_DTOS) {
  1022. int err, count = min(ISERT_QP_MAX_RECV_DTOS - outstanding,
  1023. ISERT_MIN_POSTED_RX);
  1024. err = isert_post_recv(isert_conn, count);
  1025. if (err) {
  1026. pr_err("isert_post_recv() count: %d failed, %d\n",
  1027. count, err);
  1028. }
  1029. }
  1030. }
  1031. static void
  1032. isert_unmap_cmd(struct isert_cmd *isert_cmd, struct isert_conn *isert_conn)
  1033. {
  1034. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1035. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1036. pr_debug("isert_unmap_cmd >>>>>>>>>>>>>>>>>>>>>>>\n");
  1037. if (wr->sge) {
  1038. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  1039. wr->sge = NULL;
  1040. }
  1041. kfree(wr->send_wr);
  1042. wr->send_wr = NULL;
  1043. kfree(isert_cmd->ib_sge);
  1044. isert_cmd->ib_sge = NULL;
  1045. }
  1046. static void
  1047. isert_put_cmd(struct isert_cmd *isert_cmd)
  1048. {
  1049. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1050. struct isert_conn *isert_conn = isert_cmd->conn;
  1051. struct iscsi_conn *conn = isert_conn->conn;
  1052. pr_debug("Entering isert_put_cmd: %p\n", isert_cmd);
  1053. switch (cmd->iscsi_opcode) {
  1054. case ISCSI_OP_SCSI_CMD:
  1055. spin_lock_bh(&conn->cmd_lock);
  1056. if (!list_empty(&cmd->i_conn_node))
  1057. list_del(&cmd->i_conn_node);
  1058. spin_unlock_bh(&conn->cmd_lock);
  1059. if (cmd->data_direction == DMA_TO_DEVICE)
  1060. iscsit_stop_dataout_timer(cmd);
  1061. isert_unmap_cmd(isert_cmd, isert_conn);
  1062. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1063. break;
  1064. case ISCSI_OP_SCSI_TMFUNC:
  1065. spin_lock_bh(&conn->cmd_lock);
  1066. if (!list_empty(&cmd->i_conn_node))
  1067. list_del(&cmd->i_conn_node);
  1068. spin_unlock_bh(&conn->cmd_lock);
  1069. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1070. break;
  1071. case ISCSI_OP_REJECT:
  1072. case ISCSI_OP_NOOP_OUT:
  1073. case ISCSI_OP_TEXT:
  1074. spin_lock_bh(&conn->cmd_lock);
  1075. if (!list_empty(&cmd->i_conn_node))
  1076. list_del(&cmd->i_conn_node);
  1077. spin_unlock_bh(&conn->cmd_lock);
  1078. /*
  1079. * Handle special case for REJECT when iscsi_add_reject*() has
  1080. * overwritten the original iscsi_opcode assignment, and the
  1081. * associated cmd->se_cmd needs to be released.
  1082. */
  1083. if (cmd->se_cmd.se_tfo != NULL) {
  1084. pr_debug("Calling transport_generic_free_cmd from"
  1085. " isert_put_cmd for 0x%02x\n",
  1086. cmd->iscsi_opcode);
  1087. transport_generic_free_cmd(&cmd->se_cmd, 0);
  1088. break;
  1089. }
  1090. /*
  1091. * Fall-through
  1092. */
  1093. default:
  1094. isert_release_cmd(cmd);
  1095. break;
  1096. }
  1097. }
  1098. static void
  1099. isert_unmap_tx_desc(struct iser_tx_desc *tx_desc, struct ib_device *ib_dev)
  1100. {
  1101. if (tx_desc->dma_addr != 0) {
  1102. pr_debug("Calling ib_dma_unmap_single for tx_desc->dma_addr\n");
  1103. ib_dma_unmap_single(ib_dev, tx_desc->dma_addr,
  1104. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  1105. tx_desc->dma_addr = 0;
  1106. }
  1107. }
  1108. static void
  1109. isert_completion_put(struct iser_tx_desc *tx_desc, struct isert_cmd *isert_cmd,
  1110. struct ib_device *ib_dev)
  1111. {
  1112. if (isert_cmd->pdu_buf_dma != 0) {
  1113. pr_debug("Calling ib_dma_unmap_single for isert_cmd->pdu_buf_dma\n");
  1114. ib_dma_unmap_single(ib_dev, isert_cmd->pdu_buf_dma,
  1115. isert_cmd->pdu_buf_len, DMA_TO_DEVICE);
  1116. isert_cmd->pdu_buf_dma = 0;
  1117. }
  1118. isert_unmap_tx_desc(tx_desc, ib_dev);
  1119. isert_put_cmd(isert_cmd);
  1120. }
  1121. static void
  1122. isert_completion_rdma_read(struct iser_tx_desc *tx_desc,
  1123. struct isert_cmd *isert_cmd)
  1124. {
  1125. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1126. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1127. struct se_cmd *se_cmd = &cmd->se_cmd;
  1128. struct ib_device *ib_dev = isert_cmd->conn->conn_cm_id->device;
  1129. iscsit_stop_dataout_timer(cmd);
  1130. if (wr->sge) {
  1131. pr_debug("isert_do_rdma_read_comp: Unmapping wr->sge from t_data_sg\n");
  1132. ib_dma_unmap_sg(ib_dev, wr->sge, wr->num_sge, DMA_TO_DEVICE);
  1133. wr->sge = NULL;
  1134. }
  1135. if (isert_cmd->ib_sge) {
  1136. pr_debug("isert_do_rdma_read_comp: Freeing isert_cmd->ib_sge\n");
  1137. kfree(isert_cmd->ib_sge);
  1138. isert_cmd->ib_sge = NULL;
  1139. }
  1140. cmd->write_data_done = se_cmd->data_length;
  1141. pr_debug("isert_do_rdma_read_comp, calling target_execute_cmd\n");
  1142. spin_lock_bh(&cmd->istate_lock);
  1143. cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
  1144. cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
  1145. spin_unlock_bh(&cmd->istate_lock);
  1146. target_execute_cmd(se_cmd);
  1147. }
  1148. static void
  1149. isert_do_control_comp(struct work_struct *work)
  1150. {
  1151. struct isert_cmd *isert_cmd = container_of(work,
  1152. struct isert_cmd, comp_work);
  1153. struct isert_conn *isert_conn = isert_cmd->conn;
  1154. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1155. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1156. switch (cmd->i_state) {
  1157. case ISTATE_SEND_TASKMGTRSP:
  1158. pr_debug("Calling iscsit_tmr_post_handler >>>>>>>>>>>>>>>>>\n");
  1159. atomic_dec(&isert_conn->post_send_buf_count);
  1160. iscsit_tmr_post_handler(cmd, cmd->conn);
  1161. cmd->i_state = ISTATE_SENT_STATUS;
  1162. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1163. break;
  1164. case ISTATE_SEND_REJECT:
  1165. pr_debug("Got isert_do_control_comp ISTATE_SEND_REJECT: >>>\n");
  1166. atomic_dec(&isert_conn->post_send_buf_count);
  1167. cmd->i_state = ISTATE_SENT_STATUS;
  1168. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1169. break;
  1170. case ISTATE_SEND_LOGOUTRSP:
  1171. pr_debug("Calling iscsit_logout_post_handler >>>>>>>>>>>>>>\n");
  1172. /*
  1173. * Call atomic_dec(&isert_conn->post_send_buf_count)
  1174. * from isert_free_conn()
  1175. */
  1176. isert_conn->logout_posted = true;
  1177. iscsit_logout_post_handler(cmd, cmd->conn);
  1178. break;
  1179. case ISTATE_SEND_TEXTRSP:
  1180. atomic_dec(&isert_conn->post_send_buf_count);
  1181. cmd->i_state = ISTATE_SENT_STATUS;
  1182. isert_completion_put(&isert_cmd->tx_desc, isert_cmd, ib_dev);
  1183. break;
  1184. default:
  1185. pr_err("Unknown do_control_comp i_state %d\n", cmd->i_state);
  1186. dump_stack();
  1187. break;
  1188. }
  1189. }
  1190. static void
  1191. isert_response_completion(struct iser_tx_desc *tx_desc,
  1192. struct isert_cmd *isert_cmd,
  1193. struct isert_conn *isert_conn,
  1194. struct ib_device *ib_dev)
  1195. {
  1196. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1197. if (cmd->i_state == ISTATE_SEND_TASKMGTRSP ||
  1198. cmd->i_state == ISTATE_SEND_LOGOUTRSP ||
  1199. cmd->i_state == ISTATE_SEND_REJECT ||
  1200. cmd->i_state == ISTATE_SEND_TEXTRSP) {
  1201. isert_unmap_tx_desc(tx_desc, ib_dev);
  1202. INIT_WORK(&isert_cmd->comp_work, isert_do_control_comp);
  1203. queue_work(isert_comp_wq, &isert_cmd->comp_work);
  1204. return;
  1205. }
  1206. atomic_dec(&isert_conn->post_send_buf_count);
  1207. cmd->i_state = ISTATE_SENT_STATUS;
  1208. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1209. }
  1210. static void
  1211. isert_send_completion(struct iser_tx_desc *tx_desc,
  1212. struct isert_conn *isert_conn)
  1213. {
  1214. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1215. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1216. struct isert_rdma_wr *wr;
  1217. if (!isert_cmd) {
  1218. atomic_dec(&isert_conn->post_send_buf_count);
  1219. isert_unmap_tx_desc(tx_desc, ib_dev);
  1220. return;
  1221. }
  1222. wr = &isert_cmd->rdma_wr;
  1223. switch (wr->iser_ib_op) {
  1224. case ISER_IB_RECV:
  1225. pr_err("isert_send_completion: Got ISER_IB_RECV\n");
  1226. dump_stack();
  1227. break;
  1228. case ISER_IB_SEND:
  1229. pr_debug("isert_send_completion: Got ISER_IB_SEND\n");
  1230. isert_response_completion(tx_desc, isert_cmd,
  1231. isert_conn, ib_dev);
  1232. break;
  1233. case ISER_IB_RDMA_WRITE:
  1234. pr_err("isert_send_completion: Got ISER_IB_RDMA_WRITE\n");
  1235. dump_stack();
  1236. break;
  1237. case ISER_IB_RDMA_READ:
  1238. pr_debug("isert_send_completion: Got ISER_IB_RDMA_READ:\n");
  1239. atomic_dec(&isert_conn->post_send_buf_count);
  1240. isert_completion_rdma_read(tx_desc, isert_cmd);
  1241. break;
  1242. default:
  1243. pr_err("Unknown wr->iser_ib_op: 0x%02x\n", wr->iser_ib_op);
  1244. dump_stack();
  1245. break;
  1246. }
  1247. }
  1248. static void
  1249. isert_cq_comp_err(struct iser_tx_desc *tx_desc, struct isert_conn *isert_conn)
  1250. {
  1251. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1252. if (tx_desc) {
  1253. struct isert_cmd *isert_cmd = tx_desc->isert_cmd;
  1254. if (!isert_cmd)
  1255. isert_unmap_tx_desc(tx_desc, ib_dev);
  1256. else
  1257. isert_completion_put(tx_desc, isert_cmd, ib_dev);
  1258. }
  1259. if (isert_conn->post_recv_buf_count == 0 &&
  1260. atomic_read(&isert_conn->post_send_buf_count) == 0) {
  1261. pr_debug("isert_cq_comp_err >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
  1262. pr_debug("Calling wake_up from isert_cq_comp_err\n");
  1263. mutex_lock(&isert_conn->conn_mutex);
  1264. if (isert_conn->state != ISER_CONN_DOWN)
  1265. isert_conn->state = ISER_CONN_TERMINATING;
  1266. mutex_unlock(&isert_conn->conn_mutex);
  1267. wake_up(&isert_conn->conn_wait_comp_err);
  1268. }
  1269. }
  1270. static void
  1271. isert_cq_tx_work(struct work_struct *work)
  1272. {
  1273. struct isert_cq_desc *cq_desc = container_of(work,
  1274. struct isert_cq_desc, cq_tx_work);
  1275. struct isert_device *device = cq_desc->device;
  1276. int cq_index = cq_desc->cq_index;
  1277. struct ib_cq *tx_cq = device->dev_tx_cq[cq_index];
  1278. struct isert_conn *isert_conn;
  1279. struct iser_tx_desc *tx_desc;
  1280. struct ib_wc wc;
  1281. while (ib_poll_cq(tx_cq, 1, &wc) == 1) {
  1282. tx_desc = (struct iser_tx_desc *)(unsigned long)wc.wr_id;
  1283. isert_conn = wc.qp->qp_context;
  1284. if (wc.status == IB_WC_SUCCESS) {
  1285. isert_send_completion(tx_desc, isert_conn);
  1286. } else {
  1287. pr_debug("TX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1288. pr_debug("TX wc.status: 0x%08x\n", wc.status);
  1289. pr_debug("TX wc.vendor_err: 0x%08x\n", wc.vendor_err);
  1290. atomic_dec(&isert_conn->post_send_buf_count);
  1291. isert_cq_comp_err(tx_desc, isert_conn);
  1292. }
  1293. }
  1294. ib_req_notify_cq(tx_cq, IB_CQ_NEXT_COMP);
  1295. }
  1296. static void
  1297. isert_cq_tx_callback(struct ib_cq *cq, void *context)
  1298. {
  1299. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1300. INIT_WORK(&cq_desc->cq_tx_work, isert_cq_tx_work);
  1301. queue_work(isert_comp_wq, &cq_desc->cq_tx_work);
  1302. }
  1303. static void
  1304. isert_cq_rx_work(struct work_struct *work)
  1305. {
  1306. struct isert_cq_desc *cq_desc = container_of(work,
  1307. struct isert_cq_desc, cq_rx_work);
  1308. struct isert_device *device = cq_desc->device;
  1309. int cq_index = cq_desc->cq_index;
  1310. struct ib_cq *rx_cq = device->dev_rx_cq[cq_index];
  1311. struct isert_conn *isert_conn;
  1312. struct iser_rx_desc *rx_desc;
  1313. struct ib_wc wc;
  1314. unsigned long xfer_len;
  1315. while (ib_poll_cq(rx_cq, 1, &wc) == 1) {
  1316. rx_desc = (struct iser_rx_desc *)(unsigned long)wc.wr_id;
  1317. isert_conn = wc.qp->qp_context;
  1318. if (wc.status == IB_WC_SUCCESS) {
  1319. xfer_len = (unsigned long)wc.byte_len;
  1320. isert_rx_completion(rx_desc, isert_conn, xfer_len);
  1321. } else {
  1322. pr_debug("RX wc.status != IB_WC_SUCCESS >>>>>>>>>>>>>>\n");
  1323. if (wc.status != IB_WC_WR_FLUSH_ERR) {
  1324. pr_debug("RX wc.status: 0x%08x\n", wc.status);
  1325. pr_debug("RX wc.vendor_err: 0x%08x\n",
  1326. wc.vendor_err);
  1327. }
  1328. isert_conn->post_recv_buf_count--;
  1329. isert_cq_comp_err(NULL, isert_conn);
  1330. }
  1331. }
  1332. ib_req_notify_cq(rx_cq, IB_CQ_NEXT_COMP);
  1333. }
  1334. static void
  1335. isert_cq_rx_callback(struct ib_cq *cq, void *context)
  1336. {
  1337. struct isert_cq_desc *cq_desc = (struct isert_cq_desc *)context;
  1338. INIT_WORK(&cq_desc->cq_rx_work, isert_cq_rx_work);
  1339. queue_work(isert_rx_wq, &cq_desc->cq_rx_work);
  1340. }
  1341. static int
  1342. isert_post_response(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd)
  1343. {
  1344. struct ib_send_wr *wr_failed;
  1345. int ret;
  1346. atomic_inc(&isert_conn->post_send_buf_count);
  1347. ret = ib_post_send(isert_conn->conn_qp, &isert_cmd->tx_desc.send_wr,
  1348. &wr_failed);
  1349. if (ret) {
  1350. pr_err("ib_post_send failed with %d\n", ret);
  1351. atomic_dec(&isert_conn->post_send_buf_count);
  1352. return ret;
  1353. }
  1354. return ret;
  1355. }
  1356. static int
  1357. isert_put_response(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1358. {
  1359. struct isert_cmd *isert_cmd = container_of(cmd,
  1360. struct isert_cmd, iscsi_cmd);
  1361. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1362. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1363. struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)
  1364. &isert_cmd->tx_desc.iscsi_header;
  1365. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1366. iscsit_build_rsp_pdu(cmd, conn, true, hdr);
  1367. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1368. /*
  1369. * Attach SENSE DATA payload to iSCSI Response PDU
  1370. */
  1371. if (cmd->se_cmd.sense_buffer &&
  1372. ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  1373. (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  1374. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1375. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1376. u32 padding, pdu_len;
  1377. put_unaligned_be16(cmd->se_cmd.scsi_sense_length,
  1378. cmd->sense_buffer);
  1379. cmd->se_cmd.scsi_sense_length += sizeof(__be16);
  1380. padding = -(cmd->se_cmd.scsi_sense_length) & 3;
  1381. hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
  1382. pdu_len = cmd->se_cmd.scsi_sense_length + padding;
  1383. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1384. (void *)cmd->sense_buffer, pdu_len,
  1385. DMA_TO_DEVICE);
  1386. isert_cmd->pdu_buf_len = pdu_len;
  1387. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1388. tx_dsg->length = pdu_len;
  1389. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1390. isert_cmd->tx_desc.num_sge = 2;
  1391. }
  1392. isert_init_send_wr(isert_cmd, send_wr);
  1393. pr_debug("Posting SCSI Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1394. return isert_post_response(isert_conn, isert_cmd);
  1395. }
  1396. static int
  1397. isert_put_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
  1398. bool nopout_response)
  1399. {
  1400. struct isert_cmd *isert_cmd = container_of(cmd,
  1401. struct isert_cmd, iscsi_cmd);
  1402. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1403. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1404. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1405. iscsit_build_nopin_rsp(cmd, conn, (struct iscsi_nopin *)
  1406. &isert_cmd->tx_desc.iscsi_header,
  1407. nopout_response);
  1408. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1409. isert_init_send_wr(isert_cmd, send_wr);
  1410. pr_debug("Posting NOPIN Reponse IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1411. return isert_post_response(isert_conn, isert_cmd);
  1412. }
  1413. static int
  1414. isert_put_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1415. {
  1416. struct isert_cmd *isert_cmd = container_of(cmd,
  1417. struct isert_cmd, iscsi_cmd);
  1418. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1419. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1420. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1421. iscsit_build_logout_rsp(cmd, conn, (struct iscsi_logout_rsp *)
  1422. &isert_cmd->tx_desc.iscsi_header);
  1423. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1424. isert_init_send_wr(isert_cmd, send_wr);
  1425. pr_debug("Posting Logout Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1426. return isert_post_response(isert_conn, isert_cmd);
  1427. }
  1428. static int
  1429. isert_put_tm_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1430. {
  1431. struct isert_cmd *isert_cmd = container_of(cmd,
  1432. struct isert_cmd, iscsi_cmd);
  1433. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1434. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1435. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1436. iscsit_build_task_mgt_rsp(cmd, conn, (struct iscsi_tm_rsp *)
  1437. &isert_cmd->tx_desc.iscsi_header);
  1438. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1439. isert_init_send_wr(isert_cmd, send_wr);
  1440. pr_debug("Posting Task Management Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1441. return isert_post_response(isert_conn, isert_cmd);
  1442. }
  1443. static int
  1444. isert_put_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1445. {
  1446. struct isert_cmd *isert_cmd = container_of(cmd,
  1447. struct isert_cmd, iscsi_cmd);
  1448. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1449. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1450. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1451. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1452. struct iscsi_reject *hdr =
  1453. (struct iscsi_reject *)&isert_cmd->tx_desc.iscsi_header;
  1454. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1455. iscsit_build_reject(cmd, conn, hdr);
  1456. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1457. hton24(hdr->dlength, ISCSI_HDR_LEN);
  1458. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1459. (void *)cmd->buf_ptr, ISCSI_HDR_LEN,
  1460. DMA_TO_DEVICE);
  1461. isert_cmd->pdu_buf_len = ISCSI_HDR_LEN;
  1462. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1463. tx_dsg->length = ISCSI_HDR_LEN;
  1464. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1465. isert_cmd->tx_desc.num_sge = 2;
  1466. isert_init_send_wr(isert_cmd, send_wr);
  1467. pr_debug("Posting Reject IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1468. return isert_post_response(isert_conn, isert_cmd);
  1469. }
  1470. static int
  1471. isert_put_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  1472. {
  1473. struct isert_cmd *isert_cmd = container_of(cmd,
  1474. struct isert_cmd, iscsi_cmd);
  1475. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1476. struct ib_send_wr *send_wr = &isert_cmd->tx_desc.send_wr;
  1477. struct iscsi_text_rsp *hdr =
  1478. (struct iscsi_text_rsp *)&isert_cmd->tx_desc.iscsi_header;
  1479. u32 txt_rsp_len;
  1480. int rc;
  1481. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1482. rc = iscsit_build_text_rsp(cmd, conn, hdr);
  1483. if (rc < 0)
  1484. return rc;
  1485. txt_rsp_len = rc;
  1486. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1487. if (txt_rsp_len) {
  1488. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1489. struct ib_sge *tx_dsg = &isert_cmd->tx_desc.tx_sg[1];
  1490. void *txt_rsp_buf = cmd->buf_ptr;
  1491. isert_cmd->pdu_buf_dma = ib_dma_map_single(ib_dev,
  1492. txt_rsp_buf, txt_rsp_len, DMA_TO_DEVICE);
  1493. isert_cmd->pdu_buf_len = txt_rsp_len;
  1494. tx_dsg->addr = isert_cmd->pdu_buf_dma;
  1495. tx_dsg->length = txt_rsp_len;
  1496. tx_dsg->lkey = isert_conn->conn_mr->lkey;
  1497. isert_cmd->tx_desc.num_sge = 2;
  1498. }
  1499. isert_init_send_wr(isert_cmd, send_wr);
  1500. pr_debug("Posting Text Response IB_WR_SEND >>>>>>>>>>>>>>>>>>>>>>\n");
  1501. return isert_post_response(isert_conn, isert_cmd);
  1502. }
  1503. static int
  1504. isert_build_rdma_wr(struct isert_conn *isert_conn, struct isert_cmd *isert_cmd,
  1505. struct ib_sge *ib_sge, struct ib_send_wr *send_wr,
  1506. u32 data_left, u32 offset)
  1507. {
  1508. struct iscsi_cmd *cmd = &isert_cmd->iscsi_cmd;
  1509. struct scatterlist *sg_start, *tmp_sg;
  1510. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1511. u32 sg_off, page_off;
  1512. int i = 0, sg_nents;
  1513. sg_off = offset / PAGE_SIZE;
  1514. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1515. sg_nents = min(cmd->se_cmd.t_data_nents - sg_off, isert_conn->max_sge);
  1516. page_off = offset % PAGE_SIZE;
  1517. send_wr->sg_list = ib_sge;
  1518. send_wr->num_sge = sg_nents;
  1519. send_wr->wr_id = (unsigned long)&isert_cmd->tx_desc;
  1520. /*
  1521. * Perform mapping of TCM scatterlist memory ib_sge dma_addr.
  1522. */
  1523. for_each_sg(sg_start, tmp_sg, sg_nents, i) {
  1524. pr_debug("ISER RDMA from SGL dma_addr: 0x%16llx dma_len: %u, page_off: %u\n",
  1525. (unsigned long long)tmp_sg->dma_address,
  1526. tmp_sg->length, page_off);
  1527. ib_sge->addr = ib_sg_dma_address(ib_dev, tmp_sg) + page_off;
  1528. ib_sge->length = min_t(u32, data_left,
  1529. ib_sg_dma_len(ib_dev, tmp_sg) - page_off);
  1530. ib_sge->lkey = isert_conn->conn_mr->lkey;
  1531. pr_debug("RDMA ib_sge: addr: 0x%16llx length: %u\n",
  1532. ib_sge->addr, ib_sge->length);
  1533. page_off = 0;
  1534. data_left -= ib_sge->length;
  1535. ib_sge++;
  1536. pr_debug("Incrementing ib_sge pointer to %p\n", ib_sge);
  1537. }
  1538. pr_debug("Set outgoing sg_list: %p num_sg: %u from TCM SGLs\n",
  1539. send_wr->sg_list, send_wr->num_sge);
  1540. return sg_nents;
  1541. }
  1542. static int
  1543. isert_put_datain(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
  1544. {
  1545. struct se_cmd *se_cmd = &cmd->se_cmd;
  1546. struct isert_cmd *isert_cmd = container_of(cmd,
  1547. struct isert_cmd, iscsi_cmd);
  1548. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1549. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1550. struct ib_send_wr *wr_failed, *send_wr;
  1551. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1552. struct ib_sge *ib_sge;
  1553. struct scatterlist *sg;
  1554. u32 offset = 0, data_len, data_left, rdma_write_max;
  1555. int rc, ret = 0, count, sg_nents, i, ib_sge_cnt;
  1556. pr_debug("RDMA_WRITE: data_length: %u\n", se_cmd->data_length);
  1557. sg = &se_cmd->t_data_sg[0];
  1558. sg_nents = se_cmd->t_data_nents;
  1559. count = ib_dma_map_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1560. if (unlikely(!count)) {
  1561. pr_err("Unable to map put_datain SGs\n");
  1562. return -EINVAL;
  1563. }
  1564. wr->sge = sg;
  1565. wr->num_sge = sg_nents;
  1566. pr_debug("Mapped IB count: %u sg: %p sg_nents: %u for RDMA_WRITE\n",
  1567. count, sg, sg_nents);
  1568. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1569. if (!ib_sge) {
  1570. pr_warn("Unable to allocate datain ib_sge\n");
  1571. ret = -ENOMEM;
  1572. goto unmap_sg;
  1573. }
  1574. isert_cmd->ib_sge = ib_sge;
  1575. pr_debug("Allocated ib_sge: %p from t_data_ents: %d for RDMA_WRITE\n",
  1576. ib_sge, se_cmd->t_data_nents);
  1577. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1578. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1579. GFP_KERNEL);
  1580. if (!wr->send_wr) {
  1581. pr_err("Unable to allocate wr->send_wr\n");
  1582. ret = -ENOMEM;
  1583. goto unmap_sg;
  1584. }
  1585. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1586. wr->send_wr, wr->send_wr_num);
  1587. iscsit_increment_maxcmdsn(cmd, conn->sess);
  1588. cmd->stat_sn = conn->stat_sn++;
  1589. wr->isert_cmd = isert_cmd;
  1590. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1591. data_left = se_cmd->data_length;
  1592. for (i = 0; i < wr->send_wr_num; i++) {
  1593. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1594. data_len = min(data_left, rdma_write_max);
  1595. send_wr->opcode = IB_WR_RDMA_WRITE;
  1596. send_wr->send_flags = 0;
  1597. send_wr->wr.rdma.remote_addr = isert_cmd->read_va + offset;
  1598. send_wr->wr.rdma.rkey = isert_cmd->read_stag;
  1599. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1600. send_wr, data_len, offset);
  1601. ib_sge += ib_sge_cnt;
  1602. if (i + 1 == wr->send_wr_num)
  1603. send_wr->next = &isert_cmd->tx_desc.send_wr;
  1604. else
  1605. send_wr->next = &wr->send_wr[i + 1];
  1606. offset += data_len;
  1607. data_left -= data_len;
  1608. }
  1609. /*
  1610. * Build isert_conn->tx_desc for iSCSI response PDU and attach
  1611. */
  1612. isert_create_send_desc(isert_conn, isert_cmd, &isert_cmd->tx_desc);
  1613. iscsit_build_rsp_pdu(cmd, conn, false, (struct iscsi_scsi_rsp *)
  1614. &isert_cmd->tx_desc.iscsi_header);
  1615. isert_init_tx_hdrs(isert_conn, &isert_cmd->tx_desc);
  1616. isert_init_send_wr(isert_cmd, &isert_cmd->tx_desc.send_wr);
  1617. atomic_inc(&isert_conn->post_send_buf_count);
  1618. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1619. if (rc) {
  1620. pr_warn("ib_post_send() failed for IB_WR_RDMA_WRITE\n");
  1621. atomic_dec(&isert_conn->post_send_buf_count);
  1622. }
  1623. pr_debug("Posted RDMA_WRITE + Response for iSER Data READ\n");
  1624. return 1;
  1625. unmap_sg:
  1626. ib_dma_unmap_sg(ib_dev, sg, sg_nents, DMA_TO_DEVICE);
  1627. return ret;
  1628. }
  1629. static int
  1630. isert_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, bool recovery)
  1631. {
  1632. struct se_cmd *se_cmd = &cmd->se_cmd;
  1633. struct isert_cmd *isert_cmd = container_of(cmd,
  1634. struct isert_cmd, iscsi_cmd);
  1635. struct isert_rdma_wr *wr = &isert_cmd->rdma_wr;
  1636. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1637. struct ib_send_wr *wr_failed, *send_wr;
  1638. struct ib_sge *ib_sge;
  1639. struct ib_device *ib_dev = isert_conn->conn_cm_id->device;
  1640. struct scatterlist *sg_start;
  1641. u32 sg_off, sg_nents, page_off, va_offset = 0;
  1642. u32 offset = 0, data_len, data_left, rdma_write_max;
  1643. int rc, ret = 0, count, i, ib_sge_cnt;
  1644. pr_debug("RDMA_READ: data_length: %u write_data_done: %u\n",
  1645. se_cmd->data_length, cmd->write_data_done);
  1646. sg_off = cmd->write_data_done / PAGE_SIZE;
  1647. sg_start = &cmd->se_cmd.t_data_sg[sg_off];
  1648. page_off = cmd->write_data_done % PAGE_SIZE;
  1649. pr_debug("RDMA_READ: sg_off: %d, sg_start: %p page_off: %d\n",
  1650. sg_off, sg_start, page_off);
  1651. data_left = se_cmd->data_length - cmd->write_data_done;
  1652. sg_nents = se_cmd->t_data_nents - sg_off;
  1653. pr_debug("RDMA_READ: data_left: %d, sg_nents: %d\n",
  1654. data_left, sg_nents);
  1655. count = ib_dma_map_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1656. if (unlikely(!count)) {
  1657. pr_err("Unable to map get_dataout SGs\n");
  1658. return -EINVAL;
  1659. }
  1660. wr->sge = sg_start;
  1661. wr->num_sge = sg_nents;
  1662. pr_debug("Mapped IB count: %u sg_start: %p sg_nents: %u for RDMA_READ\n",
  1663. count, sg_start, sg_nents);
  1664. ib_sge = kzalloc(sizeof(struct ib_sge) * sg_nents, GFP_KERNEL);
  1665. if (!ib_sge) {
  1666. pr_warn("Unable to allocate dataout ib_sge\n");
  1667. ret = -ENOMEM;
  1668. goto unmap_sg;
  1669. }
  1670. isert_cmd->ib_sge = ib_sge;
  1671. pr_debug("Using ib_sge: %p from sg_ents: %d for RDMA_READ\n",
  1672. ib_sge, sg_nents);
  1673. wr->send_wr_num = DIV_ROUND_UP(sg_nents, isert_conn->max_sge);
  1674. wr->send_wr = kzalloc(sizeof(struct ib_send_wr) * wr->send_wr_num,
  1675. GFP_KERNEL);
  1676. if (!wr->send_wr) {
  1677. pr_debug("Unable to allocate wr->send_wr\n");
  1678. ret = -ENOMEM;
  1679. goto unmap_sg;
  1680. }
  1681. pr_debug("Allocated wr->send_wr: %p wr->send_wr_num: %u\n",
  1682. wr->send_wr, wr->send_wr_num);
  1683. isert_cmd->tx_desc.isert_cmd = isert_cmd;
  1684. wr->iser_ib_op = ISER_IB_RDMA_READ;
  1685. wr->isert_cmd = isert_cmd;
  1686. rdma_write_max = isert_conn->max_sge * PAGE_SIZE;
  1687. offset = cmd->write_data_done;
  1688. for (i = 0; i < wr->send_wr_num; i++) {
  1689. send_wr = &isert_cmd->rdma_wr.send_wr[i];
  1690. data_len = min(data_left, rdma_write_max);
  1691. send_wr->opcode = IB_WR_RDMA_READ;
  1692. send_wr->wr.rdma.remote_addr = isert_cmd->write_va + va_offset;
  1693. send_wr->wr.rdma.rkey = isert_cmd->write_stag;
  1694. ib_sge_cnt = isert_build_rdma_wr(isert_conn, isert_cmd, ib_sge,
  1695. send_wr, data_len, offset);
  1696. ib_sge += ib_sge_cnt;
  1697. if (i + 1 == wr->send_wr_num)
  1698. send_wr->send_flags = IB_SEND_SIGNALED;
  1699. else
  1700. send_wr->next = &wr->send_wr[i + 1];
  1701. offset += data_len;
  1702. va_offset += data_len;
  1703. data_left -= data_len;
  1704. }
  1705. atomic_inc(&isert_conn->post_send_buf_count);
  1706. rc = ib_post_send(isert_conn->conn_qp, wr->send_wr, &wr_failed);
  1707. if (rc) {
  1708. pr_warn("ib_post_send() failed for IB_WR_RDMA_READ\n");
  1709. atomic_dec(&isert_conn->post_send_buf_count);
  1710. }
  1711. pr_debug("Posted RDMA_READ memory for ISER Data WRITE\n");
  1712. return 0;
  1713. unmap_sg:
  1714. ib_dma_unmap_sg(ib_dev, sg_start, sg_nents, DMA_FROM_DEVICE);
  1715. return ret;
  1716. }
  1717. static int
  1718. isert_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1719. {
  1720. int ret;
  1721. switch (state) {
  1722. case ISTATE_SEND_NOPIN_WANT_RESPONSE:
  1723. ret = isert_put_nopin(cmd, conn, false);
  1724. break;
  1725. default:
  1726. pr_err("Unknown immediate state: 0x%02x\n", state);
  1727. ret = -EINVAL;
  1728. break;
  1729. }
  1730. return ret;
  1731. }
  1732. static int
  1733. isert_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
  1734. {
  1735. int ret;
  1736. switch (state) {
  1737. case ISTATE_SEND_LOGOUTRSP:
  1738. ret = isert_put_logout_rsp(cmd, conn);
  1739. if (!ret) {
  1740. pr_debug("Returning iSER Logout -EAGAIN\n");
  1741. ret = -EAGAIN;
  1742. }
  1743. break;
  1744. case ISTATE_SEND_NOPIN:
  1745. ret = isert_put_nopin(cmd, conn, true);
  1746. break;
  1747. case ISTATE_SEND_TASKMGTRSP:
  1748. ret = isert_put_tm_rsp(cmd, conn);
  1749. break;
  1750. case ISTATE_SEND_REJECT:
  1751. ret = isert_put_reject(cmd, conn);
  1752. break;
  1753. case ISTATE_SEND_TEXTRSP:
  1754. ret = isert_put_text_rsp(cmd, conn);
  1755. break;
  1756. case ISTATE_SEND_STATUS:
  1757. /*
  1758. * Special case for sending non GOOD SCSI status from TX thread
  1759. * context during pre se_cmd excecution failure.
  1760. */
  1761. ret = isert_put_response(conn, cmd);
  1762. break;
  1763. default:
  1764. pr_err("Unknown response state: 0x%02x\n", state);
  1765. ret = -EINVAL;
  1766. break;
  1767. }
  1768. return ret;
  1769. }
  1770. static int
  1771. isert_setup_np(struct iscsi_np *np,
  1772. struct __kernel_sockaddr_storage *ksockaddr)
  1773. {
  1774. struct isert_np *isert_np;
  1775. struct rdma_cm_id *isert_lid;
  1776. struct sockaddr *sa;
  1777. int ret;
  1778. isert_np = kzalloc(sizeof(struct isert_np), GFP_KERNEL);
  1779. if (!isert_np) {
  1780. pr_err("Unable to allocate struct isert_np\n");
  1781. return -ENOMEM;
  1782. }
  1783. init_waitqueue_head(&isert_np->np_accept_wq);
  1784. mutex_init(&isert_np->np_accept_mutex);
  1785. INIT_LIST_HEAD(&isert_np->np_accept_list);
  1786. init_completion(&isert_np->np_login_comp);
  1787. sa = (struct sockaddr *)ksockaddr;
  1788. pr_debug("ksockaddr: %p, sa: %p\n", ksockaddr, sa);
  1789. /*
  1790. * Setup the np->np_sockaddr from the passed sockaddr setup
  1791. * in iscsi_target_configfs.c code..
  1792. */
  1793. memcpy(&np->np_sockaddr, ksockaddr,
  1794. sizeof(struct __kernel_sockaddr_storage));
  1795. isert_lid = rdma_create_id(isert_cma_handler, np, RDMA_PS_TCP,
  1796. IB_QPT_RC);
  1797. if (IS_ERR(isert_lid)) {
  1798. pr_err("rdma_create_id() for isert_listen_handler failed: %ld\n",
  1799. PTR_ERR(isert_lid));
  1800. ret = PTR_ERR(isert_lid);
  1801. goto out;
  1802. }
  1803. ret = rdma_bind_addr(isert_lid, sa);
  1804. if (ret) {
  1805. pr_err("rdma_bind_addr() for isert_lid failed: %d\n", ret);
  1806. goto out_lid;
  1807. }
  1808. ret = rdma_listen(isert_lid, ISERT_RDMA_LISTEN_BACKLOG);
  1809. if (ret) {
  1810. pr_err("rdma_listen() for isert_lid failed: %d\n", ret);
  1811. goto out_lid;
  1812. }
  1813. isert_np->np_cm_id = isert_lid;
  1814. np->np_context = isert_np;
  1815. pr_debug("Setup isert_lid->context: %p\n", isert_lid->context);
  1816. return 0;
  1817. out_lid:
  1818. rdma_destroy_id(isert_lid);
  1819. out:
  1820. kfree(isert_np);
  1821. return ret;
  1822. }
  1823. static int
  1824. isert_check_accept_queue(struct isert_np *isert_np)
  1825. {
  1826. int empty;
  1827. mutex_lock(&isert_np->np_accept_mutex);
  1828. empty = list_empty(&isert_np->np_accept_list);
  1829. mutex_unlock(&isert_np->np_accept_mutex);
  1830. return empty;
  1831. }
  1832. static int
  1833. isert_rdma_accept(struct isert_conn *isert_conn)
  1834. {
  1835. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1836. struct rdma_conn_param cp;
  1837. int ret;
  1838. memset(&cp, 0, sizeof(struct rdma_conn_param));
  1839. cp.responder_resources = isert_conn->responder_resources;
  1840. cp.initiator_depth = isert_conn->initiator_depth;
  1841. cp.retry_count = 7;
  1842. cp.rnr_retry_count = 7;
  1843. pr_debug("Before rdma_accept >>>>>>>>>>>>>>>>>>>>.\n");
  1844. ret = rdma_accept(cm_id, &cp);
  1845. if (ret) {
  1846. pr_err("rdma_accept() failed with: %d\n", ret);
  1847. return ret;
  1848. }
  1849. pr_debug("After rdma_accept >>>>>>>>>>>>>>>>>>>>>.\n");
  1850. return 0;
  1851. }
  1852. static int
  1853. isert_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
  1854. {
  1855. struct isert_conn *isert_conn = (struct isert_conn *)conn->context;
  1856. int ret;
  1857. pr_debug("isert_get_login_rx before conn_login_comp conn: %p\n", conn);
  1858. ret = wait_for_completion_interruptible(&isert_conn->conn_login_comp);
  1859. if (ret)
  1860. return ret;
  1861. pr_debug("isert_get_login_rx processing login->req: %p\n", login->req);
  1862. return 0;
  1863. }
  1864. static void
  1865. isert_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
  1866. struct isert_conn *isert_conn)
  1867. {
  1868. struct rdma_cm_id *cm_id = isert_conn->conn_cm_id;
  1869. struct rdma_route *cm_route = &cm_id->route;
  1870. struct sockaddr_in *sock_in;
  1871. struct sockaddr_in6 *sock_in6;
  1872. conn->login_family = np->np_sockaddr.ss_family;
  1873. if (np->np_sockaddr.ss_family == AF_INET6) {
  1874. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.dst_addr;
  1875. snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
  1876. &sock_in6->sin6_addr.in6_u);
  1877. conn->login_port = ntohs(sock_in6->sin6_port);
  1878. sock_in6 = (struct sockaddr_in6 *)&cm_route->addr.src_addr;
  1879. snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
  1880. &sock_in6->sin6_addr.in6_u);
  1881. conn->local_port = ntohs(sock_in6->sin6_port);
  1882. } else {
  1883. sock_in = (struct sockaddr_in *)&cm_route->addr.dst_addr;
  1884. sprintf(conn->login_ip, "%pI4",
  1885. &sock_in->sin_addr.s_addr);
  1886. conn->login_port = ntohs(sock_in->sin_port);
  1887. sock_in = (struct sockaddr_in *)&cm_route->addr.src_addr;
  1888. sprintf(conn->local_ip, "%pI4",
  1889. &sock_in->sin_addr.s_addr);
  1890. conn->local_port = ntohs(sock_in->sin_port);
  1891. }
  1892. }
  1893. static int
  1894. isert_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
  1895. {
  1896. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1897. struct isert_conn *isert_conn;
  1898. int max_accept = 0, ret;
  1899. accept_wait:
  1900. ret = wait_event_interruptible(isert_np->np_accept_wq,
  1901. !isert_check_accept_queue(isert_np) ||
  1902. np->np_thread_state == ISCSI_NP_THREAD_RESET);
  1903. if (max_accept > 5)
  1904. return -ENODEV;
  1905. spin_lock_bh(&np->np_thread_lock);
  1906. if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
  1907. spin_unlock_bh(&np->np_thread_lock);
  1908. pr_err("ISCSI_NP_THREAD_RESET for isert_accept_np\n");
  1909. return -ENODEV;
  1910. }
  1911. spin_unlock_bh(&np->np_thread_lock);
  1912. mutex_lock(&isert_np->np_accept_mutex);
  1913. if (list_empty(&isert_np->np_accept_list)) {
  1914. mutex_unlock(&isert_np->np_accept_mutex);
  1915. max_accept++;
  1916. goto accept_wait;
  1917. }
  1918. isert_conn = list_first_entry(&isert_np->np_accept_list,
  1919. struct isert_conn, conn_accept_node);
  1920. list_del_init(&isert_conn->conn_accept_node);
  1921. mutex_unlock(&isert_np->np_accept_mutex);
  1922. conn->context = isert_conn;
  1923. isert_conn->conn = conn;
  1924. max_accept = 0;
  1925. ret = isert_rdma_post_recvl(isert_conn);
  1926. if (ret)
  1927. return ret;
  1928. ret = isert_rdma_accept(isert_conn);
  1929. if (ret)
  1930. return ret;
  1931. isert_set_conn_info(np, conn, isert_conn);
  1932. pr_debug("Processing isert_accept_np: isert_conn: %p\n", isert_conn);
  1933. return 0;
  1934. }
  1935. static void
  1936. isert_free_np(struct iscsi_np *np)
  1937. {
  1938. struct isert_np *isert_np = (struct isert_np *)np->np_context;
  1939. rdma_destroy_id(isert_np->np_cm_id);
  1940. np->np_context = NULL;
  1941. kfree(isert_np);
  1942. }
  1943. static int isert_check_state(struct isert_conn *isert_conn, int state)
  1944. {
  1945. int ret;
  1946. mutex_lock(&isert_conn->conn_mutex);
  1947. ret = (isert_conn->state == state);
  1948. mutex_unlock(&isert_conn->conn_mutex);
  1949. return ret;
  1950. }
  1951. static void isert_free_conn(struct iscsi_conn *conn)
  1952. {
  1953. struct isert_conn *isert_conn = conn->context;
  1954. pr_debug("isert_free_conn: Starting \n");
  1955. /*
  1956. * Decrement post_send_buf_count for special case when called
  1957. * from isert_do_control_comp() -> iscsit_logout_post_handler()
  1958. */
  1959. mutex_lock(&isert_conn->conn_mutex);
  1960. if (isert_conn->logout_posted)
  1961. atomic_dec(&isert_conn->post_send_buf_count);
  1962. if (isert_conn->conn_cm_id && isert_conn->state != ISER_CONN_DOWN) {
  1963. pr_debug("Calling rdma_disconnect from isert_free_conn\n");
  1964. rdma_disconnect(isert_conn->conn_cm_id);
  1965. }
  1966. /*
  1967. * Only wait for conn_wait_comp_err if the isert_conn made it
  1968. * into full feature phase..
  1969. */
  1970. if (isert_conn->state == ISER_CONN_UP) {
  1971. pr_debug("isert_free_conn: Before wait_event comp_err %d\n",
  1972. isert_conn->state);
  1973. mutex_unlock(&isert_conn->conn_mutex);
  1974. wait_event(isert_conn->conn_wait_comp_err,
  1975. (isert_check_state(isert_conn, ISER_CONN_TERMINATING)));
  1976. wait_event(isert_conn->conn_wait,
  1977. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  1978. isert_put_conn(isert_conn);
  1979. return;
  1980. }
  1981. if (isert_conn->state == ISER_CONN_INIT) {
  1982. mutex_unlock(&isert_conn->conn_mutex);
  1983. isert_put_conn(isert_conn);
  1984. return;
  1985. }
  1986. pr_debug("isert_free_conn: wait_event conn_wait %d\n",
  1987. isert_conn->state);
  1988. mutex_unlock(&isert_conn->conn_mutex);
  1989. wait_event(isert_conn->conn_wait,
  1990. (isert_check_state(isert_conn, ISER_CONN_DOWN)));
  1991. isert_put_conn(isert_conn);
  1992. }
  1993. static struct iscsit_transport iser_target_transport = {
  1994. .name = "IB/iSER",
  1995. .transport_type = ISCSI_INFINIBAND,
  1996. .owner = THIS_MODULE,
  1997. .iscsit_setup_np = isert_setup_np,
  1998. .iscsit_accept_np = isert_accept_np,
  1999. .iscsit_free_np = isert_free_np,
  2000. .iscsit_free_conn = isert_free_conn,
  2001. .iscsit_alloc_cmd = isert_alloc_cmd,
  2002. .iscsit_get_login_rx = isert_get_login_rx,
  2003. .iscsit_put_login_tx = isert_put_login_tx,
  2004. .iscsit_immediate_queue = isert_immediate_queue,
  2005. .iscsit_response_queue = isert_response_queue,
  2006. .iscsit_get_dataout = isert_get_dataout,
  2007. .iscsit_queue_data_in = isert_put_datain,
  2008. .iscsit_queue_status = isert_put_response,
  2009. };
  2010. static int __init isert_init(void)
  2011. {
  2012. int ret;
  2013. isert_rx_wq = alloc_workqueue("isert_rx_wq", 0, 0);
  2014. if (!isert_rx_wq) {
  2015. pr_err("Unable to allocate isert_rx_wq\n");
  2016. return -ENOMEM;
  2017. }
  2018. isert_comp_wq = alloc_workqueue("isert_comp_wq", 0, 0);
  2019. if (!isert_comp_wq) {
  2020. pr_err("Unable to allocate isert_comp_wq\n");
  2021. ret = -ENOMEM;
  2022. goto destroy_rx_wq;
  2023. }
  2024. isert_cmd_cache = kmem_cache_create("isert_cmd_cache",
  2025. sizeof(struct isert_cmd), __alignof__(struct isert_cmd),
  2026. 0, NULL);
  2027. if (!isert_cmd_cache) {
  2028. pr_err("Unable to create isert_cmd_cache\n");
  2029. ret = -ENOMEM;
  2030. goto destroy_tx_cq;
  2031. }
  2032. iscsit_register_transport(&iser_target_transport);
  2033. pr_debug("iSER_TARGET[0] - Loaded iser_target_transport\n");
  2034. return 0;
  2035. destroy_tx_cq:
  2036. destroy_workqueue(isert_comp_wq);
  2037. destroy_rx_wq:
  2038. destroy_workqueue(isert_rx_wq);
  2039. return ret;
  2040. }
  2041. static void __exit isert_exit(void)
  2042. {
  2043. kmem_cache_destroy(isert_cmd_cache);
  2044. destroy_workqueue(isert_comp_wq);
  2045. destroy_workqueue(isert_rx_wq);
  2046. iscsit_unregister_transport(&iser_target_transport);
  2047. pr_debug("iSER_TARGET[0] - Released iser_target_transport\n");
  2048. }
  2049. MODULE_DESCRIPTION("iSER-Target for mainline target infrastructure");
  2050. MODULE_VERSION("0.1");
  2051. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  2052. MODULE_LICENSE("GPL");
  2053. module_init(isert_init);
  2054. module_exit(isert_exit);