tcm_qla2xxx.c 53 KB

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