scsi_dh_rdac.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * LSI/Engenio/NetApp E-Series RDAC SCSI Device Handler
  3. *
  4. * Copyright (C) 2005 Mike Christie. All rights reserved.
  5. * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
  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 <scsi/scsi.h>
  23. #include <scsi/scsi_eh.h>
  24. #include <scsi/scsi_dh.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #define RDAC_NAME "rdac"
  29. #define RDAC_RETRY_COUNT 5
  30. /*
  31. * LSI mode page stuff
  32. *
  33. * These struct definitions and the forming of the
  34. * mode page were taken from the LSI RDAC 2.4 GPL'd
  35. * driver, and then converted to Linux conventions.
  36. */
  37. #define RDAC_QUIESCENCE_TIME 20
  38. /*
  39. * Page Codes
  40. */
  41. #define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
  42. /*
  43. * Controller modes definitions
  44. */
  45. #define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
  46. /*
  47. * RDAC Options field
  48. */
  49. #define RDAC_FORCED_QUIESENCE 0x02
  50. #define RDAC_TIMEOUT (60 * HZ)
  51. #define RDAC_RETRIES 3
  52. struct rdac_mode_6_hdr {
  53. u8 data_len;
  54. u8 medium_type;
  55. u8 device_params;
  56. u8 block_desc_len;
  57. };
  58. struct rdac_mode_10_hdr {
  59. u16 data_len;
  60. u8 medium_type;
  61. u8 device_params;
  62. u16 reserved;
  63. u16 block_desc_len;
  64. };
  65. struct rdac_mode_common {
  66. u8 controller_serial[16];
  67. u8 alt_controller_serial[16];
  68. u8 rdac_mode[2];
  69. u8 alt_rdac_mode[2];
  70. u8 quiescence_timeout;
  71. u8 rdac_options;
  72. };
  73. struct rdac_pg_legacy {
  74. struct rdac_mode_6_hdr hdr;
  75. u8 page_code;
  76. u8 page_len;
  77. struct rdac_mode_common common;
  78. #define MODE6_MAX_LUN 32
  79. u8 lun_table[MODE6_MAX_LUN];
  80. u8 reserved2[32];
  81. u8 reserved3;
  82. u8 reserved4;
  83. };
  84. struct rdac_pg_expanded {
  85. struct rdac_mode_10_hdr hdr;
  86. u8 page_code;
  87. u8 subpage_code;
  88. u8 page_len[2];
  89. struct rdac_mode_common common;
  90. u8 lun_table[256];
  91. u8 reserved3;
  92. u8 reserved4;
  93. };
  94. struct c9_inquiry {
  95. u8 peripheral_info;
  96. u8 page_code; /* 0xC9 */
  97. u8 reserved1;
  98. u8 page_len;
  99. u8 page_id[4]; /* "vace" */
  100. u8 avte_cvp;
  101. u8 path_prio;
  102. u8 reserved2[38];
  103. };
  104. #define SUBSYS_ID_LEN 16
  105. #define SLOT_ID_LEN 2
  106. #define ARRAY_LABEL_LEN 31
  107. struct c4_inquiry {
  108. u8 peripheral_info;
  109. u8 page_code; /* 0xC4 */
  110. u8 reserved1;
  111. u8 page_len;
  112. u8 page_id[4]; /* "subs" */
  113. u8 subsys_id[SUBSYS_ID_LEN];
  114. u8 revision[4];
  115. u8 slot_id[SLOT_ID_LEN];
  116. u8 reserved[2];
  117. };
  118. #define UNIQUE_ID_LEN 16
  119. struct c8_inquiry {
  120. u8 peripheral_info;
  121. u8 page_code; /* 0xC8 */
  122. u8 reserved1;
  123. u8 page_len;
  124. u8 page_id[4]; /* "edid" */
  125. u8 reserved2[3];
  126. u8 vol_uniq_id_len;
  127. u8 vol_uniq_id[16];
  128. u8 vol_user_label_len;
  129. u8 vol_user_label[60];
  130. u8 array_uniq_id_len;
  131. u8 array_unique_id[UNIQUE_ID_LEN];
  132. u8 array_user_label_len;
  133. u8 array_user_label[60];
  134. u8 lun[8];
  135. };
  136. struct rdac_controller {
  137. u8 array_id[UNIQUE_ID_LEN];
  138. int use_ms10;
  139. struct kref kref;
  140. struct list_head node; /* list of all controllers */
  141. union {
  142. struct rdac_pg_legacy legacy;
  143. struct rdac_pg_expanded expanded;
  144. } mode_select;
  145. u8 index;
  146. u8 array_name[ARRAY_LABEL_LEN];
  147. struct Scsi_Host *host;
  148. spinlock_t ms_lock;
  149. int ms_queued;
  150. struct work_struct ms_work;
  151. struct scsi_device *ms_sdev;
  152. struct list_head ms_head;
  153. struct list_head dh_list;
  154. };
  155. struct c2_inquiry {
  156. u8 peripheral_info;
  157. u8 page_code; /* 0xC2 */
  158. u8 reserved1;
  159. u8 page_len;
  160. u8 page_id[4]; /* "swr4" */
  161. u8 sw_version[3];
  162. u8 sw_date[3];
  163. u8 features_enabled;
  164. u8 max_lun_supported;
  165. u8 partitions[239]; /* Total allocation length should be 0xFF */
  166. };
  167. struct rdac_dh_data {
  168. struct list_head node;
  169. struct rdac_controller *ctlr;
  170. struct scsi_device *sdev;
  171. #define UNINITIALIZED_LUN (1 << 8)
  172. unsigned lun;
  173. #define RDAC_MODE 0
  174. #define RDAC_MODE_AVT 1
  175. #define RDAC_MODE_IOSHIP 2
  176. unsigned char mode;
  177. #define RDAC_STATE_ACTIVE 0
  178. #define RDAC_STATE_PASSIVE 1
  179. unsigned char state;
  180. #define RDAC_LUN_UNOWNED 0
  181. #define RDAC_LUN_OWNED 1
  182. char lun_state;
  183. #define RDAC_PREFERRED 0
  184. #define RDAC_NON_PREFERRED 1
  185. char preferred;
  186. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  187. union {
  188. struct c2_inquiry c2;
  189. struct c4_inquiry c4;
  190. struct c8_inquiry c8;
  191. struct c9_inquiry c9;
  192. } inq;
  193. };
  194. static const char *mode[] = {
  195. "RDAC",
  196. "AVT",
  197. "IOSHIP",
  198. };
  199. static const char *lun_state[] =
  200. {
  201. "unowned",
  202. "owned",
  203. };
  204. struct rdac_queue_data {
  205. struct list_head entry;
  206. struct rdac_dh_data *h;
  207. activate_complete callback_fn;
  208. void *callback_data;
  209. };
  210. static LIST_HEAD(ctlr_list);
  211. static DEFINE_SPINLOCK(list_lock);
  212. static struct workqueue_struct *kmpath_rdacd;
  213. static void send_mode_select(struct work_struct *work);
  214. /*
  215. * module parameter to enable rdac debug logging.
  216. * 2 bits for each type of logging, only two types defined for now
  217. * Can be enhanced if required at later point
  218. */
  219. static int rdac_logging = 1;
  220. module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
  221. MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
  222. "Default is 1 - failover logging enabled, "
  223. "set it to 0xF to enable all the logs");
  224. #define RDAC_LOG_FAILOVER 0
  225. #define RDAC_LOG_SENSE 2
  226. #define RDAC_LOG_BITS 2
  227. #define RDAC_LOG_LEVEL(SHIFT) \
  228. ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
  229. #define RDAC_LOG(SHIFT, sdev, f, arg...) \
  230. do { \
  231. if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
  232. sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
  233. } while (0);
  234. static struct request *get_rdac_req(struct scsi_device *sdev,
  235. void *buffer, unsigned buflen, int rw)
  236. {
  237. struct request *rq;
  238. struct request_queue *q = sdev->request_queue;
  239. rq = blk_get_request(q, rw, GFP_NOIO);
  240. if (IS_ERR(rq)) {
  241. sdev_printk(KERN_INFO, sdev,
  242. "get_rdac_req: blk_get_request failed.\n");
  243. return NULL;
  244. }
  245. blk_rq_set_block_pc(rq);
  246. if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
  247. blk_put_request(rq);
  248. sdev_printk(KERN_INFO, sdev,
  249. "get_rdac_req: blk_rq_map_kern failed.\n");
  250. return NULL;
  251. }
  252. rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  253. REQ_FAILFAST_DRIVER;
  254. rq->retries = RDAC_RETRIES;
  255. rq->timeout = RDAC_TIMEOUT;
  256. return rq;
  257. }
  258. static struct request *rdac_failover_get(struct scsi_device *sdev,
  259. struct rdac_dh_data *h, struct list_head *list)
  260. {
  261. struct request *rq;
  262. struct rdac_mode_common *common;
  263. unsigned data_size;
  264. struct rdac_queue_data *qdata;
  265. u8 *lun_table;
  266. if (h->ctlr->use_ms10) {
  267. struct rdac_pg_expanded *rdac_pg;
  268. data_size = sizeof(struct rdac_pg_expanded);
  269. rdac_pg = &h->ctlr->mode_select.expanded;
  270. memset(rdac_pg, 0, data_size);
  271. common = &rdac_pg->common;
  272. rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
  273. rdac_pg->subpage_code = 0x1;
  274. rdac_pg->page_len[0] = 0x01;
  275. rdac_pg->page_len[1] = 0x28;
  276. lun_table = rdac_pg->lun_table;
  277. } else {
  278. struct rdac_pg_legacy *rdac_pg;
  279. data_size = sizeof(struct rdac_pg_legacy);
  280. rdac_pg = &h->ctlr->mode_select.legacy;
  281. memset(rdac_pg, 0, data_size);
  282. common = &rdac_pg->common;
  283. rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
  284. rdac_pg->page_len = 0x68;
  285. lun_table = rdac_pg->lun_table;
  286. }
  287. common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
  288. common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
  289. common->rdac_options = RDAC_FORCED_QUIESENCE;
  290. list_for_each_entry(qdata, list, entry) {
  291. lun_table[qdata->h->lun] = 0x81;
  292. }
  293. /* get request for block layer packet command */
  294. rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
  295. if (!rq)
  296. return NULL;
  297. /* Prepare the command. */
  298. if (h->ctlr->use_ms10) {
  299. rq->cmd[0] = MODE_SELECT_10;
  300. rq->cmd[7] = data_size >> 8;
  301. rq->cmd[8] = data_size & 0xff;
  302. } else {
  303. rq->cmd[0] = MODE_SELECT;
  304. rq->cmd[4] = data_size;
  305. }
  306. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  307. rq->sense = h->sense;
  308. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  309. rq->sense_len = 0;
  310. return rq;
  311. }
  312. static void release_controller(struct kref *kref)
  313. {
  314. struct rdac_controller *ctlr;
  315. ctlr = container_of(kref, struct rdac_controller, kref);
  316. list_del(&ctlr->node);
  317. kfree(ctlr);
  318. }
  319. static struct rdac_controller *get_controller(int index, char *array_name,
  320. u8 *array_id, struct scsi_device *sdev)
  321. {
  322. struct rdac_controller *ctlr, *tmp;
  323. list_for_each_entry(tmp, &ctlr_list, node) {
  324. if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
  325. (tmp->index == index) &&
  326. (tmp->host == sdev->host)) {
  327. kref_get(&tmp->kref);
  328. return tmp;
  329. }
  330. }
  331. ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
  332. if (!ctlr)
  333. return NULL;
  334. /* initialize fields of controller */
  335. memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
  336. ctlr->index = index;
  337. ctlr->host = sdev->host;
  338. memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
  339. kref_init(&ctlr->kref);
  340. ctlr->use_ms10 = -1;
  341. ctlr->ms_queued = 0;
  342. ctlr->ms_sdev = NULL;
  343. spin_lock_init(&ctlr->ms_lock);
  344. INIT_WORK(&ctlr->ms_work, send_mode_select);
  345. INIT_LIST_HEAD(&ctlr->ms_head);
  346. list_add(&ctlr->node, &ctlr_list);
  347. INIT_LIST_HEAD(&ctlr->dh_list);
  348. return ctlr;
  349. }
  350. static int submit_inquiry(struct scsi_device *sdev, int page_code,
  351. unsigned int len, struct rdac_dh_data *h)
  352. {
  353. struct request *rq;
  354. struct request_queue *q = sdev->request_queue;
  355. int err = SCSI_DH_RES_TEMP_UNAVAIL;
  356. rq = get_rdac_req(sdev, &h->inq, len, READ);
  357. if (!rq)
  358. goto done;
  359. /* Prepare the command. */
  360. rq->cmd[0] = INQUIRY;
  361. rq->cmd[1] = 1;
  362. rq->cmd[2] = page_code;
  363. rq->cmd[4] = len;
  364. rq->cmd_len = COMMAND_SIZE(INQUIRY);
  365. rq->sense = h->sense;
  366. memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
  367. rq->sense_len = 0;
  368. err = blk_execute_rq(q, NULL, rq, 1);
  369. if (err == -EIO)
  370. err = SCSI_DH_IO;
  371. blk_put_request(rq);
  372. done:
  373. return err;
  374. }
  375. static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
  376. char *array_name, u8 *array_id)
  377. {
  378. int err, i;
  379. struct c8_inquiry *inqp;
  380. err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
  381. if (err == SCSI_DH_OK) {
  382. inqp = &h->inq.c8;
  383. if (inqp->page_code != 0xc8)
  384. return SCSI_DH_NOSYS;
  385. if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
  386. inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
  387. return SCSI_DH_NOSYS;
  388. h->lun = inqp->lun[7]; /* Uses only the last byte */
  389. for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
  390. *(array_name+i) = inqp->array_user_label[(2*i)+1];
  391. *(array_name+ARRAY_LABEL_LEN-1) = '\0';
  392. memset(array_id, 0, UNIQUE_ID_LEN);
  393. memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
  394. }
  395. return err;
  396. }
  397. static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
  398. {
  399. int err, access_state;
  400. struct rdac_dh_data *tmp;
  401. struct c9_inquiry *inqp;
  402. h->state = RDAC_STATE_ACTIVE;
  403. err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
  404. if (err == SCSI_DH_OK) {
  405. inqp = &h->inq.c9;
  406. /* detect the operating mode */
  407. if ((inqp->avte_cvp >> 5) & 0x1)
  408. h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
  409. else if (inqp->avte_cvp >> 7)
  410. h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
  411. else
  412. h->mode = RDAC_MODE; /* LUN in RDAC mode */
  413. /* Update ownership */
  414. if (inqp->avte_cvp & 0x1) {
  415. h->lun_state = RDAC_LUN_OWNED;
  416. access_state = SCSI_ACCESS_STATE_OPTIMAL;
  417. } else {
  418. h->lun_state = RDAC_LUN_UNOWNED;
  419. if (h->mode == RDAC_MODE) {
  420. h->state = RDAC_STATE_PASSIVE;
  421. access_state = SCSI_ACCESS_STATE_STANDBY;
  422. } else
  423. access_state = SCSI_ACCESS_STATE_ACTIVE;
  424. }
  425. /* Update path prio*/
  426. if (inqp->path_prio & 0x1) {
  427. h->preferred = RDAC_PREFERRED;
  428. access_state |= SCSI_ACCESS_STATE_PREFERRED;
  429. } else
  430. h->preferred = RDAC_NON_PREFERRED;
  431. rcu_read_lock();
  432. list_for_each_entry_rcu(tmp, &h->ctlr->dh_list, node) {
  433. /* h->sdev should always be valid */
  434. BUG_ON(!tmp->sdev);
  435. tmp->sdev->access_state = access_state;
  436. }
  437. rcu_read_unlock();
  438. }
  439. return err;
  440. }
  441. static int initialize_controller(struct scsi_device *sdev,
  442. struct rdac_dh_data *h, char *array_name, u8 *array_id)
  443. {
  444. int err, index;
  445. struct c4_inquiry *inqp;
  446. err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
  447. if (err == SCSI_DH_OK) {
  448. inqp = &h->inq.c4;
  449. /* get the controller index */
  450. if (inqp->slot_id[1] == 0x31)
  451. index = 0;
  452. else
  453. index = 1;
  454. spin_lock(&list_lock);
  455. h->ctlr = get_controller(index, array_name, array_id, sdev);
  456. if (!h->ctlr)
  457. err = SCSI_DH_RES_TEMP_UNAVAIL;
  458. else {
  459. list_add_rcu(&h->node, &h->ctlr->dh_list);
  460. h->sdev = sdev;
  461. }
  462. spin_unlock(&list_lock);
  463. }
  464. return err;
  465. }
  466. static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
  467. {
  468. int err;
  469. struct c2_inquiry *inqp;
  470. err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
  471. if (err == SCSI_DH_OK) {
  472. inqp = &h->inq.c2;
  473. /*
  474. * If more than MODE6_MAX_LUN luns are supported, use
  475. * mode select 10
  476. */
  477. if (inqp->max_lun_supported >= MODE6_MAX_LUN)
  478. h->ctlr->use_ms10 = 1;
  479. else
  480. h->ctlr->use_ms10 = 0;
  481. }
  482. return err;
  483. }
  484. static int mode_select_handle_sense(struct scsi_device *sdev,
  485. unsigned char *sensebuf)
  486. {
  487. struct scsi_sense_hdr sense_hdr;
  488. int err = SCSI_DH_IO, ret;
  489. struct rdac_dh_data *h = sdev->handler_data;
  490. ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
  491. if (!ret)
  492. goto done;
  493. switch (sense_hdr.sense_key) {
  494. case NO_SENSE:
  495. case ABORTED_COMMAND:
  496. case UNIT_ATTENTION:
  497. err = SCSI_DH_RETRY;
  498. break;
  499. case NOT_READY:
  500. if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
  501. /* LUN Not Ready and is in the Process of Becoming
  502. * Ready
  503. */
  504. err = SCSI_DH_RETRY;
  505. break;
  506. case ILLEGAL_REQUEST:
  507. if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
  508. /*
  509. * Command Lock contention
  510. */
  511. err = SCSI_DH_IMM_RETRY;
  512. break;
  513. default:
  514. break;
  515. }
  516. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  517. "MODE_SELECT returned with sense %02x/%02x/%02x",
  518. (char *) h->ctlr->array_name, h->ctlr->index,
  519. sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
  520. done:
  521. return err;
  522. }
  523. static void send_mode_select(struct work_struct *work)
  524. {
  525. struct rdac_controller *ctlr =
  526. container_of(work, struct rdac_controller, ms_work);
  527. struct request *rq;
  528. struct scsi_device *sdev = ctlr->ms_sdev;
  529. struct rdac_dh_data *h = sdev->handler_data;
  530. struct request_queue *q = sdev->request_queue;
  531. int err, retry_cnt = RDAC_RETRY_COUNT;
  532. struct rdac_queue_data *tmp, *qdata;
  533. LIST_HEAD(list);
  534. spin_lock(&ctlr->ms_lock);
  535. list_splice_init(&ctlr->ms_head, &list);
  536. ctlr->ms_queued = 0;
  537. ctlr->ms_sdev = NULL;
  538. spin_unlock(&ctlr->ms_lock);
  539. retry:
  540. err = SCSI_DH_RES_TEMP_UNAVAIL;
  541. rq = rdac_failover_get(sdev, h, &list);
  542. if (!rq)
  543. goto done;
  544. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  545. "%s MODE_SELECT command",
  546. (char *) h->ctlr->array_name, h->ctlr->index,
  547. (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
  548. err = blk_execute_rq(q, NULL, rq, 1);
  549. blk_put_request(rq);
  550. if (err != SCSI_DH_OK) {
  551. err = mode_select_handle_sense(sdev, h->sense);
  552. if (err == SCSI_DH_RETRY && retry_cnt--)
  553. goto retry;
  554. if (err == SCSI_DH_IMM_RETRY)
  555. goto retry;
  556. }
  557. if (err == SCSI_DH_OK) {
  558. h->state = RDAC_STATE_ACTIVE;
  559. RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
  560. "MODE_SELECT completed",
  561. (char *) h->ctlr->array_name, h->ctlr->index);
  562. }
  563. done:
  564. list_for_each_entry_safe(qdata, tmp, &list, entry) {
  565. list_del(&qdata->entry);
  566. if (err == SCSI_DH_OK)
  567. qdata->h->state = RDAC_STATE_ACTIVE;
  568. if (qdata->callback_fn)
  569. qdata->callback_fn(qdata->callback_data, err);
  570. kfree(qdata);
  571. }
  572. return;
  573. }
  574. static int queue_mode_select(struct scsi_device *sdev,
  575. activate_complete fn, void *data)
  576. {
  577. struct rdac_queue_data *qdata;
  578. struct rdac_controller *ctlr;
  579. qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
  580. if (!qdata)
  581. return SCSI_DH_RETRY;
  582. qdata->h = sdev->handler_data;
  583. qdata->callback_fn = fn;
  584. qdata->callback_data = data;
  585. ctlr = qdata->h->ctlr;
  586. spin_lock(&ctlr->ms_lock);
  587. list_add_tail(&qdata->entry, &ctlr->ms_head);
  588. if (!ctlr->ms_queued) {
  589. ctlr->ms_queued = 1;
  590. ctlr->ms_sdev = sdev;
  591. queue_work(kmpath_rdacd, &ctlr->ms_work);
  592. }
  593. spin_unlock(&ctlr->ms_lock);
  594. return SCSI_DH_OK;
  595. }
  596. static int rdac_activate(struct scsi_device *sdev,
  597. activate_complete fn, void *data)
  598. {
  599. struct rdac_dh_data *h = sdev->handler_data;
  600. int err = SCSI_DH_OK;
  601. int act = 0;
  602. err = check_ownership(sdev, h);
  603. if (err != SCSI_DH_OK)
  604. goto done;
  605. switch (h->mode) {
  606. case RDAC_MODE:
  607. if (h->lun_state == RDAC_LUN_UNOWNED)
  608. act = 1;
  609. break;
  610. case RDAC_MODE_IOSHIP:
  611. if ((h->lun_state == RDAC_LUN_UNOWNED) &&
  612. (h->preferred == RDAC_PREFERRED))
  613. act = 1;
  614. break;
  615. default:
  616. break;
  617. }
  618. if (act) {
  619. err = queue_mode_select(sdev, fn, data);
  620. if (err == SCSI_DH_OK)
  621. return 0;
  622. }
  623. done:
  624. if (fn)
  625. fn(data, err);
  626. return 0;
  627. }
  628. static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
  629. {
  630. struct rdac_dh_data *h = sdev->handler_data;
  631. int ret = BLKPREP_OK;
  632. if (h->state != RDAC_STATE_ACTIVE) {
  633. ret = BLKPREP_KILL;
  634. req->cmd_flags |= REQ_QUIET;
  635. }
  636. return ret;
  637. }
  638. static int rdac_check_sense(struct scsi_device *sdev,
  639. struct scsi_sense_hdr *sense_hdr)
  640. {
  641. struct rdac_dh_data *h = sdev->handler_data;
  642. RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
  643. "I/O returned with sense %02x/%02x/%02x",
  644. (char *) h->ctlr->array_name, h->ctlr->index,
  645. sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
  646. switch (sense_hdr->sense_key) {
  647. case NOT_READY:
  648. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
  649. /* LUN Not Ready - Logical Unit Not Ready and is in
  650. * the process of becoming ready
  651. * Just retry.
  652. */
  653. return ADD_TO_MLQUEUE;
  654. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
  655. /* LUN Not Ready - Storage firmware incompatible
  656. * Manual code synchonisation required.
  657. *
  658. * Nothing we can do here. Try to bypass the path.
  659. */
  660. return SUCCESS;
  661. if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
  662. /* LUN Not Ready - Quiescense in progress
  663. *
  664. * Just retry and wait.
  665. */
  666. return ADD_TO_MLQUEUE;
  667. if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
  668. /* LUN Not Ready - Quiescense in progress
  669. * or has been achieved
  670. * Just retry.
  671. */
  672. return ADD_TO_MLQUEUE;
  673. break;
  674. case ILLEGAL_REQUEST:
  675. if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
  676. /* Invalid Request - Current Logical Unit Ownership.
  677. * Controller is not the current owner of the LUN,
  678. * Fail the path, so that the other path be used.
  679. */
  680. h->state = RDAC_STATE_PASSIVE;
  681. return SUCCESS;
  682. }
  683. break;
  684. case UNIT_ATTENTION:
  685. if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
  686. /*
  687. * Power On, Reset, or Bus Device Reset, just retry.
  688. */
  689. return ADD_TO_MLQUEUE;
  690. if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
  691. /*
  692. * Quiescence in progress , just retry.
  693. */
  694. return ADD_TO_MLQUEUE;
  695. break;
  696. }
  697. /* success just means we do not care what scsi-ml does */
  698. return SCSI_RETURN_NOT_HANDLED;
  699. }
  700. static int rdac_bus_attach(struct scsi_device *sdev)
  701. {
  702. struct rdac_dh_data *h;
  703. int err;
  704. char array_name[ARRAY_LABEL_LEN];
  705. char array_id[UNIQUE_ID_LEN];
  706. h = kzalloc(sizeof(*h) , GFP_KERNEL);
  707. if (!h)
  708. return -ENOMEM;
  709. h->lun = UNINITIALIZED_LUN;
  710. h->state = RDAC_STATE_ACTIVE;
  711. err = get_lun_info(sdev, h, array_name, array_id);
  712. if (err != SCSI_DH_OK)
  713. goto failed;
  714. err = initialize_controller(sdev, h, array_name, array_id);
  715. if (err != SCSI_DH_OK)
  716. goto failed;
  717. err = check_ownership(sdev, h);
  718. if (err != SCSI_DH_OK)
  719. goto clean_ctlr;
  720. err = set_mode_select(sdev, h);
  721. if (err != SCSI_DH_OK)
  722. goto clean_ctlr;
  723. sdev_printk(KERN_NOTICE, sdev,
  724. "%s: LUN %d (%s) (%s)\n",
  725. RDAC_NAME, h->lun, mode[(int)h->mode],
  726. lun_state[(int)h->lun_state]);
  727. sdev->handler_data = h;
  728. return 0;
  729. clean_ctlr:
  730. spin_lock(&list_lock);
  731. kref_put(&h->ctlr->kref, release_controller);
  732. spin_unlock(&list_lock);
  733. failed:
  734. kfree(h);
  735. return -EINVAL;
  736. }
  737. static void rdac_bus_detach( struct scsi_device *sdev )
  738. {
  739. struct rdac_dh_data *h = sdev->handler_data;
  740. if (h->ctlr && h->ctlr->ms_queued)
  741. flush_workqueue(kmpath_rdacd);
  742. spin_lock(&list_lock);
  743. if (h->ctlr) {
  744. list_del_rcu(&h->node);
  745. h->sdev = NULL;
  746. kref_put(&h->ctlr->kref, release_controller);
  747. }
  748. spin_unlock(&list_lock);
  749. sdev->handler_data = NULL;
  750. kfree(h);
  751. }
  752. static struct scsi_device_handler rdac_dh = {
  753. .name = RDAC_NAME,
  754. .module = THIS_MODULE,
  755. .prep_fn = rdac_prep_fn,
  756. .check_sense = rdac_check_sense,
  757. .attach = rdac_bus_attach,
  758. .detach = rdac_bus_detach,
  759. .activate = rdac_activate,
  760. };
  761. static int __init rdac_init(void)
  762. {
  763. int r;
  764. r = scsi_register_device_handler(&rdac_dh);
  765. if (r != 0) {
  766. printk(KERN_ERR "Failed to register scsi device handler.");
  767. goto done;
  768. }
  769. /*
  770. * Create workqueue to handle mode selects for rdac
  771. */
  772. kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
  773. if (!kmpath_rdacd) {
  774. scsi_unregister_device_handler(&rdac_dh);
  775. printk(KERN_ERR "kmpath_rdacd creation failed.\n");
  776. r = -EINVAL;
  777. }
  778. done:
  779. return r;
  780. }
  781. static void __exit rdac_exit(void)
  782. {
  783. destroy_workqueue(kmpath_rdacd);
  784. scsi_unregister_device_handler(&rdac_dh);
  785. }
  786. module_init(rdac_init);
  787. module_exit(rdac_exit);
  788. MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
  789. MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
  790. MODULE_VERSION("01.00.0000.0000");
  791. MODULE_LICENSE("GPL");