fc_transport_fcoe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/pci.h>
  20. #include <scsi/libfcoe.h>
  21. #include <scsi/fc_transport_fcoe.h>
  22. /* internal fcoe transport */
  23. struct fcoe_transport_internal {
  24. struct fcoe_transport *t;
  25. struct net_device *netdev;
  26. struct list_head list;
  27. };
  28. /* fcoe transports list and its lock */
  29. static LIST_HEAD(fcoe_transports);
  30. static DEFINE_MUTEX(fcoe_transports_lock);
  31. /**
  32. * fcoe_transport_default - returns ptr to the default transport fcoe_sw
  33. **/
  34. struct fcoe_transport *fcoe_transport_default(void)
  35. {
  36. return &fcoe_sw_transport;
  37. }
  38. /**
  39. * fcoe_transport_to_pcidev - get the pci dev from a netdev
  40. * @netdev: the netdev that pci dev will be retrived from
  41. *
  42. * Returns: NULL or the corrsponding pci_dev
  43. **/
  44. struct pci_dev *fcoe_transport_pcidev(const struct net_device *netdev)
  45. {
  46. if (!netdev->dev.parent)
  47. return NULL;
  48. return to_pci_dev(netdev->dev.parent);
  49. }
  50. /**
  51. * fcoe_transport_device_lookup - find out netdev is managed by the
  52. * transport
  53. * assign a transport to a device
  54. * @netdev: the netdev the transport to be attached to
  55. *
  56. * This will look for existing offload driver, if not found, it falls back to
  57. * the default sw hba (fcoe_sw) as its fcoe transport.
  58. *
  59. * Returns: 0 for success
  60. **/
  61. static struct fcoe_transport_internal *fcoe_transport_device_lookup(
  62. struct fcoe_transport *t, struct net_device *netdev)
  63. {
  64. struct fcoe_transport_internal *ti;
  65. /* assign the transpor to this device */
  66. mutex_lock(&t->devlock);
  67. list_for_each_entry(ti, &t->devlist, list) {
  68. if (ti->netdev == netdev) {
  69. mutex_unlock(&t->devlock);
  70. return ti;
  71. }
  72. }
  73. mutex_unlock(&t->devlock);
  74. return NULL;
  75. }
  76. /**
  77. * fcoe_transport_device_add - assign a transport to a device
  78. * @netdev: the netdev the transport to be attached to
  79. *
  80. * This will look for existing offload driver, if not found, it falls back to
  81. * the default sw hba (fcoe_sw) as its fcoe transport.
  82. *
  83. * Returns: 0 for success
  84. **/
  85. static int fcoe_transport_device_add(struct fcoe_transport *t,
  86. struct net_device *netdev)
  87. {
  88. struct fcoe_transport_internal *ti;
  89. ti = fcoe_transport_device_lookup(t, netdev);
  90. if (ti) {
  91. printk(KERN_DEBUG "fcoe_transport_device_add:"
  92. "device %s is already added to transport %s\n",
  93. netdev->name, t->name);
  94. return -EEXIST;
  95. }
  96. /* allocate an internal struct to host the netdev and the list */
  97. ti = kzalloc(sizeof(*ti), GFP_KERNEL);
  98. if (!ti)
  99. return -ENOMEM;
  100. ti->t = t;
  101. ti->netdev = netdev;
  102. INIT_LIST_HEAD(&ti->list);
  103. dev_hold(ti->netdev);
  104. mutex_lock(&t->devlock);
  105. list_add(&ti->list, &t->devlist);
  106. mutex_unlock(&t->devlock);
  107. printk(KERN_DEBUG "fcoe_transport_device_add:"
  108. "device %s added to transport %s\n",
  109. netdev->name, t->name);
  110. return 0;
  111. }
  112. /**
  113. * fcoe_transport_device_remove - remove a device from its transport
  114. * @netdev: the netdev the transport to be attached to
  115. *
  116. * this removes the device from the transport so the given transport will
  117. * not manage this device any more
  118. *
  119. * Returns: 0 for success
  120. **/
  121. static int fcoe_transport_device_remove(struct fcoe_transport *t,
  122. struct net_device *netdev)
  123. {
  124. struct fcoe_transport_internal *ti;
  125. ti = fcoe_transport_device_lookup(t, netdev);
  126. if (!ti) {
  127. printk(KERN_DEBUG "fcoe_transport_device_remove:"
  128. "device %s is not managed by transport %s\n",
  129. netdev->name, t->name);
  130. return -ENODEV;
  131. }
  132. mutex_lock(&t->devlock);
  133. list_del(&ti->list);
  134. mutex_unlock(&t->devlock);
  135. printk(KERN_DEBUG "fcoe_transport_device_remove:"
  136. "device %s removed from transport %s\n",
  137. netdev->name, t->name);
  138. dev_put(ti->netdev);
  139. kfree(ti);
  140. return 0;
  141. }
  142. /**
  143. * fcoe_transport_device_remove_all - remove all from transport devlist
  144. *
  145. * this removes the device from the transport so the given transport will
  146. * not manage this device any more
  147. *
  148. * Returns: 0 for success
  149. **/
  150. static void fcoe_transport_device_remove_all(struct fcoe_transport *t)
  151. {
  152. struct fcoe_transport_internal *ti, *tmp;
  153. mutex_lock(&t->devlock);
  154. list_for_each_entry_safe(ti, tmp, &t->devlist, list) {
  155. list_del(&ti->list);
  156. kfree(ti);
  157. }
  158. mutex_unlock(&t->devlock);
  159. }
  160. /**
  161. * fcoe_transport_match - use the bus device match function to match the hw
  162. * @t: the fcoe transport
  163. * @netdev:
  164. *
  165. * This function is used to check if the givne transport wants to manage the
  166. * input netdev. if the transports implements the match function, it will be
  167. * called, o.w. we just compare the pci vendor and device id.
  168. *
  169. * Returns: true for match up
  170. **/
  171. static bool fcoe_transport_match(struct fcoe_transport *t,
  172. struct net_device *netdev)
  173. {
  174. /* match transport by vendor and device id */
  175. struct pci_dev *pci;
  176. pci = fcoe_transport_pcidev(netdev);
  177. if (pci) {
  178. printk(KERN_DEBUG "fcoe_transport_match:"
  179. "%s:%x:%x -- %s:%x:%x\n",
  180. t->name, t->vendor, t->device,
  181. netdev->name, pci->vendor, pci->device);
  182. /* if transport supports match */
  183. if (t->match)
  184. return t->match(netdev);
  185. /* else just compare the vendor and device id: pci only */
  186. return (t->vendor == pci->vendor) && (t->device == pci->device);
  187. }
  188. return false;
  189. }
  190. /**
  191. * fcoe_transport_lookup - check if the transport is already registered
  192. * @t: the transport to be looked up
  193. *
  194. * This compares the parent device (pci) vendor and device id
  195. *
  196. * Returns: NULL if not found
  197. *
  198. * TODO - return default sw transport if no other transport is found
  199. **/
  200. static struct fcoe_transport *fcoe_transport_lookup(
  201. struct net_device *netdev)
  202. {
  203. struct fcoe_transport *t;
  204. mutex_lock(&fcoe_transports_lock);
  205. list_for_each_entry(t, &fcoe_transports, list) {
  206. if (fcoe_transport_match(t, netdev)) {
  207. mutex_unlock(&fcoe_transports_lock);
  208. return t;
  209. }
  210. }
  211. mutex_unlock(&fcoe_transports_lock);
  212. printk(KERN_DEBUG "fcoe_transport_lookup:"
  213. "use default transport for %s\n", netdev->name);
  214. return fcoe_transport_default();
  215. }
  216. /**
  217. * fcoe_transport_register - adds a fcoe transport to the fcoe transports list
  218. * @t: ptr to the fcoe transport to be added
  219. *
  220. * Returns: 0 for success
  221. **/
  222. int fcoe_transport_register(struct fcoe_transport *t)
  223. {
  224. struct fcoe_transport *tt;
  225. /* TODO - add fcoe_transport specific initialization here */
  226. mutex_lock(&fcoe_transports_lock);
  227. list_for_each_entry(tt, &fcoe_transports, list) {
  228. if (tt == t) {
  229. mutex_unlock(&fcoe_transports_lock);
  230. return -EEXIST;
  231. }
  232. }
  233. list_add_tail(&t->list, &fcoe_transports);
  234. mutex_unlock(&fcoe_transports_lock);
  235. mutex_init(&t->devlock);
  236. INIT_LIST_HEAD(&t->devlist);
  237. printk(KERN_DEBUG "fcoe_transport_register:%s\n", t->name);
  238. return 0;
  239. }
  240. EXPORT_SYMBOL_GPL(fcoe_transport_register);
  241. /**
  242. * fcoe_transport_unregister - remove the tranport fro the fcoe transports list
  243. * @t: ptr to the fcoe transport to be removed
  244. *
  245. * Returns: 0 for success
  246. **/
  247. int fcoe_transport_unregister(struct fcoe_transport *t)
  248. {
  249. struct fcoe_transport *tt, *tmp;
  250. mutex_lock(&fcoe_transports_lock);
  251. list_for_each_entry_safe(tt, tmp, &fcoe_transports, list) {
  252. if (tt == t) {
  253. list_del(&t->list);
  254. mutex_unlock(&fcoe_transports_lock);
  255. fcoe_transport_device_remove_all(t);
  256. printk(KERN_DEBUG "fcoe_transport_unregister:%s\n",
  257. t->name);
  258. return 0;
  259. }
  260. }
  261. mutex_unlock(&fcoe_transports_lock);
  262. return -ENODEV;
  263. }
  264. EXPORT_SYMBOL_GPL(fcoe_transport_unregister);
  265. /*
  266. * fcoe_load_transport_driver - load an offload driver by alias name
  267. * @netdev: the target net device
  268. *
  269. * Requests for an offload driver module as the fcoe transport, if fails, it
  270. * falls back to use the SW HBA (fcoe_sw) as its transport
  271. *
  272. * TODO -
  273. * 1. supports only PCI device
  274. * 2. needs fix for VLAn and bonding
  275. * 3. pure hw fcoe hba may not have netdev
  276. *
  277. * Returns: 0 for success
  278. **/
  279. int fcoe_load_transport_driver(struct net_device *netdev)
  280. {
  281. struct pci_dev *pci;
  282. struct device *dev = netdev->dev.parent;
  283. if (fcoe_transport_lookup(netdev)) {
  284. /* load default transport */
  285. printk(KERN_DEBUG "fcoe: already loaded transport for %s\n",
  286. netdev->name);
  287. return -EEXIST;
  288. }
  289. pci = to_pci_dev(dev);
  290. if (dev->bus != &pci_bus_type) {
  291. printk(KERN_DEBUG "fcoe: support noly PCI device\n");
  292. return -ENODEV;
  293. }
  294. printk(KERN_DEBUG "fcoe: loading driver fcoe-pci-0x%04x-0x%04x\n",
  295. pci->vendor, pci->device);
  296. return request_module("fcoe-pci-0x%04x-0x%04x",
  297. pci->vendor, pci->device);
  298. }
  299. EXPORT_SYMBOL_GPL(fcoe_load_transport_driver);
  300. /**
  301. * fcoe_transport_attach - load transport to fcoe
  302. * @netdev: the netdev the transport to be attached to
  303. *
  304. * This will look for existing offload driver, if not found, it falls back to
  305. * the default sw hba (fcoe_sw) as its fcoe transport.
  306. *
  307. * Returns: 0 for success
  308. **/
  309. int fcoe_transport_attach(struct net_device *netdev)
  310. {
  311. struct fcoe_transport *t;
  312. /* find the corresponding transport */
  313. t = fcoe_transport_lookup(netdev);
  314. if (!t) {
  315. printk(KERN_DEBUG "fcoe_transport_attach"
  316. ":no transport for %s:use %s\n",
  317. netdev->name, t->name);
  318. return -ENODEV;
  319. }
  320. /* add to the transport */
  321. if (fcoe_transport_device_add(t, netdev)) {
  322. printk(KERN_DEBUG "fcoe_transport_attach"
  323. ":failed to add %s to tramsport %s\n",
  324. netdev->name, t->name);
  325. return -EIO;
  326. }
  327. /* transport create function */
  328. if (t->create)
  329. t->create(netdev);
  330. printk(KERN_DEBUG "fcoe_transport_attach:transport %s for %s\n",
  331. t->name, netdev->name);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(fcoe_transport_attach);
  335. /**
  336. * fcoe_transport_release - unload transport from fcoe
  337. * @netdev: the net device on which fcoe is to be released
  338. *
  339. * Returns: 0 for success
  340. **/
  341. int fcoe_transport_release(struct net_device *netdev)
  342. {
  343. struct fcoe_transport *t;
  344. /* find the corresponding transport */
  345. t = fcoe_transport_lookup(netdev);
  346. if (!t) {
  347. printk(KERN_DEBUG "fcoe_transport_release:"
  348. "no transport for %s:use %s\n",
  349. netdev->name, t->name);
  350. return -ENODEV;
  351. }
  352. /* remove the device from the transport */
  353. if (fcoe_transport_device_remove(t, netdev)) {
  354. printk(KERN_DEBUG "fcoe_transport_release:"
  355. "failed to add %s to tramsport %s\n",
  356. netdev->name, t->name);
  357. return -EIO;
  358. }
  359. /* transport destroy function */
  360. if (t->destroy)
  361. t->destroy(netdev);
  362. printk(KERN_DEBUG "fcoe_transport_release:"
  363. "device %s dettached from transport %s\n",
  364. netdev->name, t->name);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL_GPL(fcoe_transport_release);
  368. /**
  369. * fcoe_transport_init - initializes fcoe transport layer
  370. *
  371. * This prepares for the fcoe transport layer
  372. *
  373. * Returns: none
  374. **/
  375. int __init fcoe_transport_init(void)
  376. {
  377. INIT_LIST_HEAD(&fcoe_transports);
  378. mutex_init(&fcoe_transports_lock);
  379. return 0;
  380. }
  381. /**
  382. * fcoe_transport_exit - cleans up the fcoe transport layer
  383. * This cleans up the fcoe transport layer. removing any transport on the list,
  384. * note that the transport destroy func is not called here.
  385. *
  386. * Returns: none
  387. **/
  388. int __exit fcoe_transport_exit(void)
  389. {
  390. struct fcoe_transport *t, *tmp;
  391. mutex_lock(&fcoe_transports_lock);
  392. list_for_each_entry_safe(t, tmp, &fcoe_transports, list) {
  393. list_del(&t->list);
  394. mutex_unlock(&fcoe_transports_lock);
  395. fcoe_transport_device_remove_all(t);
  396. mutex_lock(&fcoe_transports_lock);
  397. }
  398. mutex_unlock(&fcoe_transports_lock);
  399. return 0;
  400. }