ib_srpt.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <scsi/scsi_proto.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_fabric.h>
  47. #include "ib_srpt.h"
  48. /* Name of this kernel module. */
  49. #define DRV_NAME "ib_srpt"
  50. #define DRV_VERSION "2.0.0"
  51. #define DRV_RELDATE "2011-02-14"
  52. #define SRPT_ID_STRING "Linux SRP target"
  53. #undef pr_fmt
  54. #define pr_fmt(fmt) DRV_NAME " " fmt
  55. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  56. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
  57. "v" DRV_VERSION " (" DRV_RELDATE ")");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. /*
  60. * Global Variables
  61. */
  62. static u64 srpt_service_guid;
  63. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  64. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  65. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  66. module_param(srp_max_req_size, int, 0444);
  67. MODULE_PARM_DESC(srp_max_req_size,
  68. "Maximum size of SRP request messages in bytes.");
  69. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  70. module_param(srpt_srq_size, int, 0444);
  71. MODULE_PARM_DESC(srpt_srq_size,
  72. "Shared receive queue (SRQ) size.");
  73. static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
  74. {
  75. return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
  76. }
  77. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  78. 0444);
  79. MODULE_PARM_DESC(srpt_service_guid,
  80. "Using this value for ioc_guid, id_ext, and cm_listen_id"
  81. " instead of using the node_guid of the first HCA.");
  82. static struct ib_client srpt_client;
  83. static void srpt_release_cmd(struct se_cmd *se_cmd);
  84. static void srpt_free_ch(struct kref *kref);
  85. static int srpt_queue_status(struct se_cmd *cmd);
  86. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  87. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
  88. static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
  89. /*
  90. * The only allowed channel state changes are those that change the channel
  91. * state into a state with a higher numerical value. Hence the new > prev test.
  92. */
  93. static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
  94. {
  95. unsigned long flags;
  96. enum rdma_ch_state prev;
  97. bool changed = false;
  98. spin_lock_irqsave(&ch->spinlock, flags);
  99. prev = ch->state;
  100. if (new > prev) {
  101. ch->state = new;
  102. changed = true;
  103. }
  104. spin_unlock_irqrestore(&ch->spinlock, flags);
  105. return changed;
  106. }
  107. /**
  108. * srpt_event_handler() - Asynchronous IB event callback function.
  109. *
  110. * Callback function called by the InfiniBand core when an asynchronous IB
  111. * event occurs. This callback may occur in interrupt context. See also
  112. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  113. * Architecture Specification.
  114. */
  115. static void srpt_event_handler(struct ib_event_handler *handler,
  116. struct ib_event *event)
  117. {
  118. struct srpt_device *sdev;
  119. struct srpt_port *sport;
  120. sdev = ib_get_client_data(event->device, &srpt_client);
  121. if (!sdev || sdev->device != event->device)
  122. return;
  123. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  124. sdev->device->name);
  125. switch (event->event) {
  126. case IB_EVENT_PORT_ERR:
  127. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  128. sport = &sdev->port[event->element.port_num - 1];
  129. sport->lid = 0;
  130. sport->sm_lid = 0;
  131. }
  132. break;
  133. case IB_EVENT_PORT_ACTIVE:
  134. case IB_EVENT_LID_CHANGE:
  135. case IB_EVENT_PKEY_CHANGE:
  136. case IB_EVENT_SM_CHANGE:
  137. case IB_EVENT_CLIENT_REREGISTER:
  138. case IB_EVENT_GID_CHANGE:
  139. /* Refresh port data asynchronously. */
  140. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  141. sport = &sdev->port[event->element.port_num - 1];
  142. if (!sport->lid && !sport->sm_lid)
  143. schedule_work(&sport->work);
  144. }
  145. break;
  146. default:
  147. pr_err("received unrecognized IB event %d\n",
  148. event->event);
  149. break;
  150. }
  151. }
  152. /**
  153. * srpt_srq_event() - SRQ event callback function.
  154. */
  155. static void srpt_srq_event(struct ib_event *event, void *ctx)
  156. {
  157. pr_info("SRQ event %d\n", event->event);
  158. }
  159. static const char *get_ch_state_name(enum rdma_ch_state s)
  160. {
  161. switch (s) {
  162. case CH_CONNECTING:
  163. return "connecting";
  164. case CH_LIVE:
  165. return "live";
  166. case CH_DISCONNECTING:
  167. return "disconnecting";
  168. case CH_DRAINING:
  169. return "draining";
  170. case CH_DISCONNECTED:
  171. return "disconnected";
  172. }
  173. return "???";
  174. }
  175. /**
  176. * srpt_qp_event() - QP event callback function.
  177. */
  178. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  179. {
  180. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  181. event->event, ch->cm_id, ch->sess_name, ch->state);
  182. switch (event->event) {
  183. case IB_EVENT_COMM_EST:
  184. ib_cm_notify(ch->cm_id, event->event);
  185. break;
  186. case IB_EVENT_QP_LAST_WQE_REACHED:
  187. pr_debug("%s-%d, state %s: received Last WQE event.\n",
  188. ch->sess_name, ch->qp->qp_num,
  189. get_ch_state_name(ch->state));
  190. break;
  191. default:
  192. pr_err("received unrecognized IB QP event %d\n", event->event);
  193. break;
  194. }
  195. }
  196. /**
  197. * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
  198. *
  199. * @slot: one-based slot number.
  200. * @value: four-bit value.
  201. *
  202. * Copies the lowest four bits of value in element slot of the array of four
  203. * bit elements called c_list (controller list). The index slot is one-based.
  204. */
  205. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  206. {
  207. u16 id;
  208. u8 tmp;
  209. id = (slot - 1) / 2;
  210. if (slot & 0x1) {
  211. tmp = c_list[id] & 0xf;
  212. c_list[id] = (value << 4) | tmp;
  213. } else {
  214. tmp = c_list[id] & 0xf0;
  215. c_list[id] = (value & 0xf) | tmp;
  216. }
  217. }
  218. /**
  219. * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
  220. *
  221. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  222. * Specification.
  223. */
  224. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  225. {
  226. struct ib_class_port_info *cif;
  227. cif = (struct ib_class_port_info *)mad->data;
  228. memset(cif, 0, sizeof(*cif));
  229. cif->base_version = 1;
  230. cif->class_version = 1;
  231. ib_set_cpi_resp_time(cif, 20);
  232. mad->mad_hdr.status = 0;
  233. }
  234. /**
  235. * srpt_get_iou() - Write IOUnitInfo to a management datagram.
  236. *
  237. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  238. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  239. */
  240. static void srpt_get_iou(struct ib_dm_mad *mad)
  241. {
  242. struct ib_dm_iou_info *ioui;
  243. u8 slot;
  244. int i;
  245. ioui = (struct ib_dm_iou_info *)mad->data;
  246. ioui->change_id = cpu_to_be16(1);
  247. ioui->max_controllers = 16;
  248. /* set present for slot 1 and empty for the rest */
  249. srpt_set_ioc(ioui->controller_list, 1, 1);
  250. for (i = 1, slot = 2; i < 16; i++, slot++)
  251. srpt_set_ioc(ioui->controller_list, slot, 0);
  252. mad->mad_hdr.status = 0;
  253. }
  254. /**
  255. * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
  256. *
  257. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  258. * Architecture Specification. See also section B.7, table B.7 in the SRP
  259. * r16a document.
  260. */
  261. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  262. struct ib_dm_mad *mad)
  263. {
  264. struct srpt_device *sdev = sport->sdev;
  265. struct ib_dm_ioc_profile *iocp;
  266. iocp = (struct ib_dm_ioc_profile *)mad->data;
  267. if (!slot || slot > 16) {
  268. mad->mad_hdr.status
  269. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  270. return;
  271. }
  272. if (slot > 2) {
  273. mad->mad_hdr.status
  274. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  275. return;
  276. }
  277. memset(iocp, 0, sizeof(*iocp));
  278. strcpy(iocp->id_string, SRPT_ID_STRING);
  279. iocp->guid = cpu_to_be64(srpt_service_guid);
  280. iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  281. iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
  282. iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
  283. iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  284. iocp->subsys_device_id = 0x0;
  285. iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  286. iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
  287. iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
  288. iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
  289. iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
  290. iocp->rdma_read_depth = 4;
  291. iocp->send_size = cpu_to_be32(srp_max_req_size);
  292. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  293. 1U << 24));
  294. iocp->num_svc_entries = 1;
  295. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  296. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  297. mad->mad_hdr.status = 0;
  298. }
  299. /**
  300. * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
  301. *
  302. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  303. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  304. */
  305. static void srpt_get_svc_entries(u64 ioc_guid,
  306. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  307. {
  308. struct ib_dm_svc_entries *svc_entries;
  309. WARN_ON(!ioc_guid);
  310. if (!slot || slot > 16) {
  311. mad->mad_hdr.status
  312. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  313. return;
  314. }
  315. if (slot > 2 || lo > hi || hi > 1) {
  316. mad->mad_hdr.status
  317. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  318. return;
  319. }
  320. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  321. memset(svc_entries, 0, sizeof(*svc_entries));
  322. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  323. snprintf(svc_entries->service_entries[0].name,
  324. sizeof(svc_entries->service_entries[0].name),
  325. "%s%016llx",
  326. SRP_SERVICE_NAME_PREFIX,
  327. ioc_guid);
  328. mad->mad_hdr.status = 0;
  329. }
  330. /**
  331. * srpt_mgmt_method_get() - Process a received management datagram.
  332. * @sp: source port through which the MAD has been received.
  333. * @rq_mad: received MAD.
  334. * @rsp_mad: response MAD.
  335. */
  336. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  337. struct ib_dm_mad *rsp_mad)
  338. {
  339. u16 attr_id;
  340. u32 slot;
  341. u8 hi, lo;
  342. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  343. switch (attr_id) {
  344. case DM_ATTR_CLASS_PORT_INFO:
  345. srpt_get_class_port_info(rsp_mad);
  346. break;
  347. case DM_ATTR_IOU_INFO:
  348. srpt_get_iou(rsp_mad);
  349. break;
  350. case DM_ATTR_IOC_PROFILE:
  351. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  352. srpt_get_ioc(sp, slot, rsp_mad);
  353. break;
  354. case DM_ATTR_SVC_ENTRIES:
  355. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  356. hi = (u8) ((slot >> 8) & 0xff);
  357. lo = (u8) (slot & 0xff);
  358. slot = (u16) ((slot >> 16) & 0xffff);
  359. srpt_get_svc_entries(srpt_service_guid,
  360. slot, hi, lo, rsp_mad);
  361. break;
  362. default:
  363. rsp_mad->mad_hdr.status =
  364. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  365. break;
  366. }
  367. }
  368. /**
  369. * srpt_mad_send_handler() - Post MAD-send callback function.
  370. */
  371. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  372. struct ib_mad_send_wc *mad_wc)
  373. {
  374. ib_destroy_ah(mad_wc->send_buf->ah);
  375. ib_free_send_mad(mad_wc->send_buf);
  376. }
  377. /**
  378. * srpt_mad_recv_handler() - MAD reception callback function.
  379. */
  380. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  381. struct ib_mad_send_buf *send_buf,
  382. struct ib_mad_recv_wc *mad_wc)
  383. {
  384. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  385. struct ib_ah *ah;
  386. struct ib_mad_send_buf *rsp;
  387. struct ib_dm_mad *dm_mad;
  388. if (!mad_wc || !mad_wc->recv_buf.mad)
  389. return;
  390. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  391. mad_wc->recv_buf.grh, mad_agent->port_num);
  392. if (IS_ERR(ah))
  393. goto err;
  394. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  395. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  396. mad_wc->wc->pkey_index, 0,
  397. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  398. GFP_KERNEL,
  399. IB_MGMT_BASE_VERSION);
  400. if (IS_ERR(rsp))
  401. goto err_rsp;
  402. rsp->ah = ah;
  403. dm_mad = rsp->mad;
  404. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
  405. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  406. dm_mad->mad_hdr.status = 0;
  407. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  408. case IB_MGMT_METHOD_GET:
  409. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  410. break;
  411. case IB_MGMT_METHOD_SET:
  412. dm_mad->mad_hdr.status =
  413. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  414. break;
  415. default:
  416. dm_mad->mad_hdr.status =
  417. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  418. break;
  419. }
  420. if (!ib_post_send_mad(rsp, NULL)) {
  421. ib_free_recv_mad(mad_wc);
  422. /* will destroy_ah & free_send_mad in send completion */
  423. return;
  424. }
  425. ib_free_send_mad(rsp);
  426. err_rsp:
  427. ib_destroy_ah(ah);
  428. err:
  429. ib_free_recv_mad(mad_wc);
  430. }
  431. /**
  432. * srpt_refresh_port() - Configure a HCA port.
  433. *
  434. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  435. * lid and gid values, and register a callback function for processing MADs
  436. * on the specified port.
  437. *
  438. * Note: It is safe to call this function more than once for the same port.
  439. */
  440. static int srpt_refresh_port(struct srpt_port *sport)
  441. {
  442. struct ib_mad_reg_req reg_req;
  443. struct ib_port_modify port_modify;
  444. struct ib_port_attr port_attr;
  445. __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 and tag %lld\n", state,
  1003. 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(transport_check_aborted_status(&ioctx->cmd, false)
  1990. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  1991. atomic_inc(&ch->req_lim_delta);
  1992. srpt_abort_cmd(ioctx);
  1993. return;
  1994. }
  1995. /* For read commands, transfer the data to the initiator. */
  1996. if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
  1997. ioctx->cmd.data_length &&
  1998. !ioctx->queue_status_only) {
  1999. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  2000. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  2001. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
  2002. ch->sport->port, NULL, first_wr);
  2003. }
  2004. }
  2005. if (state != SRPT_STATE_MGMT)
  2006. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  2007. cmd->scsi_status);
  2008. else {
  2009. srp_tm_status
  2010. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2011. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2012. ioctx->cmd.tag);
  2013. }
  2014. atomic_inc(&ch->req_lim);
  2015. if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
  2016. &ch->sq_wr_avail) < 0)) {
  2017. pr_warn("%s: IB send queue full (needed %d)\n",
  2018. __func__, ioctx->n_rdma);
  2019. ret = -ENOMEM;
  2020. goto out;
  2021. }
  2022. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
  2023. DMA_TO_DEVICE);
  2024. sge.addr = ioctx->ioctx.dma;
  2025. sge.length = resp_len;
  2026. sge.lkey = sdev->pd->local_dma_lkey;
  2027. ioctx->ioctx.cqe.done = srpt_send_done;
  2028. send_wr.next = NULL;
  2029. send_wr.wr_cqe = &ioctx->ioctx.cqe;
  2030. send_wr.sg_list = &sge;
  2031. send_wr.num_sge = 1;
  2032. send_wr.opcode = IB_WR_SEND;
  2033. send_wr.send_flags = IB_SEND_SIGNALED;
  2034. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  2035. if (ret < 0) {
  2036. pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
  2037. __func__, ioctx->cmd.tag, ret);
  2038. goto out;
  2039. }
  2040. return;
  2041. out:
  2042. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  2043. atomic_dec(&ch->req_lim);
  2044. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2045. target_put_sess_cmd(&ioctx->cmd);
  2046. }
  2047. static int srpt_queue_data_in(struct se_cmd *cmd)
  2048. {
  2049. srpt_queue_response(cmd);
  2050. return 0;
  2051. }
  2052. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2053. {
  2054. srpt_queue_response(cmd);
  2055. }
  2056. static void srpt_aborted_task(struct se_cmd *cmd)
  2057. {
  2058. }
  2059. static int srpt_queue_status(struct se_cmd *cmd)
  2060. {
  2061. struct srpt_send_ioctx *ioctx;
  2062. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2063. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2064. if (cmd->se_cmd_flags &
  2065. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2066. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2067. ioctx->queue_status_only = true;
  2068. srpt_queue_response(cmd);
  2069. return 0;
  2070. }
  2071. static void srpt_refresh_port_work(struct work_struct *work)
  2072. {
  2073. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2074. srpt_refresh_port(sport);
  2075. }
  2076. /**
  2077. * srpt_release_sdev() - Free the channel resources associated with a target.
  2078. */
  2079. static int srpt_release_sdev(struct srpt_device *sdev)
  2080. {
  2081. int i, res;
  2082. WARN_ON_ONCE(irqs_disabled());
  2083. BUG_ON(!sdev);
  2084. mutex_lock(&sdev->mutex);
  2085. for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
  2086. sdev->port[i].enabled = false;
  2087. __srpt_close_all_ch(sdev);
  2088. mutex_unlock(&sdev->mutex);
  2089. res = wait_event_interruptible(sdev->ch_releaseQ,
  2090. list_empty_careful(&sdev->rch_list));
  2091. if (res)
  2092. pr_err("%s: interrupted.\n", __func__);
  2093. return 0;
  2094. }
  2095. static struct se_wwn *__srpt_lookup_wwn(const char *name)
  2096. {
  2097. struct ib_device *dev;
  2098. struct srpt_device *sdev;
  2099. struct srpt_port *sport;
  2100. int i;
  2101. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2102. dev = sdev->device;
  2103. if (!dev)
  2104. continue;
  2105. for (i = 0; i < dev->phys_port_cnt; i++) {
  2106. sport = &sdev->port[i];
  2107. if (strcmp(sport->port_guid, name) == 0)
  2108. return &sport->port_guid_wwn;
  2109. if (strcmp(sport->port_gid, name) == 0)
  2110. return &sport->port_gid_wwn;
  2111. }
  2112. }
  2113. return NULL;
  2114. }
  2115. static struct se_wwn *srpt_lookup_wwn(const char *name)
  2116. {
  2117. struct se_wwn *wwn;
  2118. spin_lock(&srpt_dev_lock);
  2119. wwn = __srpt_lookup_wwn(name);
  2120. spin_unlock(&srpt_dev_lock);
  2121. return wwn;
  2122. }
  2123. /**
  2124. * srpt_add_one() - Infiniband device addition callback function.
  2125. */
  2126. static void srpt_add_one(struct ib_device *device)
  2127. {
  2128. struct srpt_device *sdev;
  2129. struct srpt_port *sport;
  2130. struct ib_srq_init_attr srq_attr;
  2131. int i;
  2132. pr_debug("device = %p\n", device);
  2133. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  2134. if (!sdev)
  2135. goto err;
  2136. sdev->device = device;
  2137. INIT_LIST_HEAD(&sdev->rch_list);
  2138. init_waitqueue_head(&sdev->ch_releaseQ);
  2139. mutex_init(&sdev->mutex);
  2140. sdev->pd = ib_alloc_pd(device, 0);
  2141. if (IS_ERR(sdev->pd))
  2142. goto free_dev;
  2143. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2144. srq_attr.event_handler = srpt_srq_event;
  2145. srq_attr.srq_context = (void *)sdev;
  2146. srq_attr.attr.max_wr = sdev->srq_size;
  2147. srq_attr.attr.max_sge = 1;
  2148. srq_attr.attr.srq_limit = 0;
  2149. srq_attr.srq_type = IB_SRQT_BASIC;
  2150. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2151. if (IS_ERR(sdev->srq))
  2152. goto err_pd;
  2153. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2154. __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
  2155. device->name);
  2156. if (!srpt_service_guid)
  2157. srpt_service_guid = be64_to_cpu(device->node_guid);
  2158. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2159. if (IS_ERR(sdev->cm_id))
  2160. goto err_srq;
  2161. /* print out target login information */
  2162. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2163. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2164. srpt_service_guid, srpt_service_guid);
  2165. /*
  2166. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2167. * to identify this target. We currently use the guid of the first HCA
  2168. * in the system as service_id; therefore, the target_id will change
  2169. * if this HCA is gone bad and replaced by different HCA
  2170. */
  2171. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
  2172. goto err_cm;
  2173. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2174. srpt_event_handler);
  2175. if (ib_register_event_handler(&sdev->event_handler))
  2176. goto err_cm;
  2177. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2178. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2179. sizeof(*sdev->ioctx_ring[0]),
  2180. srp_max_req_size, DMA_FROM_DEVICE);
  2181. if (!sdev->ioctx_ring)
  2182. goto err_event;
  2183. for (i = 0; i < sdev->srq_size; ++i)
  2184. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2185. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2186. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2187. sport = &sdev->port[i - 1];
  2188. sport->sdev = sdev;
  2189. sport->port = i;
  2190. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2191. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2192. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2193. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2194. if (srpt_refresh_port(sport)) {
  2195. pr_err("MAD registration failed for %s-%d.\n",
  2196. sdev->device->name, i);
  2197. goto err_ring;
  2198. }
  2199. }
  2200. spin_lock(&srpt_dev_lock);
  2201. list_add_tail(&sdev->list, &srpt_dev_list);
  2202. spin_unlock(&srpt_dev_lock);
  2203. out:
  2204. ib_set_client_data(device, &srpt_client, sdev);
  2205. pr_debug("added %s.\n", device->name);
  2206. return;
  2207. err_ring:
  2208. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2209. sdev->srq_size, srp_max_req_size,
  2210. DMA_FROM_DEVICE);
  2211. err_event:
  2212. ib_unregister_event_handler(&sdev->event_handler);
  2213. err_cm:
  2214. ib_destroy_cm_id(sdev->cm_id);
  2215. err_srq:
  2216. ib_destroy_srq(sdev->srq);
  2217. err_pd:
  2218. ib_dealloc_pd(sdev->pd);
  2219. free_dev:
  2220. kfree(sdev);
  2221. err:
  2222. sdev = NULL;
  2223. pr_info("%s(%s) failed.\n", __func__, device->name);
  2224. goto out;
  2225. }
  2226. /**
  2227. * srpt_remove_one() - InfiniBand device removal callback function.
  2228. */
  2229. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2230. {
  2231. struct srpt_device *sdev = client_data;
  2232. int i;
  2233. if (!sdev) {
  2234. pr_info("%s(%s): nothing to do.\n", __func__, device->name);
  2235. return;
  2236. }
  2237. srpt_unregister_mad_agent(sdev);
  2238. ib_unregister_event_handler(&sdev->event_handler);
  2239. /* Cancel any work queued by the just unregistered IB event handler. */
  2240. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2241. cancel_work_sync(&sdev->port[i].work);
  2242. ib_destroy_cm_id(sdev->cm_id);
  2243. /*
  2244. * Unregistering a target must happen after destroying sdev->cm_id
  2245. * such that no new SRP_LOGIN_REQ information units can arrive while
  2246. * destroying the target.
  2247. */
  2248. spin_lock(&srpt_dev_lock);
  2249. list_del(&sdev->list);
  2250. spin_unlock(&srpt_dev_lock);
  2251. srpt_release_sdev(sdev);
  2252. ib_destroy_srq(sdev->srq);
  2253. ib_dealloc_pd(sdev->pd);
  2254. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2255. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2256. sdev->ioctx_ring = NULL;
  2257. kfree(sdev);
  2258. }
  2259. static struct ib_client srpt_client = {
  2260. .name = DRV_NAME,
  2261. .add = srpt_add_one,
  2262. .remove = srpt_remove_one
  2263. };
  2264. static int srpt_check_true(struct se_portal_group *se_tpg)
  2265. {
  2266. return 1;
  2267. }
  2268. static int srpt_check_false(struct se_portal_group *se_tpg)
  2269. {
  2270. return 0;
  2271. }
  2272. static char *srpt_get_fabric_name(void)
  2273. {
  2274. return "srpt";
  2275. }
  2276. static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
  2277. {
  2278. return tpg->se_tpg_wwn->priv;
  2279. }
  2280. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2281. {
  2282. struct srpt_port *sport = srpt_tpg_to_sport(tpg);
  2283. WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
  2284. tpg != &sport->port_gid_tpg);
  2285. return tpg == &sport->port_guid_tpg ? sport->port_guid :
  2286. sport->port_gid;
  2287. }
  2288. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2289. {
  2290. return 1;
  2291. }
  2292. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2293. {
  2294. return 1;
  2295. }
  2296. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2297. {
  2298. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2299. struct srpt_send_ioctx, cmd);
  2300. struct srpt_rdma_ch *ch = ioctx->ch;
  2301. unsigned long flags;
  2302. WARN_ON(ioctx->state != SRPT_STATE_DONE);
  2303. if (ioctx->n_rw_ctx) {
  2304. srpt_free_rw_ctxs(ch, ioctx);
  2305. ioctx->n_rw_ctx = 0;
  2306. }
  2307. spin_lock_irqsave(&ch->spinlock, flags);
  2308. list_add(&ioctx->free_list, &ch->free_list);
  2309. spin_unlock_irqrestore(&ch->spinlock, flags);
  2310. }
  2311. /**
  2312. * srpt_close_session() - Forcibly close a session.
  2313. *
  2314. * Callback function invoked by the TCM core to clean up sessions associated
  2315. * with a node ACL when the user invokes
  2316. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2317. */
  2318. static void srpt_close_session(struct se_session *se_sess)
  2319. {
  2320. DECLARE_COMPLETION_ONSTACK(release_done);
  2321. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2322. struct srpt_device *sdev = ch->sport->sdev;
  2323. bool wait;
  2324. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  2325. ch->state);
  2326. mutex_lock(&sdev->mutex);
  2327. BUG_ON(ch->release_done);
  2328. ch->release_done = &release_done;
  2329. wait = !list_empty(&ch->list);
  2330. srpt_disconnect_ch(ch);
  2331. mutex_unlock(&sdev->mutex);
  2332. if (!wait)
  2333. return;
  2334. while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
  2335. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  2336. ch->sess_name, ch->qp->qp_num, ch->state);
  2337. }
  2338. /**
  2339. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  2340. *
  2341. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2342. * This object represents an arbitrary integer used to uniquely identify a
  2343. * particular attached remote initiator port to a particular SCSI target port
  2344. * within a particular SCSI target device within a particular SCSI instance.
  2345. */
  2346. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2347. {
  2348. return 0;
  2349. }
  2350. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2351. {
  2352. }
  2353. /* Note: only used from inside debug printk's by the TCM core. */
  2354. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2355. {
  2356. struct srpt_send_ioctx *ioctx;
  2357. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2358. return srpt_get_cmd_state(ioctx);
  2359. }
  2360. static int srpt_parse_guid(u64 *guid, const char *name)
  2361. {
  2362. u16 w[4];
  2363. int ret = -EINVAL;
  2364. if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
  2365. goto out;
  2366. *guid = get_unaligned_be64(w);
  2367. ret = 0;
  2368. out:
  2369. return ret;
  2370. }
  2371. /**
  2372. * srpt_parse_i_port_id() - Parse an initiator port ID.
  2373. * @name: ASCII representation of a 128-bit initiator port ID.
  2374. * @i_port_id: Binary 128-bit port ID.
  2375. */
  2376. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2377. {
  2378. const char *p;
  2379. unsigned len, count, leading_zero_bytes;
  2380. int ret, rc;
  2381. p = name;
  2382. if (strncasecmp(p, "0x", 2) == 0)
  2383. p += 2;
  2384. ret = -EINVAL;
  2385. len = strlen(p);
  2386. if (len % 2)
  2387. goto out;
  2388. count = min(len / 2, 16U);
  2389. leading_zero_bytes = 16 - count;
  2390. memset(i_port_id, 0, leading_zero_bytes);
  2391. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2392. if (rc < 0)
  2393. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  2394. ret = 0;
  2395. out:
  2396. return ret;
  2397. }
  2398. /*
  2399. * configfs callback function invoked for
  2400. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2401. */
  2402. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2403. {
  2404. u64 guid;
  2405. u8 i_port_id[16];
  2406. int ret;
  2407. ret = srpt_parse_guid(&guid, name);
  2408. if (ret < 0)
  2409. ret = srpt_parse_i_port_id(i_port_id, name);
  2410. if (ret < 0)
  2411. pr_err("invalid initiator port ID %s\n", name);
  2412. return ret;
  2413. }
  2414. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  2415. char *page)
  2416. {
  2417. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2418. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2419. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  2420. }
  2421. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  2422. const char *page, size_t count)
  2423. {
  2424. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2425. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2426. unsigned long val;
  2427. int ret;
  2428. ret = kstrtoul(page, 0, &val);
  2429. if (ret < 0) {
  2430. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2431. return -EINVAL;
  2432. }
  2433. if (val > MAX_SRPT_RDMA_SIZE) {
  2434. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  2435. MAX_SRPT_RDMA_SIZE);
  2436. return -EINVAL;
  2437. }
  2438. if (val < DEFAULT_MAX_RDMA_SIZE) {
  2439. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  2440. val, DEFAULT_MAX_RDMA_SIZE);
  2441. return -EINVAL;
  2442. }
  2443. sport->port_attrib.srp_max_rdma_size = val;
  2444. return count;
  2445. }
  2446. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  2447. char *page)
  2448. {
  2449. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2450. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2451. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  2452. }
  2453. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  2454. const char *page, size_t count)
  2455. {
  2456. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2457. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2458. unsigned long val;
  2459. int ret;
  2460. ret = kstrtoul(page, 0, &val);
  2461. if (ret < 0) {
  2462. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2463. return -EINVAL;
  2464. }
  2465. if (val > MAX_SRPT_RSP_SIZE) {
  2466. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  2467. MAX_SRPT_RSP_SIZE);
  2468. return -EINVAL;
  2469. }
  2470. if (val < MIN_MAX_RSP_SIZE) {
  2471. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  2472. MIN_MAX_RSP_SIZE);
  2473. return -EINVAL;
  2474. }
  2475. sport->port_attrib.srp_max_rsp_size = val;
  2476. return count;
  2477. }
  2478. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  2479. char *page)
  2480. {
  2481. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2482. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2483. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  2484. }
  2485. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  2486. const char *page, size_t count)
  2487. {
  2488. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2489. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2490. unsigned long val;
  2491. int ret;
  2492. ret = kstrtoul(page, 0, &val);
  2493. if (ret < 0) {
  2494. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2495. return -EINVAL;
  2496. }
  2497. if (val > MAX_SRPT_SRQ_SIZE) {
  2498. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  2499. MAX_SRPT_SRQ_SIZE);
  2500. return -EINVAL;
  2501. }
  2502. if (val < MIN_SRPT_SRQ_SIZE) {
  2503. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  2504. MIN_SRPT_SRQ_SIZE);
  2505. return -EINVAL;
  2506. }
  2507. sport->port_attrib.srp_sq_size = val;
  2508. return count;
  2509. }
  2510. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  2511. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  2512. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  2513. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  2514. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  2515. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  2516. &srpt_tpg_attrib_attr_srp_sq_size,
  2517. NULL,
  2518. };
  2519. static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
  2520. {
  2521. struct se_portal_group *se_tpg = to_tpg(item);
  2522. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2523. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  2524. }
  2525. static ssize_t srpt_tpg_enable_store(struct config_item *item,
  2526. const char *page, size_t count)
  2527. {
  2528. struct se_portal_group *se_tpg = to_tpg(item);
  2529. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2530. struct srpt_device *sdev = sport->sdev;
  2531. struct srpt_rdma_ch *ch;
  2532. unsigned long tmp;
  2533. int ret;
  2534. ret = kstrtoul(page, 0, &tmp);
  2535. if (ret < 0) {
  2536. pr_err("Unable to extract srpt_tpg_store_enable\n");
  2537. return -EINVAL;
  2538. }
  2539. if ((tmp != 0) && (tmp != 1)) {
  2540. pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  2541. return -EINVAL;
  2542. }
  2543. if (sport->enabled == tmp)
  2544. goto out;
  2545. sport->enabled = tmp;
  2546. if (sport->enabled)
  2547. goto out;
  2548. mutex_lock(&sdev->mutex);
  2549. list_for_each_entry(ch, &sdev->rch_list, list) {
  2550. if (ch->sport == sport) {
  2551. pr_debug("%s: ch %p %s-%d\n", __func__, ch,
  2552. ch->sess_name, ch->qp->qp_num);
  2553. srpt_disconnect_ch(ch);
  2554. srpt_close_ch(ch);
  2555. }
  2556. }
  2557. mutex_unlock(&sdev->mutex);
  2558. out:
  2559. return count;
  2560. }
  2561. CONFIGFS_ATTR(srpt_tpg_, enable);
  2562. static struct configfs_attribute *srpt_tpg_attrs[] = {
  2563. &srpt_tpg_attr_enable,
  2564. NULL,
  2565. };
  2566. /**
  2567. * configfs callback invoked for
  2568. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  2569. */
  2570. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  2571. struct config_group *group,
  2572. const char *name)
  2573. {
  2574. struct srpt_port *sport = wwn->priv;
  2575. static struct se_portal_group *tpg;
  2576. int res;
  2577. WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
  2578. wwn != &sport->port_gid_wwn);
  2579. tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
  2580. &sport->port_gid_tpg;
  2581. res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
  2582. if (res)
  2583. return ERR_PTR(res);
  2584. return tpg;
  2585. }
  2586. /**
  2587. * configfs callback invoked for
  2588. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  2589. */
  2590. static void srpt_drop_tpg(struct se_portal_group *tpg)
  2591. {
  2592. struct srpt_port *sport = srpt_tpg_to_sport(tpg);
  2593. sport->enabled = false;
  2594. core_tpg_deregister(tpg);
  2595. }
  2596. /**
  2597. * configfs callback invoked for
  2598. * mkdir /sys/kernel/config/target/$driver/$port
  2599. */
  2600. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  2601. struct config_group *group,
  2602. const char *name)
  2603. {
  2604. return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
  2605. }
  2606. /**
  2607. * configfs callback invoked for
  2608. * rmdir /sys/kernel/config/target/$driver/$port
  2609. */
  2610. static void srpt_drop_tport(struct se_wwn *wwn)
  2611. {
  2612. }
  2613. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  2614. {
  2615. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  2616. }
  2617. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  2618. static struct configfs_attribute *srpt_wwn_attrs[] = {
  2619. &srpt_wwn_attr_version,
  2620. NULL,
  2621. };
  2622. static const struct target_core_fabric_ops srpt_template = {
  2623. .module = THIS_MODULE,
  2624. .name = "srpt",
  2625. .get_fabric_name = srpt_get_fabric_name,
  2626. .tpg_get_wwn = srpt_get_fabric_wwn,
  2627. .tpg_get_tag = srpt_get_tag,
  2628. .tpg_check_demo_mode = srpt_check_false,
  2629. .tpg_check_demo_mode_cache = srpt_check_true,
  2630. .tpg_check_demo_mode_write_protect = srpt_check_true,
  2631. .tpg_check_prod_mode_write_protect = srpt_check_false,
  2632. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  2633. .release_cmd = srpt_release_cmd,
  2634. .check_stop_free = srpt_check_stop_free,
  2635. .close_session = srpt_close_session,
  2636. .sess_get_index = srpt_sess_get_index,
  2637. .sess_get_initiator_sid = NULL,
  2638. .write_pending = srpt_write_pending,
  2639. .write_pending_status = srpt_write_pending_status,
  2640. .set_default_node_attributes = srpt_set_default_node_attrs,
  2641. .get_cmd_state = srpt_get_tcm_cmd_state,
  2642. .queue_data_in = srpt_queue_data_in,
  2643. .queue_status = srpt_queue_status,
  2644. .queue_tm_rsp = srpt_queue_tm_rsp,
  2645. .aborted_task = srpt_aborted_task,
  2646. /*
  2647. * Setup function pointers for generic logic in
  2648. * target_core_fabric_configfs.c
  2649. */
  2650. .fabric_make_wwn = srpt_make_tport,
  2651. .fabric_drop_wwn = srpt_drop_tport,
  2652. .fabric_make_tpg = srpt_make_tpg,
  2653. .fabric_drop_tpg = srpt_drop_tpg,
  2654. .fabric_init_nodeacl = srpt_init_nodeacl,
  2655. .tfc_wwn_attrs = srpt_wwn_attrs,
  2656. .tfc_tpg_base_attrs = srpt_tpg_attrs,
  2657. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  2658. };
  2659. /**
  2660. * srpt_init_module() - Kernel module initialization.
  2661. *
  2662. * Note: Since ib_register_client() registers callback functions, and since at
  2663. * least one of these callback functions (srpt_add_one()) calls target core
  2664. * functions, this driver must be registered with the target core before
  2665. * ib_register_client() is called.
  2666. */
  2667. static int __init srpt_init_module(void)
  2668. {
  2669. int ret;
  2670. ret = -EINVAL;
  2671. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  2672. pr_err("invalid value %d for kernel module parameter"
  2673. " srp_max_req_size -- must be at least %d.\n",
  2674. srp_max_req_size, MIN_MAX_REQ_SIZE);
  2675. goto out;
  2676. }
  2677. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  2678. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  2679. pr_err("invalid value %d for kernel module parameter"
  2680. " srpt_srq_size -- must be in the range [%d..%d].\n",
  2681. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  2682. goto out;
  2683. }
  2684. ret = target_register_template(&srpt_template);
  2685. if (ret)
  2686. goto out;
  2687. ret = ib_register_client(&srpt_client);
  2688. if (ret) {
  2689. pr_err("couldn't register IB client\n");
  2690. goto out_unregister_target;
  2691. }
  2692. return 0;
  2693. out_unregister_target:
  2694. target_unregister_template(&srpt_template);
  2695. out:
  2696. return ret;
  2697. }
  2698. static void __exit srpt_cleanup_module(void)
  2699. {
  2700. ib_unregister_client(&srpt_client);
  2701. target_unregister_template(&srpt_template);
  2702. }
  2703. module_init(srpt_init_module);
  2704. module_exit(srpt_cleanup_module);