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