ib_srp.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  1. /*
  2. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) PFX fmt
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/err.h>
  37. #include <linux/string.h>
  38. #include <linux/parser.h>
  39. #include <linux/random.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/atomic.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_dbg.h>
  45. #include <scsi/srp.h>
  46. #include <scsi/scsi_transport_srp.h>
  47. #include "ib_srp.h"
  48. #define DRV_NAME "ib_srp"
  49. #define PFX DRV_NAME ": "
  50. #define DRV_VERSION "1.0"
  51. #define DRV_RELDATE "July 1, 2013"
  52. MODULE_AUTHOR("Roland Dreier");
  53. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
  54. "v" DRV_VERSION " (" DRV_RELDATE ")");
  55. MODULE_LICENSE("Dual BSD/GPL");
  56. static unsigned int srp_sg_tablesize;
  57. static unsigned int cmd_sg_entries;
  58. static unsigned int indirect_sg_entries;
  59. static bool allow_ext_sg;
  60. static int topspin_workarounds = 1;
  61. module_param(srp_sg_tablesize, uint, 0444);
  62. MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
  63. module_param(cmd_sg_entries, uint, 0444);
  64. MODULE_PARM_DESC(cmd_sg_entries,
  65. "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
  66. module_param(indirect_sg_entries, uint, 0444);
  67. MODULE_PARM_DESC(indirect_sg_entries,
  68. "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
  69. module_param(allow_ext_sg, bool, 0444);
  70. MODULE_PARM_DESC(allow_ext_sg,
  71. "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
  72. module_param(topspin_workarounds, int, 0444);
  73. MODULE_PARM_DESC(topspin_workarounds,
  74. "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
  75. static struct kernel_param_ops srp_tmo_ops;
  76. static int srp_reconnect_delay = 10;
  77. module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
  78. S_IRUGO | S_IWUSR);
  79. MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
  80. static int srp_fast_io_fail_tmo = 15;
  81. module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
  82. S_IRUGO | S_IWUSR);
  83. MODULE_PARM_DESC(fast_io_fail_tmo,
  84. "Number of seconds between the observation of a transport"
  85. " layer error and failing all I/O. \"off\" means that this"
  86. " functionality is disabled.");
  87. static int srp_dev_loss_tmo = 600;
  88. module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
  89. S_IRUGO | S_IWUSR);
  90. MODULE_PARM_DESC(dev_loss_tmo,
  91. "Maximum number of seconds that the SRP transport should"
  92. " insulate transport layer errors. After this time has been"
  93. " exceeded the SCSI host is removed. Should be"
  94. " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  95. " if fast_io_fail_tmo has not been set. \"off\" means that"
  96. " this functionality is disabled.");
  97. static void srp_add_one(struct ib_device *device);
  98. static void srp_remove_one(struct ib_device *device);
  99. static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
  100. static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
  101. static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
  102. static struct scsi_transport_template *ib_srp_transport_template;
  103. static struct ib_client srp_client = {
  104. .name = "srp",
  105. .add = srp_add_one,
  106. .remove = srp_remove_one
  107. };
  108. static struct ib_sa_client srp_sa_client;
  109. static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
  110. {
  111. int tmo = *(int *)kp->arg;
  112. if (tmo >= 0)
  113. return sprintf(buffer, "%d", tmo);
  114. else
  115. return sprintf(buffer, "off");
  116. }
  117. static int srp_tmo_set(const char *val, const struct kernel_param *kp)
  118. {
  119. int tmo, res;
  120. if (strncmp(val, "off", 3) != 0) {
  121. res = kstrtoint(val, 0, &tmo);
  122. if (res)
  123. goto out;
  124. } else {
  125. tmo = -1;
  126. }
  127. if (kp->arg == &srp_reconnect_delay)
  128. res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
  129. srp_dev_loss_tmo);
  130. else if (kp->arg == &srp_fast_io_fail_tmo)
  131. res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
  132. else
  133. res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
  134. tmo);
  135. if (res)
  136. goto out;
  137. *(int *)kp->arg = tmo;
  138. out:
  139. return res;
  140. }
  141. static struct kernel_param_ops srp_tmo_ops = {
  142. .get = srp_tmo_get,
  143. .set = srp_tmo_set,
  144. };
  145. static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
  146. {
  147. return (struct srp_target_port *) host->hostdata;
  148. }
  149. static const char *srp_target_info(struct Scsi_Host *host)
  150. {
  151. return host_to_target(host)->target_name;
  152. }
  153. static int srp_target_is_topspin(struct srp_target_port *target)
  154. {
  155. static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
  156. static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
  157. return topspin_workarounds &&
  158. (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
  159. !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
  160. }
  161. static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
  162. gfp_t gfp_mask,
  163. enum dma_data_direction direction)
  164. {
  165. struct srp_iu *iu;
  166. iu = kmalloc(sizeof *iu, gfp_mask);
  167. if (!iu)
  168. goto out;
  169. iu->buf = kzalloc(size, gfp_mask);
  170. if (!iu->buf)
  171. goto out_free_iu;
  172. iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
  173. direction);
  174. if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
  175. goto out_free_buf;
  176. iu->size = size;
  177. iu->direction = direction;
  178. return iu;
  179. out_free_buf:
  180. kfree(iu->buf);
  181. out_free_iu:
  182. kfree(iu);
  183. out:
  184. return NULL;
  185. }
  186. static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
  187. {
  188. if (!iu)
  189. return;
  190. ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
  191. iu->direction);
  192. kfree(iu->buf);
  193. kfree(iu);
  194. }
  195. static void srp_qp_event(struct ib_event *event, void *context)
  196. {
  197. pr_debug("QP event %d\n", event->event);
  198. }
  199. static int srp_init_qp(struct srp_target_port *target,
  200. struct ib_qp *qp)
  201. {
  202. struct ib_qp_attr *attr;
  203. int ret;
  204. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  205. if (!attr)
  206. return -ENOMEM;
  207. ret = ib_find_pkey(target->srp_host->srp_dev->dev,
  208. target->srp_host->port,
  209. be16_to_cpu(target->path.pkey),
  210. &attr->pkey_index);
  211. if (ret)
  212. goto out;
  213. attr->qp_state = IB_QPS_INIT;
  214. attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
  215. IB_ACCESS_REMOTE_WRITE);
  216. attr->port_num = target->srp_host->port;
  217. ret = ib_modify_qp(qp, attr,
  218. IB_QP_STATE |
  219. IB_QP_PKEY_INDEX |
  220. IB_QP_ACCESS_FLAGS |
  221. IB_QP_PORT);
  222. out:
  223. kfree(attr);
  224. return ret;
  225. }
  226. static int srp_new_cm_id(struct srp_target_port *target)
  227. {
  228. struct ib_cm_id *new_cm_id;
  229. new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
  230. srp_cm_handler, target);
  231. if (IS_ERR(new_cm_id))
  232. return PTR_ERR(new_cm_id);
  233. if (target->cm_id)
  234. ib_destroy_cm_id(target->cm_id);
  235. target->cm_id = new_cm_id;
  236. return 0;
  237. }
  238. static int srp_create_target_ib(struct srp_target_port *target)
  239. {
  240. struct ib_qp_init_attr *init_attr;
  241. struct ib_cq *recv_cq, *send_cq;
  242. struct ib_qp *qp;
  243. int ret;
  244. init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
  245. if (!init_attr)
  246. return -ENOMEM;
  247. recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
  248. srp_recv_completion, NULL, target, SRP_RQ_SIZE,
  249. target->comp_vector);
  250. if (IS_ERR(recv_cq)) {
  251. ret = PTR_ERR(recv_cq);
  252. goto err;
  253. }
  254. send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
  255. srp_send_completion, NULL, target, SRP_SQ_SIZE,
  256. target->comp_vector);
  257. if (IS_ERR(send_cq)) {
  258. ret = PTR_ERR(send_cq);
  259. goto err_recv_cq;
  260. }
  261. ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
  262. init_attr->event_handler = srp_qp_event;
  263. init_attr->cap.max_send_wr = SRP_SQ_SIZE;
  264. init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
  265. init_attr->cap.max_recv_sge = 1;
  266. init_attr->cap.max_send_sge = 1;
  267. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  268. init_attr->qp_type = IB_QPT_RC;
  269. init_attr->send_cq = send_cq;
  270. init_attr->recv_cq = recv_cq;
  271. qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
  272. if (IS_ERR(qp)) {
  273. ret = PTR_ERR(qp);
  274. goto err_send_cq;
  275. }
  276. ret = srp_init_qp(target, qp);
  277. if (ret)
  278. goto err_qp;
  279. if (target->qp)
  280. ib_destroy_qp(target->qp);
  281. if (target->recv_cq)
  282. ib_destroy_cq(target->recv_cq);
  283. if (target->send_cq)
  284. ib_destroy_cq(target->send_cq);
  285. target->qp = qp;
  286. target->recv_cq = recv_cq;
  287. target->send_cq = send_cq;
  288. kfree(init_attr);
  289. return 0;
  290. err_qp:
  291. ib_destroy_qp(qp);
  292. err_send_cq:
  293. ib_destroy_cq(send_cq);
  294. err_recv_cq:
  295. ib_destroy_cq(recv_cq);
  296. err:
  297. kfree(init_attr);
  298. return ret;
  299. }
  300. static void srp_free_target_ib(struct srp_target_port *target)
  301. {
  302. int i;
  303. ib_destroy_qp(target->qp);
  304. ib_destroy_cq(target->send_cq);
  305. ib_destroy_cq(target->recv_cq);
  306. target->qp = NULL;
  307. target->send_cq = target->recv_cq = NULL;
  308. for (i = 0; i < SRP_RQ_SIZE; ++i)
  309. srp_free_iu(target->srp_host, target->rx_ring[i]);
  310. for (i = 0; i < SRP_SQ_SIZE; ++i)
  311. srp_free_iu(target->srp_host, target->tx_ring[i]);
  312. }
  313. static void srp_path_rec_completion(int status,
  314. struct ib_sa_path_rec *pathrec,
  315. void *target_ptr)
  316. {
  317. struct srp_target_port *target = target_ptr;
  318. target->status = status;
  319. if (status)
  320. shost_printk(KERN_ERR, target->scsi_host,
  321. PFX "Got failed path rec status %d\n", status);
  322. else
  323. target->path = *pathrec;
  324. complete(&target->done);
  325. }
  326. static int srp_lookup_path(struct srp_target_port *target)
  327. {
  328. target->path.numb_path = 1;
  329. init_completion(&target->done);
  330. target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
  331. target->srp_host->srp_dev->dev,
  332. target->srp_host->port,
  333. &target->path,
  334. IB_SA_PATH_REC_SERVICE_ID |
  335. IB_SA_PATH_REC_DGID |
  336. IB_SA_PATH_REC_SGID |
  337. IB_SA_PATH_REC_NUMB_PATH |
  338. IB_SA_PATH_REC_PKEY,
  339. SRP_PATH_REC_TIMEOUT_MS,
  340. GFP_KERNEL,
  341. srp_path_rec_completion,
  342. target, &target->path_query);
  343. if (target->path_query_id < 0)
  344. return target->path_query_id;
  345. wait_for_completion(&target->done);
  346. if (target->status < 0)
  347. shost_printk(KERN_WARNING, target->scsi_host,
  348. PFX "Path record query failed\n");
  349. return target->status;
  350. }
  351. static int srp_send_req(struct srp_target_port *target)
  352. {
  353. struct {
  354. struct ib_cm_req_param param;
  355. struct srp_login_req priv;
  356. } *req = NULL;
  357. int status;
  358. req = kzalloc(sizeof *req, GFP_KERNEL);
  359. if (!req)
  360. return -ENOMEM;
  361. req->param.primary_path = &target->path;
  362. req->param.alternate_path = NULL;
  363. req->param.service_id = target->service_id;
  364. req->param.qp_num = target->qp->qp_num;
  365. req->param.qp_type = target->qp->qp_type;
  366. req->param.private_data = &req->priv;
  367. req->param.private_data_len = sizeof req->priv;
  368. req->param.flow_control = 1;
  369. get_random_bytes(&req->param.starting_psn, 4);
  370. req->param.starting_psn &= 0xffffff;
  371. /*
  372. * Pick some arbitrary defaults here; we could make these
  373. * module parameters if anyone cared about setting them.
  374. */
  375. req->param.responder_resources = 4;
  376. req->param.remote_cm_response_timeout = 20;
  377. req->param.local_cm_response_timeout = 20;
  378. req->param.retry_count = target->tl_retry_count;
  379. req->param.rnr_retry_count = 7;
  380. req->param.max_cm_retries = 15;
  381. req->priv.opcode = SRP_LOGIN_REQ;
  382. req->priv.tag = 0;
  383. req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
  384. req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  385. SRP_BUF_FORMAT_INDIRECT);
  386. /*
  387. * In the published SRP specification (draft rev. 16a), the
  388. * port identifier format is 8 bytes of ID extension followed
  389. * by 8 bytes of GUID. Older drafts put the two halves in the
  390. * opposite order, so that the GUID comes first.
  391. *
  392. * Targets conforming to these obsolete drafts can be
  393. * recognized by the I/O Class they report.
  394. */
  395. if (target->io_class == SRP_REV10_IB_IO_CLASS) {
  396. memcpy(req->priv.initiator_port_id,
  397. &target->path.sgid.global.interface_id, 8);
  398. memcpy(req->priv.initiator_port_id + 8,
  399. &target->initiator_ext, 8);
  400. memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
  401. memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
  402. } else {
  403. memcpy(req->priv.initiator_port_id,
  404. &target->initiator_ext, 8);
  405. memcpy(req->priv.initiator_port_id + 8,
  406. &target->path.sgid.global.interface_id, 8);
  407. memcpy(req->priv.target_port_id, &target->id_ext, 8);
  408. memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
  409. }
  410. /*
  411. * Topspin/Cisco SRP targets will reject our login unless we
  412. * zero out the first 8 bytes of our initiator port ID and set
  413. * the second 8 bytes to the local node GUID.
  414. */
  415. if (srp_target_is_topspin(target)) {
  416. shost_printk(KERN_DEBUG, target->scsi_host,
  417. PFX "Topspin/Cisco initiator port ID workaround "
  418. "activated for target GUID %016llx\n",
  419. (unsigned long long) be64_to_cpu(target->ioc_guid));
  420. memset(req->priv.initiator_port_id, 0, 8);
  421. memcpy(req->priv.initiator_port_id + 8,
  422. &target->srp_host->srp_dev->dev->node_guid, 8);
  423. }
  424. status = ib_send_cm_req(target->cm_id, &req->param);
  425. kfree(req);
  426. return status;
  427. }
  428. static bool srp_queue_remove_work(struct srp_target_port *target)
  429. {
  430. bool changed = false;
  431. spin_lock_irq(&target->lock);
  432. if (target->state != SRP_TARGET_REMOVED) {
  433. target->state = SRP_TARGET_REMOVED;
  434. changed = true;
  435. }
  436. spin_unlock_irq(&target->lock);
  437. if (changed)
  438. queue_work(system_long_wq, &target->remove_work);
  439. return changed;
  440. }
  441. static bool srp_change_conn_state(struct srp_target_port *target,
  442. bool connected)
  443. {
  444. bool changed = false;
  445. spin_lock_irq(&target->lock);
  446. if (target->connected != connected) {
  447. target->connected = connected;
  448. changed = true;
  449. }
  450. spin_unlock_irq(&target->lock);
  451. return changed;
  452. }
  453. static void srp_disconnect_target(struct srp_target_port *target)
  454. {
  455. if (srp_change_conn_state(target, false)) {
  456. /* XXX should send SRP_I_LOGOUT request */
  457. if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
  458. shost_printk(KERN_DEBUG, target->scsi_host,
  459. PFX "Sending CM DREQ failed\n");
  460. }
  461. }
  462. }
  463. static void srp_free_req_data(struct srp_target_port *target)
  464. {
  465. struct ib_device *ibdev = target->srp_host->srp_dev->dev;
  466. struct srp_request *req;
  467. int i;
  468. for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) {
  469. kfree(req->fmr_list);
  470. kfree(req->map_page);
  471. if (req->indirect_dma_addr) {
  472. ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
  473. target->indirect_size,
  474. DMA_TO_DEVICE);
  475. }
  476. kfree(req->indirect_desc);
  477. }
  478. }
  479. static int srp_alloc_req_data(struct srp_target_port *target)
  480. {
  481. struct srp_device *srp_dev = target->srp_host->srp_dev;
  482. struct ib_device *ibdev = srp_dev->dev;
  483. struct srp_request *req;
  484. dma_addr_t dma_addr;
  485. int i, ret = -ENOMEM;
  486. INIT_LIST_HEAD(&target->free_reqs);
  487. for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
  488. req = &target->req_ring[i];
  489. req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
  490. GFP_KERNEL);
  491. req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
  492. GFP_KERNEL);
  493. req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
  494. if (!req->fmr_list || !req->map_page || !req->indirect_desc)
  495. goto out;
  496. dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
  497. target->indirect_size,
  498. DMA_TO_DEVICE);
  499. if (ib_dma_mapping_error(ibdev, dma_addr))
  500. goto out;
  501. req->indirect_dma_addr = dma_addr;
  502. req->index = i;
  503. list_add_tail(&req->list, &target->free_reqs);
  504. }
  505. ret = 0;
  506. out:
  507. return ret;
  508. }
  509. /**
  510. * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
  511. * @shost: SCSI host whose attributes to remove from sysfs.
  512. *
  513. * Note: Any attributes defined in the host template and that did not exist
  514. * before invocation of this function will be ignored.
  515. */
  516. static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
  517. {
  518. struct device_attribute **attr;
  519. for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
  520. device_remove_file(&shost->shost_dev, *attr);
  521. }
  522. static void srp_remove_target(struct srp_target_port *target)
  523. {
  524. WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
  525. srp_del_scsi_host_attr(target->scsi_host);
  526. srp_rport_get(target->rport);
  527. srp_remove_host(target->scsi_host);
  528. scsi_remove_host(target->scsi_host);
  529. srp_disconnect_target(target);
  530. ib_destroy_cm_id(target->cm_id);
  531. srp_free_target_ib(target);
  532. cancel_work_sync(&target->tl_err_work);
  533. srp_rport_put(target->rport);
  534. srp_free_req_data(target);
  535. scsi_host_put(target->scsi_host);
  536. }
  537. static void srp_remove_work(struct work_struct *work)
  538. {
  539. struct srp_target_port *target =
  540. container_of(work, struct srp_target_port, remove_work);
  541. WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
  542. srp_remove_target(target);
  543. spin_lock(&target->srp_host->target_lock);
  544. list_del(&target->list);
  545. spin_unlock(&target->srp_host->target_lock);
  546. }
  547. static void srp_rport_delete(struct srp_rport *rport)
  548. {
  549. struct srp_target_port *target = rport->lld_data;
  550. srp_queue_remove_work(target);
  551. }
  552. static int srp_connect_target(struct srp_target_port *target)
  553. {
  554. int retries = 3;
  555. int ret;
  556. WARN_ON_ONCE(target->connected);
  557. target->qp_in_error = false;
  558. ret = srp_lookup_path(target);
  559. if (ret)
  560. return ret;
  561. while (1) {
  562. init_completion(&target->done);
  563. ret = srp_send_req(target);
  564. if (ret)
  565. return ret;
  566. wait_for_completion(&target->done);
  567. /*
  568. * The CM event handling code will set status to
  569. * SRP_PORT_REDIRECT if we get a port redirect REJ
  570. * back, or SRP_DLID_REDIRECT if we get a lid/qp
  571. * redirect REJ back.
  572. */
  573. switch (target->status) {
  574. case 0:
  575. srp_change_conn_state(target, true);
  576. return 0;
  577. case SRP_PORT_REDIRECT:
  578. ret = srp_lookup_path(target);
  579. if (ret)
  580. return ret;
  581. break;
  582. case SRP_DLID_REDIRECT:
  583. break;
  584. case SRP_STALE_CONN:
  585. /* Our current CM id was stale, and is now in timewait.
  586. * Try to reconnect with a new one.
  587. */
  588. if (!retries-- || srp_new_cm_id(target)) {
  589. shost_printk(KERN_ERR, target->scsi_host, PFX
  590. "giving up on stale connection\n");
  591. target->status = -ECONNRESET;
  592. return target->status;
  593. }
  594. shost_printk(KERN_ERR, target->scsi_host, PFX
  595. "retrying stale connection\n");
  596. break;
  597. default:
  598. return target->status;
  599. }
  600. }
  601. }
  602. static void srp_unmap_data(struct scsi_cmnd *scmnd,
  603. struct srp_target_port *target,
  604. struct srp_request *req)
  605. {
  606. struct ib_device *ibdev = target->srp_host->srp_dev->dev;
  607. struct ib_pool_fmr **pfmr;
  608. if (!scsi_sglist(scmnd) ||
  609. (scmnd->sc_data_direction != DMA_TO_DEVICE &&
  610. scmnd->sc_data_direction != DMA_FROM_DEVICE))
  611. return;
  612. pfmr = req->fmr_list;
  613. while (req->nfmr--)
  614. ib_fmr_pool_unmap(*pfmr++);
  615. ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
  616. scmnd->sc_data_direction);
  617. }
  618. /**
  619. * srp_claim_req - Take ownership of the scmnd associated with a request.
  620. * @target: SRP target port.
  621. * @req: SRP request.
  622. * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
  623. * ownership of @req->scmnd if it equals @scmnd.
  624. *
  625. * Return value:
  626. * Either NULL or a pointer to the SCSI command the caller became owner of.
  627. */
  628. static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
  629. struct srp_request *req,
  630. struct scsi_cmnd *scmnd)
  631. {
  632. unsigned long flags;
  633. spin_lock_irqsave(&target->lock, flags);
  634. if (!scmnd) {
  635. scmnd = req->scmnd;
  636. req->scmnd = NULL;
  637. } else if (req->scmnd == scmnd) {
  638. req->scmnd = NULL;
  639. } else {
  640. scmnd = NULL;
  641. }
  642. spin_unlock_irqrestore(&target->lock, flags);
  643. return scmnd;
  644. }
  645. /**
  646. * srp_free_req() - Unmap data and add request to the free request list.
  647. */
  648. static void srp_free_req(struct srp_target_port *target,
  649. struct srp_request *req, struct scsi_cmnd *scmnd,
  650. s32 req_lim_delta)
  651. {
  652. unsigned long flags;
  653. srp_unmap_data(scmnd, target, req);
  654. spin_lock_irqsave(&target->lock, flags);
  655. target->req_lim += req_lim_delta;
  656. list_add_tail(&req->list, &target->free_reqs);
  657. spin_unlock_irqrestore(&target->lock, flags);
  658. }
  659. static void srp_finish_req(struct srp_target_port *target,
  660. struct srp_request *req, int result)
  661. {
  662. struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
  663. if (scmnd) {
  664. srp_free_req(target, req, scmnd, 0);
  665. scmnd->result = result;
  666. scmnd->scsi_done(scmnd);
  667. }
  668. }
  669. static void srp_terminate_io(struct srp_rport *rport)
  670. {
  671. struct srp_target_port *target = rport->lld_data;
  672. int i;
  673. for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
  674. struct srp_request *req = &target->req_ring[i];
  675. srp_finish_req(target, req, DID_TRANSPORT_FAILFAST << 16);
  676. }
  677. }
  678. /*
  679. * It is up to the caller to ensure that srp_rport_reconnect() calls are
  680. * serialized and that no concurrent srp_queuecommand(), srp_abort(),
  681. * srp_reset_device() or srp_reset_host() calls will occur while this function
  682. * is in progress. One way to realize that is not to call this function
  683. * directly but to call srp_reconnect_rport() instead since that last function
  684. * serializes calls of this function via rport->mutex and also blocks
  685. * srp_queuecommand() calls before invoking this function.
  686. */
  687. static int srp_rport_reconnect(struct srp_rport *rport)
  688. {
  689. struct srp_target_port *target = rport->lld_data;
  690. int i, ret;
  691. srp_disconnect_target(target);
  692. /*
  693. * Now get a new local CM ID so that we avoid confusing the target in
  694. * case things are really fouled up. Doing so also ensures that all CM
  695. * callbacks will have finished before a new QP is allocated.
  696. */
  697. ret = srp_new_cm_id(target);
  698. /*
  699. * Whether or not creating a new CM ID succeeded, create a new
  700. * QP. This guarantees that all completion callback function
  701. * invocations have finished before request resetting starts.
  702. */
  703. if (ret == 0)
  704. ret = srp_create_target_ib(target);
  705. else
  706. srp_create_target_ib(target);
  707. for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
  708. struct srp_request *req = &target->req_ring[i];
  709. srp_finish_req(target, req, DID_RESET << 16);
  710. }
  711. INIT_LIST_HEAD(&target->free_tx);
  712. for (i = 0; i < SRP_SQ_SIZE; ++i)
  713. list_add(&target->tx_ring[i]->list, &target->free_tx);
  714. if (ret == 0)
  715. ret = srp_connect_target(target);
  716. if (ret == 0)
  717. shost_printk(KERN_INFO, target->scsi_host,
  718. PFX "reconnect succeeded\n");
  719. return ret;
  720. }
  721. static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
  722. unsigned int dma_len, u32 rkey)
  723. {
  724. struct srp_direct_buf *desc = state->desc;
  725. desc->va = cpu_to_be64(dma_addr);
  726. desc->key = cpu_to_be32(rkey);
  727. desc->len = cpu_to_be32(dma_len);
  728. state->total_len += dma_len;
  729. state->desc++;
  730. state->ndesc++;
  731. }
  732. static int srp_map_finish_fmr(struct srp_map_state *state,
  733. struct srp_target_port *target)
  734. {
  735. struct srp_device *dev = target->srp_host->srp_dev;
  736. struct ib_pool_fmr *fmr;
  737. u64 io_addr = 0;
  738. if (!state->npages)
  739. return 0;
  740. if (state->npages == 1) {
  741. srp_map_desc(state, state->base_dma_addr, state->fmr_len,
  742. target->rkey);
  743. state->npages = state->fmr_len = 0;
  744. return 0;
  745. }
  746. fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
  747. state->npages, io_addr);
  748. if (IS_ERR(fmr))
  749. return PTR_ERR(fmr);
  750. *state->next_fmr++ = fmr;
  751. state->nfmr++;
  752. srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
  753. state->npages = state->fmr_len = 0;
  754. return 0;
  755. }
  756. static void srp_map_update_start(struct srp_map_state *state,
  757. struct scatterlist *sg, int sg_index,
  758. dma_addr_t dma_addr)
  759. {
  760. state->unmapped_sg = sg;
  761. state->unmapped_index = sg_index;
  762. state->unmapped_addr = dma_addr;
  763. }
  764. static int srp_map_sg_entry(struct srp_map_state *state,
  765. struct srp_target_port *target,
  766. struct scatterlist *sg, int sg_index,
  767. int use_fmr)
  768. {
  769. struct srp_device *dev = target->srp_host->srp_dev;
  770. struct ib_device *ibdev = dev->dev;
  771. dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
  772. unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
  773. unsigned int len;
  774. int ret;
  775. if (!dma_len)
  776. return 0;
  777. if (use_fmr == SRP_MAP_NO_FMR) {
  778. /* Once we're in direct map mode for a request, we don't
  779. * go back to FMR mode, so no need to update anything
  780. * other than the descriptor.
  781. */
  782. srp_map_desc(state, dma_addr, dma_len, target->rkey);
  783. return 0;
  784. }
  785. /* If we start at an offset into the FMR page, don't merge into
  786. * the current FMR. Finish it out, and use the kernel's MR for this
  787. * sg entry. This is to avoid potential bugs on some SRP targets
  788. * that were never quite defined, but went away when the initiator
  789. * avoided using FMR on such page fragments.
  790. */
  791. if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
  792. ret = srp_map_finish_fmr(state, target);
  793. if (ret)
  794. return ret;
  795. srp_map_desc(state, dma_addr, dma_len, target->rkey);
  796. srp_map_update_start(state, NULL, 0, 0);
  797. return 0;
  798. }
  799. /* If this is the first sg to go into the FMR, save our position.
  800. * We need to know the first unmapped entry, its index, and the
  801. * first unmapped address within that entry to be able to restart
  802. * mapping after an error.
  803. */
  804. if (!state->unmapped_sg)
  805. srp_map_update_start(state, sg, sg_index, dma_addr);
  806. while (dma_len) {
  807. if (state->npages == SRP_FMR_SIZE) {
  808. ret = srp_map_finish_fmr(state, target);
  809. if (ret)
  810. return ret;
  811. srp_map_update_start(state, sg, sg_index, dma_addr);
  812. }
  813. len = min_t(unsigned int, dma_len, dev->fmr_page_size);
  814. if (!state->npages)
  815. state->base_dma_addr = dma_addr;
  816. state->pages[state->npages++] = dma_addr;
  817. state->fmr_len += len;
  818. dma_addr += len;
  819. dma_len -= len;
  820. }
  821. /* If the last entry of the FMR wasn't a full page, then we need to
  822. * close it out and start a new one -- we can only merge at page
  823. * boundries.
  824. */
  825. ret = 0;
  826. if (len != dev->fmr_page_size) {
  827. ret = srp_map_finish_fmr(state, target);
  828. if (!ret)
  829. srp_map_update_start(state, NULL, 0, 0);
  830. }
  831. return ret;
  832. }
  833. static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
  834. struct srp_request *req)
  835. {
  836. struct scatterlist *scat, *sg;
  837. struct srp_cmd *cmd = req->cmd->buf;
  838. int i, len, nents, count, use_fmr;
  839. struct srp_device *dev;
  840. struct ib_device *ibdev;
  841. struct srp_map_state state;
  842. struct srp_indirect_buf *indirect_hdr;
  843. u32 table_len;
  844. u8 fmt;
  845. if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
  846. return sizeof (struct srp_cmd);
  847. if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
  848. scmnd->sc_data_direction != DMA_TO_DEVICE) {
  849. shost_printk(KERN_WARNING, target->scsi_host,
  850. PFX "Unhandled data direction %d\n",
  851. scmnd->sc_data_direction);
  852. return -EINVAL;
  853. }
  854. nents = scsi_sg_count(scmnd);
  855. scat = scsi_sglist(scmnd);
  856. dev = target->srp_host->srp_dev;
  857. ibdev = dev->dev;
  858. count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
  859. if (unlikely(count == 0))
  860. return -EIO;
  861. fmt = SRP_DATA_DESC_DIRECT;
  862. len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
  863. if (count == 1) {
  864. /*
  865. * The midlayer only generated a single gather/scatter
  866. * entry, or DMA mapping coalesced everything to a
  867. * single entry. So a direct descriptor along with
  868. * the DMA MR suffices.
  869. */
  870. struct srp_direct_buf *buf = (void *) cmd->add_data;
  871. buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
  872. buf->key = cpu_to_be32(target->rkey);
  873. buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
  874. req->nfmr = 0;
  875. goto map_complete;
  876. }
  877. /* We have more than one scatter/gather entry, so build our indirect
  878. * descriptor table, trying to merge as many entries with FMR as we
  879. * can.
  880. */
  881. indirect_hdr = (void *) cmd->add_data;
  882. ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
  883. target->indirect_size, DMA_TO_DEVICE);
  884. memset(&state, 0, sizeof(state));
  885. state.desc = req->indirect_desc;
  886. state.pages = req->map_page;
  887. state.next_fmr = req->fmr_list;
  888. use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
  889. for_each_sg(scat, sg, count, i) {
  890. if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
  891. /* FMR mapping failed, so backtrack to the first
  892. * unmapped entry and continue on without using FMR.
  893. */
  894. dma_addr_t dma_addr;
  895. unsigned int dma_len;
  896. backtrack:
  897. sg = state.unmapped_sg;
  898. i = state.unmapped_index;
  899. dma_addr = ib_sg_dma_address(ibdev, sg);
  900. dma_len = ib_sg_dma_len(ibdev, sg);
  901. dma_len -= (state.unmapped_addr - dma_addr);
  902. dma_addr = state.unmapped_addr;
  903. use_fmr = SRP_MAP_NO_FMR;
  904. srp_map_desc(&state, dma_addr, dma_len, target->rkey);
  905. }
  906. }
  907. if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
  908. goto backtrack;
  909. /* We've mapped the request, now pull as much of the indirect
  910. * descriptor table as we can into the command buffer. If this
  911. * target is not using an external indirect table, we are
  912. * guaranteed to fit into the command, as the SCSI layer won't
  913. * give us more S/G entries than we allow.
  914. */
  915. req->nfmr = state.nfmr;
  916. if (state.ndesc == 1) {
  917. /* FMR mapping was able to collapse this to one entry,
  918. * so use a direct descriptor.
  919. */
  920. struct srp_direct_buf *buf = (void *) cmd->add_data;
  921. *buf = req->indirect_desc[0];
  922. goto map_complete;
  923. }
  924. if (unlikely(target->cmd_sg_cnt < state.ndesc &&
  925. !target->allow_ext_sg)) {
  926. shost_printk(KERN_ERR, target->scsi_host,
  927. "Could not fit S/G list into SRP_CMD\n");
  928. return -EIO;
  929. }
  930. count = min(state.ndesc, target->cmd_sg_cnt);
  931. table_len = state.ndesc * sizeof (struct srp_direct_buf);
  932. fmt = SRP_DATA_DESC_INDIRECT;
  933. len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
  934. len += count * sizeof (struct srp_direct_buf);
  935. memcpy(indirect_hdr->desc_list, req->indirect_desc,
  936. count * sizeof (struct srp_direct_buf));
  937. indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
  938. indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
  939. indirect_hdr->table_desc.len = cpu_to_be32(table_len);
  940. indirect_hdr->len = cpu_to_be32(state.total_len);
  941. if (scmnd->sc_data_direction == DMA_TO_DEVICE)
  942. cmd->data_out_desc_cnt = count;
  943. else
  944. cmd->data_in_desc_cnt = count;
  945. ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
  946. DMA_TO_DEVICE);
  947. map_complete:
  948. if (scmnd->sc_data_direction == DMA_TO_DEVICE)
  949. cmd->buf_fmt = fmt << 4;
  950. else
  951. cmd->buf_fmt = fmt;
  952. return len;
  953. }
  954. /*
  955. * Return an IU and possible credit to the free pool
  956. */
  957. static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
  958. enum srp_iu_type iu_type)
  959. {
  960. unsigned long flags;
  961. spin_lock_irqsave(&target->lock, flags);
  962. list_add(&iu->list, &target->free_tx);
  963. if (iu_type != SRP_IU_RSP)
  964. ++target->req_lim;
  965. spin_unlock_irqrestore(&target->lock, flags);
  966. }
  967. /*
  968. * Must be called with target->lock held to protect req_lim and free_tx.
  969. * If IU is not sent, it must be returned using srp_put_tx_iu().
  970. *
  971. * Note:
  972. * An upper limit for the number of allocated information units for each
  973. * request type is:
  974. * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
  975. * more than Scsi_Host.can_queue requests.
  976. * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
  977. * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
  978. * one unanswered SRP request to an initiator.
  979. */
  980. static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
  981. enum srp_iu_type iu_type)
  982. {
  983. s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
  984. struct srp_iu *iu;
  985. srp_send_completion(target->send_cq, target);
  986. if (list_empty(&target->free_tx))
  987. return NULL;
  988. /* Initiator responses to target requests do not consume credits */
  989. if (iu_type != SRP_IU_RSP) {
  990. if (target->req_lim <= rsv) {
  991. ++target->zero_req_lim;
  992. return NULL;
  993. }
  994. --target->req_lim;
  995. }
  996. iu = list_first_entry(&target->free_tx, struct srp_iu, list);
  997. list_del(&iu->list);
  998. return iu;
  999. }
  1000. static int srp_post_send(struct srp_target_port *target,
  1001. struct srp_iu *iu, int len)
  1002. {
  1003. struct ib_sge list;
  1004. struct ib_send_wr wr, *bad_wr;
  1005. list.addr = iu->dma;
  1006. list.length = len;
  1007. list.lkey = target->lkey;
  1008. wr.next = NULL;
  1009. wr.wr_id = (uintptr_t) iu;
  1010. wr.sg_list = &list;
  1011. wr.num_sge = 1;
  1012. wr.opcode = IB_WR_SEND;
  1013. wr.send_flags = IB_SEND_SIGNALED;
  1014. return ib_post_send(target->qp, &wr, &bad_wr);
  1015. }
  1016. static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
  1017. {
  1018. struct ib_recv_wr wr, *bad_wr;
  1019. struct ib_sge list;
  1020. list.addr = iu->dma;
  1021. list.length = iu->size;
  1022. list.lkey = target->lkey;
  1023. wr.next = NULL;
  1024. wr.wr_id = (uintptr_t) iu;
  1025. wr.sg_list = &list;
  1026. wr.num_sge = 1;
  1027. return ib_post_recv(target->qp, &wr, &bad_wr);
  1028. }
  1029. static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
  1030. {
  1031. struct srp_request *req;
  1032. struct scsi_cmnd *scmnd;
  1033. unsigned long flags;
  1034. if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
  1035. spin_lock_irqsave(&target->lock, flags);
  1036. target->req_lim += be32_to_cpu(rsp->req_lim_delta);
  1037. spin_unlock_irqrestore(&target->lock, flags);
  1038. target->tsk_mgmt_status = -1;
  1039. if (be32_to_cpu(rsp->resp_data_len) >= 4)
  1040. target->tsk_mgmt_status = rsp->data[3];
  1041. complete(&target->tsk_mgmt_done);
  1042. } else {
  1043. req = &target->req_ring[rsp->tag];
  1044. scmnd = srp_claim_req(target, req, NULL);
  1045. if (!scmnd) {
  1046. shost_printk(KERN_ERR, target->scsi_host,
  1047. "Null scmnd for RSP w/tag %016llx\n",
  1048. (unsigned long long) rsp->tag);
  1049. spin_lock_irqsave(&target->lock, flags);
  1050. target->req_lim += be32_to_cpu(rsp->req_lim_delta);
  1051. spin_unlock_irqrestore(&target->lock, flags);
  1052. return;
  1053. }
  1054. scmnd->result = rsp->status;
  1055. if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
  1056. memcpy(scmnd->sense_buffer, rsp->data +
  1057. be32_to_cpu(rsp->resp_data_len),
  1058. min_t(int, be32_to_cpu(rsp->sense_data_len),
  1059. SCSI_SENSE_BUFFERSIZE));
  1060. }
  1061. if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
  1062. scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
  1063. else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
  1064. scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
  1065. srp_free_req(target, req, scmnd,
  1066. be32_to_cpu(rsp->req_lim_delta));
  1067. scmnd->host_scribble = NULL;
  1068. scmnd->scsi_done(scmnd);
  1069. }
  1070. }
  1071. static int srp_response_common(struct srp_target_port *target, s32 req_delta,
  1072. void *rsp, int len)
  1073. {
  1074. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1075. unsigned long flags;
  1076. struct srp_iu *iu;
  1077. int err;
  1078. spin_lock_irqsave(&target->lock, flags);
  1079. target->req_lim += req_delta;
  1080. iu = __srp_get_tx_iu(target, SRP_IU_RSP);
  1081. spin_unlock_irqrestore(&target->lock, flags);
  1082. if (!iu) {
  1083. shost_printk(KERN_ERR, target->scsi_host, PFX
  1084. "no IU available to send response\n");
  1085. return 1;
  1086. }
  1087. ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
  1088. memcpy(iu->buf, rsp, len);
  1089. ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
  1090. err = srp_post_send(target, iu, len);
  1091. if (err) {
  1092. shost_printk(KERN_ERR, target->scsi_host, PFX
  1093. "unable to post response: %d\n", err);
  1094. srp_put_tx_iu(target, iu, SRP_IU_RSP);
  1095. }
  1096. return err;
  1097. }
  1098. static void srp_process_cred_req(struct srp_target_port *target,
  1099. struct srp_cred_req *req)
  1100. {
  1101. struct srp_cred_rsp rsp = {
  1102. .opcode = SRP_CRED_RSP,
  1103. .tag = req->tag,
  1104. };
  1105. s32 delta = be32_to_cpu(req->req_lim_delta);
  1106. if (srp_response_common(target, delta, &rsp, sizeof rsp))
  1107. shost_printk(KERN_ERR, target->scsi_host, PFX
  1108. "problems processing SRP_CRED_REQ\n");
  1109. }
  1110. static void srp_process_aer_req(struct srp_target_port *target,
  1111. struct srp_aer_req *req)
  1112. {
  1113. struct srp_aer_rsp rsp = {
  1114. .opcode = SRP_AER_RSP,
  1115. .tag = req->tag,
  1116. };
  1117. s32 delta = be32_to_cpu(req->req_lim_delta);
  1118. shost_printk(KERN_ERR, target->scsi_host, PFX
  1119. "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
  1120. if (srp_response_common(target, delta, &rsp, sizeof rsp))
  1121. shost_printk(KERN_ERR, target->scsi_host, PFX
  1122. "problems processing SRP_AER_REQ\n");
  1123. }
  1124. static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
  1125. {
  1126. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1127. struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
  1128. int res;
  1129. u8 opcode;
  1130. ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
  1131. DMA_FROM_DEVICE);
  1132. opcode = *(u8 *) iu->buf;
  1133. if (0) {
  1134. shost_printk(KERN_ERR, target->scsi_host,
  1135. PFX "recv completion, opcode 0x%02x\n", opcode);
  1136. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
  1137. iu->buf, wc->byte_len, true);
  1138. }
  1139. switch (opcode) {
  1140. case SRP_RSP:
  1141. srp_process_rsp(target, iu->buf);
  1142. break;
  1143. case SRP_CRED_REQ:
  1144. srp_process_cred_req(target, iu->buf);
  1145. break;
  1146. case SRP_AER_REQ:
  1147. srp_process_aer_req(target, iu->buf);
  1148. break;
  1149. case SRP_T_LOGOUT:
  1150. /* XXX Handle target logout */
  1151. shost_printk(KERN_WARNING, target->scsi_host,
  1152. PFX "Got target logout request\n");
  1153. break;
  1154. default:
  1155. shost_printk(KERN_WARNING, target->scsi_host,
  1156. PFX "Unhandled SRP opcode 0x%02x\n", opcode);
  1157. break;
  1158. }
  1159. ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
  1160. DMA_FROM_DEVICE);
  1161. res = srp_post_recv(target, iu);
  1162. if (res != 0)
  1163. shost_printk(KERN_ERR, target->scsi_host,
  1164. PFX "Recv failed with error code %d\n", res);
  1165. }
  1166. /**
  1167. * srp_tl_err_work() - handle a transport layer error
  1168. *
  1169. * Note: This function may get invoked before the rport has been created,
  1170. * hence the target->rport test.
  1171. */
  1172. static void srp_tl_err_work(struct work_struct *work)
  1173. {
  1174. struct srp_target_port *target;
  1175. target = container_of(work, struct srp_target_port, tl_err_work);
  1176. if (target->rport)
  1177. srp_start_tl_fail_timers(target->rport);
  1178. }
  1179. static void srp_handle_qp_err(enum ib_wc_status wc_status,
  1180. enum ib_wc_opcode wc_opcode,
  1181. struct srp_target_port *target)
  1182. {
  1183. if (target->connected && !target->qp_in_error) {
  1184. shost_printk(KERN_ERR, target->scsi_host,
  1185. PFX "failed %s status %d\n",
  1186. wc_opcode & IB_WC_RECV ? "receive" : "send",
  1187. wc_status);
  1188. queue_work(system_long_wq, &target->tl_err_work);
  1189. }
  1190. target->qp_in_error = true;
  1191. }
  1192. static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
  1193. {
  1194. struct srp_target_port *target = target_ptr;
  1195. struct ib_wc wc;
  1196. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1197. while (ib_poll_cq(cq, 1, &wc) > 0) {
  1198. if (likely(wc.status == IB_WC_SUCCESS)) {
  1199. srp_handle_recv(target, &wc);
  1200. } else {
  1201. srp_handle_qp_err(wc.status, wc.opcode, target);
  1202. }
  1203. }
  1204. }
  1205. static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
  1206. {
  1207. struct srp_target_port *target = target_ptr;
  1208. struct ib_wc wc;
  1209. struct srp_iu *iu;
  1210. while (ib_poll_cq(cq, 1, &wc) > 0) {
  1211. if (likely(wc.status == IB_WC_SUCCESS)) {
  1212. iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
  1213. list_add(&iu->list, &target->free_tx);
  1214. } else {
  1215. srp_handle_qp_err(wc.status, wc.opcode, target);
  1216. }
  1217. }
  1218. }
  1219. static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
  1220. {
  1221. struct srp_target_port *target = host_to_target(shost);
  1222. struct srp_rport *rport = target->rport;
  1223. struct srp_request *req;
  1224. struct srp_iu *iu;
  1225. struct srp_cmd *cmd;
  1226. struct ib_device *dev;
  1227. unsigned long flags;
  1228. int len, result;
  1229. const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
  1230. /*
  1231. * The SCSI EH thread is the only context from which srp_queuecommand()
  1232. * can get invoked for blocked devices (SDEV_BLOCK /
  1233. * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
  1234. * locking the rport mutex if invoked from inside the SCSI EH.
  1235. */
  1236. if (in_scsi_eh)
  1237. mutex_lock(&rport->mutex);
  1238. result = srp_chkready(target->rport);
  1239. if (unlikely(result)) {
  1240. scmnd->result = result;
  1241. scmnd->scsi_done(scmnd);
  1242. goto unlock_rport;
  1243. }
  1244. spin_lock_irqsave(&target->lock, flags);
  1245. iu = __srp_get_tx_iu(target, SRP_IU_CMD);
  1246. if (!iu)
  1247. goto err_unlock;
  1248. req = list_first_entry(&target->free_reqs, struct srp_request, list);
  1249. list_del(&req->list);
  1250. spin_unlock_irqrestore(&target->lock, flags);
  1251. dev = target->srp_host->srp_dev->dev;
  1252. ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
  1253. DMA_TO_DEVICE);
  1254. scmnd->result = 0;
  1255. scmnd->host_scribble = (void *) req;
  1256. cmd = iu->buf;
  1257. memset(cmd, 0, sizeof *cmd);
  1258. cmd->opcode = SRP_CMD;
  1259. cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
  1260. cmd->tag = req->index;
  1261. memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
  1262. req->scmnd = scmnd;
  1263. req->cmd = iu;
  1264. len = srp_map_data(scmnd, target, req);
  1265. if (len < 0) {
  1266. shost_printk(KERN_ERR, target->scsi_host,
  1267. PFX "Failed to map data\n");
  1268. goto err_iu;
  1269. }
  1270. ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
  1271. DMA_TO_DEVICE);
  1272. if (srp_post_send(target, iu, len)) {
  1273. shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
  1274. goto err_unmap;
  1275. }
  1276. unlock_rport:
  1277. if (in_scsi_eh)
  1278. mutex_unlock(&rport->mutex);
  1279. return 0;
  1280. err_unmap:
  1281. srp_unmap_data(scmnd, target, req);
  1282. err_iu:
  1283. srp_put_tx_iu(target, iu, SRP_IU_CMD);
  1284. spin_lock_irqsave(&target->lock, flags);
  1285. list_add(&req->list, &target->free_reqs);
  1286. err_unlock:
  1287. spin_unlock_irqrestore(&target->lock, flags);
  1288. if (in_scsi_eh)
  1289. mutex_unlock(&rport->mutex);
  1290. return SCSI_MLQUEUE_HOST_BUSY;
  1291. }
  1292. static int srp_alloc_iu_bufs(struct srp_target_port *target)
  1293. {
  1294. int i;
  1295. for (i = 0; i < SRP_RQ_SIZE; ++i) {
  1296. target->rx_ring[i] = srp_alloc_iu(target->srp_host,
  1297. target->max_ti_iu_len,
  1298. GFP_KERNEL, DMA_FROM_DEVICE);
  1299. if (!target->rx_ring[i])
  1300. goto err;
  1301. }
  1302. for (i = 0; i < SRP_SQ_SIZE; ++i) {
  1303. target->tx_ring[i] = srp_alloc_iu(target->srp_host,
  1304. target->max_iu_len,
  1305. GFP_KERNEL, DMA_TO_DEVICE);
  1306. if (!target->tx_ring[i])
  1307. goto err;
  1308. list_add(&target->tx_ring[i]->list, &target->free_tx);
  1309. }
  1310. return 0;
  1311. err:
  1312. for (i = 0; i < SRP_RQ_SIZE; ++i) {
  1313. srp_free_iu(target->srp_host, target->rx_ring[i]);
  1314. target->rx_ring[i] = NULL;
  1315. }
  1316. for (i = 0; i < SRP_SQ_SIZE; ++i) {
  1317. srp_free_iu(target->srp_host, target->tx_ring[i]);
  1318. target->tx_ring[i] = NULL;
  1319. }
  1320. return -ENOMEM;
  1321. }
  1322. static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
  1323. {
  1324. uint64_t T_tr_ns, max_compl_time_ms;
  1325. uint32_t rq_tmo_jiffies;
  1326. /*
  1327. * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
  1328. * table 91), both the QP timeout and the retry count have to be set
  1329. * for RC QP's during the RTR to RTS transition.
  1330. */
  1331. WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
  1332. (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
  1333. /*
  1334. * Set target->rq_tmo_jiffies to one second more than the largest time
  1335. * it can take before an error completion is generated. See also
  1336. * C9-140..142 in the IBTA spec for more information about how to
  1337. * convert the QP Local ACK Timeout value to nanoseconds.
  1338. */
  1339. T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
  1340. max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
  1341. do_div(max_compl_time_ms, NSEC_PER_MSEC);
  1342. rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
  1343. return rq_tmo_jiffies;
  1344. }
  1345. static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
  1346. struct srp_login_rsp *lrsp,
  1347. struct srp_target_port *target)
  1348. {
  1349. struct ib_qp_attr *qp_attr = NULL;
  1350. int attr_mask = 0;
  1351. int ret;
  1352. int i;
  1353. if (lrsp->opcode == SRP_LOGIN_RSP) {
  1354. target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
  1355. target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
  1356. /*
  1357. * Reserve credits for task management so we don't
  1358. * bounce requests back to the SCSI mid-layer.
  1359. */
  1360. target->scsi_host->can_queue
  1361. = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
  1362. target->scsi_host->can_queue);
  1363. } else {
  1364. shost_printk(KERN_WARNING, target->scsi_host,
  1365. PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
  1366. ret = -ECONNRESET;
  1367. goto error;
  1368. }
  1369. if (!target->rx_ring[0]) {
  1370. ret = srp_alloc_iu_bufs(target);
  1371. if (ret)
  1372. goto error;
  1373. }
  1374. ret = -ENOMEM;
  1375. qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
  1376. if (!qp_attr)
  1377. goto error;
  1378. qp_attr->qp_state = IB_QPS_RTR;
  1379. ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
  1380. if (ret)
  1381. goto error_free;
  1382. ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
  1383. if (ret)
  1384. goto error_free;
  1385. for (i = 0; i < SRP_RQ_SIZE; i++) {
  1386. struct srp_iu *iu = target->rx_ring[i];
  1387. ret = srp_post_recv(target, iu);
  1388. if (ret)
  1389. goto error_free;
  1390. }
  1391. qp_attr->qp_state = IB_QPS_RTS;
  1392. ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
  1393. if (ret)
  1394. goto error_free;
  1395. target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
  1396. ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
  1397. if (ret)
  1398. goto error_free;
  1399. ret = ib_send_cm_rtu(cm_id, NULL, 0);
  1400. error_free:
  1401. kfree(qp_attr);
  1402. error:
  1403. target->status = ret;
  1404. }
  1405. static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
  1406. struct ib_cm_event *event,
  1407. struct srp_target_port *target)
  1408. {
  1409. struct Scsi_Host *shost = target->scsi_host;
  1410. struct ib_class_port_info *cpi;
  1411. int opcode;
  1412. switch (event->param.rej_rcvd.reason) {
  1413. case IB_CM_REJ_PORT_CM_REDIRECT:
  1414. cpi = event->param.rej_rcvd.ari;
  1415. target->path.dlid = cpi->redirect_lid;
  1416. target->path.pkey = cpi->redirect_pkey;
  1417. cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
  1418. memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
  1419. target->status = target->path.dlid ?
  1420. SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
  1421. break;
  1422. case IB_CM_REJ_PORT_REDIRECT:
  1423. if (srp_target_is_topspin(target)) {
  1424. /*
  1425. * Topspin/Cisco SRP gateways incorrectly send
  1426. * reject reason code 25 when they mean 24
  1427. * (port redirect).
  1428. */
  1429. memcpy(target->path.dgid.raw,
  1430. event->param.rej_rcvd.ari, 16);
  1431. shost_printk(KERN_DEBUG, shost,
  1432. PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
  1433. (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
  1434. (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
  1435. target->status = SRP_PORT_REDIRECT;
  1436. } else {
  1437. shost_printk(KERN_WARNING, shost,
  1438. " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
  1439. target->status = -ECONNRESET;
  1440. }
  1441. break;
  1442. case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
  1443. shost_printk(KERN_WARNING, shost,
  1444. " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
  1445. target->status = -ECONNRESET;
  1446. break;
  1447. case IB_CM_REJ_CONSUMER_DEFINED:
  1448. opcode = *(u8 *) event->private_data;
  1449. if (opcode == SRP_LOGIN_REJ) {
  1450. struct srp_login_rej *rej = event->private_data;
  1451. u32 reason = be32_to_cpu(rej->reason);
  1452. if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
  1453. shost_printk(KERN_WARNING, shost,
  1454. PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
  1455. else
  1456. shost_printk(KERN_WARNING, shost,
  1457. PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
  1458. } else
  1459. shost_printk(KERN_WARNING, shost,
  1460. " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
  1461. " opcode 0x%02x\n", opcode);
  1462. target->status = -ECONNRESET;
  1463. break;
  1464. case IB_CM_REJ_STALE_CONN:
  1465. shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
  1466. target->status = SRP_STALE_CONN;
  1467. break;
  1468. default:
  1469. shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
  1470. event->param.rej_rcvd.reason);
  1471. target->status = -ECONNRESET;
  1472. }
  1473. }
  1474. static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  1475. {
  1476. struct srp_target_port *target = cm_id->context;
  1477. int comp = 0;
  1478. switch (event->event) {
  1479. case IB_CM_REQ_ERROR:
  1480. shost_printk(KERN_DEBUG, target->scsi_host,
  1481. PFX "Sending CM REQ failed\n");
  1482. comp = 1;
  1483. target->status = -ECONNRESET;
  1484. break;
  1485. case IB_CM_REP_RECEIVED:
  1486. comp = 1;
  1487. srp_cm_rep_handler(cm_id, event->private_data, target);
  1488. break;
  1489. case IB_CM_REJ_RECEIVED:
  1490. shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
  1491. comp = 1;
  1492. srp_cm_rej_handler(cm_id, event, target);
  1493. break;
  1494. case IB_CM_DREQ_RECEIVED:
  1495. shost_printk(KERN_WARNING, target->scsi_host,
  1496. PFX "DREQ received - connection closed\n");
  1497. srp_change_conn_state(target, false);
  1498. if (ib_send_cm_drep(cm_id, NULL, 0))
  1499. shost_printk(KERN_ERR, target->scsi_host,
  1500. PFX "Sending CM DREP failed\n");
  1501. queue_work(system_long_wq, &target->tl_err_work);
  1502. break;
  1503. case IB_CM_TIMEWAIT_EXIT:
  1504. shost_printk(KERN_ERR, target->scsi_host,
  1505. PFX "connection closed\n");
  1506. target->status = 0;
  1507. break;
  1508. case IB_CM_MRA_RECEIVED:
  1509. case IB_CM_DREQ_ERROR:
  1510. case IB_CM_DREP_RECEIVED:
  1511. break;
  1512. default:
  1513. shost_printk(KERN_WARNING, target->scsi_host,
  1514. PFX "Unhandled CM event %d\n", event->event);
  1515. break;
  1516. }
  1517. if (comp)
  1518. complete(&target->done);
  1519. return 0;
  1520. }
  1521. static int srp_send_tsk_mgmt(struct srp_target_port *target,
  1522. u64 req_tag, unsigned int lun, u8 func)
  1523. {
  1524. struct srp_rport *rport = target->rport;
  1525. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1526. struct srp_iu *iu;
  1527. struct srp_tsk_mgmt *tsk_mgmt;
  1528. if (!target->connected || target->qp_in_error)
  1529. return -1;
  1530. init_completion(&target->tsk_mgmt_done);
  1531. /*
  1532. * Lock the rport mutex to avoid that srp_create_target_ib() is
  1533. * invoked while a task management function is being sent.
  1534. */
  1535. mutex_lock(&rport->mutex);
  1536. spin_lock_irq(&target->lock);
  1537. iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
  1538. spin_unlock_irq(&target->lock);
  1539. if (!iu) {
  1540. mutex_unlock(&rport->mutex);
  1541. return -1;
  1542. }
  1543. ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
  1544. DMA_TO_DEVICE);
  1545. tsk_mgmt = iu->buf;
  1546. memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
  1547. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1548. tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
  1549. tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
  1550. tsk_mgmt->tsk_mgmt_func = func;
  1551. tsk_mgmt->task_tag = req_tag;
  1552. ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
  1553. DMA_TO_DEVICE);
  1554. if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
  1555. srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
  1556. mutex_unlock(&rport->mutex);
  1557. return -1;
  1558. }
  1559. mutex_unlock(&rport->mutex);
  1560. if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
  1561. msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
  1562. return -1;
  1563. return 0;
  1564. }
  1565. static int srp_abort(struct scsi_cmnd *scmnd)
  1566. {
  1567. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1568. struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
  1569. int ret;
  1570. shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
  1571. if (!req || !srp_claim_req(target, req, scmnd))
  1572. return FAILED;
  1573. if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
  1574. SRP_TSK_ABORT_TASK) == 0)
  1575. ret = SUCCESS;
  1576. else if (target->rport->state == SRP_RPORT_LOST)
  1577. ret = FAST_IO_FAIL;
  1578. else
  1579. ret = FAILED;
  1580. srp_free_req(target, req, scmnd, 0);
  1581. scmnd->result = DID_ABORT << 16;
  1582. scmnd->scsi_done(scmnd);
  1583. return ret;
  1584. }
  1585. static int srp_reset_device(struct scsi_cmnd *scmnd)
  1586. {
  1587. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1588. int i;
  1589. shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
  1590. if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
  1591. SRP_TSK_LUN_RESET))
  1592. return FAILED;
  1593. if (target->tsk_mgmt_status)
  1594. return FAILED;
  1595. for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) {
  1596. struct srp_request *req = &target->req_ring[i];
  1597. if (req->scmnd && req->scmnd->device == scmnd->device)
  1598. srp_finish_req(target, req, DID_RESET << 16);
  1599. }
  1600. return SUCCESS;
  1601. }
  1602. static int srp_reset_host(struct scsi_cmnd *scmnd)
  1603. {
  1604. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1605. shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
  1606. return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
  1607. }
  1608. static int srp_slave_configure(struct scsi_device *sdev)
  1609. {
  1610. struct Scsi_Host *shost = sdev->host;
  1611. struct srp_target_port *target = host_to_target(shost);
  1612. struct request_queue *q = sdev->request_queue;
  1613. unsigned long timeout;
  1614. if (sdev->type == TYPE_DISK) {
  1615. timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
  1616. blk_queue_rq_timeout(q, timeout);
  1617. }
  1618. return 0;
  1619. }
  1620. static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
  1621. char *buf)
  1622. {
  1623. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1624. return sprintf(buf, "0x%016llx\n",
  1625. (unsigned long long) be64_to_cpu(target->id_ext));
  1626. }
  1627. static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
  1628. char *buf)
  1629. {
  1630. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1631. return sprintf(buf, "0x%016llx\n",
  1632. (unsigned long long) be64_to_cpu(target->ioc_guid));
  1633. }
  1634. static ssize_t show_service_id(struct device *dev,
  1635. struct device_attribute *attr, char *buf)
  1636. {
  1637. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1638. return sprintf(buf, "0x%016llx\n",
  1639. (unsigned long long) be64_to_cpu(target->service_id));
  1640. }
  1641. static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
  1642. char *buf)
  1643. {
  1644. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1645. return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
  1646. }
  1647. static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
  1648. char *buf)
  1649. {
  1650. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1651. return sprintf(buf, "%pI6\n", target->path.sgid.raw);
  1652. }
  1653. static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
  1654. char *buf)
  1655. {
  1656. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1657. return sprintf(buf, "%pI6\n", target->path.dgid.raw);
  1658. }
  1659. static ssize_t show_orig_dgid(struct device *dev,
  1660. struct device_attribute *attr, char *buf)
  1661. {
  1662. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1663. return sprintf(buf, "%pI6\n", target->orig_dgid);
  1664. }
  1665. static ssize_t show_req_lim(struct device *dev,
  1666. struct device_attribute *attr, char *buf)
  1667. {
  1668. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1669. return sprintf(buf, "%d\n", target->req_lim);
  1670. }
  1671. static ssize_t show_zero_req_lim(struct device *dev,
  1672. struct device_attribute *attr, char *buf)
  1673. {
  1674. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1675. return sprintf(buf, "%d\n", target->zero_req_lim);
  1676. }
  1677. static ssize_t show_local_ib_port(struct device *dev,
  1678. struct device_attribute *attr, char *buf)
  1679. {
  1680. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1681. return sprintf(buf, "%d\n", target->srp_host->port);
  1682. }
  1683. static ssize_t show_local_ib_device(struct device *dev,
  1684. struct device_attribute *attr, char *buf)
  1685. {
  1686. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1687. return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
  1688. }
  1689. static ssize_t show_comp_vector(struct device *dev,
  1690. struct device_attribute *attr, char *buf)
  1691. {
  1692. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1693. return sprintf(buf, "%d\n", target->comp_vector);
  1694. }
  1695. static ssize_t show_tl_retry_count(struct device *dev,
  1696. struct device_attribute *attr, char *buf)
  1697. {
  1698. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1699. return sprintf(buf, "%d\n", target->tl_retry_count);
  1700. }
  1701. static ssize_t show_cmd_sg_entries(struct device *dev,
  1702. struct device_attribute *attr, char *buf)
  1703. {
  1704. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1705. return sprintf(buf, "%u\n", target->cmd_sg_cnt);
  1706. }
  1707. static ssize_t show_allow_ext_sg(struct device *dev,
  1708. struct device_attribute *attr, char *buf)
  1709. {
  1710. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1711. return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
  1712. }
  1713. static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
  1714. static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
  1715. static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
  1716. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1717. static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
  1718. static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
  1719. static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
  1720. static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
  1721. static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
  1722. static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
  1723. static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
  1724. static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
  1725. static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
  1726. static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
  1727. static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
  1728. static struct device_attribute *srp_host_attrs[] = {
  1729. &dev_attr_id_ext,
  1730. &dev_attr_ioc_guid,
  1731. &dev_attr_service_id,
  1732. &dev_attr_pkey,
  1733. &dev_attr_sgid,
  1734. &dev_attr_dgid,
  1735. &dev_attr_orig_dgid,
  1736. &dev_attr_req_lim,
  1737. &dev_attr_zero_req_lim,
  1738. &dev_attr_local_ib_port,
  1739. &dev_attr_local_ib_device,
  1740. &dev_attr_comp_vector,
  1741. &dev_attr_tl_retry_count,
  1742. &dev_attr_cmd_sg_entries,
  1743. &dev_attr_allow_ext_sg,
  1744. NULL
  1745. };
  1746. static struct scsi_host_template srp_template = {
  1747. .module = THIS_MODULE,
  1748. .name = "InfiniBand SRP initiator",
  1749. .proc_name = DRV_NAME,
  1750. .slave_configure = srp_slave_configure,
  1751. .info = srp_target_info,
  1752. .queuecommand = srp_queuecommand,
  1753. .eh_abort_handler = srp_abort,
  1754. .eh_device_reset_handler = srp_reset_device,
  1755. .eh_host_reset_handler = srp_reset_host,
  1756. .skip_settle_delay = true,
  1757. .sg_tablesize = SRP_DEF_SG_TABLESIZE,
  1758. .can_queue = SRP_CMD_SQ_SIZE,
  1759. .this_id = -1,
  1760. .cmd_per_lun = SRP_CMD_SQ_SIZE,
  1761. .use_clustering = ENABLE_CLUSTERING,
  1762. .shost_attrs = srp_host_attrs
  1763. };
  1764. static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
  1765. {
  1766. struct srp_rport_identifiers ids;
  1767. struct srp_rport *rport;
  1768. sprintf(target->target_name, "SRP.T10:%016llX",
  1769. (unsigned long long) be64_to_cpu(target->id_ext));
  1770. if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
  1771. return -ENODEV;
  1772. memcpy(ids.port_id, &target->id_ext, 8);
  1773. memcpy(ids.port_id + 8, &target->ioc_guid, 8);
  1774. ids.roles = SRP_RPORT_ROLE_TARGET;
  1775. rport = srp_rport_add(target->scsi_host, &ids);
  1776. if (IS_ERR(rport)) {
  1777. scsi_remove_host(target->scsi_host);
  1778. return PTR_ERR(rport);
  1779. }
  1780. rport->lld_data = target;
  1781. target->rport = rport;
  1782. spin_lock(&host->target_lock);
  1783. list_add_tail(&target->list, &host->target_list);
  1784. spin_unlock(&host->target_lock);
  1785. target->state = SRP_TARGET_LIVE;
  1786. scsi_scan_target(&target->scsi_host->shost_gendev,
  1787. 0, target->scsi_id, SCAN_WILD_CARD, 0);
  1788. return 0;
  1789. }
  1790. static void srp_release_dev(struct device *dev)
  1791. {
  1792. struct srp_host *host =
  1793. container_of(dev, struct srp_host, dev);
  1794. complete(&host->released);
  1795. }
  1796. static struct class srp_class = {
  1797. .name = "infiniband_srp",
  1798. .dev_release = srp_release_dev
  1799. };
  1800. /**
  1801. * srp_conn_unique() - check whether the connection to a target is unique
  1802. */
  1803. static bool srp_conn_unique(struct srp_host *host,
  1804. struct srp_target_port *target)
  1805. {
  1806. struct srp_target_port *t;
  1807. bool ret = false;
  1808. if (target->state == SRP_TARGET_REMOVED)
  1809. goto out;
  1810. ret = true;
  1811. spin_lock(&host->target_lock);
  1812. list_for_each_entry(t, &host->target_list, list) {
  1813. if (t != target &&
  1814. target->id_ext == t->id_ext &&
  1815. target->ioc_guid == t->ioc_guid &&
  1816. target->initiator_ext == t->initiator_ext) {
  1817. ret = false;
  1818. break;
  1819. }
  1820. }
  1821. spin_unlock(&host->target_lock);
  1822. out:
  1823. return ret;
  1824. }
  1825. /*
  1826. * Target ports are added by writing
  1827. *
  1828. * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
  1829. * pkey=<P_Key>,service_id=<service ID>
  1830. *
  1831. * to the add_target sysfs attribute.
  1832. */
  1833. enum {
  1834. SRP_OPT_ERR = 0,
  1835. SRP_OPT_ID_EXT = 1 << 0,
  1836. SRP_OPT_IOC_GUID = 1 << 1,
  1837. SRP_OPT_DGID = 1 << 2,
  1838. SRP_OPT_PKEY = 1 << 3,
  1839. SRP_OPT_SERVICE_ID = 1 << 4,
  1840. SRP_OPT_MAX_SECT = 1 << 5,
  1841. SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
  1842. SRP_OPT_IO_CLASS = 1 << 7,
  1843. SRP_OPT_INITIATOR_EXT = 1 << 8,
  1844. SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
  1845. SRP_OPT_ALLOW_EXT_SG = 1 << 10,
  1846. SRP_OPT_SG_TABLESIZE = 1 << 11,
  1847. SRP_OPT_COMP_VECTOR = 1 << 12,
  1848. SRP_OPT_TL_RETRY_COUNT = 1 << 13,
  1849. SRP_OPT_ALL = (SRP_OPT_ID_EXT |
  1850. SRP_OPT_IOC_GUID |
  1851. SRP_OPT_DGID |
  1852. SRP_OPT_PKEY |
  1853. SRP_OPT_SERVICE_ID),
  1854. };
  1855. static const match_table_t srp_opt_tokens = {
  1856. { SRP_OPT_ID_EXT, "id_ext=%s" },
  1857. { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
  1858. { SRP_OPT_DGID, "dgid=%s" },
  1859. { SRP_OPT_PKEY, "pkey=%x" },
  1860. { SRP_OPT_SERVICE_ID, "service_id=%s" },
  1861. { SRP_OPT_MAX_SECT, "max_sect=%d" },
  1862. { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
  1863. { SRP_OPT_IO_CLASS, "io_class=%x" },
  1864. { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
  1865. { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
  1866. { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
  1867. { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
  1868. { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
  1869. { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
  1870. { SRP_OPT_ERR, NULL }
  1871. };
  1872. static int srp_parse_options(const char *buf, struct srp_target_port *target)
  1873. {
  1874. char *options, *sep_opt;
  1875. char *p;
  1876. char dgid[3];
  1877. substring_t args[MAX_OPT_ARGS];
  1878. int opt_mask = 0;
  1879. int token;
  1880. int ret = -EINVAL;
  1881. int i;
  1882. options = kstrdup(buf, GFP_KERNEL);
  1883. if (!options)
  1884. return -ENOMEM;
  1885. sep_opt = options;
  1886. while ((p = strsep(&sep_opt, ",")) != NULL) {
  1887. if (!*p)
  1888. continue;
  1889. token = match_token(p, srp_opt_tokens, args);
  1890. opt_mask |= token;
  1891. switch (token) {
  1892. case SRP_OPT_ID_EXT:
  1893. p = match_strdup(args);
  1894. if (!p) {
  1895. ret = -ENOMEM;
  1896. goto out;
  1897. }
  1898. target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1899. kfree(p);
  1900. break;
  1901. case SRP_OPT_IOC_GUID:
  1902. p = match_strdup(args);
  1903. if (!p) {
  1904. ret = -ENOMEM;
  1905. goto out;
  1906. }
  1907. target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1908. kfree(p);
  1909. break;
  1910. case SRP_OPT_DGID:
  1911. p = match_strdup(args);
  1912. if (!p) {
  1913. ret = -ENOMEM;
  1914. goto out;
  1915. }
  1916. if (strlen(p) != 32) {
  1917. pr_warn("bad dest GID parameter '%s'\n", p);
  1918. kfree(p);
  1919. goto out;
  1920. }
  1921. for (i = 0; i < 16; ++i) {
  1922. strlcpy(dgid, p + i * 2, 3);
  1923. target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
  1924. }
  1925. kfree(p);
  1926. memcpy(target->orig_dgid, target->path.dgid.raw, 16);
  1927. break;
  1928. case SRP_OPT_PKEY:
  1929. if (match_hex(args, &token)) {
  1930. pr_warn("bad P_Key parameter '%s'\n", p);
  1931. goto out;
  1932. }
  1933. target->path.pkey = cpu_to_be16(token);
  1934. break;
  1935. case SRP_OPT_SERVICE_ID:
  1936. p = match_strdup(args);
  1937. if (!p) {
  1938. ret = -ENOMEM;
  1939. goto out;
  1940. }
  1941. target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1942. target->path.service_id = target->service_id;
  1943. kfree(p);
  1944. break;
  1945. case SRP_OPT_MAX_SECT:
  1946. if (match_int(args, &token)) {
  1947. pr_warn("bad max sect parameter '%s'\n", p);
  1948. goto out;
  1949. }
  1950. target->scsi_host->max_sectors = token;
  1951. break;
  1952. case SRP_OPT_MAX_CMD_PER_LUN:
  1953. if (match_int(args, &token)) {
  1954. pr_warn("bad max cmd_per_lun parameter '%s'\n",
  1955. p);
  1956. goto out;
  1957. }
  1958. target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE);
  1959. break;
  1960. case SRP_OPT_IO_CLASS:
  1961. if (match_hex(args, &token)) {
  1962. pr_warn("bad IO class parameter '%s'\n", p);
  1963. goto out;
  1964. }
  1965. if (token != SRP_REV10_IB_IO_CLASS &&
  1966. token != SRP_REV16A_IB_IO_CLASS) {
  1967. pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
  1968. token, SRP_REV10_IB_IO_CLASS,
  1969. SRP_REV16A_IB_IO_CLASS);
  1970. goto out;
  1971. }
  1972. target->io_class = token;
  1973. break;
  1974. case SRP_OPT_INITIATOR_EXT:
  1975. p = match_strdup(args);
  1976. if (!p) {
  1977. ret = -ENOMEM;
  1978. goto out;
  1979. }
  1980. target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1981. kfree(p);
  1982. break;
  1983. case SRP_OPT_CMD_SG_ENTRIES:
  1984. if (match_int(args, &token) || token < 1 || token > 255) {
  1985. pr_warn("bad max cmd_sg_entries parameter '%s'\n",
  1986. p);
  1987. goto out;
  1988. }
  1989. target->cmd_sg_cnt = token;
  1990. break;
  1991. case SRP_OPT_ALLOW_EXT_SG:
  1992. if (match_int(args, &token)) {
  1993. pr_warn("bad allow_ext_sg parameter '%s'\n", p);
  1994. goto out;
  1995. }
  1996. target->allow_ext_sg = !!token;
  1997. break;
  1998. case SRP_OPT_SG_TABLESIZE:
  1999. if (match_int(args, &token) || token < 1 ||
  2000. token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
  2001. pr_warn("bad max sg_tablesize parameter '%s'\n",
  2002. p);
  2003. goto out;
  2004. }
  2005. target->sg_tablesize = token;
  2006. break;
  2007. case SRP_OPT_COMP_VECTOR:
  2008. if (match_int(args, &token) || token < 0) {
  2009. pr_warn("bad comp_vector parameter '%s'\n", p);
  2010. goto out;
  2011. }
  2012. target->comp_vector = token;
  2013. break;
  2014. case SRP_OPT_TL_RETRY_COUNT:
  2015. if (match_int(args, &token) || token < 2 || token > 7) {
  2016. pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
  2017. p);
  2018. goto out;
  2019. }
  2020. target->tl_retry_count = token;
  2021. break;
  2022. default:
  2023. pr_warn("unknown parameter or missing value '%s' in target creation request\n",
  2024. p);
  2025. goto out;
  2026. }
  2027. }
  2028. if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
  2029. ret = 0;
  2030. else
  2031. for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
  2032. if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
  2033. !(srp_opt_tokens[i].token & opt_mask))
  2034. pr_warn("target creation request is missing parameter '%s'\n",
  2035. srp_opt_tokens[i].pattern);
  2036. out:
  2037. kfree(options);
  2038. return ret;
  2039. }
  2040. static ssize_t srp_create_target(struct device *dev,
  2041. struct device_attribute *attr,
  2042. const char *buf, size_t count)
  2043. {
  2044. struct srp_host *host =
  2045. container_of(dev, struct srp_host, dev);
  2046. struct Scsi_Host *target_host;
  2047. struct srp_target_port *target;
  2048. struct ib_device *ibdev = host->srp_dev->dev;
  2049. int ret;
  2050. target_host = scsi_host_alloc(&srp_template,
  2051. sizeof (struct srp_target_port));
  2052. if (!target_host)
  2053. return -ENOMEM;
  2054. target_host->transportt = ib_srp_transport_template;
  2055. target_host->max_channel = 0;
  2056. target_host->max_id = 1;
  2057. target_host->max_lun = SRP_MAX_LUN;
  2058. target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
  2059. target = host_to_target(target_host);
  2060. target->io_class = SRP_REV16A_IB_IO_CLASS;
  2061. target->scsi_host = target_host;
  2062. target->srp_host = host;
  2063. target->lkey = host->srp_dev->mr->lkey;
  2064. target->rkey = host->srp_dev->mr->rkey;
  2065. target->cmd_sg_cnt = cmd_sg_entries;
  2066. target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
  2067. target->allow_ext_sg = allow_ext_sg;
  2068. target->tl_retry_count = 7;
  2069. ret = srp_parse_options(buf, target);
  2070. if (ret)
  2071. goto err;
  2072. if (!srp_conn_unique(target->srp_host, target)) {
  2073. shost_printk(KERN_INFO, target->scsi_host,
  2074. PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
  2075. be64_to_cpu(target->id_ext),
  2076. be64_to_cpu(target->ioc_guid),
  2077. be64_to_cpu(target->initiator_ext));
  2078. ret = -EEXIST;
  2079. goto err;
  2080. }
  2081. if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
  2082. target->cmd_sg_cnt < target->sg_tablesize) {
  2083. pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
  2084. target->sg_tablesize = target->cmd_sg_cnt;
  2085. }
  2086. target_host->sg_tablesize = target->sg_tablesize;
  2087. target->indirect_size = target->sg_tablesize *
  2088. sizeof (struct srp_direct_buf);
  2089. target->max_iu_len = sizeof (struct srp_cmd) +
  2090. sizeof (struct srp_indirect_buf) +
  2091. target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
  2092. INIT_WORK(&target->tl_err_work, srp_tl_err_work);
  2093. INIT_WORK(&target->remove_work, srp_remove_work);
  2094. spin_lock_init(&target->lock);
  2095. INIT_LIST_HEAD(&target->free_tx);
  2096. ret = srp_alloc_req_data(target);
  2097. if (ret)
  2098. goto err_free_mem;
  2099. ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
  2100. shost_printk(KERN_DEBUG, target->scsi_host, PFX
  2101. "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
  2102. "service_id %016llx dgid %pI6\n",
  2103. (unsigned long long) be64_to_cpu(target->id_ext),
  2104. (unsigned long long) be64_to_cpu(target->ioc_guid),
  2105. be16_to_cpu(target->path.pkey),
  2106. (unsigned long long) be64_to_cpu(target->service_id),
  2107. target->path.dgid.raw);
  2108. ret = srp_create_target_ib(target);
  2109. if (ret)
  2110. goto err_free_mem;
  2111. ret = srp_new_cm_id(target);
  2112. if (ret)
  2113. goto err_free_ib;
  2114. ret = srp_connect_target(target);
  2115. if (ret) {
  2116. shost_printk(KERN_ERR, target->scsi_host,
  2117. PFX "Connection failed\n");
  2118. goto err_cm_id;
  2119. }
  2120. ret = srp_add_target(host, target);
  2121. if (ret)
  2122. goto err_disconnect;
  2123. return count;
  2124. err_disconnect:
  2125. srp_disconnect_target(target);
  2126. err_cm_id:
  2127. ib_destroy_cm_id(target->cm_id);
  2128. err_free_ib:
  2129. srp_free_target_ib(target);
  2130. err_free_mem:
  2131. srp_free_req_data(target);
  2132. err:
  2133. scsi_host_put(target_host);
  2134. return ret;
  2135. }
  2136. static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
  2137. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  2138. char *buf)
  2139. {
  2140. struct srp_host *host = container_of(dev, struct srp_host, dev);
  2141. return sprintf(buf, "%s\n", host->srp_dev->dev->name);
  2142. }
  2143. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  2144. static ssize_t show_port(struct device *dev, struct device_attribute *attr,
  2145. char *buf)
  2146. {
  2147. struct srp_host *host = container_of(dev, struct srp_host, dev);
  2148. return sprintf(buf, "%d\n", host->port);
  2149. }
  2150. static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  2151. static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
  2152. {
  2153. struct srp_host *host;
  2154. host = kzalloc(sizeof *host, GFP_KERNEL);
  2155. if (!host)
  2156. return NULL;
  2157. INIT_LIST_HEAD(&host->target_list);
  2158. spin_lock_init(&host->target_lock);
  2159. init_completion(&host->released);
  2160. host->srp_dev = device;
  2161. host->port = port;
  2162. host->dev.class = &srp_class;
  2163. host->dev.parent = device->dev->dma_device;
  2164. dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
  2165. if (device_register(&host->dev))
  2166. goto free_host;
  2167. if (device_create_file(&host->dev, &dev_attr_add_target))
  2168. goto err_class;
  2169. if (device_create_file(&host->dev, &dev_attr_ibdev))
  2170. goto err_class;
  2171. if (device_create_file(&host->dev, &dev_attr_port))
  2172. goto err_class;
  2173. return host;
  2174. err_class:
  2175. device_unregister(&host->dev);
  2176. free_host:
  2177. kfree(host);
  2178. return NULL;
  2179. }
  2180. static void srp_add_one(struct ib_device *device)
  2181. {
  2182. struct srp_device *srp_dev;
  2183. struct ib_device_attr *dev_attr;
  2184. struct ib_fmr_pool_param fmr_param;
  2185. struct srp_host *host;
  2186. int max_pages_per_fmr, fmr_page_shift, s, e, p;
  2187. dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
  2188. if (!dev_attr)
  2189. return;
  2190. if (ib_query_device(device, dev_attr)) {
  2191. pr_warn("Query device failed for %s\n", device->name);
  2192. goto free_attr;
  2193. }
  2194. srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
  2195. if (!srp_dev)
  2196. goto free_attr;
  2197. /*
  2198. * Use the smallest page size supported by the HCA, down to a
  2199. * minimum of 4096 bytes. We're unlikely to build large sglists
  2200. * out of smaller entries.
  2201. */
  2202. fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
  2203. srp_dev->fmr_page_size = 1 << fmr_page_shift;
  2204. srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
  2205. srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
  2206. INIT_LIST_HEAD(&srp_dev->dev_list);
  2207. srp_dev->dev = device;
  2208. srp_dev->pd = ib_alloc_pd(device);
  2209. if (IS_ERR(srp_dev->pd))
  2210. goto free_dev;
  2211. srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
  2212. IB_ACCESS_LOCAL_WRITE |
  2213. IB_ACCESS_REMOTE_READ |
  2214. IB_ACCESS_REMOTE_WRITE);
  2215. if (IS_ERR(srp_dev->mr))
  2216. goto err_pd;
  2217. for (max_pages_per_fmr = SRP_FMR_SIZE;
  2218. max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
  2219. max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
  2220. memset(&fmr_param, 0, sizeof fmr_param);
  2221. fmr_param.pool_size = SRP_FMR_POOL_SIZE;
  2222. fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
  2223. fmr_param.cache = 1;
  2224. fmr_param.max_pages_per_fmr = max_pages_per_fmr;
  2225. fmr_param.page_shift = fmr_page_shift;
  2226. fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
  2227. IB_ACCESS_REMOTE_WRITE |
  2228. IB_ACCESS_REMOTE_READ);
  2229. srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
  2230. if (!IS_ERR(srp_dev->fmr_pool))
  2231. break;
  2232. }
  2233. if (IS_ERR(srp_dev->fmr_pool))
  2234. srp_dev->fmr_pool = NULL;
  2235. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2236. s = 0;
  2237. e = 0;
  2238. } else {
  2239. s = 1;
  2240. e = device->phys_port_cnt;
  2241. }
  2242. for (p = s; p <= e; ++p) {
  2243. host = srp_add_port(srp_dev, p);
  2244. if (host)
  2245. list_add_tail(&host->list, &srp_dev->dev_list);
  2246. }
  2247. ib_set_client_data(device, &srp_client, srp_dev);
  2248. goto free_attr;
  2249. err_pd:
  2250. ib_dealloc_pd(srp_dev->pd);
  2251. free_dev:
  2252. kfree(srp_dev);
  2253. free_attr:
  2254. kfree(dev_attr);
  2255. }
  2256. static void srp_remove_one(struct ib_device *device)
  2257. {
  2258. struct srp_device *srp_dev;
  2259. struct srp_host *host, *tmp_host;
  2260. struct srp_target_port *target;
  2261. srp_dev = ib_get_client_data(device, &srp_client);
  2262. if (!srp_dev)
  2263. return;
  2264. list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
  2265. device_unregister(&host->dev);
  2266. /*
  2267. * Wait for the sysfs entry to go away, so that no new
  2268. * target ports can be created.
  2269. */
  2270. wait_for_completion(&host->released);
  2271. /*
  2272. * Remove all target ports.
  2273. */
  2274. spin_lock(&host->target_lock);
  2275. list_for_each_entry(target, &host->target_list, list)
  2276. srp_queue_remove_work(target);
  2277. spin_unlock(&host->target_lock);
  2278. /*
  2279. * Wait for target port removal tasks.
  2280. */
  2281. flush_workqueue(system_long_wq);
  2282. kfree(host);
  2283. }
  2284. if (srp_dev->fmr_pool)
  2285. ib_destroy_fmr_pool(srp_dev->fmr_pool);
  2286. ib_dereg_mr(srp_dev->mr);
  2287. ib_dealloc_pd(srp_dev->pd);
  2288. kfree(srp_dev);
  2289. }
  2290. static struct srp_function_template ib_srp_transport_functions = {
  2291. .has_rport_state = true,
  2292. .reset_timer_if_blocked = true,
  2293. .reconnect_delay = &srp_reconnect_delay,
  2294. .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
  2295. .dev_loss_tmo = &srp_dev_loss_tmo,
  2296. .reconnect = srp_rport_reconnect,
  2297. .rport_delete = srp_rport_delete,
  2298. .terminate_rport_io = srp_terminate_io,
  2299. };
  2300. static int __init srp_init_module(void)
  2301. {
  2302. int ret;
  2303. BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
  2304. if (srp_sg_tablesize) {
  2305. pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
  2306. if (!cmd_sg_entries)
  2307. cmd_sg_entries = srp_sg_tablesize;
  2308. }
  2309. if (!cmd_sg_entries)
  2310. cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
  2311. if (cmd_sg_entries > 255) {
  2312. pr_warn("Clamping cmd_sg_entries to 255\n");
  2313. cmd_sg_entries = 255;
  2314. }
  2315. if (!indirect_sg_entries)
  2316. indirect_sg_entries = cmd_sg_entries;
  2317. else if (indirect_sg_entries < cmd_sg_entries) {
  2318. pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
  2319. cmd_sg_entries);
  2320. indirect_sg_entries = cmd_sg_entries;
  2321. }
  2322. ib_srp_transport_template =
  2323. srp_attach_transport(&ib_srp_transport_functions);
  2324. if (!ib_srp_transport_template)
  2325. return -ENOMEM;
  2326. ret = class_register(&srp_class);
  2327. if (ret) {
  2328. pr_err("couldn't register class infiniband_srp\n");
  2329. srp_release_transport(ib_srp_transport_template);
  2330. return ret;
  2331. }
  2332. ib_sa_register_client(&srp_sa_client);
  2333. ret = ib_register_client(&srp_client);
  2334. if (ret) {
  2335. pr_err("couldn't register IB client\n");
  2336. srp_release_transport(ib_srp_transport_template);
  2337. ib_sa_unregister_client(&srp_sa_client);
  2338. class_unregister(&srp_class);
  2339. return ret;
  2340. }
  2341. return 0;
  2342. }
  2343. static void __exit srp_cleanup_module(void)
  2344. {
  2345. ib_unregister_client(&srp_client);
  2346. ib_sa_unregister_client(&srp_sa_client);
  2347. class_unregister(&srp_class);
  2348. srp_release_transport(ib_srp_transport_template);
  2349. }
  2350. module_init(srp_init_module);
  2351. module_exit(srp_cleanup_module);