fc_transport_fcoe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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() - Lookup a transport
  52. * @netdev: the netdev the transport to be attached to
  53. *
  54. * This will look for existing offload driver, if not found, it falls back to
  55. * the default sw hba (fcoe_sw) as its fcoe transport.
  56. *
  57. * Returns: 0 for success
  58. */
  59. static struct fcoe_transport_internal *fcoe_transport_device_lookup(
  60. struct fcoe_transport *t, struct net_device *netdev)
  61. {
  62. struct fcoe_transport_internal *ti;
  63. /* assign the transpor to this device */
  64. mutex_lock(&t->devlock);
  65. list_for_each_entry(ti, &t->devlist, list) {
  66. if (ti->netdev == netdev) {
  67. mutex_unlock(&t->devlock);
  68. return ti;
  69. }
  70. }
  71. mutex_unlock(&t->devlock);
  72. return NULL;
  73. }
  74. /**
  75. * fcoe_transport_device_add() - Assign a transport to a device
  76. * @netdev: the netdev the transport to be attached to
  77. *
  78. * This will look for existing offload driver, if not found, it falls back to
  79. * the default sw hba (fcoe_sw) as its fcoe transport.
  80. *
  81. * Returns: 0 for success
  82. */
  83. static int fcoe_transport_device_add(struct fcoe_transport *t,
  84. struct net_device *netdev)
  85. {
  86. struct fcoe_transport_internal *ti;
  87. ti = fcoe_transport_device_lookup(t, netdev);
  88. if (ti) {
  89. printk(KERN_DEBUG "fcoe_transport_device_add:"
  90. "device %s is already added to transport %s\n",
  91. netdev->name, t->name);
  92. return -EEXIST;
  93. }
  94. /* allocate an internal struct to host the netdev and the list */
  95. ti = kzalloc(sizeof(*ti), GFP_KERNEL);
  96. if (!ti)
  97. return -ENOMEM;
  98. ti->t = t;
  99. ti->netdev = netdev;
  100. INIT_LIST_HEAD(&ti->list);
  101. dev_hold(ti->netdev);
  102. mutex_lock(&t->devlock);
  103. list_add(&ti->list, &t->devlist);
  104. mutex_unlock(&t->devlock);
  105. printk(KERN_DEBUG "fcoe_transport_device_add:"
  106. "device %s added to transport %s\n",
  107. netdev->name, t->name);
  108. return 0;
  109. }
  110. /**
  111. * fcoe_transport_device_remove() - Remove a device from its transport
  112. * @netdev: the netdev the transport to be attached to
  113. *
  114. * This removes the device from the transport so the given transport will
  115. * not manage this device any more
  116. *
  117. * Returns: 0 for success
  118. */
  119. static int fcoe_transport_device_remove(struct fcoe_transport *t,
  120. struct net_device *netdev)
  121. {
  122. struct fcoe_transport_internal *ti;
  123. ti = fcoe_transport_device_lookup(t, netdev);
  124. if (!ti) {
  125. printk(KERN_DEBUG "fcoe_transport_device_remove:"
  126. "device %s is not managed by transport %s\n",
  127. netdev->name, t->name);
  128. return -ENODEV;
  129. }
  130. mutex_lock(&t->devlock);
  131. list_del(&ti->list);
  132. mutex_unlock(&t->devlock);
  133. printk(KERN_DEBUG "fcoe_transport_device_remove:"
  134. "device %s removed from transport %s\n",
  135. netdev->name, t->name);
  136. dev_put(ti->netdev);
  137. kfree(ti);
  138. return 0;
  139. }
  140. /**
  141. * fcoe_transport_device_remove_all() - Remove all from transport devlist
  142. *
  143. * This removes the device from the transport so the given transport will
  144. * not manage this device any more
  145. *
  146. * Returns: 0 for success
  147. */
  148. static void fcoe_transport_device_remove_all(struct fcoe_transport *t)
  149. {
  150. struct fcoe_transport_internal *ti, *tmp;
  151. mutex_lock(&t->devlock);
  152. list_for_each_entry_safe(ti, tmp, &t->devlist, list) {
  153. list_del(&ti->list);
  154. kfree(ti);
  155. }
  156. mutex_unlock(&t->devlock);
  157. }
  158. /**
  159. * fcoe_transport_match() - Use the bus device match function to match the hw
  160. * @t: The fcoe transport to check
  161. * @netdev: The netdev to match against
  162. *
  163. * This function is used to check if the given transport wants to manage the
  164. * input netdev. if the transports implements the match function, it will be
  165. * called, o.w. we just compare the pci vendor and device id.
  166. *
  167. * Returns: true for match up
  168. */
  169. static bool fcoe_transport_match(struct fcoe_transport *t,
  170. struct net_device *netdev)
  171. {
  172. /* match transport by vendor and device id */
  173. struct pci_dev *pci;
  174. pci = fcoe_transport_pcidev(netdev);
  175. if (pci) {
  176. printk(KERN_DEBUG "fcoe_transport_match:"
  177. "%s:%x:%x -- %s:%x:%x\n",
  178. t->name, t->vendor, t->device,
  179. netdev->name, pci->vendor, pci->device);
  180. /* if transport supports match */
  181. if (t->match)
  182. return t->match(netdev);
  183. /* else just compare the vendor and device id: pci only */
  184. return (t->vendor == pci->vendor) && (t->device == pci->device);
  185. }
  186. return false;
  187. }
  188. /**
  189. * fcoe_transport_lookup() - Check if the transport is already registered
  190. * @t: the transport to be looked up
  191. *
  192. * This compares the parent device (pci) vendor and device id
  193. *
  194. * Returns: NULL if not found
  195. *
  196. * TODO: return default sw transport if no other transport is found
  197. */
  198. static struct fcoe_transport *fcoe_transport_lookup(
  199. struct net_device *netdev)
  200. {
  201. struct fcoe_transport *t;
  202. mutex_lock(&fcoe_transports_lock);
  203. list_for_each_entry(t, &fcoe_transports, list) {
  204. if (fcoe_transport_match(t, netdev)) {
  205. mutex_unlock(&fcoe_transports_lock);
  206. return t;
  207. }
  208. }
  209. mutex_unlock(&fcoe_transports_lock);
  210. printk(KERN_DEBUG "fcoe_transport_lookup:"
  211. "use default transport for %s\n", netdev->name);
  212. return fcoe_transport_default();
  213. }
  214. /**
  215. * fcoe_transport_register() - Adds a fcoe transport to the fcoe transports list
  216. * @t: ptr to the fcoe transport to be added
  217. *
  218. * Returns: 0 for success
  219. */
  220. int fcoe_transport_register(struct fcoe_transport *t)
  221. {
  222. struct fcoe_transport *tt;
  223. /* TODO - add fcoe_transport specific initialization here */
  224. mutex_lock(&fcoe_transports_lock);
  225. list_for_each_entry(tt, &fcoe_transports, list) {
  226. if (tt == t) {
  227. mutex_unlock(&fcoe_transports_lock);
  228. return -EEXIST;
  229. }
  230. }
  231. list_add_tail(&t->list, &fcoe_transports);
  232. mutex_unlock(&fcoe_transports_lock);
  233. mutex_init(&t->devlock);
  234. INIT_LIST_HEAD(&t->devlist);
  235. printk(KERN_DEBUG "fcoe_transport_register:%s\n", t->name);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(fcoe_transport_register);
  239. /**
  240. * fcoe_transport_unregister() - Remove the tranport fro the fcoe transports list
  241. * @t: ptr to the fcoe transport to be removed
  242. *
  243. * Returns: 0 for success
  244. */
  245. int fcoe_transport_unregister(struct fcoe_transport *t)
  246. {
  247. struct fcoe_transport *tt, *tmp;
  248. mutex_lock(&fcoe_transports_lock);
  249. list_for_each_entry_safe(tt, tmp, &fcoe_transports, list) {
  250. if (tt == t) {
  251. list_del(&t->list);
  252. mutex_unlock(&fcoe_transports_lock);
  253. fcoe_transport_device_remove_all(t);
  254. printk(KERN_DEBUG "fcoe_transport_unregister:%s\n",
  255. t->name);
  256. return 0;
  257. }
  258. }
  259. mutex_unlock(&fcoe_transports_lock);
  260. return -ENODEV;
  261. }
  262. EXPORT_SYMBOL_GPL(fcoe_transport_unregister);
  263. /**
  264. * fcoe_load_transport_driver() - Load an offload driver by alias name
  265. * @netdev: the target net device
  266. *
  267. * Requests for an offload driver module as the fcoe transport, if fails, it
  268. * falls back to use the SW HBA (fcoe_sw) as its transport
  269. *
  270. * TODO -
  271. * 1. supports only PCI device
  272. * 2. needs fix for VLAn and bonding
  273. * 3. pure hw fcoe hba may not have netdev
  274. *
  275. * Returns: 0 for success
  276. */
  277. int fcoe_load_transport_driver(struct net_device *netdev)
  278. {
  279. struct pci_dev *pci;
  280. struct device *dev = netdev->dev.parent;
  281. if (fcoe_transport_lookup(netdev)) {
  282. /* load default transport */
  283. printk(KERN_DEBUG "fcoe: already loaded transport for %s\n",
  284. netdev->name);
  285. return -EEXIST;
  286. }
  287. pci = to_pci_dev(dev);
  288. if (dev->bus != &pci_bus_type) {
  289. printk(KERN_DEBUG "fcoe: support noly PCI device\n");
  290. return -ENODEV;
  291. }
  292. printk(KERN_DEBUG "fcoe: loading driver fcoe-pci-0x%04x-0x%04x\n",
  293. pci->vendor, pci->device);
  294. return request_module("fcoe-pci-0x%04x-0x%04x",
  295. pci->vendor, pci->device);
  296. }
  297. EXPORT_SYMBOL_GPL(fcoe_load_transport_driver);
  298. /**
  299. * fcoe_transport_attach() - Load transport to fcoe
  300. * @netdev: the netdev the transport to be attached to
  301. *
  302. * This will look for existing offload driver, if not found, it falls back to
  303. * the default sw hba (fcoe_sw) as its fcoe transport.
  304. *
  305. * Returns: 0 for success
  306. */
  307. int fcoe_transport_attach(struct net_device *netdev)
  308. {
  309. struct fcoe_transport *t;
  310. /* find the corresponding transport */
  311. t = fcoe_transport_lookup(netdev);
  312. if (!t) {
  313. printk(KERN_DEBUG "fcoe_transport_attach"
  314. ":no transport for %s:use %s\n",
  315. netdev->name, t->name);
  316. return -ENODEV;
  317. }
  318. /* add to the transport */
  319. if (fcoe_transport_device_add(t, netdev)) {
  320. printk(KERN_DEBUG "fcoe_transport_attach"
  321. ":failed to add %s to tramsport %s\n",
  322. netdev->name, t->name);
  323. return -EIO;
  324. }
  325. /* transport create function */
  326. if (t->create)
  327. t->create(netdev);
  328. printk(KERN_DEBUG "fcoe_transport_attach:transport %s for %s\n",
  329. t->name, netdev->name);
  330. return 0;
  331. }
  332. EXPORT_SYMBOL_GPL(fcoe_transport_attach);
  333. /**
  334. * fcoe_transport_release() - Unload transport from fcoe
  335. * @netdev: the net device on which fcoe is to be released
  336. *
  337. * Returns: 0 for success
  338. */
  339. int fcoe_transport_release(struct net_device *netdev)
  340. {
  341. struct fcoe_transport *t;
  342. /* find the corresponding transport */
  343. t = fcoe_transport_lookup(netdev);
  344. if (!t) {
  345. printk(KERN_DEBUG "fcoe_transport_release:"
  346. "no transport for %s:use %s\n",
  347. netdev->name, t->name);
  348. return -ENODEV;
  349. }
  350. /* remove the device from the transport */
  351. if (fcoe_transport_device_remove(t, netdev)) {
  352. printk(KERN_DEBUG "fcoe_transport_release:"
  353. "failed to add %s to tramsport %s\n",
  354. netdev->name, t->name);
  355. return -EIO;
  356. }
  357. /* transport destroy function */
  358. if (t->destroy)
  359. t->destroy(netdev);
  360. printk(KERN_DEBUG "fcoe_transport_release:"
  361. "device %s dettached from transport %s\n",
  362. netdev->name, t->name);
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(fcoe_transport_release);
  366. /**
  367. * fcoe_transport_init() - Initializes fcoe transport layer
  368. *
  369. * This prepares for the fcoe transport layer
  370. *
  371. * Returns: none
  372. */
  373. int __init fcoe_transport_init(void)
  374. {
  375. INIT_LIST_HEAD(&fcoe_transports);
  376. mutex_init(&fcoe_transports_lock);
  377. return 0;
  378. }
  379. /**
  380. * fcoe_transport_exit() - Cleans up the fcoe transport layer
  381. *
  382. * This cleans up the fcoe transport layer. removing any transport on the list,
  383. * note that the transport destroy func is not called here.
  384. *
  385. * Returns: none
  386. */
  387. int __exit fcoe_transport_exit(void)
  388. {
  389. struct fcoe_transport *t, *tmp;
  390. mutex_lock(&fcoe_transports_lock);
  391. list_for_each_entry_safe(t, tmp, &fcoe_transports, list) {
  392. list_del(&t->list);
  393. mutex_unlock(&fcoe_transports_lock);
  394. fcoe_transport_device_remove_all(t);
  395. mutex_lock(&fcoe_transports_lock);
  396. }
  397. mutex_unlock(&fcoe_transports_lock);
  398. return 0;
  399. }