ib_srpt.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  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. cif->resp_time_value = 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. if (!sport->mad_agent) {
  462. memset(&reg_req, 0, sizeof(reg_req));
  463. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  464. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  465. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  466. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  467. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  468. sport->port,
  469. IB_QPT_GSI,
  470. &reg_req, 0,
  471. srpt_mad_send_handler,
  472. srpt_mad_recv_handler,
  473. sport, 0);
  474. if (IS_ERR(sport->mad_agent)) {
  475. ret = PTR_ERR(sport->mad_agent);
  476. sport->mad_agent = NULL;
  477. goto err_query_port;
  478. }
  479. }
  480. return 0;
  481. err_query_port:
  482. port_modify.set_port_cap_mask = 0;
  483. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  484. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  485. err_mod_port:
  486. return ret;
  487. }
  488. /**
  489. * srpt_unregister_mad_agent() - Unregister MAD callback functions.
  490. *
  491. * Note: It is safe to call this function more than once for the same device.
  492. */
  493. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  494. {
  495. struct ib_port_modify port_modify = {
  496. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  497. };
  498. struct srpt_port *sport;
  499. int i;
  500. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  501. sport = &sdev->port[i - 1];
  502. WARN_ON(sport->port != i);
  503. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  504. pr_err("disabling MAD processing failed.\n");
  505. if (sport->mad_agent) {
  506. ib_unregister_mad_agent(sport->mad_agent);
  507. sport->mad_agent = NULL;
  508. }
  509. }
  510. }
  511. /**
  512. * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
  513. */
  514. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  515. int ioctx_size, int dma_size,
  516. enum dma_data_direction dir)
  517. {
  518. struct srpt_ioctx *ioctx;
  519. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  520. if (!ioctx)
  521. goto err;
  522. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  523. if (!ioctx->buf)
  524. goto err_free_ioctx;
  525. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  526. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  527. goto err_free_buf;
  528. return ioctx;
  529. err_free_buf:
  530. kfree(ioctx->buf);
  531. err_free_ioctx:
  532. kfree(ioctx);
  533. err:
  534. return NULL;
  535. }
  536. /**
  537. * srpt_free_ioctx() - Free an SRPT I/O context structure.
  538. */
  539. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  540. int dma_size, enum dma_data_direction dir)
  541. {
  542. if (!ioctx)
  543. return;
  544. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  545. kfree(ioctx->buf);
  546. kfree(ioctx);
  547. }
  548. /**
  549. * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
  550. * @sdev: Device to allocate the I/O context ring for.
  551. * @ring_size: Number of elements in the I/O context ring.
  552. * @ioctx_size: I/O context size.
  553. * @dma_size: DMA buffer size.
  554. * @dir: DMA data direction.
  555. */
  556. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  557. int ring_size, int ioctx_size,
  558. int dma_size, enum dma_data_direction dir)
  559. {
  560. struct srpt_ioctx **ring;
  561. int i;
  562. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  563. && ioctx_size != sizeof(struct srpt_send_ioctx));
  564. ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
  565. if (!ring)
  566. goto out;
  567. for (i = 0; i < ring_size; ++i) {
  568. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  569. if (!ring[i])
  570. goto err;
  571. ring[i]->index = i;
  572. }
  573. goto out;
  574. err:
  575. while (--i >= 0)
  576. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  577. kfree(ring);
  578. ring = NULL;
  579. out:
  580. return ring;
  581. }
  582. /**
  583. * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
  584. */
  585. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  586. struct srpt_device *sdev, int ring_size,
  587. int dma_size, enum dma_data_direction dir)
  588. {
  589. int i;
  590. for (i = 0; i < ring_size; ++i)
  591. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  592. kfree(ioctx_ring);
  593. }
  594. /**
  595. * srpt_get_cmd_state() - Get the state of a SCSI command.
  596. */
  597. static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
  598. {
  599. enum srpt_command_state state;
  600. unsigned long flags;
  601. BUG_ON(!ioctx);
  602. spin_lock_irqsave(&ioctx->spinlock, flags);
  603. state = ioctx->state;
  604. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  605. return state;
  606. }
  607. /**
  608. * srpt_set_cmd_state() - Set the state of a SCSI command.
  609. *
  610. * Does not modify the state of aborted commands. Returns the previous command
  611. * state.
  612. */
  613. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  614. enum srpt_command_state new)
  615. {
  616. enum srpt_command_state previous;
  617. unsigned long flags;
  618. BUG_ON(!ioctx);
  619. spin_lock_irqsave(&ioctx->spinlock, flags);
  620. previous = ioctx->state;
  621. if (previous != SRPT_STATE_DONE)
  622. ioctx->state = new;
  623. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  624. return previous;
  625. }
  626. /**
  627. * srpt_test_and_set_cmd_state() - Test and set the state of a command.
  628. *
  629. * Returns true if and only if the previous command state was equal to 'old'.
  630. */
  631. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  632. enum srpt_command_state old,
  633. enum srpt_command_state new)
  634. {
  635. enum srpt_command_state previous;
  636. unsigned long flags;
  637. WARN_ON(!ioctx);
  638. WARN_ON(old == SRPT_STATE_DONE);
  639. WARN_ON(new == SRPT_STATE_NEW);
  640. spin_lock_irqsave(&ioctx->spinlock, flags);
  641. previous = ioctx->state;
  642. if (previous == old)
  643. ioctx->state = new;
  644. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  645. return previous == old;
  646. }
  647. /**
  648. * srpt_post_recv() - Post an IB receive request.
  649. */
  650. static int srpt_post_recv(struct srpt_device *sdev,
  651. struct srpt_recv_ioctx *ioctx)
  652. {
  653. struct ib_sge list;
  654. struct ib_recv_wr wr, *bad_wr;
  655. BUG_ON(!sdev);
  656. list.addr = ioctx->ioctx.dma;
  657. list.length = srp_max_req_size;
  658. list.lkey = sdev->pd->local_dma_lkey;
  659. ioctx->ioctx.cqe.done = srpt_recv_done;
  660. wr.wr_cqe = &ioctx->ioctx.cqe;
  661. wr.next = NULL;
  662. wr.sg_list = &list;
  663. wr.num_sge = 1;
  664. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  665. }
  666. /**
  667. * srpt_post_send() - Post an IB send request.
  668. *
  669. * Returns zero upon success and a non-zero value upon failure.
  670. */
  671. static int srpt_post_send(struct srpt_rdma_ch *ch,
  672. struct srpt_send_ioctx *ioctx, int len)
  673. {
  674. struct ib_sge list;
  675. struct ib_send_wr wr, *bad_wr;
  676. struct srpt_device *sdev = ch->sport->sdev;
  677. int ret;
  678. atomic_inc(&ch->req_lim);
  679. ret = -ENOMEM;
  680. if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
  681. pr_warn("IB send queue full (needed 1)\n");
  682. goto out;
  683. }
  684. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
  685. DMA_TO_DEVICE);
  686. list.addr = ioctx->ioctx.dma;
  687. list.length = len;
  688. list.lkey = sdev->pd->local_dma_lkey;
  689. ioctx->ioctx.cqe.done = srpt_send_done;
  690. wr.next = NULL;
  691. wr.wr_cqe = &ioctx->ioctx.cqe;
  692. wr.sg_list = &list;
  693. wr.num_sge = 1;
  694. wr.opcode = IB_WR_SEND;
  695. wr.send_flags = IB_SEND_SIGNALED;
  696. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  697. out:
  698. if (ret < 0) {
  699. atomic_inc(&ch->sq_wr_avail);
  700. atomic_dec(&ch->req_lim);
  701. }
  702. return ret;
  703. }
  704. /**
  705. * srpt_zerolength_write() - Perform a zero-length RDMA write.
  706. *
  707. * A quote from the InfiniBand specification: C9-88: For an HCA responder
  708. * using Reliable Connection service, for each zero-length RDMA READ or WRITE
  709. * request, the R_Key shall not be validated, even if the request includes
  710. * Immediate data.
  711. */
  712. static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
  713. {
  714. struct ib_send_wr wr, *bad_wr;
  715. memset(&wr, 0, sizeof(wr));
  716. wr.opcode = IB_WR_RDMA_WRITE;
  717. wr.wr_cqe = &ch->zw_cqe;
  718. wr.send_flags = IB_SEND_SIGNALED;
  719. return ib_post_send(ch->qp, &wr, &bad_wr);
  720. }
  721. static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
  722. {
  723. struct srpt_rdma_ch *ch = cq->cq_context;
  724. if (wc->status == IB_WC_SUCCESS) {
  725. srpt_process_wait_list(ch);
  726. } else {
  727. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  728. schedule_work(&ch->release_work);
  729. else
  730. WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
  731. }
  732. }
  733. /**
  734. * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
  735. * @ioctx: Pointer to the I/O context associated with the request.
  736. * @srp_cmd: Pointer to the SRP_CMD request data.
  737. * @dir: Pointer to the variable to which the transfer direction will be
  738. * written.
  739. * @data_len: Pointer to the variable to which the total data length of all
  740. * descriptors in the SRP_CMD request will be written.
  741. *
  742. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  743. *
  744. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  745. * -ENOMEM when memory allocation fails and zero upon success.
  746. */
  747. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  748. struct srp_cmd *srp_cmd,
  749. enum dma_data_direction *dir, u64 *data_len)
  750. {
  751. struct srp_indirect_buf *idb;
  752. struct srp_direct_buf *db;
  753. unsigned add_cdb_offset;
  754. int ret;
  755. /*
  756. * The pointer computations below will only be compiled correctly
  757. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  758. * whether srp_cmd::add_data has been declared as a byte pointer.
  759. */
  760. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
  761. && !__same_type(srp_cmd->add_data[0], (u8)0));
  762. BUG_ON(!dir);
  763. BUG_ON(!data_len);
  764. ret = 0;
  765. *data_len = 0;
  766. /*
  767. * The lower four bits of the buffer format field contain the DATA-IN
  768. * buffer descriptor format, and the highest four bits contain the
  769. * DATA-OUT buffer descriptor format.
  770. */
  771. *dir = DMA_NONE;
  772. if (srp_cmd->buf_fmt & 0xf)
  773. /* DATA-IN: transfer data from target to initiator (read). */
  774. *dir = DMA_FROM_DEVICE;
  775. else if (srp_cmd->buf_fmt >> 4)
  776. /* DATA-OUT: transfer data from initiator to target (write). */
  777. *dir = DMA_TO_DEVICE;
  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. add_cdb_offset = srp_cmd->add_cdb_len & ~3;
  784. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  785. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  786. ioctx->n_rbuf = 1;
  787. ioctx->rbufs = &ioctx->single_rbuf;
  788. db = (struct srp_direct_buf *)(srp_cmd->add_data
  789. + add_cdb_offset);
  790. memcpy(ioctx->rbufs, db, sizeof(*db));
  791. *data_len = be32_to_cpu(db->len);
  792. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  793. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  794. idb = (struct srp_indirect_buf *)(srp_cmd->add_data
  795. + add_cdb_offset);
  796. ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof(*db);
  797. if (ioctx->n_rbuf >
  798. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  799. pr_err("received unsupported SRP_CMD request"
  800. " type (%u out + %u in != %u / %zu)\n",
  801. srp_cmd->data_out_desc_cnt,
  802. srp_cmd->data_in_desc_cnt,
  803. be32_to_cpu(idb->table_desc.len),
  804. sizeof(*db));
  805. ioctx->n_rbuf = 0;
  806. ret = -EINVAL;
  807. goto out;
  808. }
  809. if (ioctx->n_rbuf == 1)
  810. ioctx->rbufs = &ioctx->single_rbuf;
  811. else {
  812. ioctx->rbufs =
  813. kmalloc(ioctx->n_rbuf * sizeof(*db), GFP_ATOMIC);
  814. if (!ioctx->rbufs) {
  815. ioctx->n_rbuf = 0;
  816. ret = -ENOMEM;
  817. goto out;
  818. }
  819. }
  820. db = idb->desc_list;
  821. memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof(*db));
  822. *data_len = be32_to_cpu(idb->len);
  823. }
  824. out:
  825. return ret;
  826. }
  827. /**
  828. * srpt_init_ch_qp() - Initialize queue pair attributes.
  829. *
  830. * Initialized the attributes of queue pair 'qp' by allowing local write,
  831. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  832. */
  833. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  834. {
  835. struct ib_qp_attr *attr;
  836. int ret;
  837. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  838. if (!attr)
  839. return -ENOMEM;
  840. attr->qp_state = IB_QPS_INIT;
  841. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
  842. IB_ACCESS_REMOTE_WRITE;
  843. attr->port_num = ch->sport->port;
  844. attr->pkey_index = 0;
  845. ret = ib_modify_qp(qp, attr,
  846. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  847. IB_QP_PKEY_INDEX);
  848. kfree(attr);
  849. return ret;
  850. }
  851. /**
  852. * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
  853. * @ch: channel of the queue pair.
  854. * @qp: queue pair to change the state of.
  855. *
  856. * Returns zero upon success and a negative value upon failure.
  857. *
  858. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  859. * If this structure ever becomes larger, it might be necessary to allocate
  860. * it dynamically instead of on the stack.
  861. */
  862. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  863. {
  864. struct ib_qp_attr qp_attr;
  865. int attr_mask;
  866. int ret;
  867. qp_attr.qp_state = IB_QPS_RTR;
  868. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  869. if (ret)
  870. goto out;
  871. qp_attr.max_dest_rd_atomic = 4;
  872. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  873. out:
  874. return ret;
  875. }
  876. /**
  877. * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
  878. * @ch: channel of the queue pair.
  879. * @qp: queue pair to change the state of.
  880. *
  881. * Returns zero upon success and a negative value upon failure.
  882. *
  883. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  884. * If this structure ever becomes larger, it might be necessary to allocate
  885. * it dynamically instead of on the stack.
  886. */
  887. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  888. {
  889. struct ib_qp_attr qp_attr;
  890. int attr_mask;
  891. int ret;
  892. qp_attr.qp_state = IB_QPS_RTS;
  893. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  894. if (ret)
  895. goto out;
  896. qp_attr.max_rd_atomic = 4;
  897. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  898. out:
  899. return ret;
  900. }
  901. /**
  902. * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
  903. */
  904. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  905. {
  906. struct ib_qp_attr qp_attr;
  907. qp_attr.qp_state = IB_QPS_ERR;
  908. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  909. }
  910. /**
  911. * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
  912. */
  913. static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  914. struct srpt_send_ioctx *ioctx)
  915. {
  916. struct scatterlist *sg;
  917. enum dma_data_direction dir;
  918. BUG_ON(!ch);
  919. BUG_ON(!ioctx);
  920. BUG_ON(ioctx->n_rdma && !ioctx->rdma_wrs);
  921. while (ioctx->n_rdma)
  922. kfree(ioctx->rdma_wrs[--ioctx->n_rdma].wr.sg_list);
  923. kfree(ioctx->rdma_wrs);
  924. ioctx->rdma_wrs = NULL;
  925. if (ioctx->mapped_sg_count) {
  926. sg = ioctx->sg;
  927. WARN_ON(!sg);
  928. dir = ioctx->cmd.data_direction;
  929. BUG_ON(dir == DMA_NONE);
  930. ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
  931. target_reverse_dma_direction(&ioctx->cmd));
  932. ioctx->mapped_sg_count = 0;
  933. }
  934. }
  935. /**
  936. * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
  937. */
  938. static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  939. struct srpt_send_ioctx *ioctx)
  940. {
  941. struct ib_device *dev = ch->sport->sdev->device;
  942. struct se_cmd *cmd;
  943. struct scatterlist *sg, *sg_orig;
  944. int sg_cnt;
  945. enum dma_data_direction dir;
  946. struct ib_rdma_wr *riu;
  947. struct srp_direct_buf *db;
  948. dma_addr_t dma_addr;
  949. struct ib_sge *sge;
  950. u64 raddr;
  951. u32 rsize;
  952. u32 tsize;
  953. u32 dma_len;
  954. int count, nrdma;
  955. int i, j, k;
  956. BUG_ON(!ch);
  957. BUG_ON(!ioctx);
  958. cmd = &ioctx->cmd;
  959. dir = cmd->data_direction;
  960. BUG_ON(dir == DMA_NONE);
  961. ioctx->sg = sg = sg_orig = cmd->t_data_sg;
  962. ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
  963. count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
  964. target_reverse_dma_direction(cmd));
  965. if (unlikely(!count))
  966. return -EAGAIN;
  967. ioctx->mapped_sg_count = count;
  968. if (ioctx->rdma_wrs && ioctx->n_rdma_wrs)
  969. nrdma = ioctx->n_rdma_wrs;
  970. else {
  971. nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
  972. + ioctx->n_rbuf;
  973. ioctx->rdma_wrs = kcalloc(nrdma, sizeof(*ioctx->rdma_wrs),
  974. GFP_KERNEL);
  975. if (!ioctx->rdma_wrs)
  976. goto free_mem;
  977. ioctx->n_rdma_wrs = nrdma;
  978. }
  979. db = ioctx->rbufs;
  980. tsize = cmd->data_length;
  981. dma_len = ib_sg_dma_len(dev, &sg[0]);
  982. riu = ioctx->rdma_wrs;
  983. /*
  984. * For each remote desc - calculate the #ib_sge.
  985. * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
  986. * each remote desc rdma_iu is required a rdma wr;
  987. * else
  988. * we need to allocate extra rdma_iu to carry extra #ib_sge in
  989. * another rdma wr
  990. */
  991. for (i = 0, j = 0;
  992. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  993. rsize = be32_to_cpu(db->len);
  994. raddr = be64_to_cpu(db->va);
  995. riu->remote_addr = raddr;
  996. riu->rkey = be32_to_cpu(db->key);
  997. riu->wr.num_sge = 0;
  998. /* calculate how many sge required for this remote_buf */
  999. while (rsize > 0 && tsize > 0) {
  1000. if (rsize >= dma_len) {
  1001. tsize -= dma_len;
  1002. rsize -= dma_len;
  1003. raddr += dma_len;
  1004. if (tsize > 0) {
  1005. ++j;
  1006. if (j < count) {
  1007. sg = sg_next(sg);
  1008. dma_len = ib_sg_dma_len(
  1009. dev, sg);
  1010. }
  1011. }
  1012. } else {
  1013. tsize -= rsize;
  1014. dma_len -= rsize;
  1015. rsize = 0;
  1016. }
  1017. ++riu->wr.num_sge;
  1018. if (rsize > 0 &&
  1019. riu->wr.num_sge == SRPT_DEF_SG_PER_WQE) {
  1020. ++ioctx->n_rdma;
  1021. riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
  1022. sizeof(*riu->wr.sg_list),
  1023. GFP_KERNEL);
  1024. if (!riu->wr.sg_list)
  1025. goto free_mem;
  1026. ++riu;
  1027. riu->wr.num_sge = 0;
  1028. riu->remote_addr = raddr;
  1029. riu->rkey = be32_to_cpu(db->key);
  1030. }
  1031. }
  1032. ++ioctx->n_rdma;
  1033. riu->wr.sg_list = kmalloc_array(riu->wr.num_sge,
  1034. sizeof(*riu->wr.sg_list),
  1035. GFP_KERNEL);
  1036. if (!riu->wr.sg_list)
  1037. goto free_mem;
  1038. }
  1039. db = ioctx->rbufs;
  1040. tsize = cmd->data_length;
  1041. riu = ioctx->rdma_wrs;
  1042. sg = sg_orig;
  1043. dma_len = ib_sg_dma_len(dev, &sg[0]);
  1044. dma_addr = ib_sg_dma_address(dev, &sg[0]);
  1045. /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
  1046. for (i = 0, j = 0;
  1047. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  1048. rsize = be32_to_cpu(db->len);
  1049. sge = riu->wr.sg_list;
  1050. k = 0;
  1051. while (rsize > 0 && tsize > 0) {
  1052. sge->addr = dma_addr;
  1053. sge->lkey = ch->sport->sdev->pd->local_dma_lkey;
  1054. if (rsize >= dma_len) {
  1055. sge->length =
  1056. (tsize < dma_len) ? tsize : dma_len;
  1057. tsize -= dma_len;
  1058. rsize -= dma_len;
  1059. if (tsize > 0) {
  1060. ++j;
  1061. if (j < count) {
  1062. sg = sg_next(sg);
  1063. dma_len = ib_sg_dma_len(
  1064. dev, sg);
  1065. dma_addr = ib_sg_dma_address(
  1066. dev, sg);
  1067. }
  1068. }
  1069. } else {
  1070. sge->length = (tsize < rsize) ? tsize : rsize;
  1071. tsize -= rsize;
  1072. dma_len -= rsize;
  1073. dma_addr += rsize;
  1074. rsize = 0;
  1075. }
  1076. ++k;
  1077. if (k == riu->wr.num_sge && rsize > 0 && tsize > 0) {
  1078. ++riu;
  1079. sge = riu->wr.sg_list;
  1080. k = 0;
  1081. } else if (rsize > 0 && tsize > 0)
  1082. ++sge;
  1083. }
  1084. }
  1085. return 0;
  1086. free_mem:
  1087. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1088. return -ENOMEM;
  1089. }
  1090. /**
  1091. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  1092. */
  1093. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1094. {
  1095. struct se_session *se_sess;
  1096. struct srpt_send_ioctx *ioctx;
  1097. int tag;
  1098. BUG_ON(!ch);
  1099. se_sess = ch->sess;
  1100. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
  1101. if (tag < 0) {
  1102. pr_err("Unable to obtain tag for srpt_send_ioctx\n");
  1103. return NULL;
  1104. }
  1105. ioctx = &((struct srpt_send_ioctx *)se_sess->sess_cmd_map)[tag];
  1106. memset(ioctx, 0, sizeof(struct srpt_send_ioctx));
  1107. ioctx->ch = ch;
  1108. spin_lock_init(&ioctx->spinlock);
  1109. ioctx->state = SRPT_STATE_NEW;
  1110. init_completion(&ioctx->tx_done);
  1111. ioctx->cmd.map_tag = tag;
  1112. return ioctx;
  1113. }
  1114. /**
  1115. * srpt_abort_cmd() - Abort a SCSI command.
  1116. * @ioctx: I/O context associated with the SCSI command.
  1117. * @context: Preferred execution context.
  1118. */
  1119. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1120. {
  1121. enum srpt_command_state state;
  1122. unsigned long flags;
  1123. BUG_ON(!ioctx);
  1124. /*
  1125. * If the command is in a state where the target core is waiting for
  1126. * the ib_srpt driver, change the state to the next state.
  1127. */
  1128. spin_lock_irqsave(&ioctx->spinlock, flags);
  1129. state = ioctx->state;
  1130. switch (state) {
  1131. case SRPT_STATE_NEED_DATA:
  1132. ioctx->state = SRPT_STATE_DATA_IN;
  1133. break;
  1134. case SRPT_STATE_CMD_RSP_SENT:
  1135. case SRPT_STATE_MGMT_RSP_SENT:
  1136. ioctx->state = SRPT_STATE_DONE;
  1137. break;
  1138. default:
  1139. WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
  1140. __func__, state);
  1141. break;
  1142. }
  1143. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1144. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  1145. ioctx->cmd.tag);
  1146. switch (state) {
  1147. case SRPT_STATE_NEW:
  1148. case SRPT_STATE_DATA_IN:
  1149. case SRPT_STATE_MGMT:
  1150. case SRPT_STATE_DONE:
  1151. /*
  1152. * Do nothing - defer abort processing until
  1153. * srpt_queue_response() is invoked.
  1154. */
  1155. break;
  1156. case SRPT_STATE_NEED_DATA:
  1157. pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
  1158. transport_generic_request_failure(&ioctx->cmd,
  1159. TCM_CHECK_CONDITION_ABORT_CMD);
  1160. break;
  1161. case SRPT_STATE_CMD_RSP_SENT:
  1162. /*
  1163. * SRP_RSP sending failed or the SRP_RSP send completion has
  1164. * not been received in time.
  1165. */
  1166. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1167. transport_generic_free_cmd(&ioctx->cmd, 0);
  1168. break;
  1169. case SRPT_STATE_MGMT_RSP_SENT:
  1170. transport_generic_free_cmd(&ioctx->cmd, 0);
  1171. break;
  1172. default:
  1173. WARN(1, "Unexpected command state (%d)", state);
  1174. break;
  1175. }
  1176. return state;
  1177. }
  1178. /**
  1179. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1180. * the data that has been transferred via IB RDMA had to be postponed until the
  1181. * check_stop_free() callback. None of this is necessary anymore and needs to
  1182. * be cleaned up.
  1183. */
  1184. static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
  1185. {
  1186. struct srpt_rdma_ch *ch = cq->cq_context;
  1187. struct srpt_send_ioctx *ioctx =
  1188. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1189. WARN_ON(ioctx->n_rdma <= 0);
  1190. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1191. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1192. pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
  1193. ioctx, wc->status);
  1194. srpt_abort_cmd(ioctx);
  1195. return;
  1196. }
  1197. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1198. SRPT_STATE_DATA_IN))
  1199. target_execute_cmd(&ioctx->cmd);
  1200. else
  1201. pr_err("%s[%d]: wrong state = %d\n", __func__,
  1202. __LINE__, srpt_get_cmd_state(ioctx));
  1203. }
  1204. static void srpt_rdma_write_done(struct ib_cq *cq, struct ib_wc *wc)
  1205. {
  1206. struct srpt_send_ioctx *ioctx =
  1207. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1208. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1209. /*
  1210. * Note: if an RDMA write error completion is received that
  1211. * means that a SEND also has been posted. Defer further
  1212. * processing of the associated command until the send error
  1213. * completion has been received.
  1214. */
  1215. pr_info("RDMA_WRITE for ioctx 0x%p failed with status %d\n",
  1216. ioctx, wc->status);
  1217. }
  1218. }
  1219. /**
  1220. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1221. * @ch: RDMA channel through which the request has been received.
  1222. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1223. * be built in the buffer ioctx->buf points at and hence this function will
  1224. * overwrite the request data.
  1225. * @tag: tag of the request for which this response is being generated.
  1226. * @status: value for the STATUS field of the SRP_RSP information unit.
  1227. *
  1228. * Returns the size in bytes of the SRP_RSP response.
  1229. *
  1230. * An SRP_RSP response contains a SCSI status or service response. See also
  1231. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1232. * response. See also SPC-2 for more information about sense data.
  1233. */
  1234. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1235. struct srpt_send_ioctx *ioctx, u64 tag,
  1236. int status)
  1237. {
  1238. struct srp_rsp *srp_rsp;
  1239. const u8 *sense_data;
  1240. int sense_data_len, max_sense_len;
  1241. /*
  1242. * The lowest bit of all SAM-3 status codes is zero (see also
  1243. * paragraph 5.3 in SAM-3).
  1244. */
  1245. WARN_ON(status & 1);
  1246. srp_rsp = ioctx->ioctx.buf;
  1247. BUG_ON(!srp_rsp);
  1248. sense_data = ioctx->sense_data;
  1249. sense_data_len = ioctx->cmd.scsi_sense_length;
  1250. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1251. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1252. srp_rsp->opcode = SRP_RSP;
  1253. srp_rsp->req_lim_delta =
  1254. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1255. srp_rsp->tag = tag;
  1256. srp_rsp->status = status;
  1257. if (sense_data_len) {
  1258. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1259. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1260. if (sense_data_len > max_sense_len) {
  1261. pr_warn("truncated sense data from %d to %d"
  1262. " bytes\n", sense_data_len, max_sense_len);
  1263. sense_data_len = max_sense_len;
  1264. }
  1265. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1266. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1267. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1268. }
  1269. return sizeof(*srp_rsp) + sense_data_len;
  1270. }
  1271. /**
  1272. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1273. * @ch: RDMA channel through which the request has been received.
  1274. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1275. * @rsp_code: RSP_CODE that will be stored in the response.
  1276. * @tag: Tag of the request for which this response is being generated.
  1277. *
  1278. * Returns the size in bytes of the SRP_RSP response.
  1279. *
  1280. * An SRP_RSP response contains a SCSI status or service response. See also
  1281. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1282. * response.
  1283. */
  1284. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1285. struct srpt_send_ioctx *ioctx,
  1286. u8 rsp_code, u64 tag)
  1287. {
  1288. struct srp_rsp *srp_rsp;
  1289. int resp_data_len;
  1290. int resp_len;
  1291. resp_data_len = 4;
  1292. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1293. srp_rsp = ioctx->ioctx.buf;
  1294. BUG_ON(!srp_rsp);
  1295. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1296. srp_rsp->opcode = SRP_RSP;
  1297. srp_rsp->req_lim_delta =
  1298. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1299. srp_rsp->tag = tag;
  1300. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1301. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1302. srp_rsp->data[3] = rsp_code;
  1303. return resp_len;
  1304. }
  1305. static int srpt_check_stop_free(struct se_cmd *cmd)
  1306. {
  1307. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1308. struct srpt_send_ioctx, cmd);
  1309. return target_put_sess_cmd(&ioctx->cmd);
  1310. }
  1311. /**
  1312. * srpt_handle_cmd() - Process SRP_CMD.
  1313. */
  1314. static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1315. struct srpt_recv_ioctx *recv_ioctx,
  1316. struct srpt_send_ioctx *send_ioctx)
  1317. {
  1318. struct se_cmd *cmd;
  1319. struct srp_cmd *srp_cmd;
  1320. u64 data_len;
  1321. enum dma_data_direction dir;
  1322. int rc;
  1323. BUG_ON(!send_ioctx);
  1324. srp_cmd = recv_ioctx->ioctx.buf;
  1325. cmd = &send_ioctx->cmd;
  1326. cmd->tag = srp_cmd->tag;
  1327. switch (srp_cmd->task_attr) {
  1328. case SRP_CMD_SIMPLE_Q:
  1329. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1330. break;
  1331. case SRP_CMD_ORDERED_Q:
  1332. default:
  1333. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1334. break;
  1335. case SRP_CMD_HEAD_OF_Q:
  1336. cmd->sam_task_attr = TCM_HEAD_TAG;
  1337. break;
  1338. case SRP_CMD_ACA:
  1339. cmd->sam_task_attr = TCM_ACA_TAG;
  1340. break;
  1341. }
  1342. if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
  1343. pr_err("0x%llx: parsing SRP descriptor table failed.\n",
  1344. srp_cmd->tag);
  1345. goto release_ioctx;
  1346. }
  1347. rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
  1348. &send_ioctx->sense_data[0],
  1349. scsilun_to_int(&srp_cmd->lun), data_len,
  1350. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
  1351. if (rc != 0) {
  1352. pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
  1353. srp_cmd->tag);
  1354. goto release_ioctx;
  1355. }
  1356. return;
  1357. release_ioctx:
  1358. send_ioctx->state = SRPT_STATE_DONE;
  1359. srpt_release_cmd(cmd);
  1360. }
  1361. static int srp_tmr_to_tcm(int fn)
  1362. {
  1363. switch (fn) {
  1364. case SRP_TSK_ABORT_TASK:
  1365. return TMR_ABORT_TASK;
  1366. case SRP_TSK_ABORT_TASK_SET:
  1367. return TMR_ABORT_TASK_SET;
  1368. case SRP_TSK_CLEAR_TASK_SET:
  1369. return TMR_CLEAR_TASK_SET;
  1370. case SRP_TSK_LUN_RESET:
  1371. return TMR_LUN_RESET;
  1372. case SRP_TSK_CLEAR_ACA:
  1373. return TMR_CLEAR_ACA;
  1374. default:
  1375. return -1;
  1376. }
  1377. }
  1378. /**
  1379. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1380. *
  1381. * Returns 0 if and only if the request will be processed by the target core.
  1382. *
  1383. * For more information about SRP_TSK_MGMT information units, see also section
  1384. * 6.7 in the SRP r16a document.
  1385. */
  1386. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1387. struct srpt_recv_ioctx *recv_ioctx,
  1388. struct srpt_send_ioctx *send_ioctx)
  1389. {
  1390. struct srp_tsk_mgmt *srp_tsk;
  1391. struct se_cmd *cmd;
  1392. struct se_session *sess = ch->sess;
  1393. int tcm_tmr;
  1394. int rc;
  1395. BUG_ON(!send_ioctx);
  1396. srp_tsk = recv_ioctx->ioctx.buf;
  1397. cmd = &send_ioctx->cmd;
  1398. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1399. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1400. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1401. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1402. send_ioctx->cmd.tag = srp_tsk->tag;
  1403. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1404. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
  1405. scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
  1406. GFP_KERNEL, srp_tsk->task_tag,
  1407. TARGET_SCF_ACK_KREF);
  1408. if (rc != 0) {
  1409. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1410. goto fail;
  1411. }
  1412. return;
  1413. fail:
  1414. transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
  1415. }
  1416. /**
  1417. * srpt_handle_new_iu() - Process a newly received information unit.
  1418. * @ch: RDMA channel through which the information unit has been received.
  1419. * @ioctx: SRPT I/O context associated with the information unit.
  1420. */
  1421. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1422. struct srpt_recv_ioctx *recv_ioctx,
  1423. struct srpt_send_ioctx *send_ioctx)
  1424. {
  1425. struct srp_cmd *srp_cmd;
  1426. BUG_ON(!ch);
  1427. BUG_ON(!recv_ioctx);
  1428. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1429. recv_ioctx->ioctx.dma, srp_max_req_size,
  1430. DMA_FROM_DEVICE);
  1431. if (unlikely(ch->state == CH_CONNECTING)) {
  1432. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1433. goto out;
  1434. }
  1435. if (unlikely(ch->state != CH_LIVE))
  1436. goto out;
  1437. srp_cmd = recv_ioctx->ioctx.buf;
  1438. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1439. if (!send_ioctx)
  1440. send_ioctx = srpt_get_send_ioctx(ch);
  1441. if (unlikely(!send_ioctx)) {
  1442. list_add_tail(&recv_ioctx->wait_list,
  1443. &ch->cmd_wait_list);
  1444. goto out;
  1445. }
  1446. }
  1447. switch (srp_cmd->opcode) {
  1448. case SRP_CMD:
  1449. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1450. break;
  1451. case SRP_TSK_MGMT:
  1452. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1453. break;
  1454. case SRP_I_LOGOUT:
  1455. pr_err("Not yet implemented: SRP_I_LOGOUT\n");
  1456. break;
  1457. case SRP_CRED_RSP:
  1458. pr_debug("received SRP_CRED_RSP\n");
  1459. break;
  1460. case SRP_AER_RSP:
  1461. pr_debug("received SRP_AER_RSP\n");
  1462. break;
  1463. case SRP_RSP:
  1464. pr_err("Received SRP_RSP\n");
  1465. break;
  1466. default:
  1467. pr_err("received IU with unknown opcode 0x%x\n",
  1468. srp_cmd->opcode);
  1469. break;
  1470. }
  1471. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1472. out:
  1473. return;
  1474. }
  1475. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1476. {
  1477. struct srpt_rdma_ch *ch = cq->cq_context;
  1478. struct srpt_recv_ioctx *ioctx =
  1479. container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
  1480. if (wc->status == IB_WC_SUCCESS) {
  1481. int req_lim;
  1482. req_lim = atomic_dec_return(&ch->req_lim);
  1483. if (unlikely(req_lim < 0))
  1484. pr_err("req_lim = %d < 0\n", req_lim);
  1485. srpt_handle_new_iu(ch, ioctx, NULL);
  1486. } else {
  1487. pr_info("receiving failed for ioctx %p with status %d\n",
  1488. ioctx, wc->status);
  1489. }
  1490. }
  1491. /*
  1492. * This function must be called from the context in which RDMA completions are
  1493. * processed because it accesses the wait list without protection against
  1494. * access from other threads.
  1495. */
  1496. static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
  1497. {
  1498. struct srpt_send_ioctx *ioctx;
  1499. while (!list_empty(&ch->cmd_wait_list) &&
  1500. ch->state >= CH_LIVE &&
  1501. (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
  1502. struct srpt_recv_ioctx *recv_ioctx;
  1503. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1504. struct srpt_recv_ioctx,
  1505. wait_list);
  1506. list_del(&recv_ioctx->wait_list);
  1507. srpt_handle_new_iu(ch, recv_ioctx, ioctx);
  1508. }
  1509. }
  1510. /**
  1511. * Note: Although this has not yet been observed during tests, at least in
  1512. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1513. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1514. * value in each response is set to one, and it is possible that this response
  1515. * makes the initiator send a new request before the send completion for that
  1516. * response has been processed. This could e.g. happen if the call to
  1517. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1518. * if IB retransmission causes generation of the send completion to be
  1519. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1520. * are queued on cmd_wait_list. The code below processes these delayed
  1521. * requests one at a time.
  1522. */
  1523. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1524. {
  1525. struct srpt_rdma_ch *ch = cq->cq_context;
  1526. struct srpt_send_ioctx *ioctx =
  1527. container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
  1528. enum srpt_command_state state;
  1529. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1530. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
  1531. state != SRPT_STATE_MGMT_RSP_SENT);
  1532. atomic_inc(&ch->sq_wr_avail);
  1533. if (wc->status != IB_WC_SUCCESS)
  1534. pr_info("sending response for ioctx 0x%p failed"
  1535. " with status %d\n", ioctx, wc->status);
  1536. if (state != SRPT_STATE_DONE) {
  1537. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1538. transport_generic_free_cmd(&ioctx->cmd, 0);
  1539. } else {
  1540. pr_err("IB completion has been received too late for"
  1541. " wr_id = %u.\n", ioctx->ioctx.index);
  1542. }
  1543. srpt_process_wait_list(ch);
  1544. }
  1545. /**
  1546. * srpt_create_ch_ib() - Create receive and send completion queues.
  1547. */
  1548. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1549. {
  1550. struct ib_qp_init_attr *qp_init;
  1551. struct srpt_port *sport = ch->sport;
  1552. struct srpt_device *sdev = sport->sdev;
  1553. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1554. int ret;
  1555. WARN_ON(ch->rq_size < 1);
  1556. ret = -ENOMEM;
  1557. qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
  1558. if (!qp_init)
  1559. goto out;
  1560. retry:
  1561. ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
  1562. 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
  1563. if (IS_ERR(ch->cq)) {
  1564. ret = PTR_ERR(ch->cq);
  1565. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1566. ch->rq_size + srp_sq_size, ret);
  1567. goto out;
  1568. }
  1569. qp_init->qp_context = (void *)ch;
  1570. qp_init->event_handler
  1571. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1572. qp_init->send_cq = ch->cq;
  1573. qp_init->recv_cq = ch->cq;
  1574. qp_init->srq = sdev->srq;
  1575. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1576. qp_init->qp_type = IB_QPT_RC;
  1577. qp_init->cap.max_send_wr = srp_sq_size;
  1578. qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
  1579. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1580. if (IS_ERR(ch->qp)) {
  1581. ret = PTR_ERR(ch->qp);
  1582. if (ret == -ENOMEM) {
  1583. srp_sq_size /= 2;
  1584. if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
  1585. ib_destroy_cq(ch->cq);
  1586. goto retry;
  1587. }
  1588. }
  1589. pr_err("failed to create_qp ret= %d\n", ret);
  1590. goto err_destroy_cq;
  1591. }
  1592. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1593. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1594. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1595. qp_init->cap.max_send_wr, ch->cm_id);
  1596. ret = srpt_init_ch_qp(ch, ch->qp);
  1597. if (ret)
  1598. goto err_destroy_qp;
  1599. out:
  1600. kfree(qp_init);
  1601. return ret;
  1602. err_destroy_qp:
  1603. ib_destroy_qp(ch->qp);
  1604. err_destroy_cq:
  1605. ib_free_cq(ch->cq);
  1606. goto out;
  1607. }
  1608. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1609. {
  1610. ib_destroy_qp(ch->qp);
  1611. ib_free_cq(ch->cq);
  1612. }
  1613. /**
  1614. * srpt_close_ch() - Close an RDMA channel.
  1615. *
  1616. * Make sure all resources associated with the channel will be deallocated at
  1617. * an appropriate time.
  1618. *
  1619. * Returns true if and only if the channel state has been modified into
  1620. * CH_DRAINING.
  1621. */
  1622. static bool srpt_close_ch(struct srpt_rdma_ch *ch)
  1623. {
  1624. int ret;
  1625. if (!srpt_set_ch_state(ch, CH_DRAINING)) {
  1626. pr_debug("%s-%d: already closed\n", ch->sess_name,
  1627. ch->qp->qp_num);
  1628. return false;
  1629. }
  1630. kref_get(&ch->kref);
  1631. ret = srpt_ch_qp_err(ch);
  1632. if (ret < 0)
  1633. pr_err("%s-%d: changing queue pair into error state failed: %d\n",
  1634. ch->sess_name, ch->qp->qp_num, ret);
  1635. pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
  1636. ch->qp->qp_num);
  1637. ret = srpt_zerolength_write(ch);
  1638. if (ret < 0) {
  1639. pr_err("%s-%d: queuing zero-length write failed: %d\n",
  1640. ch->sess_name, ch->qp->qp_num, ret);
  1641. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  1642. schedule_work(&ch->release_work);
  1643. else
  1644. WARN_ON_ONCE(true);
  1645. }
  1646. kref_put(&ch->kref, srpt_free_ch);
  1647. return true;
  1648. }
  1649. /*
  1650. * Change the channel state into CH_DISCONNECTING. If a channel has not yet
  1651. * reached the connected state, close it. If a channel is in the connected
  1652. * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
  1653. * the responsibility of the caller to ensure that this function is not
  1654. * invoked concurrently with the code that accepts a connection. This means
  1655. * that this function must either be invoked from inside a CM callback
  1656. * function or that it must be invoked with the srpt_port.mutex held.
  1657. */
  1658. static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
  1659. {
  1660. int ret;
  1661. if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
  1662. return -ENOTCONN;
  1663. ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
  1664. if (ret < 0)
  1665. ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
  1666. if (ret < 0 && srpt_close_ch(ch))
  1667. ret = 0;
  1668. return ret;
  1669. }
  1670. static void __srpt_close_all_ch(struct srpt_device *sdev)
  1671. {
  1672. struct srpt_rdma_ch *ch;
  1673. lockdep_assert_held(&sdev->mutex);
  1674. list_for_each_entry(ch, &sdev->rch_list, list) {
  1675. if (srpt_disconnect_ch(ch) >= 0)
  1676. pr_info("Closing channel %s-%d because target %s has been disabled\n",
  1677. ch->sess_name, ch->qp->qp_num,
  1678. sdev->device->name);
  1679. srpt_close_ch(ch);
  1680. }
  1681. }
  1682. /**
  1683. * srpt_shutdown_session() - Whether or not a session may be shut down.
  1684. */
  1685. static int srpt_shutdown_session(struct se_session *se_sess)
  1686. {
  1687. return 1;
  1688. }
  1689. static void srpt_free_ch(struct kref *kref)
  1690. {
  1691. struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
  1692. kfree(ch);
  1693. }
  1694. static void srpt_release_channel_work(struct work_struct *w)
  1695. {
  1696. struct srpt_rdma_ch *ch;
  1697. struct srpt_device *sdev;
  1698. struct se_session *se_sess;
  1699. ch = container_of(w, struct srpt_rdma_ch, release_work);
  1700. pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
  1701. ch->qp->qp_num, ch->release_done);
  1702. sdev = ch->sport->sdev;
  1703. BUG_ON(!sdev);
  1704. se_sess = ch->sess;
  1705. BUG_ON(!se_sess);
  1706. target_sess_cmd_list_set_waiting(se_sess);
  1707. target_wait_for_sess_cmds(se_sess);
  1708. transport_deregister_session_configfs(se_sess);
  1709. transport_deregister_session(se_sess);
  1710. ch->sess = NULL;
  1711. ib_destroy_cm_id(ch->cm_id);
  1712. srpt_destroy_ch_ib(ch);
  1713. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1714. ch->sport->sdev, ch->rq_size,
  1715. ch->rsp_size, DMA_TO_DEVICE);
  1716. mutex_lock(&sdev->mutex);
  1717. list_del_init(&ch->list);
  1718. if (ch->release_done)
  1719. complete(ch->release_done);
  1720. mutex_unlock(&sdev->mutex);
  1721. wake_up(&sdev->ch_releaseQ);
  1722. kref_put(&ch->kref, srpt_free_ch);
  1723. }
  1724. /**
  1725. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  1726. *
  1727. * Ownership of the cm_id is transferred to the target session if this
  1728. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  1729. */
  1730. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  1731. struct ib_cm_req_event_param *param,
  1732. void *private_data)
  1733. {
  1734. struct srpt_device *sdev = cm_id->context;
  1735. struct srpt_port *sport = &sdev->port[param->port - 1];
  1736. struct srp_login_req *req;
  1737. struct srp_login_rsp *rsp;
  1738. struct srp_login_rej *rej;
  1739. struct ib_cm_rep_param *rep_param;
  1740. struct srpt_rdma_ch *ch, *tmp_ch;
  1741. u32 it_iu_len;
  1742. int ret = 0;
  1743. unsigned char *p;
  1744. WARN_ON_ONCE(irqs_disabled());
  1745. if (WARN_ON(!sdev || !private_data))
  1746. return -EINVAL;
  1747. req = (struct srp_login_req *)private_data;
  1748. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  1749. pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  1750. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  1751. " (guid=0x%llx:0x%llx)\n",
  1752. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  1753. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  1754. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  1755. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  1756. it_iu_len,
  1757. param->port,
  1758. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  1759. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  1760. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  1761. rej = kzalloc(sizeof(*rej), GFP_KERNEL);
  1762. rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
  1763. if (!rsp || !rej || !rep_param) {
  1764. ret = -ENOMEM;
  1765. goto out;
  1766. }
  1767. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  1768. rej->reason = cpu_to_be32(
  1769. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  1770. ret = -EINVAL;
  1771. pr_err("rejected SRP_LOGIN_REQ because its"
  1772. " length (%d bytes) is out of range (%d .. %d)\n",
  1773. it_iu_len, 64, srp_max_req_size);
  1774. goto reject;
  1775. }
  1776. if (!sport->enabled) {
  1777. rej->reason = cpu_to_be32(
  1778. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1779. ret = -EINVAL;
  1780. pr_err("rejected SRP_LOGIN_REQ because the target port"
  1781. " has not yet been enabled\n");
  1782. goto reject;
  1783. }
  1784. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  1785. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  1786. mutex_lock(&sdev->mutex);
  1787. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  1788. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  1789. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  1790. && param->port == ch->sport->port
  1791. && param->listen_id == ch->sport->sdev->cm_id
  1792. && ch->cm_id) {
  1793. if (srpt_disconnect_ch(ch) < 0)
  1794. continue;
  1795. pr_info("Relogin - closed existing channel %s\n",
  1796. ch->sess_name);
  1797. rsp->rsp_flags =
  1798. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  1799. }
  1800. }
  1801. mutex_unlock(&sdev->mutex);
  1802. } else
  1803. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  1804. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  1805. || *(__be64 *)(req->target_port_id + 8) !=
  1806. cpu_to_be64(srpt_service_guid)) {
  1807. rej->reason = cpu_to_be32(
  1808. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  1809. ret = -ENOMEM;
  1810. pr_err("rejected SRP_LOGIN_REQ because it"
  1811. " has an invalid target port identifier.\n");
  1812. goto reject;
  1813. }
  1814. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1815. if (!ch) {
  1816. rej->reason = cpu_to_be32(
  1817. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1818. pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
  1819. ret = -ENOMEM;
  1820. goto reject;
  1821. }
  1822. kref_init(&ch->kref);
  1823. ch->zw_cqe.done = srpt_zerolength_write_done;
  1824. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  1825. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  1826. memcpy(ch->t_port_id, req->target_port_id, 16);
  1827. ch->sport = &sdev->port[param->port - 1];
  1828. ch->cm_id = cm_id;
  1829. cm_id->context = ch;
  1830. /*
  1831. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  1832. * for the SRP protocol to the command queue size.
  1833. */
  1834. ch->rq_size = SRPT_RQ_SIZE;
  1835. spin_lock_init(&ch->spinlock);
  1836. ch->state = CH_CONNECTING;
  1837. INIT_LIST_HEAD(&ch->cmd_wait_list);
  1838. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  1839. ch->ioctx_ring = (struct srpt_send_ioctx **)
  1840. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1841. sizeof(*ch->ioctx_ring[0]),
  1842. ch->rsp_size, DMA_TO_DEVICE);
  1843. if (!ch->ioctx_ring)
  1844. goto free_ch;
  1845. ret = srpt_create_ch_ib(ch);
  1846. if (ret) {
  1847. rej->reason = cpu_to_be32(
  1848. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1849. pr_err("rejected SRP_LOGIN_REQ because creating"
  1850. " a new RDMA channel failed.\n");
  1851. goto free_ring;
  1852. }
  1853. ret = srpt_ch_qp_rtr(ch, ch->qp);
  1854. if (ret) {
  1855. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1856. pr_err("rejected SRP_LOGIN_REQ because enabling"
  1857. " RTR failed (error code = %d)\n", ret);
  1858. goto destroy_ib;
  1859. }
  1860. /*
  1861. * Use the initator port identifier as the session name, when
  1862. * checking against se_node_acl->initiatorname[] this can be
  1863. * with or without preceeding '0x'.
  1864. */
  1865. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  1866. be64_to_cpu(*(__be64 *)ch->i_port_id),
  1867. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  1868. pr_debug("registering session %s\n", ch->sess_name);
  1869. p = &ch->sess_name[0];
  1870. try_again:
  1871. ch->sess = target_alloc_session(&sport->port_tpg_1, ch->rq_size,
  1872. sizeof(struct srpt_send_ioctx),
  1873. TARGET_PROT_NORMAL, p, ch, NULL);
  1874. if (IS_ERR(ch->sess)) {
  1875. pr_info("Rejected login because no ACL has been"
  1876. " configured yet for initiator %s.\n", p);
  1877. /*
  1878. * XXX: Hack to retry of ch->i_port_id without leading '0x'
  1879. */
  1880. if (p == &ch->sess_name[0]) {
  1881. p += 2;
  1882. goto try_again;
  1883. }
  1884. rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
  1885. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
  1886. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  1887. goto destroy_ib;
  1888. }
  1889. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  1890. ch->sess_name, ch->cm_id);
  1891. /* create srp_login_response */
  1892. rsp->opcode = SRP_LOGIN_RSP;
  1893. rsp->tag = req->tag;
  1894. rsp->max_it_iu_len = req->req_it_iu_len;
  1895. rsp->max_ti_iu_len = req->req_it_iu_len;
  1896. ch->max_ti_iu_len = it_iu_len;
  1897. rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1898. | SRP_BUF_FORMAT_INDIRECT);
  1899. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  1900. atomic_set(&ch->req_lim, ch->rq_size);
  1901. atomic_set(&ch->req_lim_delta, 0);
  1902. /* create cm reply */
  1903. rep_param->qp_num = ch->qp->qp_num;
  1904. rep_param->private_data = (void *)rsp;
  1905. rep_param->private_data_len = sizeof(*rsp);
  1906. rep_param->rnr_retry_count = 7;
  1907. rep_param->flow_control = 1;
  1908. rep_param->failover_accepted = 0;
  1909. rep_param->srq = 1;
  1910. rep_param->responder_resources = 4;
  1911. rep_param->initiator_depth = 4;
  1912. ret = ib_send_cm_rep(cm_id, rep_param);
  1913. if (ret) {
  1914. pr_err("sending SRP_LOGIN_REQ response failed"
  1915. " (error code = %d)\n", ret);
  1916. goto release_channel;
  1917. }
  1918. mutex_lock(&sdev->mutex);
  1919. list_add_tail(&ch->list, &sdev->rch_list);
  1920. mutex_unlock(&sdev->mutex);
  1921. goto out;
  1922. release_channel:
  1923. srpt_disconnect_ch(ch);
  1924. transport_deregister_session_configfs(ch->sess);
  1925. transport_deregister_session(ch->sess);
  1926. ch->sess = NULL;
  1927. destroy_ib:
  1928. srpt_destroy_ch_ib(ch);
  1929. free_ring:
  1930. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1931. ch->sport->sdev, ch->rq_size,
  1932. ch->rsp_size, DMA_TO_DEVICE);
  1933. free_ch:
  1934. kfree(ch);
  1935. reject:
  1936. rej->opcode = SRP_LOGIN_REJ;
  1937. rej->tag = req->tag;
  1938. rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1939. | SRP_BUF_FORMAT_INDIRECT);
  1940. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  1941. (void *)rej, sizeof(*rej));
  1942. out:
  1943. kfree(rep_param);
  1944. kfree(rsp);
  1945. kfree(rej);
  1946. return ret;
  1947. }
  1948. static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
  1949. enum ib_cm_rej_reason reason,
  1950. const u8 *private_data,
  1951. u8 private_data_len)
  1952. {
  1953. char *priv = NULL;
  1954. int i;
  1955. if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
  1956. GFP_KERNEL))) {
  1957. for (i = 0; i < private_data_len; i++)
  1958. sprintf(priv + 3 * i, " %02x", private_data[i]);
  1959. }
  1960. pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
  1961. ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
  1962. "; private data" : "", priv ? priv : " (?)");
  1963. kfree(priv);
  1964. }
  1965. /**
  1966. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  1967. *
  1968. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  1969. * and that the recipient may begin transmitting (RTU = ready to use).
  1970. */
  1971. static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
  1972. {
  1973. int ret;
  1974. if (srpt_set_ch_state(ch, CH_LIVE)) {
  1975. ret = srpt_ch_qp_rts(ch, ch->qp);
  1976. if (ret == 0) {
  1977. /* Trigger wait list processing. */
  1978. ret = srpt_zerolength_write(ch);
  1979. WARN_ONCE(ret < 0, "%d\n", ret);
  1980. } else {
  1981. srpt_close_ch(ch);
  1982. }
  1983. }
  1984. }
  1985. /**
  1986. * srpt_cm_handler() - IB connection manager callback function.
  1987. *
  1988. * A non-zero return value will cause the caller destroy the CM ID.
  1989. *
  1990. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  1991. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  1992. * a non-zero value in any other case will trigger a race with the
  1993. * ib_destroy_cm_id() call in srpt_release_channel().
  1994. */
  1995. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  1996. {
  1997. struct srpt_rdma_ch *ch = cm_id->context;
  1998. int ret;
  1999. ret = 0;
  2000. switch (event->event) {
  2001. case IB_CM_REQ_RECEIVED:
  2002. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  2003. event->private_data);
  2004. break;
  2005. case IB_CM_REJ_RECEIVED:
  2006. srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
  2007. event->private_data,
  2008. IB_CM_REJ_PRIVATE_DATA_SIZE);
  2009. break;
  2010. case IB_CM_RTU_RECEIVED:
  2011. case IB_CM_USER_ESTABLISHED:
  2012. srpt_cm_rtu_recv(ch);
  2013. break;
  2014. case IB_CM_DREQ_RECEIVED:
  2015. srpt_disconnect_ch(ch);
  2016. break;
  2017. case IB_CM_DREP_RECEIVED:
  2018. pr_info("Received CM DREP message for ch %s-%d.\n",
  2019. ch->sess_name, ch->qp->qp_num);
  2020. srpt_close_ch(ch);
  2021. break;
  2022. case IB_CM_TIMEWAIT_EXIT:
  2023. pr_info("Received CM TimeWait exit for ch %s-%d.\n",
  2024. ch->sess_name, ch->qp->qp_num);
  2025. srpt_close_ch(ch);
  2026. break;
  2027. case IB_CM_REP_ERROR:
  2028. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  2029. ch->qp->qp_num);
  2030. break;
  2031. case IB_CM_DREQ_ERROR:
  2032. pr_info("Received CM DREQ ERROR event.\n");
  2033. break;
  2034. case IB_CM_MRA_RECEIVED:
  2035. pr_info("Received CM MRA event\n");
  2036. break;
  2037. default:
  2038. pr_err("received unrecognized CM event %d\n", event->event);
  2039. break;
  2040. }
  2041. return ret;
  2042. }
  2043. /**
  2044. * srpt_perform_rdmas() - Perform IB RDMA.
  2045. *
  2046. * Returns zero upon success or a negative number upon failure.
  2047. */
  2048. static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
  2049. struct srpt_send_ioctx *ioctx)
  2050. {
  2051. struct ib_send_wr *bad_wr;
  2052. int sq_wr_avail, ret, i;
  2053. enum dma_data_direction dir;
  2054. const int n_rdma = ioctx->n_rdma;
  2055. dir = ioctx->cmd.data_direction;
  2056. if (dir == DMA_TO_DEVICE) {
  2057. /* write */
  2058. ret = -ENOMEM;
  2059. sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
  2060. if (sq_wr_avail < 0) {
  2061. pr_warn("IB send queue full (needed %d)\n",
  2062. n_rdma);
  2063. goto out;
  2064. }
  2065. }
  2066. for (i = 0; i < n_rdma; i++) {
  2067. struct ib_send_wr *wr = &ioctx->rdma_wrs[i].wr;
  2068. wr->opcode = (dir == DMA_FROM_DEVICE) ?
  2069. IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
  2070. if (i == n_rdma - 1) {
  2071. /* only get completion event for the last rdma read */
  2072. if (dir == DMA_TO_DEVICE) {
  2073. wr->send_flags = IB_SEND_SIGNALED;
  2074. ioctx->rdma_cqe.done = srpt_rdma_read_done;
  2075. } else {
  2076. ioctx->rdma_cqe.done = srpt_rdma_write_done;
  2077. }
  2078. wr->wr_cqe = &ioctx->rdma_cqe;
  2079. wr->next = NULL;
  2080. } else {
  2081. wr->wr_cqe = NULL;
  2082. wr->next = &ioctx->rdma_wrs[i + 1].wr;
  2083. }
  2084. }
  2085. ret = ib_post_send(ch->qp, &ioctx->rdma_wrs->wr, &bad_wr);
  2086. if (ret)
  2087. pr_err("%s[%d]: ib_post_send() returned %d for %d/%d\n",
  2088. __func__, __LINE__, ret, i, n_rdma);
  2089. out:
  2090. if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
  2091. atomic_add(n_rdma, &ch->sq_wr_avail);
  2092. return ret;
  2093. }
  2094. /**
  2095. * srpt_xfer_data() - Start data transfer from initiator to target.
  2096. */
  2097. static int srpt_xfer_data(struct srpt_rdma_ch *ch,
  2098. struct srpt_send_ioctx *ioctx)
  2099. {
  2100. int ret;
  2101. ret = srpt_map_sg_to_ib_sge(ch, ioctx);
  2102. if (ret) {
  2103. pr_err("%s[%d] ret=%d\n", __func__, __LINE__, ret);
  2104. goto out;
  2105. }
  2106. ret = srpt_perform_rdmas(ch, ioctx);
  2107. if (ret) {
  2108. if (ret == -EAGAIN || ret == -ENOMEM)
  2109. pr_info("%s[%d] queue full -- ret=%d\n",
  2110. __func__, __LINE__, ret);
  2111. else
  2112. pr_err("%s[%d] fatal error -- ret=%d\n",
  2113. __func__, __LINE__, ret);
  2114. goto out_unmap;
  2115. }
  2116. out:
  2117. return ret;
  2118. out_unmap:
  2119. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2120. goto out;
  2121. }
  2122. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2123. {
  2124. struct srpt_send_ioctx *ioctx;
  2125. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2126. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  2127. }
  2128. /*
  2129. * srpt_write_pending() - Start data transfer from initiator to target (write).
  2130. */
  2131. static int srpt_write_pending(struct se_cmd *se_cmd)
  2132. {
  2133. struct srpt_send_ioctx *ioctx =
  2134. container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2135. struct srpt_rdma_ch *ch = ioctx->ch;
  2136. enum srpt_command_state new_state;
  2137. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2138. WARN_ON(new_state == SRPT_STATE_DONE);
  2139. return srpt_xfer_data(ch, ioctx);
  2140. }
  2141. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2142. {
  2143. switch (tcm_mgmt_status) {
  2144. case TMR_FUNCTION_COMPLETE:
  2145. return SRP_TSK_MGMT_SUCCESS;
  2146. case TMR_FUNCTION_REJECTED:
  2147. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2148. }
  2149. return SRP_TSK_MGMT_FAILED;
  2150. }
  2151. /**
  2152. * srpt_queue_response() - Transmits the response to a SCSI command.
  2153. *
  2154. * Callback function called by the TCM core. Must not block since it can be
  2155. * invoked on the context of the IB completion handler.
  2156. */
  2157. static void srpt_queue_response(struct se_cmd *cmd)
  2158. {
  2159. struct srpt_rdma_ch *ch;
  2160. struct srpt_send_ioctx *ioctx;
  2161. enum srpt_command_state state;
  2162. unsigned long flags;
  2163. int ret;
  2164. enum dma_data_direction dir;
  2165. int resp_len;
  2166. u8 srp_tm_status;
  2167. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2168. ch = ioctx->ch;
  2169. BUG_ON(!ch);
  2170. spin_lock_irqsave(&ioctx->spinlock, flags);
  2171. state = ioctx->state;
  2172. switch (state) {
  2173. case SRPT_STATE_NEW:
  2174. case SRPT_STATE_DATA_IN:
  2175. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2176. break;
  2177. case SRPT_STATE_MGMT:
  2178. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2179. break;
  2180. default:
  2181. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2182. ch, ioctx->ioctx.index, ioctx->state);
  2183. break;
  2184. }
  2185. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  2186. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  2187. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  2188. atomic_inc(&ch->req_lim_delta);
  2189. srpt_abort_cmd(ioctx);
  2190. return;
  2191. }
  2192. dir = ioctx->cmd.data_direction;
  2193. /* For read commands, transfer the data to the initiator. */
  2194. if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
  2195. !ioctx->queue_status_only) {
  2196. ret = srpt_xfer_data(ch, ioctx);
  2197. if (ret) {
  2198. pr_err("xfer_data failed for tag %llu\n",
  2199. ioctx->cmd.tag);
  2200. return;
  2201. }
  2202. }
  2203. if (state != SRPT_STATE_MGMT)
  2204. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  2205. cmd->scsi_status);
  2206. else {
  2207. srp_tm_status
  2208. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2209. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2210. ioctx->cmd.tag);
  2211. }
  2212. ret = srpt_post_send(ch, ioctx, resp_len);
  2213. if (ret) {
  2214. pr_err("sending cmd response failed for tag %llu\n",
  2215. ioctx->cmd.tag);
  2216. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2217. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2218. target_put_sess_cmd(&ioctx->cmd);
  2219. }
  2220. }
  2221. static int srpt_queue_data_in(struct se_cmd *cmd)
  2222. {
  2223. srpt_queue_response(cmd);
  2224. return 0;
  2225. }
  2226. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2227. {
  2228. srpt_queue_response(cmd);
  2229. }
  2230. static void srpt_aborted_task(struct se_cmd *cmd)
  2231. {
  2232. struct srpt_send_ioctx *ioctx = container_of(cmd,
  2233. struct srpt_send_ioctx, cmd);
  2234. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  2235. }
  2236. static int srpt_queue_status(struct se_cmd *cmd)
  2237. {
  2238. struct srpt_send_ioctx *ioctx;
  2239. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2240. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2241. if (cmd->se_cmd_flags &
  2242. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2243. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2244. ioctx->queue_status_only = true;
  2245. srpt_queue_response(cmd);
  2246. return 0;
  2247. }
  2248. static void srpt_refresh_port_work(struct work_struct *work)
  2249. {
  2250. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2251. srpt_refresh_port(sport);
  2252. }
  2253. /**
  2254. * srpt_release_sdev() - Free the channel resources associated with a target.
  2255. */
  2256. static int srpt_release_sdev(struct srpt_device *sdev)
  2257. {
  2258. int i, res;
  2259. WARN_ON_ONCE(irqs_disabled());
  2260. BUG_ON(!sdev);
  2261. mutex_lock(&sdev->mutex);
  2262. for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
  2263. sdev->port[i].enabled = false;
  2264. __srpt_close_all_ch(sdev);
  2265. mutex_unlock(&sdev->mutex);
  2266. res = wait_event_interruptible(sdev->ch_releaseQ,
  2267. list_empty_careful(&sdev->rch_list));
  2268. if (res)
  2269. pr_err("%s: interrupted.\n", __func__);
  2270. return 0;
  2271. }
  2272. static struct srpt_port *__srpt_lookup_port(const char *name)
  2273. {
  2274. struct ib_device *dev;
  2275. struct srpt_device *sdev;
  2276. struct srpt_port *sport;
  2277. int i;
  2278. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2279. dev = sdev->device;
  2280. if (!dev)
  2281. continue;
  2282. for (i = 0; i < dev->phys_port_cnt; i++) {
  2283. sport = &sdev->port[i];
  2284. if (!strcmp(sport->port_guid, name))
  2285. return sport;
  2286. }
  2287. }
  2288. return NULL;
  2289. }
  2290. static struct srpt_port *srpt_lookup_port(const char *name)
  2291. {
  2292. struct srpt_port *sport;
  2293. spin_lock(&srpt_dev_lock);
  2294. sport = __srpt_lookup_port(name);
  2295. spin_unlock(&srpt_dev_lock);
  2296. return sport;
  2297. }
  2298. /**
  2299. * srpt_add_one() - Infiniband device addition callback function.
  2300. */
  2301. static void srpt_add_one(struct ib_device *device)
  2302. {
  2303. struct srpt_device *sdev;
  2304. struct srpt_port *sport;
  2305. struct ib_srq_init_attr srq_attr;
  2306. int i;
  2307. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2308. device->dma_ops);
  2309. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  2310. if (!sdev)
  2311. goto err;
  2312. sdev->device = device;
  2313. INIT_LIST_HEAD(&sdev->rch_list);
  2314. init_waitqueue_head(&sdev->ch_releaseQ);
  2315. mutex_init(&sdev->mutex);
  2316. sdev->pd = ib_alloc_pd(device);
  2317. if (IS_ERR(sdev->pd))
  2318. goto free_dev;
  2319. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2320. srq_attr.event_handler = srpt_srq_event;
  2321. srq_attr.srq_context = (void *)sdev;
  2322. srq_attr.attr.max_wr = sdev->srq_size;
  2323. srq_attr.attr.max_sge = 1;
  2324. srq_attr.attr.srq_limit = 0;
  2325. srq_attr.srq_type = IB_SRQT_BASIC;
  2326. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2327. if (IS_ERR(sdev->srq))
  2328. goto err_pd;
  2329. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2330. __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
  2331. device->name);
  2332. if (!srpt_service_guid)
  2333. srpt_service_guid = be64_to_cpu(device->node_guid);
  2334. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2335. if (IS_ERR(sdev->cm_id))
  2336. goto err_srq;
  2337. /* print out target login information */
  2338. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2339. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2340. srpt_service_guid, srpt_service_guid);
  2341. /*
  2342. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2343. * to identify this target. We currently use the guid of the first HCA
  2344. * in the system as service_id; therefore, the target_id will change
  2345. * if this HCA is gone bad and replaced by different HCA
  2346. */
  2347. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
  2348. goto err_cm;
  2349. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2350. srpt_event_handler);
  2351. if (ib_register_event_handler(&sdev->event_handler))
  2352. goto err_cm;
  2353. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2354. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2355. sizeof(*sdev->ioctx_ring[0]),
  2356. srp_max_req_size, DMA_FROM_DEVICE);
  2357. if (!sdev->ioctx_ring)
  2358. goto err_event;
  2359. for (i = 0; i < sdev->srq_size; ++i)
  2360. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2361. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2362. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2363. sport = &sdev->port[i - 1];
  2364. sport->sdev = sdev;
  2365. sport->port = i;
  2366. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2367. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2368. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2369. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2370. if (srpt_refresh_port(sport)) {
  2371. pr_err("MAD registration failed for %s-%d.\n",
  2372. sdev->device->name, i);
  2373. goto err_ring;
  2374. }
  2375. snprintf(sport->port_guid, sizeof(sport->port_guid),
  2376. "0x%016llx%016llx",
  2377. be64_to_cpu(sport->gid.global.subnet_prefix),
  2378. be64_to_cpu(sport->gid.global.interface_id));
  2379. }
  2380. spin_lock(&srpt_dev_lock);
  2381. list_add_tail(&sdev->list, &srpt_dev_list);
  2382. spin_unlock(&srpt_dev_lock);
  2383. out:
  2384. ib_set_client_data(device, &srpt_client, sdev);
  2385. pr_debug("added %s.\n", device->name);
  2386. return;
  2387. err_ring:
  2388. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2389. sdev->srq_size, srp_max_req_size,
  2390. DMA_FROM_DEVICE);
  2391. err_event:
  2392. ib_unregister_event_handler(&sdev->event_handler);
  2393. err_cm:
  2394. ib_destroy_cm_id(sdev->cm_id);
  2395. err_srq:
  2396. ib_destroy_srq(sdev->srq);
  2397. err_pd:
  2398. ib_dealloc_pd(sdev->pd);
  2399. free_dev:
  2400. kfree(sdev);
  2401. err:
  2402. sdev = NULL;
  2403. pr_info("%s(%s) failed.\n", __func__, device->name);
  2404. goto out;
  2405. }
  2406. /**
  2407. * srpt_remove_one() - InfiniBand device removal callback function.
  2408. */
  2409. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2410. {
  2411. struct srpt_device *sdev = client_data;
  2412. int i;
  2413. if (!sdev) {
  2414. pr_info("%s(%s): nothing to do.\n", __func__, device->name);
  2415. return;
  2416. }
  2417. srpt_unregister_mad_agent(sdev);
  2418. ib_unregister_event_handler(&sdev->event_handler);
  2419. /* Cancel any work queued by the just unregistered IB event handler. */
  2420. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2421. cancel_work_sync(&sdev->port[i].work);
  2422. ib_destroy_cm_id(sdev->cm_id);
  2423. /*
  2424. * Unregistering a target must happen after destroying sdev->cm_id
  2425. * such that no new SRP_LOGIN_REQ information units can arrive while
  2426. * destroying the target.
  2427. */
  2428. spin_lock(&srpt_dev_lock);
  2429. list_del(&sdev->list);
  2430. spin_unlock(&srpt_dev_lock);
  2431. srpt_release_sdev(sdev);
  2432. ib_destroy_srq(sdev->srq);
  2433. ib_dealloc_pd(sdev->pd);
  2434. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2435. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2436. sdev->ioctx_ring = NULL;
  2437. kfree(sdev);
  2438. }
  2439. static struct ib_client srpt_client = {
  2440. .name = DRV_NAME,
  2441. .add = srpt_add_one,
  2442. .remove = srpt_remove_one
  2443. };
  2444. static int srpt_check_true(struct se_portal_group *se_tpg)
  2445. {
  2446. return 1;
  2447. }
  2448. static int srpt_check_false(struct se_portal_group *se_tpg)
  2449. {
  2450. return 0;
  2451. }
  2452. static char *srpt_get_fabric_name(void)
  2453. {
  2454. return "srpt";
  2455. }
  2456. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2457. {
  2458. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2459. return sport->port_guid;
  2460. }
  2461. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2462. {
  2463. return 1;
  2464. }
  2465. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2466. {
  2467. return 1;
  2468. }
  2469. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2470. {
  2471. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2472. struct srpt_send_ioctx, cmd);
  2473. struct srpt_rdma_ch *ch = ioctx->ch;
  2474. struct se_session *se_sess = ch->sess;
  2475. WARN_ON(ioctx->state != SRPT_STATE_DONE);
  2476. WARN_ON(ioctx->mapped_sg_count != 0);
  2477. if (ioctx->n_rbuf > 1) {
  2478. kfree(ioctx->rbufs);
  2479. ioctx->rbufs = NULL;
  2480. ioctx->n_rbuf = 0;
  2481. }
  2482. percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
  2483. }
  2484. /**
  2485. * srpt_close_session() - Forcibly close a session.
  2486. *
  2487. * Callback function invoked by the TCM core to clean up sessions associated
  2488. * with a node ACL when the user invokes
  2489. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2490. */
  2491. static void srpt_close_session(struct se_session *se_sess)
  2492. {
  2493. DECLARE_COMPLETION_ONSTACK(release_done);
  2494. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2495. struct srpt_device *sdev = ch->sport->sdev;
  2496. bool wait;
  2497. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  2498. ch->state);
  2499. mutex_lock(&sdev->mutex);
  2500. BUG_ON(ch->release_done);
  2501. ch->release_done = &release_done;
  2502. wait = !list_empty(&ch->list);
  2503. srpt_disconnect_ch(ch);
  2504. mutex_unlock(&sdev->mutex);
  2505. if (!wait)
  2506. return;
  2507. while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
  2508. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  2509. ch->sess_name, ch->qp->qp_num, ch->state);
  2510. }
  2511. /**
  2512. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  2513. *
  2514. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2515. * This object represents an arbitrary integer used to uniquely identify a
  2516. * particular attached remote initiator port to a particular SCSI target port
  2517. * within a particular SCSI target device within a particular SCSI instance.
  2518. */
  2519. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2520. {
  2521. return 0;
  2522. }
  2523. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2524. {
  2525. }
  2526. /* Note: only used from inside debug printk's by the TCM core. */
  2527. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2528. {
  2529. struct srpt_send_ioctx *ioctx;
  2530. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2531. return srpt_get_cmd_state(ioctx);
  2532. }
  2533. /**
  2534. * srpt_parse_i_port_id() - Parse an initiator port ID.
  2535. * @name: ASCII representation of a 128-bit initiator port ID.
  2536. * @i_port_id: Binary 128-bit port ID.
  2537. */
  2538. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2539. {
  2540. const char *p;
  2541. unsigned len, count, leading_zero_bytes;
  2542. int ret, rc;
  2543. p = name;
  2544. if (strncasecmp(p, "0x", 2) == 0)
  2545. p += 2;
  2546. ret = -EINVAL;
  2547. len = strlen(p);
  2548. if (len % 2)
  2549. goto out;
  2550. count = min(len / 2, 16U);
  2551. leading_zero_bytes = 16 - count;
  2552. memset(i_port_id, 0, leading_zero_bytes);
  2553. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2554. if (rc < 0)
  2555. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  2556. ret = 0;
  2557. out:
  2558. return ret;
  2559. }
  2560. /*
  2561. * configfs callback function invoked for
  2562. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2563. */
  2564. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2565. {
  2566. u8 i_port_id[16];
  2567. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  2568. pr_err("invalid initiator port ID %s\n", name);
  2569. return -EINVAL;
  2570. }
  2571. return 0;
  2572. }
  2573. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  2574. char *page)
  2575. {
  2576. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2577. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2578. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  2579. }
  2580. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  2581. const char *page, size_t count)
  2582. {
  2583. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2584. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2585. unsigned long val;
  2586. int ret;
  2587. ret = kstrtoul(page, 0, &val);
  2588. if (ret < 0) {
  2589. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2590. return -EINVAL;
  2591. }
  2592. if (val > MAX_SRPT_RDMA_SIZE) {
  2593. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  2594. MAX_SRPT_RDMA_SIZE);
  2595. return -EINVAL;
  2596. }
  2597. if (val < DEFAULT_MAX_RDMA_SIZE) {
  2598. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  2599. val, DEFAULT_MAX_RDMA_SIZE);
  2600. return -EINVAL;
  2601. }
  2602. sport->port_attrib.srp_max_rdma_size = val;
  2603. return count;
  2604. }
  2605. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  2606. char *page)
  2607. {
  2608. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2609. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2610. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  2611. }
  2612. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  2613. const char *page, size_t count)
  2614. {
  2615. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2616. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2617. unsigned long val;
  2618. int ret;
  2619. ret = kstrtoul(page, 0, &val);
  2620. if (ret < 0) {
  2621. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2622. return -EINVAL;
  2623. }
  2624. if (val > MAX_SRPT_RSP_SIZE) {
  2625. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  2626. MAX_SRPT_RSP_SIZE);
  2627. return -EINVAL;
  2628. }
  2629. if (val < MIN_MAX_RSP_SIZE) {
  2630. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  2631. MIN_MAX_RSP_SIZE);
  2632. return -EINVAL;
  2633. }
  2634. sport->port_attrib.srp_max_rsp_size = val;
  2635. return count;
  2636. }
  2637. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  2638. char *page)
  2639. {
  2640. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2641. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2642. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  2643. }
  2644. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  2645. const char *page, size_t count)
  2646. {
  2647. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2648. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2649. unsigned long val;
  2650. int ret;
  2651. ret = kstrtoul(page, 0, &val);
  2652. if (ret < 0) {
  2653. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2654. return -EINVAL;
  2655. }
  2656. if (val > MAX_SRPT_SRQ_SIZE) {
  2657. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  2658. MAX_SRPT_SRQ_SIZE);
  2659. return -EINVAL;
  2660. }
  2661. if (val < MIN_SRPT_SRQ_SIZE) {
  2662. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  2663. MIN_SRPT_SRQ_SIZE);
  2664. return -EINVAL;
  2665. }
  2666. sport->port_attrib.srp_sq_size = val;
  2667. return count;
  2668. }
  2669. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  2670. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  2671. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  2672. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  2673. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  2674. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  2675. &srpt_tpg_attrib_attr_srp_sq_size,
  2676. NULL,
  2677. };
  2678. static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
  2679. {
  2680. struct se_portal_group *se_tpg = to_tpg(item);
  2681. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2682. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  2683. }
  2684. static ssize_t srpt_tpg_enable_store(struct config_item *item,
  2685. const char *page, size_t count)
  2686. {
  2687. struct se_portal_group *se_tpg = to_tpg(item);
  2688. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2689. struct srpt_device *sdev = sport->sdev;
  2690. struct srpt_rdma_ch *ch;
  2691. unsigned long tmp;
  2692. int ret;
  2693. ret = kstrtoul(page, 0, &tmp);
  2694. if (ret < 0) {
  2695. pr_err("Unable to extract srpt_tpg_store_enable\n");
  2696. return -EINVAL;
  2697. }
  2698. if ((tmp != 0) && (tmp != 1)) {
  2699. pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  2700. return -EINVAL;
  2701. }
  2702. if (sport->enabled == tmp)
  2703. goto out;
  2704. sport->enabled = tmp;
  2705. if (sport->enabled)
  2706. goto out;
  2707. mutex_lock(&sdev->mutex);
  2708. list_for_each_entry(ch, &sdev->rch_list, list) {
  2709. if (ch->sport == sport) {
  2710. pr_debug("%s: ch %p %s-%d\n", __func__, ch,
  2711. ch->sess_name, ch->qp->qp_num);
  2712. srpt_disconnect_ch(ch);
  2713. srpt_close_ch(ch);
  2714. }
  2715. }
  2716. mutex_unlock(&sdev->mutex);
  2717. out:
  2718. return count;
  2719. }
  2720. CONFIGFS_ATTR(srpt_tpg_, enable);
  2721. static struct configfs_attribute *srpt_tpg_attrs[] = {
  2722. &srpt_tpg_attr_enable,
  2723. NULL,
  2724. };
  2725. /**
  2726. * configfs callback invoked for
  2727. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  2728. */
  2729. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  2730. struct config_group *group,
  2731. const char *name)
  2732. {
  2733. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2734. int res;
  2735. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  2736. res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
  2737. if (res)
  2738. return ERR_PTR(res);
  2739. return &sport->port_tpg_1;
  2740. }
  2741. /**
  2742. * configfs callback invoked for
  2743. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  2744. */
  2745. static void srpt_drop_tpg(struct se_portal_group *tpg)
  2746. {
  2747. struct srpt_port *sport = container_of(tpg,
  2748. struct srpt_port, port_tpg_1);
  2749. sport->enabled = false;
  2750. core_tpg_deregister(&sport->port_tpg_1);
  2751. }
  2752. /**
  2753. * configfs callback invoked for
  2754. * mkdir /sys/kernel/config/target/$driver/$port
  2755. */
  2756. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  2757. struct config_group *group,
  2758. const char *name)
  2759. {
  2760. struct srpt_port *sport;
  2761. int ret;
  2762. sport = srpt_lookup_port(name);
  2763. pr_debug("make_tport(%s)\n", name);
  2764. ret = -EINVAL;
  2765. if (!sport)
  2766. goto err;
  2767. return &sport->port_wwn;
  2768. err:
  2769. return ERR_PTR(ret);
  2770. }
  2771. /**
  2772. * configfs callback invoked for
  2773. * rmdir /sys/kernel/config/target/$driver/$port
  2774. */
  2775. static void srpt_drop_tport(struct se_wwn *wwn)
  2776. {
  2777. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2778. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  2779. }
  2780. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  2781. {
  2782. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  2783. }
  2784. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  2785. static struct configfs_attribute *srpt_wwn_attrs[] = {
  2786. &srpt_wwn_attr_version,
  2787. NULL,
  2788. };
  2789. static const struct target_core_fabric_ops srpt_template = {
  2790. .module = THIS_MODULE,
  2791. .name = "srpt",
  2792. .get_fabric_name = srpt_get_fabric_name,
  2793. .tpg_get_wwn = srpt_get_fabric_wwn,
  2794. .tpg_get_tag = srpt_get_tag,
  2795. .tpg_check_demo_mode = srpt_check_false,
  2796. .tpg_check_demo_mode_cache = srpt_check_true,
  2797. .tpg_check_demo_mode_write_protect = srpt_check_true,
  2798. .tpg_check_prod_mode_write_protect = srpt_check_false,
  2799. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  2800. .release_cmd = srpt_release_cmd,
  2801. .check_stop_free = srpt_check_stop_free,
  2802. .shutdown_session = srpt_shutdown_session,
  2803. .close_session = srpt_close_session,
  2804. .sess_get_index = srpt_sess_get_index,
  2805. .sess_get_initiator_sid = NULL,
  2806. .write_pending = srpt_write_pending,
  2807. .write_pending_status = srpt_write_pending_status,
  2808. .set_default_node_attributes = srpt_set_default_node_attrs,
  2809. .get_cmd_state = srpt_get_tcm_cmd_state,
  2810. .queue_data_in = srpt_queue_data_in,
  2811. .queue_status = srpt_queue_status,
  2812. .queue_tm_rsp = srpt_queue_tm_rsp,
  2813. .aborted_task = srpt_aborted_task,
  2814. /*
  2815. * Setup function pointers for generic logic in
  2816. * target_core_fabric_configfs.c
  2817. */
  2818. .fabric_make_wwn = srpt_make_tport,
  2819. .fabric_drop_wwn = srpt_drop_tport,
  2820. .fabric_make_tpg = srpt_make_tpg,
  2821. .fabric_drop_tpg = srpt_drop_tpg,
  2822. .fabric_init_nodeacl = srpt_init_nodeacl,
  2823. .tfc_wwn_attrs = srpt_wwn_attrs,
  2824. .tfc_tpg_base_attrs = srpt_tpg_attrs,
  2825. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  2826. };
  2827. /**
  2828. * srpt_init_module() - Kernel module initialization.
  2829. *
  2830. * Note: Since ib_register_client() registers callback functions, and since at
  2831. * least one of these callback functions (srpt_add_one()) calls target core
  2832. * functions, this driver must be registered with the target core before
  2833. * ib_register_client() is called.
  2834. */
  2835. static int __init srpt_init_module(void)
  2836. {
  2837. int ret;
  2838. ret = -EINVAL;
  2839. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  2840. pr_err("invalid value %d for kernel module parameter"
  2841. " srp_max_req_size -- must be at least %d.\n",
  2842. srp_max_req_size, MIN_MAX_REQ_SIZE);
  2843. goto out;
  2844. }
  2845. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  2846. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  2847. pr_err("invalid value %d for kernel module parameter"
  2848. " srpt_srq_size -- must be in the range [%d..%d].\n",
  2849. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  2850. goto out;
  2851. }
  2852. ret = target_register_template(&srpt_template);
  2853. if (ret)
  2854. goto out;
  2855. ret = ib_register_client(&srpt_client);
  2856. if (ret) {
  2857. pr_err("couldn't register IB client\n");
  2858. goto out_unregister_target;
  2859. }
  2860. return 0;
  2861. out_unregister_target:
  2862. target_unregister_template(&srpt_template);
  2863. out:
  2864. return ret;
  2865. }
  2866. static void __exit srpt_cleanup_module(void)
  2867. {
  2868. ib_unregister_client(&srpt_client);
  2869. target_unregister_template(&srpt_template);
  2870. }
  2871. module_init(srpt_init_module);
  2872. module_exit(srpt_cleanup_module);