sas_init.c 14 KB

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