scsi_dh_rdac.c 20 KB

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