scsi_dh_alua.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 err = SCSI_DH_RES_TEMP_UNAVAIL;
  271. int stpg_len = 8;
  272. struct scsi_device *sdev = h->sdev;
  273. /* Prepare the data buffer */
  274. memset(h->buff, 0, stpg_len);
  275. h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
  276. h->buff[6] = (h->group_id >> 8) & 0xff;
  277. h->buff[7] = h->group_id & 0xff;
  278. rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
  279. if (!rq)
  280. return SCSI_DH_RES_TEMP_UNAVAIL;
  281. /* Prepare the command. */
  282. rq->cmd[0] = MAINTENANCE_OUT;
  283. rq->cmd[1] = MO_SET_TARGET_PGS;
  284. rq->cmd[6] = (stpg_len >> 24) & 0xff;
  285. rq->cmd[7] = (stpg_len >> 16) & 0xff;
  286. rq->cmd[8] = (stpg_len >> 8) & 0xff;
  287. rq->cmd[9] = stpg_len & 0xff;
  288. rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
  289. rq->sense = h->sense;
  290. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  291. rq->sense_len = h->senselen = 0;
  292. rq->end_io_data = h;
  293. blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
  294. return err;
  295. }
  296. /*
  297. * alua_std_inquiry - Evaluate standard INQUIRY command
  298. * @sdev: device to be checked
  299. *
  300. * Just extract the TPGS setting to find out if ALUA
  301. * is supported.
  302. */
  303. static int alua_std_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  304. {
  305. int err;
  306. err = submit_std_inquiry(sdev, h);
  307. if (err != SCSI_DH_OK)
  308. return err;
  309. /* Check TPGS setting */
  310. h->tpgs = (h->inq[5] >> 4) & 0x3;
  311. switch (h->tpgs) {
  312. case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
  313. sdev_printk(KERN_INFO, sdev,
  314. "%s: supports implicit and explicit TPGS\n",
  315. ALUA_DH_NAME);
  316. break;
  317. case TPGS_MODE_EXPLICIT:
  318. sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
  319. ALUA_DH_NAME);
  320. break;
  321. case TPGS_MODE_IMPLICIT:
  322. sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
  323. ALUA_DH_NAME);
  324. break;
  325. default:
  326. h->tpgs = TPGS_MODE_NONE;
  327. sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
  328. ALUA_DH_NAME);
  329. err = SCSI_DH_DEV_UNSUPP;
  330. break;
  331. }
  332. return err;
  333. }
  334. /*
  335. * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
  336. * @sdev: device to be checked
  337. *
  338. * Extract the relative target port and the target port group
  339. * descriptor from the list of identificators.
  340. */
  341. static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
  342. {
  343. int len;
  344. unsigned err;
  345. unsigned char *d;
  346. retry:
  347. err = submit_vpd_inquiry(sdev, h);
  348. if (err != SCSI_DH_OK)
  349. return err;
  350. /* Check if vpd page exceeds initial buffer */
  351. len = (h->buff[2] << 8) + h->buff[3] + 4;
  352. if (len > h->bufflen) {
  353. /* Resubmit with the correct length */
  354. if (realloc_buffer(h, len)) {
  355. sdev_printk(KERN_WARNING, sdev,
  356. "%s: kmalloc buffer failed\n",
  357. ALUA_DH_NAME);
  358. /* Temporary failure, bypass */
  359. return SCSI_DH_DEV_TEMP_BUSY;
  360. }
  361. goto retry;
  362. }
  363. /*
  364. * Now look for the correct descriptor.
  365. */
  366. d = h->buff + 4;
  367. while (d < h->buff + len) {
  368. switch (d[1] & 0xf) {
  369. case 0x4:
  370. /* Relative target port */
  371. h->rel_port = (d[6] << 8) + d[7];
  372. break;
  373. case 0x5:
  374. /* Target port group */
  375. h->group_id = (d[6] << 8) + d[7];
  376. break;
  377. default:
  378. break;
  379. }
  380. d += d[3] + 4;
  381. }
  382. if (h->group_id == -1) {
  383. /*
  384. * Internal error; TPGS supported but required
  385. * VPD identification descriptors not present.
  386. * Disable ALUA support
  387. */
  388. sdev_printk(KERN_INFO, sdev,
  389. "%s: No target port descriptors found\n",
  390. ALUA_DH_NAME);
  391. h->state = TPGS_STATE_OPTIMIZED;
  392. h->tpgs = TPGS_MODE_NONE;
  393. err = SCSI_DH_DEV_UNSUPP;
  394. } else {
  395. sdev_printk(KERN_INFO, sdev,
  396. "%s: port group %02x rel port %02x\n",
  397. ALUA_DH_NAME, h->group_id, h->rel_port);
  398. }
  399. return err;
  400. }
  401. static char print_alua_state(int state)
  402. {
  403. switch (state) {
  404. case TPGS_STATE_OPTIMIZED:
  405. return 'A';
  406. case TPGS_STATE_NONOPTIMIZED:
  407. return 'N';
  408. case TPGS_STATE_STANDBY:
  409. return 'S';
  410. case TPGS_STATE_UNAVAILABLE:
  411. return 'U';
  412. case TPGS_STATE_LBA_DEPENDENT:
  413. return 'L';
  414. case TPGS_STATE_OFFLINE:
  415. return 'O';
  416. case TPGS_STATE_TRANSITIONING:
  417. return 'T';
  418. default:
  419. return 'X';
  420. }
  421. }
  422. static int alua_check_sense(struct scsi_device *sdev,
  423. struct scsi_sense_hdr *sense_hdr)
  424. {
  425. switch (sense_hdr->sense_key) {
  426. case NOT_READY:
  427. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
  428. /*
  429. * LUN Not Accessible - ALUA state transition
  430. */
  431. return ADD_TO_MLQUEUE;
  432. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
  433. /*
  434. * LUN Not Accessible -- Target port in standby state
  435. */
  436. return SUCCESS;
  437. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
  438. /*
  439. * LUN Not Accessible -- Target port in unavailable state
  440. */
  441. return SUCCESS;
  442. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
  443. /*
  444. * LUN Not Ready -- Offline
  445. */
  446. return SUCCESS;
  447. break;
  448. case UNIT_ATTENTION:
  449. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  450. /*
  451. * Power On, Reset, or Bus Device Reset, just retry.
  452. */
  453. return ADD_TO_MLQUEUE;
  454. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
  455. /*
  456. * ALUA state changed
  457. */
  458. return ADD_TO_MLQUEUE;
  459. }
  460. if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
  461. /*
  462. * Implicit ALUA state transition failed
  463. */
  464. return ADD_TO_MLQUEUE;
  465. }
  466. if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) {
  467. /*
  468. * REPORTED_LUNS_DATA_HAS_CHANGED is reported
  469. * when switching controllers on targets like
  470. * Intel Multi-Flex. We can just retry.
  471. */
  472. return ADD_TO_MLQUEUE;
  473. }
  474. break;
  475. }
  476. return SCSI_RETURN_NOT_HANDLED;
  477. }
  478. /*
  479. * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
  480. * @sdev: the device to be evaluated.
  481. *
  482. * Evaluate the Target Port Group State.
  483. * Returns SCSI_DH_DEV_OFFLINED if the path is
  484. * found to be unuseable.
  485. */
  486. static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
  487. {
  488. struct scsi_sense_hdr sense_hdr;
  489. int len, k, off, valid_states = 0;
  490. char *ucp;
  491. unsigned err;
  492. unsigned long expiry, interval = 10;
  493. expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
  494. retry:
  495. err = submit_rtpg(sdev, h);
  496. if (err == SCSI_DH_IO && h->senselen > 0) {
  497. err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
  498. &sense_hdr);
  499. if (!err)
  500. return SCSI_DH_IO;
  501. err = alua_check_sense(sdev, &sense_hdr);
  502. if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
  503. goto retry;
  504. sdev_printk(KERN_INFO, sdev,
  505. "%s: rtpg sense code %02x/%02x/%02x\n",
  506. ALUA_DH_NAME, sense_hdr.sense_key,
  507. sense_hdr.asc, sense_hdr.ascq);
  508. err = SCSI_DH_IO;
  509. }
  510. if (err != SCSI_DH_OK)
  511. return err;
  512. len = (h->buff[0] << 24) + (h->buff[1] << 16) +
  513. (h->buff[2] << 8) + h->buff[3] + 4;
  514. if (len > h->bufflen) {
  515. /* Resubmit with the correct length */
  516. if (realloc_buffer(h, len)) {
  517. sdev_printk(KERN_WARNING, sdev,
  518. "%s: kmalloc buffer failed\n",__func__);
  519. /* Temporary failure, bypass */
  520. return SCSI_DH_DEV_TEMP_BUSY;
  521. }
  522. goto retry;
  523. }
  524. for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
  525. if (h->group_id == (ucp[2] << 8) + ucp[3]) {
  526. h->state = ucp[0] & 0x0f;
  527. valid_states = ucp[1];
  528. }
  529. off = 8 + (ucp[7] * 4);
  530. }
  531. sdev_printk(KERN_INFO, sdev,
  532. "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
  533. ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
  534. valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
  535. valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
  536. valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
  537. valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
  538. valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
  539. valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
  540. valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
  541. switch (h->state) {
  542. case TPGS_STATE_TRANSITIONING:
  543. if (time_before(jiffies, expiry)) {
  544. /* State transition, retry */
  545. interval *= 10;
  546. msleep(interval);
  547. goto retry;
  548. }
  549. /* Transitioning time exceeded, set port to standby */
  550. err = SCSI_DH_RETRY;
  551. h->state = TPGS_STATE_STANDBY;
  552. break;
  553. case TPGS_STATE_OFFLINE:
  554. case TPGS_STATE_UNAVAILABLE:
  555. /* Path unuseable for unavailable/offline */
  556. err = SCSI_DH_DEV_OFFLINED;
  557. break;
  558. default:
  559. /* Useable path if active */
  560. err = SCSI_DH_OK;
  561. break;
  562. }
  563. return err;
  564. }
  565. /*
  566. * alua_initialize - Initialize ALUA state
  567. * @sdev: the device to be initialized
  568. *
  569. * For the prep_fn to work correctly we have
  570. * to initialize the ALUA state for the device.
  571. */
  572. static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
  573. {
  574. int err;
  575. err = alua_std_inquiry(sdev, h);
  576. if (err != SCSI_DH_OK)
  577. goto out;
  578. err = alua_vpd_inquiry(sdev, h);
  579. if (err != SCSI_DH_OK)
  580. goto out;
  581. err = alua_rtpg(sdev, h);
  582. if (err != SCSI_DH_OK)
  583. goto out;
  584. out:
  585. return err;
  586. }
  587. /*
  588. * alua_activate - activate a path
  589. * @sdev: device on the path to be activated
  590. *
  591. * We're currently switching the port group to be activated only and
  592. * let the array figure out the rest.
  593. * There may be other arrays which require us to switch all port groups
  594. * based on a certain policy. But until we actually encounter them it
  595. * should be okay.
  596. */
  597. static int alua_activate(struct scsi_device *sdev,
  598. activate_complete fn, void *data)
  599. {
  600. struct alua_dh_data *h = get_alua_data(sdev);
  601. int err = SCSI_DH_OK;
  602. if (h->group_id != -1) {
  603. err = alua_rtpg(sdev, h);
  604. if (err != SCSI_DH_OK)
  605. goto out;
  606. }
  607. if (h->tpgs & TPGS_MODE_EXPLICIT &&
  608. h->state != TPGS_STATE_OPTIMIZED &&
  609. h->state != TPGS_STATE_LBA_DEPENDENT) {
  610. h->callback_fn = fn;
  611. h->callback_data = data;
  612. err = submit_stpg(h);
  613. if (err == SCSI_DH_OK)
  614. return 0;
  615. h->callback_fn = h->callback_data = NULL;
  616. }
  617. out:
  618. if (fn)
  619. fn(data, err);
  620. return 0;
  621. }
  622. /*
  623. * alua_prep_fn - request callback
  624. *
  625. * Fail I/O to all paths not in state
  626. * active/optimized or active/non-optimized.
  627. */
  628. static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
  629. {
  630. struct alua_dh_data *h = get_alua_data(sdev);
  631. int ret = BLKPREP_OK;
  632. if (h->state == TPGS_STATE_TRANSITIONING)
  633. ret = BLKPREP_DEFER;
  634. else if (h->state != TPGS_STATE_OPTIMIZED &&
  635. h->state != TPGS_STATE_NONOPTIMIZED &&
  636. h->state != TPGS_STATE_LBA_DEPENDENT) {
  637. ret = BLKPREP_KILL;
  638. req->cmd_flags |= REQ_QUIET;
  639. }
  640. return ret;
  641. }
  642. static const struct scsi_dh_devlist alua_dev_list[] = {
  643. {"HP", "MSA VOLUME" },
  644. {"HP", "HSV101" },
  645. {"HP", "HSV111" },
  646. {"HP", "HSV200" },
  647. {"HP", "HSV210" },
  648. {"HP", "HSV300" },
  649. {"IBM", "2107900" },
  650. {"IBM", "2145" },
  651. {"Pillar", "Axiom" },
  652. {"Intel", "Multi-Flex"},
  653. {"NETAPP", "LUN"},
  654. {"AIX", "NVDISK"},
  655. {NULL, NULL}
  656. };
  657. static int alua_bus_attach(struct scsi_device *sdev);
  658. static void alua_bus_detach(struct scsi_device *sdev);
  659. static struct scsi_device_handler alua_dh = {
  660. .name = ALUA_DH_NAME,
  661. .module = THIS_MODULE,
  662. .devlist = alua_dev_list,
  663. .attach = alua_bus_attach,
  664. .detach = alua_bus_detach,
  665. .prep_fn = alua_prep_fn,
  666. .check_sense = alua_check_sense,
  667. .activate = alua_activate,
  668. };
  669. /*
  670. * alua_bus_attach - Attach device handler
  671. * @sdev: device to be attached to
  672. */
  673. static int alua_bus_attach(struct scsi_device *sdev)
  674. {
  675. struct scsi_dh_data *scsi_dh_data;
  676. struct alua_dh_data *h;
  677. unsigned long flags;
  678. int err = SCSI_DH_OK;
  679. scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
  680. + sizeof(*h) , GFP_KERNEL);
  681. if (!scsi_dh_data) {
  682. sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
  683. ALUA_DH_NAME);
  684. return -ENOMEM;
  685. }
  686. scsi_dh_data->scsi_dh = &alua_dh;
  687. h = (struct alua_dh_data *) scsi_dh_data->buf;
  688. h->tpgs = TPGS_MODE_UNINITIALIZED;
  689. h->state = TPGS_STATE_OPTIMIZED;
  690. h->group_id = -1;
  691. h->rel_port = -1;
  692. h->buff = h->inq;
  693. h->bufflen = ALUA_INQUIRY_SIZE;
  694. h->sdev = sdev;
  695. err = alua_initialize(sdev, h);
  696. if (err != SCSI_DH_OK)
  697. goto failed;
  698. if (!try_module_get(THIS_MODULE))
  699. goto failed;
  700. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  701. sdev->scsi_dh_data = scsi_dh_data;
  702. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  703. return 0;
  704. failed:
  705. kfree(scsi_dh_data);
  706. sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
  707. return -EINVAL;
  708. }
  709. /*
  710. * alua_bus_detach - Detach device handler
  711. * @sdev: device to be detached from
  712. */
  713. static void alua_bus_detach(struct scsi_device *sdev)
  714. {
  715. struct scsi_dh_data *scsi_dh_data;
  716. struct alua_dh_data *h;
  717. unsigned long flags;
  718. spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
  719. scsi_dh_data = sdev->scsi_dh_data;
  720. sdev->scsi_dh_data = NULL;
  721. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  722. h = (struct alua_dh_data *) scsi_dh_data->buf;
  723. if (h->buff && h->inq != h->buff)
  724. kfree(h->buff);
  725. kfree(scsi_dh_data);
  726. module_put(THIS_MODULE);
  727. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
  728. }
  729. static int __init alua_init(void)
  730. {
  731. int r;
  732. r = scsi_register_device_handler(&alua_dh);
  733. if (r != 0)
  734. printk(KERN_ERR "%s: Failed to register scsi device handler",
  735. ALUA_DH_NAME);
  736. return r;
  737. }
  738. static void __exit alua_exit(void)
  739. {
  740. scsi_unregister_device_handler(&alua_dh);
  741. }
  742. module_init(alua_init);
  743. module_exit(alua_exit);
  744. MODULE_DESCRIPTION("DM Multipath ALUA support");
  745. MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
  746. MODULE_LICENSE("GPL");
  747. MODULE_VERSION(ALUA_DH_VER);