scsi_dh_alua.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * Generic SCSI-3 ALUA SCSI Device Handler
  3. *
  4. * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <asm/unaligned.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_proto.h>
  28. #include <scsi/scsi_dbg.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_dh.h>
  31. #define ALUA_DH_NAME "alua"
  32. #define ALUA_DH_VER "2.0"
  33. #define TPGS_SUPPORT_NONE 0x00
  34. #define TPGS_SUPPORT_OPTIMIZED 0x01
  35. #define TPGS_SUPPORT_NONOPTIMIZED 0x02
  36. #define TPGS_SUPPORT_STANDBY 0x04
  37. #define TPGS_SUPPORT_UNAVAILABLE 0x08
  38. #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
  39. #define TPGS_SUPPORT_OFFLINE 0x40
  40. #define TPGS_SUPPORT_TRANSITION 0x80
  41. #define TPGS_SUPPORT_ALL 0xdf
  42. #define RTPG_FMT_MASK 0x70
  43. #define RTPG_FMT_EXT_HDR 0x10
  44. #define TPGS_MODE_UNINITIALIZED -1
  45. #define TPGS_MODE_NONE 0x0
  46. #define TPGS_MODE_IMPLICIT 0x1
  47. #define TPGS_MODE_EXPLICIT 0x2
  48. #define ALUA_RTPG_SIZE 128
  49. #define ALUA_FAILOVER_TIMEOUT 60
  50. #define ALUA_FAILOVER_RETRIES 5
  51. #define ALUA_RTPG_DELAY_MSECS 5
  52. #define ALUA_RTPG_RETRY_DELAY 2
  53. /* device handler flags */
  54. #define ALUA_OPTIMIZE_STPG 0x01
  55. #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
  56. /* State machine flags */
  57. #define ALUA_PG_RUN_RTPG 0x10
  58. #define ALUA_PG_RUN_STPG 0x20
  59. #define ALUA_PG_RUNNING 0x40
  60. static uint optimize_stpg;
  61. module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
  62. MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0.");
  63. static LIST_HEAD(port_group_list);
  64. static DEFINE_SPINLOCK(port_group_lock);
  65. static struct workqueue_struct *kaluad_wq;
  66. struct alua_port_group {
  67. struct kref kref;
  68. struct rcu_head rcu;
  69. struct list_head node;
  70. struct list_head dh_list;
  71. unsigned char device_id_str[256];
  72. int device_id_len;
  73. int group_id;
  74. int tpgs;
  75. int state;
  76. int pref;
  77. int valid_states;
  78. unsigned flags; /* used for optimizing STPG */
  79. unsigned char transition_tmo;
  80. unsigned long expiry;
  81. unsigned long interval;
  82. struct delayed_work rtpg_work;
  83. spinlock_t lock;
  84. struct list_head rtpg_list;
  85. struct scsi_device *rtpg_sdev;
  86. };
  87. struct alua_dh_data {
  88. struct list_head node;
  89. struct alua_port_group __rcu *pg;
  90. int group_id;
  91. spinlock_t pg_lock;
  92. struct scsi_device *sdev;
  93. int init_error;
  94. struct mutex init_mutex;
  95. };
  96. struct alua_queue_data {
  97. struct list_head entry;
  98. activate_complete callback_fn;
  99. void *callback_data;
  100. };
  101. #define ALUA_POLICY_SWITCH_CURRENT 0
  102. #define ALUA_POLICY_SWITCH_ALL 1
  103. static void alua_rtpg_work(struct work_struct *work);
  104. static bool alua_rtpg_queue(struct alua_port_group *pg,
  105. struct scsi_device *sdev,
  106. struct alua_queue_data *qdata, bool force);
  107. static void alua_check(struct scsi_device *sdev, bool force);
  108. static void release_port_group(struct kref *kref)
  109. {
  110. struct alua_port_group *pg;
  111. pg = container_of(kref, struct alua_port_group, kref);
  112. if (pg->rtpg_sdev)
  113. flush_delayed_work(&pg->rtpg_work);
  114. spin_lock(&port_group_lock);
  115. list_del(&pg->node);
  116. spin_unlock(&port_group_lock);
  117. kfree_rcu(pg, rcu);
  118. }
  119. /*
  120. * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
  121. * @sdev: sdev the command should be sent to
  122. */
  123. static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
  124. int bufflen, struct scsi_sense_hdr *sshdr, int flags)
  125. {
  126. u8 cdb[MAX_COMMAND_SIZE];
  127. int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  128. REQ_FAILFAST_DRIVER;
  129. /* Prepare the command. */
  130. memset(cdb, 0x0, MAX_COMMAND_SIZE);
  131. cdb[0] = MAINTENANCE_IN;
  132. if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
  133. cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
  134. else
  135. cdb[1] = MI_REPORT_TARGET_PGS;
  136. put_unaligned_be32(bufflen, &cdb[6]);
  137. return scsi_execute(sdev, cdb, DMA_FROM_DEVICE, buff, bufflen, NULL,
  138. sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
  139. ALUA_FAILOVER_RETRIES, req_flags, 0, NULL);
  140. }
  141. /*
  142. * submit_stpg - Issue a SET TARGET PORT GROUP command
  143. *
  144. * Currently we're only setting the current target port group state
  145. * to 'active/optimized' and let the array firmware figure out
  146. * the states of the remaining groups.
  147. */
  148. static int submit_stpg(struct scsi_device *sdev, int group_id,
  149. struct scsi_sense_hdr *sshdr)
  150. {
  151. u8 cdb[MAX_COMMAND_SIZE];
  152. unsigned char stpg_data[8];
  153. int stpg_len = 8;
  154. int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  155. REQ_FAILFAST_DRIVER;
  156. /* Prepare the data buffer */
  157. memset(stpg_data, 0, stpg_len);
  158. stpg_data[4] = SCSI_ACCESS_STATE_OPTIMAL;
  159. put_unaligned_be16(group_id, &stpg_data[6]);
  160. /* Prepare the command. */
  161. memset(cdb, 0x0, MAX_COMMAND_SIZE);
  162. cdb[0] = MAINTENANCE_OUT;
  163. cdb[1] = MO_SET_TARGET_PGS;
  164. put_unaligned_be32(stpg_len, &cdb[6]);
  165. return scsi_execute(sdev, cdb, DMA_TO_DEVICE, stpg_data, stpg_len, NULL,
  166. sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
  167. ALUA_FAILOVER_RETRIES, req_flags, 0, NULL);
  168. }
  169. static struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size,
  170. int group_id)
  171. {
  172. struct alua_port_group *pg;
  173. if (!id_str || !id_size || !strlen(id_str))
  174. return NULL;
  175. list_for_each_entry(pg, &port_group_list, node) {
  176. if (pg->group_id != group_id)
  177. continue;
  178. if (!pg->device_id_len || pg->device_id_len != id_size)
  179. continue;
  180. if (strncmp(pg->device_id_str, id_str, id_size))
  181. continue;
  182. if (!kref_get_unless_zero(&pg->kref))
  183. continue;
  184. return pg;
  185. }
  186. return NULL;
  187. }
  188. /*
  189. * alua_alloc_pg - Allocate a new port_group structure
  190. * @sdev: scsi device
  191. * @group_id: port group id
  192. * @tpgs: target port group settings
  193. *
  194. * Allocate a new port_group structure for a given
  195. * device.
  196. */
  197. static struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
  198. int group_id, int tpgs)
  199. {
  200. struct alua_port_group *pg, *tmp_pg;
  201. pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
  202. if (!pg)
  203. return ERR_PTR(-ENOMEM);
  204. pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str,
  205. sizeof(pg->device_id_str));
  206. if (pg->device_id_len <= 0) {
  207. /*
  208. * TPGS supported but no device identification found.
  209. * Generate private device identification.
  210. */
  211. sdev_printk(KERN_INFO, sdev,
  212. "%s: No device descriptors found\n",
  213. ALUA_DH_NAME);
  214. pg->device_id_str[0] = '\0';
  215. pg->device_id_len = 0;
  216. }
  217. pg->group_id = group_id;
  218. pg->tpgs = tpgs;
  219. pg->state = SCSI_ACCESS_STATE_OPTIMAL;
  220. pg->valid_states = TPGS_SUPPORT_ALL;
  221. if (optimize_stpg)
  222. pg->flags |= ALUA_OPTIMIZE_STPG;
  223. kref_init(&pg->kref);
  224. INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work);
  225. INIT_LIST_HEAD(&pg->rtpg_list);
  226. INIT_LIST_HEAD(&pg->node);
  227. INIT_LIST_HEAD(&pg->dh_list);
  228. spin_lock_init(&pg->lock);
  229. spin_lock(&port_group_lock);
  230. tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
  231. group_id);
  232. if (tmp_pg) {
  233. spin_unlock(&port_group_lock);
  234. kfree(pg);
  235. return tmp_pg;
  236. }
  237. list_add(&pg->node, &port_group_list);
  238. spin_unlock(&port_group_lock);
  239. return pg;
  240. }
  241. /*
  242. * alua_check_tpgs - Evaluate TPGS setting
  243. * @sdev: device to be checked
  244. *
  245. * Examine the TPGS setting of the sdev to find out if ALUA
  246. * is supported.
  247. */
  248. static int alua_check_tpgs(struct scsi_device *sdev)
  249. {
  250. int tpgs = TPGS_MODE_NONE;
  251. /*
  252. * ALUA support for non-disk devices is fraught with
  253. * difficulties, so disable it for now.
  254. */
  255. if (sdev->type != TYPE_DISK) {
  256. sdev_printk(KERN_INFO, sdev,
  257. "%s: disable for non-disk devices\n",
  258. ALUA_DH_NAME);
  259. return tpgs;
  260. }
  261. tpgs = scsi_device_tpgs(sdev);
  262. switch (tpgs) {
  263. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  264. sdev_printk(KERN_INFO, sdev,
  265. "%s: supports implicit and explicit TPGS\n",
  266. ALUA_DH_NAME);
  267. break;
  268. case TPGS_MODE_EXPLICIT:
  269. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  270. ALUA_DH_NAME);
  271. break;
  272. case TPGS_MODE_IMPLICIT:
  273. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  274. ALUA_DH_NAME);
  275. break;
  276. case TPGS_MODE_NONE:
  277. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  278. ALUA_DH_NAME);
  279. break;
  280. default:
  281. sdev_printk(KERN_INFO, sdev,
  282. "%s: unsupported TPGS setting %d\n",
  283. ALUA_DH_NAME, tpgs);
  284. tpgs = TPGS_MODE_NONE;
  285. break;
  286. }
  287. return tpgs;
  288. }
  289. /*
  290. * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
  291. * @sdev: device to be checked
  292. *
  293. * Extract the relative target port and the target port group
  294. * descriptor from the list of identificators.
  295. */
  296. static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
  297. int tpgs)
  298. {
  299. int rel_port = -1, group_id;
  300. struct alua_port_group *pg, *old_pg = NULL;
  301. bool pg_updated = false;
  302. unsigned long flags;
  303. group_id = scsi_vpd_tpg_id(sdev, &rel_port);
  304. if (group_id < 0) {
  305. /*
  306. * Internal error; TPGS supported but required
  307. * VPD identification descriptors not present.
  308. * Disable ALUA support
  309. */
  310. sdev_printk(KERN_INFO, sdev,
  311. "%s: No target port descriptors found\n",
  312. ALUA_DH_NAME);
  313. return SCSI_DH_DEV_UNSUPP;
  314. }
  315. pg = alua_alloc_pg(sdev, group_id, tpgs);
  316. if (IS_ERR(pg)) {
  317. if (PTR_ERR(pg) == -ENOMEM)
  318. return SCSI_DH_NOMEM;
  319. return SCSI_DH_DEV_UNSUPP;
  320. }
  321. if (pg->device_id_len)
  322. sdev_printk(KERN_INFO, sdev,
  323. "%s: device %s port group %x rel port %x\n",
  324. ALUA_DH_NAME, pg->device_id_str,
  325. group_id, rel_port);
  326. else
  327. sdev_printk(KERN_INFO, sdev,
  328. "%s: port group %x rel port %x\n",
  329. ALUA_DH_NAME, group_id, rel_port);
  330. /* Check for existing port group references */
  331. spin_lock(&h->pg_lock);
  332. old_pg = rcu_dereference_protected(h->pg, lockdep_is_held(&h->pg_lock));
  333. if (old_pg != pg) {
  334. /* port group has changed. Update to new port group */
  335. if (h->pg) {
  336. spin_lock_irqsave(&old_pg->lock, flags);
  337. list_del_rcu(&h->node);
  338. spin_unlock_irqrestore(&old_pg->lock, flags);
  339. }
  340. rcu_assign_pointer(h->pg, pg);
  341. pg_updated = true;
  342. }
  343. spin_lock_irqsave(&pg->lock, flags);
  344. if (pg_updated)
  345. list_add_rcu(&h->node, &pg->dh_list);
  346. spin_unlock_irqrestore(&pg->lock, flags);
  347. alua_rtpg_queue(rcu_dereference_protected(h->pg,
  348. lockdep_is_held(&h->pg_lock)),
  349. sdev, NULL, true);
  350. spin_unlock(&h->pg_lock);
  351. if (old_pg)
  352. kref_put(&old_pg->kref, release_port_group);
  353. return SCSI_DH_OK;
  354. }
  355. static char print_alua_state(unsigned char state)
  356. {
  357. switch (state) {
  358. case SCSI_ACCESS_STATE_OPTIMAL:
  359. return 'A';
  360. case SCSI_ACCESS_STATE_ACTIVE:
  361. return 'N';
  362. case SCSI_ACCESS_STATE_STANDBY:
  363. return 'S';
  364. case SCSI_ACCESS_STATE_UNAVAILABLE:
  365. return 'U';
  366. case SCSI_ACCESS_STATE_LBA:
  367. return 'L';
  368. case SCSI_ACCESS_STATE_OFFLINE:
  369. return 'O';
  370. case SCSI_ACCESS_STATE_TRANSITIONING:
  371. return 'T';
  372. default:
  373. return 'X';
  374. }
  375. }
  376. static int alua_check_sense(struct scsi_device *sdev,
  377. struct scsi_sense_hdr *sense_hdr)
  378. {
  379. switch (sense_hdr->sense_key) {
  380. case NOT_READY:
  381. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) {
  382. /*
  383. * LUN Not Accessible - ALUA state transition
  384. */
  385. alua_check(sdev, false);
  386. return NEEDS_RETRY;
  387. }
  388. break;
  389. case UNIT_ATTENTION:
  390. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) {
  391. /*
  392. * Power On, Reset, or Bus Device Reset.
  393. * Might have obscured a state transition,
  394. * so schedule a recheck.
  395. */
  396. alua_check(sdev, true);
  397. return ADD_TO_MLQUEUE;
  398. }
  399. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
  400. /*
  401. * Device internal reset
  402. */
  403. return ADD_TO_MLQUEUE;
  404. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
  405. /*
  406. * Mode Parameters Changed
  407. */
  408. return ADD_TO_MLQUEUE;
  409. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
  410. /*
  411. * ALUA state changed
  412. */
  413. alua_check(sdev, true);
  414. return ADD_TO_MLQUEUE;
  415. }
  416. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
  417. /*
  418. * Implicit ALUA state transition failed
  419. */
  420. alua_check(sdev, true);
  421. return ADD_TO_MLQUEUE;
  422. }
  423. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  424. /*
  425. * Inquiry data has changed
  426. */
  427. return ADD_TO_MLQUEUE;
  428. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  429. /*
  430. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  431. * when switching controllers on targets like
  432. * Intel Multi-Flex. We can just retry.
  433. */
  434. return ADD_TO_MLQUEUE;
  435. break;
  436. }
  437. return SCSI_RETURN_NOT_HANDLED;
  438. }
  439. /*
  440. * alua_tur - Send a TEST UNIT READY
  441. * @sdev: device to which the TEST UNIT READY command should be send
  442. *
  443. * Send a TEST UNIT READY to @sdev to figure out the device state
  444. * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
  445. * SCSI_DH_OK if no error occurred, and SCSI_DH_IO otherwise.
  446. */
  447. static int alua_tur(struct scsi_device *sdev)
  448. {
  449. struct scsi_sense_hdr sense_hdr;
  450. int retval;
  451. retval = scsi_test_unit_ready(sdev, ALUA_FAILOVER_TIMEOUT * HZ,
  452. ALUA_FAILOVER_RETRIES, &sense_hdr);
  453. if (sense_hdr.sense_key == NOT_READY &&
  454. sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
  455. return SCSI_DH_RETRY;
  456. else if (retval)
  457. return SCSI_DH_IO;
  458. else
  459. return SCSI_DH_OK;
  460. }
  461. /*
  462. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  463. * @sdev: the device to be evaluated.
  464. *
  465. * Evaluate the Target Port Group State.
  466. * Returns SCSI_DH_DEV_OFFLINED if the path is
  467. * found to be unusable.
  468. */
  469. static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
  470. {
  471. struct scsi_sense_hdr sense_hdr;
  472. struct alua_port_group *tmp_pg;
  473. int len, k, off, bufflen = ALUA_RTPG_SIZE;
  474. unsigned char *desc, *buff;
  475. unsigned err, retval;
  476. unsigned int tpg_desc_tbl_off;
  477. unsigned char orig_transition_tmo;
  478. unsigned long flags;
  479. bool transitioning_sense = false;
  480. if (!pg->expiry) {
  481. unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ;
  482. if (pg->transition_tmo)
  483. transition_tmo = pg->transition_tmo * HZ;
  484. pg->expiry = round_jiffies_up(jiffies + transition_tmo);
  485. }
  486. buff = kzalloc(bufflen, GFP_KERNEL);
  487. if (!buff)
  488. return SCSI_DH_DEV_TEMP_BUSY;
  489. retry:
  490. err = 0;
  491. retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
  492. if (retval) {
  493. /*
  494. * Some (broken) implementations have a habit of returning
  495. * an error during things like firmware update etc.
  496. * But if the target only supports active/optimized there's
  497. * not much we can do; it's not that we can switch paths
  498. * or anything.
  499. * So ignore any errors to avoid spurious failures during
  500. * path failover.
  501. */
  502. if ((pg->valid_states & ~TPGS_SUPPORT_OPTIMIZED) == 0) {
  503. sdev_printk(KERN_INFO, sdev,
  504. "%s: ignoring rtpg result %d\n",
  505. ALUA_DH_NAME, retval);
  506. kfree(buff);
  507. return SCSI_DH_OK;
  508. }
  509. if (!scsi_sense_valid(&sense_hdr)) {
  510. sdev_printk(KERN_INFO, sdev,
  511. "%s: rtpg failed, result %d\n",
  512. ALUA_DH_NAME, retval);
  513. kfree(buff);
  514. if (driver_byte(retval) == DRIVER_ERROR)
  515. return SCSI_DH_DEV_TEMP_BUSY;
  516. return SCSI_DH_IO;
  517. }
  518. /*
  519. * submit_rtpg() has failed on existing arrays
  520. * when requesting extended header info, and
  521. * the array doesn't support extended headers,
  522. * even though it shouldn't according to T10.
  523. * The retry without rtpg_ext_hdr_req set
  524. * handles this.
  525. * Note: some arrays return a sense key of ILLEGAL_REQUEST
  526. * with ASC 00h if they don't support the extended header.
  527. */
  528. if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
  529. sense_hdr.sense_key == ILLEGAL_REQUEST) {
  530. pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
  531. goto retry;
  532. }
  533. /*
  534. * If the array returns with 'ALUA state transition'
  535. * sense code here it cannot return RTPG data during
  536. * transition. So set the state to 'transitioning' directly.
  537. */
  538. if (sense_hdr.sense_key == NOT_READY &&
  539. sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) {
  540. transitioning_sense = true;
  541. goto skip_rtpg;
  542. }
  543. /*
  544. * Retry on any other UNIT ATTENTION occurred.
  545. */
  546. if (sense_hdr.sense_key == UNIT_ATTENTION)
  547. err = SCSI_DH_RETRY;
  548. if (err == SCSI_DH_RETRY &&
  549. pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
  550. sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
  551. ALUA_DH_NAME);
  552. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  553. kfree(buff);
  554. return err;
  555. }
  556. sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
  557. ALUA_DH_NAME);
  558. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  559. kfree(buff);
  560. pg->expiry = 0;
  561. return SCSI_DH_IO;
  562. }
  563. len = get_unaligned_be32(&buff[0]) + 4;
  564. if (len > bufflen) {
  565. /* Resubmit with the correct length */
  566. kfree(buff);
  567. bufflen = len;
  568. buff = kmalloc(bufflen, GFP_KERNEL);
  569. if (!buff) {
  570. sdev_printk(KERN_WARNING, sdev,
  571. "%s: kmalloc buffer failed\n",__func__);
  572. /* Temporary failure, bypass */
  573. pg->expiry = 0;
  574. return SCSI_DH_DEV_TEMP_BUSY;
  575. }
  576. goto retry;
  577. }
  578. orig_transition_tmo = pg->transition_tmo;
  579. if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
  580. pg->transition_tmo = buff[5];
  581. else
  582. pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
  583. if (orig_transition_tmo != pg->transition_tmo) {
  584. sdev_printk(KERN_INFO, sdev,
  585. "%s: transition timeout set to %d seconds\n",
  586. ALUA_DH_NAME, pg->transition_tmo);
  587. pg->expiry = jiffies + pg->transition_tmo * HZ;
  588. }
  589. if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
  590. tpg_desc_tbl_off = 8;
  591. else
  592. tpg_desc_tbl_off = 4;
  593. for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
  594. k < len;
  595. k += off, desc += off) {
  596. u16 group_id = get_unaligned_be16(&desc[2]);
  597. spin_lock_irqsave(&port_group_lock, flags);
  598. tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len,
  599. group_id);
  600. spin_unlock_irqrestore(&port_group_lock, flags);
  601. if (tmp_pg) {
  602. if (spin_trylock_irqsave(&tmp_pg->lock, flags)) {
  603. if ((tmp_pg == pg) ||
  604. !(tmp_pg->flags & ALUA_PG_RUNNING)) {
  605. struct alua_dh_data *h;
  606. tmp_pg->state = desc[0] & 0x0f;
  607. tmp_pg->pref = desc[0] >> 7;
  608. rcu_read_lock();
  609. list_for_each_entry_rcu(h,
  610. &tmp_pg->dh_list, node) {
  611. if (!h->sdev)
  612. continue;
  613. h->sdev->access_state = desc[0];
  614. }
  615. rcu_read_unlock();
  616. }
  617. if (tmp_pg == pg)
  618. tmp_pg->valid_states = desc[1];
  619. spin_unlock_irqrestore(&tmp_pg->lock, flags);
  620. }
  621. kref_put(&tmp_pg->kref, release_port_group);
  622. }
  623. off = 8 + (desc[7] * 4);
  624. }
  625. skip_rtpg:
  626. spin_lock_irqsave(&pg->lock, flags);
  627. if (transitioning_sense)
  628. pg->state = SCSI_ACCESS_STATE_TRANSITIONING;
  629. sdev_printk(KERN_INFO, sdev,
  630. "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
  631. ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
  632. pg->pref ? "preferred" : "non-preferred",
  633. pg->valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  634. pg->valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  635. pg->valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  636. pg->valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  637. pg->valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  638. pg->valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  639. pg->valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  640. switch (pg->state) {
  641. case SCSI_ACCESS_STATE_TRANSITIONING:
  642. if (time_before(jiffies, pg->expiry)) {
  643. /* State transition, retry */
  644. pg->interval = ALUA_RTPG_RETRY_DELAY;
  645. err = SCSI_DH_RETRY;
  646. } else {
  647. struct alua_dh_data *h;
  648. /* Transitioning time exceeded, set port to standby */
  649. err = SCSI_DH_IO;
  650. pg->state = SCSI_ACCESS_STATE_STANDBY;
  651. pg->expiry = 0;
  652. rcu_read_lock();
  653. list_for_each_entry_rcu(h, &pg->dh_list, node) {
  654. if (!h->sdev)
  655. continue;
  656. h->sdev->access_state =
  657. (pg->state & SCSI_ACCESS_STATE_MASK);
  658. if (pg->pref)
  659. h->sdev->access_state |=
  660. SCSI_ACCESS_STATE_PREFERRED;
  661. }
  662. rcu_read_unlock();
  663. }
  664. break;
  665. case SCSI_ACCESS_STATE_OFFLINE:
  666. /* Path unusable */
  667. err = SCSI_DH_DEV_OFFLINED;
  668. pg->expiry = 0;
  669. break;
  670. default:
  671. /* Useable path if active */
  672. err = SCSI_DH_OK;
  673. pg->expiry = 0;
  674. break;
  675. }
  676. spin_unlock_irqrestore(&pg->lock, flags);
  677. kfree(buff);
  678. return err;
  679. }
  680. /*
  681. * alua_stpg - Issue a SET TARGET PORT GROUP command
  682. *
  683. * Issue a SET TARGET PORT GROUP command and evaluate the
  684. * response. Returns SCSI_DH_RETRY per default to trigger
  685. * a re-evaluation of the target group state or SCSI_DH_OK
  686. * if no further action needs to be taken.
  687. */
  688. static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
  689. {
  690. int retval;
  691. struct scsi_sense_hdr sense_hdr;
  692. if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
  693. /* Only implicit ALUA supported, retry */
  694. return SCSI_DH_RETRY;
  695. }
  696. switch (pg->state) {
  697. case SCSI_ACCESS_STATE_OPTIMAL:
  698. return SCSI_DH_OK;
  699. case SCSI_ACCESS_STATE_ACTIVE:
  700. if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
  701. !pg->pref &&
  702. (pg->tpgs & TPGS_MODE_IMPLICIT))
  703. return SCSI_DH_OK;
  704. break;
  705. case SCSI_ACCESS_STATE_STANDBY:
  706. case SCSI_ACCESS_STATE_UNAVAILABLE:
  707. break;
  708. case SCSI_ACCESS_STATE_OFFLINE:
  709. return SCSI_DH_IO;
  710. case SCSI_ACCESS_STATE_TRANSITIONING:
  711. break;
  712. default:
  713. sdev_printk(KERN_INFO, sdev,
  714. "%s: stpg failed, unhandled TPGS state %d",
  715. ALUA_DH_NAME, pg->state);
  716. return SCSI_DH_NOSYS;
  717. }
  718. retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
  719. if (retval) {
  720. if (!scsi_sense_valid(&sense_hdr)) {
  721. sdev_printk(KERN_INFO, sdev,
  722. "%s: stpg failed, result %d",
  723. ALUA_DH_NAME, retval);
  724. if (driver_byte(retval) == DRIVER_ERROR)
  725. return SCSI_DH_DEV_TEMP_BUSY;
  726. } else {
  727. sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
  728. ALUA_DH_NAME);
  729. scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
  730. }
  731. }
  732. /* Retry RTPG */
  733. return SCSI_DH_RETRY;
  734. }
  735. static void alua_rtpg_work(struct work_struct *work)
  736. {
  737. struct alua_port_group *pg =
  738. container_of(work, struct alua_port_group, rtpg_work.work);
  739. struct scsi_device *sdev;
  740. LIST_HEAD(qdata_list);
  741. int err = SCSI_DH_OK;
  742. struct alua_queue_data *qdata, *tmp;
  743. unsigned long flags;
  744. spin_lock_irqsave(&pg->lock, flags);
  745. sdev = pg->rtpg_sdev;
  746. if (!sdev) {
  747. WARN_ON(pg->flags & ALUA_PG_RUN_RTPG);
  748. WARN_ON(pg->flags & ALUA_PG_RUN_STPG);
  749. spin_unlock_irqrestore(&pg->lock, flags);
  750. kref_put(&pg->kref, release_port_group);
  751. return;
  752. }
  753. pg->flags |= ALUA_PG_RUNNING;
  754. if (pg->flags & ALUA_PG_RUN_RTPG) {
  755. int state = pg->state;
  756. pg->flags &= ~ALUA_PG_RUN_RTPG;
  757. spin_unlock_irqrestore(&pg->lock, flags);
  758. if (state == SCSI_ACCESS_STATE_TRANSITIONING) {
  759. if (alua_tur(sdev) == SCSI_DH_RETRY) {
  760. spin_lock_irqsave(&pg->lock, flags);
  761. pg->flags &= ~ALUA_PG_RUNNING;
  762. pg->flags |= ALUA_PG_RUN_RTPG;
  763. if (!pg->interval)
  764. pg->interval = ALUA_RTPG_RETRY_DELAY;
  765. spin_unlock_irqrestore(&pg->lock, flags);
  766. queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  767. pg->interval * HZ);
  768. return;
  769. }
  770. /* Send RTPG on failure or if TUR indicates SUCCESS */
  771. }
  772. err = alua_rtpg(sdev, pg);
  773. spin_lock_irqsave(&pg->lock, flags);
  774. if (err == SCSI_DH_RETRY || pg->flags & ALUA_PG_RUN_RTPG) {
  775. pg->flags &= ~ALUA_PG_RUNNING;
  776. if (!pg->interval && !(pg->flags & ALUA_PG_RUN_RTPG))
  777. pg->interval = ALUA_RTPG_RETRY_DELAY;
  778. pg->flags |= ALUA_PG_RUN_RTPG;
  779. spin_unlock_irqrestore(&pg->lock, flags);
  780. queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  781. pg->interval * HZ);
  782. return;
  783. }
  784. if (err != SCSI_DH_OK)
  785. pg->flags &= ~ALUA_PG_RUN_STPG;
  786. }
  787. if (pg->flags & ALUA_PG_RUN_STPG) {
  788. pg->flags &= ~ALUA_PG_RUN_STPG;
  789. spin_unlock_irqrestore(&pg->lock, flags);
  790. err = alua_stpg(sdev, pg);
  791. spin_lock_irqsave(&pg->lock, flags);
  792. if (err == SCSI_DH_RETRY || pg->flags & ALUA_PG_RUN_RTPG) {
  793. pg->flags |= ALUA_PG_RUN_RTPG;
  794. pg->interval = 0;
  795. pg->flags &= ~ALUA_PG_RUNNING;
  796. spin_unlock_irqrestore(&pg->lock, flags);
  797. queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  798. pg->interval * HZ);
  799. return;
  800. }
  801. }
  802. list_splice_init(&pg->rtpg_list, &qdata_list);
  803. pg->rtpg_sdev = NULL;
  804. spin_unlock_irqrestore(&pg->lock, flags);
  805. list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) {
  806. list_del(&qdata->entry);
  807. if (qdata->callback_fn)
  808. qdata->callback_fn(qdata->callback_data, err);
  809. kfree(qdata);
  810. }
  811. spin_lock_irqsave(&pg->lock, flags);
  812. pg->flags &= ~ALUA_PG_RUNNING;
  813. spin_unlock_irqrestore(&pg->lock, flags);
  814. scsi_device_put(sdev);
  815. kref_put(&pg->kref, release_port_group);
  816. }
  817. /**
  818. * alua_rtpg_queue() - cause RTPG to be submitted asynchronously
  819. * @pg: ALUA port group associated with @sdev.
  820. * @sdev: SCSI device for which to submit an RTPG.
  821. * @qdata: Information about the callback to invoke after the RTPG.
  822. * @force: Whether or not to submit an RTPG if a work item that will submit an
  823. * RTPG already has been scheduled.
  824. *
  825. * Returns true if and only if alua_rtpg_work() will be called asynchronously.
  826. * That function is responsible for calling @qdata->fn().
  827. */
  828. static bool alua_rtpg_queue(struct alua_port_group *pg,
  829. struct scsi_device *sdev,
  830. struct alua_queue_data *qdata, bool force)
  831. {
  832. int start_queue = 0;
  833. unsigned long flags;
  834. if (WARN_ON_ONCE(!pg) || scsi_device_get(sdev))
  835. return false;
  836. spin_lock_irqsave(&pg->lock, flags);
  837. if (qdata) {
  838. list_add_tail(&qdata->entry, &pg->rtpg_list);
  839. pg->flags |= ALUA_PG_RUN_STPG;
  840. force = true;
  841. }
  842. if (pg->rtpg_sdev == NULL) {
  843. pg->interval = 0;
  844. pg->flags |= ALUA_PG_RUN_RTPG;
  845. kref_get(&pg->kref);
  846. pg->rtpg_sdev = sdev;
  847. start_queue = 1;
  848. } else if (!(pg->flags & ALUA_PG_RUN_RTPG) && force) {
  849. pg->flags |= ALUA_PG_RUN_RTPG;
  850. /* Do not queue if the worker is already running */
  851. if (!(pg->flags & ALUA_PG_RUNNING)) {
  852. kref_get(&pg->kref);
  853. start_queue = 1;
  854. }
  855. }
  856. spin_unlock_irqrestore(&pg->lock, flags);
  857. if (start_queue) {
  858. if (queue_delayed_work(kaluad_wq, &pg->rtpg_work,
  859. msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS)))
  860. sdev = NULL;
  861. else
  862. kref_put(&pg->kref, release_port_group);
  863. }
  864. if (sdev)
  865. scsi_device_put(sdev);
  866. return true;
  867. }
  868. /*
  869. * alua_initialize - Initialize ALUA state
  870. * @sdev: the device to be initialized
  871. *
  872. * For the prep_fn to work correctly we have
  873. * to initialize the ALUA state for the device.
  874. */
  875. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  876. {
  877. int err = SCSI_DH_DEV_UNSUPP, tpgs;
  878. mutex_lock(&h->init_mutex);
  879. tpgs = alua_check_tpgs(sdev);
  880. if (tpgs != TPGS_MODE_NONE)
  881. err = alua_check_vpd(sdev, h, tpgs);
  882. h->init_error = err;
  883. mutex_unlock(&h->init_mutex);
  884. return err;
  885. }
  886. /*
  887. * alua_set_params - set/unset the optimize flag
  888. * @sdev: device on the path to be activated
  889. * params - parameters in the following format
  890. * "no_of_params\0param1\0param2\0param3\0...\0"
  891. * For example, to set the flag pass the following parameters
  892. * from multipath.conf
  893. * hardware_handler "2 alua 1"
  894. */
  895. static int alua_set_params(struct scsi_device *sdev, const char *params)
  896. {
  897. struct alua_dh_data *h = sdev->handler_data;
  898. struct alua_port_group *pg = NULL;
  899. unsigned int optimize = 0, argc;
  900. const char *p = params;
  901. int result = SCSI_DH_OK;
  902. unsigned long flags;
  903. if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
  904. return -EINVAL;
  905. while (*p++)
  906. ;
  907. if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
  908. return -EINVAL;
  909. rcu_read_lock();
  910. pg = rcu_dereference(h->pg);
  911. if (!pg) {
  912. rcu_read_unlock();
  913. return -ENXIO;
  914. }
  915. spin_lock_irqsave(&pg->lock, flags);
  916. if (optimize)
  917. pg->flags |= ALUA_OPTIMIZE_STPG;
  918. else
  919. pg->flags &= ~ALUA_OPTIMIZE_STPG;
  920. spin_unlock_irqrestore(&pg->lock, flags);
  921. rcu_read_unlock();
  922. return result;
  923. }
  924. /*
  925. * alua_activate - activate a path
  926. * @sdev: device on the path to be activated
  927. *
  928. * We're currently switching the port group to be activated only and
  929. * let the array figure out the rest.
  930. * There may be other arrays which require us to switch all port groups
  931. * based on a certain policy. But until we actually encounter them it
  932. * should be okay.
  933. */
  934. static int alua_activate(struct scsi_device *sdev,
  935. activate_complete fn, void *data)
  936. {
  937. struct alua_dh_data *h = sdev->handler_data;
  938. int err = SCSI_DH_OK;
  939. struct alua_queue_data *qdata;
  940. struct alua_port_group *pg;
  941. qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
  942. if (!qdata) {
  943. err = SCSI_DH_RES_TEMP_UNAVAIL;
  944. goto out;
  945. }
  946. qdata->callback_fn = fn;
  947. qdata->callback_data = data;
  948. mutex_lock(&h->init_mutex);
  949. rcu_read_lock();
  950. pg = rcu_dereference(h->pg);
  951. if (!pg || !kref_get_unless_zero(&pg->kref)) {
  952. rcu_read_unlock();
  953. kfree(qdata);
  954. err = h->init_error;
  955. mutex_unlock(&h->init_mutex);
  956. goto out;
  957. }
  958. rcu_read_unlock();
  959. mutex_unlock(&h->init_mutex);
  960. if (alua_rtpg_queue(pg, sdev, qdata, true))
  961. fn = NULL;
  962. else
  963. err = SCSI_DH_DEV_OFFLINED;
  964. kref_put(&pg->kref, release_port_group);
  965. out:
  966. if (fn)
  967. fn(data, err);
  968. return 0;
  969. }
  970. /*
  971. * alua_check - check path status
  972. * @sdev: device on the path to be checked
  973. *
  974. * Check the device status
  975. */
  976. static void alua_check(struct scsi_device *sdev, bool force)
  977. {
  978. struct alua_dh_data *h = sdev->handler_data;
  979. struct alua_port_group *pg;
  980. rcu_read_lock();
  981. pg = rcu_dereference(h->pg);
  982. if (!pg || !kref_get_unless_zero(&pg->kref)) {
  983. rcu_read_unlock();
  984. return;
  985. }
  986. rcu_read_unlock();
  987. alua_rtpg_queue(pg, sdev, NULL, force);
  988. kref_put(&pg->kref, release_port_group);
  989. }
  990. /*
  991. * alua_prep_fn - request callback
  992. *
  993. * Fail I/O to all paths not in state
  994. * active/optimized or active/non-optimized.
  995. */
  996. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  997. {
  998. struct alua_dh_data *h = sdev->handler_data;
  999. struct alua_port_group *pg;
  1000. unsigned char state = SCSI_ACCESS_STATE_OPTIMAL;
  1001. int ret = BLKPREP_OK;
  1002. rcu_read_lock();
  1003. pg = rcu_dereference(h->pg);
  1004. if (pg)
  1005. state = pg->state;
  1006. rcu_read_unlock();
  1007. if (state == SCSI_ACCESS_STATE_TRANSITIONING)
  1008. ret = BLKPREP_DEFER;
  1009. else if (state != SCSI_ACCESS_STATE_OPTIMAL &&
  1010. state != SCSI_ACCESS_STATE_ACTIVE &&
  1011. state != SCSI_ACCESS_STATE_LBA) {
  1012. ret = BLKPREP_KILL;
  1013. req->rq_flags |= RQF_QUIET;
  1014. }
  1015. return ret;
  1016. }
  1017. static void alua_rescan(struct scsi_device *sdev)
  1018. {
  1019. struct alua_dh_data *h = sdev->handler_data;
  1020. alua_initialize(sdev, h);
  1021. }
  1022. /*
  1023. * alua_bus_attach - Attach device handler
  1024. * @sdev: device to be attached to
  1025. */
  1026. static int alua_bus_attach(struct scsi_device *sdev)
  1027. {
  1028. struct alua_dh_data *h;
  1029. int err;
  1030. h = kzalloc(sizeof(*h) , GFP_KERNEL);
  1031. if (!h)
  1032. return SCSI_DH_NOMEM;
  1033. spin_lock_init(&h->pg_lock);
  1034. rcu_assign_pointer(h->pg, NULL);
  1035. h->init_error = SCSI_DH_OK;
  1036. h->sdev = sdev;
  1037. INIT_LIST_HEAD(&h->node);
  1038. mutex_init(&h->init_mutex);
  1039. err = alua_initialize(sdev, h);
  1040. if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
  1041. goto failed;
  1042. sdev->handler_data = h;
  1043. return SCSI_DH_OK;
  1044. failed:
  1045. kfree(h);
  1046. return err;
  1047. }
  1048. /*
  1049. * alua_bus_detach - Detach device handler
  1050. * @sdev: device to be detached from
  1051. */
  1052. static void alua_bus_detach(struct scsi_device *sdev)
  1053. {
  1054. struct alua_dh_data *h = sdev->handler_data;
  1055. struct alua_port_group *pg;
  1056. spin_lock(&h->pg_lock);
  1057. pg = rcu_dereference_protected(h->pg, lockdep_is_held(&h->pg_lock));
  1058. rcu_assign_pointer(h->pg, NULL);
  1059. spin_unlock(&h->pg_lock);
  1060. if (pg) {
  1061. spin_lock_irq(&pg->lock);
  1062. list_del_rcu(&h->node);
  1063. spin_unlock_irq(&pg->lock);
  1064. kref_put(&pg->kref, release_port_group);
  1065. }
  1066. sdev->handler_data = NULL;
  1067. synchronize_rcu();
  1068. kfree(h);
  1069. }
  1070. static struct scsi_device_handler alua_dh = {
  1071. .name = ALUA_DH_NAME,
  1072. .module = THIS_MODULE,
  1073. .attach = alua_bus_attach,
  1074. .detach = alua_bus_detach,
  1075. .prep_fn = alua_prep_fn,
  1076. .check_sense = alua_check_sense,
  1077. .activate = alua_activate,
  1078. .rescan = alua_rescan,
  1079. .set_params = alua_set_params,
  1080. };
  1081. static int __init alua_init(void)
  1082. {
  1083. int r;
  1084. kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0);
  1085. if (!kaluad_wq)
  1086. return -ENOMEM;
  1087. r = scsi_register_device_handler(&alua_dh);
  1088. if (r != 0) {
  1089. printk(KERN_ERR "%s: Failed to register scsi device handler",
  1090. ALUA_DH_NAME);
  1091. destroy_workqueue(kaluad_wq);
  1092. }
  1093. return r;
  1094. }
  1095. static void __exit alua_exit(void)
  1096. {
  1097. scsi_unregister_device_handler(&alua_dh);
  1098. destroy_workqueue(kaluad_wq);
  1099. }
  1100. module_init(alua_init);
  1101. module_exit(alua_exit);
  1102. MODULE_DESCRIPTION("DM Multipath ALUA support");
  1103. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  1104. MODULE_LICENSE("GPL");
  1105. MODULE_VERSION(ALUA_DH_VER);