target_core_device.c 41 KB

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