scsi_dh_alua.c 20 KB

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