target_core_device.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <net/sock.h>
  36. #include <net/tcp.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_device.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_backend.h>
  41. #include <target/target_core_fabric.h>
  42. #include "target_core_internal.h"
  43. #include "target_core_alua.h"
  44. #include "target_core_pr.h"
  45. #include "target_core_ua.h"
  46. DEFINE_MUTEX(g_device_mutex);
  47. LIST_HEAD(g_device_list);
  48. static struct se_hba *lun0_hba;
  49. /* not static, needed by tpg.c */
  50. struct se_device *g_lun0_dev;
  51. sense_reason_t
  52. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  53. {
  54. struct se_lun *se_lun = NULL;
  55. struct se_session *se_sess = se_cmd->se_sess;
  56. struct se_device *dev;
  57. unsigned long flags;
  58. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  59. return TCM_NON_EXISTENT_LUN;
  60. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  61. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  62. if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  63. struct se_dev_entry *deve = se_cmd->se_deve;
  64. deve->total_cmds++;
  65. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  66. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
  67. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  68. " Access for 0x%08x\n",
  69. se_cmd->se_tfo->get_fabric_name(),
  70. unpacked_lun);
  71. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  72. return TCM_WRITE_PROTECTED;
  73. }
  74. if (se_cmd->data_direction == DMA_TO_DEVICE)
  75. deve->write_bytes += se_cmd->data_length;
  76. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  77. deve->read_bytes += se_cmd->data_length;
  78. se_lun = deve->se_lun;
  79. se_cmd->se_lun = deve->se_lun;
  80. se_cmd->pr_res_key = deve->pr_res_key;
  81. se_cmd->orig_fe_lun = unpacked_lun;
  82. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  83. percpu_ref_get(&se_lun->lun_ref);
  84. se_cmd->lun_ref_active = true;
  85. }
  86. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  87. if (!se_lun) {
  88. /*
  89. * Use the se_portal_group->tpg_virt_lun0 to allow for
  90. * REPORT_LUNS, et al to be returned when no active
  91. * MappedLUN=0 exists for this Initiator Port.
  92. */
  93. if (unpacked_lun != 0) {
  94. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  95. " Access for 0x%08x\n",
  96. se_cmd->se_tfo->get_fabric_name(),
  97. unpacked_lun);
  98. return TCM_NON_EXISTENT_LUN;
  99. }
  100. /*
  101. * Force WRITE PROTECT for virtual LUN 0
  102. */
  103. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  104. (se_cmd->data_direction != DMA_NONE))
  105. return TCM_WRITE_PROTECTED;
  106. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  107. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  108. se_cmd->orig_fe_lun = 0;
  109. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  110. percpu_ref_get(&se_lun->lun_ref);
  111. se_cmd->lun_ref_active = true;
  112. }
  113. /* Directly associate cmd with se_dev */
  114. se_cmd->se_dev = se_lun->lun_se_dev;
  115. dev = se_lun->lun_se_dev;
  116. atomic_long_inc(&dev->num_cmds);
  117. if (se_cmd->data_direction == DMA_TO_DEVICE)
  118. atomic_long_add(se_cmd->data_length, &dev->write_bytes);
  119. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  120. atomic_long_add(se_cmd->data_length, &dev->read_bytes);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  124. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  125. {
  126. struct se_dev_entry *deve;
  127. struct se_lun *se_lun = NULL;
  128. struct se_session *se_sess = se_cmd->se_sess;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. unsigned long flags;
  131. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  132. return -ENODEV;
  133. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  134. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  135. deve = se_cmd->se_deve;
  136. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  137. se_tmr->tmr_lun = deve->se_lun;
  138. se_cmd->se_lun = deve->se_lun;
  139. se_lun = deve->se_lun;
  140. se_cmd->pr_res_key = deve->pr_res_key;
  141. se_cmd->orig_fe_lun = unpacked_lun;
  142. }
  143. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  144. if (!se_lun) {
  145. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  146. " Access for 0x%08x\n",
  147. se_cmd->se_tfo->get_fabric_name(),
  148. unpacked_lun);
  149. return -ENODEV;
  150. }
  151. /* Directly associate cmd with se_dev */
  152. se_cmd->se_dev = se_lun->lun_se_dev;
  153. se_tmr->tmr_dev = se_lun->lun_se_dev;
  154. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  155. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  156. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  160. /*
  161. * This function is called from core_scsi3_emulate_pro_register_and_move()
  162. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
  163. * when a matching rtpi is found.
  164. */
  165. struct se_dev_entry *core_get_se_deve_from_rtpi(
  166. struct se_node_acl *nacl,
  167. u16 rtpi)
  168. {
  169. struct se_dev_entry *deve;
  170. struct se_lun *lun;
  171. struct se_port *port;
  172. struct se_portal_group *tpg = nacl->se_tpg;
  173. u32 i;
  174. spin_lock_irq(&nacl->device_list_lock);
  175. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  176. deve = nacl->device_list[i];
  177. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  178. continue;
  179. lun = deve->se_lun;
  180. if (!lun) {
  181. pr_err("%s device entries device pointer is"
  182. " NULL, but Initiator has access.\n",
  183. tpg->se_tpg_tfo->get_fabric_name());
  184. continue;
  185. }
  186. port = lun->lun_sep;
  187. if (!port) {
  188. pr_err("%s device entries device pointer is"
  189. " NULL, but Initiator has access.\n",
  190. tpg->se_tpg_tfo->get_fabric_name());
  191. continue;
  192. }
  193. if (port->sep_rtpi != rtpi)
  194. continue;
  195. atomic_inc_mb(&deve->pr_ref_count);
  196. spin_unlock_irq(&nacl->device_list_lock);
  197. return deve;
  198. }
  199. spin_unlock_irq(&nacl->device_list_lock);
  200. return NULL;
  201. }
  202. int core_free_device_list_for_node(
  203. struct se_node_acl *nacl,
  204. struct se_portal_group *tpg)
  205. {
  206. struct se_dev_entry *deve;
  207. struct se_lun *lun;
  208. u32 i;
  209. if (!nacl->device_list)
  210. return 0;
  211. spin_lock_irq(&nacl->device_list_lock);
  212. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  213. deve = nacl->device_list[i];
  214. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  215. continue;
  216. if (!deve->se_lun) {
  217. pr_err("%s device entries device pointer is"
  218. " NULL, but Initiator has access.\n",
  219. tpg->se_tpg_tfo->get_fabric_name());
  220. continue;
  221. }
  222. lun = deve->se_lun;
  223. spin_unlock_irq(&nacl->device_list_lock);
  224. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  225. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  226. spin_lock_irq(&nacl->device_list_lock);
  227. }
  228. spin_unlock_irq(&nacl->device_list_lock);
  229. array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
  230. nacl->device_list = NULL;
  231. return 0;
  232. }
  233. void core_update_device_list_access(
  234. u32 mapped_lun,
  235. u32 lun_access,
  236. struct se_node_acl *nacl)
  237. {
  238. struct se_dev_entry *deve;
  239. spin_lock_irq(&nacl->device_list_lock);
  240. deve = nacl->device_list[mapped_lun];
  241. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  242. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  243. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  244. } else {
  245. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  246. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  247. }
  248. spin_unlock_irq(&nacl->device_list_lock);
  249. }
  250. /* core_enable_device_list_for_node():
  251. *
  252. *
  253. */
  254. int core_enable_device_list_for_node(
  255. struct se_lun *lun,
  256. struct se_lun_acl *lun_acl,
  257. u32 mapped_lun,
  258. u32 lun_access,
  259. struct se_node_acl *nacl,
  260. struct se_portal_group *tpg)
  261. {
  262. struct se_port *port = lun->lun_sep;
  263. struct se_dev_entry *deve;
  264. spin_lock_irq(&nacl->device_list_lock);
  265. deve = nacl->device_list[mapped_lun];
  266. /*
  267. * Check if the call is handling demo mode -> explicit LUN ACL
  268. * transition. This transition must be for the same struct se_lun
  269. * + mapped_lun that was setup in demo mode..
  270. */
  271. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  272. if (deve->se_lun_acl != NULL) {
  273. pr_err("struct se_dev_entry->se_lun_acl"
  274. " already set for demo mode -> explicit"
  275. " LUN ACL transition\n");
  276. spin_unlock_irq(&nacl->device_list_lock);
  277. return -EINVAL;
  278. }
  279. if (deve->se_lun != lun) {
  280. pr_err("struct se_dev_entry->se_lun does"
  281. " match passed struct se_lun for demo mode"
  282. " -> explicit LUN ACL transition\n");
  283. spin_unlock_irq(&nacl->device_list_lock);
  284. return -EINVAL;
  285. }
  286. deve->se_lun_acl = lun_acl;
  287. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  288. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  289. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  290. } else {
  291. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  292. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  293. }
  294. spin_unlock_irq(&nacl->device_list_lock);
  295. return 0;
  296. }
  297. deve->se_lun = lun;
  298. deve->se_lun_acl = lun_acl;
  299. deve->mapped_lun = mapped_lun;
  300. deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
  301. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  302. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  303. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  304. } else {
  305. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  306. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  307. }
  308. deve->creation_time = get_jiffies_64();
  309. deve->attach_count++;
  310. spin_unlock_irq(&nacl->device_list_lock);
  311. spin_lock_bh(&port->sep_alua_lock);
  312. list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
  313. spin_unlock_bh(&port->sep_alua_lock);
  314. return 0;
  315. }
  316. /* core_disable_device_list_for_node():
  317. *
  318. *
  319. */
  320. int core_disable_device_list_for_node(
  321. struct se_lun *lun,
  322. struct se_lun_acl *lun_acl,
  323. u32 mapped_lun,
  324. u32 lun_access,
  325. struct se_node_acl *nacl,
  326. struct se_portal_group *tpg)
  327. {
  328. struct se_port *port = lun->lun_sep;
  329. struct se_dev_entry *deve = nacl->device_list[mapped_lun];
  330. /*
  331. * If the MappedLUN entry is being disabled, the entry in
  332. * port->sep_alua_list must be removed now before clearing the
  333. * struct se_dev_entry pointers below as logic in
  334. * core_alua_do_transition_tg_pt() depends on these being present.
  335. *
  336. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  337. * that have not been explicitly converted to MappedLUNs ->
  338. * struct se_lun_acl, but we remove deve->alua_port_list from
  339. * port->sep_alua_list. This also means that active UAs and
  340. * NodeACL context specific PR metadata for demo-mode
  341. * MappedLUN *deve will be released below..
  342. */
  343. spin_lock_bh(&port->sep_alua_lock);
  344. list_del(&deve->alua_port_list);
  345. spin_unlock_bh(&port->sep_alua_lock);
  346. /*
  347. * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
  348. * PR operation to complete.
  349. */
  350. while (atomic_read(&deve->pr_ref_count) != 0)
  351. cpu_relax();
  352. spin_lock_irq(&nacl->device_list_lock);
  353. /*
  354. * Disable struct se_dev_entry LUN ACL mapping
  355. */
  356. core_scsi3_ua_release_all(deve);
  357. deve->se_lun = NULL;
  358. deve->se_lun_acl = NULL;
  359. deve->lun_flags = 0;
  360. deve->creation_time = 0;
  361. deve->attach_count--;
  362. spin_unlock_irq(&nacl->device_list_lock);
  363. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  364. return 0;
  365. }
  366. /* core_clear_lun_from_tpg():
  367. *
  368. *
  369. */
  370. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  371. {
  372. struct se_node_acl *nacl;
  373. struct se_dev_entry *deve;
  374. u32 i;
  375. spin_lock_irq(&tpg->acl_node_lock);
  376. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  377. spin_unlock_irq(&tpg->acl_node_lock);
  378. spin_lock_irq(&nacl->device_list_lock);
  379. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  380. deve = nacl->device_list[i];
  381. if (lun != deve->se_lun)
  382. continue;
  383. spin_unlock_irq(&nacl->device_list_lock);
  384. core_disable_device_list_for_node(lun, NULL,
  385. deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
  386. nacl, tpg);
  387. spin_lock_irq(&nacl->device_list_lock);
  388. }
  389. spin_unlock_irq(&nacl->device_list_lock);
  390. spin_lock_irq(&tpg->acl_node_lock);
  391. }
  392. spin_unlock_irq(&tpg->acl_node_lock);
  393. }
  394. static struct se_port *core_alloc_port(struct se_device *dev)
  395. {
  396. struct se_port *port, *port_tmp;
  397. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  398. if (!port) {
  399. pr_err("Unable to allocate struct se_port\n");
  400. return ERR_PTR(-ENOMEM);
  401. }
  402. INIT_LIST_HEAD(&port->sep_alua_list);
  403. INIT_LIST_HEAD(&port->sep_list);
  404. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  405. spin_lock_init(&port->sep_alua_lock);
  406. mutex_init(&port->sep_tg_pt_md_mutex);
  407. spin_lock(&dev->se_port_lock);
  408. if (dev->dev_port_count == 0x0000ffff) {
  409. pr_warn("Reached dev->dev_port_count =="
  410. " 0x0000ffff\n");
  411. spin_unlock(&dev->se_port_lock);
  412. return ERR_PTR(-ENOSPC);
  413. }
  414. again:
  415. /*
  416. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  417. * Here is the table from spc4r17 section 7.7.3.8.
  418. *
  419. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  420. *
  421. * Code Description
  422. * 0h Reserved
  423. * 1h Relative port 1, historically known as port A
  424. * 2h Relative port 2, historically known as port B
  425. * 3h to FFFFh Relative port 3 through 65 535
  426. */
  427. port->sep_rtpi = dev->dev_rpti_counter++;
  428. if (!port->sep_rtpi)
  429. goto again;
  430. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  431. /*
  432. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  433. * for 16-bit wrap..
  434. */
  435. if (port->sep_rtpi == port_tmp->sep_rtpi)
  436. goto again;
  437. }
  438. spin_unlock(&dev->se_port_lock);
  439. return port;
  440. }
  441. static void core_export_port(
  442. struct se_device *dev,
  443. struct se_portal_group *tpg,
  444. struct se_port *port,
  445. struct se_lun *lun)
  446. {
  447. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  448. spin_lock(&dev->se_port_lock);
  449. spin_lock(&lun->lun_sep_lock);
  450. port->sep_tpg = tpg;
  451. port->sep_lun = lun;
  452. lun->lun_sep = port;
  453. spin_unlock(&lun->lun_sep_lock);
  454. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  455. spin_unlock(&dev->se_port_lock);
  456. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  457. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  458. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  459. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  460. pr_err("Unable to allocate t10_alua_tg_pt"
  461. "_gp_member_t\n");
  462. return;
  463. }
  464. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  465. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  466. dev->t10_alua.default_tg_pt_gp);
  467. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  468. pr_debug("%s/%s: Adding to default ALUA Target Port"
  469. " Group: alua/default_tg_pt_gp\n",
  470. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  471. }
  472. dev->dev_port_count++;
  473. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  474. }
  475. /*
  476. * Called with struct se_device->se_port_lock spinlock held.
  477. */
  478. static void core_release_port(struct se_device *dev, struct se_port *port)
  479. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  480. {
  481. /*
  482. * Wait for any port reference for PR ALL_TG_PT=1 operation
  483. * to complete in __core_scsi3_alloc_registration()
  484. */
  485. spin_unlock(&dev->se_port_lock);
  486. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  487. cpu_relax();
  488. spin_lock(&dev->se_port_lock);
  489. core_alua_free_tg_pt_gp_mem(port);
  490. list_del(&port->sep_list);
  491. dev->dev_port_count--;
  492. kfree(port);
  493. }
  494. int core_dev_export(
  495. struct se_device *dev,
  496. struct se_portal_group *tpg,
  497. struct se_lun *lun)
  498. {
  499. struct se_hba *hba = dev->se_hba;
  500. struct se_port *port;
  501. port = core_alloc_port(dev);
  502. if (IS_ERR(port))
  503. return PTR_ERR(port);
  504. lun->lun_se_dev = dev;
  505. spin_lock(&hba->device_lock);
  506. dev->export_count++;
  507. spin_unlock(&hba->device_lock);
  508. core_export_port(dev, tpg, port, lun);
  509. return 0;
  510. }
  511. void core_dev_unexport(
  512. struct se_device *dev,
  513. struct se_portal_group *tpg,
  514. struct se_lun *lun)
  515. {
  516. struct se_hba *hba = dev->se_hba;
  517. struct se_port *port = lun->lun_sep;
  518. spin_lock(&lun->lun_sep_lock);
  519. if (lun->lun_se_dev == NULL) {
  520. spin_unlock(&lun->lun_sep_lock);
  521. return;
  522. }
  523. spin_unlock(&lun->lun_sep_lock);
  524. spin_lock(&dev->se_port_lock);
  525. core_release_port(dev, port);
  526. spin_unlock(&dev->se_port_lock);
  527. spin_lock(&hba->device_lock);
  528. dev->export_count--;
  529. spin_unlock(&hba->device_lock);
  530. lun->lun_sep = NULL;
  531. lun->lun_se_dev = NULL;
  532. }
  533. static void se_release_vpd_for_dev(struct se_device *dev)
  534. {
  535. struct t10_vpd *vpd, *vpd_tmp;
  536. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  537. list_for_each_entry_safe(vpd, vpd_tmp,
  538. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  539. list_del(&vpd->vpd_list);
  540. kfree(vpd);
  541. }
  542. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  543. }
  544. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  545. {
  546. u32 aligned_max_sectors;
  547. u32 alignment;
  548. /*
  549. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  550. * transport_allocate_data_tasks() operation.
  551. */
  552. alignment = max(1ul, PAGE_SIZE / block_size);
  553. aligned_max_sectors = rounddown(max_sectors, alignment);
  554. if (max_sectors != aligned_max_sectors)
  555. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  556. max_sectors, aligned_max_sectors);
  557. return aligned_max_sectors;
  558. }
  559. int se_dev_set_max_unmap_lba_count(
  560. struct se_device *dev,
  561. u32 max_unmap_lba_count)
  562. {
  563. dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  564. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  565. dev, dev->dev_attrib.max_unmap_lba_count);
  566. return 0;
  567. }
  568. EXPORT_SYMBOL(se_dev_set_max_unmap_lba_count);
  569. int se_dev_set_max_unmap_block_desc_count(
  570. struct se_device *dev,
  571. u32 max_unmap_block_desc_count)
  572. {
  573. dev->dev_attrib.max_unmap_block_desc_count =
  574. max_unmap_block_desc_count;
  575. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  576. dev, dev->dev_attrib.max_unmap_block_desc_count);
  577. return 0;
  578. }
  579. EXPORT_SYMBOL(se_dev_set_max_unmap_block_desc_count);
  580. int se_dev_set_unmap_granularity(
  581. struct se_device *dev,
  582. u32 unmap_granularity)
  583. {
  584. dev->dev_attrib.unmap_granularity = unmap_granularity;
  585. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  586. dev, dev->dev_attrib.unmap_granularity);
  587. return 0;
  588. }
  589. EXPORT_SYMBOL(se_dev_set_unmap_granularity);
  590. int se_dev_set_unmap_granularity_alignment(
  591. struct se_device *dev,
  592. u32 unmap_granularity_alignment)
  593. {
  594. dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  595. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  596. dev, dev->dev_attrib.unmap_granularity_alignment);
  597. return 0;
  598. }
  599. EXPORT_SYMBOL(se_dev_set_unmap_granularity_alignment);
  600. int se_dev_set_max_write_same_len(
  601. struct se_device *dev,
  602. u32 max_write_same_len)
  603. {
  604. dev->dev_attrib.max_write_same_len = max_write_same_len;
  605. pr_debug("dev[%p]: Set max_write_same_len: %u\n",
  606. dev, dev->dev_attrib.max_write_same_len);
  607. return 0;
  608. }
  609. EXPORT_SYMBOL(se_dev_set_max_write_same_len);
  610. static void dev_set_t10_wwn_model_alias(struct se_device *dev)
  611. {
  612. const char *configname;
  613. configname = config_item_name(&dev->dev_group.cg_item);
  614. if (strlen(configname) >= 16) {
  615. pr_warn("dev[%p]: Backstore name '%s' is too long for "
  616. "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
  617. configname);
  618. }
  619. snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
  620. }
  621. int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
  622. {
  623. if (dev->export_count) {
  624. pr_err("dev[%p]: Unable to change model alias"
  625. " while export_count is %d\n",
  626. dev, dev->export_count);
  627. return -EINVAL;
  628. }
  629. if (flag != 0 && flag != 1) {
  630. pr_err("Illegal value %d\n", flag);
  631. return -EINVAL;
  632. }
  633. if (flag) {
  634. dev_set_t10_wwn_model_alias(dev);
  635. } else {
  636. strncpy(&dev->t10_wwn.model[0],
  637. dev->transport->inquiry_prod, 16);
  638. }
  639. dev->dev_attrib.emulate_model_alias = flag;
  640. return 0;
  641. }
  642. EXPORT_SYMBOL(se_dev_set_emulate_model_alias);
  643. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  644. {
  645. if (flag != 0 && flag != 1) {
  646. pr_err("Illegal value %d\n", flag);
  647. return -EINVAL;
  648. }
  649. if (flag) {
  650. pr_err("dpo_emulated not supported\n");
  651. return -EINVAL;
  652. }
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(se_dev_set_emulate_dpo);
  656. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  657. {
  658. if (flag != 0 && flag != 1) {
  659. pr_err("Illegal value %d\n", flag);
  660. return -EINVAL;
  661. }
  662. if (flag &&
  663. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  664. pr_err("emulate_fua_write not supported for pSCSI\n");
  665. return -EINVAL;
  666. }
  667. dev->dev_attrib.emulate_fua_write = flag;
  668. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  669. dev, dev->dev_attrib.emulate_fua_write);
  670. return 0;
  671. }
  672. EXPORT_SYMBOL(se_dev_set_emulate_fua_write);
  673. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  674. {
  675. if (flag != 0 && flag != 1) {
  676. pr_err("Illegal value %d\n", flag);
  677. return -EINVAL;
  678. }
  679. if (flag) {
  680. pr_err("ua read emulated not supported\n");
  681. return -EINVAL;
  682. }
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(se_dev_set_emulate_fua_read);
  686. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  687. {
  688. if (flag != 0 && flag != 1) {
  689. pr_err("Illegal value %d\n", flag);
  690. return -EINVAL;
  691. }
  692. if (flag &&
  693. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  694. pr_err("emulate_write_cache not supported for pSCSI\n");
  695. return -EINVAL;
  696. }
  697. if (flag &&
  698. dev->transport->get_write_cache) {
  699. pr_err("emulate_write_cache not supported for this device\n");
  700. return -EINVAL;
  701. }
  702. dev->dev_attrib.emulate_write_cache = flag;
  703. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  704. dev, dev->dev_attrib.emulate_write_cache);
  705. return 0;
  706. }
  707. EXPORT_SYMBOL(se_dev_set_emulate_write_cache);
  708. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  709. {
  710. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  711. pr_err("Illegal value %d\n", flag);
  712. return -EINVAL;
  713. }
  714. if (dev->export_count) {
  715. pr_err("dev[%p]: Unable to change SE Device"
  716. " UA_INTRLCK_CTRL while export_count is %d\n",
  717. dev, dev->export_count);
  718. return -EINVAL;
  719. }
  720. dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
  721. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  722. dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(se_dev_set_emulate_ua_intlck_ctrl);
  726. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  727. {
  728. if ((flag != 0) && (flag != 1)) {
  729. pr_err("Illegal value %d\n", flag);
  730. return -EINVAL;
  731. }
  732. if (dev->export_count) {
  733. pr_err("dev[%p]: Unable to change SE Device TAS while"
  734. " export_count is %d\n",
  735. dev, dev->export_count);
  736. return -EINVAL;
  737. }
  738. dev->dev_attrib.emulate_tas = flag;
  739. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  740. dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  741. return 0;
  742. }
  743. EXPORT_SYMBOL(se_dev_set_emulate_tas);
  744. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  745. {
  746. if ((flag != 0) && (flag != 1)) {
  747. pr_err("Illegal value %d\n", flag);
  748. return -EINVAL;
  749. }
  750. /*
  751. * We expect this value to be non-zero when generic Block Layer
  752. * Discard supported is detected iblock_create_virtdevice().
  753. */
  754. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  755. pr_err("Generic Block Discard not supported\n");
  756. return -ENOSYS;
  757. }
  758. dev->dev_attrib.emulate_tpu = flag;
  759. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  760. dev, flag);
  761. return 0;
  762. }
  763. EXPORT_SYMBOL(se_dev_set_emulate_tpu);
  764. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  765. {
  766. if ((flag != 0) && (flag != 1)) {
  767. pr_err("Illegal value %d\n", flag);
  768. return -EINVAL;
  769. }
  770. /*
  771. * We expect this value to be non-zero when generic Block Layer
  772. * Discard supported is detected iblock_create_virtdevice().
  773. */
  774. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  775. pr_err("Generic Block Discard not supported\n");
  776. return -ENOSYS;
  777. }
  778. dev->dev_attrib.emulate_tpws = flag;
  779. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  780. dev, flag);
  781. return 0;
  782. }
  783. EXPORT_SYMBOL(se_dev_set_emulate_tpws);
  784. int se_dev_set_emulate_caw(struct se_device *dev, int flag)
  785. {
  786. if (flag != 0 && flag != 1) {
  787. pr_err("Illegal value %d\n", flag);
  788. return -EINVAL;
  789. }
  790. dev->dev_attrib.emulate_caw = flag;
  791. pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
  792. dev, flag);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(se_dev_set_emulate_caw);
  796. int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
  797. {
  798. if (flag != 0 && flag != 1) {
  799. pr_err("Illegal value %d\n", flag);
  800. return -EINVAL;
  801. }
  802. dev->dev_attrib.emulate_3pc = flag;
  803. pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
  804. dev, flag);
  805. return 0;
  806. }
  807. EXPORT_SYMBOL(se_dev_set_emulate_3pc);
  808. int se_dev_set_pi_prot_type(struct se_device *dev, int flag)
  809. {
  810. int rc, old_prot = dev->dev_attrib.pi_prot_type;
  811. if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
  812. pr_err("Illegal value %d for pi_prot_type\n", flag);
  813. return -EINVAL;
  814. }
  815. if (flag == 2) {
  816. pr_err("DIF TYPE2 protection currently not supported\n");
  817. return -ENOSYS;
  818. }
  819. if (dev->dev_attrib.hw_pi_prot_type) {
  820. pr_warn("DIF protection enabled on underlying hardware,"
  821. " ignoring\n");
  822. return 0;
  823. }
  824. if (!dev->transport->init_prot || !dev->transport->free_prot) {
  825. /* 0 is only allowed value for non-supporting backends */
  826. if (flag == 0)
  827. return 0;
  828. pr_err("DIF protection not supported by backend: %s\n",
  829. dev->transport->name);
  830. return -ENOSYS;
  831. }
  832. if (!(dev->dev_flags & DF_CONFIGURED)) {
  833. pr_err("DIF protection requires device to be configured\n");
  834. return -ENODEV;
  835. }
  836. if (dev->export_count) {
  837. pr_err("dev[%p]: Unable to change SE Device PROT type while"
  838. " export_count is %d\n", dev, dev->export_count);
  839. return -EINVAL;
  840. }
  841. dev->dev_attrib.pi_prot_type = flag;
  842. if (flag && !old_prot) {
  843. rc = dev->transport->init_prot(dev);
  844. if (rc) {
  845. dev->dev_attrib.pi_prot_type = old_prot;
  846. return rc;
  847. }
  848. } else if (!flag && old_prot) {
  849. dev->transport->free_prot(dev);
  850. }
  851. pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
  852. return 0;
  853. }
  854. EXPORT_SYMBOL(se_dev_set_pi_prot_type);
  855. int se_dev_set_pi_prot_format(struct se_device *dev, int flag)
  856. {
  857. int rc;
  858. if (!flag)
  859. return 0;
  860. if (flag != 1) {
  861. pr_err("Illegal value %d for pi_prot_format\n", flag);
  862. return -EINVAL;
  863. }
  864. if (!dev->transport->format_prot) {
  865. pr_err("DIF protection format not supported by backend %s\n",
  866. dev->transport->name);
  867. return -ENOSYS;
  868. }
  869. if (!(dev->dev_flags & DF_CONFIGURED)) {
  870. pr_err("DIF protection format requires device to be configured\n");
  871. return -ENODEV;
  872. }
  873. if (dev->export_count) {
  874. pr_err("dev[%p]: Unable to format SE Device PROT type while"
  875. " export_count is %d\n", dev, dev->export_count);
  876. return -EINVAL;
  877. }
  878. rc = dev->transport->format_prot(dev);
  879. if (rc)
  880. return rc;
  881. pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
  882. return 0;
  883. }
  884. EXPORT_SYMBOL(se_dev_set_pi_prot_format);
  885. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  886. {
  887. if ((flag != 0) && (flag != 1)) {
  888. pr_err("Illegal value %d\n", flag);
  889. return -EINVAL;
  890. }
  891. dev->dev_attrib.enforce_pr_isids = flag;
  892. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  893. (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  894. return 0;
  895. }
  896. EXPORT_SYMBOL(se_dev_set_enforce_pr_isids);
  897. int se_dev_set_force_pr_aptpl(struct se_device *dev, int flag)
  898. {
  899. if ((flag != 0) && (flag != 1)) {
  900. printk(KERN_ERR "Illegal value %d\n", flag);
  901. return -EINVAL;
  902. }
  903. if (dev->export_count) {
  904. pr_err("dev[%p]: Unable to set force_pr_aptpl while"
  905. " export_count is %d\n", dev, dev->export_count);
  906. return -EINVAL;
  907. }
  908. dev->dev_attrib.force_pr_aptpl = flag;
  909. pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", dev, flag);
  910. return 0;
  911. }
  912. EXPORT_SYMBOL(se_dev_set_force_pr_aptpl);
  913. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  914. {
  915. if ((flag != 0) && (flag != 1)) {
  916. printk(KERN_ERR "Illegal value %d\n", flag);
  917. return -EINVAL;
  918. }
  919. dev->dev_attrib.is_nonrot = flag;
  920. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  921. dev, flag);
  922. return 0;
  923. }
  924. EXPORT_SYMBOL(se_dev_set_is_nonrot);
  925. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  926. {
  927. if (flag != 0) {
  928. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  929. " reordering not implemented\n", dev);
  930. return -ENOSYS;
  931. }
  932. dev->dev_attrib.emulate_rest_reord = flag;
  933. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  934. return 0;
  935. }
  936. EXPORT_SYMBOL(se_dev_set_emulate_rest_reord);
  937. /*
  938. * Note, this can only be called on unexported SE Device Object.
  939. */
  940. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  941. {
  942. if (dev->export_count) {
  943. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  944. " export_count is %d\n",
  945. dev, dev->export_count);
  946. return -EINVAL;
  947. }
  948. if (!queue_depth) {
  949. pr_err("dev[%p]: Illegal ZERO value for queue"
  950. "_depth\n", dev);
  951. return -EINVAL;
  952. }
  953. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  954. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  955. pr_err("dev[%p]: Passed queue_depth: %u"
  956. " exceeds TCM/SE_Device TCQ: %u\n",
  957. dev, queue_depth,
  958. dev->dev_attrib.hw_queue_depth);
  959. return -EINVAL;
  960. }
  961. } else {
  962. if (queue_depth > dev->dev_attrib.queue_depth) {
  963. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  964. pr_err("dev[%p]: Passed queue_depth:"
  965. " %u exceeds TCM/SE_Device MAX"
  966. " TCQ: %u\n", dev, queue_depth,
  967. dev->dev_attrib.hw_queue_depth);
  968. return -EINVAL;
  969. }
  970. }
  971. }
  972. dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  973. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  974. dev, queue_depth);
  975. return 0;
  976. }
  977. EXPORT_SYMBOL(se_dev_set_queue_depth);
  978. int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
  979. {
  980. int block_size = dev->dev_attrib.block_size;
  981. if (dev->export_count) {
  982. pr_err("dev[%p]: Unable to change SE Device"
  983. " fabric_max_sectors while export_count is %d\n",
  984. dev, dev->export_count);
  985. return -EINVAL;
  986. }
  987. if (!fabric_max_sectors) {
  988. pr_err("dev[%p]: Illegal ZERO value for"
  989. " fabric_max_sectors\n", dev);
  990. return -EINVAL;
  991. }
  992. if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  993. pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
  994. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
  995. DA_STATUS_MAX_SECTORS_MIN);
  996. return -EINVAL;
  997. }
  998. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  999. if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
  1000. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  1001. " greater than TCM/SE_Device max_sectors:"
  1002. " %u\n", dev, fabric_max_sectors,
  1003. dev->dev_attrib.hw_max_sectors);
  1004. return -EINVAL;
  1005. }
  1006. } else {
  1007. if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  1008. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  1009. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  1010. " %u\n", dev, fabric_max_sectors,
  1011. DA_STATUS_MAX_SECTORS_MAX);
  1012. return -EINVAL;
  1013. }
  1014. }
  1015. /*
  1016. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  1017. */
  1018. if (!block_size) {
  1019. block_size = 512;
  1020. pr_warn("Defaulting to 512 for zero block_size\n");
  1021. }
  1022. fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
  1023. block_size);
  1024. dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
  1025. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  1026. dev, fabric_max_sectors);
  1027. return 0;
  1028. }
  1029. EXPORT_SYMBOL(se_dev_set_fabric_max_sectors);
  1030. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  1031. {
  1032. if (dev->export_count) {
  1033. pr_err("dev[%p]: Unable to change SE Device"
  1034. " optimal_sectors while export_count is %d\n",
  1035. dev, dev->export_count);
  1036. return -EINVAL;
  1037. }
  1038. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1039. pr_err("dev[%p]: Passed optimal_sectors cannot be"
  1040. " changed for TCM/pSCSI\n", dev);
  1041. return -EINVAL;
  1042. }
  1043. if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
  1044. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  1045. " greater than fabric_max_sectors: %u\n", dev,
  1046. optimal_sectors, dev->dev_attrib.fabric_max_sectors);
  1047. return -EINVAL;
  1048. }
  1049. dev->dev_attrib.optimal_sectors = optimal_sectors;
  1050. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  1051. dev, optimal_sectors);
  1052. return 0;
  1053. }
  1054. EXPORT_SYMBOL(se_dev_set_optimal_sectors);
  1055. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  1056. {
  1057. if (dev->export_count) {
  1058. pr_err("dev[%p]: Unable to change SE Device block_size"
  1059. " while export_count is %d\n",
  1060. dev, dev->export_count);
  1061. return -EINVAL;
  1062. }
  1063. if ((block_size != 512) &&
  1064. (block_size != 1024) &&
  1065. (block_size != 2048) &&
  1066. (block_size != 4096)) {
  1067. pr_err("dev[%p]: Illegal value for block_device: %u"
  1068. " for SE device, must be 512, 1024, 2048 or 4096\n",
  1069. dev, block_size);
  1070. return -EINVAL;
  1071. }
  1072. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1073. pr_err("dev[%p]: Not allowed to change block_size for"
  1074. " Physical Device, use for Linux/SCSI to change"
  1075. " block_size for underlying hardware\n", dev);
  1076. return -EINVAL;
  1077. }
  1078. dev->dev_attrib.block_size = block_size;
  1079. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  1080. dev, block_size);
  1081. if (dev->dev_attrib.max_bytes_per_io)
  1082. dev->dev_attrib.hw_max_sectors =
  1083. dev->dev_attrib.max_bytes_per_io / block_size;
  1084. return 0;
  1085. }
  1086. EXPORT_SYMBOL(se_dev_set_block_size);
  1087. struct se_lun *core_dev_add_lun(
  1088. struct se_portal_group *tpg,
  1089. struct se_device *dev,
  1090. u32 unpacked_lun)
  1091. {
  1092. struct se_lun *lun;
  1093. int rc;
  1094. lun = core_tpg_alloc_lun(tpg, unpacked_lun);
  1095. if (IS_ERR(lun))
  1096. return lun;
  1097. rc = core_tpg_add_lun(tpg, lun,
  1098. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  1099. if (rc < 0)
  1100. return ERR_PTR(rc);
  1101. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  1102. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1103. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1104. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  1105. /*
  1106. * Update LUN maps for dynamically added initiators when
  1107. * generate_node_acl is enabled.
  1108. */
  1109. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  1110. struct se_node_acl *acl;
  1111. spin_lock_irq(&tpg->acl_node_lock);
  1112. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  1113. if (acl->dynamic_node_acl &&
  1114. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  1115. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  1116. spin_unlock_irq(&tpg->acl_node_lock);
  1117. core_tpg_add_node_to_devs(acl, tpg);
  1118. spin_lock_irq(&tpg->acl_node_lock);
  1119. }
  1120. }
  1121. spin_unlock_irq(&tpg->acl_node_lock);
  1122. }
  1123. return lun;
  1124. }
  1125. /* core_dev_del_lun():
  1126. *
  1127. *
  1128. */
  1129. void core_dev_del_lun(
  1130. struct se_portal_group *tpg,
  1131. struct se_lun *lun)
  1132. {
  1133. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivating %s Logical Unit from"
  1134. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  1135. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1136. tpg->se_tpg_tfo->get_fabric_name());
  1137. core_tpg_remove_lun(tpg, lun);
  1138. }
  1139. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  1140. {
  1141. struct se_lun *lun;
  1142. spin_lock(&tpg->tpg_lun_lock);
  1143. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1144. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  1145. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  1146. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1147. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1148. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1149. spin_unlock(&tpg->tpg_lun_lock);
  1150. return NULL;
  1151. }
  1152. lun = tpg->tpg_lun_list[unpacked_lun];
  1153. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  1154. pr_err("%s Logical Unit Number: %u is not free on"
  1155. " Target Portal Group: %hu, ignoring request.\n",
  1156. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1157. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1158. spin_unlock(&tpg->tpg_lun_lock);
  1159. return NULL;
  1160. }
  1161. spin_unlock(&tpg->tpg_lun_lock);
  1162. return lun;
  1163. }
  1164. /* core_dev_get_lun():
  1165. *
  1166. *
  1167. */
  1168. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1169. {
  1170. struct se_lun *lun;
  1171. spin_lock(&tpg->tpg_lun_lock);
  1172. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1173. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1174. "_TPG-1: %u for Target Portal Group: %hu\n",
  1175. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1176. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1177. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1178. spin_unlock(&tpg->tpg_lun_lock);
  1179. return NULL;
  1180. }
  1181. lun = tpg->tpg_lun_list[unpacked_lun];
  1182. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1183. pr_err("%s Logical Unit Number: %u is not active on"
  1184. " Target Portal Group: %hu, ignoring request.\n",
  1185. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1186. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1187. spin_unlock(&tpg->tpg_lun_lock);
  1188. return NULL;
  1189. }
  1190. spin_unlock(&tpg->tpg_lun_lock);
  1191. return lun;
  1192. }
  1193. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1194. struct se_portal_group *tpg,
  1195. struct se_node_acl *nacl,
  1196. u32 mapped_lun,
  1197. int *ret)
  1198. {
  1199. struct se_lun_acl *lacl;
  1200. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  1201. pr_err("%s InitiatorName exceeds maximum size.\n",
  1202. tpg->se_tpg_tfo->get_fabric_name());
  1203. *ret = -EOVERFLOW;
  1204. return NULL;
  1205. }
  1206. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1207. if (!lacl) {
  1208. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1209. *ret = -ENOMEM;
  1210. return NULL;
  1211. }
  1212. INIT_LIST_HEAD(&lacl->lacl_list);
  1213. lacl->mapped_lun = mapped_lun;
  1214. lacl->se_lun_nacl = nacl;
  1215. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  1216. nacl->initiatorname);
  1217. return lacl;
  1218. }
  1219. int core_dev_add_initiator_node_lun_acl(
  1220. struct se_portal_group *tpg,
  1221. struct se_lun_acl *lacl,
  1222. u32 unpacked_lun,
  1223. u32 lun_access)
  1224. {
  1225. struct se_lun *lun;
  1226. struct se_node_acl *nacl;
  1227. lun = core_dev_get_lun(tpg, unpacked_lun);
  1228. if (!lun) {
  1229. pr_err("%s Logical Unit Number: %u is not active on"
  1230. " Target Portal Group: %hu, ignoring request.\n",
  1231. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1232. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1233. return -EINVAL;
  1234. }
  1235. nacl = lacl->se_lun_nacl;
  1236. if (!nacl)
  1237. return -EINVAL;
  1238. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1239. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1240. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1241. lacl->se_lun = lun;
  1242. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1243. lun_access, nacl, tpg) < 0)
  1244. return -EINVAL;
  1245. spin_lock(&lun->lun_acl_lock);
  1246. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1247. atomic_inc_mb(&lun->lun_acl_count);
  1248. spin_unlock(&lun->lun_acl_lock);
  1249. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1250. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1251. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1252. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1253. lacl->initiatorname);
  1254. /*
  1255. * Check to see if there are any existing persistent reservation APTPL
  1256. * pre-registrations that need to be enabled for this LUN ACL..
  1257. */
  1258. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, nacl,
  1259. lacl->mapped_lun);
  1260. return 0;
  1261. }
  1262. /* core_dev_del_initiator_node_lun_acl():
  1263. *
  1264. *
  1265. */
  1266. int core_dev_del_initiator_node_lun_acl(
  1267. struct se_portal_group *tpg,
  1268. struct se_lun *lun,
  1269. struct se_lun_acl *lacl)
  1270. {
  1271. struct se_node_acl *nacl;
  1272. nacl = lacl->se_lun_nacl;
  1273. if (!nacl)
  1274. return -EINVAL;
  1275. spin_lock(&lun->lun_acl_lock);
  1276. list_del(&lacl->lacl_list);
  1277. atomic_dec_mb(&lun->lun_acl_count);
  1278. spin_unlock(&lun->lun_acl_lock);
  1279. core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1280. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  1281. lacl->se_lun = NULL;
  1282. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1283. " InitiatorNode: %s Mapped LUN: %u\n",
  1284. tpg->se_tpg_tfo->get_fabric_name(),
  1285. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1286. lacl->initiatorname, lacl->mapped_lun);
  1287. return 0;
  1288. }
  1289. void core_dev_free_initiator_node_lun_acl(
  1290. struct se_portal_group *tpg,
  1291. struct se_lun_acl *lacl)
  1292. {
  1293. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1294. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1295. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1296. tpg->se_tpg_tfo->get_fabric_name(),
  1297. lacl->initiatorname, lacl->mapped_lun);
  1298. kfree(lacl);
  1299. }
  1300. static void scsi_dump_inquiry(struct se_device *dev)
  1301. {
  1302. struct t10_wwn *wwn = &dev->t10_wwn;
  1303. char buf[17];
  1304. int i, device_type;
  1305. /*
  1306. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1307. */
  1308. for (i = 0; i < 8; i++)
  1309. if (wwn->vendor[i] >= 0x20)
  1310. buf[i] = wwn->vendor[i];
  1311. else
  1312. buf[i] = ' ';
  1313. buf[i] = '\0';
  1314. pr_debug(" Vendor: %s\n", buf);
  1315. for (i = 0; i < 16; i++)
  1316. if (wwn->model[i] >= 0x20)
  1317. buf[i] = wwn->model[i];
  1318. else
  1319. buf[i] = ' ';
  1320. buf[i] = '\0';
  1321. pr_debug(" Model: %s\n", buf);
  1322. for (i = 0; i < 4; i++)
  1323. if (wwn->revision[i] >= 0x20)
  1324. buf[i] = wwn->revision[i];
  1325. else
  1326. buf[i] = ' ';
  1327. buf[i] = '\0';
  1328. pr_debug(" Revision: %s\n", buf);
  1329. device_type = dev->transport->get_device_type(dev);
  1330. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1331. }
  1332. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  1333. {
  1334. struct se_device *dev;
  1335. struct se_lun *xcopy_lun;
  1336. dev = hba->transport->alloc_device(hba, name);
  1337. if (!dev)
  1338. return NULL;
  1339. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1340. dev->se_hba = hba;
  1341. dev->transport = hba->transport;
  1342. dev->prot_length = sizeof(struct se_dif_v1_tuple);
  1343. INIT_LIST_HEAD(&dev->dev_list);
  1344. INIT_LIST_HEAD(&dev->dev_sep_list);
  1345. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1346. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1347. INIT_LIST_HEAD(&dev->state_list);
  1348. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1349. INIT_LIST_HEAD(&dev->g_dev_node);
  1350. spin_lock_init(&dev->execute_task_lock);
  1351. spin_lock_init(&dev->delayed_cmd_lock);
  1352. spin_lock_init(&dev->dev_reservation_lock);
  1353. spin_lock_init(&dev->se_port_lock);
  1354. spin_lock_init(&dev->se_tmr_lock);
  1355. spin_lock_init(&dev->qf_cmd_lock);
  1356. sema_init(&dev->caw_sem, 1);
  1357. atomic_set(&dev->dev_ordered_id, 0);
  1358. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  1359. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  1360. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  1361. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  1362. spin_lock_init(&dev->t10_pr.registration_lock);
  1363. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  1364. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  1365. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  1366. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  1367. spin_lock_init(&dev->t10_alua.lba_map_lock);
  1368. dev->t10_wwn.t10_dev = dev;
  1369. dev->t10_alua.t10_dev = dev;
  1370. dev->dev_attrib.da_dev = dev;
  1371. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  1372. dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  1373. dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  1374. dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  1375. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  1376. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  1377. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  1378. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  1379. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  1380. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  1381. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  1382. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  1383. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  1384. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  1385. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  1386. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  1387. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  1388. dev->dev_attrib.max_unmap_block_desc_count =
  1389. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  1390. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  1391. dev->dev_attrib.unmap_granularity_alignment =
  1392. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  1393. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  1394. dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
  1395. dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
  1396. xcopy_lun = &dev->xcopy_lun;
  1397. xcopy_lun->lun_se_dev = dev;
  1398. init_completion(&xcopy_lun->lun_shutdown_comp);
  1399. INIT_LIST_HEAD(&xcopy_lun->lun_acl_list);
  1400. spin_lock_init(&xcopy_lun->lun_acl_lock);
  1401. spin_lock_init(&xcopy_lun->lun_sep_lock);
  1402. init_completion(&xcopy_lun->lun_ref_comp);
  1403. return dev;
  1404. }
  1405. int target_configure_device(struct se_device *dev)
  1406. {
  1407. struct se_hba *hba = dev->se_hba;
  1408. int ret;
  1409. if (dev->dev_flags & DF_CONFIGURED) {
  1410. pr_err("se_dev->se_dev_ptr already set for storage"
  1411. " object\n");
  1412. return -EEXIST;
  1413. }
  1414. ret = dev->transport->configure_device(dev);
  1415. if (ret)
  1416. goto out;
  1417. dev->dev_flags |= DF_CONFIGURED;
  1418. /*
  1419. * XXX: there is not much point to have two different values here..
  1420. */
  1421. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  1422. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  1423. /*
  1424. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  1425. */
  1426. dev->dev_attrib.hw_max_sectors =
  1427. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  1428. dev->dev_attrib.hw_block_size);
  1429. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1430. dev->creation_time = get_jiffies_64();
  1431. ret = core_setup_alua(dev);
  1432. if (ret)
  1433. goto out;
  1434. /*
  1435. * Startup the struct se_device processing thread
  1436. */
  1437. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  1438. dev->transport->name);
  1439. if (!dev->tmr_wq) {
  1440. pr_err("Unable to create tmr workqueue for %s\n",
  1441. dev->transport->name);
  1442. ret = -ENOMEM;
  1443. goto out_free_alua;
  1444. }
  1445. /*
  1446. * Setup work_queue for QUEUE_FULL
  1447. */
  1448. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1449. /*
  1450. * Preload the initial INQUIRY const values if we are doing
  1451. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1452. * passthrough because this is being provided by the backend LLD.
  1453. */
  1454. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1455. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1456. strncpy(&dev->t10_wwn.model[0],
  1457. dev->transport->inquiry_prod, 16);
  1458. strncpy(&dev->t10_wwn.revision[0],
  1459. dev->transport->inquiry_rev, 4);
  1460. }
  1461. scsi_dump_inquiry(dev);
  1462. spin_lock(&hba->device_lock);
  1463. hba->dev_count++;
  1464. spin_unlock(&hba->device_lock);
  1465. mutex_lock(&g_device_mutex);
  1466. list_add_tail(&dev->g_dev_node, &g_device_list);
  1467. mutex_unlock(&g_device_mutex);
  1468. return 0;
  1469. out_free_alua:
  1470. core_alua_free_lu_gp_mem(dev);
  1471. out:
  1472. se_release_vpd_for_dev(dev);
  1473. return ret;
  1474. }
  1475. void target_free_device(struct se_device *dev)
  1476. {
  1477. struct se_hba *hba = dev->se_hba;
  1478. WARN_ON(!list_empty(&dev->dev_sep_list));
  1479. if (dev->dev_flags & DF_CONFIGURED) {
  1480. destroy_workqueue(dev->tmr_wq);
  1481. mutex_lock(&g_device_mutex);
  1482. list_del(&dev->g_dev_node);
  1483. mutex_unlock(&g_device_mutex);
  1484. spin_lock(&hba->device_lock);
  1485. hba->dev_count--;
  1486. spin_unlock(&hba->device_lock);
  1487. }
  1488. core_alua_free_lu_gp_mem(dev);
  1489. core_alua_set_lba_map(dev, NULL, 0, 0);
  1490. core_scsi3_free_all_registrations(dev);
  1491. se_release_vpd_for_dev(dev);
  1492. if (dev->transport->free_prot)
  1493. dev->transport->free_prot(dev);
  1494. dev->transport->free_device(dev);
  1495. }
  1496. int core_dev_setup_virtual_lun0(void)
  1497. {
  1498. struct se_hba *hba;
  1499. struct se_device *dev;
  1500. char buf[] = "rd_pages=8,rd_nullio=1";
  1501. int ret;
  1502. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1503. if (IS_ERR(hba))
  1504. return PTR_ERR(hba);
  1505. dev = target_alloc_device(hba, "virt_lun0");
  1506. if (!dev) {
  1507. ret = -ENOMEM;
  1508. goto out_free_hba;
  1509. }
  1510. hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
  1511. ret = target_configure_device(dev);
  1512. if (ret)
  1513. goto out_free_se_dev;
  1514. lun0_hba = hba;
  1515. g_lun0_dev = dev;
  1516. return 0;
  1517. out_free_se_dev:
  1518. target_free_device(dev);
  1519. out_free_hba:
  1520. core_delete_hba(hba);
  1521. return ret;
  1522. }
  1523. void core_dev_release_virtual_lun0(void)
  1524. {
  1525. struct se_hba *hba = lun0_hba;
  1526. if (!hba)
  1527. return;
  1528. if (g_lun0_dev)
  1529. target_free_device(g_lun0_dev);
  1530. core_delete_hba(hba);
  1531. }