scsi_dh_alua.c 21 KB

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