ib_srpt.c 83 KB

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