libcxgbi.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590
  1. /*
  2. * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
  3. *
  4. * Copyright (c) 2010 Chelsio Communications, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation.
  9. *
  10. * Written by: Karen Xie (kxie@chelsio.com)
  11. * Written by: Rakesh Ranjan (rranjan@chelsio.com)
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  14. #include <linux/skbuff.h>
  15. #include <linux/crypto.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/pci.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_host.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/inet.h>
  23. #include <net/dst.h>
  24. #include <net/route.h>
  25. #include <linux/inetdevice.h> /* ip_dev_find */
  26. #include <net/tcp.h>
  27. static unsigned int dbg_level;
  28. #include "libcxgbi.h"
  29. #define DRV_MODULE_NAME "libcxgbi"
  30. #define DRV_MODULE_DESC "Chelsio iSCSI driver library"
  31. #define DRV_MODULE_VERSION "0.9.0"
  32. #define DRV_MODULE_RELDATE "Jun. 2010"
  33. MODULE_AUTHOR("Chelsio Communications, Inc.");
  34. MODULE_DESCRIPTION(DRV_MODULE_DESC);
  35. MODULE_VERSION(DRV_MODULE_VERSION);
  36. MODULE_LICENSE("GPL");
  37. module_param(dbg_level, uint, 0644);
  38. MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
  39. /*
  40. * cxgbi device management
  41. * maintains a list of the cxgbi devices
  42. */
  43. static LIST_HEAD(cdev_list);
  44. static DEFINE_MUTEX(cdev_mutex);
  45. int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
  46. unsigned int max_conn)
  47. {
  48. struct cxgbi_ports_map *pmap = &cdev->pmap;
  49. pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
  50. sizeof(struct cxgbi_sock *),
  51. GFP_KERNEL);
  52. if (!pmap->port_csk) {
  53. pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
  54. return -ENOMEM;
  55. }
  56. pmap->max_connect = max_conn;
  57. pmap->sport_base = base;
  58. spin_lock_init(&pmap->lock);
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
  62. void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
  63. {
  64. struct cxgbi_ports_map *pmap = &cdev->pmap;
  65. struct cxgbi_sock *csk;
  66. int i;
  67. for (i = 0; i < pmap->max_connect; i++) {
  68. if (pmap->port_csk[i]) {
  69. csk = pmap->port_csk[i];
  70. pmap->port_csk[i] = NULL;
  71. log_debug(1 << CXGBI_DBG_SOCK,
  72. "csk 0x%p, cdev 0x%p, offload down.\n",
  73. csk, cdev);
  74. spin_lock_bh(&csk->lock);
  75. cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
  76. cxgbi_sock_closed(csk);
  77. spin_unlock_bh(&csk->lock);
  78. cxgbi_sock_put(csk);
  79. }
  80. }
  81. }
  82. EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
  83. static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
  84. {
  85. log_debug(1 << CXGBI_DBG_DEV,
  86. "cdev 0x%p, p# %u.\n", cdev, cdev->nports);
  87. cxgbi_hbas_remove(cdev);
  88. cxgbi_device_portmap_cleanup(cdev);
  89. if (cdev->dev_ddp_cleanup)
  90. cdev->dev_ddp_cleanup(cdev);
  91. else
  92. cxgbi_ddp_cleanup(cdev);
  93. if (cdev->ddp)
  94. cxgbi_ddp_cleanup(cdev);
  95. if (cdev->pmap.max_connect)
  96. cxgbi_free_big_mem(cdev->pmap.port_csk);
  97. kfree(cdev);
  98. }
  99. struct cxgbi_device *cxgbi_device_register(unsigned int extra,
  100. unsigned int nports)
  101. {
  102. struct cxgbi_device *cdev;
  103. cdev = kzalloc(sizeof(*cdev) + extra + nports *
  104. (sizeof(struct cxgbi_hba *) +
  105. sizeof(struct net_device *)),
  106. GFP_KERNEL);
  107. if (!cdev) {
  108. pr_warn("nport %d, OOM.\n", nports);
  109. return NULL;
  110. }
  111. cdev->ports = (struct net_device **)(cdev + 1);
  112. cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
  113. sizeof(struct net_device *));
  114. if (extra)
  115. cdev->dd_data = ((char *)cdev->hbas) +
  116. nports * sizeof(struct cxgbi_hba *);
  117. spin_lock_init(&cdev->pmap.lock);
  118. mutex_lock(&cdev_mutex);
  119. list_add_tail(&cdev->list_head, &cdev_list);
  120. mutex_unlock(&cdev_mutex);
  121. log_debug(1 << CXGBI_DBG_DEV,
  122. "cdev 0x%p, p# %u.\n", cdev, nports);
  123. return cdev;
  124. }
  125. EXPORT_SYMBOL_GPL(cxgbi_device_register);
  126. void cxgbi_device_unregister(struct cxgbi_device *cdev)
  127. {
  128. log_debug(1 << CXGBI_DBG_DEV,
  129. "cdev 0x%p, p# %u,%s.\n",
  130. cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
  131. mutex_lock(&cdev_mutex);
  132. list_del(&cdev->list_head);
  133. mutex_unlock(&cdev_mutex);
  134. cxgbi_device_destroy(cdev);
  135. }
  136. EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
  137. void cxgbi_device_unregister_all(unsigned int flag)
  138. {
  139. struct cxgbi_device *cdev, *tmp;
  140. mutex_lock(&cdev_mutex);
  141. list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
  142. if ((cdev->flags & flag) == flag) {
  143. log_debug(1 << CXGBI_DBG_DEV,
  144. "cdev 0x%p, p# %u,%s.\n",
  145. cdev, cdev->nports, cdev->nports ?
  146. cdev->ports[0]->name : "");
  147. list_del(&cdev->list_head);
  148. cxgbi_device_destroy(cdev);
  149. }
  150. }
  151. mutex_unlock(&cdev_mutex);
  152. }
  153. EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
  154. struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
  155. {
  156. struct cxgbi_device *cdev, *tmp;
  157. mutex_lock(&cdev_mutex);
  158. list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
  159. if (cdev->lldev == lldev) {
  160. mutex_unlock(&cdev_mutex);
  161. return cdev;
  162. }
  163. }
  164. mutex_unlock(&cdev_mutex);
  165. log_debug(1 << CXGBI_DBG_DEV,
  166. "lldev 0x%p, NO match found.\n", lldev);
  167. return NULL;
  168. }
  169. EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
  170. static struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
  171. int *port)
  172. {
  173. struct net_device *vdev = NULL;
  174. struct cxgbi_device *cdev, *tmp;
  175. int i;
  176. if (ndev->priv_flags & IFF_802_1Q_VLAN) {
  177. vdev = ndev;
  178. ndev = vlan_dev_real_dev(ndev);
  179. log_debug(1 << CXGBI_DBG_DEV,
  180. "vlan dev %s -> %s.\n", vdev->name, ndev->name);
  181. }
  182. mutex_lock(&cdev_mutex);
  183. list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
  184. for (i = 0; i < cdev->nports; i++) {
  185. if (ndev == cdev->ports[i]) {
  186. cdev->hbas[i]->vdev = vdev;
  187. mutex_unlock(&cdev_mutex);
  188. if (port)
  189. *port = i;
  190. return cdev;
  191. }
  192. }
  193. }
  194. mutex_unlock(&cdev_mutex);
  195. log_debug(1 << CXGBI_DBG_DEV,
  196. "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
  197. return NULL;
  198. }
  199. void cxgbi_hbas_remove(struct cxgbi_device *cdev)
  200. {
  201. int i;
  202. struct cxgbi_hba *chba;
  203. log_debug(1 << CXGBI_DBG_DEV,
  204. "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
  205. for (i = 0; i < cdev->nports; i++) {
  206. chba = cdev->hbas[i];
  207. if (chba) {
  208. cdev->hbas[i] = NULL;
  209. iscsi_host_remove(chba->shost);
  210. pci_dev_put(cdev->pdev);
  211. iscsi_host_free(chba->shost);
  212. }
  213. }
  214. }
  215. EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
  216. int cxgbi_hbas_add(struct cxgbi_device *cdev, unsigned int max_lun,
  217. unsigned int max_id, struct scsi_host_template *sht,
  218. struct scsi_transport_template *stt)
  219. {
  220. struct cxgbi_hba *chba;
  221. struct Scsi_Host *shost;
  222. int i, err;
  223. log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
  224. for (i = 0; i < cdev->nports; i++) {
  225. shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
  226. if (!shost) {
  227. pr_info("0x%p, p%d, %s, host alloc failed.\n",
  228. cdev, i, cdev->ports[i]->name);
  229. err = -ENOMEM;
  230. goto err_out;
  231. }
  232. shost->transportt = stt;
  233. shost->max_lun = max_lun;
  234. shost->max_id = max_id;
  235. shost->max_channel = 0;
  236. shost->max_cmd_len = 16;
  237. chba = iscsi_host_priv(shost);
  238. chba->cdev = cdev;
  239. chba->ndev = cdev->ports[i];
  240. chba->shost = shost;
  241. log_debug(1 << CXGBI_DBG_DEV,
  242. "cdev 0x%p, p#%d %s: chba 0x%p.\n",
  243. cdev, i, cdev->ports[i]->name, chba);
  244. pci_dev_get(cdev->pdev);
  245. err = iscsi_host_add(shost, &cdev->pdev->dev);
  246. if (err) {
  247. pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
  248. cdev, i, cdev->ports[i]->name);
  249. pci_dev_put(cdev->pdev);
  250. scsi_host_put(shost);
  251. goto err_out;
  252. }
  253. cdev->hbas[i] = chba;
  254. }
  255. return 0;
  256. err_out:
  257. cxgbi_hbas_remove(cdev);
  258. return err;
  259. }
  260. EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
  261. /*
  262. * iSCSI offload
  263. *
  264. * - source port management
  265. * To find a free source port in the port allocation map we use a very simple
  266. * rotor scheme to look for the next free port.
  267. *
  268. * If a source port has been specified make sure that it doesn't collide with
  269. * our normal source port allocation map. If it's outside the range of our
  270. * allocation/deallocation scheme just let them use it.
  271. *
  272. * If the source port is outside our allocation range, the caller is
  273. * responsible for keeping track of their port usage.
  274. */
  275. static int sock_get_port(struct cxgbi_sock *csk)
  276. {
  277. struct cxgbi_device *cdev = csk->cdev;
  278. struct cxgbi_ports_map *pmap = &cdev->pmap;
  279. unsigned int start;
  280. int idx;
  281. if (!pmap->max_connect) {
  282. pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
  283. cdev, csk->port_id, cdev->ports[csk->port_id]->name);
  284. return -EADDRNOTAVAIL;
  285. }
  286. if (csk->saddr.sin_port) {
  287. pr_err("source port NON-ZERO %u.\n",
  288. ntohs(csk->saddr.sin_port));
  289. return -EADDRINUSE;
  290. }
  291. spin_lock_bh(&pmap->lock);
  292. if (pmap->used >= pmap->max_connect) {
  293. spin_unlock_bh(&pmap->lock);
  294. pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
  295. cdev, csk->port_id, cdev->ports[csk->port_id]->name);
  296. return -EADDRNOTAVAIL;
  297. }
  298. start = idx = pmap->next;
  299. do {
  300. if (++idx >= pmap->max_connect)
  301. idx = 0;
  302. if (!pmap->port_csk[idx]) {
  303. pmap->used++;
  304. csk->saddr.sin_port =
  305. htons(pmap->sport_base + idx);
  306. pmap->next = idx;
  307. pmap->port_csk[idx] = csk;
  308. spin_unlock_bh(&pmap->lock);
  309. cxgbi_sock_get(csk);
  310. log_debug(1 << CXGBI_DBG_SOCK,
  311. "cdev 0x%p, p#%u %s, p %u, %u.\n",
  312. cdev, csk->port_id,
  313. cdev->ports[csk->port_id]->name,
  314. pmap->sport_base + idx, pmap->next);
  315. return 0;
  316. }
  317. } while (idx != start);
  318. spin_unlock_bh(&pmap->lock);
  319. /* should not happen */
  320. pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
  321. cdev, csk->port_id, cdev->ports[csk->port_id]->name,
  322. pmap->next);
  323. return -EADDRNOTAVAIL;
  324. }
  325. static void sock_put_port(struct cxgbi_sock *csk)
  326. {
  327. struct cxgbi_device *cdev = csk->cdev;
  328. struct cxgbi_ports_map *pmap = &cdev->pmap;
  329. if (csk->saddr.sin_port) {
  330. int idx = ntohs(csk->saddr.sin_port) - pmap->sport_base;
  331. csk->saddr.sin_port = 0;
  332. if (idx < 0 || idx >= pmap->max_connect) {
  333. pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
  334. cdev, csk->port_id,
  335. cdev->ports[csk->port_id]->name,
  336. ntohs(csk->saddr.sin_port));
  337. return;
  338. }
  339. spin_lock_bh(&pmap->lock);
  340. pmap->port_csk[idx] = NULL;
  341. pmap->used--;
  342. spin_unlock_bh(&pmap->lock);
  343. log_debug(1 << CXGBI_DBG_SOCK,
  344. "cdev 0x%p, p#%u %s, release %u.\n",
  345. cdev, csk->port_id, cdev->ports[csk->port_id]->name,
  346. pmap->sport_base + idx);
  347. cxgbi_sock_put(csk);
  348. }
  349. }
  350. /*
  351. * iscsi tcp connection
  352. */
  353. void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
  354. {
  355. if (csk->cpl_close) {
  356. kfree_skb(csk->cpl_close);
  357. csk->cpl_close = NULL;
  358. }
  359. if (csk->cpl_abort_req) {
  360. kfree_skb(csk->cpl_abort_req);
  361. csk->cpl_abort_req = NULL;
  362. }
  363. if (csk->cpl_abort_rpl) {
  364. kfree_skb(csk->cpl_abort_rpl);
  365. csk->cpl_abort_rpl = NULL;
  366. }
  367. }
  368. EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
  369. static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
  370. {
  371. struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
  372. if (!csk) {
  373. pr_info("alloc csk %zu failed.\n", sizeof(*csk));
  374. return NULL;
  375. }
  376. if (cdev->csk_alloc_cpls(csk) < 0) {
  377. pr_info("csk 0x%p, alloc cpls failed.\n", csk);
  378. kfree(csk);
  379. return NULL;
  380. }
  381. spin_lock_init(&csk->lock);
  382. kref_init(&csk->refcnt);
  383. skb_queue_head_init(&csk->receive_queue);
  384. skb_queue_head_init(&csk->write_queue);
  385. setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
  386. rwlock_init(&csk->callback_lock);
  387. csk->cdev = cdev;
  388. csk->flags = 0;
  389. cxgbi_sock_set_state(csk, CTP_CLOSED);
  390. log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
  391. return csk;
  392. }
  393. static struct rtable *find_route_ipv4(struct flowi4 *fl4,
  394. __be32 saddr, __be32 daddr,
  395. __be16 sport, __be16 dport, u8 tos)
  396. {
  397. struct rtable *rt;
  398. rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
  399. dport, sport, IPPROTO_TCP, tos, 0);
  400. if (IS_ERR(rt))
  401. return NULL;
  402. return rt;
  403. }
  404. static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
  405. {
  406. struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
  407. struct dst_entry *dst;
  408. struct net_device *ndev;
  409. struct cxgbi_device *cdev;
  410. struct rtable *rt = NULL;
  411. struct flowi4 fl4;
  412. struct cxgbi_sock *csk = NULL;
  413. unsigned int mtu = 0;
  414. int port = 0xFFFF;
  415. int err = 0;
  416. if (daddr->sin_family != AF_INET) {
  417. pr_info("address family 0x%x NOT supported.\n",
  418. daddr->sin_family);
  419. err = -EAFNOSUPPORT;
  420. goto err_out;
  421. }
  422. rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
  423. if (!rt) {
  424. pr_info("no route to ipv4 0x%x, port %u.\n",
  425. daddr->sin_addr.s_addr, daddr->sin_port);
  426. err = -ENETUNREACH;
  427. goto err_out;
  428. }
  429. dst = &rt->dst;
  430. ndev = dst_get_neighbour(dst)->dev;
  431. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  432. pr_info("multi-cast route %pI4, port %u, dev %s.\n",
  433. &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
  434. ndev->name);
  435. err = -ENETUNREACH;
  436. goto rel_rt;
  437. }
  438. if (ndev->flags & IFF_LOOPBACK) {
  439. ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
  440. mtu = ndev->mtu;
  441. pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
  442. dst_get_neighbour(dst)->dev->name, ndev->name, mtu);
  443. }
  444. cdev = cxgbi_device_find_by_netdev(ndev, &port);
  445. if (!cdev) {
  446. pr_info("dst %pI4, %s, NOT cxgbi device.\n",
  447. &daddr->sin_addr.s_addr, ndev->name);
  448. err = -ENETUNREACH;
  449. goto rel_rt;
  450. }
  451. log_debug(1 << CXGBI_DBG_SOCK,
  452. "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
  453. &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
  454. port, ndev->name, cdev);
  455. csk = cxgbi_sock_create(cdev);
  456. if (!csk) {
  457. err = -ENOMEM;
  458. goto rel_rt;
  459. }
  460. csk->cdev = cdev;
  461. csk->port_id = port;
  462. csk->mtu = mtu;
  463. csk->dst = dst;
  464. csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
  465. csk->daddr.sin_port = daddr->sin_port;
  466. csk->daddr.sin_family = daddr->sin_family;
  467. csk->saddr.sin_addr.s_addr = fl4.saddr;
  468. return csk;
  469. rel_rt:
  470. ip_rt_put(rt);
  471. if (csk)
  472. cxgbi_sock_closed(csk);
  473. err_out:
  474. return ERR_PTR(err);
  475. }
  476. void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
  477. unsigned int opt)
  478. {
  479. csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
  480. dst_confirm(csk->dst);
  481. smp_mb();
  482. cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
  483. }
  484. EXPORT_SYMBOL_GPL(cxgbi_sock_established);
  485. static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
  486. {
  487. log_debug(1 << CXGBI_DBG_SOCK,
  488. "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
  489. csk, csk->state, csk->flags, csk->user_data);
  490. if (csk->state != CTP_ESTABLISHED) {
  491. read_lock_bh(&csk->callback_lock);
  492. if (csk->user_data)
  493. iscsi_conn_failure(csk->user_data,
  494. ISCSI_ERR_CONN_FAILED);
  495. read_unlock_bh(&csk->callback_lock);
  496. }
  497. }
  498. void cxgbi_sock_closed(struct cxgbi_sock *csk)
  499. {
  500. log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
  501. csk, (csk)->state, (csk)->flags, (csk)->tid);
  502. cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
  503. if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
  504. return;
  505. if (csk->saddr.sin_port)
  506. sock_put_port(csk);
  507. if (csk->dst)
  508. dst_release(csk->dst);
  509. csk->cdev->csk_release_offload_resources(csk);
  510. cxgbi_sock_set_state(csk, CTP_CLOSED);
  511. cxgbi_inform_iscsi_conn_closing(csk);
  512. cxgbi_sock_put(csk);
  513. }
  514. EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
  515. static void need_active_close(struct cxgbi_sock *csk)
  516. {
  517. int data_lost;
  518. int close_req = 0;
  519. log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
  520. csk, (csk)->state, (csk)->flags, (csk)->tid);
  521. spin_lock_bh(&csk->lock);
  522. dst_confirm(csk->dst);
  523. data_lost = skb_queue_len(&csk->receive_queue);
  524. __skb_queue_purge(&csk->receive_queue);
  525. if (csk->state == CTP_ACTIVE_OPEN)
  526. cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
  527. else if (csk->state == CTP_ESTABLISHED) {
  528. close_req = 1;
  529. cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
  530. } else if (csk->state == CTP_PASSIVE_CLOSE) {
  531. close_req = 1;
  532. cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
  533. }
  534. if (close_req) {
  535. if (data_lost)
  536. csk->cdev->csk_send_abort_req(csk);
  537. else
  538. csk->cdev->csk_send_close_req(csk);
  539. }
  540. spin_unlock_bh(&csk->lock);
  541. }
  542. void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
  543. {
  544. pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
  545. csk, csk->state, csk->flags,
  546. &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
  547. &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
  548. errno);
  549. cxgbi_sock_set_state(csk, CTP_CONNECTING);
  550. csk->err = errno;
  551. cxgbi_sock_closed(csk);
  552. }
  553. EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
  554. void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
  555. {
  556. struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
  557. log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
  558. csk, (csk)->state, (csk)->flags, (csk)->tid);
  559. cxgbi_sock_get(csk);
  560. spin_lock_bh(&csk->lock);
  561. if (csk->state == CTP_ACTIVE_OPEN)
  562. cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
  563. spin_unlock_bh(&csk->lock);
  564. cxgbi_sock_put(csk);
  565. __kfree_skb(skb);
  566. }
  567. EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
  568. void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
  569. {
  570. cxgbi_sock_get(csk);
  571. spin_lock_bh(&csk->lock);
  572. if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
  573. if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_RCVD))
  574. cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
  575. else {
  576. cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_RCVD);
  577. cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
  578. if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
  579. pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
  580. csk, csk->state, csk->flags, csk->tid);
  581. cxgbi_sock_closed(csk);
  582. }
  583. }
  584. spin_unlock_bh(&csk->lock);
  585. cxgbi_sock_put(csk);
  586. }
  587. EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
  588. void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
  589. {
  590. log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
  591. csk, (csk)->state, (csk)->flags, (csk)->tid);
  592. cxgbi_sock_get(csk);
  593. spin_lock_bh(&csk->lock);
  594. if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
  595. goto done;
  596. switch (csk->state) {
  597. case CTP_ESTABLISHED:
  598. cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
  599. break;
  600. case CTP_ACTIVE_CLOSE:
  601. cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
  602. break;
  603. case CTP_CLOSE_WAIT_1:
  604. cxgbi_sock_closed(csk);
  605. break;
  606. case CTP_ABORTING:
  607. break;
  608. default:
  609. pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
  610. csk, csk->state, csk->flags, csk->tid);
  611. }
  612. cxgbi_inform_iscsi_conn_closing(csk);
  613. done:
  614. spin_unlock_bh(&csk->lock);
  615. cxgbi_sock_put(csk);
  616. }
  617. EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
  618. void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
  619. {
  620. log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
  621. csk, (csk)->state, (csk)->flags, (csk)->tid);
  622. cxgbi_sock_get(csk);
  623. spin_lock_bh(&csk->lock);
  624. csk->snd_una = snd_nxt - 1;
  625. if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
  626. goto done;
  627. switch (csk->state) {
  628. case CTP_ACTIVE_CLOSE:
  629. cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
  630. break;
  631. case CTP_CLOSE_WAIT_1:
  632. case CTP_CLOSE_WAIT_2:
  633. cxgbi_sock_closed(csk);
  634. break;
  635. case CTP_ABORTING:
  636. break;
  637. default:
  638. pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
  639. csk, csk->state, csk->flags, csk->tid);
  640. }
  641. done:
  642. spin_unlock_bh(&csk->lock);
  643. cxgbi_sock_put(csk);
  644. }
  645. EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
  646. void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
  647. unsigned int snd_una, int seq_chk)
  648. {
  649. log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
  650. "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
  651. csk, csk->state, csk->flags, csk->tid, credits,
  652. csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
  653. spin_lock_bh(&csk->lock);
  654. csk->wr_cred += credits;
  655. if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
  656. csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
  657. while (credits) {
  658. struct sk_buff *p = cxgbi_sock_peek_wr(csk);
  659. if (unlikely(!p)) {
  660. pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
  661. csk, csk->state, csk->flags, csk->tid, credits,
  662. csk->wr_cred, csk->wr_una_cred);
  663. break;
  664. }
  665. if (unlikely(credits < p->csum)) {
  666. pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
  667. csk, csk->state, csk->flags, csk->tid,
  668. credits, csk->wr_cred, csk->wr_una_cred,
  669. p->csum);
  670. p->csum -= credits;
  671. break;
  672. } else {
  673. cxgbi_sock_dequeue_wr(csk);
  674. credits -= p->csum;
  675. kfree_skb(p);
  676. }
  677. }
  678. cxgbi_sock_check_wr_invariants(csk);
  679. if (seq_chk) {
  680. if (unlikely(before(snd_una, csk->snd_una))) {
  681. pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
  682. csk, csk->state, csk->flags, csk->tid, snd_una,
  683. csk->snd_una);
  684. goto done;
  685. }
  686. if (csk->snd_una != snd_una) {
  687. csk->snd_una = snd_una;
  688. dst_confirm(csk->dst);
  689. }
  690. }
  691. if (skb_queue_len(&csk->write_queue)) {
  692. if (csk->cdev->csk_push_tx_frames(csk, 0))
  693. cxgbi_conn_tx_open(csk);
  694. } else
  695. cxgbi_conn_tx_open(csk);
  696. done:
  697. spin_unlock_bh(&csk->lock);
  698. }
  699. EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
  700. static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
  701. unsigned short mtu)
  702. {
  703. int i = 0;
  704. while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
  705. ++i;
  706. return i;
  707. }
  708. unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
  709. {
  710. unsigned int idx;
  711. struct dst_entry *dst = csk->dst;
  712. csk->advmss = dst_metric_advmss(dst);
  713. if (csk->advmss > pmtu - 40)
  714. csk->advmss = pmtu - 40;
  715. if (csk->advmss < csk->cdev->mtus[0] - 40)
  716. csk->advmss = csk->cdev->mtus[0] - 40;
  717. idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
  718. return idx;
  719. }
  720. EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
  721. void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
  722. {
  723. cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
  724. __skb_queue_tail(&csk->write_queue, skb);
  725. }
  726. EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
  727. void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
  728. {
  729. struct sk_buff *skb;
  730. while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
  731. kfree_skb(skb);
  732. }
  733. EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
  734. void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
  735. {
  736. int pending = cxgbi_sock_count_pending_wrs(csk);
  737. if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
  738. pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
  739. csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
  740. }
  741. EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
  742. static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
  743. {
  744. struct cxgbi_device *cdev = csk->cdev;
  745. struct sk_buff *next;
  746. int err, copied = 0;
  747. spin_lock_bh(&csk->lock);
  748. if (csk->state != CTP_ESTABLISHED) {
  749. log_debug(1 << CXGBI_DBG_PDU_TX,
  750. "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
  751. csk, csk->state, csk->flags, csk->tid);
  752. err = -EAGAIN;
  753. goto out_err;
  754. }
  755. if (csk->err) {
  756. log_debug(1 << CXGBI_DBG_PDU_TX,
  757. "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
  758. csk, csk->state, csk->flags, csk->tid, csk->err);
  759. err = -EPIPE;
  760. goto out_err;
  761. }
  762. if (csk->write_seq - csk->snd_una >= cdev->snd_win) {
  763. log_debug(1 << CXGBI_DBG_PDU_TX,
  764. "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
  765. csk, csk->state, csk->flags, csk->tid, csk->write_seq,
  766. csk->snd_una, cdev->snd_win);
  767. err = -ENOBUFS;
  768. goto out_err;
  769. }
  770. while (skb) {
  771. int frags = skb_shinfo(skb)->nr_frags +
  772. (skb->len != skb->data_len);
  773. if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
  774. pr_err("csk 0x%p, skb head %u < %u.\n",
  775. csk, skb_headroom(skb), cdev->skb_tx_rsvd);
  776. err = -EINVAL;
  777. goto out_err;
  778. }
  779. if (frags >= SKB_WR_LIST_SIZE) {
  780. pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
  781. csk, skb_shinfo(skb)->nr_frags, skb->len,
  782. skb->data_len, (uint)(SKB_WR_LIST_SIZE));
  783. err = -EINVAL;
  784. goto out_err;
  785. }
  786. next = skb->next;
  787. skb->next = NULL;
  788. cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
  789. cxgbi_sock_skb_entail(csk, skb);
  790. copied += skb->len;
  791. csk->write_seq += skb->len +
  792. cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
  793. skb = next;
  794. }
  795. done:
  796. if (likely(skb_queue_len(&csk->write_queue)))
  797. cdev->csk_push_tx_frames(csk, 1);
  798. spin_unlock_bh(&csk->lock);
  799. return copied;
  800. out_err:
  801. if (copied == 0 && err == -EPIPE)
  802. copied = csk->err ? csk->err : -EPIPE;
  803. else
  804. copied = err;
  805. goto done;
  806. }
  807. /*
  808. * Direct Data Placement -
  809. * Directly place the iSCSI Data-In or Data-Out PDU's payload into pre-posted
  810. * final destination host-memory buffers based on the Initiator Task Tag (ITT)
  811. * in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
  812. * The host memory address is programmed into h/w in the format of pagepod
  813. * entries.
  814. * The location of the pagepod entry is encoded into ddp tag which is used as
  815. * the base for ITT/TTT.
  816. */
  817. static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
  818. static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
  819. static unsigned char page_idx = DDP_PGIDX_MAX;
  820. static unsigned char sw_tag_idx_bits;
  821. static unsigned char sw_tag_age_bits;
  822. /*
  823. * Direct-Data Placement page size adjustment
  824. */
  825. static int ddp_adjust_page_table(void)
  826. {
  827. int i;
  828. unsigned int base_order, order;
  829. if (PAGE_SIZE < (1UL << ddp_page_shift[0])) {
  830. pr_info("PAGE_SIZE 0x%lx too small, min 0x%lx\n",
  831. PAGE_SIZE, 1UL << ddp_page_shift[0]);
  832. return -EINVAL;
  833. }
  834. base_order = get_order(1UL << ddp_page_shift[0]);
  835. order = get_order(1UL << PAGE_SHIFT);
  836. for (i = 0; i < DDP_PGIDX_MAX; i++) {
  837. /* first is the kernel page size, then just doubling */
  838. ddp_page_order[i] = order - base_order + i;
  839. ddp_page_shift[i] = PAGE_SHIFT + i;
  840. }
  841. return 0;
  842. }
  843. static int ddp_find_page_index(unsigned long pgsz)
  844. {
  845. int i;
  846. for (i = 0; i < DDP_PGIDX_MAX; i++) {
  847. if (pgsz == (1UL << ddp_page_shift[i]))
  848. return i;
  849. }
  850. pr_info("ddp page size %lu not supported.\n", pgsz);
  851. return DDP_PGIDX_MAX;
  852. }
  853. static void ddp_setup_host_page_size(void)
  854. {
  855. if (page_idx == DDP_PGIDX_MAX) {
  856. page_idx = ddp_find_page_index(PAGE_SIZE);
  857. if (page_idx == DDP_PGIDX_MAX) {
  858. pr_info("system PAGE %lu, update hw.\n", PAGE_SIZE);
  859. if (ddp_adjust_page_table() < 0) {
  860. pr_info("PAGE %lu, disable ddp.\n", PAGE_SIZE);
  861. return;
  862. }
  863. page_idx = ddp_find_page_index(PAGE_SIZE);
  864. }
  865. pr_info("system PAGE %lu, ddp idx %u.\n", PAGE_SIZE, page_idx);
  866. }
  867. }
  868. void cxgbi_ddp_page_size_factor(int *pgsz_factor)
  869. {
  870. int i;
  871. for (i = 0; i < DDP_PGIDX_MAX; i++)
  872. pgsz_factor[i] = ddp_page_order[i];
  873. }
  874. EXPORT_SYMBOL_GPL(cxgbi_ddp_page_size_factor);
  875. /*
  876. * DDP setup & teardown
  877. */
  878. void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *ppod,
  879. struct cxgbi_pagepod_hdr *hdr,
  880. struct cxgbi_gather_list *gl, unsigned int gidx)
  881. {
  882. int i;
  883. memcpy(ppod, hdr, sizeof(*hdr));
  884. for (i = 0; i < (PPOD_PAGES_MAX + 1); i++, gidx++) {
  885. ppod->addr[i] = gidx < gl->nelem ?
  886. cpu_to_be64(gl->phys_addr[gidx]) : 0ULL;
  887. }
  888. }
  889. EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_set);
  890. void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *ppod)
  891. {
  892. memset(ppod, 0, sizeof(*ppod));
  893. }
  894. EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_clear);
  895. static inline int ddp_find_unused_entries(struct cxgbi_ddp_info *ddp,
  896. unsigned int start, unsigned int max,
  897. unsigned int count,
  898. struct cxgbi_gather_list *gl)
  899. {
  900. unsigned int i, j, k;
  901. /* not enough entries */
  902. if ((max - start) < count) {
  903. log_debug(1 << CXGBI_DBG_DDP,
  904. "NOT enough entries %u+%u < %u.\n", start, count, max);
  905. return -EBUSY;
  906. }
  907. max -= count;
  908. spin_lock(&ddp->map_lock);
  909. for (i = start; i < max;) {
  910. for (j = 0, k = i; j < count; j++, k++) {
  911. if (ddp->gl_map[k])
  912. break;
  913. }
  914. if (j == count) {
  915. for (j = 0, k = i; j < count; j++, k++)
  916. ddp->gl_map[k] = gl;
  917. spin_unlock(&ddp->map_lock);
  918. return i;
  919. }
  920. i += j + 1;
  921. }
  922. spin_unlock(&ddp->map_lock);
  923. log_debug(1 << CXGBI_DBG_DDP,
  924. "NO suitable entries %u available.\n", count);
  925. return -EBUSY;
  926. }
  927. static inline void ddp_unmark_entries(struct cxgbi_ddp_info *ddp,
  928. int start, int count)
  929. {
  930. spin_lock(&ddp->map_lock);
  931. memset(&ddp->gl_map[start], 0,
  932. count * sizeof(struct cxgbi_gather_list *));
  933. spin_unlock(&ddp->map_lock);
  934. }
  935. static inline void ddp_gl_unmap(struct pci_dev *pdev,
  936. struct cxgbi_gather_list *gl)
  937. {
  938. int i;
  939. for (i = 0; i < gl->nelem; i++)
  940. dma_unmap_page(&pdev->dev, gl->phys_addr[i], PAGE_SIZE,
  941. PCI_DMA_FROMDEVICE);
  942. }
  943. static inline int ddp_gl_map(struct pci_dev *pdev,
  944. struct cxgbi_gather_list *gl)
  945. {
  946. int i;
  947. for (i = 0; i < gl->nelem; i++) {
  948. gl->phys_addr[i] = dma_map_page(&pdev->dev, gl->pages[i], 0,
  949. PAGE_SIZE,
  950. PCI_DMA_FROMDEVICE);
  951. if (unlikely(dma_mapping_error(&pdev->dev, gl->phys_addr[i]))) {
  952. log_debug(1 << CXGBI_DBG_DDP,
  953. "page %d 0x%p, 0x%p dma mapping err.\n",
  954. i, gl->pages[i], pdev);
  955. goto unmap;
  956. }
  957. }
  958. return i;
  959. unmap:
  960. if (i) {
  961. unsigned int nelem = gl->nelem;
  962. gl->nelem = i;
  963. ddp_gl_unmap(pdev, gl);
  964. gl->nelem = nelem;
  965. }
  966. return -EINVAL;
  967. }
  968. static void ddp_release_gl(struct cxgbi_gather_list *gl,
  969. struct pci_dev *pdev)
  970. {
  971. ddp_gl_unmap(pdev, gl);
  972. kfree(gl);
  973. }
  974. static struct cxgbi_gather_list *ddp_make_gl(unsigned int xferlen,
  975. struct scatterlist *sgl,
  976. unsigned int sgcnt,
  977. struct pci_dev *pdev,
  978. gfp_t gfp)
  979. {
  980. struct cxgbi_gather_list *gl;
  981. struct scatterlist *sg = sgl;
  982. struct page *sgpage = sg_page(sg);
  983. unsigned int sglen = sg->length;
  984. unsigned int sgoffset = sg->offset;
  985. unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
  986. PAGE_SHIFT;
  987. int i = 1, j = 0;
  988. if (xferlen < DDP_THRESHOLD) {
  989. log_debug(1 << CXGBI_DBG_DDP,
  990. "xfer %u < threshold %u, no ddp.\n",
  991. xferlen, DDP_THRESHOLD);
  992. return NULL;
  993. }
  994. gl = kzalloc(sizeof(struct cxgbi_gather_list) +
  995. npages * (sizeof(dma_addr_t) +
  996. sizeof(struct page *)), gfp);
  997. if (!gl) {
  998. log_debug(1 << CXGBI_DBG_DDP,
  999. "xfer %u, %u pages, OOM.\n", xferlen, npages);
  1000. return NULL;
  1001. }
  1002. log_debug(1 << CXGBI_DBG_DDP,
  1003. "xfer %u, sgl %u, gl max %u.\n", xferlen, sgcnt, npages);
  1004. gl->pages = (struct page **)&gl->phys_addr[npages];
  1005. gl->nelem = npages;
  1006. gl->length = xferlen;
  1007. gl->offset = sgoffset;
  1008. gl->pages[0] = sgpage;
  1009. for (i = 1, sg = sg_next(sgl), j = 0; i < sgcnt;
  1010. i++, sg = sg_next(sg)) {
  1011. struct page *page = sg_page(sg);
  1012. if (sgpage == page && sg->offset == sgoffset + sglen)
  1013. sglen += sg->length;
  1014. else {
  1015. /* make sure the sgl is fit for ddp:
  1016. * each has the same page size, and
  1017. * all of the middle pages are used completely
  1018. */
  1019. if ((j && sgoffset) || ((i != sgcnt - 1) &&
  1020. ((sglen + sgoffset) & ~PAGE_MASK))) {
  1021. log_debug(1 << CXGBI_DBG_DDP,
  1022. "page %d/%u, %u + %u.\n",
  1023. i, sgcnt, sgoffset, sglen);
  1024. goto error_out;
  1025. }
  1026. j++;
  1027. if (j == gl->nelem || sg->offset) {
  1028. log_debug(1 << CXGBI_DBG_DDP,
  1029. "page %d/%u, offset %u.\n",
  1030. j, gl->nelem, sg->offset);
  1031. goto error_out;
  1032. }
  1033. gl->pages[j] = page;
  1034. sglen = sg->length;
  1035. sgoffset = sg->offset;
  1036. sgpage = page;
  1037. }
  1038. }
  1039. gl->nelem = ++j;
  1040. if (ddp_gl_map(pdev, gl) < 0)
  1041. goto error_out;
  1042. return gl;
  1043. error_out:
  1044. kfree(gl);
  1045. return NULL;
  1046. }
  1047. static void ddp_tag_release(struct cxgbi_hba *chba, u32 tag)
  1048. {
  1049. struct cxgbi_device *cdev = chba->cdev;
  1050. struct cxgbi_ddp_info *ddp = cdev->ddp;
  1051. u32 idx;
  1052. idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
  1053. if (idx < ddp->nppods) {
  1054. struct cxgbi_gather_list *gl = ddp->gl_map[idx];
  1055. unsigned int npods;
  1056. if (!gl || !gl->nelem) {
  1057. pr_warn("tag 0x%x, idx %u, gl 0x%p, %u.\n",
  1058. tag, idx, gl, gl ? gl->nelem : 0);
  1059. return;
  1060. }
  1061. npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
  1062. log_debug(1 << CXGBI_DBG_DDP,
  1063. "tag 0x%x, release idx %u, npods %u.\n",
  1064. tag, idx, npods);
  1065. cdev->csk_ddp_clear(chba, tag, idx, npods);
  1066. ddp_unmark_entries(ddp, idx, npods);
  1067. ddp_release_gl(gl, ddp->pdev);
  1068. } else
  1069. pr_warn("tag 0x%x, idx %u > max %u.\n", tag, idx, ddp->nppods);
  1070. }
  1071. static int ddp_tag_reserve(struct cxgbi_sock *csk, unsigned int tid,
  1072. u32 sw_tag, u32 *tagp, struct cxgbi_gather_list *gl,
  1073. gfp_t gfp)
  1074. {
  1075. struct cxgbi_device *cdev = csk->cdev;
  1076. struct cxgbi_ddp_info *ddp = cdev->ddp;
  1077. struct cxgbi_tag_format *tformat = &cdev->tag_format;
  1078. struct cxgbi_pagepod_hdr hdr;
  1079. unsigned int npods;
  1080. int idx = -1;
  1081. int err = -ENOMEM;
  1082. u32 tag;
  1083. npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
  1084. if (ddp->idx_last == ddp->nppods)
  1085. idx = ddp_find_unused_entries(ddp, 0, ddp->nppods,
  1086. npods, gl);
  1087. else {
  1088. idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
  1089. ddp->nppods, npods,
  1090. gl);
  1091. if (idx < 0 && ddp->idx_last >= npods) {
  1092. idx = ddp_find_unused_entries(ddp, 0,
  1093. min(ddp->idx_last + npods, ddp->nppods),
  1094. npods, gl);
  1095. }
  1096. }
  1097. if (idx < 0) {
  1098. log_debug(1 << CXGBI_DBG_DDP,
  1099. "xferlen %u, gl %u, npods %u NO DDP.\n",
  1100. gl->length, gl->nelem, npods);
  1101. return idx;
  1102. }
  1103. tag = cxgbi_ddp_tag_base(tformat, sw_tag);
  1104. tag |= idx << PPOD_IDX_SHIFT;
  1105. hdr.rsvd = 0;
  1106. hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
  1107. hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
  1108. hdr.max_offset = htonl(gl->length);
  1109. hdr.page_offset = htonl(gl->offset);
  1110. err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
  1111. if (err < 0)
  1112. goto unmark_entries;
  1113. ddp->idx_last = idx;
  1114. log_debug(1 << CXGBI_DBG_DDP,
  1115. "xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
  1116. gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
  1117. npods);
  1118. *tagp = tag;
  1119. return 0;
  1120. unmark_entries:
  1121. ddp_unmark_entries(ddp, idx, npods);
  1122. return err;
  1123. }
  1124. int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
  1125. unsigned int sw_tag, unsigned int xferlen,
  1126. struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
  1127. {
  1128. struct cxgbi_device *cdev = csk->cdev;
  1129. struct cxgbi_tag_format *tformat = &cdev->tag_format;
  1130. struct cxgbi_gather_list *gl;
  1131. int err;
  1132. if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
  1133. xferlen < DDP_THRESHOLD) {
  1134. log_debug(1 << CXGBI_DBG_DDP,
  1135. "pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
  1136. return -EINVAL;
  1137. }
  1138. if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
  1139. log_debug(1 << CXGBI_DBG_DDP,
  1140. "sw_tag 0x%x NOT usable.\n", sw_tag);
  1141. return -EINVAL;
  1142. }
  1143. gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
  1144. if (!gl)
  1145. return -ENOMEM;
  1146. err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
  1147. if (err < 0)
  1148. ddp_release_gl(gl, cdev->pdev);
  1149. return err;
  1150. }
  1151. static void ddp_destroy(struct kref *kref)
  1152. {
  1153. struct cxgbi_ddp_info *ddp = container_of(kref,
  1154. struct cxgbi_ddp_info,
  1155. refcnt);
  1156. struct cxgbi_device *cdev = ddp->cdev;
  1157. int i = 0;
  1158. pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
  1159. while (i < ddp->nppods) {
  1160. struct cxgbi_gather_list *gl = ddp->gl_map[i];
  1161. if (gl) {
  1162. int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
  1163. >> PPOD_PAGES_SHIFT;
  1164. pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
  1165. kfree(gl);
  1166. i += npods;
  1167. } else
  1168. i++;
  1169. }
  1170. cxgbi_free_big_mem(ddp);
  1171. }
  1172. int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
  1173. {
  1174. struct cxgbi_ddp_info *ddp = cdev->ddp;
  1175. log_debug(1 << CXGBI_DBG_DDP,
  1176. "cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
  1177. cdev->ddp = NULL;
  1178. if (ddp)
  1179. return kref_put(&ddp->refcnt, ddp_destroy);
  1180. return 0;
  1181. }
  1182. EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
  1183. int cxgbi_ddp_init(struct cxgbi_device *cdev,
  1184. unsigned int llimit, unsigned int ulimit,
  1185. unsigned int max_txsz, unsigned int max_rxsz)
  1186. {
  1187. struct cxgbi_ddp_info *ddp;
  1188. unsigned int ppmax, bits;
  1189. ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
  1190. bits = __ilog2_u32(ppmax) + 1;
  1191. if (bits > PPOD_IDX_MAX_SIZE)
  1192. bits = PPOD_IDX_MAX_SIZE;
  1193. ppmax = (1 << (bits - 1)) - 1;
  1194. ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
  1195. ppmax * (sizeof(struct cxgbi_gather_list *) +
  1196. sizeof(struct sk_buff *)),
  1197. GFP_KERNEL);
  1198. if (!ddp) {
  1199. pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
  1200. return -ENOMEM;
  1201. }
  1202. ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
  1203. cdev->ddp = ddp;
  1204. spin_lock_init(&ddp->map_lock);
  1205. kref_init(&ddp->refcnt);
  1206. ddp->cdev = cdev;
  1207. ddp->pdev = cdev->pdev;
  1208. ddp->llimit = llimit;
  1209. ddp->ulimit = ulimit;
  1210. ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
  1211. ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
  1212. ddp->nppods = ppmax;
  1213. ddp->idx_last = ppmax;
  1214. ddp->idx_bits = bits;
  1215. ddp->idx_mask = (1 << bits) - 1;
  1216. ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
  1217. cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
  1218. cdev->tag_format.rsvd_bits = ddp->idx_bits;
  1219. cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
  1220. cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
  1221. pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
  1222. cdev->ports[0]->name, cdev->tag_format.sw_bits,
  1223. cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
  1224. cdev->tag_format.rsvd_mask);
  1225. cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
  1226. ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
  1227. cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
  1228. ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
  1229. log_debug(1 << CXGBI_DBG_DDP,
  1230. "%s max payload size: %u/%u, %u/%u.\n",
  1231. cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
  1232. cdev->rx_max_size, ddp->max_rxsz);
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
  1236. /*
  1237. * APIs interacting with open-iscsi libraries
  1238. */
  1239. static unsigned char padding[4];
  1240. static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
  1241. {
  1242. struct scsi_cmnd *sc = task->sc;
  1243. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  1244. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1245. struct cxgbi_hba *chba = cconn->chba;
  1246. struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
  1247. u32 tag = ntohl((__force u32)hdr_itt);
  1248. log_debug(1 << CXGBI_DBG_DDP,
  1249. "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
  1250. if (sc &&
  1251. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  1252. cxgbi_is_ddp_tag(tformat, tag))
  1253. ddp_tag_release(chba, tag);
  1254. }
  1255. static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
  1256. {
  1257. struct scsi_cmnd *sc = task->sc;
  1258. struct iscsi_conn *conn = task->conn;
  1259. struct iscsi_session *sess = conn->session;
  1260. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1261. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1262. struct cxgbi_hba *chba = cconn->chba;
  1263. struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
  1264. u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
  1265. u32 tag = 0;
  1266. int err = -EINVAL;
  1267. if (sc &&
  1268. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
  1269. err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
  1270. scsi_in(sc)->length,
  1271. scsi_in(sc)->table.sgl,
  1272. scsi_in(sc)->table.nents,
  1273. GFP_ATOMIC);
  1274. if (err < 0)
  1275. log_debug(1 << CXGBI_DBG_DDP,
  1276. "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
  1277. cconn->cep->csk, task, scsi_in(sc)->length,
  1278. scsi_in(sc)->table.nents);
  1279. }
  1280. if (err < 0)
  1281. tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
  1282. /* the itt need to sent in big-endian order */
  1283. *hdr_itt = (__force itt_t)htonl(tag);
  1284. log_debug(1 << CXGBI_DBG_DDP,
  1285. "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
  1286. chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
  1287. return 0;
  1288. }
  1289. void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
  1290. {
  1291. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1292. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1293. struct cxgbi_device *cdev = cconn->chba->cdev;
  1294. u32 tag = ntohl((__force u32) itt);
  1295. u32 sw_bits;
  1296. sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
  1297. if (idx)
  1298. *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
  1299. if (age)
  1300. *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
  1301. log_debug(1 << CXGBI_DBG_DDP,
  1302. "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
  1303. cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
  1304. age ? *age : 0xFF);
  1305. }
  1306. EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
  1307. void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
  1308. {
  1309. struct iscsi_conn *conn = csk->user_data;
  1310. if (conn) {
  1311. log_debug(1 << CXGBI_DBG_SOCK,
  1312. "csk 0x%p, cid %d.\n", csk, conn->id);
  1313. iscsi_conn_queue_work(conn);
  1314. }
  1315. }
  1316. EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
  1317. /*
  1318. * pdu receive, interact with libiscsi_tcp
  1319. */
  1320. static inline int read_pdu_skb(struct iscsi_conn *conn,
  1321. struct sk_buff *skb,
  1322. unsigned int offset,
  1323. int offloaded)
  1324. {
  1325. int status = 0;
  1326. int bytes_read;
  1327. bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
  1328. switch (status) {
  1329. case ISCSI_TCP_CONN_ERR:
  1330. pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
  1331. skb, offset, offloaded);
  1332. return -EIO;
  1333. case ISCSI_TCP_SUSPENDED:
  1334. log_debug(1 << CXGBI_DBG_PDU_RX,
  1335. "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
  1336. skb, offset, offloaded, bytes_read);
  1337. /* no transfer - just have caller flush queue */
  1338. return bytes_read;
  1339. case ISCSI_TCP_SKB_DONE:
  1340. pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
  1341. skb, offset, offloaded);
  1342. /*
  1343. * pdus should always fit in the skb and we should get
  1344. * segment done notifcation.
  1345. */
  1346. iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
  1347. return -EFAULT;
  1348. case ISCSI_TCP_SEGMENT_DONE:
  1349. log_debug(1 << CXGBI_DBG_PDU_RX,
  1350. "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
  1351. skb, offset, offloaded, bytes_read);
  1352. return bytes_read;
  1353. default:
  1354. pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
  1355. skb, offset, offloaded, status);
  1356. return -EINVAL;
  1357. }
  1358. }
  1359. static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
  1360. {
  1361. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1362. log_debug(1 << CXGBI_DBG_PDU_RX,
  1363. "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
  1364. conn, skb, skb->len, cxgbi_skcb_flags(skb));
  1365. if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
  1366. pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
  1367. iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
  1368. return -EIO;
  1369. }
  1370. if (conn->hdrdgst_en &&
  1371. cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
  1372. pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
  1373. iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
  1374. return -EIO;
  1375. }
  1376. return read_pdu_skb(conn, skb, 0, 0);
  1377. }
  1378. static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
  1379. struct sk_buff *skb, unsigned int offset)
  1380. {
  1381. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1382. bool offloaded = 0;
  1383. int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
  1384. log_debug(1 << CXGBI_DBG_PDU_RX,
  1385. "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
  1386. conn, skb, skb->len, cxgbi_skcb_flags(skb));
  1387. if (conn->datadgst_en &&
  1388. cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
  1389. pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
  1390. conn, lskb, cxgbi_skcb_flags(lskb));
  1391. iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
  1392. return -EIO;
  1393. }
  1394. if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
  1395. return 0;
  1396. /* coalesced, add header digest length */
  1397. if (lskb == skb && conn->hdrdgst_en)
  1398. offset += ISCSI_DIGEST_SIZE;
  1399. if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
  1400. offloaded = 1;
  1401. if (opcode == ISCSI_OP_SCSI_DATA_IN)
  1402. log_debug(1 << CXGBI_DBG_PDU_RX,
  1403. "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
  1404. skb, opcode, ntohl(tcp_conn->in.hdr->itt),
  1405. tcp_conn->in.datalen, offloaded ? "is" : "not");
  1406. return read_pdu_skb(conn, skb, offset, offloaded);
  1407. }
  1408. static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
  1409. {
  1410. struct cxgbi_device *cdev = csk->cdev;
  1411. int must_send;
  1412. u32 credits;
  1413. log_debug(1 << CXGBI_DBG_PDU_RX,
  1414. "csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
  1415. csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
  1416. csk->rcv_wup, cdev->rx_credit_thres,
  1417. cdev->rcv_win);
  1418. if (csk->state != CTP_ESTABLISHED)
  1419. return;
  1420. credits = csk->copied_seq - csk->rcv_wup;
  1421. if (unlikely(!credits))
  1422. return;
  1423. if (unlikely(cdev->rx_credit_thres == 0))
  1424. return;
  1425. must_send = credits + 16384 >= cdev->rcv_win;
  1426. if (must_send || credits >= cdev->rx_credit_thres)
  1427. csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
  1428. }
  1429. void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
  1430. {
  1431. struct cxgbi_device *cdev = csk->cdev;
  1432. struct iscsi_conn *conn = csk->user_data;
  1433. struct sk_buff *skb;
  1434. unsigned int read = 0;
  1435. int err = 0;
  1436. log_debug(1 << CXGBI_DBG_PDU_RX,
  1437. "csk 0x%p, conn 0x%p.\n", csk, conn);
  1438. if (unlikely(!conn || conn->suspend_rx)) {
  1439. log_debug(1 << CXGBI_DBG_PDU_RX,
  1440. "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
  1441. csk, conn, conn ? conn->id : 0xFF,
  1442. conn ? conn->suspend_rx : 0xFF);
  1443. return;
  1444. }
  1445. while (!err) {
  1446. skb = skb_peek(&csk->receive_queue);
  1447. if (!skb ||
  1448. !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
  1449. if (skb)
  1450. log_debug(1 << CXGBI_DBG_PDU_RX,
  1451. "skb 0x%p, NOT ready 0x%lx.\n",
  1452. skb, cxgbi_skcb_flags(skb));
  1453. break;
  1454. }
  1455. __skb_unlink(skb, &csk->receive_queue);
  1456. read += cxgbi_skcb_rx_pdulen(skb);
  1457. log_debug(1 << CXGBI_DBG_PDU_RX,
  1458. "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
  1459. csk, skb, skb->len, cxgbi_skcb_flags(skb),
  1460. cxgbi_skcb_rx_pdulen(skb));
  1461. if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
  1462. err = skb_read_pdu_bhs(conn, skb);
  1463. if (err < 0) {
  1464. pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
  1465. "f 0x%lx, plen %u.\n",
  1466. csk, skb, skb->len,
  1467. cxgbi_skcb_flags(skb),
  1468. cxgbi_skcb_rx_pdulen(skb));
  1469. goto skb_done;
  1470. }
  1471. err = skb_read_pdu_data(conn, skb, skb,
  1472. err + cdev->skb_rx_extra);
  1473. if (err < 0)
  1474. pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
  1475. "f 0x%lx, plen %u.\n",
  1476. csk, skb, skb->len,
  1477. cxgbi_skcb_flags(skb),
  1478. cxgbi_skcb_rx_pdulen(skb));
  1479. } else {
  1480. err = skb_read_pdu_bhs(conn, skb);
  1481. if (err < 0) {
  1482. pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
  1483. "f 0x%lx, plen %u.\n",
  1484. csk, skb, skb->len,
  1485. cxgbi_skcb_flags(skb),
  1486. cxgbi_skcb_rx_pdulen(skb));
  1487. goto skb_done;
  1488. }
  1489. if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
  1490. struct sk_buff *dskb;
  1491. dskb = skb_peek(&csk->receive_queue);
  1492. if (!dskb) {
  1493. pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
  1494. " plen %u, NO data.\n",
  1495. csk, skb, skb->len,
  1496. cxgbi_skcb_flags(skb),
  1497. cxgbi_skcb_rx_pdulen(skb));
  1498. err = -EIO;
  1499. goto skb_done;
  1500. }
  1501. __skb_unlink(dskb, &csk->receive_queue);
  1502. err = skb_read_pdu_data(conn, skb, dskb, 0);
  1503. if (err < 0)
  1504. pr_err("data, csk 0x%p, skb 0x%p,%u, "
  1505. "f 0x%lx, plen %u, dskb 0x%p,"
  1506. "%u.\n",
  1507. csk, skb, skb->len,
  1508. cxgbi_skcb_flags(skb),
  1509. cxgbi_skcb_rx_pdulen(skb),
  1510. dskb, dskb->len);
  1511. __kfree_skb(dskb);
  1512. } else
  1513. err = skb_read_pdu_data(conn, skb, skb, 0);
  1514. }
  1515. skb_done:
  1516. __kfree_skb(skb);
  1517. if (err < 0)
  1518. break;
  1519. }
  1520. log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
  1521. if (read) {
  1522. csk->copied_seq += read;
  1523. csk_return_rx_credits(csk, read);
  1524. conn->rxdata_octets += read;
  1525. }
  1526. if (err < 0) {
  1527. pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
  1528. csk, conn, err, read);
  1529. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1530. }
  1531. }
  1532. EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
  1533. static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
  1534. unsigned int offset, unsigned int *off,
  1535. struct scatterlist **sgp)
  1536. {
  1537. int i;
  1538. struct scatterlist *sg;
  1539. for_each_sg(sgl, sg, sgcnt, i) {
  1540. if (offset < sg->length) {
  1541. *off = offset;
  1542. *sgp = sg;
  1543. return 0;
  1544. }
  1545. offset -= sg->length;
  1546. }
  1547. return -EFAULT;
  1548. }
  1549. static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
  1550. unsigned int dlen, skb_frag_t *frags,
  1551. int frag_max)
  1552. {
  1553. unsigned int datalen = dlen;
  1554. unsigned int sglen = sg->length - sgoffset;
  1555. struct page *page = sg_page(sg);
  1556. int i;
  1557. i = 0;
  1558. do {
  1559. unsigned int copy;
  1560. if (!sglen) {
  1561. sg = sg_next(sg);
  1562. if (!sg) {
  1563. pr_warn("sg %d NULL, len %u/%u.\n",
  1564. i, datalen, dlen);
  1565. return -EINVAL;
  1566. }
  1567. sgoffset = 0;
  1568. sglen = sg->length;
  1569. page = sg_page(sg);
  1570. }
  1571. copy = min(datalen, sglen);
  1572. if (i && page == frags[i - 1].page &&
  1573. sgoffset + sg->offset ==
  1574. frags[i - 1].page_offset + frags[i - 1].size) {
  1575. frags[i - 1].size += copy;
  1576. } else {
  1577. if (i >= frag_max) {
  1578. pr_warn("too many pages %u, dlen %u.\n",
  1579. frag_max, dlen);
  1580. return -EINVAL;
  1581. }
  1582. frags[i].page = page;
  1583. frags[i].page_offset = sg->offset + sgoffset;
  1584. frags[i].size = copy;
  1585. i++;
  1586. }
  1587. datalen -= copy;
  1588. sgoffset += copy;
  1589. sglen -= copy;
  1590. } while (datalen);
  1591. return i;
  1592. }
  1593. int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
  1594. {
  1595. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  1596. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1597. struct cxgbi_device *cdev = cconn->chba->cdev;
  1598. struct iscsi_conn *conn = task->conn;
  1599. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1600. struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
  1601. struct scsi_cmnd *sc = task->sc;
  1602. int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
  1603. tcp_task->dd_data = tdata;
  1604. task->hdr = NULL;
  1605. if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
  1606. (opcode == ISCSI_OP_SCSI_DATA_OUT ||
  1607. (opcode == ISCSI_OP_SCSI_CMD &&
  1608. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
  1609. /* data could goes into skb head */
  1610. headroom += min_t(unsigned int,
  1611. SKB_MAX_HEAD(cdev->skb_tx_rsvd),
  1612. conn->max_xmit_dlength);
  1613. tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
  1614. if (!tdata->skb) {
  1615. pr_warn("alloc skb %u+%u, opcode 0x%x failed.\n",
  1616. cdev->skb_tx_rsvd, headroom, opcode);
  1617. return -ENOMEM;
  1618. }
  1619. skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
  1620. task->hdr = (struct iscsi_hdr *)tdata->skb->data;
  1621. task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
  1622. /* data_out uses scsi_cmd's itt */
  1623. if (opcode != ISCSI_OP_SCSI_DATA_OUT)
  1624. task_reserve_itt(task, &task->hdr->itt);
  1625. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
  1626. "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
  1627. task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
  1628. conn->max_xmit_dlength, ntohl(task->hdr->itt));
  1629. return 0;
  1630. }
  1631. EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
  1632. static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
  1633. {
  1634. if (hcrc || dcrc) {
  1635. u8 submode = 0;
  1636. if (hcrc)
  1637. submode |= 1;
  1638. if (dcrc)
  1639. submode |= 2;
  1640. cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
  1641. } else
  1642. cxgbi_skcb_ulp_mode(skb) = 0;
  1643. }
  1644. int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
  1645. unsigned int count)
  1646. {
  1647. struct iscsi_conn *conn = task->conn;
  1648. struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
  1649. struct sk_buff *skb = tdata->skb;
  1650. unsigned int datalen = count;
  1651. int i, padlen = iscsi_padding(count);
  1652. struct page *pg;
  1653. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
  1654. "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
  1655. task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
  1656. ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
  1657. skb_put(skb, task->hdr_len);
  1658. tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
  1659. if (!count)
  1660. return 0;
  1661. if (task->sc) {
  1662. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  1663. struct scatterlist *sg = NULL;
  1664. int err;
  1665. tdata->offset = offset;
  1666. tdata->count = count;
  1667. err = sgl_seek_offset(
  1668. sdb->table.sgl, sdb->table.nents,
  1669. tdata->offset, &tdata->sgoffset, &sg);
  1670. if (err < 0) {
  1671. pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
  1672. sdb->table.nents, tdata->offset, sdb->length);
  1673. return err;
  1674. }
  1675. err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
  1676. tdata->frags, MAX_PDU_FRAGS);
  1677. if (err < 0) {
  1678. pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
  1679. sdb->table.nents, tdata->offset, tdata->count);
  1680. return err;
  1681. }
  1682. tdata->nr_frags = err;
  1683. if (tdata->nr_frags > MAX_SKB_FRAGS ||
  1684. (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
  1685. char *dst = skb->data + task->hdr_len;
  1686. skb_frag_t *frag = tdata->frags;
  1687. /* data fits in the skb's headroom */
  1688. for (i = 0; i < tdata->nr_frags; i++, frag++) {
  1689. char *src = kmap_atomic(frag->page,
  1690. KM_SOFTIRQ0);
  1691. memcpy(dst, src+frag->page_offset, frag->size);
  1692. dst += frag->size;
  1693. kunmap_atomic(src, KM_SOFTIRQ0);
  1694. }
  1695. if (padlen) {
  1696. memset(dst, 0, padlen);
  1697. padlen = 0;
  1698. }
  1699. skb_put(skb, count + padlen);
  1700. } else {
  1701. /* data fit into frag_list */
  1702. for (i = 0; i < tdata->nr_frags; i++)
  1703. get_page(tdata->frags[i].page);
  1704. memcpy(skb_shinfo(skb)->frags, tdata->frags,
  1705. sizeof(skb_frag_t) * tdata->nr_frags);
  1706. skb_shinfo(skb)->nr_frags = tdata->nr_frags;
  1707. skb->len += count;
  1708. skb->data_len += count;
  1709. skb->truesize += count;
  1710. }
  1711. } else {
  1712. pg = virt_to_page(task->data);
  1713. get_page(pg);
  1714. skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
  1715. count);
  1716. skb->len += count;
  1717. skb->data_len += count;
  1718. skb->truesize += count;
  1719. }
  1720. if (padlen) {
  1721. i = skb_shinfo(skb)->nr_frags;
  1722. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  1723. virt_to_page(padding), offset_in_page(padding),
  1724. padlen);
  1725. skb->data_len += padlen;
  1726. skb->truesize += padlen;
  1727. skb->len += padlen;
  1728. }
  1729. return 0;
  1730. }
  1731. EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
  1732. int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
  1733. {
  1734. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  1735. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1736. struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
  1737. struct sk_buff *skb = tdata->skb;
  1738. unsigned int datalen;
  1739. int err;
  1740. if (!skb) {
  1741. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
  1742. "task 0x%p, skb NULL.\n", task);
  1743. return 0;
  1744. }
  1745. datalen = skb->data_len;
  1746. tdata->skb = NULL;
  1747. err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
  1748. if (err > 0) {
  1749. int pdulen = err;
  1750. log_debug(1 << CXGBI_DBG_PDU_TX,
  1751. "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
  1752. task, task->sc, skb, skb->len, skb->data_len, err);
  1753. if (task->conn->hdrdgst_en)
  1754. pdulen += ISCSI_DIGEST_SIZE;
  1755. if (datalen && task->conn->datadgst_en)
  1756. pdulen += ISCSI_DIGEST_SIZE;
  1757. task->conn->txdata_octets += pdulen;
  1758. return 0;
  1759. }
  1760. if (err == -EAGAIN || err == -ENOBUFS) {
  1761. log_debug(1 << CXGBI_DBG_PDU_TX,
  1762. "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
  1763. task, skb, skb->len, skb->data_len, err);
  1764. /* reset skb to send when we are called again */
  1765. tdata->skb = skb;
  1766. return err;
  1767. }
  1768. kfree_skb(skb);
  1769. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
  1770. "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
  1771. task->itt, skb, skb->len, skb->data_len, err);
  1772. iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
  1773. iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
  1774. return err;
  1775. }
  1776. EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
  1777. void cxgbi_cleanup_task(struct iscsi_task *task)
  1778. {
  1779. struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
  1780. log_debug(1 << CXGBI_DBG_ISCSI,
  1781. "task 0x%p, skb 0x%p, itt 0x%x.\n",
  1782. task, tdata->skb, task->hdr_itt);
  1783. /* never reached the xmit task callout */
  1784. if (tdata->skb)
  1785. __kfree_skb(tdata->skb);
  1786. memset(tdata, 0, sizeof(*tdata));
  1787. task_release_itt(task, task->hdr_itt);
  1788. iscsi_tcp_cleanup_task(task);
  1789. }
  1790. EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
  1791. void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
  1792. struct iscsi_stats *stats)
  1793. {
  1794. struct iscsi_conn *conn = cls_conn->dd_data;
  1795. stats->txdata_octets = conn->txdata_octets;
  1796. stats->rxdata_octets = conn->rxdata_octets;
  1797. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1798. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1799. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1800. stats->datain_pdus = conn->datain_pdus_cnt;
  1801. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1802. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1803. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1804. stats->digest_err = 0;
  1805. stats->timeout_err = 0;
  1806. stats->custom_length = 1;
  1807. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  1808. stats->custom[0].value = conn->eh_abort_cnt;
  1809. }
  1810. EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
  1811. static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
  1812. {
  1813. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1814. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1815. struct cxgbi_device *cdev = cconn->chba->cdev;
  1816. unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
  1817. unsigned int max_def = 512 * MAX_SKB_FRAGS;
  1818. unsigned int max = max(max_def, headroom);
  1819. max = min(cconn->chba->cdev->tx_max_size, max);
  1820. if (conn->max_xmit_dlength)
  1821. conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
  1822. else
  1823. conn->max_xmit_dlength = max;
  1824. cxgbi_align_pdu_size(conn->max_xmit_dlength);
  1825. return 0;
  1826. }
  1827. static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
  1828. {
  1829. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1830. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1831. unsigned int max = cconn->chba->cdev->rx_max_size;
  1832. cxgbi_align_pdu_size(max);
  1833. if (conn->max_recv_dlength) {
  1834. if (conn->max_recv_dlength > max) {
  1835. pr_err("MaxRecvDataSegmentLength %u > %u.\n",
  1836. conn->max_recv_dlength, max);
  1837. return -EINVAL;
  1838. }
  1839. conn->max_recv_dlength = min(conn->max_recv_dlength, max);
  1840. cxgbi_align_pdu_size(conn->max_recv_dlength);
  1841. } else
  1842. conn->max_recv_dlength = max;
  1843. return 0;
  1844. }
  1845. int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
  1846. enum iscsi_param param, char *buf, int buflen)
  1847. {
  1848. struct iscsi_conn *conn = cls_conn->dd_data;
  1849. struct iscsi_session *session = conn->session;
  1850. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1851. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1852. struct cxgbi_sock *csk = cconn->cep->csk;
  1853. int value, err = 0;
  1854. log_debug(1 << CXGBI_DBG_ISCSI,
  1855. "cls_conn 0x%p, param %d, buf(%d) %s.\n",
  1856. cls_conn, param, buflen, buf);
  1857. switch (param) {
  1858. case ISCSI_PARAM_HDRDGST_EN:
  1859. err = iscsi_set_param(cls_conn, param, buf, buflen);
  1860. if (!err && conn->hdrdgst_en)
  1861. err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
  1862. conn->hdrdgst_en,
  1863. conn->datadgst_en, 0);
  1864. break;
  1865. case ISCSI_PARAM_DATADGST_EN:
  1866. err = iscsi_set_param(cls_conn, param, buf, buflen);
  1867. if (!err && conn->datadgst_en)
  1868. err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
  1869. conn->hdrdgst_en,
  1870. conn->datadgst_en, 0);
  1871. break;
  1872. case ISCSI_PARAM_MAX_R2T:
  1873. sscanf(buf, "%d", &value);
  1874. if (value <= 0 || !is_power_of_2(value))
  1875. return -EINVAL;
  1876. if (session->max_r2t == value)
  1877. break;
  1878. iscsi_tcp_r2tpool_free(session);
  1879. err = iscsi_set_param(cls_conn, param, buf, buflen);
  1880. if (!err && iscsi_tcp_r2tpool_alloc(session))
  1881. return -ENOMEM;
  1882. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1883. err = iscsi_set_param(cls_conn, param, buf, buflen);
  1884. if (!err)
  1885. err = cxgbi_conn_max_recv_dlength(conn);
  1886. break;
  1887. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1888. err = iscsi_set_param(cls_conn, param, buf, buflen);
  1889. if (!err)
  1890. err = cxgbi_conn_max_xmit_dlength(conn);
  1891. break;
  1892. default:
  1893. return iscsi_set_param(cls_conn, param, buf, buflen);
  1894. }
  1895. return err;
  1896. }
  1897. EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
  1898. int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
  1899. char *buf)
  1900. {
  1901. struct cxgbi_endpoint *cep = ep->dd_data;
  1902. struct cxgbi_sock *csk;
  1903. int len;
  1904. log_debug(1 << CXGBI_DBG_ISCSI,
  1905. "cls_conn 0x%p, param %d.\n", ep, param);
  1906. switch (param) {
  1907. case ISCSI_PARAM_CONN_PORT:
  1908. case ISCSI_PARAM_CONN_ADDRESS:
  1909. if (!cep)
  1910. return -ENOTCONN;
  1911. csk = cep->csk;
  1912. if (!csk)
  1913. return -ENOTCONN;
  1914. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  1915. &csk->daddr, param, buf);
  1916. default:
  1917. return -ENOSYS;
  1918. }
  1919. return len;
  1920. }
  1921. EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
  1922. struct iscsi_cls_conn *
  1923. cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
  1924. {
  1925. struct iscsi_cls_conn *cls_conn;
  1926. struct iscsi_conn *conn;
  1927. struct iscsi_tcp_conn *tcp_conn;
  1928. struct cxgbi_conn *cconn;
  1929. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
  1930. if (!cls_conn)
  1931. return NULL;
  1932. conn = cls_conn->dd_data;
  1933. tcp_conn = conn->dd_data;
  1934. cconn = tcp_conn->dd_data;
  1935. cconn->iconn = conn;
  1936. log_debug(1 << CXGBI_DBG_ISCSI,
  1937. "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
  1938. cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
  1939. return cls_conn;
  1940. }
  1941. EXPORT_SYMBOL_GPL(cxgbi_create_conn);
  1942. int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
  1943. struct iscsi_cls_conn *cls_conn,
  1944. u64 transport_eph, int is_leading)
  1945. {
  1946. struct iscsi_conn *conn = cls_conn->dd_data;
  1947. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1948. struct cxgbi_conn *cconn = tcp_conn->dd_data;
  1949. struct iscsi_endpoint *ep;
  1950. struct cxgbi_endpoint *cep;
  1951. struct cxgbi_sock *csk;
  1952. int err;
  1953. ep = iscsi_lookup_endpoint(transport_eph);
  1954. if (!ep)
  1955. return -EINVAL;
  1956. /* setup ddp pagesize */
  1957. cep = ep->dd_data;
  1958. csk = cep->csk;
  1959. err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
  1960. if (err < 0)
  1961. return err;
  1962. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1963. if (err)
  1964. return -EINVAL;
  1965. /* calculate the tag idx bits needed for this conn based on cmds_max */
  1966. cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
  1967. write_lock_bh(&csk->callback_lock);
  1968. csk->user_data = conn;
  1969. cconn->chba = cep->chba;
  1970. cconn->cep = cep;
  1971. cep->cconn = cconn;
  1972. write_unlock_bh(&csk->callback_lock);
  1973. cxgbi_conn_max_xmit_dlength(conn);
  1974. cxgbi_conn_max_recv_dlength(conn);
  1975. log_debug(1 << CXGBI_DBG_ISCSI,
  1976. "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
  1977. cls_session, cls_conn, ep, cconn, csk);
  1978. /* init recv engine */
  1979. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1980. return 0;
  1981. }
  1982. EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
  1983. struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
  1984. u16 cmds_max, u16 qdepth,
  1985. u32 initial_cmdsn)
  1986. {
  1987. struct cxgbi_endpoint *cep;
  1988. struct cxgbi_hba *chba;
  1989. struct Scsi_Host *shost;
  1990. struct iscsi_cls_session *cls_session;
  1991. struct iscsi_session *session;
  1992. if (!ep) {
  1993. pr_err("missing endpoint.\n");
  1994. return NULL;
  1995. }
  1996. cep = ep->dd_data;
  1997. chba = cep->chba;
  1998. shost = chba->shost;
  1999. BUG_ON(chba != iscsi_host_priv(shost));
  2000. cls_session = iscsi_session_setup(chba->cdev->itp, shost,
  2001. cmds_max, 0,
  2002. sizeof(struct iscsi_tcp_task) +
  2003. sizeof(struct cxgbi_task_data),
  2004. initial_cmdsn, ISCSI_MAX_TARGET);
  2005. if (!cls_session)
  2006. return NULL;
  2007. session = cls_session->dd_data;
  2008. if (iscsi_tcp_r2tpool_alloc(session))
  2009. goto remove_session;
  2010. log_debug(1 << CXGBI_DBG_ISCSI,
  2011. "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
  2012. return cls_session;
  2013. remove_session:
  2014. iscsi_session_teardown(cls_session);
  2015. return NULL;
  2016. }
  2017. EXPORT_SYMBOL_GPL(cxgbi_create_session);
  2018. void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
  2019. {
  2020. log_debug(1 << CXGBI_DBG_ISCSI,
  2021. "cls sess 0x%p.\n", cls_session);
  2022. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  2023. iscsi_session_teardown(cls_session);
  2024. }
  2025. EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
  2026. int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2027. char *buf, int buflen)
  2028. {
  2029. struct cxgbi_hba *chba = iscsi_host_priv(shost);
  2030. if (!chba->ndev) {
  2031. shost_printk(KERN_ERR, shost, "Could not get host param. "
  2032. "netdev for host not set.\n");
  2033. return -ENODEV;
  2034. }
  2035. log_debug(1 << CXGBI_DBG_ISCSI,
  2036. "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
  2037. shost, chba, chba->ndev->name, param, buflen, buf);
  2038. switch (param) {
  2039. case ISCSI_HOST_PARAM_IPADDRESS:
  2040. {
  2041. __be32 addr = in_aton(buf);
  2042. log_debug(1 << CXGBI_DBG_ISCSI,
  2043. "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
  2044. cxgbi_set_iscsi_ipv4(chba, addr);
  2045. return 0;
  2046. }
  2047. case ISCSI_HOST_PARAM_HWADDRESS:
  2048. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2049. return 0;
  2050. default:
  2051. return iscsi_host_set_param(shost, param, buf, buflen);
  2052. }
  2053. }
  2054. EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
  2055. int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2056. char *buf)
  2057. {
  2058. struct cxgbi_hba *chba = iscsi_host_priv(shost);
  2059. int len = 0;
  2060. if (!chba->ndev) {
  2061. shost_printk(KERN_ERR, shost, "Could not get host param. "
  2062. "netdev for host not set.\n");
  2063. return -ENODEV;
  2064. }
  2065. log_debug(1 << CXGBI_DBG_ISCSI,
  2066. "shost 0x%p, hba 0x%p,%s, param %d.\n",
  2067. shost, chba, chba->ndev->name, param);
  2068. switch (param) {
  2069. case ISCSI_HOST_PARAM_HWADDRESS:
  2070. len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
  2071. break;
  2072. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2073. len = sprintf(buf, "%s\n", chba->ndev->name);
  2074. break;
  2075. case ISCSI_HOST_PARAM_IPADDRESS:
  2076. {
  2077. __be32 addr;
  2078. addr = cxgbi_get_iscsi_ipv4(chba);
  2079. len = sprintf(buf, "%pI4", &addr);
  2080. log_debug(1 << CXGBI_DBG_ISCSI,
  2081. "hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
  2082. break;
  2083. }
  2084. default:
  2085. return iscsi_host_get_param(shost, param, buf);
  2086. }
  2087. return len;
  2088. }
  2089. EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
  2090. struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
  2091. struct sockaddr *dst_addr,
  2092. int non_blocking)
  2093. {
  2094. struct iscsi_endpoint *ep;
  2095. struct cxgbi_endpoint *cep;
  2096. struct cxgbi_hba *hba = NULL;
  2097. struct cxgbi_sock *csk;
  2098. int err = -EINVAL;
  2099. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
  2100. "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
  2101. shost, non_blocking, dst_addr);
  2102. if (shost) {
  2103. hba = iscsi_host_priv(shost);
  2104. if (!hba) {
  2105. pr_info("shost 0x%p, priv NULL.\n", shost);
  2106. goto err_out;
  2107. }
  2108. }
  2109. csk = cxgbi_check_route(dst_addr);
  2110. if (IS_ERR(csk))
  2111. return (struct iscsi_endpoint *)csk;
  2112. cxgbi_sock_get(csk);
  2113. if (!hba)
  2114. hba = csk->cdev->hbas[csk->port_id];
  2115. else if (hba != csk->cdev->hbas[csk->port_id]) {
  2116. pr_info("Could not connect through requested host %u"
  2117. "hba 0x%p != 0x%p (%u).\n",
  2118. shost->host_no, hba,
  2119. csk->cdev->hbas[csk->port_id], csk->port_id);
  2120. err = -ENOSPC;
  2121. goto release_conn;
  2122. }
  2123. err = sock_get_port(csk);
  2124. if (err)
  2125. goto release_conn;
  2126. cxgbi_sock_set_state(csk, CTP_CONNECTING);
  2127. err = csk->cdev->csk_init_act_open(csk);
  2128. if (err)
  2129. goto release_conn;
  2130. if (cxgbi_sock_is_closing(csk)) {
  2131. err = -ENOSPC;
  2132. pr_info("csk 0x%p is closing.\n", csk);
  2133. goto release_conn;
  2134. }
  2135. ep = iscsi_create_endpoint(sizeof(*cep));
  2136. if (!ep) {
  2137. err = -ENOMEM;
  2138. pr_info("iscsi alloc ep, OOM.\n");
  2139. goto release_conn;
  2140. }
  2141. cep = ep->dd_data;
  2142. cep->csk = csk;
  2143. cep->chba = hba;
  2144. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
  2145. "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
  2146. ep, cep, csk, hba, hba->ndev->name);
  2147. return ep;
  2148. release_conn:
  2149. cxgbi_sock_put(csk);
  2150. cxgbi_sock_closed(csk);
  2151. err_out:
  2152. return ERR_PTR(err);
  2153. }
  2154. EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
  2155. int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  2156. {
  2157. struct cxgbi_endpoint *cep = ep->dd_data;
  2158. struct cxgbi_sock *csk = cep->csk;
  2159. if (!cxgbi_sock_is_established(csk))
  2160. return 0;
  2161. return 1;
  2162. }
  2163. EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
  2164. void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
  2165. {
  2166. struct cxgbi_endpoint *cep = ep->dd_data;
  2167. struct cxgbi_conn *cconn = cep->cconn;
  2168. struct cxgbi_sock *csk = cep->csk;
  2169. log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
  2170. "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
  2171. ep, cep, cconn, csk, csk->state, csk->flags);
  2172. if (cconn && cconn->iconn) {
  2173. iscsi_suspend_tx(cconn->iconn);
  2174. write_lock_bh(&csk->callback_lock);
  2175. cep->csk->user_data = NULL;
  2176. cconn->cep = NULL;
  2177. write_unlock_bh(&csk->callback_lock);
  2178. }
  2179. iscsi_destroy_endpoint(ep);
  2180. if (likely(csk->state >= CTP_ESTABLISHED))
  2181. need_active_close(csk);
  2182. else
  2183. cxgbi_sock_closed(csk);
  2184. cxgbi_sock_put(csk);
  2185. }
  2186. EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
  2187. int cxgbi_iscsi_init(struct iscsi_transport *itp,
  2188. struct scsi_transport_template **stt)
  2189. {
  2190. *stt = iscsi_register_transport(itp);
  2191. if (*stt == NULL) {
  2192. pr_err("unable to register %s transport 0x%p.\n",
  2193. itp->name, itp);
  2194. return -ENODEV;
  2195. }
  2196. log_debug(1 << CXGBI_DBG_ISCSI,
  2197. "%s, registered iscsi transport 0x%p.\n",
  2198. itp->name, stt);
  2199. return 0;
  2200. }
  2201. EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
  2202. void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
  2203. struct scsi_transport_template **stt)
  2204. {
  2205. if (*stt) {
  2206. log_debug(1 << CXGBI_DBG_ISCSI,
  2207. "de-register transport 0x%p, %s, stt 0x%p.\n",
  2208. itp, itp->name, *stt);
  2209. *stt = NULL;
  2210. iscsi_unregister_transport(itp);
  2211. }
  2212. }
  2213. EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
  2214. static int __init libcxgbi_init_module(void)
  2215. {
  2216. sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
  2217. sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
  2218. pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
  2219. ISCSI_ITT_MASK, sw_tag_idx_bits,
  2220. ISCSI_AGE_MASK, sw_tag_age_bits);
  2221. ddp_setup_host_page_size();
  2222. return 0;
  2223. }
  2224. static void __exit libcxgbi_exit_module(void)
  2225. {
  2226. cxgbi_device_unregister_all(0xFF);
  2227. return;
  2228. }
  2229. module_init(libcxgbi_init_module);
  2230. module_exit(libcxgbi_exit_module);