tcm_qla2xxx.c 53 KB

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