tcm_qla2xxx.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /*******************************************************************************
  2. * This file contains tcm implementation using v4 configfs fabric infrastructure
  3. * for QLogic target mode HBAs
  4. *
  5. * ?? Copyright 2010-2011 RisingTide Systems LLC.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL)
  8. * version 2.
  9. *
  10. * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
  11. *
  12. * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
  13. * the TCM_FC / Open-FCoE.org fabric module.
  14. *
  15. * Copyright (c) 2010 Cisco Systems, Inc
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. ****************************************************************************/
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <generated/utsrelease.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <linux/kthread.h>
  35. #include <linux/types.h>
  36. #include <linux/string.h>
  37. #include <linux/configfs.h>
  38. #include <linux/ctype.h>
  39. #include <asm/unaligned.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <target/target_core_base.h>
  45. #include <target/target_core_fabric.h>
  46. #include <target/target_core_fabric_configfs.h>
  47. #include <target/target_core_configfs.h>
  48. #include <target/configfs_macros.h>
  49. #include "qla_def.h"
  50. #include "qla_target.h"
  51. #include "tcm_qla2xxx.h"
  52. struct workqueue_struct *tcm_qla2xxx_free_wq;
  53. struct workqueue_struct *tcm_qla2xxx_cmd_wq;
  54. static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
  55. {
  56. return 1;
  57. }
  58. static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
  59. {
  60. return 0;
  61. }
  62. /*
  63. * Parse WWN.
  64. * If strict, we require lower-case hex and colon separators to be sure
  65. * the name is the same as what would be generated by ft_format_wwn()
  66. * so the name and wwn are mapped one-to-one.
  67. */
  68. static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
  69. {
  70. const char *cp;
  71. char c;
  72. u32 nibble;
  73. u32 byte = 0;
  74. u32 pos = 0;
  75. u32 err;
  76. *wwn = 0;
  77. for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
  78. c = *cp;
  79. if (c == '\n' && cp[1] == '\0')
  80. continue;
  81. if (strict && pos++ == 2 && byte++ < 7) {
  82. pos = 0;
  83. if (c == ':')
  84. continue;
  85. err = 1;
  86. goto fail;
  87. }
  88. if (c == '\0') {
  89. err = 2;
  90. if (strict && byte != 8)
  91. goto fail;
  92. return cp - name;
  93. }
  94. err = 3;
  95. if (isdigit(c))
  96. nibble = c - '0';
  97. else if (isxdigit(c) && (islower(c) || !strict))
  98. nibble = tolower(c) - 'a' + 10;
  99. else
  100. goto fail;
  101. *wwn = (*wwn << 4) | nibble;
  102. }
  103. err = 4;
  104. fail:
  105. pr_debug("err %u len %zu pos %u byte %u\n",
  106. err, cp - name, pos, byte);
  107. return -1;
  108. }
  109. static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
  110. {
  111. u8 b[8];
  112. put_unaligned_be64(wwn, b);
  113. return snprintf(buf, len,
  114. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  115. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
  116. }
  117. static char *tcm_qla2xxx_get_fabric_name(void)
  118. {
  119. return "qla2xxx";
  120. }
  121. /*
  122. * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
  123. */
  124. static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
  125. {
  126. unsigned int i, j;
  127. u8 wwn[8];
  128. memset(wwn, 0, sizeof(wwn));
  129. /* Validate and store the new name */
  130. for (i = 0, j = 0; i < 16; i++) {
  131. int value;
  132. value = hex_to_bin(*ns++);
  133. if (value >= 0)
  134. j = (j << 4) | value;
  135. else
  136. return -EINVAL;
  137. if (i % 2) {
  138. wwn[i/2] = j & 0xff;
  139. j = 0;
  140. }
  141. }
  142. *nm = wwn_to_u64(wwn);
  143. return 0;
  144. }
  145. /*
  146. * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
  147. * store_fc_host_vport_create()
  148. */
  149. static int tcm_qla2xxx_npiv_parse_wwn(
  150. const char *name,
  151. size_t count,
  152. u64 *wwpn,
  153. u64 *wwnn)
  154. {
  155. unsigned int cnt = count;
  156. int rc;
  157. *wwpn = 0;
  158. *wwnn = 0;
  159. /* count may include a LF at end of string */
  160. if (name[cnt-1] == '\n')
  161. cnt--;
  162. /* validate we have enough characters for WWPN */
  163. if ((cnt != (16+1+16)) || (name[16] != ':'))
  164. return -EINVAL;
  165. rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
  166. if (rc != 0)
  167. return rc;
  168. rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
  169. if (rc != 0)
  170. return rc;
  171. return 0;
  172. }
  173. static ssize_t tcm_qla2xxx_npiv_format_wwn(char *buf, size_t len,
  174. u64 wwpn, u64 wwnn)
  175. {
  176. u8 b[8], b2[8];
  177. put_unaligned_be64(wwpn, b);
  178. put_unaligned_be64(wwnn, b2);
  179. return snprintf(buf, len,
  180. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x,"
  181. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  182. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
  183. b2[0], b2[1], b2[2], b2[3], b2[4], b2[5], b2[6], b2[7]);
  184. }
  185. static char *tcm_qla2xxx_npiv_get_fabric_name(void)
  186. {
  187. return "qla2xxx_npiv";
  188. }
  189. static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  190. {
  191. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  192. struct tcm_qla2xxx_tpg, se_tpg);
  193. struct tcm_qla2xxx_lport *lport = tpg->lport;
  194. u8 proto_id;
  195. switch (lport->lport_proto_id) {
  196. case SCSI_PROTOCOL_FCP:
  197. default:
  198. proto_id = fc_get_fabric_proto_ident(se_tpg);
  199. break;
  200. }
  201. return proto_id;
  202. }
  203. static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
  204. {
  205. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  206. struct tcm_qla2xxx_tpg, se_tpg);
  207. struct tcm_qla2xxx_lport *lport = tpg->lport;
  208. return lport->lport_naa_name;
  209. }
  210. static char *tcm_qla2xxx_npiv_get_fabric_wwn(struct se_portal_group *se_tpg)
  211. {
  212. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  213. struct tcm_qla2xxx_tpg, se_tpg);
  214. struct tcm_qla2xxx_lport *lport = tpg->lport;
  215. return &lport->lport_npiv_name[0];
  216. }
  217. static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
  218. {
  219. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  220. struct tcm_qla2xxx_tpg, se_tpg);
  221. return tpg->lport_tpgt;
  222. }
  223. static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
  224. {
  225. return 1;
  226. }
  227. static u32 tcm_qla2xxx_get_pr_transport_id(
  228. struct se_portal_group *se_tpg,
  229. struct se_node_acl *se_nacl,
  230. struct t10_pr_registration *pr_reg,
  231. int *format_code,
  232. unsigned char *buf)
  233. {
  234. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  235. struct tcm_qla2xxx_tpg, se_tpg);
  236. struct tcm_qla2xxx_lport *lport = tpg->lport;
  237. int ret = 0;
  238. switch (lport->lport_proto_id) {
  239. case SCSI_PROTOCOL_FCP:
  240. default:
  241. ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  242. format_code, buf);
  243. break;
  244. }
  245. return ret;
  246. }
  247. static u32 tcm_qla2xxx_get_pr_transport_id_len(
  248. struct se_portal_group *se_tpg,
  249. struct se_node_acl *se_nacl,
  250. struct t10_pr_registration *pr_reg,
  251. int *format_code)
  252. {
  253. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  254. struct tcm_qla2xxx_tpg, se_tpg);
  255. struct tcm_qla2xxx_lport *lport = tpg->lport;
  256. int ret = 0;
  257. switch (lport->lport_proto_id) {
  258. case SCSI_PROTOCOL_FCP:
  259. default:
  260. ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  261. format_code);
  262. break;
  263. }
  264. return ret;
  265. }
  266. static char *tcm_qla2xxx_parse_pr_out_transport_id(
  267. struct se_portal_group *se_tpg,
  268. const char *buf,
  269. u32 *out_tid_len,
  270. char **port_nexus_ptr)
  271. {
  272. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  273. struct tcm_qla2xxx_tpg, se_tpg);
  274. struct tcm_qla2xxx_lport *lport = tpg->lport;
  275. char *tid = NULL;
  276. switch (lport->lport_proto_id) {
  277. case SCSI_PROTOCOL_FCP:
  278. default:
  279. tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  280. port_nexus_ptr);
  281. break;
  282. }
  283. return tid;
  284. }
  285. static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
  286. {
  287. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  288. struct tcm_qla2xxx_tpg, se_tpg);
  289. return QLA_TPG_ATTRIB(tpg)->generate_node_acls;
  290. }
  291. static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
  292. {
  293. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  294. struct tcm_qla2xxx_tpg, se_tpg);
  295. return QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls;
  296. }
  297. static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
  298. {
  299. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  300. struct tcm_qla2xxx_tpg, se_tpg);
  301. return QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect;
  302. }
  303. static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
  304. {
  305. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  306. struct tcm_qla2xxx_tpg, se_tpg);
  307. return QLA_TPG_ATTRIB(tpg)->prod_mode_write_protect;
  308. }
  309. static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
  310. struct se_portal_group *se_tpg)
  311. {
  312. struct tcm_qla2xxx_nacl *nacl;
  313. nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
  314. if (!nacl) {
  315. pr_err("Unable to allocate struct tcm_qla2xxx_nacl\n");
  316. return NULL;
  317. }
  318. return &nacl->se_node_acl;
  319. }
  320. static void tcm_qla2xxx_release_fabric_acl(
  321. struct se_portal_group *se_tpg,
  322. struct se_node_acl *se_nacl)
  323. {
  324. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  325. struct tcm_qla2xxx_nacl, se_node_acl);
  326. kfree(nacl);
  327. }
  328. static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
  329. {
  330. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  331. struct tcm_qla2xxx_tpg, se_tpg);
  332. return tpg->lport_tpgt;
  333. }
  334. static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
  335. {
  336. struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
  337. struct qla_tgt_mgmt_cmd, free_work);
  338. transport_generic_free_cmd(&mcmd->se_cmd, 0);
  339. }
  340. /*
  341. * Called from qla_target_template->free_mcmd(), and will call
  342. * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
  343. * release callback. qla_hw_data->hardware_lock is expected to be held
  344. */
  345. static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
  346. {
  347. INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
  348. queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
  349. }
  350. static void tcm_qla2xxx_complete_free(struct work_struct *work)
  351. {
  352. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  353. transport_generic_free_cmd(&cmd->se_cmd, 0);
  354. }
  355. /*
  356. * Called from qla_target_template->free_cmd(), and will call
  357. * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
  358. * release callback. qla_hw_data->hardware_lock is expected to be held
  359. */
  360. static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
  361. {
  362. INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
  363. queue_work(tcm_qla2xxx_free_wq, &cmd->work);
  364. }
  365. /*
  366. * Called from struct target_core_fabric_ops->check_stop_free() context
  367. */
  368. static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
  369. {
  370. return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  371. }
  372. /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
  373. * fabric descriptor @se_cmd command to release
  374. */
  375. static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
  376. {
  377. struct qla_tgt_cmd *cmd;
  378. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
  379. struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
  380. struct qla_tgt_mgmt_cmd, se_cmd);
  381. qlt_free_mcmd(mcmd);
  382. return;
  383. }
  384. cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
  385. qlt_free_cmd(cmd);
  386. }
  387. static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
  388. {
  389. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  390. struct scsi_qla_host *vha;
  391. unsigned long flags;
  392. BUG_ON(!sess);
  393. vha = sess->vha;
  394. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  395. target_sess_cmd_list_set_waiting(se_sess);
  396. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  397. return 1;
  398. }
  399. static void tcm_qla2xxx_close_session(struct se_session *se_sess)
  400. {
  401. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  402. struct scsi_qla_host *vha;
  403. unsigned long flags;
  404. BUG_ON(!sess);
  405. vha = sess->vha;
  406. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  407. qlt_unreg_sess(sess);
  408. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  409. }
  410. static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
  411. {
  412. return 0;
  413. }
  414. /*
  415. * The LIO target core uses DMA_TO_DEVICE to mean that data is going
  416. * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
  417. * that data is coming from the target (eg handling a READ). However,
  418. * this is just the opposite of what we have to tell the DMA mapping
  419. * layer -- eg when handling a READ, the HBA will have to DMA the data
  420. * out of memory so it can send it to the initiator, which means we
  421. * need to use DMA_TO_DEVICE when we map the data.
  422. */
  423. static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
  424. {
  425. if (se_cmd->se_cmd_flags & SCF_BIDI)
  426. return DMA_BIDIRECTIONAL;
  427. switch (se_cmd->data_direction) {
  428. case DMA_TO_DEVICE:
  429. return DMA_FROM_DEVICE;
  430. case DMA_FROM_DEVICE:
  431. return DMA_TO_DEVICE;
  432. case DMA_NONE:
  433. default:
  434. return DMA_NONE;
  435. }
  436. }
  437. static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
  438. {
  439. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  440. struct qla_tgt_cmd, se_cmd);
  441. cmd->bufflen = se_cmd->data_length;
  442. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  443. cmd->sg_cnt = se_cmd->t_data_nents;
  444. cmd->sg = se_cmd->t_data_sg;
  445. /*
  446. * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
  447. * the SGL mappings into PCIe memory for incoming FCP WRITE data.
  448. */
  449. return qlt_rdy_to_xfer(cmd);
  450. }
  451. static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
  452. {
  453. unsigned long flags;
  454. /*
  455. * Check for WRITE_PENDING status to determine if we need to wait for
  456. * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
  457. */
  458. spin_lock_irqsave(&se_cmd->t_state_lock, flags);
  459. if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
  460. se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
  461. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  462. wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
  463. 3000);
  464. return 0;
  465. }
  466. spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
  467. return 0;
  468. }
  469. static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
  470. {
  471. return;
  472. }
  473. static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
  474. {
  475. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  476. struct qla_tgt_cmd, se_cmd);
  477. return cmd->tag;
  478. }
  479. static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
  480. {
  481. return 0;
  482. }
  483. /*
  484. * Called from process context in qla_target.c:qlt_do_work() code
  485. */
  486. static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
  487. unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
  488. int data_dir, int bidi)
  489. {
  490. struct se_cmd *se_cmd = &cmd->se_cmd;
  491. struct se_session *se_sess;
  492. struct qla_tgt_sess *sess;
  493. int flags = TARGET_SCF_ACK_KREF;
  494. if (bidi)
  495. flags |= TARGET_SCF_BIDI_OP;
  496. sess = cmd->sess;
  497. if (!sess) {
  498. pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
  499. return -EINVAL;
  500. }
  501. se_sess = sess->se_sess;
  502. if (!se_sess) {
  503. pr_err("Unable to locate active struct se_session\n");
  504. return -EINVAL;
  505. }
  506. return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
  507. cmd->unpacked_lun, data_length, fcp_task_attr,
  508. data_dir, flags);
  509. }
  510. static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
  511. {
  512. struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
  513. /*
  514. * Ensure that the complete FCP WRITE payload has been received.
  515. * Otherwise return an exception via CHECK_CONDITION status.
  516. */
  517. if (!cmd->write_data_transferred) {
  518. /*
  519. * Check if se_cmd has already been aborted via LUN_RESET, and
  520. * waiting upon completion in tcm_qla2xxx_write_pending_status()
  521. */
  522. if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
  523. complete(&cmd->se_cmd.t_transport_stop_comp);
  524. return;
  525. }
  526. transport_generic_request_failure(&cmd->se_cmd,
  527. TCM_CHECK_CONDITION_ABORT_CMD);
  528. return;
  529. }
  530. return target_execute_cmd(&cmd->se_cmd);
  531. }
  532. /*
  533. * Called from qla_target.c:qlt_do_ctio_completion()
  534. */
  535. static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
  536. {
  537. INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
  538. queue_work(tcm_qla2xxx_free_wq, &cmd->work);
  539. }
  540. /*
  541. * Called from qla_target.c:qlt_issue_task_mgmt()
  542. */
  543. static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
  544. uint8_t tmr_func, uint32_t tag)
  545. {
  546. struct qla_tgt_sess *sess = mcmd->sess;
  547. struct se_cmd *se_cmd = &mcmd->se_cmd;
  548. return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
  549. tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
  550. }
  551. static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
  552. {
  553. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  554. struct qla_tgt_cmd, se_cmd);
  555. cmd->bufflen = se_cmd->data_length;
  556. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  557. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  558. cmd->sg_cnt = se_cmd->t_data_nents;
  559. cmd->sg = se_cmd->t_data_sg;
  560. cmd->offset = 0;
  561. /*
  562. * Now queue completed DATA_IN the qla2xxx LLD and response ring
  563. */
  564. return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
  565. se_cmd->scsi_status);
  566. }
  567. static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
  568. {
  569. struct qla_tgt_cmd *cmd = container_of(se_cmd,
  570. struct qla_tgt_cmd, se_cmd);
  571. int xmit_type = QLA_TGT_XMIT_STATUS;
  572. cmd->bufflen = se_cmd->data_length;
  573. cmd->sg = NULL;
  574. cmd->sg_cnt = 0;
  575. cmd->offset = 0;
  576. cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
  577. cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
  578. if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  579. /*
  580. * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
  581. * for qla_tgt_xmit_response LLD code
  582. */
  583. if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  584. se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
  585. se_cmd->residual_count = 0;
  586. }
  587. se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
  588. se_cmd->residual_count += se_cmd->data_length;
  589. cmd->bufflen = 0;
  590. }
  591. /*
  592. * Now queue status response to qla2xxx LLD code and response ring
  593. */
  594. return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
  595. }
  596. static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
  597. {
  598. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  599. struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
  600. struct qla_tgt_mgmt_cmd, se_cmd);
  601. pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
  602. mcmd, se_tmr->function, se_tmr->response);
  603. /*
  604. * Do translation between TCM TM response codes and
  605. * QLA2xxx FC TM response codes.
  606. */
  607. switch (se_tmr->response) {
  608. case TMR_FUNCTION_COMPLETE:
  609. mcmd->fc_tm_rsp = FC_TM_SUCCESS;
  610. break;
  611. case TMR_TASK_DOES_NOT_EXIST:
  612. mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
  613. break;
  614. case TMR_FUNCTION_REJECTED:
  615. mcmd->fc_tm_rsp = FC_TM_REJECT;
  616. break;
  617. case TMR_LUN_DOES_NOT_EXIST:
  618. default:
  619. mcmd->fc_tm_rsp = FC_TM_FAILED;
  620. break;
  621. }
  622. /*
  623. * Queue the TM response to QLA2xxx LLD to build a
  624. * CTIO response packet.
  625. */
  626. qlt_xmit_tm_rsp(mcmd);
  627. }
  628. /* Local pointer to allocated TCM configfs fabric module */
  629. struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
  630. struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
  631. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
  632. struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
  633. /*
  634. * Expected to be called with struct qla_hw_data->hardware_lock held
  635. */
  636. static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
  637. {
  638. struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
  639. struct se_portal_group *se_tpg = se_nacl->se_tpg;
  640. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  641. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  642. struct tcm_qla2xxx_lport, lport_wwn);
  643. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  644. struct tcm_qla2xxx_nacl, se_node_acl);
  645. void *node;
  646. pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
  647. node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
  648. WARN_ON(node && (node != se_nacl));
  649. pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
  650. se_nacl, nacl->nport_wwnn, nacl->nport_id);
  651. /*
  652. * Now clear the se_nacl and session pointers from our HW lport lookup
  653. * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
  654. *
  655. * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
  656. * target_wait_for_sess_cmds() before the session waits for outstanding
  657. * I/O to complete, to avoid a race between session shutdown execution
  658. * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
  659. */
  660. tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
  661. }
  662. static void tcm_qla2xxx_release_session(struct kref *kref)
  663. {
  664. struct se_session *se_sess = container_of(kref,
  665. struct se_session, sess_kref);
  666. qlt_unreg_sess(se_sess->fabric_sess_ptr);
  667. }
  668. static void tcm_qla2xxx_put_session(struct se_session *se_sess)
  669. {
  670. struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
  671. struct qla_hw_data *ha = sess->vha->hw;
  672. unsigned long flags;
  673. spin_lock_irqsave(&ha->hardware_lock, flags);
  674. kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
  675. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  676. }
  677. static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
  678. {
  679. assert_spin_locked(&sess->vha->hw->hardware_lock);
  680. kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
  681. }
  682. static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
  683. {
  684. assert_spin_locked(&sess->vha->hw->hardware_lock);
  685. target_sess_cmd_list_set_waiting(sess->se_sess);
  686. }
  687. static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
  688. struct se_portal_group *se_tpg,
  689. struct config_group *group,
  690. const char *name)
  691. {
  692. struct se_node_acl *se_nacl, *se_nacl_new;
  693. struct tcm_qla2xxx_nacl *nacl;
  694. u64 wwnn;
  695. u32 qla2xxx_nexus_depth;
  696. if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
  697. return ERR_PTR(-EINVAL);
  698. se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
  699. if (!se_nacl_new)
  700. return ERR_PTR(-ENOMEM);
  701. /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
  702. qla2xxx_nexus_depth = 1;
  703. /*
  704. * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
  705. * when converting a NodeACL from demo mode -> explict
  706. */
  707. se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
  708. name, qla2xxx_nexus_depth);
  709. if (IS_ERR(se_nacl)) {
  710. tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
  711. return se_nacl;
  712. }
  713. /*
  714. * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
  715. */
  716. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  717. nacl->nport_wwnn = wwnn;
  718. tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
  719. return se_nacl;
  720. }
  721. static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
  722. {
  723. struct se_portal_group *se_tpg = se_acl->se_tpg;
  724. struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
  725. struct tcm_qla2xxx_nacl, se_node_acl);
  726. core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
  727. kfree(nacl);
  728. }
  729. /* Start items for tcm_qla2xxx_tpg_attrib_cit */
  730. #define DEF_QLA_TPG_ATTRIB(name) \
  731. \
  732. static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
  733. struct se_portal_group *se_tpg, \
  734. char *page) \
  735. { \
  736. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  737. struct tcm_qla2xxx_tpg, se_tpg); \
  738. \
  739. return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
  740. } \
  741. \
  742. static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
  743. struct se_portal_group *se_tpg, \
  744. const char *page, \
  745. size_t count) \
  746. { \
  747. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
  748. struct tcm_qla2xxx_tpg, se_tpg); \
  749. unsigned long val; \
  750. int ret; \
  751. \
  752. ret = kstrtoul(page, 0, &val); \
  753. if (ret < 0) { \
  754. pr_err("kstrtoul() failed with" \
  755. " ret: %d\n", ret); \
  756. return -EINVAL; \
  757. } \
  758. ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
  759. \
  760. return (!ret) ? count : -EINVAL; \
  761. }
  762. #define DEF_QLA_TPG_ATTR_BOOL(_name) \
  763. \
  764. static int tcm_qla2xxx_set_attrib_##_name( \
  765. struct tcm_qla2xxx_tpg *tpg, \
  766. unsigned long val) \
  767. { \
  768. struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
  769. \
  770. if ((val != 0) && (val != 1)) { \
  771. pr_err("Illegal boolean value %lu\n", val); \
  772. return -EINVAL; \
  773. } \
  774. \
  775. a->_name = val; \
  776. return 0; \
  777. }
  778. #define QLA_TPG_ATTR(_name, _mode) \
  779. TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
  780. /*
  781. * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
  782. */
  783. DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
  784. DEF_QLA_TPG_ATTRIB(generate_node_acls);
  785. QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
  786. /*
  787. Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
  788. */
  789. DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
  790. DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
  791. QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
  792. /*
  793. * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
  794. */
  795. DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
  796. DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
  797. QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
  798. /*
  799. * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
  800. */
  801. DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
  802. DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
  803. QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
  804. static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
  805. &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
  806. &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
  807. &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
  808. &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
  809. NULL,
  810. };
  811. /* End items for tcm_qla2xxx_tpg_attrib_cit */
  812. static ssize_t tcm_qla2xxx_tpg_show_enable(
  813. struct se_portal_group *se_tpg,
  814. char *page)
  815. {
  816. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  817. struct tcm_qla2xxx_tpg, se_tpg);
  818. return snprintf(page, PAGE_SIZE, "%d\n",
  819. atomic_read(&tpg->lport_tpg_enabled));
  820. }
  821. static ssize_t tcm_qla2xxx_tpg_store_enable(
  822. struct se_portal_group *se_tpg,
  823. const char *page,
  824. size_t count)
  825. {
  826. struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
  827. struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
  828. struct tcm_qla2xxx_lport, lport_wwn);
  829. struct scsi_qla_host *vha = lport->qla_vha;
  830. struct qla_hw_data *ha = vha->hw;
  831. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  832. struct tcm_qla2xxx_tpg, se_tpg);
  833. unsigned long op;
  834. int rc;
  835. rc = kstrtoul(page, 0, &op);
  836. if (rc < 0) {
  837. pr_err("kstrtoul() returned %d\n", rc);
  838. return -EINVAL;
  839. }
  840. if ((op != 1) && (op != 0)) {
  841. pr_err("Illegal value for tpg_enable: %lu\n", op);
  842. return -EINVAL;
  843. }
  844. if (op) {
  845. atomic_set(&tpg->lport_tpg_enabled, 1);
  846. qlt_enable_vha(vha);
  847. } else {
  848. if (!ha->tgt.qla_tgt) {
  849. pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
  850. return -ENODEV;
  851. }
  852. atomic_set(&tpg->lport_tpg_enabled, 0);
  853. qlt_stop_phase1(ha->tgt.qla_tgt);
  854. }
  855. return count;
  856. }
  857. TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
  858. static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
  859. &tcm_qla2xxx_tpg_enable.attr,
  860. NULL,
  861. };
  862. static struct se_portal_group *tcm_qla2xxx_make_tpg(
  863. struct se_wwn *wwn,
  864. struct config_group *group,
  865. const char *name)
  866. {
  867. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  868. struct tcm_qla2xxx_lport, lport_wwn);
  869. struct tcm_qla2xxx_tpg *tpg;
  870. unsigned long tpgt;
  871. int ret;
  872. if (strstr(name, "tpgt_") != name)
  873. return ERR_PTR(-EINVAL);
  874. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  875. return ERR_PTR(-EINVAL);
  876. if (!lport->qla_npiv_vp && (tpgt != 1)) {
  877. pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
  878. return ERR_PTR(-ENOSYS);
  879. }
  880. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  881. if (!tpg) {
  882. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  883. return ERR_PTR(-ENOMEM);
  884. }
  885. tpg->lport = lport;
  886. tpg->lport_tpgt = tpgt;
  887. /*
  888. * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
  889. * NodeACLs
  890. */
  891. QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
  892. QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
  893. QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
  894. ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
  895. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  896. if (ret < 0) {
  897. kfree(tpg);
  898. return NULL;
  899. }
  900. /*
  901. * Setup local TPG=1 pointer for non NPIV mode.
  902. */
  903. if (lport->qla_npiv_vp == NULL)
  904. lport->tpg_1 = tpg;
  905. return &tpg->se_tpg;
  906. }
  907. static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
  908. {
  909. struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
  910. struct tcm_qla2xxx_tpg, se_tpg);
  911. struct tcm_qla2xxx_lport *lport = tpg->lport;
  912. struct scsi_qla_host *vha = lport->qla_vha;
  913. struct qla_hw_data *ha = vha->hw;
  914. /*
  915. * Call into qla2x_target.c LLD logic to shutdown the active
  916. * FC Nexuses and disable target mode operation for this qla_hw_data
  917. */
  918. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
  919. qlt_stop_phase1(ha->tgt.qla_tgt);
  920. core_tpg_deregister(se_tpg);
  921. /*
  922. * Clear local TPG=1 pointer for non NPIV mode.
  923. */
  924. if (lport->qla_npiv_vp == NULL)
  925. lport->tpg_1 = NULL;
  926. kfree(tpg);
  927. }
  928. static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
  929. struct se_wwn *wwn,
  930. struct config_group *group,
  931. const char *name)
  932. {
  933. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  934. struct tcm_qla2xxx_lport, lport_wwn);
  935. struct tcm_qla2xxx_tpg *tpg;
  936. unsigned long tpgt;
  937. int ret;
  938. if (strstr(name, "tpgt_") != name)
  939. return ERR_PTR(-EINVAL);
  940. if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
  941. return ERR_PTR(-EINVAL);
  942. tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
  943. if (!tpg) {
  944. pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
  945. return ERR_PTR(-ENOMEM);
  946. }
  947. tpg->lport = lport;
  948. tpg->lport_tpgt = tpgt;
  949. ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
  950. &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
  951. if (ret < 0) {
  952. kfree(tpg);
  953. return NULL;
  954. }
  955. return &tpg->se_tpg;
  956. }
  957. /*
  958. * Expected to be called with struct qla_hw_data->hardware_lock held
  959. */
  960. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
  961. scsi_qla_host_t *vha,
  962. const uint8_t *s_id)
  963. {
  964. struct qla_hw_data *ha = vha->hw;
  965. struct tcm_qla2xxx_lport *lport;
  966. struct se_node_acl *se_nacl;
  967. struct tcm_qla2xxx_nacl *nacl;
  968. u32 key;
  969. lport = ha->tgt.target_lport_ptr;
  970. if (!lport) {
  971. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  972. dump_stack();
  973. return NULL;
  974. }
  975. key = (((unsigned long)s_id[0] << 16) |
  976. ((unsigned long)s_id[1] << 8) |
  977. (unsigned long)s_id[2]);
  978. pr_debug("find_sess_by_s_id: 0x%06x\n", key);
  979. se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
  980. if (!se_nacl) {
  981. pr_debug("Unable to locate s_id: 0x%06x\n", key);
  982. return NULL;
  983. }
  984. pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
  985. se_nacl, se_nacl->initiatorname);
  986. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  987. if (!nacl->qla_tgt_sess) {
  988. pr_err("Unable to locate struct qla_tgt_sess\n");
  989. return NULL;
  990. }
  991. return nacl->qla_tgt_sess;
  992. }
  993. /*
  994. * Expected to be called with struct qla_hw_data->hardware_lock held
  995. */
  996. static void tcm_qla2xxx_set_sess_by_s_id(
  997. struct tcm_qla2xxx_lport *lport,
  998. struct se_node_acl *new_se_nacl,
  999. struct tcm_qla2xxx_nacl *nacl,
  1000. struct se_session *se_sess,
  1001. struct qla_tgt_sess *qla_tgt_sess,
  1002. uint8_t *s_id)
  1003. {
  1004. u32 key;
  1005. void *slot;
  1006. int rc;
  1007. key = (((unsigned long)s_id[0] << 16) |
  1008. ((unsigned long)s_id[1] << 8) |
  1009. (unsigned long)s_id[2]);
  1010. pr_debug("set_sess_by_s_id: %06x\n", key);
  1011. slot = btree_lookup32(&lport->lport_fcport_map, key);
  1012. if (!slot) {
  1013. if (new_se_nacl) {
  1014. pr_debug("Setting up new fc_port entry to new_se_nacl\n");
  1015. nacl->nport_id = key;
  1016. rc = btree_insert32(&lport->lport_fcport_map, key,
  1017. new_se_nacl, GFP_ATOMIC);
  1018. if (rc)
  1019. printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
  1020. (int)key);
  1021. } else {
  1022. pr_debug("Wiping nonexisting fc_port entry\n");
  1023. }
  1024. qla_tgt_sess->se_sess = se_sess;
  1025. nacl->qla_tgt_sess = qla_tgt_sess;
  1026. return;
  1027. }
  1028. if (nacl->qla_tgt_sess) {
  1029. if (new_se_nacl == NULL) {
  1030. pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
  1031. btree_remove32(&lport->lport_fcport_map, key);
  1032. nacl->qla_tgt_sess = NULL;
  1033. return;
  1034. }
  1035. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
  1036. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1037. qla_tgt_sess->se_sess = se_sess;
  1038. nacl->qla_tgt_sess = qla_tgt_sess;
  1039. return;
  1040. }
  1041. if (new_se_nacl == NULL) {
  1042. pr_debug("Clearing existing fc_port entry\n");
  1043. btree_remove32(&lport->lport_fcport_map, key);
  1044. return;
  1045. }
  1046. pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
  1047. btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
  1048. qla_tgt_sess->se_sess = se_sess;
  1049. nacl->qla_tgt_sess = qla_tgt_sess;
  1050. pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
  1051. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1052. }
  1053. /*
  1054. * Expected to be called with struct qla_hw_data->hardware_lock held
  1055. */
  1056. static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
  1057. scsi_qla_host_t *vha,
  1058. const uint16_t loop_id)
  1059. {
  1060. struct qla_hw_data *ha = vha->hw;
  1061. struct tcm_qla2xxx_lport *lport;
  1062. struct se_node_acl *se_nacl;
  1063. struct tcm_qla2xxx_nacl *nacl;
  1064. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1065. lport = ha->tgt.target_lport_ptr;
  1066. if (!lport) {
  1067. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1068. dump_stack();
  1069. return NULL;
  1070. }
  1071. pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1072. fc_loopid = lport->lport_loopid_map + loop_id;
  1073. se_nacl = fc_loopid->se_nacl;
  1074. if (!se_nacl) {
  1075. pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
  1076. loop_id);
  1077. return NULL;
  1078. }
  1079. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1080. if (!nacl->qla_tgt_sess) {
  1081. pr_err("Unable to locate struct qla_tgt_sess\n");
  1082. return NULL;
  1083. }
  1084. return nacl->qla_tgt_sess;
  1085. }
  1086. /*
  1087. * Expected to be called with struct qla_hw_data->hardware_lock held
  1088. */
  1089. static void tcm_qla2xxx_set_sess_by_loop_id(
  1090. struct tcm_qla2xxx_lport *lport,
  1091. struct se_node_acl *new_se_nacl,
  1092. struct tcm_qla2xxx_nacl *nacl,
  1093. struct se_session *se_sess,
  1094. struct qla_tgt_sess *qla_tgt_sess,
  1095. uint16_t loop_id)
  1096. {
  1097. struct se_node_acl *saved_nacl;
  1098. struct tcm_qla2xxx_fc_loopid *fc_loopid;
  1099. pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
  1100. fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
  1101. lport->lport_loopid_map)[loop_id];
  1102. saved_nacl = fc_loopid->se_nacl;
  1103. if (!saved_nacl) {
  1104. pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
  1105. fc_loopid->se_nacl = new_se_nacl;
  1106. if (qla_tgt_sess->se_sess != se_sess)
  1107. qla_tgt_sess->se_sess = se_sess;
  1108. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1109. nacl->qla_tgt_sess = qla_tgt_sess;
  1110. return;
  1111. }
  1112. if (nacl->qla_tgt_sess) {
  1113. if (new_se_nacl == NULL) {
  1114. pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1115. fc_loopid->se_nacl = NULL;
  1116. nacl->qla_tgt_sess = NULL;
  1117. return;
  1118. }
  1119. pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
  1120. fc_loopid->se_nacl = new_se_nacl;
  1121. if (qla_tgt_sess->se_sess != se_sess)
  1122. qla_tgt_sess->se_sess = se_sess;
  1123. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1124. nacl->qla_tgt_sess = qla_tgt_sess;
  1125. return;
  1126. }
  1127. if (new_se_nacl == NULL) {
  1128. pr_debug("Clearing fc_loopid->se_nacl\n");
  1129. fc_loopid->se_nacl = NULL;
  1130. return;
  1131. }
  1132. pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
  1133. fc_loopid->se_nacl = new_se_nacl;
  1134. if (qla_tgt_sess->se_sess != se_sess)
  1135. qla_tgt_sess->se_sess = se_sess;
  1136. if (nacl->qla_tgt_sess != qla_tgt_sess)
  1137. nacl->qla_tgt_sess = qla_tgt_sess;
  1138. pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
  1139. nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
  1140. }
  1141. /*
  1142. * Should always be called with qla_hw_data->hardware_lock held.
  1143. */
  1144. static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
  1145. struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
  1146. {
  1147. struct se_session *se_sess = sess->se_sess;
  1148. unsigned char be_sid[3];
  1149. be_sid[0] = sess->s_id.b.domain;
  1150. be_sid[1] = sess->s_id.b.area;
  1151. be_sid[2] = sess->s_id.b.al_pa;
  1152. tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
  1153. sess, be_sid);
  1154. tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
  1155. sess, sess->loop_id);
  1156. }
  1157. static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
  1158. {
  1159. struct qla_tgt *tgt = sess->tgt;
  1160. struct qla_hw_data *ha = tgt->ha;
  1161. struct se_session *se_sess;
  1162. struct se_node_acl *se_nacl;
  1163. struct tcm_qla2xxx_lport *lport;
  1164. struct tcm_qla2xxx_nacl *nacl;
  1165. BUG_ON(in_interrupt());
  1166. se_sess = sess->se_sess;
  1167. if (!se_sess) {
  1168. pr_err("struct qla_tgt_sess->se_sess is NULL\n");
  1169. dump_stack();
  1170. return;
  1171. }
  1172. se_nacl = se_sess->se_node_acl;
  1173. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1174. lport = ha->tgt.target_lport_ptr;
  1175. if (!lport) {
  1176. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1177. dump_stack();
  1178. return;
  1179. }
  1180. target_wait_for_sess_cmds(se_sess);
  1181. transport_deregister_session_configfs(sess->se_sess);
  1182. transport_deregister_session(sess->se_sess);
  1183. }
  1184. /*
  1185. * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
  1186. * to locate struct se_node_acl
  1187. */
  1188. static int tcm_qla2xxx_check_initiator_node_acl(
  1189. scsi_qla_host_t *vha,
  1190. unsigned char *fc_wwpn,
  1191. void *qla_tgt_sess,
  1192. uint8_t *s_id,
  1193. uint16_t loop_id)
  1194. {
  1195. struct qla_hw_data *ha = vha->hw;
  1196. struct tcm_qla2xxx_lport *lport;
  1197. struct tcm_qla2xxx_tpg *tpg;
  1198. struct tcm_qla2xxx_nacl *nacl;
  1199. struct se_portal_group *se_tpg;
  1200. struct se_node_acl *se_nacl;
  1201. struct se_session *se_sess;
  1202. struct qla_tgt_sess *sess = qla_tgt_sess;
  1203. unsigned char port_name[36];
  1204. unsigned long flags;
  1205. lport = ha->tgt.target_lport_ptr;
  1206. if (!lport) {
  1207. pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
  1208. dump_stack();
  1209. return -EINVAL;
  1210. }
  1211. /*
  1212. * Locate the TPG=1 reference..
  1213. */
  1214. tpg = lport->tpg_1;
  1215. if (!tpg) {
  1216. pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
  1217. return -EINVAL;
  1218. }
  1219. se_tpg = &tpg->se_tpg;
  1220. se_sess = transport_init_session();
  1221. if (IS_ERR(se_sess)) {
  1222. pr_err("Unable to initialize struct se_session\n");
  1223. return PTR_ERR(se_sess);
  1224. }
  1225. /*
  1226. * Format the FCP Initiator port_name into colon seperated values to
  1227. * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
  1228. */
  1229. memset(&port_name, 0, 36);
  1230. snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  1231. fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
  1232. fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
  1233. /*
  1234. * Locate our struct se_node_acl either from an explict NodeACL created
  1235. * via ConfigFS, or via running in TPG demo mode.
  1236. */
  1237. se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
  1238. port_name);
  1239. if (!se_sess->se_node_acl) {
  1240. transport_free_session(se_sess);
  1241. return -EINVAL;
  1242. }
  1243. se_nacl = se_sess->se_node_acl;
  1244. nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
  1245. /*
  1246. * And now setup the new se_nacl and session pointers into our HW lport
  1247. * mappings for fabric S_ID and LOOP_ID.
  1248. */
  1249. spin_lock_irqsave(&ha->hardware_lock, flags);
  1250. tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
  1251. qla_tgt_sess, s_id);
  1252. tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
  1253. qla_tgt_sess, loop_id);
  1254. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1255. /*
  1256. * Finally register the new FC Nexus with TCM
  1257. */
  1258. __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
  1259. return 0;
  1260. }
  1261. static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
  1262. uint16_t loop_id, bool conf_compl_supported)
  1263. {
  1264. struct qla_tgt *tgt = sess->tgt;
  1265. struct qla_hw_data *ha = tgt->ha;
  1266. struct tcm_qla2xxx_lport *lport = ha->tgt.target_lport_ptr;
  1267. struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
  1268. struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
  1269. struct tcm_qla2xxx_nacl, se_node_acl);
  1270. u32 key;
  1271. if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
  1272. pr_info("Updating session %p from port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
  1273. sess,
  1274. sess->port_name[0], sess->port_name[1],
  1275. sess->port_name[2], sess->port_name[3],
  1276. sess->port_name[4], sess->port_name[5],
  1277. sess->port_name[6], sess->port_name[7],
  1278. sess->loop_id, loop_id,
  1279. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
  1280. s_id.b.domain, s_id.b.area, s_id.b.al_pa);
  1281. if (sess->loop_id != loop_id) {
  1282. /*
  1283. * Because we can shuffle loop IDs around and we
  1284. * update different sessions non-atomically, we might
  1285. * have overwritten this session's old loop ID
  1286. * already, and we might end up overwriting some other
  1287. * session that will be updated later. So we have to
  1288. * be extra careful and we can't warn about those things...
  1289. */
  1290. if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
  1291. lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
  1292. lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
  1293. sess->loop_id = loop_id;
  1294. }
  1295. if (sess->s_id.b24 != s_id.b24) {
  1296. key = (((u32) sess->s_id.b.domain << 16) |
  1297. ((u32) sess->s_id.b.area << 8) |
  1298. ((u32) sess->s_id.b.al_pa));
  1299. if (btree_lookup32(&lport->lport_fcport_map, key))
  1300. WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
  1301. "Found wrong se_nacl when updating s_id %x:%x:%x\n",
  1302. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
  1303. else
  1304. WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
  1305. sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
  1306. key = (((u32) s_id.b.domain << 16) |
  1307. ((u32) s_id.b.area << 8) |
  1308. ((u32) s_id.b.al_pa));
  1309. if (btree_lookup32(&lport->lport_fcport_map, key)) {
  1310. WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
  1311. s_id.b.domain, s_id.b.area, s_id.b.al_pa);
  1312. btree_update32(&lport->lport_fcport_map, key, se_nacl);
  1313. } else {
  1314. btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
  1315. }
  1316. sess->s_id = s_id;
  1317. nacl->nport_id = key;
  1318. }
  1319. sess->conf_compl_supported = conf_compl_supported;
  1320. }
  1321. /*
  1322. * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
  1323. */
  1324. static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
  1325. .handle_cmd = tcm_qla2xxx_handle_cmd,
  1326. .handle_data = tcm_qla2xxx_handle_data,
  1327. .handle_tmr = tcm_qla2xxx_handle_tmr,
  1328. .free_cmd = tcm_qla2xxx_free_cmd,
  1329. .free_mcmd = tcm_qla2xxx_free_mcmd,
  1330. .free_session = tcm_qla2xxx_free_session,
  1331. .update_sess = tcm_qla2xxx_update_sess,
  1332. .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
  1333. .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
  1334. .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
  1335. .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
  1336. .put_sess = tcm_qla2xxx_put_sess,
  1337. .shutdown_sess = tcm_qla2xxx_shutdown_sess,
  1338. };
  1339. static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
  1340. {
  1341. int rc;
  1342. rc = btree_init32(&lport->lport_fcport_map);
  1343. if (rc) {
  1344. pr_err("Unable to initialize lport->lport_fcport_map btree\n");
  1345. return rc;
  1346. }
  1347. lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
  1348. 65536);
  1349. if (!lport->lport_loopid_map) {
  1350. pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
  1351. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1352. btree_destroy32(&lport->lport_fcport_map);
  1353. return -ENOMEM;
  1354. }
  1355. memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
  1356. * 65536);
  1357. pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
  1358. sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
  1359. return 0;
  1360. }
  1361. static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
  1362. {
  1363. struct qla_hw_data *ha = vha->hw;
  1364. struct tcm_qla2xxx_lport *lport;
  1365. /*
  1366. * Setup local pointer to vha, NPIV VP pointer (if present) and
  1367. * vha->tcm_lport pointer
  1368. */
  1369. lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
  1370. lport->qla_vha = vha;
  1371. return 0;
  1372. }
  1373. static struct se_wwn *tcm_qla2xxx_make_lport(
  1374. struct target_fabric_configfs *tf,
  1375. struct config_group *group,
  1376. const char *name)
  1377. {
  1378. struct tcm_qla2xxx_lport *lport;
  1379. u64 wwpn;
  1380. int ret = -ENODEV;
  1381. if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
  1382. return ERR_PTR(-EINVAL);
  1383. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1384. if (!lport) {
  1385. pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
  1386. return ERR_PTR(-ENOMEM);
  1387. }
  1388. lport->lport_wwpn = wwpn;
  1389. tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
  1390. wwpn);
  1391. sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
  1392. ret = tcm_qla2xxx_init_lport(lport);
  1393. if (ret != 0)
  1394. goto out;
  1395. ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
  1396. tcm_qla2xxx_lport_register_cb, lport);
  1397. if (ret != 0)
  1398. goto out_lport;
  1399. return &lport->lport_wwn;
  1400. out_lport:
  1401. vfree(lport->lport_loopid_map);
  1402. btree_destroy32(&lport->lport_fcport_map);
  1403. out:
  1404. kfree(lport);
  1405. return ERR_PTR(ret);
  1406. }
  1407. static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
  1408. {
  1409. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1410. struct tcm_qla2xxx_lport, lport_wwn);
  1411. struct scsi_qla_host *vha = lport->qla_vha;
  1412. struct qla_hw_data *ha = vha->hw;
  1413. struct se_node_acl *node;
  1414. u32 key = 0;
  1415. /*
  1416. * Call into qla2x_target.c LLD logic to complete the
  1417. * shutdown of struct qla_tgt after the call to
  1418. * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
  1419. */
  1420. if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
  1421. qlt_stop_phase2(ha->tgt.qla_tgt);
  1422. qlt_lport_deregister(vha);
  1423. vfree(lport->lport_loopid_map);
  1424. btree_for_each_safe32(&lport->lport_fcport_map, key, node)
  1425. btree_remove32(&lport->lport_fcport_map, key);
  1426. btree_destroy32(&lport->lport_fcport_map);
  1427. kfree(lport);
  1428. }
  1429. static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
  1430. struct target_fabric_configfs *tf,
  1431. struct config_group *group,
  1432. const char *name)
  1433. {
  1434. struct tcm_qla2xxx_lport *lport;
  1435. u64 npiv_wwpn, npiv_wwnn;
  1436. int ret;
  1437. if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
  1438. &npiv_wwpn, &npiv_wwnn) < 0)
  1439. return ERR_PTR(-EINVAL);
  1440. lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
  1441. if (!lport) {
  1442. pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
  1443. return ERR_PTR(-ENOMEM);
  1444. }
  1445. lport->lport_npiv_wwpn = npiv_wwpn;
  1446. lport->lport_npiv_wwnn = npiv_wwnn;
  1447. tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
  1448. TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
  1449. sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
  1450. /* FIXME: tcm_qla2xxx_npiv_make_lport */
  1451. ret = -ENOSYS;
  1452. if (ret != 0)
  1453. goto out;
  1454. return &lport->lport_wwn;
  1455. out:
  1456. kfree(lport);
  1457. return ERR_PTR(ret);
  1458. }
  1459. static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
  1460. {
  1461. struct tcm_qla2xxx_lport *lport = container_of(wwn,
  1462. struct tcm_qla2xxx_lport, lport_wwn);
  1463. struct scsi_qla_host *vha = lport->qla_vha;
  1464. struct Scsi_Host *sh = vha->host;
  1465. /*
  1466. * Notify libfc that we want to release the lport->npiv_vport
  1467. */
  1468. fc_vport_terminate(lport->npiv_vport);
  1469. scsi_host_put(sh);
  1470. kfree(lport);
  1471. }
  1472. static ssize_t tcm_qla2xxx_wwn_show_attr_version(
  1473. struct target_fabric_configfs *tf,
  1474. char *page)
  1475. {
  1476. return sprintf(page,
  1477. "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
  1478. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1479. utsname()->machine);
  1480. }
  1481. TF_WWN_ATTR_RO(tcm_qla2xxx, version);
  1482. static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
  1483. &tcm_qla2xxx_wwn_version.attr,
  1484. NULL,
  1485. };
  1486. static struct target_core_fabric_ops tcm_qla2xxx_ops = {
  1487. .get_fabric_name = tcm_qla2xxx_get_fabric_name,
  1488. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1489. .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
  1490. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1491. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1492. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1493. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1494. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1495. .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
  1496. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
  1497. .tpg_check_demo_mode_write_protect =
  1498. tcm_qla2xxx_check_demo_write_protect,
  1499. .tpg_check_prod_mode_write_protect =
  1500. tcm_qla2xxx_check_prod_write_protect,
  1501. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1502. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1503. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1504. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1505. .check_stop_free = tcm_qla2xxx_check_stop_free,
  1506. .release_cmd = tcm_qla2xxx_release_cmd,
  1507. .put_session = tcm_qla2xxx_put_session,
  1508. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1509. .close_session = tcm_qla2xxx_close_session,
  1510. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1511. .sess_get_initiator_sid = NULL,
  1512. .write_pending = tcm_qla2xxx_write_pending,
  1513. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1514. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1515. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1516. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1517. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1518. .queue_status = tcm_qla2xxx_queue_status,
  1519. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1520. /*
  1521. * Setup function pointers for generic logic in
  1522. * target_core_fabric_configfs.c
  1523. */
  1524. .fabric_make_wwn = tcm_qla2xxx_make_lport,
  1525. .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
  1526. .fabric_make_tpg = tcm_qla2xxx_make_tpg,
  1527. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1528. .fabric_post_link = NULL,
  1529. .fabric_pre_unlink = NULL,
  1530. .fabric_make_np = NULL,
  1531. .fabric_drop_np = NULL,
  1532. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1533. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1534. };
  1535. static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
  1536. .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
  1537. .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
  1538. .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
  1539. .tpg_get_tag = tcm_qla2xxx_get_tag,
  1540. .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
  1541. .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
  1542. .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
  1543. .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
  1544. .tpg_check_demo_mode = tcm_qla2xxx_check_false,
  1545. .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
  1546. .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
  1547. .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
  1548. .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
  1549. .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
  1550. .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
  1551. .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
  1552. .release_cmd = tcm_qla2xxx_release_cmd,
  1553. .put_session = tcm_qla2xxx_put_session,
  1554. .shutdown_session = tcm_qla2xxx_shutdown_session,
  1555. .close_session = tcm_qla2xxx_close_session,
  1556. .sess_get_index = tcm_qla2xxx_sess_get_index,
  1557. .sess_get_initiator_sid = NULL,
  1558. .write_pending = tcm_qla2xxx_write_pending,
  1559. .write_pending_status = tcm_qla2xxx_write_pending_status,
  1560. .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
  1561. .get_task_tag = tcm_qla2xxx_get_task_tag,
  1562. .get_cmd_state = tcm_qla2xxx_get_cmd_state,
  1563. .queue_data_in = tcm_qla2xxx_queue_data_in,
  1564. .queue_status = tcm_qla2xxx_queue_status,
  1565. .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
  1566. /*
  1567. * Setup function pointers for generic logic in
  1568. * target_core_fabric_configfs.c
  1569. */
  1570. .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
  1571. .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
  1572. .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
  1573. .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
  1574. .fabric_post_link = NULL,
  1575. .fabric_pre_unlink = NULL,
  1576. .fabric_make_np = NULL,
  1577. .fabric_drop_np = NULL,
  1578. .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
  1579. .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
  1580. };
  1581. static int tcm_qla2xxx_register_configfs(void)
  1582. {
  1583. struct target_fabric_configfs *fabric, *npiv_fabric;
  1584. int ret;
  1585. pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
  1586. UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
  1587. utsname()->machine);
  1588. /*
  1589. * Register the top level struct config_item_type with TCM core
  1590. */
  1591. fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
  1592. if (IS_ERR(fabric)) {
  1593. pr_err("target_fabric_configfs_init() failed\n");
  1594. return PTR_ERR(fabric);
  1595. }
  1596. /*
  1597. * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
  1598. */
  1599. fabric->tf_ops = tcm_qla2xxx_ops;
  1600. /*
  1601. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1602. */
  1603. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1604. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
  1605. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
  1606. tcm_qla2xxx_tpg_attrib_attrs;
  1607. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1608. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1609. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1610. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1611. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1612. TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1613. /*
  1614. * Register the fabric for use within TCM
  1615. */
  1616. ret = target_fabric_configfs_register(fabric);
  1617. if (ret < 0) {
  1618. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1619. return ret;
  1620. }
  1621. /*
  1622. * Setup our local pointer to *fabric
  1623. */
  1624. tcm_qla2xxx_fabric_configfs = fabric;
  1625. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
  1626. /*
  1627. * Register the top level struct config_item_type for NPIV with TCM core
  1628. */
  1629. npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
  1630. if (IS_ERR(npiv_fabric)) {
  1631. pr_err("target_fabric_configfs_init() failed\n");
  1632. ret = PTR_ERR(npiv_fabric);
  1633. goto out_fabric;
  1634. }
  1635. /*
  1636. * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
  1637. */
  1638. npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
  1639. /*
  1640. * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
  1641. */
  1642. TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
  1643. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
  1644. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1645. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1646. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1647. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  1648. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  1649. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  1650. TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  1651. /*
  1652. * Register the npiv_fabric for use within TCM
  1653. */
  1654. ret = target_fabric_configfs_register(npiv_fabric);
  1655. if (ret < 0) {
  1656. pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
  1657. goto out_fabric;
  1658. }
  1659. /*
  1660. * Setup our local pointer to *npiv_fabric
  1661. */
  1662. tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
  1663. pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
  1664. tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
  1665. WQ_MEM_RECLAIM, 0);
  1666. if (!tcm_qla2xxx_free_wq) {
  1667. ret = -ENOMEM;
  1668. goto out_fabric_npiv;
  1669. }
  1670. tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
  1671. if (!tcm_qla2xxx_cmd_wq) {
  1672. ret = -ENOMEM;
  1673. goto out_free_wq;
  1674. }
  1675. return 0;
  1676. out_free_wq:
  1677. destroy_workqueue(tcm_qla2xxx_free_wq);
  1678. out_fabric_npiv:
  1679. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1680. out_fabric:
  1681. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1682. return ret;
  1683. }
  1684. static void tcm_qla2xxx_deregister_configfs(void)
  1685. {
  1686. destroy_workqueue(tcm_qla2xxx_cmd_wq);
  1687. destroy_workqueue(tcm_qla2xxx_free_wq);
  1688. target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
  1689. tcm_qla2xxx_fabric_configfs = NULL;
  1690. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
  1691. target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
  1692. tcm_qla2xxx_npiv_fabric_configfs = NULL;
  1693. pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
  1694. }
  1695. static int __init tcm_qla2xxx_init(void)
  1696. {
  1697. int ret;
  1698. ret = tcm_qla2xxx_register_configfs();
  1699. if (ret < 0)
  1700. return ret;
  1701. return 0;
  1702. }
  1703. static void __exit tcm_qla2xxx_exit(void)
  1704. {
  1705. tcm_qla2xxx_deregister_configfs();
  1706. }
  1707. MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
  1708. MODULE_LICENSE("GPL");
  1709. module_init(tcm_qla2xxx_init);
  1710. module_exit(tcm_qla2xxx_exit);