tcm_qla2xxx.c 54 KB

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