ses.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /*
  2. * SCSI Enclosure Services
  3. *
  4. * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
  5. *
  6. **-----------------------------------------------------------------------------
  7. **
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** version 2 as published by the Free Software Foundation.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/enclosure.h>
  27. #include <asm/unaligned.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_dbg.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_driver.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. struct ses_device {
  36. unsigned char *page1;
  37. unsigned char *page1_types;
  38. unsigned char *page2;
  39. unsigned char *page10;
  40. short page1_len;
  41. short page1_num_types;
  42. short page2_len;
  43. short page10_len;
  44. };
  45. struct ses_component {
  46. u64 addr;
  47. };
  48. static int ses_probe(struct device *dev)
  49. {
  50. struct scsi_device *sdev = to_scsi_device(dev);
  51. int err = -ENODEV;
  52. if (sdev->type != TYPE_ENCLOSURE)
  53. goto out;
  54. err = 0;
  55. sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
  56. out:
  57. return err;
  58. }
  59. #define SES_TIMEOUT (30 * HZ)
  60. #define SES_RETRIES 3
  61. static void init_device_slot_control(unsigned char *dest_desc,
  62. struct enclosure_component *ecomp,
  63. unsigned char *status)
  64. {
  65. memcpy(dest_desc, status, 4);
  66. dest_desc[0] = 0;
  67. /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
  68. if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
  69. dest_desc[1] = 0;
  70. dest_desc[2] &= 0xde;
  71. dest_desc[3] &= 0x3c;
  72. }
  73. static int ses_recv_diag(struct scsi_device *sdev, int page_code,
  74. void *buf, int bufflen)
  75. {
  76. int ret;
  77. unsigned char cmd[] = {
  78. RECEIVE_DIAGNOSTIC,
  79. 1, /* Set PCV bit */
  80. page_code,
  81. bufflen >> 8,
  82. bufflen & 0xff,
  83. 0
  84. };
  85. unsigned char recv_page_code;
  86. ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
  87. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  88. if (unlikely(!ret))
  89. return ret;
  90. recv_page_code = ((unsigned char *)buf)[0];
  91. if (likely(recv_page_code == page_code))
  92. return ret;
  93. /* successful diagnostic but wrong page code. This happens to some
  94. * USB devices, just print a message and pretend there was an error */
  95. sdev_printk(KERN_ERR, sdev,
  96. "Wrong diagnostic page; asked for %d got %u\n",
  97. page_code, recv_page_code);
  98. return -EINVAL;
  99. }
  100. static int ses_send_diag(struct scsi_device *sdev, int page_code,
  101. void *buf, int bufflen)
  102. {
  103. u32 result;
  104. unsigned char cmd[] = {
  105. SEND_DIAGNOSTIC,
  106. 0x10, /* Set PF bit */
  107. 0,
  108. bufflen >> 8,
  109. bufflen & 0xff,
  110. 0
  111. };
  112. result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
  113. NULL, SES_TIMEOUT, SES_RETRIES, NULL);
  114. if (result)
  115. sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
  116. result);
  117. return result;
  118. }
  119. static int ses_set_page2_descriptor(struct enclosure_device *edev,
  120. struct enclosure_component *ecomp,
  121. unsigned char *desc)
  122. {
  123. int i, j, count = 0, descriptor = ecomp->number;
  124. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  125. struct ses_device *ses_dev = edev->scratch;
  126. unsigned char *type_ptr = ses_dev->page1_types;
  127. unsigned char *desc_ptr = ses_dev->page2 + 8;
  128. /* Clear everything */
  129. memset(desc_ptr, 0, ses_dev->page2_len - 8);
  130. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  131. for (j = 0; j < type_ptr[1]; j++) {
  132. desc_ptr += 4;
  133. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  134. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  135. continue;
  136. if (count++ == descriptor) {
  137. memcpy(desc_ptr, desc, 4);
  138. /* set select */
  139. desc_ptr[0] |= 0x80;
  140. /* clear reserved, just in case */
  141. desc_ptr[0] &= 0xf0;
  142. }
  143. }
  144. }
  145. return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  146. }
  147. static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
  148. struct enclosure_component *ecomp)
  149. {
  150. int i, j, count = 0, descriptor = ecomp->number;
  151. struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
  152. struct ses_device *ses_dev = edev->scratch;
  153. unsigned char *type_ptr = ses_dev->page1_types;
  154. unsigned char *desc_ptr = ses_dev->page2 + 8;
  155. ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
  156. for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
  157. for (j = 0; j < type_ptr[1]; j++) {
  158. desc_ptr += 4;
  159. if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
  160. type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  161. continue;
  162. if (count++ == descriptor)
  163. return desc_ptr;
  164. }
  165. }
  166. return NULL;
  167. }
  168. /* For device slot and array device slot elements, byte 3 bit 6
  169. * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
  170. * code stands these bits are shifted 4 positions right so in
  171. * sysfs they will appear as bits 2 and 1 respectively. Strange. */
  172. static void ses_get_fault(struct enclosure_device *edev,
  173. struct enclosure_component *ecomp)
  174. {
  175. unsigned char *desc;
  176. desc = ses_get_page2_descriptor(edev, ecomp);
  177. if (desc)
  178. ecomp->fault = (desc[3] & 0x60) >> 4;
  179. }
  180. static int ses_set_fault(struct enclosure_device *edev,
  181. struct enclosure_component *ecomp,
  182. enum enclosure_component_setting val)
  183. {
  184. unsigned char desc[4];
  185. unsigned char *desc_ptr;
  186. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  187. if (!desc_ptr)
  188. return -EIO;
  189. init_device_slot_control(desc, ecomp, desc_ptr);
  190. switch (val) {
  191. case ENCLOSURE_SETTING_DISABLED:
  192. desc[3] &= 0xdf;
  193. break;
  194. case ENCLOSURE_SETTING_ENABLED:
  195. desc[3] |= 0x20;
  196. break;
  197. default:
  198. /* SES doesn't do the SGPIO blink settings */
  199. return -EINVAL;
  200. }
  201. return ses_set_page2_descriptor(edev, ecomp, desc);
  202. }
  203. static void ses_get_status(struct enclosure_device *edev,
  204. struct enclosure_component *ecomp)
  205. {
  206. unsigned char *desc;
  207. desc = ses_get_page2_descriptor(edev, ecomp);
  208. if (desc)
  209. ecomp->status = (desc[0] & 0x0f);
  210. }
  211. static void ses_get_locate(struct enclosure_device *edev,
  212. struct enclosure_component *ecomp)
  213. {
  214. unsigned char *desc;
  215. desc = ses_get_page2_descriptor(edev, ecomp);
  216. if (desc)
  217. ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
  218. }
  219. static int ses_set_locate(struct enclosure_device *edev,
  220. struct enclosure_component *ecomp,
  221. enum enclosure_component_setting val)
  222. {
  223. unsigned char desc[4];
  224. unsigned char *desc_ptr;
  225. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  226. if (!desc_ptr)
  227. return -EIO;
  228. init_device_slot_control(desc, ecomp, desc_ptr);
  229. switch (val) {
  230. case ENCLOSURE_SETTING_DISABLED:
  231. desc[2] &= 0xfd;
  232. break;
  233. case ENCLOSURE_SETTING_ENABLED:
  234. desc[2] |= 0x02;
  235. break;
  236. default:
  237. /* SES doesn't do the SGPIO blink settings */
  238. return -EINVAL;
  239. }
  240. return ses_set_page2_descriptor(edev, ecomp, desc);
  241. }
  242. static int ses_set_active(struct enclosure_device *edev,
  243. struct enclosure_component *ecomp,
  244. enum enclosure_component_setting val)
  245. {
  246. unsigned char desc[4];
  247. unsigned char *desc_ptr;
  248. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  249. if (!desc_ptr)
  250. return -EIO;
  251. init_device_slot_control(desc, ecomp, desc_ptr);
  252. switch (val) {
  253. case ENCLOSURE_SETTING_DISABLED:
  254. desc[2] &= 0x7f;
  255. ecomp->active = 0;
  256. break;
  257. case ENCLOSURE_SETTING_ENABLED:
  258. desc[2] |= 0x80;
  259. ecomp->active = 1;
  260. break;
  261. default:
  262. /* SES doesn't do the SGPIO blink settings */
  263. return -EINVAL;
  264. }
  265. return ses_set_page2_descriptor(edev, ecomp, desc);
  266. }
  267. static int ses_show_id(struct enclosure_device *edev, char *buf)
  268. {
  269. struct ses_device *ses_dev = edev->scratch;
  270. unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
  271. return sprintf(buf, "%#llx\n", id);
  272. }
  273. static void ses_get_power_status(struct enclosure_device *edev,
  274. struct enclosure_component *ecomp)
  275. {
  276. unsigned char *desc;
  277. desc = ses_get_page2_descriptor(edev, ecomp);
  278. if (desc)
  279. ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
  280. }
  281. static int ses_set_power_status(struct enclosure_device *edev,
  282. struct enclosure_component *ecomp,
  283. int val)
  284. {
  285. unsigned char desc[4];
  286. unsigned char *desc_ptr;
  287. desc_ptr = ses_get_page2_descriptor(edev, ecomp);
  288. if (!desc_ptr)
  289. return -EIO;
  290. init_device_slot_control(desc, ecomp, desc_ptr);
  291. switch (val) {
  292. /* power = 1 is device_off = 0 and vice versa */
  293. case 0:
  294. desc[3] |= 0x10;
  295. break;
  296. case 1:
  297. desc[3] &= 0xef;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. ecomp->power_status = val;
  303. return ses_set_page2_descriptor(edev, ecomp, desc);
  304. }
  305. static struct enclosure_component_callbacks ses_enclosure_callbacks = {
  306. .get_fault = ses_get_fault,
  307. .set_fault = ses_set_fault,
  308. .get_status = ses_get_status,
  309. .get_locate = ses_get_locate,
  310. .set_locate = ses_set_locate,
  311. .get_power_status = ses_get_power_status,
  312. .set_power_status = ses_set_power_status,
  313. .set_active = ses_set_active,
  314. .show_id = ses_show_id,
  315. };
  316. struct ses_host_edev {
  317. struct Scsi_Host *shost;
  318. struct enclosure_device *edev;
  319. };
  320. #if 0
  321. int ses_match_host(struct enclosure_device *edev, void *data)
  322. {
  323. struct ses_host_edev *sed = data;
  324. struct scsi_device *sdev;
  325. if (!scsi_is_sdev_device(edev->edev.parent))
  326. return 0;
  327. sdev = to_scsi_device(edev->edev.parent);
  328. if (sdev->host != sed->shost)
  329. return 0;
  330. sed->edev = edev;
  331. return 1;
  332. }
  333. #endif /* 0 */
  334. static void ses_process_descriptor(struct enclosure_component *ecomp,
  335. unsigned char *desc)
  336. {
  337. int eip = desc[0] & 0x10;
  338. int invalid = desc[0] & 0x80;
  339. enum scsi_protocol proto = desc[0] & 0x0f;
  340. u64 addr = 0;
  341. int slot = -1;
  342. struct ses_component *scomp = ecomp->scratch;
  343. unsigned char *d;
  344. if (invalid)
  345. return;
  346. switch (proto) {
  347. case SCSI_PROTOCOL_FCP:
  348. if (eip) {
  349. d = desc + 4;
  350. slot = d[3];
  351. }
  352. break;
  353. case SCSI_PROTOCOL_SAS:
  354. if (eip) {
  355. d = desc + 4;
  356. slot = d[3];
  357. d = desc + 8;
  358. } else
  359. d = desc + 4;
  360. /* only take the phy0 addr */
  361. addr = (u64)d[12] << 56 |
  362. (u64)d[13] << 48 |
  363. (u64)d[14] << 40 |
  364. (u64)d[15] << 32 |
  365. (u64)d[16] << 24 |
  366. (u64)d[17] << 16 |
  367. (u64)d[18] << 8 |
  368. (u64)d[19];
  369. break;
  370. default:
  371. /* FIXME: Need to add more protocols than just SAS */
  372. break;
  373. }
  374. ecomp->slot = slot;
  375. scomp->addr = addr;
  376. }
  377. struct efd {
  378. u64 addr;
  379. struct device *dev;
  380. };
  381. static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
  382. void *data)
  383. {
  384. struct efd *efd = data;
  385. int i;
  386. struct ses_component *scomp;
  387. if (!edev->component[0].scratch)
  388. return 0;
  389. for (i = 0; i < edev->components; i++) {
  390. scomp = edev->component[i].scratch;
  391. if (scomp->addr != efd->addr)
  392. continue;
  393. if (enclosure_add_device(edev, i, efd->dev) == 0)
  394. kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. #define INIT_ALLOC_SIZE 32
  400. static void ses_enclosure_data_process(struct enclosure_device *edev,
  401. struct scsi_device *sdev,
  402. int create)
  403. {
  404. u32 result;
  405. unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
  406. int i, j, page7_len, len, components;
  407. struct ses_device *ses_dev = edev->scratch;
  408. int types = ses_dev->page1_num_types;
  409. unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  410. if (!hdr_buf)
  411. goto simple_populate;
  412. /* re-read page 10 */
  413. if (ses_dev->page10)
  414. ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
  415. /* Page 7 for the descriptors is optional */
  416. result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
  417. if (result)
  418. goto simple_populate;
  419. page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  420. /* add 1 for trailing '\0' we'll use */
  421. buf = kzalloc(len + 1, GFP_KERNEL);
  422. if (!buf)
  423. goto simple_populate;
  424. result = ses_recv_diag(sdev, 7, buf, len);
  425. if (result) {
  426. simple_populate:
  427. kfree(buf);
  428. buf = NULL;
  429. desc_ptr = NULL;
  430. len = 0;
  431. page7_len = 0;
  432. } else {
  433. desc_ptr = buf + 8;
  434. len = (desc_ptr[2] << 8) + desc_ptr[3];
  435. /* skip past overall descriptor */
  436. desc_ptr += len + 4;
  437. }
  438. if (ses_dev->page10)
  439. addl_desc_ptr = ses_dev->page10 + 8;
  440. type_ptr = ses_dev->page1_types;
  441. components = 0;
  442. for (i = 0; i < types; i++, type_ptr += 4) {
  443. for (j = 0; j < type_ptr[1]; j++) {
  444. char *name = NULL;
  445. struct enclosure_component *ecomp;
  446. if (desc_ptr) {
  447. if (desc_ptr >= buf + page7_len) {
  448. desc_ptr = NULL;
  449. } else {
  450. len = (desc_ptr[2] << 8) + desc_ptr[3];
  451. desc_ptr += 4;
  452. /* Add trailing zero - pushes into
  453. * reserved space */
  454. desc_ptr[len] = '\0';
  455. name = desc_ptr;
  456. }
  457. }
  458. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  459. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
  460. if (create)
  461. ecomp = enclosure_component_alloc(
  462. edev,
  463. components++,
  464. type_ptr[0],
  465. name);
  466. else
  467. ecomp = &edev->component[components++];
  468. if (!IS_ERR(ecomp)) {
  469. if (addl_desc_ptr)
  470. ses_process_descriptor(
  471. ecomp,
  472. addl_desc_ptr);
  473. if (create)
  474. enclosure_component_register(
  475. ecomp);
  476. }
  477. }
  478. if (desc_ptr)
  479. desc_ptr += len;
  480. if (addl_desc_ptr &&
  481. /* only find additional descriptions for specific devices */
  482. (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  483. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
  484. type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
  485. /* these elements are optional */
  486. type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
  487. type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
  488. type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
  489. addl_desc_ptr += addl_desc_ptr[1] + 2;
  490. }
  491. }
  492. kfree(buf);
  493. kfree(hdr_buf);
  494. }
  495. static void ses_match_to_enclosure(struct enclosure_device *edev,
  496. struct scsi_device *sdev,
  497. int refresh)
  498. {
  499. struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
  500. struct efd efd = {
  501. .addr = 0,
  502. };
  503. if (refresh)
  504. ses_enclosure_data_process(edev, edev_sdev, 0);
  505. if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
  506. efd.addr = sas_get_address(sdev);
  507. if (efd.addr) {
  508. efd.dev = &sdev->sdev_gendev;
  509. enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
  510. }
  511. }
  512. static int ses_intf_add(struct device *cdev,
  513. struct class_interface *intf)
  514. {
  515. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  516. struct scsi_device *tmp_sdev;
  517. unsigned char *buf = NULL, *hdr_buf, *type_ptr;
  518. struct ses_device *ses_dev;
  519. u32 result;
  520. int i, types, len, components = 0;
  521. int err = -ENOMEM;
  522. int num_enclosures;
  523. struct enclosure_device *edev;
  524. struct ses_component *scomp = NULL;
  525. if (!scsi_device_enclosure(sdev)) {
  526. /* not an enclosure, but might be in one */
  527. struct enclosure_device *prev = NULL;
  528. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  529. ses_match_to_enclosure(edev, sdev, 1);
  530. prev = edev;
  531. }
  532. return -ENODEV;
  533. }
  534. /* TYPE_ENCLOSURE prints a message in probe */
  535. if (sdev->type != TYPE_ENCLOSURE)
  536. sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
  537. ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
  538. hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
  539. if (!hdr_buf || !ses_dev)
  540. goto err_init_free;
  541. result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
  542. if (result)
  543. goto recv_failed;
  544. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  545. buf = kzalloc(len, GFP_KERNEL);
  546. if (!buf)
  547. goto err_free;
  548. result = ses_recv_diag(sdev, 1, buf, len);
  549. if (result)
  550. goto recv_failed;
  551. types = 0;
  552. /* we always have one main enclosure and the rest are referred
  553. * to as secondary subenclosures */
  554. num_enclosures = buf[1] + 1;
  555. /* begin at the enclosure descriptor */
  556. type_ptr = buf + 8;
  557. /* skip all the enclosure descriptors */
  558. for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
  559. types += type_ptr[2];
  560. type_ptr += type_ptr[3] + 4;
  561. }
  562. ses_dev->page1_types = type_ptr;
  563. ses_dev->page1_num_types = types;
  564. for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
  565. if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
  566. type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
  567. components += type_ptr[1];
  568. }
  569. ses_dev->page1 = buf;
  570. ses_dev->page1_len = len;
  571. buf = NULL;
  572. result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
  573. if (result)
  574. goto recv_failed;
  575. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  576. buf = kzalloc(len, GFP_KERNEL);
  577. if (!buf)
  578. goto err_free;
  579. /* make sure getting page 2 actually works */
  580. result = ses_recv_diag(sdev, 2, buf, len);
  581. if (result)
  582. goto recv_failed;
  583. ses_dev->page2 = buf;
  584. ses_dev->page2_len = len;
  585. buf = NULL;
  586. /* The additional information page --- allows us
  587. * to match up the devices */
  588. result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
  589. if (!result) {
  590. len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
  591. buf = kzalloc(len, GFP_KERNEL);
  592. if (!buf)
  593. goto err_free;
  594. result = ses_recv_diag(sdev, 10, buf, len);
  595. if (result)
  596. goto recv_failed;
  597. ses_dev->page10 = buf;
  598. ses_dev->page10_len = len;
  599. buf = NULL;
  600. }
  601. scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
  602. if (!scomp)
  603. goto err_free;
  604. edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
  605. components, &ses_enclosure_callbacks);
  606. if (IS_ERR(edev)) {
  607. err = PTR_ERR(edev);
  608. goto err_free;
  609. }
  610. kfree(hdr_buf);
  611. edev->scratch = ses_dev;
  612. for (i = 0; i < components; i++)
  613. edev->component[i].scratch = scomp + i;
  614. ses_enclosure_data_process(edev, sdev, 1);
  615. /* see if there are any devices matching before
  616. * we found the enclosure */
  617. shost_for_each_device(tmp_sdev, sdev->host) {
  618. if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
  619. continue;
  620. ses_match_to_enclosure(edev, tmp_sdev, 0);
  621. }
  622. return 0;
  623. recv_failed:
  624. sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
  625. result);
  626. err = -ENODEV;
  627. err_free:
  628. kfree(buf);
  629. kfree(scomp);
  630. kfree(ses_dev->page10);
  631. kfree(ses_dev->page2);
  632. kfree(ses_dev->page1);
  633. err_init_free:
  634. kfree(ses_dev);
  635. kfree(hdr_buf);
  636. sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
  637. return err;
  638. }
  639. static int ses_remove(struct device *dev)
  640. {
  641. return 0;
  642. }
  643. static void ses_intf_remove_component(struct scsi_device *sdev)
  644. {
  645. struct enclosure_device *edev, *prev = NULL;
  646. while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
  647. prev = edev;
  648. if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
  649. break;
  650. }
  651. if (edev)
  652. put_device(&edev->edev);
  653. }
  654. static void ses_intf_remove_enclosure(struct scsi_device *sdev)
  655. {
  656. struct enclosure_device *edev;
  657. struct ses_device *ses_dev;
  658. /* exact match to this enclosure */
  659. edev = enclosure_find(&sdev->sdev_gendev, NULL);
  660. if (!edev)
  661. return;
  662. enclosure_unregister(edev);
  663. ses_dev = edev->scratch;
  664. edev->scratch = NULL;
  665. kfree(ses_dev->page10);
  666. kfree(ses_dev->page1);
  667. kfree(ses_dev->page2);
  668. kfree(ses_dev);
  669. kfree(edev->component[0].scratch);
  670. put_device(&edev->edev);
  671. }
  672. static void ses_intf_remove(struct device *cdev,
  673. struct class_interface *intf)
  674. {
  675. struct scsi_device *sdev = to_scsi_device(cdev->parent);
  676. if (!scsi_device_enclosure(sdev))
  677. ses_intf_remove_component(sdev);
  678. else
  679. ses_intf_remove_enclosure(sdev);
  680. }
  681. static struct class_interface ses_interface = {
  682. .add_dev = ses_intf_add,
  683. .remove_dev = ses_intf_remove,
  684. };
  685. static struct scsi_driver ses_template = {
  686. .gendrv = {
  687. .name = "ses",
  688. .owner = THIS_MODULE,
  689. .probe = ses_probe,
  690. .remove = ses_remove,
  691. },
  692. };
  693. static int __init ses_init(void)
  694. {
  695. int err;
  696. err = scsi_register_interface(&ses_interface);
  697. if (err)
  698. return err;
  699. err = scsi_register_driver(&ses_template.gendrv);
  700. if (err)
  701. goto out_unreg;
  702. return 0;
  703. out_unreg:
  704. scsi_unregister_interface(&ses_interface);
  705. return err;
  706. }
  707. static void __exit ses_exit(void)
  708. {
  709. scsi_unregister_driver(&ses_template.gendrv);
  710. scsi_unregister_interface(&ses_interface);
  711. }
  712. module_init(ses_init);
  713. module_exit(ses_exit);
  714. MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
  715. MODULE_AUTHOR("James Bottomley");
  716. MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
  717. MODULE_LICENSE("GPL v2");