ib_srp.c 65 KB

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