target_core_alua.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*******************************************************************************
  2. * Filename: target_core_alua.c
  3. *
  4. * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
  5. *
  6. * Copyright (c) 2009-2010 Rising Tide Systems
  7. * Copyright (c) 2009-2010 Linux-iSCSI.org
  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/version.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/configfs.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <target/target_core_base.h>
  33. #include <target/target_core_device.h>
  34. #include <target/target_core_transport.h>
  35. #include <target/target_core_fabric_ops.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_alua.h"
  38. #include "target_core_hba.h"
  39. #include "target_core_ua.h"
  40. static int core_alua_check_transition(int state, int *primary);
  41. static int core_alua_set_tg_pt_secondary_state(
  42. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  43. struct se_port *port, int explict, int offline);
  44. /*
  45. * REPORT_TARGET_PORT_GROUPS
  46. *
  47. * See spc4r17 section 6.27
  48. */
  49. int core_emulate_report_target_port_groups(struct se_cmd *cmd)
  50. {
  51. struct se_subsystem_dev *su_dev = SE_DEV(cmd)->se_sub_dev;
  52. struct se_port *port;
  53. struct t10_alua_tg_pt_gp *tg_pt_gp;
  54. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  55. unsigned char *buf = (unsigned char *)T_TASK(cmd)->t_task_buf;
  56. u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
  57. Target port group descriptor */
  58. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  59. list_for_each_entry(tg_pt_gp, &T10_ALUA(su_dev)->tg_pt_gps_list,
  60. tg_pt_gp_list) {
  61. /*
  62. * PREF: Preferred target port bit, determine if this
  63. * bit should be set for port group.
  64. */
  65. if (tg_pt_gp->tg_pt_gp_pref)
  66. buf[off] = 0x80;
  67. /*
  68. * Set the ASYMMETRIC ACCESS State
  69. */
  70. buf[off++] |= (atomic_read(
  71. &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  72. /*
  73. * Set supported ASYMMETRIC ACCESS State bits
  74. */
  75. buf[off] = 0x80; /* T_SUP */
  76. buf[off] |= 0x40; /* O_SUP */
  77. buf[off] |= 0x8; /* U_SUP */
  78. buf[off] |= 0x4; /* S_SUP */
  79. buf[off] |= 0x2; /* AN_SUP */
  80. buf[off++] |= 0x1; /* AO_SUP */
  81. /*
  82. * TARGET PORT GROUP
  83. */
  84. buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  85. buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  86. off++; /* Skip over Reserved */
  87. /*
  88. * STATUS CODE
  89. */
  90. buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  91. /*
  92. * Vendor Specific field
  93. */
  94. buf[off++] = 0x00;
  95. /*
  96. * TARGET PORT COUNT
  97. */
  98. buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  99. rd_len += 8;
  100. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  101. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  102. tg_pt_gp_mem_list) {
  103. port = tg_pt_gp_mem->tg_pt;
  104. /*
  105. * Start Target Port descriptor format
  106. *
  107. * See spc4r17 section 6.2.7 Table 247
  108. */
  109. off += 2; /* Skip over Obsolete */
  110. /*
  111. * Set RELATIVE TARGET PORT IDENTIFIER
  112. */
  113. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  114. buf[off++] = (port->sep_rtpi & 0xff);
  115. rd_len += 4;
  116. }
  117. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  118. }
  119. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  120. /*
  121. * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  122. */
  123. buf[0] = ((rd_len >> 24) & 0xff);
  124. buf[1] = ((rd_len >> 16) & 0xff);
  125. buf[2] = ((rd_len >> 8) & 0xff);
  126. buf[3] = (rd_len & 0xff);
  127. return 0;
  128. }
  129. /*
  130. * SET_TARGET_PORT_GROUPS for explict ALUA operation.
  131. *
  132. * See spc4r17 section 6.35
  133. */
  134. int core_emulate_set_target_port_groups(struct se_cmd *cmd)
  135. {
  136. struct se_device *dev = SE_DEV(cmd);
  137. struct se_subsystem_dev *su_dev = SE_DEV(cmd)->se_sub_dev;
  138. struct se_port *port, *l_port = SE_LUN(cmd)->lun_sep;
  139. struct se_node_acl *nacl = SE_SESS(cmd)->se_node_acl;
  140. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  141. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
  142. unsigned char *buf = (unsigned char *)T_TASK(cmd)->t_task_buf;
  143. unsigned char *ptr = &buf[4]; /* Skip over RESERVED area in header */
  144. u32 len = 4; /* Skip over RESERVED area in header */
  145. int alua_access_state, primary = 0, rc;
  146. u16 tg_pt_id, rtpi;
  147. if (!(l_port))
  148. return PYX_TRANSPORT_LU_COMM_FAILURE;
  149. /*
  150. * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  151. * for the local tg_pt_gp.
  152. */
  153. l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
  154. if (!(l_tg_pt_gp_mem)) {
  155. printk(KERN_ERR "Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
  156. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  157. }
  158. spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  159. l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
  160. if (!(l_tg_pt_gp)) {
  161. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  162. printk(KERN_ERR "Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
  163. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  164. }
  165. rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  166. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  167. if (!(rc)) {
  168. printk(KERN_INFO "Unable to process SET_TARGET_PORT_GROUPS"
  169. " while TPGS_EXPLICT_ALUA is disabled\n");
  170. return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
  171. }
  172. while (len < cmd->data_length) {
  173. alua_access_state = (ptr[0] & 0x0f);
  174. /*
  175. * Check the received ALUA access state, and determine if
  176. * the state is a primary or secondary target port asymmetric
  177. * access state.
  178. */
  179. rc = core_alua_check_transition(alua_access_state, &primary);
  180. if (rc != 0) {
  181. /*
  182. * If the SET TARGET PORT GROUPS attempts to establish
  183. * an invalid combination of target port asymmetric
  184. * access states or attempts to establish an
  185. * unsupported target port asymmetric access state,
  186. * then the command shall be terminated with CHECK
  187. * CONDITION status, with the sense key set to ILLEGAL
  188. * REQUEST, and the additional sense code set to INVALID
  189. * FIELD IN PARAMETER LIST.
  190. */
  191. return PYX_TRANSPORT_INVALID_PARAMETER_LIST;
  192. }
  193. rc = -1;
  194. /*
  195. * If the ASYMMETRIC ACCESS STATE field (see table 267)
  196. * specifies a primary target port asymmetric access state,
  197. * then the TARGET PORT GROUP OR TARGET PORT field specifies
  198. * a primary target port group for which the primary target
  199. * port asymmetric access state shall be changed. If the
  200. * ASYMMETRIC ACCESS STATE field specifies a secondary target
  201. * port asymmetric access state, then the TARGET PORT GROUP OR
  202. * TARGET PORT field specifies the relative target port
  203. * identifier (see 3.1.120) of the target port for which the
  204. * secondary target port asymmetric access state shall be
  205. * changed.
  206. */
  207. if (primary) {
  208. tg_pt_id = ((ptr[2] << 8) & 0xff);
  209. tg_pt_id |= (ptr[3] & 0xff);
  210. /*
  211. * Locate the matching target port group ID from
  212. * the global tg_pt_gp list
  213. */
  214. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  215. list_for_each_entry(tg_pt_gp,
  216. &T10_ALUA(su_dev)->tg_pt_gps_list,
  217. tg_pt_gp_list) {
  218. if (!(tg_pt_gp->tg_pt_gp_valid_id))
  219. continue;
  220. if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  221. continue;
  222. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  223. smp_mb__after_atomic_inc();
  224. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  225. rc = core_alua_do_port_transition(tg_pt_gp,
  226. dev, l_port, nacl,
  227. alua_access_state, 1);
  228. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  229. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  230. smp_mb__after_atomic_dec();
  231. break;
  232. }
  233. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  234. /*
  235. * If not matching target port group ID can be located
  236. * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  237. */
  238. if (rc != 0)
  239. return PYX_TRANSPORT_INVALID_PARAMETER_LIST;
  240. } else {
  241. /*
  242. * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  243. * the Target Port in question for the the incoming
  244. * SET_TARGET_PORT_GROUPS op.
  245. */
  246. rtpi = ((ptr[2] << 8) & 0xff);
  247. rtpi |= (ptr[3] & 0xff);
  248. /*
  249. * Locate the matching relative target port identifer
  250. * for the struct se_device storage object.
  251. */
  252. spin_lock(&dev->se_port_lock);
  253. list_for_each_entry(port, &dev->dev_sep_list,
  254. sep_list) {
  255. if (port->sep_rtpi != rtpi)
  256. continue;
  257. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  258. spin_unlock(&dev->se_port_lock);
  259. rc = core_alua_set_tg_pt_secondary_state(
  260. tg_pt_gp_mem, port, 1, 1);
  261. spin_lock(&dev->se_port_lock);
  262. break;
  263. }
  264. spin_unlock(&dev->se_port_lock);
  265. /*
  266. * If not matching relative target port identifier can
  267. * be located, throw an exception with ASCQ:
  268. * INVALID_PARAMETER_LIST
  269. */
  270. if (rc != 0)
  271. return PYX_TRANSPORT_INVALID_PARAMETER_LIST;
  272. }
  273. ptr += 4;
  274. len += 4;
  275. }
  276. return 0;
  277. }
  278. static inline int core_alua_state_nonoptimized(
  279. struct se_cmd *cmd,
  280. unsigned char *cdb,
  281. int nonop_delay_msecs,
  282. u8 *alua_ascq)
  283. {
  284. /*
  285. * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  286. * later to determine if processing of this cmd needs to be
  287. * temporarily delayed for the Active/NonOptimized primary access state.
  288. */
  289. cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  290. cmd->alua_nonop_delay = nonop_delay_msecs;
  291. return 0;
  292. }
  293. static inline int core_alua_state_standby(
  294. struct se_cmd *cmd,
  295. unsigned char *cdb,
  296. u8 *alua_ascq)
  297. {
  298. /*
  299. * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  300. * spc4r17 section 5.9.2.4.4
  301. */
  302. switch (cdb[0]) {
  303. case INQUIRY:
  304. case LOG_SELECT:
  305. case LOG_SENSE:
  306. case MODE_SELECT:
  307. case MODE_SENSE:
  308. case REPORT_LUNS:
  309. case RECEIVE_DIAGNOSTIC:
  310. case SEND_DIAGNOSTIC:
  311. case MAINTENANCE_IN:
  312. switch (cdb[1]) {
  313. case MI_REPORT_TARGET_PGS:
  314. return 0;
  315. default:
  316. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  317. return 1;
  318. }
  319. case MAINTENANCE_OUT:
  320. switch (cdb[1]) {
  321. case MO_SET_TARGET_PGS:
  322. return 0;
  323. default:
  324. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  325. return 1;
  326. }
  327. case REQUEST_SENSE:
  328. case PERSISTENT_RESERVE_IN:
  329. case PERSISTENT_RESERVE_OUT:
  330. case READ_BUFFER:
  331. case WRITE_BUFFER:
  332. return 0;
  333. default:
  334. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  335. return 1;
  336. }
  337. return 0;
  338. }
  339. static inline int core_alua_state_unavailable(
  340. struct se_cmd *cmd,
  341. unsigned char *cdb,
  342. u8 *alua_ascq)
  343. {
  344. /*
  345. * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  346. * spc4r17 section 5.9.2.4.5
  347. */
  348. switch (cdb[0]) {
  349. case INQUIRY:
  350. case REPORT_LUNS:
  351. case MAINTENANCE_IN:
  352. switch (cdb[1]) {
  353. case MI_REPORT_TARGET_PGS:
  354. return 0;
  355. default:
  356. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  357. return 1;
  358. }
  359. case MAINTENANCE_OUT:
  360. switch (cdb[1]) {
  361. case MO_SET_TARGET_PGS:
  362. return 0;
  363. default:
  364. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  365. return 1;
  366. }
  367. case REQUEST_SENSE:
  368. case READ_BUFFER:
  369. case WRITE_BUFFER:
  370. return 0;
  371. default:
  372. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. static inline int core_alua_state_transition(
  378. struct se_cmd *cmd,
  379. unsigned char *cdb,
  380. u8 *alua_ascq)
  381. {
  382. /*
  383. * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  384. * spc4r17 section 5.9.2.5
  385. */
  386. switch (cdb[0]) {
  387. case INQUIRY:
  388. case REPORT_LUNS:
  389. case MAINTENANCE_IN:
  390. switch (cdb[1]) {
  391. case MI_REPORT_TARGET_PGS:
  392. return 0;
  393. default:
  394. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  395. return 1;
  396. }
  397. case REQUEST_SENSE:
  398. case READ_BUFFER:
  399. case WRITE_BUFFER:
  400. return 0;
  401. default:
  402. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  403. return 1;
  404. }
  405. return 0;
  406. }
  407. /*
  408. * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
  409. * in transport_cmd_sequencer(). This function is assigned to
  410. * struct t10_alua *->state_check() in core_setup_alua()
  411. */
  412. static int core_alua_state_check_nop(
  413. struct se_cmd *cmd,
  414. unsigned char *cdb,
  415. u8 *alua_ascq)
  416. {
  417. return 0;
  418. }
  419. /*
  420. * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
  421. * This function is assigned to struct t10_alua *->state_check() in
  422. * core_setup_alua()
  423. *
  424. * Also, this function can return three different return codes to
  425. * signal transport_generic_cmd_sequencer()
  426. *
  427. * return 1: Is used to signal LUN not accecsable, and check condition/not ready
  428. * return 0: Used to signal success
  429. * reutrn -1: Used to signal failure, and invalid cdb field
  430. */
  431. static int core_alua_state_check(
  432. struct se_cmd *cmd,
  433. unsigned char *cdb,
  434. u8 *alua_ascq)
  435. {
  436. struct se_lun *lun = SE_LUN(cmd);
  437. struct se_port *port = lun->lun_sep;
  438. struct t10_alua_tg_pt_gp *tg_pt_gp;
  439. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  440. int out_alua_state, nonop_delay_msecs;
  441. if (!(port))
  442. return 0;
  443. /*
  444. * First, check for a struct se_port specific secondary ALUA target port
  445. * access state: OFFLINE
  446. */
  447. if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  448. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  449. printk(KERN_INFO "ALUA: Got secondary offline status for local"
  450. " target port\n");
  451. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  452. return 1;
  453. }
  454. /*
  455. * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  456. * ALUA target port group, to obtain current ALUA access state.
  457. * Otherwise look for the underlying struct se_device association with
  458. * a ALUA logical unit group.
  459. */
  460. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  461. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  462. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  463. out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  464. nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  465. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  466. /*
  467. * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a seperate conditional
  468. * statement so the complier knows explictly to check this case first.
  469. * For the Optimized ALUA access state case, we want to process the
  470. * incoming fabric cmd ASAP..
  471. */
  472. if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  473. return 0;
  474. switch (out_alua_state) {
  475. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  476. return core_alua_state_nonoptimized(cmd, cdb,
  477. nonop_delay_msecs, alua_ascq);
  478. case ALUA_ACCESS_STATE_STANDBY:
  479. return core_alua_state_standby(cmd, cdb, alua_ascq);
  480. case ALUA_ACCESS_STATE_UNAVAILABLE:
  481. return core_alua_state_unavailable(cmd, cdb, alua_ascq);
  482. case ALUA_ACCESS_STATE_TRANSITION:
  483. return core_alua_state_transition(cmd, cdb, alua_ascq);
  484. /*
  485. * OFFLINE is a secondary ALUA target port group access state, that is
  486. * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  487. */
  488. case ALUA_ACCESS_STATE_OFFLINE:
  489. default:
  490. printk(KERN_ERR "Unknown ALUA access state: 0x%02x\n",
  491. out_alua_state);
  492. return -1;
  493. }
  494. return 0;
  495. }
  496. /*
  497. * Check implict and explict ALUA state change request.
  498. */
  499. static int core_alua_check_transition(int state, int *primary)
  500. {
  501. switch (state) {
  502. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  503. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  504. case ALUA_ACCESS_STATE_STANDBY:
  505. case ALUA_ACCESS_STATE_UNAVAILABLE:
  506. /*
  507. * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  508. * defined as primary target port asymmetric access states.
  509. */
  510. *primary = 1;
  511. break;
  512. case ALUA_ACCESS_STATE_OFFLINE:
  513. /*
  514. * OFFLINE state is defined as a secondary target port
  515. * asymmetric access state.
  516. */
  517. *primary = 0;
  518. break;
  519. default:
  520. printk(KERN_ERR "Unknown ALUA access state: 0x%02x\n", state);
  521. return -1;
  522. }
  523. return 0;
  524. }
  525. static char *core_alua_dump_state(int state)
  526. {
  527. switch (state) {
  528. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  529. return "Active/Optimized";
  530. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  531. return "Active/NonOptimized";
  532. case ALUA_ACCESS_STATE_STANDBY:
  533. return "Standby";
  534. case ALUA_ACCESS_STATE_UNAVAILABLE:
  535. return "Unavailable";
  536. case ALUA_ACCESS_STATE_OFFLINE:
  537. return "Offline";
  538. default:
  539. return "Unknown";
  540. }
  541. return NULL;
  542. }
  543. char *core_alua_dump_status(int status)
  544. {
  545. switch (status) {
  546. case ALUA_STATUS_NONE:
  547. return "None";
  548. case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  549. return "Altered by Explict STPG";
  550. case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  551. return "Altered by Implict ALUA";
  552. default:
  553. return "Unknown";
  554. }
  555. return NULL;
  556. }
  557. /*
  558. * Used by fabric modules to determine when we need to delay processing
  559. * for the Active/NonOptimized paths..
  560. */
  561. int core_alua_check_nonop_delay(
  562. struct se_cmd *cmd)
  563. {
  564. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  565. return 0;
  566. if (in_interrupt())
  567. return 0;
  568. /*
  569. * The ALUA Active/NonOptimized access state delay can be disabled
  570. * in via configfs with a value of zero
  571. */
  572. if (!(cmd->alua_nonop_delay))
  573. return 0;
  574. /*
  575. * struct se_cmd->alua_nonop_delay gets set by a target port group
  576. * defined interval in core_alua_state_nonoptimized()
  577. */
  578. msleep_interruptible(cmd->alua_nonop_delay);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  582. /*
  583. * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
  584. *
  585. */
  586. static int core_alua_write_tpg_metadata(
  587. const char *path,
  588. unsigned char *md_buf,
  589. u32 md_buf_len)
  590. {
  591. mm_segment_t old_fs;
  592. struct file *file;
  593. struct iovec iov[1];
  594. int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  595. memset(iov, 0, sizeof(struct iovec));
  596. file = filp_open(path, flags, 0600);
  597. if (IS_ERR(file) || !file || !file->f_dentry) {
  598. printk(KERN_ERR "filp_open(%s) for ALUA metadata failed\n",
  599. path);
  600. return -ENODEV;
  601. }
  602. iov[0].iov_base = &md_buf[0];
  603. iov[0].iov_len = md_buf_len;
  604. old_fs = get_fs();
  605. set_fs(get_ds());
  606. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  607. set_fs(old_fs);
  608. if (ret < 0) {
  609. printk(KERN_ERR "Error writing ALUA metadata file: %s\n", path);
  610. filp_close(file, NULL);
  611. return -EIO;
  612. }
  613. filp_close(file, NULL);
  614. return 0;
  615. }
  616. /*
  617. * Called with tg_pt_gp->tg_pt_gp_md_mutex held
  618. */
  619. static int core_alua_update_tpg_primary_metadata(
  620. struct t10_alua_tg_pt_gp *tg_pt_gp,
  621. int primary_state,
  622. unsigned char *md_buf)
  623. {
  624. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  625. struct t10_wwn *wwn = &su_dev->t10_wwn;
  626. char path[ALUA_METADATA_PATH_LEN];
  627. int len;
  628. memset(path, 0, ALUA_METADATA_PATH_LEN);
  629. len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  630. "tg_pt_gp_id=%hu\n"
  631. "alua_access_state=0x%02x\n"
  632. "alua_access_status=0x%02x\n",
  633. tg_pt_gp->tg_pt_gp_id, primary_state,
  634. tg_pt_gp->tg_pt_gp_alua_access_status);
  635. snprintf(path, ALUA_METADATA_PATH_LEN,
  636. "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  637. config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  638. return core_alua_write_tpg_metadata(path, md_buf, len);
  639. }
  640. static int core_alua_do_transition_tg_pt(
  641. struct t10_alua_tg_pt_gp *tg_pt_gp,
  642. struct se_port *l_port,
  643. struct se_node_acl *nacl,
  644. unsigned char *md_buf,
  645. int new_state,
  646. int explict)
  647. {
  648. struct se_dev_entry *se_deve;
  649. struct se_lun_acl *lacl;
  650. struct se_port *port;
  651. struct t10_alua_tg_pt_gp_member *mem;
  652. int old_state = 0;
  653. /*
  654. * Save the old primary ALUA access state, and set the current state
  655. * to ALUA_ACCESS_STATE_TRANSITION.
  656. */
  657. old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  658. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  659. ALUA_ACCESS_STATE_TRANSITION);
  660. tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  661. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  662. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  663. /*
  664. * Check for the optional ALUA primary state transition delay
  665. */
  666. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  667. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  668. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  669. list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  670. tg_pt_gp_mem_list) {
  671. port = mem->tg_pt;
  672. /*
  673. * After an implicit target port asymmetric access state
  674. * change, a device server shall establish a unit attention
  675. * condition for the initiator port associated with every I_T
  676. * nexus with the additional sense code set to ASYMMETRIC
  677. * ACCESS STATE CHAGED.
  678. *
  679. * After an explicit target port asymmetric access state
  680. * change, a device server shall establish a unit attention
  681. * condition with the additional sense code set to ASYMMETRIC
  682. * ACCESS STATE CHANGED for the initiator port associated with
  683. * every I_T nexus other than the I_T nexus on which the SET
  684. * TARGET PORT GROUPS command
  685. */
  686. atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  687. smp_mb__after_atomic_inc();
  688. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  689. spin_lock_bh(&port->sep_alua_lock);
  690. list_for_each_entry(se_deve, &port->sep_alua_list,
  691. alua_port_list) {
  692. lacl = se_deve->se_lun_acl;
  693. /*
  694. * se_deve->se_lun_acl pointer may be NULL for a
  695. * entry created without explict Node+MappedLUN ACLs
  696. */
  697. if (!(lacl))
  698. continue;
  699. if (explict &&
  700. (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  701. (l_port != NULL) && (l_port == port))
  702. continue;
  703. core_scsi3_ua_allocate(lacl->se_lun_nacl,
  704. se_deve->mapped_lun, 0x2A,
  705. ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  706. }
  707. spin_unlock_bh(&port->sep_alua_lock);
  708. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  709. atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  710. smp_mb__after_atomic_dec();
  711. }
  712. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  713. /*
  714. * Update the ALUA metadata buf that has been allocated in
  715. * core_alua_do_port_transition(), this metadata will be written
  716. * to struct file.
  717. *
  718. * Note that there is the case where we do not want to update the
  719. * metadata when the saved metadata is being parsed in userspace
  720. * when setting the existing port access state and access status.
  721. *
  722. * Also note that the failure to write out the ALUA metadata to
  723. * struct file does NOT affect the actual ALUA transition.
  724. */
  725. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  726. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  727. core_alua_update_tpg_primary_metadata(tg_pt_gp,
  728. new_state, md_buf);
  729. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  730. }
  731. /*
  732. * Set the current primary ALUA access state to the requested new state
  733. */
  734. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
  735. printk(KERN_INFO "Successful %s ALUA transition TG PT Group: %s ID: %hu"
  736. " from primary access state %s to %s\n", (explict) ? "explict" :
  737. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  738. tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  739. core_alua_dump_state(new_state));
  740. return 0;
  741. }
  742. int core_alua_do_port_transition(
  743. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  744. struct se_device *l_dev,
  745. struct se_port *l_port,
  746. struct se_node_acl *l_nacl,
  747. int new_state,
  748. int explict)
  749. {
  750. struct se_device *dev;
  751. struct se_port *port;
  752. struct se_subsystem_dev *su_dev;
  753. struct se_node_acl *nacl;
  754. struct t10_alua_lu_gp *lu_gp;
  755. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  756. struct t10_alua_tg_pt_gp *tg_pt_gp;
  757. unsigned char *md_buf;
  758. int primary;
  759. if (core_alua_check_transition(new_state, &primary) != 0)
  760. return -EINVAL;
  761. md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
  762. if (!(md_buf)) {
  763. printk("Unable to allocate buf for ALUA metadata\n");
  764. return -ENOMEM;
  765. }
  766. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  767. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  768. lu_gp = local_lu_gp_mem->lu_gp;
  769. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  770. smp_mb__after_atomic_inc();
  771. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  772. /*
  773. * For storage objects that are members of the 'default_lu_gp',
  774. * we only do transition on the passed *l_tp_pt_gp, and not
  775. * on all of the matching target port groups IDs in default_lu_gp.
  776. */
  777. if (!(lu_gp->lu_gp_id)) {
  778. /*
  779. * core_alua_do_transition_tg_pt() will always return
  780. * success.
  781. */
  782. core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  783. md_buf, new_state, explict);
  784. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  785. smp_mb__after_atomic_dec();
  786. kfree(md_buf);
  787. return 0;
  788. }
  789. /*
  790. * For all other LU groups aside from 'default_lu_gp', walk all of
  791. * the associated storage objects looking for a matching target port
  792. * group ID from the local target port group.
  793. */
  794. spin_lock(&lu_gp->lu_gp_lock);
  795. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  796. lu_gp_mem_list) {
  797. dev = lu_gp_mem->lu_gp_mem_dev;
  798. su_dev = dev->se_sub_dev;
  799. atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  800. smp_mb__after_atomic_inc();
  801. spin_unlock(&lu_gp->lu_gp_lock);
  802. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  803. list_for_each_entry(tg_pt_gp,
  804. &T10_ALUA(su_dev)->tg_pt_gps_list,
  805. tg_pt_gp_list) {
  806. if (!(tg_pt_gp->tg_pt_gp_valid_id))
  807. continue;
  808. /*
  809. * If the target behavior port asymmetric access state
  810. * is changed for any target port group accessiable via
  811. * a logical unit within a LU group, the target port
  812. * behavior group asymmetric access states for the same
  813. * target port group accessible via other logical units
  814. * in that LU group will also change.
  815. */
  816. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  817. continue;
  818. if (l_tg_pt_gp == tg_pt_gp) {
  819. port = l_port;
  820. nacl = l_nacl;
  821. } else {
  822. port = NULL;
  823. nacl = NULL;
  824. }
  825. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  826. smp_mb__after_atomic_inc();
  827. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  828. /*
  829. * core_alua_do_transition_tg_pt() will always return
  830. * success.
  831. */
  832. core_alua_do_transition_tg_pt(tg_pt_gp, port,
  833. nacl, md_buf, new_state, explict);
  834. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  835. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  836. smp_mb__after_atomic_dec();
  837. }
  838. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  839. spin_lock(&lu_gp->lu_gp_lock);
  840. atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  841. smp_mb__after_atomic_dec();
  842. }
  843. spin_unlock(&lu_gp->lu_gp_lock);
  844. printk(KERN_INFO "Successfully processed LU Group: %s all ALUA TG PT"
  845. " Group IDs: %hu %s transition to primary state: %s\n",
  846. config_item_name(&lu_gp->lu_gp_group.cg_item),
  847. l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  848. core_alua_dump_state(new_state));
  849. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  850. smp_mb__after_atomic_dec();
  851. kfree(md_buf);
  852. return 0;
  853. }
  854. /*
  855. * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
  856. */
  857. static int core_alua_update_tpg_secondary_metadata(
  858. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  859. struct se_port *port,
  860. unsigned char *md_buf,
  861. u32 md_buf_len)
  862. {
  863. struct se_portal_group *se_tpg = port->sep_tpg;
  864. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  865. int len;
  866. memset(path, 0, ALUA_METADATA_PATH_LEN);
  867. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  868. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  869. TPG_TFO(se_tpg)->tpg_get_wwn(se_tpg));
  870. if (TPG_TFO(se_tpg)->tpg_get_tag != NULL)
  871. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  872. TPG_TFO(se_tpg)->tpg_get_tag(se_tpg));
  873. len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
  874. "alua_tg_pt_status=0x%02x\n",
  875. atomic_read(&port->sep_tg_pt_secondary_offline),
  876. port->sep_tg_pt_secondary_stat);
  877. snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
  878. TPG_TFO(se_tpg)->get_fabric_name(), wwn,
  879. port->sep_lun->unpacked_lun);
  880. return core_alua_write_tpg_metadata(path, md_buf, len);
  881. }
  882. static int core_alua_set_tg_pt_secondary_state(
  883. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  884. struct se_port *port,
  885. int explict,
  886. int offline)
  887. {
  888. struct t10_alua_tg_pt_gp *tg_pt_gp;
  889. unsigned char *md_buf;
  890. u32 md_buf_len;
  891. int trans_delay_msecs;
  892. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  893. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  894. if (!(tg_pt_gp)) {
  895. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  896. printk(KERN_ERR "Unable to complete secondary state"
  897. " transition\n");
  898. return -1;
  899. }
  900. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  901. /*
  902. * Set the secondary ALUA target port access state to OFFLINE
  903. * or release the previously secondary state for struct se_port
  904. */
  905. if (offline)
  906. atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  907. else
  908. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  909. md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  910. port->sep_tg_pt_secondary_stat = (explict) ?
  911. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  912. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  913. printk(KERN_INFO "Successful %s ALUA transition TG PT Group: %s ID: %hu"
  914. " to secondary access state: %s\n", (explict) ? "explict" :
  915. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  916. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  917. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  918. /*
  919. * Do the optional transition delay after we set the secondary
  920. * ALUA access state.
  921. */
  922. if (trans_delay_msecs != 0)
  923. msleep_interruptible(trans_delay_msecs);
  924. /*
  925. * See if we need to update the ALUA fabric port metadata for
  926. * secondary state and status
  927. */
  928. if (port->sep_tg_pt_secondary_write_md) {
  929. md_buf = kzalloc(md_buf_len, GFP_KERNEL);
  930. if (!(md_buf)) {
  931. printk(KERN_ERR "Unable to allocate md_buf for"
  932. " secondary ALUA access metadata\n");
  933. return -1;
  934. }
  935. mutex_lock(&port->sep_tg_pt_md_mutex);
  936. core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  937. md_buf, md_buf_len);
  938. mutex_unlock(&port->sep_tg_pt_md_mutex);
  939. kfree(md_buf);
  940. }
  941. return 0;
  942. }
  943. struct t10_alua_lu_gp *
  944. core_alua_allocate_lu_gp(const char *name, int def_group)
  945. {
  946. struct t10_alua_lu_gp *lu_gp;
  947. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  948. if (!(lu_gp)) {
  949. printk(KERN_ERR "Unable to allocate struct t10_alua_lu_gp\n");
  950. return ERR_PTR(-ENOMEM);;
  951. }
  952. INIT_LIST_HEAD(&lu_gp->lu_gp_list);
  953. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  954. spin_lock_init(&lu_gp->lu_gp_lock);
  955. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  956. if (def_group) {
  957. lu_gp->lu_gp_id = se_global->alua_lu_gps_counter++;;
  958. lu_gp->lu_gp_valid_id = 1;
  959. se_global->alua_lu_gps_count++;
  960. }
  961. return lu_gp;
  962. }
  963. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  964. {
  965. struct t10_alua_lu_gp *lu_gp_tmp;
  966. u16 lu_gp_id_tmp;
  967. /*
  968. * The lu_gp->lu_gp_id may only be set once..
  969. */
  970. if (lu_gp->lu_gp_valid_id) {
  971. printk(KERN_WARNING "ALUA LU Group already has a valid ID,"
  972. " ignoring request\n");
  973. return -1;
  974. }
  975. spin_lock(&se_global->lu_gps_lock);
  976. if (se_global->alua_lu_gps_count == 0x0000ffff) {
  977. printk(KERN_ERR "Maximum ALUA se_global->alua_lu_gps_count:"
  978. " 0x0000ffff reached\n");
  979. spin_unlock(&se_global->lu_gps_lock);
  980. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  981. return -1;
  982. }
  983. again:
  984. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  985. se_global->alua_lu_gps_counter++;
  986. list_for_each_entry(lu_gp_tmp, &se_global->g_lu_gps_list, lu_gp_list) {
  987. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  988. if (!(lu_gp_id))
  989. goto again;
  990. printk(KERN_WARNING "ALUA Logical Unit Group ID: %hu"
  991. " already exists, ignoring request\n",
  992. lu_gp_id);
  993. spin_unlock(&se_global->lu_gps_lock);
  994. return -1;
  995. }
  996. }
  997. lu_gp->lu_gp_id = lu_gp_id_tmp;
  998. lu_gp->lu_gp_valid_id = 1;
  999. list_add_tail(&lu_gp->lu_gp_list, &se_global->g_lu_gps_list);
  1000. se_global->alua_lu_gps_count++;
  1001. spin_unlock(&se_global->lu_gps_lock);
  1002. return 0;
  1003. }
  1004. static struct t10_alua_lu_gp_member *
  1005. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1006. {
  1007. struct t10_alua_lu_gp_member *lu_gp_mem;
  1008. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1009. if (!(lu_gp_mem)) {
  1010. printk(KERN_ERR "Unable to allocate struct t10_alua_lu_gp_member\n");
  1011. return ERR_PTR(-ENOMEM);
  1012. }
  1013. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1014. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1015. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1016. lu_gp_mem->lu_gp_mem_dev = dev;
  1017. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1018. return lu_gp_mem;
  1019. }
  1020. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1021. {
  1022. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1023. /*
  1024. * Once we have reached this point, config_item_put() has
  1025. * already been called from target_core_alua_drop_lu_gp().
  1026. *
  1027. * Here, we remove the *lu_gp from the global list so that
  1028. * no associations can be made while we are releasing
  1029. * struct t10_alua_lu_gp.
  1030. */
  1031. spin_lock(&se_global->lu_gps_lock);
  1032. atomic_set(&lu_gp->lu_gp_shutdown, 1);
  1033. list_del(&lu_gp->lu_gp_list);
  1034. se_global->alua_lu_gps_count--;
  1035. spin_unlock(&se_global->lu_gps_lock);
  1036. /*
  1037. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1038. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1039. * released with core_alua_put_lu_gp_from_name()
  1040. */
  1041. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1042. cpu_relax();
  1043. /*
  1044. * Release reference to struct t10_alua_lu_gp * from all associated
  1045. * struct se_device.
  1046. */
  1047. spin_lock(&lu_gp->lu_gp_lock);
  1048. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1049. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1050. if (lu_gp_mem->lu_gp_assoc) {
  1051. list_del(&lu_gp_mem->lu_gp_mem_list);
  1052. lu_gp->lu_gp_members--;
  1053. lu_gp_mem->lu_gp_assoc = 0;
  1054. }
  1055. spin_unlock(&lu_gp->lu_gp_lock);
  1056. /*
  1057. *
  1058. * lu_gp_mem is assoicated with a single
  1059. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1060. * struct se_device is released via core_alua_free_lu_gp_mem().
  1061. *
  1062. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1063. * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  1064. */
  1065. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1066. if (lu_gp != se_global->default_lu_gp)
  1067. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1068. se_global->default_lu_gp);
  1069. else
  1070. lu_gp_mem->lu_gp = NULL;
  1071. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1072. spin_lock(&lu_gp->lu_gp_lock);
  1073. }
  1074. spin_unlock(&lu_gp->lu_gp_lock);
  1075. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1076. }
  1077. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1078. {
  1079. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1080. struct t10_alua *alua = T10_ALUA(su_dev);
  1081. struct t10_alua_lu_gp *lu_gp;
  1082. struct t10_alua_lu_gp_member *lu_gp_mem;
  1083. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1084. return;
  1085. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1086. if (!(lu_gp_mem))
  1087. return;
  1088. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1089. cpu_relax();
  1090. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1091. lu_gp = lu_gp_mem->lu_gp;
  1092. if ((lu_gp)) {
  1093. spin_lock(&lu_gp->lu_gp_lock);
  1094. if (lu_gp_mem->lu_gp_assoc) {
  1095. list_del(&lu_gp_mem->lu_gp_mem_list);
  1096. lu_gp->lu_gp_members--;
  1097. lu_gp_mem->lu_gp_assoc = 0;
  1098. }
  1099. spin_unlock(&lu_gp->lu_gp_lock);
  1100. lu_gp_mem->lu_gp = NULL;
  1101. }
  1102. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1103. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1104. }
  1105. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1106. {
  1107. struct t10_alua_lu_gp *lu_gp;
  1108. struct config_item *ci;
  1109. spin_lock(&se_global->lu_gps_lock);
  1110. list_for_each_entry(lu_gp, &se_global->g_lu_gps_list, lu_gp_list) {
  1111. if (!(lu_gp->lu_gp_valid_id))
  1112. continue;
  1113. ci = &lu_gp->lu_gp_group.cg_item;
  1114. if (!(strcmp(config_item_name(ci), name))) {
  1115. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1116. spin_unlock(&se_global->lu_gps_lock);
  1117. return lu_gp;
  1118. }
  1119. }
  1120. spin_unlock(&se_global->lu_gps_lock);
  1121. return NULL;
  1122. }
  1123. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1124. {
  1125. spin_lock(&se_global->lu_gps_lock);
  1126. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1127. spin_unlock(&se_global->lu_gps_lock);
  1128. }
  1129. /*
  1130. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1131. */
  1132. void __core_alua_attach_lu_gp_mem(
  1133. struct t10_alua_lu_gp_member *lu_gp_mem,
  1134. struct t10_alua_lu_gp *lu_gp)
  1135. {
  1136. spin_lock(&lu_gp->lu_gp_lock);
  1137. lu_gp_mem->lu_gp = lu_gp;
  1138. lu_gp_mem->lu_gp_assoc = 1;
  1139. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1140. lu_gp->lu_gp_members++;
  1141. spin_unlock(&lu_gp->lu_gp_lock);
  1142. }
  1143. /*
  1144. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1145. */
  1146. void __core_alua_drop_lu_gp_mem(
  1147. struct t10_alua_lu_gp_member *lu_gp_mem,
  1148. struct t10_alua_lu_gp *lu_gp)
  1149. {
  1150. spin_lock(&lu_gp->lu_gp_lock);
  1151. list_del(&lu_gp_mem->lu_gp_mem_list);
  1152. lu_gp_mem->lu_gp = NULL;
  1153. lu_gp_mem->lu_gp_assoc = 0;
  1154. lu_gp->lu_gp_members--;
  1155. spin_unlock(&lu_gp->lu_gp_lock);
  1156. }
  1157. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
  1158. struct se_subsystem_dev *su_dev,
  1159. const char *name,
  1160. int def_group)
  1161. {
  1162. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1163. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1164. if (!(tg_pt_gp)) {
  1165. printk(KERN_ERR "Unable to allocate struct t10_alua_tg_pt_gp\n");
  1166. return NULL;
  1167. }
  1168. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1169. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  1170. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1171. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1172. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1173. tg_pt_gp->tg_pt_gp_su_dev = su_dev;
  1174. tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  1175. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1176. ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  1177. /*
  1178. * Enable both explict and implict ALUA support by default
  1179. */
  1180. tg_pt_gp->tg_pt_gp_alua_access_type =
  1181. TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  1182. /*
  1183. * Set the default Active/NonOptimized Delay in milliseconds
  1184. */
  1185. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1186. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1187. if (def_group) {
  1188. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1189. tg_pt_gp->tg_pt_gp_id =
  1190. T10_ALUA(su_dev)->alua_tg_pt_gps_counter++;
  1191. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1192. T10_ALUA(su_dev)->alua_tg_pt_gps_count++;
  1193. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1194. &T10_ALUA(su_dev)->tg_pt_gps_list);
  1195. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1196. }
  1197. return tg_pt_gp;
  1198. }
  1199. int core_alua_set_tg_pt_gp_id(
  1200. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1201. u16 tg_pt_gp_id)
  1202. {
  1203. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1204. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1205. u16 tg_pt_gp_id_tmp;
  1206. /*
  1207. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1208. */
  1209. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1210. printk(KERN_WARNING "ALUA TG PT Group already has a valid ID,"
  1211. " ignoring request\n");
  1212. return -1;
  1213. }
  1214. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1215. if (T10_ALUA(su_dev)->alua_tg_pt_gps_count == 0x0000ffff) {
  1216. printk(KERN_ERR "Maximum ALUA alua_tg_pt_gps_count:"
  1217. " 0x0000ffff reached\n");
  1218. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1219. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1220. return -1;
  1221. }
  1222. again:
  1223. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1224. T10_ALUA(su_dev)->alua_tg_pt_gps_counter++;
  1225. list_for_each_entry(tg_pt_gp_tmp, &T10_ALUA(su_dev)->tg_pt_gps_list,
  1226. tg_pt_gp_list) {
  1227. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1228. if (!(tg_pt_gp_id))
  1229. goto again;
  1230. printk(KERN_ERR "ALUA Target Port Group ID: %hu already"
  1231. " exists, ignoring request\n", tg_pt_gp_id);
  1232. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1233. return -1;
  1234. }
  1235. }
  1236. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1237. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1238. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1239. &T10_ALUA(su_dev)->tg_pt_gps_list);
  1240. T10_ALUA(su_dev)->alua_tg_pt_gps_count++;
  1241. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1242. return 0;
  1243. }
  1244. struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  1245. struct se_port *port)
  1246. {
  1247. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1248. tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  1249. GFP_KERNEL);
  1250. if (!(tg_pt_gp_mem)) {
  1251. printk(KERN_ERR "Unable to allocate struct t10_alua_tg_pt_gp_member\n");
  1252. return ERR_PTR(-ENOMEM);
  1253. }
  1254. INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1255. spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1256. atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  1257. tg_pt_gp_mem->tg_pt = port;
  1258. port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
  1259. atomic_set(&port->sep_tg_pt_gp_active, 1);
  1260. return tg_pt_gp_mem;
  1261. }
  1262. void core_alua_free_tg_pt_gp(
  1263. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1264. {
  1265. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1266. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  1267. /*
  1268. * Once we have reached this point, config_item_put() has already
  1269. * been called from target_core_alua_drop_tg_pt_gp().
  1270. *
  1271. * Here we remove *tg_pt_gp from the global list so that
  1272. * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  1273. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1274. */
  1275. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1276. list_del(&tg_pt_gp->tg_pt_gp_list);
  1277. T10_ALUA(su_dev)->alua_tg_pt_gps_counter--;
  1278. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1279. /*
  1280. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1281. * core_alua_get_tg_pt_gp_by_name() in
  1282. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1283. * to be released with core_alua_put_tg_pt_gp_from_name().
  1284. */
  1285. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1286. cpu_relax();
  1287. /*
  1288. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1289. * struct se_port.
  1290. */
  1291. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1292. list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  1293. &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  1294. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1295. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1296. tg_pt_gp->tg_pt_gp_members--;
  1297. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1298. }
  1299. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1300. /*
  1301. * tg_pt_gp_mem is assoicated with a single
  1302. * se_port->sep_alua_tg_pt_gp_mem, and is released via
  1303. * core_alua_free_tg_pt_gp_mem().
  1304. *
  1305. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1306. * assume we want to re-assocate a given tg_pt_gp_mem with
  1307. * default_tg_pt_gp.
  1308. */
  1309. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1310. if (tg_pt_gp != T10_ALUA(su_dev)->default_tg_pt_gp) {
  1311. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1312. T10_ALUA(su_dev)->default_tg_pt_gp);
  1313. } else
  1314. tg_pt_gp_mem->tg_pt_gp = NULL;
  1315. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1316. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1317. }
  1318. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1319. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1320. }
  1321. void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  1322. {
  1323. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1324. struct t10_alua *alua = T10_ALUA(su_dev);
  1325. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1326. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1327. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1328. return;
  1329. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1330. if (!(tg_pt_gp_mem))
  1331. return;
  1332. while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  1333. cpu_relax();
  1334. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1335. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1336. if ((tg_pt_gp)) {
  1337. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1338. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1339. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1340. tg_pt_gp->tg_pt_gp_members--;
  1341. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1342. }
  1343. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1344. tg_pt_gp_mem->tg_pt_gp = NULL;
  1345. }
  1346. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1347. kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  1348. }
  1349. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1350. struct se_subsystem_dev *su_dev,
  1351. const char *name)
  1352. {
  1353. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1354. struct config_item *ci;
  1355. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1356. list_for_each_entry(tg_pt_gp, &T10_ALUA(su_dev)->tg_pt_gps_list,
  1357. tg_pt_gp_list) {
  1358. if (!(tg_pt_gp->tg_pt_gp_valid_id))
  1359. continue;
  1360. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1361. if (!(strcmp(config_item_name(ci), name))) {
  1362. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1363. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1364. return tg_pt_gp;
  1365. }
  1366. }
  1367. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1368. return NULL;
  1369. }
  1370. static void core_alua_put_tg_pt_gp_from_name(
  1371. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1372. {
  1373. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1374. spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1375. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1376. spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock);
  1377. }
  1378. /*
  1379. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1380. */
  1381. void __core_alua_attach_tg_pt_gp_mem(
  1382. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1383. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1384. {
  1385. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1386. tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  1387. tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  1388. list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  1389. &tg_pt_gp->tg_pt_gp_mem_list);
  1390. tg_pt_gp->tg_pt_gp_members++;
  1391. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1392. }
  1393. /*
  1394. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1395. */
  1396. static void __core_alua_drop_tg_pt_gp_mem(
  1397. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1398. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1399. {
  1400. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1401. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1402. tg_pt_gp_mem->tg_pt_gp = NULL;
  1403. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1404. tg_pt_gp->tg_pt_gp_members--;
  1405. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1406. }
  1407. ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  1408. {
  1409. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1410. struct config_item *tg_pt_ci;
  1411. struct t10_alua *alua = T10_ALUA(su_dev);
  1412. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1413. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1414. ssize_t len = 0;
  1415. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1416. return len;
  1417. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1418. if (!(tg_pt_gp_mem))
  1419. return len;
  1420. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1421. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1422. if ((tg_pt_gp)) {
  1423. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1424. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1425. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1426. "Primary Access Status: %s\nTG Port Secondary Access"
  1427. " State: %s\nTG Port Secondary Access Status: %s\n",
  1428. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1429. core_alua_dump_state(atomic_read(
  1430. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1431. core_alua_dump_status(
  1432. tg_pt_gp->tg_pt_gp_alua_access_status),
  1433. (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  1434. "Offline" : "None",
  1435. core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  1436. }
  1437. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1438. return len;
  1439. }
  1440. ssize_t core_alua_store_tg_pt_gp_info(
  1441. struct se_port *port,
  1442. const char *page,
  1443. size_t count)
  1444. {
  1445. struct se_portal_group *tpg;
  1446. struct se_lun *lun;
  1447. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1448. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1449. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1450. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1451. int move = 0;
  1452. tpg = port->sep_tpg;
  1453. lun = port->sep_lun;
  1454. if (T10_ALUA(su_dev)->alua_type != SPC3_ALUA_EMULATED) {
  1455. printk(KERN_WARNING "SPC3_ALUA_EMULATED not enabled for"
  1456. " %s/tpgt_%hu/%s\n", TPG_TFO(tpg)->tpg_get_wwn(tpg),
  1457. TPG_TFO(tpg)->tpg_get_tag(tpg),
  1458. config_item_name(&lun->lun_group.cg_item));
  1459. return -EINVAL;
  1460. }
  1461. if (count > TG_PT_GROUP_NAME_BUF) {
  1462. printk(KERN_ERR "ALUA Target Port Group alias too large!\n");
  1463. return -EINVAL;
  1464. }
  1465. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1466. memcpy(buf, page, count);
  1467. /*
  1468. * Any ALUA target port group alias besides "NULL" means we will be
  1469. * making a new group association.
  1470. */
  1471. if (strcmp(strstrip(buf), "NULL")) {
  1472. /*
  1473. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1474. * struct t10_alua_tg_pt_gp. This reference is released with
  1475. * core_alua_put_tg_pt_gp_from_name() below.
  1476. */
  1477. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
  1478. strstrip(buf));
  1479. if (!(tg_pt_gp_new))
  1480. return -ENODEV;
  1481. }
  1482. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1483. if (!(tg_pt_gp_mem)) {
  1484. if (tg_pt_gp_new)
  1485. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1486. printk(KERN_ERR "NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
  1487. return -EINVAL;
  1488. }
  1489. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1490. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1491. if ((tg_pt_gp)) {
  1492. /*
  1493. * Clearing an existing tg_pt_gp association, and replacing
  1494. * with the default_tg_pt_gp.
  1495. */
  1496. if (!(tg_pt_gp_new)) {
  1497. printk(KERN_INFO "Target_Core_ConfigFS: Moving"
  1498. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1499. " alua/%s, ID: %hu back to"
  1500. " default_tg_pt_gp\n",
  1501. TPG_TFO(tpg)->tpg_get_wwn(tpg),
  1502. TPG_TFO(tpg)->tpg_get_tag(tpg),
  1503. config_item_name(&lun->lun_group.cg_item),
  1504. config_item_name(
  1505. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1506. tg_pt_gp->tg_pt_gp_id);
  1507. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1508. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1509. T10_ALUA(su_dev)->default_tg_pt_gp);
  1510. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1511. return count;
  1512. }
  1513. /*
  1514. * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  1515. */
  1516. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1517. move = 1;
  1518. }
  1519. /*
  1520. * Associate tg_pt_gp_mem with tg_pt_gp_new.
  1521. */
  1522. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  1523. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1524. printk(KERN_INFO "Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1525. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1526. "Moving" : "Adding", TPG_TFO(tpg)->tpg_get_wwn(tpg),
  1527. TPG_TFO(tpg)->tpg_get_tag(tpg),
  1528. config_item_name(&lun->lun_group.cg_item),
  1529. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1530. tg_pt_gp_new->tg_pt_gp_id);
  1531. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1532. return count;
  1533. }
  1534. ssize_t core_alua_show_access_type(
  1535. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1536. char *page)
  1537. {
  1538. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  1539. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  1540. return sprintf(page, "Implict and Explict\n");
  1541. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  1542. return sprintf(page, "Implict\n");
  1543. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  1544. return sprintf(page, "Explict\n");
  1545. else
  1546. return sprintf(page, "None\n");
  1547. }
  1548. ssize_t core_alua_store_access_type(
  1549. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1550. const char *page,
  1551. size_t count)
  1552. {
  1553. unsigned long tmp;
  1554. int ret;
  1555. ret = strict_strtoul(page, 0, &tmp);
  1556. if (ret < 0) {
  1557. printk(KERN_ERR "Unable to extract alua_access_type\n");
  1558. return -EINVAL;
  1559. }
  1560. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1561. printk(KERN_ERR "Illegal value for alua_access_type:"
  1562. " %lu\n", tmp);
  1563. return -EINVAL;
  1564. }
  1565. if (tmp == 3)
  1566. tg_pt_gp->tg_pt_gp_alua_access_type =
  1567. TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  1568. else if (tmp == 2)
  1569. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  1570. else if (tmp == 1)
  1571. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  1572. else
  1573. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1574. return count;
  1575. }
  1576. ssize_t core_alua_show_nonop_delay_msecs(
  1577. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1578. char *page)
  1579. {
  1580. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1581. }
  1582. ssize_t core_alua_store_nonop_delay_msecs(
  1583. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1584. const char *page,
  1585. size_t count)
  1586. {
  1587. unsigned long tmp;
  1588. int ret;
  1589. ret = strict_strtoul(page, 0, &tmp);
  1590. if (ret < 0) {
  1591. printk(KERN_ERR "Unable to extract nonop_delay_msecs\n");
  1592. return -EINVAL;
  1593. }
  1594. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1595. printk(KERN_ERR "Passed nonop_delay_msecs: %lu, exceeds"
  1596. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1597. ALUA_MAX_NONOP_DELAY_MSECS);
  1598. return -EINVAL;
  1599. }
  1600. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1601. return count;
  1602. }
  1603. ssize_t core_alua_show_trans_delay_msecs(
  1604. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1605. char *page)
  1606. {
  1607. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1608. }
  1609. ssize_t core_alua_store_trans_delay_msecs(
  1610. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1611. const char *page,
  1612. size_t count)
  1613. {
  1614. unsigned long tmp;
  1615. int ret;
  1616. ret = strict_strtoul(page, 0, &tmp);
  1617. if (ret < 0) {
  1618. printk(KERN_ERR "Unable to extract trans_delay_msecs\n");
  1619. return -EINVAL;
  1620. }
  1621. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1622. printk(KERN_ERR "Passed trans_delay_msecs: %lu, exceeds"
  1623. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1624. ALUA_MAX_TRANS_DELAY_MSECS);
  1625. return -EINVAL;
  1626. }
  1627. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1628. return count;
  1629. }
  1630. ssize_t core_alua_show_preferred_bit(
  1631. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1632. char *page)
  1633. {
  1634. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1635. }
  1636. ssize_t core_alua_store_preferred_bit(
  1637. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1638. const char *page,
  1639. size_t count)
  1640. {
  1641. unsigned long tmp;
  1642. int ret;
  1643. ret = strict_strtoul(page, 0, &tmp);
  1644. if (ret < 0) {
  1645. printk(KERN_ERR "Unable to extract preferred ALUA value\n");
  1646. return -EINVAL;
  1647. }
  1648. if ((tmp != 0) && (tmp != 1)) {
  1649. printk(KERN_ERR "Illegal value for preferred ALUA: %lu\n", tmp);
  1650. return -EINVAL;
  1651. }
  1652. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1653. return count;
  1654. }
  1655. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1656. {
  1657. if (!(lun->lun_sep))
  1658. return -ENODEV;
  1659. return sprintf(page, "%d\n",
  1660. atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  1661. }
  1662. ssize_t core_alua_store_offline_bit(
  1663. struct se_lun *lun,
  1664. const char *page,
  1665. size_t count)
  1666. {
  1667. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1668. unsigned long tmp;
  1669. int ret;
  1670. if (!(lun->lun_sep))
  1671. return -ENODEV;
  1672. ret = strict_strtoul(page, 0, &tmp);
  1673. if (ret < 0) {
  1674. printk(KERN_ERR "Unable to extract alua_tg_pt_offline value\n");
  1675. return -EINVAL;
  1676. }
  1677. if ((tmp != 0) && (tmp != 1)) {
  1678. printk(KERN_ERR "Illegal value for alua_tg_pt_offline: %lu\n",
  1679. tmp);
  1680. return -EINVAL;
  1681. }
  1682. tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
  1683. if (!(tg_pt_gp_mem)) {
  1684. printk(KERN_ERR "Unable to locate *tg_pt_gp_mem\n");
  1685. return -EINVAL;
  1686. }
  1687. ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  1688. lun->lun_sep, 0, (int)tmp);
  1689. if (ret < 0)
  1690. return -EINVAL;
  1691. return count;
  1692. }
  1693. ssize_t core_alua_show_secondary_status(
  1694. struct se_lun *lun,
  1695. char *page)
  1696. {
  1697. return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
  1698. }
  1699. ssize_t core_alua_store_secondary_status(
  1700. struct se_lun *lun,
  1701. const char *page,
  1702. size_t count)
  1703. {
  1704. unsigned long tmp;
  1705. int ret;
  1706. ret = strict_strtoul(page, 0, &tmp);
  1707. if (ret < 0) {
  1708. printk(KERN_ERR "Unable to extract alua_tg_pt_status\n");
  1709. return -EINVAL;
  1710. }
  1711. if ((tmp != ALUA_STATUS_NONE) &&
  1712. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1713. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1714. printk(KERN_ERR "Illegal value for alua_tg_pt_status: %lu\n",
  1715. tmp);
  1716. return -EINVAL;
  1717. }
  1718. lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  1719. return count;
  1720. }
  1721. ssize_t core_alua_show_secondary_write_metadata(
  1722. struct se_lun *lun,
  1723. char *page)
  1724. {
  1725. return sprintf(page, "%d\n",
  1726. lun->lun_sep->sep_tg_pt_secondary_write_md);
  1727. }
  1728. ssize_t core_alua_store_secondary_write_metadata(
  1729. struct se_lun *lun,
  1730. const char *page,
  1731. size_t count)
  1732. {
  1733. unsigned long tmp;
  1734. int ret;
  1735. ret = strict_strtoul(page, 0, &tmp);
  1736. if (ret < 0) {
  1737. printk(KERN_ERR "Unable to extract alua_tg_pt_write_md\n");
  1738. return -EINVAL;
  1739. }
  1740. if ((tmp != 0) && (tmp != 1)) {
  1741. printk(KERN_ERR "Illegal value for alua_tg_pt_write_md:"
  1742. " %lu\n", tmp);
  1743. return -EINVAL;
  1744. }
  1745. lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  1746. return count;
  1747. }
  1748. int core_setup_alua(struct se_device *dev, int force_pt)
  1749. {
  1750. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1751. struct t10_alua *alua = T10_ALUA(su_dev);
  1752. struct t10_alua_lu_gp_member *lu_gp_mem;
  1753. /*
  1754. * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
  1755. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  1756. * cause a problem because libata and some SATA RAID HBAs appear
  1757. * under Linux/SCSI, but emulate SCSI logic themselves.
  1758. */
  1759. if (((TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  1760. !(DEV_ATTRIB(dev)->emulate_alua)) || force_pt) {
  1761. alua->alua_type = SPC_ALUA_PASSTHROUGH;
  1762. alua->alua_state_check = &core_alua_state_check_nop;
  1763. printk(KERN_INFO "%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
  1764. " emulation\n", TRANSPORT(dev)->name);
  1765. return 0;
  1766. }
  1767. /*
  1768. * If SPC-3 or above is reported by real or emulated struct se_device,
  1769. * use emulated ALUA.
  1770. */
  1771. if (TRANSPORT(dev)->get_device_rev(dev) >= SCSI_3) {
  1772. printk(KERN_INFO "%s: Enabling ALUA Emulation for SPC-3"
  1773. " device\n", TRANSPORT(dev)->name);
  1774. /*
  1775. * Assoicate this struct se_device with the default ALUA
  1776. * LUN Group.
  1777. */
  1778. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  1779. if (IS_ERR(lu_gp_mem) || !lu_gp_mem)
  1780. return -1;
  1781. alua->alua_type = SPC3_ALUA_EMULATED;
  1782. alua->alua_state_check = &core_alua_state_check;
  1783. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1784. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1785. se_global->default_lu_gp);
  1786. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1787. printk(KERN_INFO "%s: Adding to default ALUA LU Group:"
  1788. " core/alua/lu_gps/default_lu_gp\n",
  1789. TRANSPORT(dev)->name);
  1790. } else {
  1791. alua->alua_type = SPC2_ALUA_DISABLED;
  1792. alua->alua_state_check = &core_alua_state_check_nop;
  1793. printk(KERN_INFO "%s: Disabling ALUA Emulation for SPC-2"
  1794. " device\n", TRANSPORT(dev)->name);
  1795. }
  1796. return 0;
  1797. }