tcm_qla2xxx.c 53 KB

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