cxgb3i_iscsi.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /* cxgb3i_iscsi.c: Chelsio S3xx iSCSI driver.
  2. *
  3. * Copyright (c) 2008 Chelsio Communications, Inc.
  4. * Copyright (c) 2008 Mike Christie
  5. * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation.
  10. *
  11. * Written by: Karen Xie (kxie@chelsio.com)
  12. */
  13. #include <linux/inet.h>
  14. #include <linux/slab.h>
  15. #include <linux/crypto.h>
  16. #include <linux/if_vlan.h>
  17. #include <net/dst.h>
  18. #include <net/tcp.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_eh.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi.h>
  24. #include <scsi/iscsi_proto.h>
  25. #include <scsi/libiscsi.h>
  26. #include <scsi/scsi_transport_iscsi.h>
  27. #include "cxgb3i.h"
  28. #include "cxgb3i_pdu.h"
  29. #ifdef __DEBUG_CXGB3I_TAG__
  30. #define cxgb3i_tag_debug cxgb3i_log_debug
  31. #else
  32. #define cxgb3i_tag_debug(fmt...)
  33. #endif
  34. #ifdef __DEBUG_CXGB3I_API__
  35. #define cxgb3i_api_debug cxgb3i_log_debug
  36. #else
  37. #define cxgb3i_api_debug(fmt...)
  38. #endif
  39. /*
  40. * align pdu size to multiple of 512 for better performance
  41. */
  42. #define align_pdu_size(n) do { n = (n) & (~511); } while (0)
  43. static struct scsi_transport_template *cxgb3i_scsi_transport;
  44. static struct scsi_host_template cxgb3i_host_template;
  45. static struct iscsi_transport cxgb3i_iscsi_transport;
  46. static unsigned char sw_tag_idx_bits;
  47. static unsigned char sw_tag_age_bits;
  48. static LIST_HEAD(cxgb3i_snic_list);
  49. static DEFINE_RWLOCK(cxgb3i_snic_rwlock);
  50. /**
  51. * cxgb3i_adpater_find_by_tdev - find the cxgb3i_adapter structure via t3cdev
  52. * @tdev: t3cdev pointer
  53. */
  54. struct cxgb3i_adapter *cxgb3i_adapter_find_by_tdev(struct t3cdev *tdev)
  55. {
  56. struct cxgb3i_adapter *snic;
  57. read_lock(&cxgb3i_snic_rwlock);
  58. list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
  59. if (snic->tdev == tdev) {
  60. read_unlock(&cxgb3i_snic_rwlock);
  61. return snic;
  62. }
  63. }
  64. read_unlock(&cxgb3i_snic_rwlock);
  65. return NULL;
  66. }
  67. static inline int adapter_update(struct cxgb3i_adapter *snic)
  68. {
  69. cxgb3i_log_info("snic 0x%p, t3dev 0x%p, updating.\n",
  70. snic, snic->tdev);
  71. return cxgb3i_adapter_ddp_info(snic->tdev, &snic->tag_format,
  72. &snic->tx_max_size,
  73. &snic->rx_max_size);
  74. }
  75. static int adapter_add(struct cxgb3i_adapter *snic)
  76. {
  77. struct t3cdev *t3dev = snic->tdev;
  78. struct adapter *adapter = tdev2adap(t3dev);
  79. int i, err;
  80. snic->pdev = adapter->pdev;
  81. snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
  82. err = cxgb3i_adapter_ddp_info(t3dev, &snic->tag_format,
  83. &snic->tx_max_size,
  84. &snic->rx_max_size);
  85. if (err < 0)
  86. return err;
  87. for_each_port(adapter, i) {
  88. snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
  89. if (!snic->hba[i])
  90. return -EINVAL;
  91. }
  92. snic->hba_cnt = adapter->params.nports;
  93. /* add to the list */
  94. write_lock(&cxgb3i_snic_rwlock);
  95. list_add_tail(&snic->list_head, &cxgb3i_snic_list);
  96. write_unlock(&cxgb3i_snic_rwlock);
  97. cxgb3i_log_info("t3dev 0x%p open, snic 0x%p, %u scsi hosts added.\n",
  98. t3dev, snic, snic->hba_cnt);
  99. return 0;
  100. }
  101. /**
  102. * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
  103. * @t3dev: t3cdev adapter
  104. */
  105. void cxgb3i_adapter_open(struct t3cdev *t3dev)
  106. {
  107. struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
  108. int err;
  109. if (snic)
  110. err = adapter_update(snic);
  111. else {
  112. snic = kzalloc(sizeof(*snic), GFP_KERNEL);
  113. if (snic) {
  114. spin_lock_init(&snic->lock);
  115. snic->tdev = t3dev;
  116. err = adapter_add(snic);
  117. } else
  118. err = -ENOMEM;
  119. }
  120. if (err < 0) {
  121. cxgb3i_log_info("snic 0x%p, f 0x%x, t3dev 0x%p open, err %d.\n",
  122. snic, snic ? snic->flags : 0, t3dev, err);
  123. if (snic) {
  124. snic->flags &= ~CXGB3I_ADAPTER_FLAG_RESET;
  125. cxgb3i_adapter_close(t3dev);
  126. }
  127. }
  128. }
  129. /**
  130. * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
  131. * @t3dev: t3cdev adapter
  132. */
  133. void cxgb3i_adapter_close(struct t3cdev *t3dev)
  134. {
  135. struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
  136. int i;
  137. if (!snic || snic->flags & CXGB3I_ADAPTER_FLAG_RESET) {
  138. cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, f 0x%x.\n",
  139. t3dev, snic, snic ? snic->flags : 0);
  140. return;
  141. }
  142. /* remove from the list */
  143. write_lock(&cxgb3i_snic_rwlock);
  144. list_del(&snic->list_head);
  145. write_unlock(&cxgb3i_snic_rwlock);
  146. for (i = 0; i < snic->hba_cnt; i++) {
  147. if (snic->hba[i]) {
  148. cxgb3i_hba_host_remove(snic->hba[i]);
  149. snic->hba[i] = NULL;
  150. }
  151. }
  152. cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, %u scsi hosts removed.\n",
  153. t3dev, snic, snic->hba_cnt);
  154. kfree(snic);
  155. }
  156. /**
  157. * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
  158. * @t3dev: t3cdev adapter
  159. */
  160. static struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
  161. {
  162. struct cxgb3i_adapter *snic;
  163. int i;
  164. if (ndev->priv_flags & IFF_802_1Q_VLAN)
  165. ndev = vlan_dev_real_dev(ndev);
  166. read_lock(&cxgb3i_snic_rwlock);
  167. list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
  168. for (i = 0; i < snic->hba_cnt; i++) {
  169. if (snic->hba[i]->ndev == ndev) {
  170. read_unlock(&cxgb3i_snic_rwlock);
  171. return snic->hba[i];
  172. }
  173. }
  174. }
  175. read_unlock(&cxgb3i_snic_rwlock);
  176. return NULL;
  177. }
  178. /**
  179. * cxgb3i_hba_host_add - register a new host with scsi/iscsi
  180. * @snic: the cxgb3i adapter
  181. * @ndev: associated net_device
  182. */
  183. struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
  184. struct net_device *ndev)
  185. {
  186. struct cxgb3i_hba *hba;
  187. struct Scsi_Host *shost;
  188. int err;
  189. shost = iscsi_host_alloc(&cxgb3i_host_template,
  190. sizeof(struct cxgb3i_hba), 1);
  191. if (!shost) {
  192. cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_alloc failed.\n",
  193. snic, ndev);
  194. return NULL;
  195. }
  196. shost->transportt = cxgb3i_scsi_transport;
  197. shost->max_lun = CXGB3I_MAX_LUN;
  198. shost->max_id = CXGB3I_MAX_TARGET;
  199. shost->max_channel = 0;
  200. shost->max_cmd_len = 16;
  201. hba = iscsi_host_priv(shost);
  202. hba->snic = snic;
  203. hba->ndev = ndev;
  204. hba->shost = shost;
  205. pci_dev_get(snic->pdev);
  206. err = iscsi_host_add(shost, &snic->pdev->dev);
  207. if (err) {
  208. cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_add failed.\n",
  209. snic, ndev);
  210. goto pci_dev_put;
  211. }
  212. cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
  213. shost, hba, shost->host_no);
  214. return hba;
  215. pci_dev_put:
  216. pci_dev_put(snic->pdev);
  217. scsi_host_put(shost);
  218. return NULL;
  219. }
  220. /**
  221. * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
  222. * @hba: the cxgb3i hba
  223. */
  224. void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
  225. {
  226. cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
  227. hba->shost, hba, hba->shost->host_no);
  228. iscsi_host_remove(hba->shost);
  229. pci_dev_put(hba->snic->pdev);
  230. iscsi_host_free(hba->shost);
  231. }
  232. /**
  233. * cxgb3i_ep_connect - establish TCP connection to target portal
  234. * @shost: scsi host to use
  235. * @dst_addr: target IP address
  236. * @non_blocking: blocking or non-blocking call
  237. *
  238. * Initiates a TCP/IP connection to the dst_addr
  239. */
  240. static struct iscsi_endpoint *cxgb3i_ep_connect(struct Scsi_Host *shost,
  241. struct sockaddr *dst_addr,
  242. int non_blocking)
  243. {
  244. struct iscsi_endpoint *ep;
  245. struct cxgb3i_endpoint *cep;
  246. struct cxgb3i_hba *hba = NULL;
  247. struct s3_conn *c3cn = NULL;
  248. int err = 0;
  249. if (shost)
  250. hba = iscsi_host_priv(shost);
  251. cxgb3i_api_debug("shost 0x%p, hba 0x%p.\n", shost, hba);
  252. c3cn = cxgb3i_c3cn_create();
  253. if (!c3cn) {
  254. cxgb3i_log_info("ep connect OOM.\n");
  255. err = -ENOMEM;
  256. goto release_conn;
  257. }
  258. err = cxgb3i_c3cn_connect(hba ? hba->ndev : NULL, c3cn,
  259. (struct sockaddr_in *)dst_addr);
  260. if (err < 0) {
  261. cxgb3i_log_info("ep connect failed.\n");
  262. goto release_conn;
  263. }
  264. hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
  265. if (!hba) {
  266. err = -ENOSPC;
  267. cxgb3i_log_info("NOT going through cxgbi device.\n");
  268. goto release_conn;
  269. }
  270. if (shost && hba != iscsi_host_priv(shost)) {
  271. err = -ENOSPC;
  272. cxgb3i_log_info("Could not connect through request host%u\n",
  273. shost->host_no);
  274. goto release_conn;
  275. }
  276. if (c3cn_is_closing(c3cn)) {
  277. err = -ENOSPC;
  278. cxgb3i_log_info("ep connect unable to connect.\n");
  279. goto release_conn;
  280. }
  281. ep = iscsi_create_endpoint(sizeof(*cep));
  282. if (!ep) {
  283. err = -ENOMEM;
  284. cxgb3i_log_info("iscsi alloc ep, OOM.\n");
  285. goto release_conn;
  286. }
  287. cep = ep->dd_data;
  288. cep->c3cn = c3cn;
  289. cep->hba = hba;
  290. cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
  291. ep, cep, c3cn, hba);
  292. return ep;
  293. release_conn:
  294. cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
  295. if (c3cn)
  296. cxgb3i_c3cn_release(c3cn);
  297. return ERR_PTR(err);
  298. }
  299. /**
  300. * cxgb3i_ep_poll - polls for TCP connection establishement
  301. * @ep: TCP connection (endpoint) handle
  302. * @timeout_ms: timeout value in milli secs
  303. *
  304. * polls for TCP connect request to complete
  305. */
  306. static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  307. {
  308. struct cxgb3i_endpoint *cep = ep->dd_data;
  309. struct s3_conn *c3cn = cep->c3cn;
  310. if (!c3cn_is_established(c3cn))
  311. return 0;
  312. cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
  313. return 1;
  314. }
  315. /**
  316. * cxgb3i_ep_disconnect - teardown TCP connection
  317. * @ep: TCP connection (endpoint) handle
  318. *
  319. * teardown TCP connection
  320. */
  321. static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
  322. {
  323. struct cxgb3i_endpoint *cep = ep->dd_data;
  324. struct cxgb3i_conn *cconn = cep->cconn;
  325. cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
  326. if (cconn && cconn->conn) {
  327. /*
  328. * stop the xmit path so the xmit_pdu function is
  329. * not being called
  330. */
  331. iscsi_suspend_tx(cconn->conn);
  332. write_lock_bh(&cep->c3cn->callback_lock);
  333. cep->c3cn->user_data = NULL;
  334. cconn->cep = NULL;
  335. write_unlock_bh(&cep->c3cn->callback_lock);
  336. }
  337. cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
  338. ep, cep, cep->c3cn);
  339. cxgb3i_c3cn_release(cep->c3cn);
  340. iscsi_destroy_endpoint(ep);
  341. }
  342. /**
  343. * cxgb3i_session_create - create a new iscsi session
  344. * @cmds_max: max # of commands
  345. * @qdepth: scsi queue depth
  346. * @initial_cmdsn: initial iscsi CMDSN for this session
  347. *
  348. * Creates a new iSCSI session
  349. */
  350. static struct iscsi_cls_session *
  351. cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
  352. u32 initial_cmdsn)
  353. {
  354. struct cxgb3i_endpoint *cep;
  355. struct cxgb3i_hba *hba;
  356. struct Scsi_Host *shost;
  357. struct iscsi_cls_session *cls_session;
  358. struct iscsi_session *session;
  359. if (!ep) {
  360. cxgb3i_log_error("%s, missing endpoint.\n", __func__);
  361. return NULL;
  362. }
  363. cep = ep->dd_data;
  364. hba = cep->hba;
  365. shost = hba->shost;
  366. cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
  367. BUG_ON(hba != iscsi_host_priv(shost));
  368. cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
  369. cmds_max, 0,
  370. sizeof(struct iscsi_tcp_task) +
  371. sizeof(struct cxgb3i_task_data),
  372. initial_cmdsn, ISCSI_MAX_TARGET);
  373. if (!cls_session)
  374. return NULL;
  375. session = cls_session->dd_data;
  376. if (iscsi_tcp_r2tpool_alloc(session))
  377. goto remove_session;
  378. return cls_session;
  379. remove_session:
  380. iscsi_session_teardown(cls_session);
  381. return NULL;
  382. }
  383. /**
  384. * cxgb3i_session_destroy - destroys iscsi session
  385. * @cls_session: pointer to iscsi cls session
  386. *
  387. * Destroys an iSCSI session instance and releases its all resources held
  388. */
  389. static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
  390. {
  391. cxgb3i_api_debug("sess 0x%p.\n", cls_session);
  392. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  393. iscsi_session_teardown(cls_session);
  394. }
  395. /**
  396. * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
  397. * @conn: iscsi connection
  398. * check the max. xmit pdu payload, reduce it if needed
  399. */
  400. static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
  401. {
  402. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  403. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  404. unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
  405. max = min(cconn->hba->snic->tx_max_size, max);
  406. if (conn->max_xmit_dlength)
  407. conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
  408. else
  409. conn->max_xmit_dlength = max;
  410. align_pdu_size(conn->max_xmit_dlength);
  411. cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
  412. conn, conn->max_xmit_dlength);
  413. return 0;
  414. }
  415. /**
  416. * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
  417. * @conn: iscsi connection
  418. * return 0 if the value is valid, < 0 otherwise.
  419. */
  420. static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
  421. {
  422. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  423. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  424. unsigned int max = cconn->hba->snic->rx_max_size;
  425. align_pdu_size(max);
  426. if (conn->max_recv_dlength) {
  427. if (conn->max_recv_dlength > max) {
  428. cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
  429. " Need to be <= %u.\n",
  430. conn->max_recv_dlength, max);
  431. return -EINVAL;
  432. }
  433. conn->max_recv_dlength = min(conn->max_recv_dlength, max);
  434. align_pdu_size(conn->max_recv_dlength);
  435. } else
  436. conn->max_recv_dlength = max;
  437. cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
  438. conn, conn->max_recv_dlength);
  439. return 0;
  440. }
  441. /**
  442. * cxgb3i_conn_create - create iscsi connection instance
  443. * @cls_session: pointer to iscsi cls session
  444. * @cid: iscsi cid
  445. *
  446. * Creates a new iSCSI connection instance for a given session
  447. */
  448. static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
  449. *cls_session, u32 cid)
  450. {
  451. struct iscsi_cls_conn *cls_conn;
  452. struct iscsi_conn *conn;
  453. struct iscsi_tcp_conn *tcp_conn;
  454. struct cxgb3i_conn *cconn;
  455. cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
  456. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
  457. if (!cls_conn)
  458. return NULL;
  459. conn = cls_conn->dd_data;
  460. tcp_conn = conn->dd_data;
  461. cconn = tcp_conn->dd_data;
  462. cconn->conn = conn;
  463. return cls_conn;
  464. }
  465. /**
  466. * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
  467. * @cls_session: pointer to iscsi cls session
  468. * @cls_conn: pointer to iscsi cls conn
  469. * @transport_eph: 64-bit EP handle
  470. * @is_leading: leading connection on this session?
  471. *
  472. * Binds together an iSCSI session, an iSCSI connection and a
  473. * TCP connection. This routine returns error code if the TCP
  474. * connection does not belong on the device iSCSI sess/conn is bound
  475. */
  476. static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
  477. struct iscsi_cls_conn *cls_conn,
  478. u64 transport_eph, int is_leading)
  479. {
  480. struct iscsi_conn *conn = cls_conn->dd_data;
  481. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  482. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  483. struct cxgb3i_adapter *snic;
  484. struct iscsi_endpoint *ep;
  485. struct cxgb3i_endpoint *cep;
  486. struct s3_conn *c3cn;
  487. int err;
  488. ep = iscsi_lookup_endpoint(transport_eph);
  489. if (!ep)
  490. return -EINVAL;
  491. /* setup ddp pagesize */
  492. cep = ep->dd_data;
  493. c3cn = cep->c3cn;
  494. snic = cep->hba->snic;
  495. err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
  496. if (err < 0)
  497. return err;
  498. cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
  499. ep, cls_session, cls_conn);
  500. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  501. if (err)
  502. return -EINVAL;
  503. /* calculate the tag idx bits needed for this conn based on cmds_max */
  504. cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
  505. cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
  506. conn->session->cmds_max, cconn->task_idx_bits);
  507. read_lock(&c3cn->callback_lock);
  508. c3cn->user_data = conn;
  509. cconn->hba = cep->hba;
  510. cconn->cep = cep;
  511. cep->cconn = cconn;
  512. read_unlock(&c3cn->callback_lock);
  513. cxgb3i_conn_max_xmit_dlength(conn);
  514. cxgb3i_conn_max_recv_dlength(conn);
  515. spin_lock_bh(&conn->session->lock);
  516. sprintf(conn->portal_address, "%pI4", &c3cn->daddr.sin_addr.s_addr);
  517. conn->portal_port = ntohs(c3cn->daddr.sin_port);
  518. spin_unlock_bh(&conn->session->lock);
  519. /* init recv engine */
  520. iscsi_tcp_hdr_recv_prep(tcp_conn);
  521. return 0;
  522. }
  523. /**
  524. * cxgb3i_conn_get_param - return iscsi connection parameter to caller
  525. * @cls_conn: pointer to iscsi cls conn
  526. * @param: parameter type identifier
  527. * @buf: buffer pointer
  528. *
  529. * returns iSCSI connection parameters
  530. */
  531. static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
  532. enum iscsi_param param, char *buf)
  533. {
  534. struct iscsi_conn *conn = cls_conn->dd_data;
  535. int len;
  536. cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
  537. switch (param) {
  538. case ISCSI_PARAM_CONN_PORT:
  539. spin_lock_bh(&conn->session->lock);
  540. len = sprintf(buf, "%hu\n", conn->portal_port);
  541. spin_unlock_bh(&conn->session->lock);
  542. break;
  543. case ISCSI_PARAM_CONN_ADDRESS:
  544. spin_lock_bh(&conn->session->lock);
  545. len = sprintf(buf, "%s\n", conn->portal_address);
  546. spin_unlock_bh(&conn->session->lock);
  547. break;
  548. default:
  549. return iscsi_conn_get_param(cls_conn, param, buf);
  550. }
  551. return len;
  552. }
  553. /**
  554. * cxgb3i_conn_set_param - set iscsi connection parameter
  555. * @cls_conn: pointer to iscsi cls conn
  556. * @param: parameter type identifier
  557. * @buf: buffer pointer
  558. * @buflen: buffer length
  559. *
  560. * set iSCSI connection parameters
  561. */
  562. static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
  563. enum iscsi_param param, char *buf, int buflen)
  564. {
  565. struct iscsi_conn *conn = cls_conn->dd_data;
  566. struct iscsi_session *session = conn->session;
  567. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  568. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  569. struct cxgb3i_adapter *snic = cconn->hba->snic;
  570. struct s3_conn *c3cn = cconn->cep->c3cn;
  571. int value, err = 0;
  572. switch (param) {
  573. case ISCSI_PARAM_HDRDGST_EN:
  574. err = iscsi_set_param(cls_conn, param, buf, buflen);
  575. if (!err && conn->hdrdgst_en)
  576. err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
  577. conn->hdrdgst_en,
  578. conn->datadgst_en, 0);
  579. break;
  580. case ISCSI_PARAM_DATADGST_EN:
  581. err = iscsi_set_param(cls_conn, param, buf, buflen);
  582. if (!err && conn->datadgst_en)
  583. err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
  584. conn->hdrdgst_en,
  585. conn->datadgst_en, 0);
  586. break;
  587. case ISCSI_PARAM_MAX_R2T:
  588. sscanf(buf, "%d", &value);
  589. if (value <= 0 || !is_power_of_2(value))
  590. return -EINVAL;
  591. if (session->max_r2t == value)
  592. break;
  593. iscsi_tcp_r2tpool_free(session);
  594. err = iscsi_set_param(cls_conn, param, buf, buflen);
  595. if (!err && iscsi_tcp_r2tpool_alloc(session))
  596. return -ENOMEM;
  597. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  598. err = iscsi_set_param(cls_conn, param, buf, buflen);
  599. if (!err)
  600. err = cxgb3i_conn_max_recv_dlength(conn);
  601. break;
  602. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  603. err = iscsi_set_param(cls_conn, param, buf, buflen);
  604. if (!err)
  605. err = cxgb3i_conn_max_xmit_dlength(conn);
  606. break;
  607. default:
  608. return iscsi_set_param(cls_conn, param, buf, buflen);
  609. }
  610. return err;
  611. }
  612. /**
  613. * cxgb3i_host_set_param - configure host (adapter) related parameters
  614. * @shost: scsi host pointer
  615. * @param: parameter type identifier
  616. * @buf: buffer pointer
  617. */
  618. static int cxgb3i_host_set_param(struct Scsi_Host *shost,
  619. enum iscsi_host_param param,
  620. char *buf, int buflen)
  621. {
  622. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  623. if (!hba->ndev) {
  624. shost_printk(KERN_ERR, shost, "Could not set host param. "
  625. "Netdev for host not set.\n");
  626. return -ENODEV;
  627. }
  628. cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
  629. switch (param) {
  630. case ISCSI_HOST_PARAM_IPADDRESS:
  631. {
  632. __be32 addr = in_aton(buf);
  633. cxgb3i_set_private_ipv4addr(hba->ndev, addr);
  634. return 0;
  635. }
  636. case ISCSI_HOST_PARAM_HWADDRESS:
  637. case ISCSI_HOST_PARAM_NETDEV_NAME:
  638. /* ignore */
  639. return 0;
  640. default:
  641. return iscsi_host_set_param(shost, param, buf, buflen);
  642. }
  643. }
  644. /**
  645. * cxgb3i_host_get_param - returns host (adapter) related parameters
  646. * @shost: scsi host pointer
  647. * @param: parameter type identifier
  648. * @buf: buffer pointer
  649. */
  650. static int cxgb3i_host_get_param(struct Scsi_Host *shost,
  651. enum iscsi_host_param param, char *buf)
  652. {
  653. struct cxgb3i_hba *hba = iscsi_host_priv(shost);
  654. int len = 0;
  655. if (!hba->ndev) {
  656. shost_printk(KERN_ERR, shost, "Could not set host param. "
  657. "Netdev for host not set.\n");
  658. return -ENODEV;
  659. }
  660. cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
  661. switch (param) {
  662. case ISCSI_HOST_PARAM_HWADDRESS:
  663. len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
  664. break;
  665. case ISCSI_HOST_PARAM_NETDEV_NAME:
  666. len = sprintf(buf, "%s\n", hba->ndev->name);
  667. break;
  668. case ISCSI_HOST_PARAM_IPADDRESS:
  669. {
  670. __be32 addr;
  671. addr = cxgb3i_get_private_ipv4addr(hba->ndev);
  672. len = sprintf(buf, "%pI4", &addr);
  673. break;
  674. }
  675. default:
  676. return iscsi_host_get_param(shost, param, buf);
  677. }
  678. return len;
  679. }
  680. /**
  681. * cxgb3i_conn_get_stats - returns iSCSI stats
  682. * @cls_conn: pointer to iscsi cls conn
  683. * @stats: pointer to iscsi statistic struct
  684. */
  685. static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  686. struct iscsi_stats *stats)
  687. {
  688. struct iscsi_conn *conn = cls_conn->dd_data;
  689. stats->txdata_octets = conn->txdata_octets;
  690. stats->rxdata_octets = conn->rxdata_octets;
  691. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  692. stats->dataout_pdus = conn->dataout_pdus_cnt;
  693. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  694. stats->datain_pdus = conn->datain_pdus_cnt;
  695. stats->r2t_pdus = conn->r2t_pdus_cnt;
  696. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  697. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  698. stats->digest_err = 0;
  699. stats->timeout_err = 0;
  700. stats->custom_length = 1;
  701. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  702. stats->custom[0].value = conn->eh_abort_cnt;
  703. }
  704. /**
  705. * cxgb3i_parse_itt - get the idx and age bits from a given tag
  706. * @conn: iscsi connection
  707. * @itt: itt tag
  708. * @idx: task index, filled in by this function
  709. * @age: session age, filled in by this function
  710. */
  711. static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
  712. int *idx, int *age)
  713. {
  714. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  715. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  716. struct cxgb3i_adapter *snic = cconn->hba->snic;
  717. u32 tag = ntohl((__force u32) itt);
  718. u32 sw_bits;
  719. sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
  720. if (idx)
  721. *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
  722. if (age)
  723. *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
  724. cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
  725. tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
  726. age ? *age : 0xFF);
  727. }
  728. /**
  729. * cxgb3i_reserve_itt - generate tag for a give task
  730. * @task: iscsi task
  731. * @hdr_itt: tag, filled in by this function
  732. * Set up ddp for scsi read tasks if possible.
  733. */
  734. int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
  735. {
  736. struct scsi_cmnd *sc = task->sc;
  737. struct iscsi_conn *conn = task->conn;
  738. struct iscsi_session *sess = conn->session;
  739. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  740. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  741. struct cxgb3i_adapter *snic = cconn->hba->snic;
  742. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  743. u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
  744. u32 tag;
  745. int err = -EINVAL;
  746. if (sc &&
  747. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  748. cxgb3i_sw_tag_usable(tformat, sw_tag)) {
  749. struct s3_conn *c3cn = cconn->cep->c3cn;
  750. struct cxgb3i_gather_list *gl;
  751. gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
  752. scsi_in(sc)->table.sgl,
  753. scsi_in(sc)->table.nents,
  754. snic->pdev,
  755. GFP_ATOMIC);
  756. if (gl) {
  757. tag = sw_tag;
  758. err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
  759. tformat, &tag,
  760. gl, GFP_ATOMIC);
  761. if (err < 0)
  762. cxgb3i_ddp_release_gl(gl, snic->pdev);
  763. }
  764. }
  765. if (err < 0)
  766. tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
  767. /* the itt need to sent in big-endian order */
  768. *hdr_itt = (__force itt_t)htonl(tag);
  769. cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
  770. tag, *hdr_itt, task->itt, sess->age);
  771. return 0;
  772. }
  773. /**
  774. * cxgb3i_release_itt - release the tag for a given task
  775. * @task: iscsi task
  776. * @hdr_itt: tag
  777. * If the tag is a ddp tag, release the ddp setup
  778. */
  779. void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
  780. {
  781. struct scsi_cmnd *sc = task->sc;
  782. struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
  783. struct cxgb3i_conn *cconn = tcp_conn->dd_data;
  784. struct cxgb3i_adapter *snic = cconn->hba->snic;
  785. struct cxgb3i_tag_format *tformat = &snic->tag_format;
  786. u32 tag = ntohl((__force u32)hdr_itt);
  787. cxgb3i_tag_debug("release tag 0x%x.\n", tag);
  788. if (sc &&
  789. (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
  790. cxgb3i_is_ddp_tag(tformat, tag))
  791. cxgb3i_ddp_tag_release(snic->tdev, tag);
  792. }
  793. /**
  794. * cxgb3i_host_template -- Scsi_Host_Template structure
  795. * used when registering with the scsi mid layer
  796. */
  797. static struct scsi_host_template cxgb3i_host_template = {
  798. .module = THIS_MODULE,
  799. .name = "Chelsio S3xx iSCSI Initiator",
  800. .proc_name = "cxgb3i",
  801. .queuecommand = iscsi_queuecommand,
  802. .change_queue_depth = iscsi_change_queue_depth,
  803. .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
  804. .sg_tablesize = SG_ALL,
  805. .max_sectors = 0xFFFF,
  806. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  807. .eh_abort_handler = iscsi_eh_abort,
  808. .eh_device_reset_handler = iscsi_eh_device_reset,
  809. .eh_target_reset_handler = iscsi_eh_recover_target,
  810. .target_alloc = iscsi_target_alloc,
  811. .use_clustering = DISABLE_CLUSTERING,
  812. .this_id = -1,
  813. };
  814. static struct iscsi_transport cxgb3i_iscsi_transport = {
  815. .owner = THIS_MODULE,
  816. .name = "cxgb3i",
  817. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  818. | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
  819. CAP_PADDING_OFFLOAD,
  820. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  821. ISCSI_MAX_XMIT_DLENGTH |
  822. ISCSI_HDRDGST_EN |
  823. ISCSI_DATADGST_EN |
  824. ISCSI_INITIAL_R2T_EN |
  825. ISCSI_MAX_R2T |
  826. ISCSI_IMM_DATA_EN |
  827. ISCSI_FIRST_BURST |
  828. ISCSI_MAX_BURST |
  829. ISCSI_PDU_INORDER_EN |
  830. ISCSI_DATASEQ_INORDER_EN |
  831. ISCSI_ERL |
  832. ISCSI_CONN_PORT |
  833. ISCSI_CONN_ADDRESS |
  834. ISCSI_EXP_STATSN |
  835. ISCSI_PERSISTENT_PORT |
  836. ISCSI_PERSISTENT_ADDRESS |
  837. ISCSI_TARGET_NAME | ISCSI_TPGT |
  838. ISCSI_USERNAME | ISCSI_PASSWORD |
  839. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  840. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  841. ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
  842. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  843. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  844. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  845. ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
  846. .get_host_param = cxgb3i_host_get_param,
  847. .set_host_param = cxgb3i_host_set_param,
  848. /* session management */
  849. .create_session = cxgb3i_session_create,
  850. .destroy_session = cxgb3i_session_destroy,
  851. .get_session_param = iscsi_session_get_param,
  852. /* connection management */
  853. .create_conn = cxgb3i_conn_create,
  854. .bind_conn = cxgb3i_conn_bind,
  855. .destroy_conn = iscsi_tcp_conn_teardown,
  856. .start_conn = iscsi_conn_start,
  857. .stop_conn = iscsi_conn_stop,
  858. .get_conn_param = cxgb3i_conn_get_param,
  859. .set_param = cxgb3i_conn_set_param,
  860. .get_stats = cxgb3i_conn_get_stats,
  861. /* pdu xmit req. from user space */
  862. .send_pdu = iscsi_conn_send_pdu,
  863. /* task */
  864. .init_task = iscsi_tcp_task_init,
  865. .xmit_task = iscsi_tcp_task_xmit,
  866. .cleanup_task = cxgb3i_conn_cleanup_task,
  867. /* pdu */
  868. .alloc_pdu = cxgb3i_conn_alloc_pdu,
  869. .init_pdu = cxgb3i_conn_init_pdu,
  870. .xmit_pdu = cxgb3i_conn_xmit_pdu,
  871. .parse_pdu_itt = cxgb3i_parse_itt,
  872. /* TCP connect/disconnect */
  873. .ep_connect = cxgb3i_ep_connect,
  874. .ep_poll = cxgb3i_ep_poll,
  875. .ep_disconnect = cxgb3i_ep_disconnect,
  876. /* Error recovery timeout call */
  877. .session_recovery_timedout = iscsi_session_recovery_timedout,
  878. };
  879. int cxgb3i_iscsi_init(void)
  880. {
  881. sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
  882. sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
  883. cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
  884. ISCSI_ITT_MASK, sw_tag_idx_bits,
  885. ISCSI_AGE_MASK, sw_tag_age_bits);
  886. cxgb3i_scsi_transport =
  887. iscsi_register_transport(&cxgb3i_iscsi_transport);
  888. if (!cxgb3i_scsi_transport) {
  889. cxgb3i_log_error("Could not register cxgb3i transport.\n");
  890. return -ENODEV;
  891. }
  892. cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
  893. return 0;
  894. }
  895. void cxgb3i_iscsi_cleanup(void)
  896. {
  897. if (cxgb3i_scsi_transport) {
  898. cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
  899. cxgb3i_scsi_transport);
  900. iscsi_unregister_transport(&cxgb3i_iscsi_transport);
  901. }
  902. }