sas_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Serial Attached SCSI (SAS) Transport Layer initialization
  3. *
  4. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  5. * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6. *
  7. * This file is licensed under GPLv2.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <linux/spinlock.h>
  30. #include <scsi/sas_ata.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_sas.h>
  35. #include "sas_internal.h"
  36. #include "../scsi_sas_internal.h"
  37. static struct kmem_cache *sas_task_cache;
  38. struct sas_task *sas_alloc_task(gfp_t flags)
  39. {
  40. struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags);
  41. if (task) {
  42. spin_lock_init(&task->task_state_lock);
  43. task->task_state_flags = SAS_TASK_STATE_PENDING;
  44. }
  45. return task;
  46. }
  47. EXPORT_SYMBOL_GPL(sas_alloc_task);
  48. struct sas_task *sas_alloc_slow_task(gfp_t flags)
  49. {
  50. struct sas_task *task = sas_alloc_task(flags);
  51. struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags);
  52. if (!task || !slow) {
  53. if (task)
  54. kmem_cache_free(sas_task_cache, task);
  55. kfree(slow);
  56. return NULL;
  57. }
  58. task->slow_task = slow;
  59. init_timer(&slow->timer);
  60. init_completion(&slow->completion);
  61. return task;
  62. }
  63. EXPORT_SYMBOL_GPL(sas_alloc_slow_task);
  64. void sas_free_task(struct sas_task *task)
  65. {
  66. if (task) {
  67. kfree(task->slow_task);
  68. kmem_cache_free(sas_task_cache, task);
  69. }
  70. }
  71. EXPORT_SYMBOL_GPL(sas_free_task);
  72. /*------------ SAS addr hash -----------*/
  73. void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
  74. {
  75. const u32 poly = 0x00DB2777;
  76. u32 r = 0;
  77. int i;
  78. for (i = 0; i < 8; i++) {
  79. int b;
  80. for (b = 7; b >= 0; b--) {
  81. r <<= 1;
  82. if ((1 << b) & sas_addr[i]) {
  83. if (!(r & 0x01000000))
  84. r ^= poly;
  85. } else if (r & 0x01000000)
  86. r ^= poly;
  87. }
  88. }
  89. hashed[0] = (r >> 16) & 0xFF;
  90. hashed[1] = (r >> 8) & 0xFF ;
  91. hashed[2] = r & 0xFF;
  92. }
  93. /* ---------- HA events ---------- */
  94. void sas_hae_reset(struct work_struct *work)
  95. {
  96. struct sas_ha_event *ev = to_sas_ha_event(work);
  97. struct sas_ha_struct *ha = ev->ha;
  98. clear_bit(HAE_RESET, &ha->pending);
  99. }
  100. int sas_register_ha(struct sas_ha_struct *sas_ha)
  101. {
  102. int error = 0;
  103. mutex_init(&sas_ha->disco_mutex);
  104. spin_lock_init(&sas_ha->phy_port_lock);
  105. sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr);
  106. set_bit(SAS_HA_REGISTERED, &sas_ha->state);
  107. spin_lock_init(&sas_ha->lock);
  108. mutex_init(&sas_ha->drain_mutex);
  109. init_waitqueue_head(&sas_ha->eh_wait_q);
  110. INIT_LIST_HEAD(&sas_ha->defer_q);
  111. INIT_LIST_HEAD(&sas_ha->eh_dev_q);
  112. error = sas_register_phys(sas_ha);
  113. if (error) {
  114. printk(KERN_NOTICE "couldn't register sas phys:%d\n", error);
  115. return error;
  116. }
  117. error = sas_register_ports(sas_ha);
  118. if (error) {
  119. printk(KERN_NOTICE "couldn't register sas ports:%d\n", error);
  120. goto Undo_phys;
  121. }
  122. error = sas_init_events(sas_ha);
  123. if (error) {
  124. printk(KERN_NOTICE "couldn't start event thread:%d\n", error);
  125. goto Undo_ports;
  126. }
  127. INIT_LIST_HEAD(&sas_ha->eh_done_q);
  128. INIT_LIST_HEAD(&sas_ha->eh_ata_q);
  129. return 0;
  130. Undo_ports:
  131. sas_unregister_ports(sas_ha);
  132. Undo_phys:
  133. return error;
  134. }
  135. static void sas_disable_events(struct sas_ha_struct *sas_ha)
  136. {
  137. /* Set the state to unregistered to avoid further unchained
  138. * events to be queued, and flush any in-progress drainers
  139. */
  140. mutex_lock(&sas_ha->drain_mutex);
  141. spin_lock_irq(&sas_ha->lock);
  142. clear_bit(SAS_HA_REGISTERED, &sas_ha->state);
  143. spin_unlock_irq(&sas_ha->lock);
  144. __sas_drain_work(sas_ha);
  145. mutex_unlock(&sas_ha->drain_mutex);
  146. }
  147. int sas_unregister_ha(struct sas_ha_struct *sas_ha)
  148. {
  149. sas_disable_events(sas_ha);
  150. sas_unregister_ports(sas_ha);
  151. /* flush unregistration work */
  152. mutex_lock(&sas_ha->drain_mutex);
  153. __sas_drain_work(sas_ha);
  154. mutex_unlock(&sas_ha->drain_mutex);
  155. return 0;
  156. }
  157. static int sas_get_linkerrors(struct sas_phy *phy)
  158. {
  159. if (scsi_is_sas_phy_local(phy)) {
  160. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  161. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  162. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  163. struct sas_internal *i =
  164. to_sas_internal(sas_ha->core.shost->transportt);
  165. return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL);
  166. }
  167. return sas_smp_get_phy_events(phy);
  168. }
  169. int sas_try_ata_reset(struct asd_sas_phy *asd_phy)
  170. {
  171. struct domain_device *dev = NULL;
  172. /* try to route user requested link resets through libata */
  173. if (asd_phy->port)
  174. dev = asd_phy->port->port_dev;
  175. /* validate that dev has been probed */
  176. if (dev)
  177. dev = sas_find_dev_by_rphy(dev->rphy);
  178. if (dev && dev_is_sata(dev)) {
  179. sas_ata_schedule_reset(dev);
  180. sas_ata_wait_eh(dev);
  181. return 0;
  182. }
  183. return -ENODEV;
  184. }
  185. /**
  186. * transport_sas_phy_reset - reset a phy and permit libata to manage the link
  187. *
  188. * phy reset request via sysfs in host workqueue context so we know we
  189. * can block on eh and safely traverse the domain_device topology
  190. */
  191. static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset)
  192. {
  193. enum phy_func reset_type;
  194. if (hard_reset)
  195. reset_type = PHY_FUNC_HARD_RESET;
  196. else
  197. reset_type = PHY_FUNC_LINK_RESET;
  198. if (scsi_is_sas_phy_local(phy)) {
  199. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  200. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  201. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  202. struct sas_internal *i =
  203. to_sas_internal(sas_ha->core.shost->transportt);
  204. if (!hard_reset && sas_try_ata_reset(asd_phy) == 0)
  205. return 0;
  206. return i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
  207. } else {
  208. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  209. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  210. struct domain_device *ata_dev = sas_ex_to_ata(ddev, phy->number);
  211. if (ata_dev && !hard_reset) {
  212. sas_ata_schedule_reset(ata_dev);
  213. sas_ata_wait_eh(ata_dev);
  214. return 0;
  215. } else
  216. return sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
  217. }
  218. }
  219. static int sas_phy_enable(struct sas_phy *phy, int enable)
  220. {
  221. int ret;
  222. enum phy_func cmd;
  223. if (enable)
  224. cmd = PHY_FUNC_LINK_RESET;
  225. else
  226. cmd = PHY_FUNC_DISABLE;
  227. if (scsi_is_sas_phy_local(phy)) {
  228. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  229. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  230. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  231. struct sas_internal *i =
  232. to_sas_internal(sas_ha->core.shost->transportt);
  233. if (enable)
  234. ret = transport_sas_phy_reset(phy, 0);
  235. else
  236. ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL);
  237. } else {
  238. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  239. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  240. if (enable)
  241. ret = transport_sas_phy_reset(phy, 0);
  242. else
  243. ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL);
  244. }
  245. return ret;
  246. }
  247. int sas_phy_reset(struct sas_phy *phy, int hard_reset)
  248. {
  249. int ret;
  250. enum phy_func reset_type;
  251. if (!phy->enabled)
  252. return -ENODEV;
  253. if (hard_reset)
  254. reset_type = PHY_FUNC_HARD_RESET;
  255. else
  256. reset_type = PHY_FUNC_LINK_RESET;
  257. if (scsi_is_sas_phy_local(phy)) {
  258. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  259. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  260. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  261. struct sas_internal *i =
  262. to_sas_internal(sas_ha->core.shost->transportt);
  263. ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
  264. } else {
  265. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  266. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  267. ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
  268. }
  269. return ret;
  270. }
  271. int sas_set_phy_speed(struct sas_phy *phy,
  272. struct sas_phy_linkrates *rates)
  273. {
  274. int ret;
  275. if ((rates->minimum_linkrate &&
  276. rates->minimum_linkrate > phy->maximum_linkrate) ||
  277. (rates->maximum_linkrate &&
  278. rates->maximum_linkrate < phy->minimum_linkrate))
  279. return -EINVAL;
  280. if (rates->minimum_linkrate &&
  281. rates->minimum_linkrate < phy->minimum_linkrate_hw)
  282. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  283. if (rates->maximum_linkrate &&
  284. rates->maximum_linkrate > phy->maximum_linkrate_hw)
  285. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  286. if (scsi_is_sas_phy_local(phy)) {
  287. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  288. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  289. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  290. struct sas_internal *i =
  291. to_sas_internal(sas_ha->core.shost->transportt);
  292. ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE,
  293. rates);
  294. } else {
  295. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  296. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  297. ret = sas_smp_phy_control(ddev, phy->number,
  298. PHY_FUNC_LINK_RESET, rates);
  299. }
  300. return ret;
  301. }
  302. void sas_prep_resume_ha(struct sas_ha_struct *ha)
  303. {
  304. int i;
  305. set_bit(SAS_HA_REGISTERED, &ha->state);
  306. /* clear out any stale link events/data from the suspension path */
  307. for (i = 0; i < ha->num_phys; i++) {
  308. struct asd_sas_phy *phy = ha->sas_phy[i];
  309. memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
  310. phy->port_events_pending = 0;
  311. phy->phy_events_pending = 0;
  312. phy->frame_rcvd_size = 0;
  313. }
  314. }
  315. EXPORT_SYMBOL(sas_prep_resume_ha);
  316. static int phys_suspended(struct sas_ha_struct *ha)
  317. {
  318. int i, rc = 0;
  319. for (i = 0; i < ha->num_phys; i++) {
  320. struct asd_sas_phy *phy = ha->sas_phy[i];
  321. if (phy->suspended)
  322. rc++;
  323. }
  324. return rc;
  325. }
  326. void sas_resume_ha(struct sas_ha_struct *ha)
  327. {
  328. const unsigned long tmo = msecs_to_jiffies(25000);
  329. int i;
  330. /* deform ports on phys that did not resume
  331. * at this point we may be racing the phy coming back (as posted
  332. * by the lldd). So we post the event and once we are in the
  333. * libsas context check that the phy remains suspended before
  334. * tearing it down.
  335. */
  336. i = phys_suspended(ha);
  337. if (i)
  338. dev_info(ha->dev, "waiting up to 25 seconds for %d phy%s to resume\n",
  339. i, i > 1 ? "s" : "");
  340. wait_event_timeout(ha->eh_wait_q, phys_suspended(ha) == 0, tmo);
  341. for (i = 0; i < ha->num_phys; i++) {
  342. struct asd_sas_phy *phy = ha->sas_phy[i];
  343. if (phy->suspended) {
  344. dev_warn(&phy->phy->dev, "resume timeout\n");
  345. sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT);
  346. }
  347. }
  348. /* all phys are back up or timed out, turn on i/o so we can
  349. * flush out disks that did not return
  350. */
  351. scsi_unblock_requests(ha->core.shost);
  352. sas_drain_work(ha);
  353. }
  354. EXPORT_SYMBOL(sas_resume_ha);
  355. void sas_suspend_ha(struct sas_ha_struct *ha)
  356. {
  357. int i;
  358. sas_disable_events(ha);
  359. scsi_block_requests(ha->core.shost);
  360. for (i = 0; i < ha->num_phys; i++) {
  361. struct asd_sas_port *port = ha->sas_port[i];
  362. sas_discover_event(port, DISCE_SUSPEND);
  363. }
  364. /* flush suspend events while unregistered */
  365. mutex_lock(&ha->drain_mutex);
  366. __sas_drain_work(ha);
  367. mutex_unlock(&ha->drain_mutex);
  368. }
  369. EXPORT_SYMBOL(sas_suspend_ha);
  370. static void sas_phy_release(struct sas_phy *phy)
  371. {
  372. kfree(phy->hostdata);
  373. phy->hostdata = NULL;
  374. }
  375. static void phy_reset_work(struct work_struct *work)
  376. {
  377. struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work);
  378. d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset);
  379. }
  380. static void phy_enable_work(struct work_struct *work)
  381. {
  382. struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work);
  383. d->enable_result = sas_phy_enable(d->phy, d->enable);
  384. }
  385. static int sas_phy_setup(struct sas_phy *phy)
  386. {
  387. struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL);
  388. if (!d)
  389. return -ENOMEM;
  390. mutex_init(&d->event_lock);
  391. INIT_SAS_WORK(&d->reset_work, phy_reset_work);
  392. INIT_SAS_WORK(&d->enable_work, phy_enable_work);
  393. d->phy = phy;
  394. phy->hostdata = d;
  395. return 0;
  396. }
  397. static int queue_phy_reset(struct sas_phy *phy, int hard_reset)
  398. {
  399. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  400. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  401. struct sas_phy_data *d = phy->hostdata;
  402. int rc;
  403. if (!d)
  404. return -ENOMEM;
  405. /* libsas workqueue coordinates ata-eh reset with discovery */
  406. mutex_lock(&d->event_lock);
  407. d->reset_result = 0;
  408. d->hard_reset = hard_reset;
  409. spin_lock_irq(&ha->lock);
  410. sas_queue_work(ha, &d->reset_work);
  411. spin_unlock_irq(&ha->lock);
  412. rc = sas_drain_work(ha);
  413. if (rc == 0)
  414. rc = d->reset_result;
  415. mutex_unlock(&d->event_lock);
  416. return rc;
  417. }
  418. static int queue_phy_enable(struct sas_phy *phy, int enable)
  419. {
  420. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  421. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  422. struct sas_phy_data *d = phy->hostdata;
  423. int rc;
  424. if (!d)
  425. return -ENOMEM;
  426. /* libsas workqueue coordinates ata-eh reset with discovery */
  427. mutex_lock(&d->event_lock);
  428. d->enable_result = 0;
  429. d->enable = enable;
  430. spin_lock_irq(&ha->lock);
  431. sas_queue_work(ha, &d->enable_work);
  432. spin_unlock_irq(&ha->lock);
  433. rc = sas_drain_work(ha);
  434. if (rc == 0)
  435. rc = d->enable_result;
  436. mutex_unlock(&d->event_lock);
  437. return rc;
  438. }
  439. static struct sas_function_template sft = {
  440. .phy_enable = queue_phy_enable,
  441. .phy_reset = queue_phy_reset,
  442. .phy_setup = sas_phy_setup,
  443. .phy_release = sas_phy_release,
  444. .set_phy_speed = sas_set_phy_speed,
  445. .get_linkerrors = sas_get_linkerrors,
  446. .smp_handler = sas_smp_handler,
  447. };
  448. struct scsi_transport_template *
  449. sas_domain_attach_transport(struct sas_domain_function_template *dft)
  450. {
  451. struct scsi_transport_template *stt = sas_attach_transport(&sft);
  452. struct sas_internal *i;
  453. if (!stt)
  454. return stt;
  455. i = to_sas_internal(stt);
  456. i->dft = dft;
  457. stt->create_work_queue = 1;
  458. stt->eh_timed_out = sas_scsi_timed_out;
  459. stt->eh_strategy_handler = sas_scsi_recover_host;
  460. return stt;
  461. }
  462. EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
  463. void sas_domain_release_transport(struct scsi_transport_template *stt)
  464. {
  465. sas_release_transport(stt);
  466. }
  467. EXPORT_SYMBOL_GPL(sas_domain_release_transport);
  468. /* ---------- SAS Class register/unregister ---------- */
  469. static int __init sas_class_init(void)
  470. {
  471. sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN);
  472. if (!sas_task_cache)
  473. return -ENOMEM;
  474. return 0;
  475. }
  476. static void __exit sas_class_exit(void)
  477. {
  478. kmem_cache_destroy(sas_task_cache);
  479. }
  480. MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
  481. MODULE_DESCRIPTION("SAS Transport Layer");
  482. MODULE_LICENSE("GPL v2");
  483. module_init(sas_class_init);
  484. module_exit(sas_class_exit);
  485. EXPORT_SYMBOL_GPL(sas_register_ha);
  486. EXPORT_SYMBOL_GPL(sas_unregister_ha);