scsi_dh_alua.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 <scsi/scsi.h>
  26. #include <scsi/scsi_eh.h>
  27. #include <scsi/scsi_dh.h>
  28. #define ALUA_DH_NAME "alua"
  29. #define ALUA_DH_VER "1.3"
  30. #define TPGS_STATE_OPTIMIZED 0x0
  31. #define TPGS_STATE_NONOPTIMIZED 0x1
  32. #define TPGS_STATE_STANDBY 0x2
  33. #define TPGS_STATE_UNAVAILABLE 0x3
  34. #define TPGS_STATE_LBA_DEPENDENT 0x4
  35. #define TPGS_STATE_OFFLINE 0xe
  36. #define TPGS_STATE_TRANSITIONING 0xf
  37. #define TPGS_SUPPORT_NONE 0x00
  38. #define TPGS_SUPPORT_OPTIMIZED 0x01
  39. #define TPGS_SUPPORT_NONOPTIMIZED 0x02
  40. #define TPGS_SUPPORT_STANDBY 0x04
  41. #define TPGS_SUPPORT_UNAVAILABLE 0x08
  42. #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
  43. #define TPGS_SUPPORT_OFFLINE 0x40
  44. #define TPGS_SUPPORT_TRANSITION 0x80
  45. #define TPGS_MODE_UNINITIALIZED -1
  46. #define TPGS_MODE_NONE 0x0
  47. #define TPGS_MODE_IMPLICIT 0x1
  48. #define TPGS_MODE_EXPLICIT 0x2
  49. #define ALUA_INQUIRY_SIZE 36
  50. #define ALUA_FAILOVER_TIMEOUT (60 * HZ)
  51. #define ALUA_FAILOVER_RETRIES 5
  52. /* flags passed from user level */
  53. #define ALUA_OPTIMIZE_STPG 1
  54. struct alua_dh_data {
  55. int group_id;
  56. int rel_port;
  57. int tpgs;
  58. int state;
  59. unsigned flags; /* used for optimizing STPG */
  60. unsigned char inq[ALUA_INQUIRY_SIZE];
  61. unsigned char *buff;
  62. int bufflen;
  63. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  64. int senselen;
  65. struct scsi_device *sdev;
  66. activate_complete callback_fn;
  67. void *callback_data;
  68. };
  69. #define ALUA_POLICY_SWITCH_CURRENT 0
  70. #define ALUA_POLICY_SWITCH_ALL 1
  71. static char print_alua_state(int);
  72. static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
  73. static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
  74. {
  75. struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
  76. BUG_ON(scsi_dh_data == NULL);
  77. return ((struct alua_dh_data *) scsi_dh_data->buf);
  78. }
  79. static int realloc_buffer(struct alua_dh_data *h, unsigned len)
  80. {
  81. if (h->buff && h->buff != h->inq)
  82. kfree(h->buff);
  83. h->buff = kmalloc(len, GFP_NOIO);
  84. if (!h->buff) {
  85. h->buff = h->inq;
  86. h->bufflen = ALUA_INQUIRY_SIZE;
  87. return 1;
  88. }
  89. h->bufflen = len;
  90. return 0;
  91. }
  92. static struct request *get_alua_req(struct scsi_device *sdev,
  93. void *buffer, unsigned buflen, int rw)
  94. {
  95. struct request *rq;
  96. struct request_queue *q = sdev->request_queue;
  97. rq = blk_get_request(q, rw, GFP_NOIO);
  98. if (!rq) {
  99. sdev_printk(KERN_INFO, sdev,
  100. "%s: blk_get_request failed\n", __func__);
  101. return NULL;
  102. }
  103. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  104. blk_put_request(rq);
  105. sdev_printk(KERN_INFO, sdev,
  106. "%s: blk_rq_map_kern failed\n", __func__);
  107. return NULL;
  108. }
  109. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  110. rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  111. REQ_FAILFAST_DRIVER;
  112. rq->retries = ALUA_FAILOVER_RETRIES;
  113. rq->timeout = ALUA_FAILOVER_TIMEOUT;
  114. return rq;
  115. }
  116. /*
  117. * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
  118. * @sdev: sdev the command should be sent to
  119. */
  120. static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  121. {
  122. struct request *rq;
  123. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  124. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  125. if (!rq)
  126. goto done;
  127. /* Prepare the command. */
  128. rq->cmd[0] = INQUIRY;
  129. rq->cmd[1] = 1;
  130. rq->cmd[2] = 0x83;
  131. rq->cmd[4] = h->bufflen;
  132. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  133. rq->sense = h->sense;
  134. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  135. rq->sense_len = h->senselen = 0;
  136. err = blk_execute_rq(rq->q, NULL, rq, 1);
  137. if (err == -EIO) {
  138. sdev_printk(KERN_INFO, sdev,
  139. "%s: evpd inquiry failed with %x\n",
  140. ALUA_DH_NAME, rq->errors);
  141. h->senselen = rq->sense_len;
  142. err = SCSI_DH_IO;
  143. }
  144. blk_put_request(rq);
  145. done:
  146. return err;
  147. }
  148. /*
  149. * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
  150. * @sdev: sdev the command should be sent to
  151. */
  152. static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  153. {
  154. struct request *rq;
  155. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  156. rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
  157. if (!rq)
  158. goto done;
  159. /* Prepare the command. */
  160. rq->cmd[0] = MAINTENANCE_IN;
  161. rq->cmd[1] = MI_REPORT_TARGET_PGS;
  162. rq->cmd[6] = (h->bufflen >> 24) & 0xff;
  163. rq->cmd[7] = (h->bufflen >> 16) & 0xff;
  164. rq->cmd[8] = (h->bufflen >> 8) & 0xff;
  165. rq->cmd[9] = h->bufflen & 0xff;
  166. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
  167. rq->sense = h->sense;
  168. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  169. rq->sense_len = h->senselen = 0;
  170. err = blk_execute_rq(rq->q, NULL, rq, 1);
  171. if (err == -EIO) {
  172. sdev_printk(KERN_INFO, sdev,
  173. "%s: rtpg failed with %x\n",
  174. ALUA_DH_NAME, rq->errors);
  175. h->senselen = rq->sense_len;
  176. err = SCSI_DH_IO;
  177. }
  178. blk_put_request(rq);
  179. done:
  180. return err;
  181. }
  182. /*
  183. * alua_stpg - Evaluate SET TARGET GROUP STATES
  184. * @sdev: the device to be evaluated
  185. * @state: the new target group state
  186. *
  187. * Send a SET TARGET GROUP STATES command to the device.
  188. * We only have to test here if we should resubmit the command;
  189. * any other error is assumed as a failure.
  190. */
  191. static void stpg_endio(struct request *req, int error)
  192. {
  193. struct alua_dh_data *h = req->end_io_data;
  194. struct scsi_sense_hdr sense_hdr;
  195. unsigned err = SCSI_DH_OK;
  196. if (error || host_byte(req->errors) != DID_OK ||
  197. msg_byte(req->errors) != COMMAND_COMPLETE) {
  198. err = SCSI_DH_IO;
  199. goto done;
  200. }
  201. if (h->senselen > 0) {
  202. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  203. &sense_hdr);
  204. if (!err) {
  205. err = SCSI_DH_IO;
  206. goto done;
  207. }
  208. err = alua_check_sense(h->sdev, &sense_hdr);
  209. if (err == ADD_TO_MLQUEUE) {
  210. err = SCSI_DH_RETRY;
  211. goto done;
  212. }
  213. sdev_printk(KERN_INFO, h->sdev,
  214. "%s: stpg sense code: %02x/%02x/%02x\n",
  215. ALUA_DH_NAME, sense_hdr.sense_key,
  216. sense_hdr.asc, sense_hdr.ascq);
  217. err = SCSI_DH_IO;
  218. }
  219. if (err == SCSI_DH_OK) {
  220. h->state = TPGS_STATE_OPTIMIZED;
  221. sdev_printk(KERN_INFO, h->sdev,
  222. "%s: port group %02x switched to state %c\n",
  223. ALUA_DH_NAME, h->group_id,
  224. print_alua_state(h->state));
  225. }
  226. done:
  227. req->end_io_data = NULL;
  228. __blk_put_request(req->q, req);
  229. if (h->callback_fn) {
  230. h->callback_fn(h->callback_data, err);
  231. h->callback_fn = h->callback_data = NULL;
  232. }
  233. return;
  234. }
  235. /*
  236. * submit_stpg - Issue a SET TARGET GROUP STATES command
  237. *
  238. * Currently we're only setting the current target port group state
  239. * to 'active/optimized' and let the array firmware figure out
  240. * the states of the remaining groups.
  241. */
  242. static unsigned submit_stpg(struct alua_dh_data *h)
  243. {
  244. struct request *rq;
  245. int stpg_len = 8;
  246. struct scsi_device *sdev = h->sdev;
  247. /* Prepare the data buffer */
  248. memset(h->buff, 0, stpg_len);
  249. h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
  250. h->buff[6] = (h->group_id >> 8) & 0xff;
  251. h->buff[7] = h->group_id & 0xff;
  252. rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
  253. if (!rq)
  254. return SCSI_DH_RES_TEMP_UNAVAIL;
  255. /* Prepare the command. */
  256. rq->cmd[0] = MAINTENANCE_OUT;
  257. rq->cmd[1] = MO_SET_TARGET_PGS;
  258. rq->cmd[6] = (stpg_len >> 24) & 0xff;
  259. rq->cmd[7] = (stpg_len >> 16) & 0xff;
  260. rq->cmd[8] = (stpg_len >> 8) & 0xff;
  261. rq->cmd[9] = stpg_len & 0xff;
  262. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
  263. rq->sense = h->sense;
  264. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  265. rq->sense_len = h->senselen = 0;
  266. rq->end_io_data = h;
  267. blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
  268. return SCSI_DH_OK;
  269. }
  270. /*
  271. * alua_check_tpgs - Evaluate TPGS setting
  272. * @sdev: device to be checked
  273. *
  274. * Examine the TPGS setting of the sdev to find out if ALUA
  275. * is supported.
  276. */
  277. static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
  278. {
  279. int err = SCSI_DH_OK;
  280. h->tpgs = scsi_device_tpgs(sdev);
  281. switch (h->tpgs) {
  282. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  283. sdev_printk(KERN_INFO, sdev,
  284. "%s: supports implicit and explicit TPGS\n",
  285. ALUA_DH_NAME);
  286. break;
  287. case TPGS_MODE_EXPLICIT:
  288. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  289. ALUA_DH_NAME);
  290. break;
  291. case TPGS_MODE_IMPLICIT:
  292. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  293. ALUA_DH_NAME);
  294. break;
  295. default:
  296. h->tpgs = TPGS_MODE_NONE;
  297. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  298. ALUA_DH_NAME);
  299. err = SCSI_DH_DEV_UNSUPP;
  300. break;
  301. }
  302. return err;
  303. }
  304. /*
  305. * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
  306. * @sdev: device to be checked
  307. *
  308. * Extract the relative target port and the target port group
  309. * descriptor from the list of identificators.
  310. */
  311. static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  312. {
  313. int len;
  314. unsigned err;
  315. unsigned char *d;
  316. retry:
  317. err = submit_vpd_inquiry(sdev, h);
  318. if (err != SCSI_DH_OK)
  319. return err;
  320. /* Check if vpd page exceeds initial buffer */
  321. len = (h->buff[2] << 8) + h->buff[3] + 4;
  322. if (len > h->bufflen) {
  323. /* Resubmit with the correct length */
  324. if (realloc_buffer(h, len)) {
  325. sdev_printk(KERN_WARNING, sdev,
  326. "%s: kmalloc buffer failed\n",
  327. ALUA_DH_NAME);
  328. /* Temporary failure, bypass */
  329. return SCSI_DH_DEV_TEMP_BUSY;
  330. }
  331. goto retry;
  332. }
  333. /*
  334. * Now look for the correct descriptor.
  335. */
  336. d = h->buff + 4;
  337. while (d < h->buff + len) {
  338. switch (d[1] & 0xf) {
  339. case 0x4:
  340. /* Relative target port */
  341. h->rel_port = (d[6] << 8) + d[7];
  342. break;
  343. case 0x5:
  344. /* Target port group */
  345. h->group_id = (d[6] << 8) + d[7];
  346. break;
  347. default:
  348. break;
  349. }
  350. d += d[3] + 4;
  351. }
  352. if (h->group_id == -1) {
  353. /*
  354. * Internal error; TPGS supported but required
  355. * VPD identification descriptors not present.
  356. * Disable ALUA support
  357. */
  358. sdev_printk(KERN_INFO, sdev,
  359. "%s: No target port descriptors found\n",
  360. ALUA_DH_NAME);
  361. h->state = TPGS_STATE_OPTIMIZED;
  362. h->tpgs = TPGS_MODE_NONE;
  363. err = SCSI_DH_DEV_UNSUPP;
  364. } else {
  365. sdev_printk(KERN_INFO, sdev,
  366. "%s: port group %02x rel port %02x\n",
  367. ALUA_DH_NAME, h->group_id, h->rel_port);
  368. }
  369. return err;
  370. }
  371. static char print_alua_state(int state)
  372. {
  373. switch (state) {
  374. case TPGS_STATE_OPTIMIZED:
  375. return 'A';
  376. case TPGS_STATE_NONOPTIMIZED:
  377. return 'N';
  378. case TPGS_STATE_STANDBY:
  379. return 'S';
  380. case TPGS_STATE_UNAVAILABLE:
  381. return 'U';
  382. case TPGS_STATE_LBA_DEPENDENT:
  383. return 'L';
  384. case TPGS_STATE_OFFLINE:
  385. return 'O';
  386. case TPGS_STATE_TRANSITIONING:
  387. return 'T';
  388. default:
  389. return 'X';
  390. }
  391. }
  392. static int alua_check_sense(struct scsi_device *sdev,
  393. struct scsi_sense_hdr *sense_hdr)
  394. {
  395. switch (sense_hdr->sense_key) {
  396. case NOT_READY:
  397. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
  398. /*
  399. * LUN Not Accessible - ALUA state transition
  400. */
  401. return ADD_TO_MLQUEUE;
  402. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
  403. /*
  404. * LUN Not Accessible -- Target port in standby state
  405. */
  406. return SUCCESS;
  407. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
  408. /*
  409. * LUN Not Accessible -- Target port in unavailable state
  410. */
  411. return SUCCESS;
  412. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
  413. /*
  414. * LUN Not Ready -- Offline
  415. */
  416. return SUCCESS;
  417. break;
  418. case UNIT_ATTENTION:
  419. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  420. /*
  421. * Power On, Reset, or Bus Device Reset, just retry.
  422. */
  423. return ADD_TO_MLQUEUE;
  424. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
  425. /*
  426. * Mode Parameters Changed
  427. */
  428. return ADD_TO_MLQUEUE;
  429. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
  430. /*
  431. * ALUA state changed
  432. */
  433. return ADD_TO_MLQUEUE;
  434. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
  435. /*
  436. * Implicit ALUA state transition failed
  437. */
  438. return ADD_TO_MLQUEUE;
  439. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
  440. /*
  441. * Inquiry data has changed
  442. */
  443. return ADD_TO_MLQUEUE;
  444. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
  445. /*
  446. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  447. * when switching controllers on targets like
  448. * Intel Multi-Flex. We can just retry.
  449. */
  450. return ADD_TO_MLQUEUE;
  451. break;
  452. }
  453. return SCSI_RETURN_NOT_HANDLED;
  454. }
  455. /*
  456. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  457. * @sdev: the device to be evaluated.
  458. *
  459. * Evaluate the Target Port Group State.
  460. * Returns SCSI_DH_DEV_OFFLINED if the path is
  461. * found to be unusable.
  462. */
  463. static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  464. {
  465. struct scsi_sense_hdr sense_hdr;
  466. int len, k, off, valid_states = 0;
  467. unsigned char *ucp;
  468. unsigned err;
  469. unsigned long expiry, interval = 1000;
  470. expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
  471. retry:
  472. err = submit_rtpg(sdev, h);
  473. if (err == SCSI_DH_IO && h->senselen > 0) {
  474. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  475. &sense_hdr);
  476. if (!err)
  477. return SCSI_DH_IO;
  478. err = alua_check_sense(sdev, &sense_hdr);
  479. if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
  480. goto retry;
  481. sdev_printk(KERN_INFO, sdev,
  482. "%s: rtpg sense code %02x/%02x/%02x\n",
  483. ALUA_DH_NAME, sense_hdr.sense_key,
  484. sense_hdr.asc, sense_hdr.ascq);
  485. err = SCSI_DH_IO;
  486. }
  487. if (err != SCSI_DH_OK)
  488. return err;
  489. len = (h->buff[0] << 24) + (h->buff[1] << 16) +
  490. (h->buff[2] << 8) + h->buff[3] + 4;
  491. if (len > h->bufflen) {
  492. /* Resubmit with the correct length */
  493. if (realloc_buffer(h, len)) {
  494. sdev_printk(KERN_WARNING, sdev,
  495. "%s: kmalloc buffer failed\n",__func__);
  496. /* Temporary failure, bypass */
  497. return SCSI_DH_DEV_TEMP_BUSY;
  498. }
  499. goto retry;
  500. }
  501. for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
  502. if (h->group_id == (ucp[2] << 8) + ucp[3]) {
  503. h->state = ucp[0] & 0x0f;
  504. valid_states = ucp[1];
  505. }
  506. off = 8 + (ucp[7] * 4);
  507. }
  508. sdev_printk(KERN_INFO, sdev,
  509. "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
  510. ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
  511. valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  512. valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  513. valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  514. valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  515. valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  516. valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  517. valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  518. switch (h->state) {
  519. case TPGS_STATE_TRANSITIONING:
  520. if (time_before(jiffies, expiry)) {
  521. /* State transition, retry */
  522. interval *= 2;
  523. msleep(interval);
  524. goto retry;
  525. }
  526. /* Transitioning time exceeded, set port to standby */
  527. err = SCSI_DH_RETRY;
  528. h->state = TPGS_STATE_STANDBY;
  529. break;
  530. case TPGS_STATE_OFFLINE:
  531. case TPGS_STATE_UNAVAILABLE:
  532. /* Path unusable for unavailable/offline */
  533. err = SCSI_DH_DEV_OFFLINED;
  534. break;
  535. default:
  536. /* Useable path if active */
  537. err = SCSI_DH_OK;
  538. break;
  539. }
  540. return err;
  541. }
  542. /*
  543. * alua_initialize - Initialize ALUA state
  544. * @sdev: the device to be initialized
  545. *
  546. * For the prep_fn to work correctly we have
  547. * to initialize the ALUA state for the device.
  548. */
  549. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  550. {
  551. int err;
  552. err = alua_check_tpgs(sdev, h);
  553. if (err != SCSI_DH_OK)
  554. goto out;
  555. err = alua_vpd_inquiry(sdev, h);
  556. if (err != SCSI_DH_OK)
  557. goto out;
  558. err = alua_rtpg(sdev, h);
  559. if (err != SCSI_DH_OK)
  560. goto out;
  561. out:
  562. return err;
  563. }
  564. /*
  565. * alua_set_params - set/unset the optimize flag
  566. * @sdev: device on the path to be activated
  567. * params - parameters in the following format
  568. * "no_of_params\0param1\0param2\0param3\0...\0"
  569. * For example, to set the flag pass the following parameters
  570. * from multipath.conf
  571. * hardware_handler "2 alua 1"
  572. */
  573. static int alua_set_params(struct scsi_device *sdev, const char *params)
  574. {
  575. struct alua_dh_data *h = get_alua_data(sdev);
  576. unsigned int optimize = 0, argc;
  577. const char *p = params;
  578. int result = SCSI_DH_OK;
  579. if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
  580. return -EINVAL;
  581. while (*p++)
  582. ;
  583. if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
  584. return -EINVAL;
  585. if (optimize)
  586. h->flags |= ALUA_OPTIMIZE_STPG;
  587. else
  588. h->flags &= ~ALUA_OPTIMIZE_STPG;
  589. return result;
  590. }
  591. /*
  592. * alua_activate - activate a path
  593. * @sdev: device on the path to be activated
  594. *
  595. * We're currently switching the port group to be activated only and
  596. * let the array figure out the rest.
  597. * There may be other arrays which require us to switch all port groups
  598. * based on a certain policy. But until we actually encounter them it
  599. * should be okay.
  600. */
  601. static int alua_activate(struct scsi_device *sdev,
  602. activate_complete fn, void *data)
  603. {
  604. struct alua_dh_data *h = get_alua_data(sdev);
  605. int err = SCSI_DH_OK;
  606. err = alua_rtpg(sdev, h);
  607. if (err != SCSI_DH_OK)
  608. goto out;
  609. if (h->tpgs & TPGS_MODE_EXPLICIT &&
  610. h->state != TPGS_STATE_OPTIMIZED &&
  611. h->state != TPGS_STATE_LBA_DEPENDENT) {
  612. h->callback_fn = fn;
  613. h->callback_data = data;
  614. err = submit_stpg(h);
  615. if (err == SCSI_DH_OK)
  616. return 0;
  617. h->callback_fn = h->callback_data = NULL;
  618. }
  619. out:
  620. if (fn)
  621. fn(data, err);
  622. return 0;
  623. }
  624. /*
  625. * alua_prep_fn - request callback
  626. *
  627. * Fail I/O to all paths not in state
  628. * active/optimized or active/non-optimized.
  629. */
  630. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  631. {
  632. struct alua_dh_data *h = get_alua_data(sdev);
  633. int ret = BLKPREP_OK;
  634. if (h->state == TPGS_STATE_TRANSITIONING)
  635. ret = BLKPREP_DEFER;
  636. else if (h->state != TPGS_STATE_OPTIMIZED &&
  637. h->state != TPGS_STATE_NONOPTIMIZED &&
  638. h->state != TPGS_STATE_LBA_DEPENDENT) {
  639. ret = BLKPREP_KILL;
  640. req->cmd_flags |= REQ_QUIET;
  641. }
  642. return ret;
  643. }
  644. static bool alua_match(struct scsi_device *sdev)
  645. {
  646. return (scsi_device_tpgs(sdev) != 0);
  647. }
  648. static int alua_bus_attach(struct scsi_device *sdev);
  649. static void alua_bus_detach(struct scsi_device *sdev);
  650. static struct scsi_device_handler alua_dh = {
  651. .name = ALUA_DH_NAME,
  652. .module = THIS_MODULE,
  653. .attach = alua_bus_attach,
  654. .detach = alua_bus_detach,
  655. .prep_fn = alua_prep_fn,
  656. .check_sense = alua_check_sense,
  657. .activate = alua_activate,
  658. .set_params = alua_set_params,
  659. .match = alua_match,
  660. };
  661. /*
  662. * alua_bus_attach - Attach device handler
  663. * @sdev: device to be attached to
  664. */
  665. static int alua_bus_attach(struct scsi_device *sdev)
  666. {
  667. struct scsi_dh_data *scsi_dh_data;
  668. struct alua_dh_data *h;
  669. unsigned long flags;
  670. int err = SCSI_DH_OK;
  671. scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
  672. + sizeof(*h) , GFP_KERNEL);
  673. if (!scsi_dh_data) {
  674. sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
  675. ALUA_DH_NAME);
  676. return -ENOMEM;
  677. }
  678. scsi_dh_data->scsi_dh = &alua_dh;
  679. h = (struct alua_dh_data *) scsi_dh_data->buf;
  680. h->tpgs = TPGS_MODE_UNINITIALIZED;
  681. h->state = TPGS_STATE_OPTIMIZED;
  682. h->group_id = -1;
  683. h->rel_port = -1;
  684. h->buff = h->inq;
  685. h->bufflen = ALUA_INQUIRY_SIZE;
  686. h->sdev = sdev;
  687. err = alua_initialize(sdev, h);
  688. if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
  689. goto failed;
  690. if (!try_module_get(THIS_MODULE))
  691. goto failed;
  692. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  693. sdev->scsi_dh_data = scsi_dh_data;
  694. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  695. sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
  696. return 0;
  697. failed:
  698. kfree(scsi_dh_data);
  699. sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
  700. return -EINVAL;
  701. }
  702. /*
  703. * alua_bus_detach - Detach device handler
  704. * @sdev: device to be detached from
  705. */
  706. static void alua_bus_detach(struct scsi_device *sdev)
  707. {
  708. struct scsi_dh_data *scsi_dh_data;
  709. struct alua_dh_data *h;
  710. unsigned long flags;
  711. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  712. scsi_dh_data = sdev->scsi_dh_data;
  713. sdev->scsi_dh_data = NULL;
  714. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  715. h = (struct alua_dh_data *) scsi_dh_data->buf;
  716. if (h->buff && h->inq != h->buff)
  717. kfree(h->buff);
  718. kfree(scsi_dh_data);
  719. module_put(THIS_MODULE);
  720. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
  721. }
  722. static int __init alua_init(void)
  723. {
  724. int r;
  725. r = scsi_register_device_handler(&alua_dh);
  726. if (r != 0)
  727. printk(KERN_ERR "%s: Failed to register scsi device handler",
  728. ALUA_DH_NAME);
  729. return r;
  730. }
  731. static void __exit alua_exit(void)
  732. {
  733. scsi_unregister_device_handler(&alua_dh);
  734. }
  735. module_init(alua_init);
  736. module_exit(alua_exit);
  737. MODULE_DESCRIPTION("DM Multipath ALUA support");
  738. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  739. MODULE_LICENSE("GPL");
  740. MODULE_VERSION(ALUA_DH_VER);