tcm_qla2xxx.c 55 KB

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