ib_srpt.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <scsi/scsi_proto.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_fabric.h>
  47. #include "ib_srpt.h"
  48. /* Name of this kernel module. */
  49. #define DRV_NAME "ib_srpt"
  50. #define DRV_VERSION "2.0.0"
  51. #define DRV_RELDATE "2011-02-14"
  52. #define SRPT_ID_STRING "Linux SRP target"
  53. #undef pr_fmt
  54. #define pr_fmt(fmt) DRV_NAME " " fmt
  55. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  56. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
  57. "v" DRV_VERSION " (" DRV_RELDATE ")");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. /*
  60. * Global Variables
  61. */
  62. static u64 srpt_service_guid;
  63. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  64. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  65. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  66. module_param(srp_max_req_size, int, 0444);
  67. MODULE_PARM_DESC(srp_max_req_size,
  68. "Maximum size of SRP request messages in bytes.");
  69. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  70. module_param(srpt_srq_size, int, 0444);
  71. MODULE_PARM_DESC(srpt_srq_size,
  72. "Shared receive queue (SRQ) size.");
  73. static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
  74. {
  75. return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
  76. }
  77. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  78. 0444);
  79. MODULE_PARM_DESC(srpt_service_guid,
  80. "Using this value for ioc_guid, id_ext, and cm_listen_id"
  81. " instead of using the node_guid of the first HCA.");
  82. static struct ib_client srpt_client;
  83. static void srpt_release_cmd(struct se_cmd *se_cmd);
  84. static void srpt_free_ch(struct kref *kref);
  85. static int srpt_queue_status(struct se_cmd *cmd);
  86. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  87. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
  88. static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
  89. /*
  90. * The only allowed channel state changes are those that change the channel
  91. * state into a state with a higher numerical value. Hence the new > prev test.
  92. */
  93. static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
  94. {
  95. unsigned long flags;
  96. enum rdma_ch_state prev;
  97. bool changed = false;
  98. spin_lock_irqsave(&ch->spinlock, flags);
  99. prev = ch->state;
  100. if (new > prev) {
  101. ch->state = new;
  102. changed = true;
  103. }
  104. spin_unlock_irqrestore(&ch->spinlock, flags);
  105. return changed;
  106. }
  107. /**
  108. * srpt_event_handler() - Asynchronous IB event callback function.
  109. *
  110. * Callback function called by the InfiniBand core when an asynchronous IB
  111. * event occurs. This callback may occur in interrupt context. See also
  112. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  113. * Architecture Specification.
  114. */
  115. static void srpt_event_handler(struct ib_event_handler *handler,
  116. struct ib_event *event)
  117. {
  118. struct srpt_device *sdev;
  119. struct srpt_port *sport;
  120. sdev = ib_get_client_data(event->device, &srpt_client);
  121. if (!sdev || sdev->device != event->device)
  122. return;
  123. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  124. sdev->device->name);
  125. switch (event->event) {
  126. case IB_EVENT_PORT_ERR:
  127. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  128. sport = &sdev->port[event->element.port_num - 1];
  129. sport->lid = 0;
  130. sport->sm_lid = 0;
  131. }
  132. break;
  133. case IB_EVENT_PORT_ACTIVE:
  134. case IB_EVENT_LID_CHANGE:
  135. case IB_EVENT_PKEY_CHANGE:
  136. case IB_EVENT_SM_CHANGE:
  137. case IB_EVENT_CLIENT_REREGISTER:
  138. case IB_EVENT_GID_CHANGE:
  139. /* Refresh port data asynchronously. */
  140. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  141. sport = &sdev->port[event->element.port_num - 1];
  142. if (!sport->lid && !sport->sm_lid)
  143. schedule_work(&sport->work);
  144. }
  145. break;
  146. default:
  147. pr_err("received unrecognized IB event %d\n",
  148. event->event);
  149. break;
  150. }
  151. }
  152. /**
  153. * srpt_srq_event() - SRQ event callback function.
  154. */
  155. static void srpt_srq_event(struct ib_event *event, void *ctx)
  156. {
  157. pr_info("SRQ event %d\n", event->event);
  158. }
  159. static const char *get_ch_state_name(enum rdma_ch_state s)
  160. {
  161. switch (s) {
  162. case CH_CONNECTING:
  163. return "connecting";
  164. case CH_LIVE:
  165. return "live";
  166. case CH_DISCONNECTING:
  167. return "disconnecting";
  168. case CH_DRAINING:
  169. return "draining";
  170. case CH_DISCONNECTED:
  171. return "disconnected";
  172. }
  173. return "???";
  174. }
  175. /**
  176. * srpt_qp_event() - QP event callback function.
  177. */
  178. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  179. {
  180. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  181. event->event, ch->cm_id, ch->sess_name, ch->state);
  182. switch (event->event) {
  183. case IB_EVENT_COMM_EST:
  184. ib_cm_notify(ch->cm_id, event->event);
  185. break;
  186. case IB_EVENT_QP_LAST_WQE_REACHED:
  187. pr_debug("%s-%d, state %s: received Last WQE event.\n",
  188. ch->sess_name, ch->qp->qp_num,
  189. get_ch_state_name(ch->state));
  190. break;
  191. default:
  192. pr_err("received unrecognized IB QP event %d\n", event->event);
  193. break;
  194. }
  195. }
  196. /**
  197. * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
  198. *
  199. * @slot: one-based slot number.
  200. * @value: four-bit value.
  201. *
  202. * Copies the lowest four bits of value in element slot of the array of four
  203. * bit elements called c_list (controller list). The index slot is one-based.
  204. */
  205. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  206. {
  207. u16 id;
  208. u8 tmp;
  209. id = (slot - 1) / 2;
  210. if (slot & 0x1) {
  211. tmp = c_list[id] & 0xf;
  212. c_list[id] = (value << 4) | tmp;
  213. } else {
  214. tmp = c_list[id] & 0xf0;
  215. c_list[id] = (value & 0xf) | tmp;
  216. }
  217. }
  218. /**
  219. * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
  220. *
  221. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  222. * Specification.
  223. */
  224. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  225. {
  226. struct ib_class_port_info *cif;
  227. cif = (struct ib_class_port_info *)mad->data;
  228. memset(cif, 0, sizeof(*cif));
  229. cif->base_version = 1;
  230. cif->class_version = 1;
  231. ib_set_cpi_resp_time(cif, 20);
  232. mad->mad_hdr.status = 0;
  233. }
  234. /**
  235. * srpt_get_iou() - Write IOUnitInfo to a management datagram.
  236. *
  237. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  238. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  239. */
  240. static void srpt_get_iou(struct ib_dm_mad *mad)
  241. {
  242. struct ib_dm_iou_info *ioui;
  243. u8 slot;
  244. int i;
  245. ioui = (struct ib_dm_iou_info *)mad->data;
  246. ioui->change_id = cpu_to_be16(1);
  247. ioui->max_controllers = 16;
  248. /* set present for slot 1 and empty for the rest */
  249. srpt_set_ioc(ioui->controller_list, 1, 1);
  250. for (i = 1, slot = 2; i < 16; i++, slot++)
  251. srpt_set_ioc(ioui->controller_list, slot, 0);
  252. mad->mad_hdr.status = 0;
  253. }
  254. /**
  255. * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
  256. *
  257. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  258. * Architecture Specification. See also section B.7, table B.7 in the SRP
  259. * r16a document.
  260. */
  261. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  262. struct ib_dm_mad *mad)
  263. {
  264. struct srpt_device *sdev = sport->sdev;
  265. struct ib_dm_ioc_profile *iocp;
  266. iocp = (struct ib_dm_ioc_profile *)mad->data;
  267. if (!slot || slot > 16) {
  268. mad->mad_hdr.status
  269. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  270. return;
  271. }
  272. if (slot > 2) {
  273. mad->mad_hdr.status
  274. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  275. return;
  276. }
  277. memset(iocp, 0, sizeof(*iocp));
  278. strcpy(iocp->id_string, SRPT_ID_STRING);
  279. iocp->guid = cpu_to_be64(srpt_service_guid);
  280. iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  281. iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
  282. iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
  283. iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  284. iocp->subsys_device_id = 0x0;
  285. iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  286. iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
  287. iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
  288. iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
  289. iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
  290. iocp->rdma_read_depth = 4;
  291. iocp->send_size = cpu_to_be32(srp_max_req_size);
  292. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  293. 1U << 24));
  294. iocp->num_svc_entries = 1;
  295. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  296. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  297. mad->mad_hdr.status = 0;
  298. }
  299. /**
  300. * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
  301. *
  302. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  303. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  304. */
  305. static void srpt_get_svc_entries(u64 ioc_guid,
  306. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  307. {
  308. struct ib_dm_svc_entries *svc_entries;
  309. WARN_ON(!ioc_guid);
  310. if (!slot || slot > 16) {
  311. mad->mad_hdr.status
  312. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  313. return;
  314. }
  315. if (slot > 2 || lo > hi || hi > 1) {
  316. mad->mad_hdr.status
  317. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  318. return;
  319. }
  320. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  321. memset(svc_entries, 0, sizeof(*svc_entries));
  322. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  323. snprintf(svc_entries->service_entries[0].name,
  324. sizeof(svc_entries->service_entries[0].name),
  325. "%s%016llx",
  326. SRP_SERVICE_NAME_PREFIX,
  327. ioc_guid);
  328. mad->mad_hdr.status = 0;
  329. }
  330. /**
  331. * srpt_mgmt_method_get() - Process a received management datagram.
  332. * @sp: source port through which the MAD has been received.
  333. * @rq_mad: received MAD.
  334. * @rsp_mad: response MAD.
  335. */
  336. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  337. struct ib_dm_mad *rsp_mad)
  338. {
  339. u16 attr_id;
  340. u32 slot;
  341. u8 hi, lo;
  342. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  343. switch (attr_id) {
  344. case DM_ATTR_CLASS_PORT_INFO:
  345. srpt_get_class_port_info(rsp_mad);
  346. break;
  347. case DM_ATTR_IOU_INFO:
  348. srpt_get_iou(rsp_mad);
  349. break;
  350. case DM_ATTR_IOC_PROFILE:
  351. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  352. srpt_get_ioc(sp, slot, rsp_mad);
  353. break;
  354. case DM_ATTR_SVC_ENTRIES:
  355. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  356. hi = (u8) ((slot >> 8) & 0xff);
  357. lo = (u8) (slot & 0xff);
  358. slot = (u16) ((slot >> 16) & 0xffff);
  359. srpt_get_svc_entries(srpt_service_guid,
  360. slot, hi, lo, rsp_mad);
  361. break;
  362. default:
  363. rsp_mad->mad_hdr.status =
  364. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  365. break;
  366. }
  367. }
  368. /**
  369. * srpt_mad_send_handler() - Post MAD-send callback function.
  370. */
  371. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  372. struct ib_mad_send_wc *mad_wc)
  373. {
  374. ib_destroy_ah(mad_wc->send_buf->ah);
  375. ib_free_send_mad(mad_wc->send_buf);
  376. }
  377. /**
  378. * srpt_mad_recv_handler() - MAD reception callback function.
  379. */
  380. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  381. struct ib_mad_send_buf *send_buf,
  382. struct ib_mad_recv_wc *mad_wc)
  383. {
  384. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  385. struct ib_ah *ah;
  386. struct ib_mad_send_buf *rsp;
  387. struct ib_dm_mad *dm_mad;
  388. if (!mad_wc || !mad_wc->recv_buf.mad)
  389. return;
  390. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  391. mad_wc->recv_buf.grh, mad_agent->port_num);
  392. if (IS_ERR(ah))
  393. goto err;
  394. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  395. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  396. mad_wc->wc->pkey_index, 0,
  397. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  398. GFP_KERNEL,
  399. IB_MGMT_BASE_VERSION);
  400. if (IS_ERR(rsp))
  401. goto err_rsp;
  402. rsp->ah = ah;
  403. dm_mad = rsp->mad;
  404. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
  405. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  406. dm_mad->mad_hdr.status = 0;
  407. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  408. case IB_MGMT_METHOD_GET:
  409. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  410. break;
  411. case IB_MGMT_METHOD_SET:
  412. dm_mad->mad_hdr.status =
  413. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  414. break;
  415. default:
  416. dm_mad->mad_hdr.status =
  417. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  418. break;
  419. }
  420. if (!ib_post_send_mad(rsp, NULL)) {
  421. ib_free_recv_mad(mad_wc);
  422. /* will destroy_ah & free_send_mad in send completion */
  423. return;
  424. }
  425. ib_free_send_mad(rsp);
  426. err_rsp:
  427. ib_destroy_ah(ah);
  428. err:
  429. ib_free_recv_mad(mad_wc);
  430. }
  431. /**
  432. * srpt_refresh_port() - Configure a HCA port.
  433. *
  434. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  435. * lid and gid values, and register a callback function for processing MADs
  436. * on the specified port.
  437. *
  438. * Note: It is safe to call this function more than once for the same port.
  439. */
  440. static int srpt_refresh_port(struct srpt_port *sport)
  441. {
  442. struct ib_mad_reg_req reg_req;
  443. struct ib_port_modify port_modify;
  444. struct ib_port_attr port_attr;
  445. int ret;
  446. memset(&port_modify, 0, sizeof(port_modify));
  447. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  448. port_modify.clr_port_cap_mask = 0;
  449. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  450. if (ret)
  451. goto err_mod_port;
  452. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  453. if (ret)
  454. goto err_query_port;
  455. sport->sm_lid = port_attr.sm_lid;
  456. sport->lid = port_attr.lid;
  457. ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
  458. NULL);
  459. if (ret)
  460. goto err_query_port;
  461. snprintf(sport->port_guid, sizeof(sport->port_guid),
  462. "0x%016llx%016llx",
  463. be64_to_cpu(sport->gid.global.subnet_prefix),
  464. be64_to_cpu(sport->gid.global.interface_id));
  465. if (!sport->mad_agent) {
  466. memset(&reg_req, 0, sizeof(reg_req));
  467. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  468. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  469. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  470. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  471. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  472. sport->port,
  473. IB_QPT_GSI,
  474. &reg_req, 0,
  475. srpt_mad_send_handler,
  476. srpt_mad_recv_handler,
  477. sport, 0);
  478. if (IS_ERR(sport->mad_agent)) {
  479. ret = PTR_ERR(sport->mad_agent);
  480. sport->mad_agent = NULL;
  481. goto err_query_port;
  482. }
  483. }
  484. return 0;
  485. err_query_port:
  486. port_modify.set_port_cap_mask = 0;
  487. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  488. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  489. err_mod_port:
  490. return ret;
  491. }
  492. /**
  493. * srpt_unregister_mad_agent() - Unregister MAD callback functions.
  494. *
  495. * Note: It is safe to call this function more than once for the same device.
  496. */
  497. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  498. {
  499. struct ib_port_modify port_modify = {
  500. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  501. };
  502. struct srpt_port *sport;
  503. int i;
  504. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  505. sport = &sdev->port[i - 1];
  506. WARN_ON(sport->port != i);
  507. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  508. pr_err("disabling MAD processing failed.\n");
  509. if (sport->mad_agent) {
  510. ib_unregister_mad_agent(sport->mad_agent);
  511. sport->mad_agent = NULL;
  512. }
  513. }
  514. }
  515. /**
  516. * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
  517. */
  518. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  519. int ioctx_size, int dma_size,
  520. enum dma_data_direction dir)
  521. {
  522. struct srpt_ioctx *ioctx;
  523. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  524. if (!ioctx)
  525. goto err;
  526. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  527. if (!ioctx->buf)
  528. goto err_free_ioctx;
  529. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  530. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  531. goto err_free_buf;
  532. return ioctx;
  533. err_free_buf:
  534. kfree(ioctx->buf);
  535. err_free_ioctx:
  536. kfree(ioctx);
  537. err:
  538. return NULL;
  539. }
  540. /**
  541. * srpt_free_ioctx() - Free an SRPT I/O context structure.
  542. */
  543. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  544. int dma_size, enum dma_data_direction dir)
  545. {
  546. if (!ioctx)
  547. return;
  548. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  549. kfree(ioctx->buf);
  550. kfree(ioctx);
  551. }
  552. /**
  553. * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
  554. * @sdev: Device to allocate the I/O context ring for.
  555. * @ring_size: Number of elements in the I/O context ring.
  556. * @ioctx_size: I/O context size.
  557. * @dma_size: DMA buffer size.
  558. * @dir: DMA data direction.
  559. */
  560. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  561. int ring_size, int ioctx_size,
  562. int dma_size, enum dma_data_direction dir)
  563. {
  564. struct srpt_ioctx **ring;
  565. int i;
  566. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  567. && ioctx_size != sizeof(struct srpt_send_ioctx));
  568. ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
  569. if (!ring)
  570. goto out;
  571. for (i = 0; i < ring_size; ++i) {
  572. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  573. if (!ring[i])
  574. goto err;
  575. ring[i]->index = i;
  576. }
  577. goto out;
  578. err:
  579. while (--i >= 0)
  580. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  581. kfree(ring);
  582. ring = NULL;
  583. out:
  584. return ring;
  585. }
  586. /**
  587. * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
  588. */
  589. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  590. struct srpt_device *sdev, int ring_size,
  591. int dma_size, enum dma_data_direction dir)
  592. {
  593. int i;
  594. for (i = 0; i < ring_size; ++i)
  595. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  596. kfree(ioctx_ring);
  597. }
  598. /**
  599. * srpt_get_cmd_state() - Get the state of a SCSI command.
  600. */
  601. static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
  602. {
  603. enum srpt_command_state state;
  604. unsigned long flags;
  605. BUG_ON(!ioctx);
  606. spin_lock_irqsave(&ioctx->spinlock, flags);
  607. state = ioctx->state;
  608. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  609. return state;
  610. }
  611. /**
  612. * srpt_set_cmd_state() - Set the state of a SCSI command.
  613. *
  614. * Does not modify the state of aborted commands. Returns the previous command
  615. * state.
  616. */
  617. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  618. enum srpt_command_state new)
  619. {
  620. enum srpt_command_state previous;
  621. unsigned long flags;
  622. BUG_ON(!ioctx);
  623. spin_lock_irqsave(&ioctx->spinlock, flags);
  624. previous = ioctx->state;
  625. if (previous != SRPT_STATE_DONE)
  626. ioctx->state = new;
  627. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  628. return previous;
  629. }
  630. /**
  631. * srpt_test_and_set_cmd_state() - Test and set the state of a command.
  632. *
  633. * Returns true if and only if the previous command state was equal to 'old'.
  634. */
  635. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  636. enum srpt_command_state old,
  637. enum srpt_command_state new)
  638. {
  639. enum srpt_command_state previous;
  640. unsigned long flags;
  641. WARN_ON(!ioctx);
  642. WARN_ON(old == SRPT_STATE_DONE);
  643. WARN_ON(new == SRPT_STATE_NEW);
  644. spin_lock_irqsave(&ioctx->spinlock, flags);
  645. previous = ioctx->state;
  646. if (previous == old)
  647. ioctx->state = new;
  648. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  649. return previous == old;
  650. }
  651. /**
  652. * srpt_post_recv() - Post an IB receive request.
  653. */
  654. static int srpt_post_recv(struct srpt_device *sdev,
  655. struct srpt_recv_ioctx *ioctx)
  656. {
  657. struct ib_sge list;
  658. struct ib_recv_wr wr, *bad_wr;
  659. BUG_ON(!sdev);
  660. list.addr = ioctx->ioctx.dma;
  661. list.length = srp_max_req_size;
  662. list.lkey = sdev->pd->local_dma_lkey;
  663. ioctx->ioctx.cqe.done = srpt_recv_done;
  664. wr.wr_cqe = &ioctx->ioctx.cqe;
  665. wr.next = NULL;
  666. wr.sg_list = &list;
  667. wr.num_sge = 1;
  668. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  669. }
  670. /**
  671. * srpt_zerolength_write() - Perform a zero-length RDMA write.
  672. *
  673. * A quote from the InfiniBand specification: C9-88: For an HCA responder
  674. * using Reliable Connection service, for each zero-length RDMA READ or WRITE
  675. * request, the R_Key shall not be validated, even if the request includes
  676. * Immediate data.
  677. */
  678. static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
  679. {
  680. struct ib_send_wr wr, *bad_wr;
  681. memset(&wr, 0, sizeof(wr));
  682. wr.opcode = IB_WR_RDMA_WRITE;
  683. wr.wr_cqe = &ch->zw_cqe;
  684. wr.send_flags = IB_SEND_SIGNALED;
  685. return ib_post_send(ch->qp, &wr, &bad_wr);
  686. }
  687. static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
  688. {
  689. struct srpt_rdma_ch *ch = cq->cq_context;
  690. if (wc->status == IB_WC_SUCCESS) {
  691. srpt_process_wait_list(ch);
  692. } else {
  693. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  694. schedule_work(&ch->release_work);
  695. else
  696. WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
  697. }
  698. }
  699. static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
  700. struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
  701. unsigned *sg_cnt)
  702. {
  703. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  704. struct srpt_rdma_ch *ch = ioctx->ch;
  705. struct scatterlist *prev = NULL;
  706. unsigned prev_nents;
  707. int ret, i;
  708. if (nbufs == 1) {
  709. ioctx->rw_ctxs = &ioctx->s_rw_ctx;
  710. } else {
  711. ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
  712. GFP_KERNEL);
  713. if (!ioctx->rw_ctxs)
  714. return -ENOMEM;
  715. }
  716. for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
  717. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  718. u64 remote_addr = be64_to_cpu(db->va);
  719. u32 size = be32_to_cpu(db->len);
  720. u32 rkey = be32_to_cpu(db->key);
  721. ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
  722. i < nbufs - 1);
  723. if (ret)
  724. goto unwind;
  725. ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
  726. ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
  727. if (ret < 0) {
  728. target_free_sgl(ctx->sg, ctx->nents);
  729. goto unwind;
  730. }
  731. ioctx->n_rdma += ret;
  732. ioctx->n_rw_ctx++;
  733. if (prev) {
  734. sg_unmark_end(&prev[prev_nents - 1]);
  735. sg_chain(prev, prev_nents + 1, ctx->sg);
  736. } else {
  737. *sg = ctx->sg;
  738. }
  739. prev = ctx->sg;
  740. prev_nents = ctx->nents;
  741. *sg_cnt += ctx->nents;
  742. }
  743. return 0;
  744. unwind:
  745. while (--i >= 0) {
  746. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  747. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  748. ctx->sg, ctx->nents, dir);
  749. target_free_sgl(ctx->sg, ctx->nents);
  750. }
  751. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  752. kfree(ioctx->rw_ctxs);
  753. return ret;
  754. }
  755. static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
  756. struct srpt_send_ioctx *ioctx)
  757. {
  758. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  759. int i;
  760. for (i = 0; i < ioctx->n_rw_ctx; i++) {
  761. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  762. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  763. ctx->sg, ctx->nents, dir);
  764. target_free_sgl(ctx->sg, ctx->nents);
  765. }
  766. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  767. kfree(ioctx->rw_ctxs);
  768. }
  769. static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
  770. {
  771. /*
  772. * The pointer computations below will only be compiled correctly
  773. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  774. * whether srp_cmd::add_data has been declared as a byte pointer.
  775. */
  776. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
  777. !__same_type(srp_cmd->add_data[0], (u8)0));
  778. /*
  779. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  780. * CDB LENGTH' field are reserved and the size in bytes of this field
  781. * is four times the value specified in bits 3..7. Hence the "& ~3".
  782. */
  783. return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
  784. }
  785. /**
  786. * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
  787. * @ioctx: Pointer to the I/O context associated with the request.
  788. * @srp_cmd: Pointer to the SRP_CMD request data.
  789. * @dir: Pointer to the variable to which the transfer direction will be
  790. * written.
  791. * @data_len: Pointer to the variable to which the total data length of all
  792. * descriptors in the SRP_CMD request will be written.
  793. *
  794. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  795. *
  796. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  797. * -ENOMEM when memory allocation fails and zero upon success.
  798. */
  799. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  800. struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
  801. struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
  802. {
  803. BUG_ON(!dir);
  804. BUG_ON(!data_len);
  805. /*
  806. * The lower four bits of the buffer format field contain the DATA-IN
  807. * buffer descriptor format, and the highest four bits contain the
  808. * DATA-OUT buffer descriptor format.
  809. */
  810. if (srp_cmd->buf_fmt & 0xf)
  811. /* DATA-IN: transfer data from target to initiator (read). */
  812. *dir = DMA_FROM_DEVICE;
  813. else if (srp_cmd->buf_fmt >> 4)
  814. /* DATA-OUT: transfer data from initiator to target (write). */
  815. *dir = DMA_TO_DEVICE;
  816. else
  817. *dir = DMA_NONE;
  818. /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
  819. ioctx->cmd.data_direction = *dir;
  820. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  821. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  822. struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
  823. *data_len = be32_to_cpu(db->len);
  824. return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
  825. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  826. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  827. struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
  828. int nbufs = be32_to_cpu(idb->table_desc.len) /
  829. sizeof(struct srp_direct_buf);
  830. if (nbufs >
  831. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  832. pr_err("received unsupported SRP_CMD request"
  833. " type (%u out + %u in != %u / %zu)\n",
  834. srp_cmd->data_out_desc_cnt,
  835. srp_cmd->data_in_desc_cnt,
  836. be32_to_cpu(idb->table_desc.len),
  837. sizeof(struct srp_direct_buf));
  838. return -EINVAL;
  839. }
  840. *data_len = be32_to_cpu(idb->len);
  841. return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
  842. sg, sg_cnt);
  843. } else {
  844. *data_len = 0;
  845. return 0;
  846. }
  847. }
  848. /**
  849. * srpt_init_ch_qp() - Initialize queue pair attributes.
  850. *
  851. * Initialized the attributes of queue pair 'qp' by allowing local write,
  852. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  853. */
  854. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  855. {
  856. struct ib_qp_attr *attr;
  857. int ret;
  858. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  859. if (!attr)
  860. return -ENOMEM;
  861. attr->qp_state = IB_QPS_INIT;
  862. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
  863. IB_ACCESS_REMOTE_WRITE;
  864. attr->port_num = ch->sport->port;
  865. attr->pkey_index = 0;
  866. ret = ib_modify_qp(qp, attr,
  867. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  868. IB_QP_PKEY_INDEX);
  869. kfree(attr);
  870. return ret;
  871. }
  872. /**
  873. * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
  874. * @ch: channel of the queue pair.
  875. * @qp: queue pair to change the state of.
  876. *
  877. * Returns zero upon success and a negative value upon failure.
  878. *
  879. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  880. * If this structure ever becomes larger, it might be necessary to allocate
  881. * it dynamically instead of on the stack.
  882. */
  883. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  884. {
  885. struct ib_qp_attr qp_attr;
  886. int attr_mask;
  887. int ret;
  888. qp_attr.qp_state = IB_QPS_RTR;
  889. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  890. if (ret)
  891. goto out;
  892. qp_attr.max_dest_rd_atomic = 4;
  893. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  894. out:
  895. return ret;
  896. }
  897. /**
  898. * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
  899. * @ch: channel of the queue pair.
  900. * @qp: queue pair to change the state of.
  901. *
  902. * Returns zero upon success and a negative value upon failure.
  903. *
  904. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  905. * If this structure ever becomes larger, it might be necessary to allocate
  906. * it dynamically instead of on the stack.
  907. */
  908. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  909. {
  910. struct ib_qp_attr qp_attr;
  911. int attr_mask;
  912. int ret;
  913. qp_attr.qp_state = IB_QPS_RTS;
  914. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  915. if (ret)
  916. goto out;
  917. qp_attr.max_rd_atomic = 4;
  918. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  919. out:
  920. return ret;
  921. }
  922. /**
  923. * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
  924. */
  925. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  926. {
  927. struct ib_qp_attr qp_attr;
  928. qp_attr.qp_state = IB_QPS_ERR;
  929. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  930. }
  931. /**
  932. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  933. */
  934. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  935. {
  936. struct srpt_send_ioctx *ioctx;
  937. unsigned long flags;
  938. BUG_ON(!ch);
  939. ioctx = NULL;
  940. spin_lock_irqsave(&ch->spinlock, flags);
  941. if (!list_empty(&ch->free_list)) {
  942. ioctx = list_first_entry(&ch->free_list,
  943. struct srpt_send_ioctx, free_list);
  944. list_del(&ioctx->free_list);
  945. }
  946. spin_unlock_irqrestore(&ch->spinlock, flags);
  947. if (!ioctx)
  948. return ioctx;
  949. BUG_ON(ioctx->ch != ch);
  950. spin_lock_init(&ioctx->spinlock);
  951. ioctx->state = SRPT_STATE_NEW;
  952. ioctx->n_rdma = 0;
  953. ioctx->n_rw_ctx = 0;
  954. init_completion(&ioctx->tx_done);
  955. ioctx->queue_status_only = false;
  956. /*
  957. * transport_init_se_cmd() does not initialize all fields, so do it
  958. * here.
  959. */
  960. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  961. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  962. return ioctx;
  963. }
  964. /**
  965. * srpt_abort_cmd() - Abort a SCSI command.
  966. * @ioctx: I/O context associated with the SCSI command.
  967. * @context: Preferred execution context.
  968. */
  969. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  970. {
  971. enum srpt_command_state state;
  972. unsigned long flags;
  973. BUG_ON(!ioctx);
  974. /*
  975. * If the command is in a state where the target core is waiting for
  976. * the ib_srpt driver, change the state to the next state.
  977. */
  978. spin_lock_irqsave(&ioctx->spinlock, flags);
  979. state = ioctx->state;
  980. switch (state) {
  981. case SRPT_STATE_NEED_DATA:
  982. ioctx->state = SRPT_STATE_DATA_IN;
  983. break;
  984. case SRPT_STATE_CMD_RSP_SENT:
  985. case SRPT_STATE_MGMT_RSP_SENT:
  986. ioctx->state = SRPT_STATE_DONE;
  987. break;
  988. default:
  989. WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
  990. __func__, state);
  991. break;
  992. }
  993. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  994. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  995. ioctx->cmd.tag);
  996. switch (state) {
  997. case SRPT_STATE_NEW:
  998. case SRPT_STATE_DATA_IN:
  999. case SRPT_STATE_MGMT:
  1000. case SRPT_STATE_DONE:
  1001. /*
  1002. * Do nothing - defer abort processing until
  1003. * srpt_queue_response() is invoked.
  1004. */
  1005. break;
  1006. case SRPT_STATE_NEED_DATA:
  1007. pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
  1008. transport_generic_request_failure(&ioctx->cmd,
  1009. TCM_CHECK_CONDITION_ABORT_CMD);
  1010. break;
  1011. case SRPT_STATE_CMD_RSP_SENT:
  1012. /*
  1013. * SRP_RSP sending failed or the SRP_RSP send completion has
  1014. * not been received in time.
  1015. */
  1016. transport_generic_free_cmd(&ioctx->cmd, 0);
  1017. break;
  1018. case SRPT_STATE_MGMT_RSP_SENT:
  1019. transport_generic_free_cmd(&ioctx->cmd, 0);
  1020. break;
  1021. default:
  1022. WARN(1, "Unexpected command state (%d)", state);
  1023. break;
  1024. }
  1025. return state;
  1026. }
  1027. /**
  1028. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1029. * the data that has been transferred via IB RDMA had to be postponed until the
  1030. * check_stop_free() callback. None of this is necessary anymore and needs to
  1031. * be cleaned up.
  1032. */
  1033. static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
  1034. {
  1035. struct srpt_rdma_ch *ch = cq->cq_context;
  1036. struct srpt_send_ioctx *ioctx =
  1037. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1038. WARN_ON(ioctx->n_rdma <= 0);
  1039. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1040. ioctx->n_rdma = 0;
  1041. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1042. pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
  1043. ioctx, wc->status);
  1044. srpt_abort_cmd(ioctx);
  1045. return;
  1046. }
  1047. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1048. SRPT_STATE_DATA_IN))
  1049. target_execute_cmd(&ioctx->cmd);
  1050. else
  1051. pr_err("%s[%d]: wrong state = %d\n", __func__,
  1052. __LINE__, srpt_get_cmd_state(ioctx));
  1053. }
  1054. /**
  1055. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1056. * @ch: RDMA channel through which the request has been received.
  1057. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1058. * be built in the buffer ioctx->buf points at and hence this function will
  1059. * overwrite the request data.
  1060. * @tag: tag of the request for which this response is being generated.
  1061. * @status: value for the STATUS field of the SRP_RSP information unit.
  1062. *
  1063. * Returns the size in bytes of the SRP_RSP response.
  1064. *
  1065. * An SRP_RSP response contains a SCSI status or service response. See also
  1066. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1067. * response. See also SPC-2 for more information about sense data.
  1068. */
  1069. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1070. struct srpt_send_ioctx *ioctx, u64 tag,
  1071. int status)
  1072. {
  1073. struct srp_rsp *srp_rsp;
  1074. const u8 *sense_data;
  1075. int sense_data_len, max_sense_len;
  1076. /*
  1077. * The lowest bit of all SAM-3 status codes is zero (see also
  1078. * paragraph 5.3 in SAM-3).
  1079. */
  1080. WARN_ON(status & 1);
  1081. srp_rsp = ioctx->ioctx.buf;
  1082. BUG_ON(!srp_rsp);
  1083. sense_data = ioctx->sense_data;
  1084. sense_data_len = ioctx->cmd.scsi_sense_length;
  1085. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1086. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1087. srp_rsp->opcode = SRP_RSP;
  1088. srp_rsp->req_lim_delta =
  1089. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1090. srp_rsp->tag = tag;
  1091. srp_rsp->status = status;
  1092. if (sense_data_len) {
  1093. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1094. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1095. if (sense_data_len > max_sense_len) {
  1096. pr_warn("truncated sense data from %d to %d"
  1097. " bytes\n", sense_data_len, max_sense_len);
  1098. sense_data_len = max_sense_len;
  1099. }
  1100. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1101. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1102. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1103. }
  1104. return sizeof(*srp_rsp) + sense_data_len;
  1105. }
  1106. /**
  1107. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1108. * @ch: RDMA channel through which the request has been received.
  1109. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1110. * @rsp_code: RSP_CODE that will be stored in the response.
  1111. * @tag: Tag of the request for which this response is being generated.
  1112. *
  1113. * Returns the size in bytes of the SRP_RSP response.
  1114. *
  1115. * An SRP_RSP response contains a SCSI status or service response. See also
  1116. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1117. * response.
  1118. */
  1119. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1120. struct srpt_send_ioctx *ioctx,
  1121. u8 rsp_code, u64 tag)
  1122. {
  1123. struct srp_rsp *srp_rsp;
  1124. int resp_data_len;
  1125. int resp_len;
  1126. resp_data_len = 4;
  1127. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1128. srp_rsp = ioctx->ioctx.buf;
  1129. BUG_ON(!srp_rsp);
  1130. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1131. srp_rsp->opcode = SRP_RSP;
  1132. srp_rsp->req_lim_delta =
  1133. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1134. srp_rsp->tag = tag;
  1135. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1136. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1137. srp_rsp->data[3] = rsp_code;
  1138. return resp_len;
  1139. }
  1140. static int srpt_check_stop_free(struct se_cmd *cmd)
  1141. {
  1142. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1143. struct srpt_send_ioctx, cmd);
  1144. return target_put_sess_cmd(&ioctx->cmd);
  1145. }
  1146. /**
  1147. * srpt_handle_cmd() - Process SRP_CMD.
  1148. */
  1149. static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1150. struct srpt_recv_ioctx *recv_ioctx,
  1151. struct srpt_send_ioctx *send_ioctx)
  1152. {
  1153. struct se_cmd *cmd;
  1154. struct srp_cmd *srp_cmd;
  1155. struct scatterlist *sg = NULL;
  1156. unsigned sg_cnt = 0;
  1157. u64 data_len;
  1158. enum dma_data_direction dir;
  1159. int rc;
  1160. BUG_ON(!send_ioctx);
  1161. srp_cmd = recv_ioctx->ioctx.buf;
  1162. cmd = &send_ioctx->cmd;
  1163. cmd->tag = srp_cmd->tag;
  1164. switch (srp_cmd->task_attr) {
  1165. case SRP_CMD_SIMPLE_Q:
  1166. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1167. break;
  1168. case SRP_CMD_ORDERED_Q:
  1169. default:
  1170. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1171. break;
  1172. case SRP_CMD_HEAD_OF_Q:
  1173. cmd->sam_task_attr = TCM_HEAD_TAG;
  1174. break;
  1175. case SRP_CMD_ACA:
  1176. cmd->sam_task_attr = TCM_ACA_TAG;
  1177. break;
  1178. }
  1179. rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
  1180. &data_len);
  1181. if (rc) {
  1182. if (rc != -EAGAIN) {
  1183. pr_err("0x%llx: parsing SRP descriptor table failed.\n",
  1184. srp_cmd->tag);
  1185. }
  1186. goto release_ioctx;
  1187. }
  1188. rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
  1189. &send_ioctx->sense_data[0],
  1190. scsilun_to_int(&srp_cmd->lun), data_len,
  1191. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
  1192. sg, sg_cnt, NULL, 0, NULL, 0);
  1193. if (rc != 0) {
  1194. pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
  1195. srp_cmd->tag);
  1196. goto release_ioctx;
  1197. }
  1198. return;
  1199. release_ioctx:
  1200. send_ioctx->state = SRPT_STATE_DONE;
  1201. srpt_release_cmd(cmd);
  1202. }
  1203. static int srp_tmr_to_tcm(int fn)
  1204. {
  1205. switch (fn) {
  1206. case SRP_TSK_ABORT_TASK:
  1207. return TMR_ABORT_TASK;
  1208. case SRP_TSK_ABORT_TASK_SET:
  1209. return TMR_ABORT_TASK_SET;
  1210. case SRP_TSK_CLEAR_TASK_SET:
  1211. return TMR_CLEAR_TASK_SET;
  1212. case SRP_TSK_LUN_RESET:
  1213. return TMR_LUN_RESET;
  1214. case SRP_TSK_CLEAR_ACA:
  1215. return TMR_CLEAR_ACA;
  1216. default:
  1217. return -1;
  1218. }
  1219. }
  1220. /**
  1221. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1222. *
  1223. * Returns 0 if and only if the request will be processed by the target core.
  1224. *
  1225. * For more information about SRP_TSK_MGMT information units, see also section
  1226. * 6.7 in the SRP r16a document.
  1227. */
  1228. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1229. struct srpt_recv_ioctx *recv_ioctx,
  1230. struct srpt_send_ioctx *send_ioctx)
  1231. {
  1232. struct srp_tsk_mgmt *srp_tsk;
  1233. struct se_cmd *cmd;
  1234. struct se_session *sess = ch->sess;
  1235. int tcm_tmr;
  1236. int rc;
  1237. BUG_ON(!send_ioctx);
  1238. srp_tsk = recv_ioctx->ioctx.buf;
  1239. cmd = &send_ioctx->cmd;
  1240. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1241. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1242. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1243. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1244. send_ioctx->cmd.tag = srp_tsk->tag;
  1245. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1246. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
  1247. scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
  1248. GFP_KERNEL, srp_tsk->task_tag,
  1249. TARGET_SCF_ACK_KREF);
  1250. if (rc != 0) {
  1251. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1252. goto fail;
  1253. }
  1254. return;
  1255. fail:
  1256. transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
  1257. }
  1258. /**
  1259. * srpt_handle_new_iu() - Process a newly received information unit.
  1260. * @ch: RDMA channel through which the information unit has been received.
  1261. * @ioctx: SRPT I/O context associated with the information unit.
  1262. */
  1263. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1264. struct srpt_recv_ioctx *recv_ioctx,
  1265. struct srpt_send_ioctx *send_ioctx)
  1266. {
  1267. struct srp_cmd *srp_cmd;
  1268. BUG_ON(!ch);
  1269. BUG_ON(!recv_ioctx);
  1270. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1271. recv_ioctx->ioctx.dma, srp_max_req_size,
  1272. DMA_FROM_DEVICE);
  1273. if (unlikely(ch->state == CH_CONNECTING))
  1274. goto out_wait;
  1275. if (unlikely(ch->state != CH_LIVE))
  1276. return;
  1277. srp_cmd = recv_ioctx->ioctx.buf;
  1278. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1279. if (!send_ioctx) {
  1280. if (!list_empty(&ch->cmd_wait_list))
  1281. goto out_wait;
  1282. send_ioctx = srpt_get_send_ioctx(ch);
  1283. }
  1284. if (unlikely(!send_ioctx))
  1285. goto out_wait;
  1286. }
  1287. switch (srp_cmd->opcode) {
  1288. case SRP_CMD:
  1289. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1290. break;
  1291. case SRP_TSK_MGMT:
  1292. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1293. break;
  1294. case SRP_I_LOGOUT:
  1295. pr_err("Not yet implemented: SRP_I_LOGOUT\n");
  1296. break;
  1297. case SRP_CRED_RSP:
  1298. pr_debug("received SRP_CRED_RSP\n");
  1299. break;
  1300. case SRP_AER_RSP:
  1301. pr_debug("received SRP_AER_RSP\n");
  1302. break;
  1303. case SRP_RSP:
  1304. pr_err("Received SRP_RSP\n");
  1305. break;
  1306. default:
  1307. pr_err("received IU with unknown opcode 0x%x\n",
  1308. srp_cmd->opcode);
  1309. break;
  1310. }
  1311. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1312. return;
  1313. out_wait:
  1314. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1315. }
  1316. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1317. {
  1318. struct srpt_rdma_ch *ch = cq->cq_context;
  1319. struct srpt_recv_ioctx *ioctx =
  1320. container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
  1321. if (wc->status == IB_WC_SUCCESS) {
  1322. int req_lim;
  1323. req_lim = atomic_dec_return(&ch->req_lim);
  1324. if (unlikely(req_lim < 0))
  1325. pr_err("req_lim = %d < 0\n", req_lim);
  1326. srpt_handle_new_iu(ch, ioctx, NULL);
  1327. } else {
  1328. pr_info("receiving failed for ioctx %p with status %d\n",
  1329. ioctx, wc->status);
  1330. }
  1331. }
  1332. /*
  1333. * This function must be called from the context in which RDMA completions are
  1334. * processed because it accesses the wait list without protection against
  1335. * access from other threads.
  1336. */
  1337. static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
  1338. {
  1339. struct srpt_send_ioctx *ioctx;
  1340. while (!list_empty(&ch->cmd_wait_list) &&
  1341. ch->state >= CH_LIVE &&
  1342. (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
  1343. struct srpt_recv_ioctx *recv_ioctx;
  1344. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1345. struct srpt_recv_ioctx,
  1346. wait_list);
  1347. list_del(&recv_ioctx->wait_list);
  1348. srpt_handle_new_iu(ch, recv_ioctx, ioctx);
  1349. }
  1350. }
  1351. /**
  1352. * Note: Although this has not yet been observed during tests, at least in
  1353. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1354. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1355. * value in each response is set to one, and it is possible that this response
  1356. * makes the initiator send a new request before the send completion for that
  1357. * response has been processed. This could e.g. happen if the call to
  1358. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1359. * if IB retransmission causes generation of the send completion to be
  1360. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1361. * are queued on cmd_wait_list. The code below processes these delayed
  1362. * requests one at a time.
  1363. */
  1364. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1365. {
  1366. struct srpt_rdma_ch *ch = cq->cq_context;
  1367. struct srpt_send_ioctx *ioctx =
  1368. container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
  1369. enum srpt_command_state state;
  1370. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1371. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
  1372. state != SRPT_STATE_MGMT_RSP_SENT);
  1373. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  1374. if (wc->status != IB_WC_SUCCESS)
  1375. pr_info("sending response for ioctx 0x%p failed"
  1376. " with status %d\n", ioctx, wc->status);
  1377. if (state != SRPT_STATE_DONE) {
  1378. transport_generic_free_cmd(&ioctx->cmd, 0);
  1379. } else {
  1380. pr_err("IB completion has been received too late for"
  1381. " wr_id = %u.\n", ioctx->ioctx.index);
  1382. }
  1383. srpt_process_wait_list(ch);
  1384. }
  1385. /**
  1386. * srpt_create_ch_ib() - Create receive and send completion queues.
  1387. */
  1388. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1389. {
  1390. struct ib_qp_init_attr *qp_init;
  1391. struct srpt_port *sport = ch->sport;
  1392. struct srpt_device *sdev = sport->sdev;
  1393. const struct ib_device_attr *attrs = &sdev->device->attrs;
  1394. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1395. int ret;
  1396. WARN_ON(ch->rq_size < 1);
  1397. ret = -ENOMEM;
  1398. qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
  1399. if (!qp_init)
  1400. goto out;
  1401. retry:
  1402. ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
  1403. 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
  1404. if (IS_ERR(ch->cq)) {
  1405. ret = PTR_ERR(ch->cq);
  1406. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1407. ch->rq_size + srp_sq_size, ret);
  1408. goto out;
  1409. }
  1410. qp_init->qp_context = (void *)ch;
  1411. qp_init->event_handler
  1412. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1413. qp_init->send_cq = ch->cq;
  1414. qp_init->recv_cq = ch->cq;
  1415. qp_init->srq = sdev->srq;
  1416. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1417. qp_init->qp_type = IB_QPT_RC;
  1418. /*
  1419. * We divide up our send queue size into half SEND WRs to send the
  1420. * completions, and half R/W contexts to actually do the RDMA
  1421. * READ/WRITE transfers. Note that we need to allocate CQ slots for
  1422. * both both, as RDMA contexts will also post completions for the
  1423. * RDMA READ case.
  1424. */
  1425. qp_init->cap.max_send_wr = srp_sq_size / 2;
  1426. qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
  1427. qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
  1428. qp_init->port_num = ch->sport->port;
  1429. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1430. if (IS_ERR(ch->qp)) {
  1431. ret = PTR_ERR(ch->qp);
  1432. if (ret == -ENOMEM) {
  1433. srp_sq_size /= 2;
  1434. if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
  1435. ib_destroy_cq(ch->cq);
  1436. goto retry;
  1437. }
  1438. }
  1439. pr_err("failed to create_qp ret= %d\n", ret);
  1440. goto err_destroy_cq;
  1441. }
  1442. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1443. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1444. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1445. qp_init->cap.max_send_wr, ch->cm_id);
  1446. ret = srpt_init_ch_qp(ch, ch->qp);
  1447. if (ret)
  1448. goto err_destroy_qp;
  1449. out:
  1450. kfree(qp_init);
  1451. return ret;
  1452. err_destroy_qp:
  1453. ib_destroy_qp(ch->qp);
  1454. err_destroy_cq:
  1455. ib_free_cq(ch->cq);
  1456. goto out;
  1457. }
  1458. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1459. {
  1460. ib_destroy_qp(ch->qp);
  1461. ib_free_cq(ch->cq);
  1462. }
  1463. /**
  1464. * srpt_close_ch() - Close an RDMA channel.
  1465. *
  1466. * Make sure all resources associated with the channel will be deallocated at
  1467. * an appropriate time.
  1468. *
  1469. * Returns true if and only if the channel state has been modified into
  1470. * CH_DRAINING.
  1471. */
  1472. static bool srpt_close_ch(struct srpt_rdma_ch *ch)
  1473. {
  1474. int ret;
  1475. if (!srpt_set_ch_state(ch, CH_DRAINING)) {
  1476. pr_debug("%s-%d: already closed\n", ch->sess_name,
  1477. ch->qp->qp_num);
  1478. return false;
  1479. }
  1480. kref_get(&ch->kref);
  1481. ret = srpt_ch_qp_err(ch);
  1482. if (ret < 0)
  1483. pr_err("%s-%d: changing queue pair into error state failed: %d\n",
  1484. ch->sess_name, ch->qp->qp_num, ret);
  1485. pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
  1486. ch->qp->qp_num);
  1487. ret = srpt_zerolength_write(ch);
  1488. if (ret < 0) {
  1489. pr_err("%s-%d: queuing zero-length write failed: %d\n",
  1490. ch->sess_name, ch->qp->qp_num, ret);
  1491. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  1492. schedule_work(&ch->release_work);
  1493. else
  1494. WARN_ON_ONCE(true);
  1495. }
  1496. kref_put(&ch->kref, srpt_free_ch);
  1497. return true;
  1498. }
  1499. /*
  1500. * Change the channel state into CH_DISCONNECTING. If a channel has not yet
  1501. * reached the connected state, close it. If a channel is in the connected
  1502. * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
  1503. * the responsibility of the caller to ensure that this function is not
  1504. * invoked concurrently with the code that accepts a connection. This means
  1505. * that this function must either be invoked from inside a CM callback
  1506. * function or that it must be invoked with the srpt_port.mutex held.
  1507. */
  1508. static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
  1509. {
  1510. int ret;
  1511. if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
  1512. return -ENOTCONN;
  1513. ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
  1514. if (ret < 0)
  1515. ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
  1516. if (ret < 0 && srpt_close_ch(ch))
  1517. ret = 0;
  1518. return ret;
  1519. }
  1520. static void __srpt_close_all_ch(struct srpt_device *sdev)
  1521. {
  1522. struct srpt_rdma_ch *ch;
  1523. lockdep_assert_held(&sdev->mutex);
  1524. list_for_each_entry(ch, &sdev->rch_list, list) {
  1525. if (srpt_disconnect_ch(ch) >= 0)
  1526. pr_info("Closing channel %s-%d because target %s has been disabled\n",
  1527. ch->sess_name, ch->qp->qp_num,
  1528. sdev->device->name);
  1529. srpt_close_ch(ch);
  1530. }
  1531. }
  1532. static void srpt_free_ch(struct kref *kref)
  1533. {
  1534. struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
  1535. kfree(ch);
  1536. }
  1537. static void srpt_release_channel_work(struct work_struct *w)
  1538. {
  1539. struct srpt_rdma_ch *ch;
  1540. struct srpt_device *sdev;
  1541. struct se_session *se_sess;
  1542. ch = container_of(w, struct srpt_rdma_ch, release_work);
  1543. pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
  1544. ch->qp->qp_num, ch->release_done);
  1545. sdev = ch->sport->sdev;
  1546. BUG_ON(!sdev);
  1547. se_sess = ch->sess;
  1548. BUG_ON(!se_sess);
  1549. target_sess_cmd_list_set_waiting(se_sess);
  1550. target_wait_for_sess_cmds(se_sess);
  1551. transport_deregister_session_configfs(se_sess);
  1552. transport_deregister_session(se_sess);
  1553. ch->sess = NULL;
  1554. ib_destroy_cm_id(ch->cm_id);
  1555. srpt_destroy_ch_ib(ch);
  1556. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1557. ch->sport->sdev, ch->rq_size,
  1558. ch->rsp_size, DMA_TO_DEVICE);
  1559. mutex_lock(&sdev->mutex);
  1560. list_del_init(&ch->list);
  1561. if (ch->release_done)
  1562. complete(ch->release_done);
  1563. mutex_unlock(&sdev->mutex);
  1564. wake_up(&sdev->ch_releaseQ);
  1565. kref_put(&ch->kref, srpt_free_ch);
  1566. }
  1567. /**
  1568. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  1569. *
  1570. * Ownership of the cm_id is transferred to the target session if this
  1571. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  1572. */
  1573. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  1574. struct ib_cm_req_event_param *param,
  1575. void *private_data)
  1576. {
  1577. struct srpt_device *sdev = cm_id->context;
  1578. struct srpt_port *sport = &sdev->port[param->port - 1];
  1579. struct srp_login_req *req;
  1580. struct srp_login_rsp *rsp;
  1581. struct srp_login_rej *rej;
  1582. struct ib_cm_rep_param *rep_param;
  1583. struct srpt_rdma_ch *ch, *tmp_ch;
  1584. u32 it_iu_len;
  1585. int i, ret = 0;
  1586. unsigned char *p;
  1587. WARN_ON_ONCE(irqs_disabled());
  1588. if (WARN_ON(!sdev || !private_data))
  1589. return -EINVAL;
  1590. req = (struct srp_login_req *)private_data;
  1591. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  1592. pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  1593. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  1594. " (guid=0x%llx:0x%llx)\n",
  1595. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  1596. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  1597. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  1598. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  1599. it_iu_len,
  1600. param->port,
  1601. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  1602. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  1603. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  1604. rej = kzalloc(sizeof(*rej), GFP_KERNEL);
  1605. rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
  1606. if (!rsp || !rej || !rep_param) {
  1607. ret = -ENOMEM;
  1608. goto out;
  1609. }
  1610. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  1611. rej->reason = cpu_to_be32(
  1612. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  1613. ret = -EINVAL;
  1614. pr_err("rejected SRP_LOGIN_REQ because its"
  1615. " length (%d bytes) is out of range (%d .. %d)\n",
  1616. it_iu_len, 64, srp_max_req_size);
  1617. goto reject;
  1618. }
  1619. if (!sport->enabled) {
  1620. rej->reason = cpu_to_be32(
  1621. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1622. ret = -EINVAL;
  1623. pr_err("rejected SRP_LOGIN_REQ because the target port"
  1624. " has not yet been enabled\n");
  1625. goto reject;
  1626. }
  1627. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  1628. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  1629. mutex_lock(&sdev->mutex);
  1630. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  1631. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  1632. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  1633. && param->port == ch->sport->port
  1634. && param->listen_id == ch->sport->sdev->cm_id
  1635. && ch->cm_id) {
  1636. if (srpt_disconnect_ch(ch) < 0)
  1637. continue;
  1638. pr_info("Relogin - closed existing channel %s\n",
  1639. ch->sess_name);
  1640. rsp->rsp_flags =
  1641. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  1642. }
  1643. }
  1644. mutex_unlock(&sdev->mutex);
  1645. } else
  1646. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  1647. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  1648. || *(__be64 *)(req->target_port_id + 8) !=
  1649. cpu_to_be64(srpt_service_guid)) {
  1650. rej->reason = cpu_to_be32(
  1651. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  1652. ret = -ENOMEM;
  1653. pr_err("rejected SRP_LOGIN_REQ because it"
  1654. " has an invalid target port identifier.\n");
  1655. goto reject;
  1656. }
  1657. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1658. if (!ch) {
  1659. rej->reason = cpu_to_be32(
  1660. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1661. pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
  1662. ret = -ENOMEM;
  1663. goto reject;
  1664. }
  1665. kref_init(&ch->kref);
  1666. ch->zw_cqe.done = srpt_zerolength_write_done;
  1667. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  1668. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  1669. memcpy(ch->t_port_id, req->target_port_id, 16);
  1670. ch->sport = &sdev->port[param->port - 1];
  1671. ch->cm_id = cm_id;
  1672. cm_id->context = ch;
  1673. /*
  1674. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  1675. * for the SRP protocol to the command queue size.
  1676. */
  1677. ch->rq_size = SRPT_RQ_SIZE;
  1678. spin_lock_init(&ch->spinlock);
  1679. ch->state = CH_CONNECTING;
  1680. INIT_LIST_HEAD(&ch->cmd_wait_list);
  1681. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  1682. ch->ioctx_ring = (struct srpt_send_ioctx **)
  1683. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1684. sizeof(*ch->ioctx_ring[0]),
  1685. ch->rsp_size, DMA_TO_DEVICE);
  1686. if (!ch->ioctx_ring)
  1687. goto free_ch;
  1688. INIT_LIST_HEAD(&ch->free_list);
  1689. for (i = 0; i < ch->rq_size; i++) {
  1690. ch->ioctx_ring[i]->ch = ch;
  1691. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  1692. }
  1693. ret = srpt_create_ch_ib(ch);
  1694. if (ret) {
  1695. rej->reason = cpu_to_be32(
  1696. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1697. pr_err("rejected SRP_LOGIN_REQ because creating"
  1698. " a new RDMA channel failed.\n");
  1699. goto free_ring;
  1700. }
  1701. ret = srpt_ch_qp_rtr(ch, ch->qp);
  1702. if (ret) {
  1703. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1704. pr_err("rejected SRP_LOGIN_REQ because enabling"
  1705. " RTR failed (error code = %d)\n", ret);
  1706. goto destroy_ib;
  1707. }
  1708. /*
  1709. * Use the initator port identifier as the session name, when
  1710. * checking against se_node_acl->initiatorname[] this can be
  1711. * with or without preceeding '0x'.
  1712. */
  1713. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  1714. be64_to_cpu(*(__be64 *)ch->i_port_id),
  1715. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  1716. pr_debug("registering session %s\n", ch->sess_name);
  1717. p = &ch->sess_name[0];
  1718. try_again:
  1719. ch->sess = target_alloc_session(&sport->port_tpg_1, 0, 0,
  1720. TARGET_PROT_NORMAL, p, ch, NULL);
  1721. if (IS_ERR(ch->sess)) {
  1722. pr_info("Rejected login because no ACL has been"
  1723. " configured yet for initiator %s.\n", p);
  1724. /*
  1725. * XXX: Hack to retry of ch->i_port_id without leading '0x'
  1726. */
  1727. if (p == &ch->sess_name[0]) {
  1728. p += 2;
  1729. goto try_again;
  1730. }
  1731. rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
  1732. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
  1733. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  1734. goto destroy_ib;
  1735. }
  1736. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  1737. ch->sess_name, ch->cm_id);
  1738. /* create srp_login_response */
  1739. rsp->opcode = SRP_LOGIN_RSP;
  1740. rsp->tag = req->tag;
  1741. rsp->max_it_iu_len = req->req_it_iu_len;
  1742. rsp->max_ti_iu_len = req->req_it_iu_len;
  1743. ch->max_ti_iu_len = it_iu_len;
  1744. rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1745. | SRP_BUF_FORMAT_INDIRECT);
  1746. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  1747. atomic_set(&ch->req_lim, ch->rq_size);
  1748. atomic_set(&ch->req_lim_delta, 0);
  1749. /* create cm reply */
  1750. rep_param->qp_num = ch->qp->qp_num;
  1751. rep_param->private_data = (void *)rsp;
  1752. rep_param->private_data_len = sizeof(*rsp);
  1753. rep_param->rnr_retry_count = 7;
  1754. rep_param->flow_control = 1;
  1755. rep_param->failover_accepted = 0;
  1756. rep_param->srq = 1;
  1757. rep_param->responder_resources = 4;
  1758. rep_param->initiator_depth = 4;
  1759. ret = ib_send_cm_rep(cm_id, rep_param);
  1760. if (ret) {
  1761. pr_err("sending SRP_LOGIN_REQ response failed"
  1762. " (error code = %d)\n", ret);
  1763. goto release_channel;
  1764. }
  1765. mutex_lock(&sdev->mutex);
  1766. list_add_tail(&ch->list, &sdev->rch_list);
  1767. mutex_unlock(&sdev->mutex);
  1768. goto out;
  1769. release_channel:
  1770. srpt_disconnect_ch(ch);
  1771. transport_deregister_session_configfs(ch->sess);
  1772. transport_deregister_session(ch->sess);
  1773. ch->sess = NULL;
  1774. destroy_ib:
  1775. srpt_destroy_ch_ib(ch);
  1776. free_ring:
  1777. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1778. ch->sport->sdev, ch->rq_size,
  1779. ch->rsp_size, DMA_TO_DEVICE);
  1780. free_ch:
  1781. kfree(ch);
  1782. reject:
  1783. rej->opcode = SRP_LOGIN_REJ;
  1784. rej->tag = req->tag;
  1785. rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1786. | SRP_BUF_FORMAT_INDIRECT);
  1787. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  1788. (void *)rej, sizeof(*rej));
  1789. out:
  1790. kfree(rep_param);
  1791. kfree(rsp);
  1792. kfree(rej);
  1793. return ret;
  1794. }
  1795. static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
  1796. enum ib_cm_rej_reason reason,
  1797. const u8 *private_data,
  1798. u8 private_data_len)
  1799. {
  1800. char *priv = NULL;
  1801. int i;
  1802. if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
  1803. GFP_KERNEL))) {
  1804. for (i = 0; i < private_data_len; i++)
  1805. sprintf(priv + 3 * i, " %02x", private_data[i]);
  1806. }
  1807. pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
  1808. ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
  1809. "; private data" : "", priv ? priv : " (?)");
  1810. kfree(priv);
  1811. }
  1812. /**
  1813. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  1814. *
  1815. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  1816. * and that the recipient may begin transmitting (RTU = ready to use).
  1817. */
  1818. static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
  1819. {
  1820. int ret;
  1821. if (srpt_set_ch_state(ch, CH_LIVE)) {
  1822. ret = srpt_ch_qp_rts(ch, ch->qp);
  1823. if (ret == 0) {
  1824. /* Trigger wait list processing. */
  1825. ret = srpt_zerolength_write(ch);
  1826. WARN_ONCE(ret < 0, "%d\n", ret);
  1827. } else {
  1828. srpt_close_ch(ch);
  1829. }
  1830. }
  1831. }
  1832. /**
  1833. * srpt_cm_handler() - IB connection manager callback function.
  1834. *
  1835. * A non-zero return value will cause the caller destroy the CM ID.
  1836. *
  1837. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  1838. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  1839. * a non-zero value in any other case will trigger a race with the
  1840. * ib_destroy_cm_id() call in srpt_release_channel().
  1841. */
  1842. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  1843. {
  1844. struct srpt_rdma_ch *ch = cm_id->context;
  1845. int ret;
  1846. ret = 0;
  1847. switch (event->event) {
  1848. case IB_CM_REQ_RECEIVED:
  1849. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  1850. event->private_data);
  1851. break;
  1852. case IB_CM_REJ_RECEIVED:
  1853. srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
  1854. event->private_data,
  1855. IB_CM_REJ_PRIVATE_DATA_SIZE);
  1856. break;
  1857. case IB_CM_RTU_RECEIVED:
  1858. case IB_CM_USER_ESTABLISHED:
  1859. srpt_cm_rtu_recv(ch);
  1860. break;
  1861. case IB_CM_DREQ_RECEIVED:
  1862. srpt_disconnect_ch(ch);
  1863. break;
  1864. case IB_CM_DREP_RECEIVED:
  1865. pr_info("Received CM DREP message for ch %s-%d.\n",
  1866. ch->sess_name, ch->qp->qp_num);
  1867. srpt_close_ch(ch);
  1868. break;
  1869. case IB_CM_TIMEWAIT_EXIT:
  1870. pr_info("Received CM TimeWait exit for ch %s-%d.\n",
  1871. ch->sess_name, ch->qp->qp_num);
  1872. srpt_close_ch(ch);
  1873. break;
  1874. case IB_CM_REP_ERROR:
  1875. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  1876. ch->qp->qp_num);
  1877. break;
  1878. case IB_CM_DREQ_ERROR:
  1879. pr_info("Received CM DREQ ERROR event.\n");
  1880. break;
  1881. case IB_CM_MRA_RECEIVED:
  1882. pr_info("Received CM MRA event\n");
  1883. break;
  1884. default:
  1885. pr_err("received unrecognized CM event %d\n", event->event);
  1886. break;
  1887. }
  1888. return ret;
  1889. }
  1890. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  1891. {
  1892. struct srpt_send_ioctx *ioctx;
  1893. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  1894. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  1895. }
  1896. /*
  1897. * srpt_write_pending() - Start data transfer from initiator to target (write).
  1898. */
  1899. static int srpt_write_pending(struct se_cmd *se_cmd)
  1900. {
  1901. struct srpt_send_ioctx *ioctx =
  1902. container_of(se_cmd, struct srpt_send_ioctx, cmd);
  1903. struct srpt_rdma_ch *ch = ioctx->ch;
  1904. struct ib_send_wr *first_wr = NULL, *bad_wr;
  1905. struct ib_cqe *cqe = &ioctx->rdma_cqe;
  1906. enum srpt_command_state new_state;
  1907. int ret, i;
  1908. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  1909. WARN_ON(new_state == SRPT_STATE_DONE);
  1910. if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
  1911. pr_warn("%s: IB send queue full (needed %d)\n",
  1912. __func__, ioctx->n_rdma);
  1913. ret = -ENOMEM;
  1914. goto out_undo;
  1915. }
  1916. cqe->done = srpt_rdma_read_done;
  1917. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  1918. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  1919. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
  1920. cqe, first_wr);
  1921. cqe = NULL;
  1922. }
  1923. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  1924. if (ret) {
  1925. pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
  1926. __func__, ret, ioctx->n_rdma,
  1927. atomic_read(&ch->sq_wr_avail));
  1928. goto out_undo;
  1929. }
  1930. return 0;
  1931. out_undo:
  1932. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1933. return ret;
  1934. }
  1935. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  1936. {
  1937. switch (tcm_mgmt_status) {
  1938. case TMR_FUNCTION_COMPLETE:
  1939. return SRP_TSK_MGMT_SUCCESS;
  1940. case TMR_FUNCTION_REJECTED:
  1941. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  1942. }
  1943. return SRP_TSK_MGMT_FAILED;
  1944. }
  1945. /**
  1946. * srpt_queue_response() - Transmits the response to a SCSI command.
  1947. *
  1948. * Callback function called by the TCM core. Must not block since it can be
  1949. * invoked on the context of the IB completion handler.
  1950. */
  1951. static void srpt_queue_response(struct se_cmd *cmd)
  1952. {
  1953. struct srpt_send_ioctx *ioctx =
  1954. container_of(cmd, struct srpt_send_ioctx, cmd);
  1955. struct srpt_rdma_ch *ch = ioctx->ch;
  1956. struct srpt_device *sdev = ch->sport->sdev;
  1957. struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
  1958. struct ib_sge sge;
  1959. enum srpt_command_state state;
  1960. unsigned long flags;
  1961. int resp_len, ret, i;
  1962. u8 srp_tm_status;
  1963. BUG_ON(!ch);
  1964. spin_lock_irqsave(&ioctx->spinlock, flags);
  1965. state = ioctx->state;
  1966. switch (state) {
  1967. case SRPT_STATE_NEW:
  1968. case SRPT_STATE_DATA_IN:
  1969. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  1970. break;
  1971. case SRPT_STATE_MGMT:
  1972. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  1973. break;
  1974. default:
  1975. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  1976. ch, ioctx->ioctx.index, ioctx->state);
  1977. break;
  1978. }
  1979. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1980. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  1981. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  1982. atomic_inc(&ch->req_lim_delta);
  1983. srpt_abort_cmd(ioctx);
  1984. return;
  1985. }
  1986. /* For read commands, transfer the data to the initiator. */
  1987. if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
  1988. ioctx->cmd.data_length &&
  1989. !ioctx->queue_status_only) {
  1990. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  1991. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  1992. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
  1993. ch->sport->port, NULL, first_wr);
  1994. }
  1995. }
  1996. if (state != SRPT_STATE_MGMT)
  1997. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  1998. cmd->scsi_status);
  1999. else {
  2000. srp_tm_status
  2001. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2002. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2003. ioctx->cmd.tag);
  2004. }
  2005. atomic_inc(&ch->req_lim);
  2006. if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
  2007. &ch->sq_wr_avail) < 0)) {
  2008. pr_warn("%s: IB send queue full (needed %d)\n",
  2009. __func__, ioctx->n_rdma);
  2010. ret = -ENOMEM;
  2011. goto out;
  2012. }
  2013. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
  2014. DMA_TO_DEVICE);
  2015. sge.addr = ioctx->ioctx.dma;
  2016. sge.length = resp_len;
  2017. sge.lkey = sdev->pd->local_dma_lkey;
  2018. ioctx->ioctx.cqe.done = srpt_send_done;
  2019. send_wr.next = NULL;
  2020. send_wr.wr_cqe = &ioctx->ioctx.cqe;
  2021. send_wr.sg_list = &sge;
  2022. send_wr.num_sge = 1;
  2023. send_wr.opcode = IB_WR_SEND;
  2024. send_wr.send_flags = IB_SEND_SIGNALED;
  2025. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  2026. if (ret < 0) {
  2027. pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
  2028. __func__, ioctx->cmd.tag, ret);
  2029. goto out;
  2030. }
  2031. return;
  2032. out:
  2033. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  2034. atomic_dec(&ch->req_lim);
  2035. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2036. target_put_sess_cmd(&ioctx->cmd);
  2037. }
  2038. static int srpt_queue_data_in(struct se_cmd *cmd)
  2039. {
  2040. srpt_queue_response(cmd);
  2041. return 0;
  2042. }
  2043. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2044. {
  2045. srpt_queue_response(cmd);
  2046. }
  2047. static void srpt_aborted_task(struct se_cmd *cmd)
  2048. {
  2049. }
  2050. static int srpt_queue_status(struct se_cmd *cmd)
  2051. {
  2052. struct srpt_send_ioctx *ioctx;
  2053. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2054. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2055. if (cmd->se_cmd_flags &
  2056. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2057. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2058. ioctx->queue_status_only = true;
  2059. srpt_queue_response(cmd);
  2060. return 0;
  2061. }
  2062. static void srpt_refresh_port_work(struct work_struct *work)
  2063. {
  2064. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2065. srpt_refresh_port(sport);
  2066. }
  2067. /**
  2068. * srpt_release_sdev() - Free the channel resources associated with a target.
  2069. */
  2070. static int srpt_release_sdev(struct srpt_device *sdev)
  2071. {
  2072. int i, res;
  2073. WARN_ON_ONCE(irqs_disabled());
  2074. BUG_ON(!sdev);
  2075. mutex_lock(&sdev->mutex);
  2076. for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
  2077. sdev->port[i].enabled = false;
  2078. __srpt_close_all_ch(sdev);
  2079. mutex_unlock(&sdev->mutex);
  2080. res = wait_event_interruptible(sdev->ch_releaseQ,
  2081. list_empty_careful(&sdev->rch_list));
  2082. if (res)
  2083. pr_err("%s: interrupted.\n", __func__);
  2084. return 0;
  2085. }
  2086. static struct srpt_port *__srpt_lookup_port(const char *name)
  2087. {
  2088. struct ib_device *dev;
  2089. struct srpt_device *sdev;
  2090. struct srpt_port *sport;
  2091. int i;
  2092. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2093. dev = sdev->device;
  2094. if (!dev)
  2095. continue;
  2096. for (i = 0; i < dev->phys_port_cnt; i++) {
  2097. sport = &sdev->port[i];
  2098. if (!strcmp(sport->port_guid, name))
  2099. return sport;
  2100. }
  2101. }
  2102. return NULL;
  2103. }
  2104. static struct srpt_port *srpt_lookup_port(const char *name)
  2105. {
  2106. struct srpt_port *sport;
  2107. spin_lock(&srpt_dev_lock);
  2108. sport = __srpt_lookup_port(name);
  2109. spin_unlock(&srpt_dev_lock);
  2110. return sport;
  2111. }
  2112. /**
  2113. * srpt_add_one() - Infiniband device addition callback function.
  2114. */
  2115. static void srpt_add_one(struct ib_device *device)
  2116. {
  2117. struct srpt_device *sdev;
  2118. struct srpt_port *sport;
  2119. struct ib_srq_init_attr srq_attr;
  2120. int i;
  2121. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2122. device->dma_ops);
  2123. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  2124. if (!sdev)
  2125. goto err;
  2126. sdev->device = device;
  2127. INIT_LIST_HEAD(&sdev->rch_list);
  2128. init_waitqueue_head(&sdev->ch_releaseQ);
  2129. mutex_init(&sdev->mutex);
  2130. sdev->pd = ib_alloc_pd(device);
  2131. if (IS_ERR(sdev->pd))
  2132. goto free_dev;
  2133. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2134. srq_attr.event_handler = srpt_srq_event;
  2135. srq_attr.srq_context = (void *)sdev;
  2136. srq_attr.attr.max_wr = sdev->srq_size;
  2137. srq_attr.attr.max_sge = 1;
  2138. srq_attr.attr.srq_limit = 0;
  2139. srq_attr.srq_type = IB_SRQT_BASIC;
  2140. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2141. if (IS_ERR(sdev->srq))
  2142. goto err_pd;
  2143. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2144. __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
  2145. device->name);
  2146. if (!srpt_service_guid)
  2147. srpt_service_guid = be64_to_cpu(device->node_guid);
  2148. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2149. if (IS_ERR(sdev->cm_id))
  2150. goto err_srq;
  2151. /* print out target login information */
  2152. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2153. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2154. srpt_service_guid, srpt_service_guid);
  2155. /*
  2156. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2157. * to identify this target. We currently use the guid of the first HCA
  2158. * in the system as service_id; therefore, the target_id will change
  2159. * if this HCA is gone bad and replaced by different HCA
  2160. */
  2161. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
  2162. goto err_cm;
  2163. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2164. srpt_event_handler);
  2165. if (ib_register_event_handler(&sdev->event_handler))
  2166. goto err_cm;
  2167. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2168. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2169. sizeof(*sdev->ioctx_ring[0]),
  2170. srp_max_req_size, DMA_FROM_DEVICE);
  2171. if (!sdev->ioctx_ring)
  2172. goto err_event;
  2173. for (i = 0; i < sdev->srq_size; ++i)
  2174. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2175. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2176. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2177. sport = &sdev->port[i - 1];
  2178. sport->sdev = sdev;
  2179. sport->port = i;
  2180. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2181. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2182. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2183. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2184. if (srpt_refresh_port(sport)) {
  2185. pr_err("MAD registration failed for %s-%d.\n",
  2186. sdev->device->name, i);
  2187. goto err_ring;
  2188. }
  2189. }
  2190. spin_lock(&srpt_dev_lock);
  2191. list_add_tail(&sdev->list, &srpt_dev_list);
  2192. spin_unlock(&srpt_dev_lock);
  2193. out:
  2194. ib_set_client_data(device, &srpt_client, sdev);
  2195. pr_debug("added %s.\n", device->name);
  2196. return;
  2197. err_ring:
  2198. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2199. sdev->srq_size, srp_max_req_size,
  2200. DMA_FROM_DEVICE);
  2201. err_event:
  2202. ib_unregister_event_handler(&sdev->event_handler);
  2203. err_cm:
  2204. ib_destroy_cm_id(sdev->cm_id);
  2205. err_srq:
  2206. ib_destroy_srq(sdev->srq);
  2207. err_pd:
  2208. ib_dealloc_pd(sdev->pd);
  2209. free_dev:
  2210. kfree(sdev);
  2211. err:
  2212. sdev = NULL;
  2213. pr_info("%s(%s) failed.\n", __func__, device->name);
  2214. goto out;
  2215. }
  2216. /**
  2217. * srpt_remove_one() - InfiniBand device removal callback function.
  2218. */
  2219. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2220. {
  2221. struct srpt_device *sdev = client_data;
  2222. int i;
  2223. if (!sdev) {
  2224. pr_info("%s(%s): nothing to do.\n", __func__, device->name);
  2225. return;
  2226. }
  2227. srpt_unregister_mad_agent(sdev);
  2228. ib_unregister_event_handler(&sdev->event_handler);
  2229. /* Cancel any work queued by the just unregistered IB event handler. */
  2230. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2231. cancel_work_sync(&sdev->port[i].work);
  2232. ib_destroy_cm_id(sdev->cm_id);
  2233. /*
  2234. * Unregistering a target must happen after destroying sdev->cm_id
  2235. * such that no new SRP_LOGIN_REQ information units can arrive while
  2236. * destroying the target.
  2237. */
  2238. spin_lock(&srpt_dev_lock);
  2239. list_del(&sdev->list);
  2240. spin_unlock(&srpt_dev_lock);
  2241. srpt_release_sdev(sdev);
  2242. ib_destroy_srq(sdev->srq);
  2243. ib_dealloc_pd(sdev->pd);
  2244. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2245. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2246. sdev->ioctx_ring = NULL;
  2247. kfree(sdev);
  2248. }
  2249. static struct ib_client srpt_client = {
  2250. .name = DRV_NAME,
  2251. .add = srpt_add_one,
  2252. .remove = srpt_remove_one
  2253. };
  2254. static int srpt_check_true(struct se_portal_group *se_tpg)
  2255. {
  2256. return 1;
  2257. }
  2258. static int srpt_check_false(struct se_portal_group *se_tpg)
  2259. {
  2260. return 0;
  2261. }
  2262. static char *srpt_get_fabric_name(void)
  2263. {
  2264. return "srpt";
  2265. }
  2266. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2267. {
  2268. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2269. return sport->port_guid;
  2270. }
  2271. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2272. {
  2273. return 1;
  2274. }
  2275. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2276. {
  2277. return 1;
  2278. }
  2279. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2280. {
  2281. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2282. struct srpt_send_ioctx, cmd);
  2283. struct srpt_rdma_ch *ch = ioctx->ch;
  2284. unsigned long flags;
  2285. WARN_ON(ioctx->state != SRPT_STATE_DONE);
  2286. if (ioctx->n_rw_ctx) {
  2287. srpt_free_rw_ctxs(ch, ioctx);
  2288. ioctx->n_rw_ctx = 0;
  2289. }
  2290. spin_lock_irqsave(&ch->spinlock, flags);
  2291. list_add(&ioctx->free_list, &ch->free_list);
  2292. spin_unlock_irqrestore(&ch->spinlock, flags);
  2293. }
  2294. /**
  2295. * srpt_close_session() - Forcibly close a session.
  2296. *
  2297. * Callback function invoked by the TCM core to clean up sessions associated
  2298. * with a node ACL when the user invokes
  2299. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2300. */
  2301. static void srpt_close_session(struct se_session *se_sess)
  2302. {
  2303. DECLARE_COMPLETION_ONSTACK(release_done);
  2304. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2305. struct srpt_device *sdev = ch->sport->sdev;
  2306. bool wait;
  2307. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  2308. ch->state);
  2309. mutex_lock(&sdev->mutex);
  2310. BUG_ON(ch->release_done);
  2311. ch->release_done = &release_done;
  2312. wait = !list_empty(&ch->list);
  2313. srpt_disconnect_ch(ch);
  2314. mutex_unlock(&sdev->mutex);
  2315. if (!wait)
  2316. return;
  2317. while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
  2318. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  2319. ch->sess_name, ch->qp->qp_num, ch->state);
  2320. }
  2321. /**
  2322. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  2323. *
  2324. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2325. * This object represents an arbitrary integer used to uniquely identify a
  2326. * particular attached remote initiator port to a particular SCSI target port
  2327. * within a particular SCSI target device within a particular SCSI instance.
  2328. */
  2329. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2330. {
  2331. return 0;
  2332. }
  2333. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2334. {
  2335. }
  2336. /* Note: only used from inside debug printk's by the TCM core. */
  2337. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2338. {
  2339. struct srpt_send_ioctx *ioctx;
  2340. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2341. return srpt_get_cmd_state(ioctx);
  2342. }
  2343. /**
  2344. * srpt_parse_i_port_id() - Parse an initiator port ID.
  2345. * @name: ASCII representation of a 128-bit initiator port ID.
  2346. * @i_port_id: Binary 128-bit port ID.
  2347. */
  2348. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2349. {
  2350. const char *p;
  2351. unsigned len, count, leading_zero_bytes;
  2352. int ret, rc;
  2353. p = name;
  2354. if (strncasecmp(p, "0x", 2) == 0)
  2355. p += 2;
  2356. ret = -EINVAL;
  2357. len = strlen(p);
  2358. if (len % 2)
  2359. goto out;
  2360. count = min(len / 2, 16U);
  2361. leading_zero_bytes = 16 - count;
  2362. memset(i_port_id, 0, leading_zero_bytes);
  2363. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2364. if (rc < 0)
  2365. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  2366. ret = 0;
  2367. out:
  2368. return ret;
  2369. }
  2370. /*
  2371. * configfs callback function invoked for
  2372. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2373. */
  2374. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2375. {
  2376. u8 i_port_id[16];
  2377. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  2378. pr_err("invalid initiator port ID %s\n", name);
  2379. return -EINVAL;
  2380. }
  2381. return 0;
  2382. }
  2383. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  2384. char *page)
  2385. {
  2386. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2387. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2388. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  2389. }
  2390. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  2391. const char *page, size_t count)
  2392. {
  2393. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2394. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2395. unsigned long val;
  2396. int ret;
  2397. ret = kstrtoul(page, 0, &val);
  2398. if (ret < 0) {
  2399. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2400. return -EINVAL;
  2401. }
  2402. if (val > MAX_SRPT_RDMA_SIZE) {
  2403. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  2404. MAX_SRPT_RDMA_SIZE);
  2405. return -EINVAL;
  2406. }
  2407. if (val < DEFAULT_MAX_RDMA_SIZE) {
  2408. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  2409. val, DEFAULT_MAX_RDMA_SIZE);
  2410. return -EINVAL;
  2411. }
  2412. sport->port_attrib.srp_max_rdma_size = val;
  2413. return count;
  2414. }
  2415. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  2416. char *page)
  2417. {
  2418. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2419. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2420. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  2421. }
  2422. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  2423. const char *page, size_t count)
  2424. {
  2425. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2426. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2427. unsigned long val;
  2428. int ret;
  2429. ret = kstrtoul(page, 0, &val);
  2430. if (ret < 0) {
  2431. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2432. return -EINVAL;
  2433. }
  2434. if (val > MAX_SRPT_RSP_SIZE) {
  2435. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  2436. MAX_SRPT_RSP_SIZE);
  2437. return -EINVAL;
  2438. }
  2439. if (val < MIN_MAX_RSP_SIZE) {
  2440. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  2441. MIN_MAX_RSP_SIZE);
  2442. return -EINVAL;
  2443. }
  2444. sport->port_attrib.srp_max_rsp_size = val;
  2445. return count;
  2446. }
  2447. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  2448. char *page)
  2449. {
  2450. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2451. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2452. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  2453. }
  2454. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  2455. const char *page, size_t count)
  2456. {
  2457. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2458. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2459. unsigned long val;
  2460. int ret;
  2461. ret = kstrtoul(page, 0, &val);
  2462. if (ret < 0) {
  2463. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2464. return -EINVAL;
  2465. }
  2466. if (val > MAX_SRPT_SRQ_SIZE) {
  2467. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  2468. MAX_SRPT_SRQ_SIZE);
  2469. return -EINVAL;
  2470. }
  2471. if (val < MIN_SRPT_SRQ_SIZE) {
  2472. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  2473. MIN_SRPT_SRQ_SIZE);
  2474. return -EINVAL;
  2475. }
  2476. sport->port_attrib.srp_sq_size = val;
  2477. return count;
  2478. }
  2479. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  2480. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  2481. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  2482. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  2483. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  2484. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  2485. &srpt_tpg_attrib_attr_srp_sq_size,
  2486. NULL,
  2487. };
  2488. static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
  2489. {
  2490. struct se_portal_group *se_tpg = to_tpg(item);
  2491. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2492. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  2493. }
  2494. static ssize_t srpt_tpg_enable_store(struct config_item *item,
  2495. const char *page, size_t count)
  2496. {
  2497. struct se_portal_group *se_tpg = to_tpg(item);
  2498. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2499. struct srpt_device *sdev = sport->sdev;
  2500. struct srpt_rdma_ch *ch;
  2501. unsigned long tmp;
  2502. int ret;
  2503. ret = kstrtoul(page, 0, &tmp);
  2504. if (ret < 0) {
  2505. pr_err("Unable to extract srpt_tpg_store_enable\n");
  2506. return -EINVAL;
  2507. }
  2508. if ((tmp != 0) && (tmp != 1)) {
  2509. pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  2510. return -EINVAL;
  2511. }
  2512. if (sport->enabled == tmp)
  2513. goto out;
  2514. sport->enabled = tmp;
  2515. if (sport->enabled)
  2516. goto out;
  2517. mutex_lock(&sdev->mutex);
  2518. list_for_each_entry(ch, &sdev->rch_list, list) {
  2519. if (ch->sport == sport) {
  2520. pr_debug("%s: ch %p %s-%d\n", __func__, ch,
  2521. ch->sess_name, ch->qp->qp_num);
  2522. srpt_disconnect_ch(ch);
  2523. srpt_close_ch(ch);
  2524. }
  2525. }
  2526. mutex_unlock(&sdev->mutex);
  2527. out:
  2528. return count;
  2529. }
  2530. CONFIGFS_ATTR(srpt_tpg_, enable);
  2531. static struct configfs_attribute *srpt_tpg_attrs[] = {
  2532. &srpt_tpg_attr_enable,
  2533. NULL,
  2534. };
  2535. /**
  2536. * configfs callback invoked for
  2537. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  2538. */
  2539. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  2540. struct config_group *group,
  2541. const char *name)
  2542. {
  2543. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2544. int res;
  2545. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  2546. res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
  2547. if (res)
  2548. return ERR_PTR(res);
  2549. return &sport->port_tpg_1;
  2550. }
  2551. /**
  2552. * configfs callback invoked for
  2553. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  2554. */
  2555. static void srpt_drop_tpg(struct se_portal_group *tpg)
  2556. {
  2557. struct srpt_port *sport = container_of(tpg,
  2558. struct srpt_port, port_tpg_1);
  2559. sport->enabled = false;
  2560. core_tpg_deregister(&sport->port_tpg_1);
  2561. }
  2562. /**
  2563. * configfs callback invoked for
  2564. * mkdir /sys/kernel/config/target/$driver/$port
  2565. */
  2566. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  2567. struct config_group *group,
  2568. const char *name)
  2569. {
  2570. struct srpt_port *sport;
  2571. int ret;
  2572. sport = srpt_lookup_port(name);
  2573. pr_debug("make_tport(%s)\n", name);
  2574. ret = -EINVAL;
  2575. if (!sport)
  2576. goto err;
  2577. return &sport->port_wwn;
  2578. err:
  2579. return ERR_PTR(ret);
  2580. }
  2581. /**
  2582. * configfs callback invoked for
  2583. * rmdir /sys/kernel/config/target/$driver/$port
  2584. */
  2585. static void srpt_drop_tport(struct se_wwn *wwn)
  2586. {
  2587. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2588. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  2589. }
  2590. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  2591. {
  2592. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  2593. }
  2594. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  2595. static struct configfs_attribute *srpt_wwn_attrs[] = {
  2596. &srpt_wwn_attr_version,
  2597. NULL,
  2598. };
  2599. static const struct target_core_fabric_ops srpt_template = {
  2600. .module = THIS_MODULE,
  2601. .name = "srpt",
  2602. .get_fabric_name = srpt_get_fabric_name,
  2603. .tpg_get_wwn = srpt_get_fabric_wwn,
  2604. .tpg_get_tag = srpt_get_tag,
  2605. .tpg_check_demo_mode = srpt_check_false,
  2606. .tpg_check_demo_mode_cache = srpt_check_true,
  2607. .tpg_check_demo_mode_write_protect = srpt_check_true,
  2608. .tpg_check_prod_mode_write_protect = srpt_check_false,
  2609. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  2610. .release_cmd = srpt_release_cmd,
  2611. .check_stop_free = srpt_check_stop_free,
  2612. .close_session = srpt_close_session,
  2613. .sess_get_index = srpt_sess_get_index,
  2614. .sess_get_initiator_sid = NULL,
  2615. .write_pending = srpt_write_pending,
  2616. .write_pending_status = srpt_write_pending_status,
  2617. .set_default_node_attributes = srpt_set_default_node_attrs,
  2618. .get_cmd_state = srpt_get_tcm_cmd_state,
  2619. .queue_data_in = srpt_queue_data_in,
  2620. .queue_status = srpt_queue_status,
  2621. .queue_tm_rsp = srpt_queue_tm_rsp,
  2622. .aborted_task = srpt_aborted_task,
  2623. /*
  2624. * Setup function pointers for generic logic in
  2625. * target_core_fabric_configfs.c
  2626. */
  2627. .fabric_make_wwn = srpt_make_tport,
  2628. .fabric_drop_wwn = srpt_drop_tport,
  2629. .fabric_make_tpg = srpt_make_tpg,
  2630. .fabric_drop_tpg = srpt_drop_tpg,
  2631. .fabric_init_nodeacl = srpt_init_nodeacl,
  2632. .tfc_wwn_attrs = srpt_wwn_attrs,
  2633. .tfc_tpg_base_attrs = srpt_tpg_attrs,
  2634. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  2635. };
  2636. /**
  2637. * srpt_init_module() - Kernel module initialization.
  2638. *
  2639. * Note: Since ib_register_client() registers callback functions, and since at
  2640. * least one of these callback functions (srpt_add_one()) calls target core
  2641. * functions, this driver must be registered with the target core before
  2642. * ib_register_client() is called.
  2643. */
  2644. static int __init srpt_init_module(void)
  2645. {
  2646. int ret;
  2647. ret = -EINVAL;
  2648. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  2649. pr_err("invalid value %d for kernel module parameter"
  2650. " srp_max_req_size -- must be at least %d.\n",
  2651. srp_max_req_size, MIN_MAX_REQ_SIZE);
  2652. goto out;
  2653. }
  2654. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  2655. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  2656. pr_err("invalid value %d for kernel module parameter"
  2657. " srpt_srq_size -- must be in the range [%d..%d].\n",
  2658. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  2659. goto out;
  2660. }
  2661. ret = target_register_template(&srpt_template);
  2662. if (ret)
  2663. goto out;
  2664. ret = ib_register_client(&srpt_client);
  2665. if (ret) {
  2666. pr_err("couldn't register IB client\n");
  2667. goto out_unregister_target;
  2668. }
  2669. return 0;
  2670. out_unregister_target:
  2671. target_unregister_template(&srpt_template);
  2672. out:
  2673. return ret;
  2674. }
  2675. static void __exit srpt_cleanup_module(void)
  2676. {
  2677. ib_unregister_client(&srpt_client);
  2678. target_unregister_template(&srpt_template);
  2679. }
  2680. module_init(srpt_init_module);
  2681. module_exit(srpt_cleanup_module);