sas_init.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Serial Attached SCSI (SAS) Transport Layer initialization
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/spinlock.h>
  13. #include <scsi/sas_ata.h>
  14. #include <scsi/scsi_host.h>
  15. #include <scsi/scsi_device.h>
  16. #include <scsi/scsi_transport.h>
  17. #include <scsi/scsi_transport_sas.h>
  18. #include "sas_internal.h"
  19. #include "scsi_sas_internal.h"
  20. static struct kmem_cache *sas_task_cache;
  21. static struct kmem_cache *sas_event_cache;
  22. struct sas_task *sas_alloc_task(gfp_t flags)
  23. {
  24. struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags);
  25. if (task) {
  26. spin_lock_init(&task->task_state_lock);
  27. task->task_state_flags = SAS_TASK_STATE_PENDING;
  28. }
  29. return task;
  30. }
  31. EXPORT_SYMBOL_GPL(sas_alloc_task);
  32. struct sas_task *sas_alloc_slow_task(gfp_t flags)
  33. {
  34. struct sas_task *task = sas_alloc_task(flags);
  35. struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags);
  36. if (!task || !slow) {
  37. if (task)
  38. kmem_cache_free(sas_task_cache, task);
  39. kfree(slow);
  40. return NULL;
  41. }
  42. task->slow_task = slow;
  43. slow->task = task;
  44. timer_setup(&slow->timer, NULL, 0);
  45. init_completion(&slow->completion);
  46. return task;
  47. }
  48. EXPORT_SYMBOL_GPL(sas_alloc_slow_task);
  49. void sas_free_task(struct sas_task *task)
  50. {
  51. if (task) {
  52. kfree(task->slow_task);
  53. kmem_cache_free(sas_task_cache, task);
  54. }
  55. }
  56. EXPORT_SYMBOL_GPL(sas_free_task);
  57. /*------------ SAS addr hash -----------*/
  58. void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
  59. {
  60. const u32 poly = 0x00DB2777;
  61. u32 r = 0;
  62. int i;
  63. for (i = 0; i < SAS_ADDR_SIZE; i++) {
  64. int b;
  65. for (b = (SAS_ADDR_SIZE - 1); b >= 0; b--) {
  66. r <<= 1;
  67. if ((1 << b) & sas_addr[i]) {
  68. if (!(r & 0x01000000))
  69. r ^= poly;
  70. } else if (r & 0x01000000) {
  71. r ^= poly;
  72. }
  73. }
  74. }
  75. hashed[0] = (r >> 16) & 0xFF;
  76. hashed[1] = (r >> 8) & 0xFF;
  77. hashed[2] = r & 0xFF;
  78. }
  79. int sas_register_ha(struct sas_ha_struct *sas_ha)
  80. {
  81. char name[64];
  82. int error = 0;
  83. mutex_init(&sas_ha->disco_mutex);
  84. spin_lock_init(&sas_ha->phy_port_lock);
  85. sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr);
  86. set_bit(SAS_HA_REGISTERED, &sas_ha->state);
  87. spin_lock_init(&sas_ha->lock);
  88. mutex_init(&sas_ha->drain_mutex);
  89. init_waitqueue_head(&sas_ha->eh_wait_q);
  90. INIT_LIST_HEAD(&sas_ha->defer_q);
  91. INIT_LIST_HEAD(&sas_ha->eh_dev_q);
  92. sas_ha->event_thres = SAS_PHY_SHUTDOWN_THRES;
  93. error = sas_register_phys(sas_ha);
  94. if (error) {
  95. pr_notice("couldn't register sas phys:%d\n", error);
  96. return error;
  97. }
  98. error = sas_register_ports(sas_ha);
  99. if (error) {
  100. pr_notice("couldn't register sas ports:%d\n", error);
  101. goto Undo_phys;
  102. }
  103. error = -ENOMEM;
  104. snprintf(name, sizeof(name), "%s_event_q", dev_name(sas_ha->dev));
  105. sas_ha->event_q = create_singlethread_workqueue(name);
  106. if (!sas_ha->event_q)
  107. goto Undo_ports;
  108. snprintf(name, sizeof(name), "%s_disco_q", dev_name(sas_ha->dev));
  109. sas_ha->disco_q = create_singlethread_workqueue(name);
  110. if (!sas_ha->disco_q)
  111. goto Undo_event_q;
  112. INIT_LIST_HEAD(&sas_ha->eh_done_q);
  113. INIT_LIST_HEAD(&sas_ha->eh_ata_q);
  114. return 0;
  115. Undo_event_q:
  116. destroy_workqueue(sas_ha->event_q);
  117. Undo_ports:
  118. sas_unregister_ports(sas_ha);
  119. Undo_phys:
  120. return error;
  121. }
  122. EXPORT_SYMBOL_GPL(sas_register_ha);
  123. static void sas_disable_events(struct sas_ha_struct *sas_ha)
  124. {
  125. /* Set the state to unregistered to avoid further unchained
  126. * events to be queued, and flush any in-progress drainers
  127. */
  128. mutex_lock(&sas_ha->drain_mutex);
  129. spin_lock_irq(&sas_ha->lock);
  130. clear_bit(SAS_HA_REGISTERED, &sas_ha->state);
  131. spin_unlock_irq(&sas_ha->lock);
  132. __sas_drain_work(sas_ha);
  133. mutex_unlock(&sas_ha->drain_mutex);
  134. }
  135. int sas_unregister_ha(struct sas_ha_struct *sas_ha)
  136. {
  137. sas_disable_events(sas_ha);
  138. sas_unregister_ports(sas_ha);
  139. /* flush unregistration work */
  140. mutex_lock(&sas_ha->drain_mutex);
  141. __sas_drain_work(sas_ha);
  142. mutex_unlock(&sas_ha->drain_mutex);
  143. destroy_workqueue(sas_ha->disco_q);
  144. destroy_workqueue(sas_ha->event_q);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL_GPL(sas_unregister_ha);
  148. static int sas_get_linkerrors(struct sas_phy *phy)
  149. {
  150. if (scsi_is_sas_phy_local(phy)) {
  151. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  152. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  153. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  154. struct sas_internal *i =
  155. to_sas_internal(sas_ha->core.shost->transportt);
  156. return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL);
  157. }
  158. return sas_smp_get_phy_events(phy);
  159. }
  160. int sas_try_ata_reset(struct asd_sas_phy *asd_phy)
  161. {
  162. struct domain_device *dev = NULL;
  163. /* try to route user requested link resets through libata */
  164. if (asd_phy->port)
  165. dev = asd_phy->port->port_dev;
  166. /* validate that dev has been probed */
  167. if (dev)
  168. dev = sas_find_dev_by_rphy(dev->rphy);
  169. if (dev && dev_is_sata(dev)) {
  170. sas_ata_schedule_reset(dev);
  171. sas_ata_wait_eh(dev);
  172. return 0;
  173. }
  174. return -ENODEV;
  175. }
  176. /*
  177. * transport_sas_phy_reset - reset a phy and permit libata to manage the link
  178. *
  179. * phy reset request via sysfs in host workqueue context so we know we
  180. * can block on eh and safely traverse the domain_device topology
  181. */
  182. static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset)
  183. {
  184. enum phy_func reset_type;
  185. if (hard_reset)
  186. reset_type = PHY_FUNC_HARD_RESET;
  187. else
  188. reset_type = PHY_FUNC_LINK_RESET;
  189. if (scsi_is_sas_phy_local(phy)) {
  190. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  191. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  192. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  193. struct sas_internal *i =
  194. to_sas_internal(sas_ha->core.shost->transportt);
  195. if (!hard_reset && sas_try_ata_reset(asd_phy) == 0)
  196. return 0;
  197. return i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
  198. } else {
  199. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  200. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  201. struct domain_device *ata_dev = sas_ex_to_ata(ddev, phy->number);
  202. if (ata_dev && !hard_reset) {
  203. sas_ata_schedule_reset(ata_dev);
  204. sas_ata_wait_eh(ata_dev);
  205. return 0;
  206. } else
  207. return sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
  208. }
  209. }
  210. int sas_phy_enable(struct sas_phy *phy, int enable)
  211. {
  212. int ret;
  213. enum phy_func cmd;
  214. if (enable)
  215. cmd = PHY_FUNC_LINK_RESET;
  216. else
  217. cmd = PHY_FUNC_DISABLE;
  218. if (scsi_is_sas_phy_local(phy)) {
  219. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  220. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  221. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  222. struct sas_internal *i =
  223. to_sas_internal(sas_ha->core.shost->transportt);
  224. if (enable)
  225. ret = transport_sas_phy_reset(phy, 0);
  226. else
  227. ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL);
  228. } else {
  229. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  230. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  231. if (enable)
  232. ret = transport_sas_phy_reset(phy, 0);
  233. else
  234. ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL);
  235. }
  236. return ret;
  237. }
  238. EXPORT_SYMBOL_GPL(sas_phy_enable);
  239. int sas_phy_reset(struct sas_phy *phy, int hard_reset)
  240. {
  241. int ret;
  242. enum phy_func reset_type;
  243. if (!phy->enabled)
  244. return -ENODEV;
  245. if (hard_reset)
  246. reset_type = PHY_FUNC_HARD_RESET;
  247. else
  248. reset_type = PHY_FUNC_LINK_RESET;
  249. if (scsi_is_sas_phy_local(phy)) {
  250. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  251. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  252. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  253. struct sas_internal *i =
  254. to_sas_internal(sas_ha->core.shost->transportt);
  255. ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL);
  256. } else {
  257. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  258. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  259. ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL);
  260. }
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(sas_phy_reset);
  264. int sas_set_phy_speed(struct sas_phy *phy,
  265. struct sas_phy_linkrates *rates)
  266. {
  267. int ret;
  268. if ((rates->minimum_linkrate &&
  269. rates->minimum_linkrate > phy->maximum_linkrate) ||
  270. (rates->maximum_linkrate &&
  271. rates->maximum_linkrate < phy->minimum_linkrate))
  272. return -EINVAL;
  273. if (rates->minimum_linkrate &&
  274. rates->minimum_linkrate < phy->minimum_linkrate_hw)
  275. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  276. if (rates->maximum_linkrate &&
  277. rates->maximum_linkrate > phy->maximum_linkrate_hw)
  278. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  279. if (scsi_is_sas_phy_local(phy)) {
  280. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  281. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  282. struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
  283. struct sas_internal *i =
  284. to_sas_internal(sas_ha->core.shost->transportt);
  285. ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE,
  286. rates);
  287. } else {
  288. struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
  289. struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
  290. ret = sas_smp_phy_control(ddev, phy->number,
  291. PHY_FUNC_LINK_RESET, rates);
  292. }
  293. return ret;
  294. }
  295. void sas_prep_resume_ha(struct sas_ha_struct *ha)
  296. {
  297. int i;
  298. set_bit(SAS_HA_REGISTERED, &ha->state);
  299. set_bit(SAS_HA_RESUMING, &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->frame_rcvd_size = 0;
  305. }
  306. }
  307. EXPORT_SYMBOL(sas_prep_resume_ha);
  308. static int phys_suspended(struct sas_ha_struct *ha)
  309. {
  310. int i, rc = 0;
  311. for (i = 0; i < ha->num_phys; i++) {
  312. struct asd_sas_phy *phy = ha->sas_phy[i];
  313. if (phy->suspended)
  314. rc++;
  315. }
  316. return rc;
  317. }
  318. static void sas_resume_insert_broadcast_ha(struct sas_ha_struct *ha)
  319. {
  320. int i;
  321. for (i = 0; i < ha->num_phys; i++) {
  322. struct asd_sas_port *port = ha->sas_port[i];
  323. struct domain_device *dev = port->port_dev;
  324. if (dev && dev_is_expander(dev->dev_type)) {
  325. struct asd_sas_phy *first_phy;
  326. spin_lock(&port->phy_list_lock);
  327. first_phy = list_first_entry_or_null(
  328. &port->phy_list, struct asd_sas_phy,
  329. port_phy_el);
  330. spin_unlock(&port->phy_list_lock);
  331. if (first_phy)
  332. sas_notify_port_event(first_phy,
  333. PORTE_BROADCAST_RCVD, GFP_KERNEL);
  334. }
  335. }
  336. }
  337. static void _sas_resume_ha(struct sas_ha_struct *ha, bool drain)
  338. {
  339. const unsigned long tmo = msecs_to_jiffies(25000);
  340. int i;
  341. /* deform ports on phys that did not resume
  342. * at this point we may be racing the phy coming back (as posted
  343. * by the lldd). So we post the event and once we are in the
  344. * libsas context check that the phy remains suspended before
  345. * tearing it down.
  346. */
  347. i = phys_suspended(ha);
  348. if (i)
  349. dev_info(ha->dev, "waiting up to 25 seconds for %d phy%s to resume\n",
  350. i, i > 1 ? "s" : "");
  351. wait_event_timeout(ha->eh_wait_q, phys_suspended(ha) == 0, tmo);
  352. for (i = 0; i < ha->num_phys; i++) {
  353. struct asd_sas_phy *phy = ha->sas_phy[i];
  354. if (phy->suspended) {
  355. dev_warn(&phy->phy->dev, "resume timeout\n");
  356. sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT,
  357. GFP_KERNEL);
  358. }
  359. }
  360. /* all phys are back up or timed out, turn on i/o so we can
  361. * flush out disks that did not return
  362. */
  363. scsi_unblock_requests(ha->core.shost);
  364. if (drain)
  365. sas_drain_work(ha);
  366. clear_bit(SAS_HA_RESUMING, &ha->state);
  367. sas_queue_deferred_work(ha);
  368. /* send event PORTE_BROADCAST_RCVD to identify some new inserted
  369. * disks for expander
  370. */
  371. sas_resume_insert_broadcast_ha(ha);
  372. }
  373. void sas_resume_ha(struct sas_ha_struct *ha)
  374. {
  375. _sas_resume_ha(ha, true);
  376. }
  377. EXPORT_SYMBOL(sas_resume_ha);
  378. /* A no-sync variant, which does not call sas_drain_ha(). */
  379. void sas_resume_ha_no_sync(struct sas_ha_struct *ha)
  380. {
  381. _sas_resume_ha(ha, false);
  382. }
  383. EXPORT_SYMBOL(sas_resume_ha_no_sync);
  384. void sas_suspend_ha(struct sas_ha_struct *ha)
  385. {
  386. int i;
  387. sas_disable_events(ha);
  388. scsi_block_requests(ha->core.shost);
  389. for (i = 0; i < ha->num_phys; i++) {
  390. struct asd_sas_port *port = ha->sas_port[i];
  391. sas_discover_event(port, DISCE_SUSPEND);
  392. }
  393. /* flush suspend events while unregistered */
  394. mutex_lock(&ha->drain_mutex);
  395. __sas_drain_work(ha);
  396. mutex_unlock(&ha->drain_mutex);
  397. }
  398. EXPORT_SYMBOL(sas_suspend_ha);
  399. static void sas_phy_release(struct sas_phy *phy)
  400. {
  401. kfree(phy->hostdata);
  402. phy->hostdata = NULL;
  403. }
  404. static void phy_reset_work(struct work_struct *work)
  405. {
  406. struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work);
  407. d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset);
  408. }
  409. static void phy_enable_work(struct work_struct *work)
  410. {
  411. struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work);
  412. d->enable_result = sas_phy_enable(d->phy, d->enable);
  413. }
  414. static int sas_phy_setup(struct sas_phy *phy)
  415. {
  416. struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL);
  417. if (!d)
  418. return -ENOMEM;
  419. mutex_init(&d->event_lock);
  420. INIT_SAS_WORK(&d->reset_work, phy_reset_work);
  421. INIT_SAS_WORK(&d->enable_work, phy_enable_work);
  422. d->phy = phy;
  423. phy->hostdata = d;
  424. return 0;
  425. }
  426. static int queue_phy_reset(struct sas_phy *phy, int hard_reset)
  427. {
  428. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  429. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  430. struct sas_phy_data *d = phy->hostdata;
  431. int rc;
  432. if (!d)
  433. return -ENOMEM;
  434. pm_runtime_get_sync(ha->dev);
  435. /* libsas workqueue coordinates ata-eh reset with discovery */
  436. mutex_lock(&d->event_lock);
  437. d->reset_result = 0;
  438. d->hard_reset = hard_reset;
  439. spin_lock_irq(&ha->lock);
  440. sas_queue_work(ha, &d->reset_work);
  441. spin_unlock_irq(&ha->lock);
  442. rc = sas_drain_work(ha);
  443. if (rc == 0)
  444. rc = d->reset_result;
  445. mutex_unlock(&d->event_lock);
  446. pm_runtime_put_sync(ha->dev);
  447. return rc;
  448. }
  449. static int queue_phy_enable(struct sas_phy *phy, int enable)
  450. {
  451. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  452. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  453. struct sas_phy_data *d = phy->hostdata;
  454. int rc;
  455. if (!d)
  456. return -ENOMEM;
  457. pm_runtime_get_sync(ha->dev);
  458. /* libsas workqueue coordinates ata-eh reset with discovery */
  459. mutex_lock(&d->event_lock);
  460. d->enable_result = 0;
  461. d->enable = enable;
  462. spin_lock_irq(&ha->lock);
  463. sas_queue_work(ha, &d->enable_work);
  464. spin_unlock_irq(&ha->lock);
  465. rc = sas_drain_work(ha);
  466. if (rc == 0)
  467. rc = d->enable_result;
  468. mutex_unlock(&d->event_lock);
  469. pm_runtime_put_sync(ha->dev);
  470. return rc;
  471. }
  472. static struct sas_function_template sft = {
  473. .phy_enable = queue_phy_enable,
  474. .phy_reset = queue_phy_reset,
  475. .phy_setup = sas_phy_setup,
  476. .phy_release = sas_phy_release,
  477. .set_phy_speed = sas_set_phy_speed,
  478. .get_linkerrors = sas_get_linkerrors,
  479. .smp_handler = sas_smp_handler,
  480. };
  481. static inline ssize_t phy_event_threshold_show(struct device *dev,
  482. struct device_attribute *attr, char *buf)
  483. {
  484. struct Scsi_Host *shost = class_to_shost(dev);
  485. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  486. return scnprintf(buf, PAGE_SIZE, "%u\n", sha->event_thres);
  487. }
  488. static inline ssize_t phy_event_threshold_store(struct device *dev,
  489. struct device_attribute *attr,
  490. const char *buf, size_t count)
  491. {
  492. struct Scsi_Host *shost = class_to_shost(dev);
  493. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  494. sha->event_thres = simple_strtol(buf, NULL, 10);
  495. /* threshold cannot be set too small */
  496. if (sha->event_thres < 32)
  497. sha->event_thres = 32;
  498. return count;
  499. }
  500. DEVICE_ATTR(phy_event_threshold,
  501. S_IRUGO|S_IWUSR,
  502. phy_event_threshold_show,
  503. phy_event_threshold_store);
  504. EXPORT_SYMBOL_GPL(dev_attr_phy_event_threshold);
  505. struct scsi_transport_template *
  506. sas_domain_attach_transport(struct sas_domain_function_template *dft)
  507. {
  508. struct scsi_transport_template *stt = sas_attach_transport(&sft);
  509. struct sas_internal *i;
  510. if (!stt)
  511. return stt;
  512. i = to_sas_internal(stt);
  513. i->dft = dft;
  514. stt->create_work_queue = 1;
  515. stt->eh_strategy_handler = sas_scsi_recover_host;
  516. return stt;
  517. }
  518. EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
  519. struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy,
  520. gfp_t gfp_flags)
  521. {
  522. struct asd_sas_event *event;
  523. struct sas_ha_struct *sas_ha = phy->ha;
  524. struct sas_internal *i =
  525. to_sas_internal(sas_ha->core.shost->transportt);
  526. event = kmem_cache_zalloc(sas_event_cache, gfp_flags);
  527. if (!event)
  528. return NULL;
  529. atomic_inc(&phy->event_nr);
  530. if (atomic_read(&phy->event_nr) > phy->ha->event_thres) {
  531. if (i->dft->lldd_control_phy) {
  532. if (cmpxchg(&phy->in_shutdown, 0, 1) == 0) {
  533. pr_notice("The phy%d bursting events, shut it down.\n",
  534. phy->id);
  535. sas_notify_phy_event(phy, PHYE_SHUTDOWN,
  536. gfp_flags);
  537. }
  538. } else {
  539. /* Do not support PHY control, stop allocating events */
  540. WARN_ONCE(1, "PHY control not supported.\n");
  541. kmem_cache_free(sas_event_cache, event);
  542. atomic_dec(&phy->event_nr);
  543. event = NULL;
  544. }
  545. }
  546. return event;
  547. }
  548. void sas_free_event(struct asd_sas_event *event)
  549. {
  550. struct asd_sas_phy *phy = event->phy;
  551. kmem_cache_free(sas_event_cache, event);
  552. atomic_dec(&phy->event_nr);
  553. }
  554. /* ---------- SAS Class register/unregister ---------- */
  555. static int __init sas_class_init(void)
  556. {
  557. sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN);
  558. if (!sas_task_cache)
  559. goto out;
  560. sas_event_cache = KMEM_CACHE(asd_sas_event, SLAB_HWCACHE_ALIGN);
  561. if (!sas_event_cache)
  562. goto free_task_kmem;
  563. return 0;
  564. free_task_kmem:
  565. kmem_cache_destroy(sas_task_cache);
  566. out:
  567. return -ENOMEM;
  568. }
  569. static void __exit sas_class_exit(void)
  570. {
  571. kmem_cache_destroy(sas_task_cache);
  572. kmem_cache_destroy(sas_event_cache);
  573. }
  574. MODULE_AUTHOR("Luben Tuikov <[email protected]>");
  575. MODULE_DESCRIPTION("SAS Transport Layer");
  576. MODULE_LICENSE("GPL v2");
  577. module_init(sas_class_init);
  578. module_exit(sas_class_exit);