be_iscsi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /**
  2. * Copyright (C) 2005 - 2010 ServerEngines
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@serverengines.com
  14. *
  15. * ServerEngines
  16. * 209 N. Fair Oaks Ave
  17. * Sunnyvale, CA 94085
  18. *
  19. */
  20. #include <scsi/libiscsi.h>
  21. #include <scsi/scsi_transport_iscsi.h>
  22. #include <scsi/scsi_transport.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi.h>
  27. #include "be_iscsi.h"
  28. extern struct iscsi_transport beiscsi_iscsi_transport;
  29. /**
  30. * beiscsi_session_create - creates a new iscsi session
  31. * @cmds_max: max commands supported
  32. * @qdepth: max queue depth supported
  33. * @initial_cmdsn: initial iscsi CMDSN
  34. */
  35. struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
  36. u16 cmds_max,
  37. u16 qdepth,
  38. u32 initial_cmdsn)
  39. {
  40. struct Scsi_Host *shost;
  41. struct beiscsi_endpoint *beiscsi_ep;
  42. struct iscsi_cls_session *cls_session;
  43. struct beiscsi_hba *phba;
  44. struct iscsi_session *sess;
  45. struct beiscsi_session *beiscsi_sess;
  46. struct beiscsi_io_task *io_task;
  47. SE_DEBUG(DBG_LVL_8, "In beiscsi_session_create\n");
  48. if (!ep) {
  49. SE_DEBUG(DBG_LVL_1, "beiscsi_session_create: invalid ep \n");
  50. return NULL;
  51. }
  52. beiscsi_ep = ep->dd_data;
  53. phba = beiscsi_ep->phba;
  54. shost = phba->shost;
  55. if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
  56. shost_printk(KERN_ERR, shost, "Cannot handle %d cmds."
  57. "Max cmds per session supported is %d. Using %d. "
  58. "\n", cmds_max,
  59. beiscsi_ep->phba->params.wrbs_per_cxn,
  60. beiscsi_ep->phba->params.wrbs_per_cxn);
  61. cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
  62. }
  63. cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
  64. shost, cmds_max,
  65. sizeof(*beiscsi_sess),
  66. sizeof(*io_task),
  67. initial_cmdsn, ISCSI_MAX_TARGET);
  68. if (!cls_session)
  69. return NULL;
  70. sess = cls_session->dd_data;
  71. beiscsi_sess = sess->dd_data;
  72. beiscsi_sess->bhs_pool = pci_pool_create("beiscsi_bhs_pool",
  73. phba->pcidev,
  74. sizeof(struct be_cmd_bhs),
  75. 64, 0);
  76. if (!beiscsi_sess->bhs_pool)
  77. goto destroy_sess;
  78. return cls_session;
  79. destroy_sess:
  80. iscsi_session_teardown(cls_session);
  81. return NULL;
  82. }
  83. /**
  84. * beiscsi_session_destroy - destroys iscsi session
  85. * @cls_session: pointer to iscsi cls session
  86. *
  87. * Destroys iSCSI session instance and releases
  88. * resources allocated for it.
  89. */
  90. void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
  91. {
  92. struct iscsi_session *sess = cls_session->dd_data;
  93. struct beiscsi_session *beiscsi_sess = sess->dd_data;
  94. SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
  95. pci_pool_destroy(beiscsi_sess->bhs_pool);
  96. iscsi_session_teardown(cls_session);
  97. }
  98. /**
  99. * beiscsi_conn_create - create an instance of iscsi connection
  100. * @cls_session: ptr to iscsi_cls_session
  101. * @cid: iscsi cid
  102. */
  103. struct iscsi_cls_conn *
  104. beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
  105. {
  106. struct beiscsi_hba *phba;
  107. struct Scsi_Host *shost;
  108. struct iscsi_cls_conn *cls_conn;
  109. struct beiscsi_conn *beiscsi_conn;
  110. struct iscsi_conn *conn;
  111. struct iscsi_session *sess;
  112. struct beiscsi_session *beiscsi_sess;
  113. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_create ,cid"
  114. "from iscsi layer=%d\n", cid);
  115. shost = iscsi_session_to_shost(cls_session);
  116. phba = iscsi_host_priv(shost);
  117. cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
  118. if (!cls_conn)
  119. return NULL;
  120. conn = cls_conn->dd_data;
  121. beiscsi_conn = conn->dd_data;
  122. beiscsi_conn->ep = NULL;
  123. beiscsi_conn->phba = phba;
  124. beiscsi_conn->conn = conn;
  125. sess = cls_session->dd_data;
  126. beiscsi_sess = sess->dd_data;
  127. beiscsi_conn->beiscsi_sess = beiscsi_sess;
  128. return cls_conn;
  129. }
  130. /**
  131. * beiscsi_bindconn_cid - Bind the beiscsi_conn with phba connection table
  132. * @beiscsi_conn: The pointer to beiscsi_conn structure
  133. * @phba: The phba instance
  134. * @cid: The cid to free
  135. */
  136. static int beiscsi_bindconn_cid(struct beiscsi_hba *phba,
  137. struct beiscsi_conn *beiscsi_conn,
  138. unsigned int cid)
  139. {
  140. if (phba->conn_table[cid]) {
  141. SE_DEBUG(DBG_LVL_1,
  142. "Connection table already occupied. Detected clash\n");
  143. return -EINVAL;
  144. } else {
  145. SE_DEBUG(DBG_LVL_8, "phba->conn_table[%d]=%p(beiscsi_conn) \n",
  146. cid, beiscsi_conn);
  147. phba->conn_table[cid] = beiscsi_conn;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
  153. * @cls_session: pointer to iscsi cls session
  154. * @cls_conn: pointer to iscsi cls conn
  155. * @transport_fd: EP handle(64 bit)
  156. *
  157. * This function binds the TCP Conn with iSCSI Connection and Session.
  158. */
  159. int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
  160. struct iscsi_cls_conn *cls_conn,
  161. u64 transport_fd, int is_leading)
  162. {
  163. struct iscsi_conn *conn = cls_conn->dd_data;
  164. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  165. struct Scsi_Host *shost =
  166. (struct Scsi_Host *)iscsi_session_to_shost(cls_session);
  167. struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
  168. struct beiscsi_endpoint *beiscsi_ep;
  169. struct iscsi_endpoint *ep;
  170. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_bind\n");
  171. ep = iscsi_lookup_endpoint(transport_fd);
  172. if (!ep)
  173. return -EINVAL;
  174. beiscsi_ep = ep->dd_data;
  175. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  176. return -EINVAL;
  177. if (beiscsi_ep->phba != phba) {
  178. SE_DEBUG(DBG_LVL_8,
  179. "beiscsi_ep->hba=%p not equal to phba=%p \n",
  180. beiscsi_ep->phba, phba);
  181. return -EEXIST;
  182. }
  183. beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
  184. beiscsi_conn->ep = beiscsi_ep;
  185. beiscsi_ep->conn = beiscsi_conn;
  186. SE_DEBUG(DBG_LVL_8, "beiscsi_conn=%p conn=%p ep_cid=%d \n",
  187. beiscsi_conn, conn, beiscsi_ep->ep_cid);
  188. return beiscsi_bindconn_cid(phba, beiscsi_conn, beiscsi_ep->ep_cid);
  189. }
  190. /**
  191. * beiscsi_conn_get_param - get the iscsi parameter
  192. * @cls_conn: pointer to iscsi cls conn
  193. * @param: parameter type identifier
  194. * @buf: buffer pointer
  195. *
  196. * returns iscsi parameter
  197. */
  198. int beiscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  199. enum iscsi_param param, char *buf)
  200. {
  201. struct beiscsi_endpoint *beiscsi_ep;
  202. struct iscsi_conn *conn = cls_conn->dd_data;
  203. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  204. int len = 0;
  205. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
  206. beiscsi_ep = beiscsi_conn->ep;
  207. if (!beiscsi_ep) {
  208. SE_DEBUG(DBG_LVL_1,
  209. "In beiscsi_conn_get_param , no beiscsi_ep\n");
  210. return -1;
  211. }
  212. switch (param) {
  213. case ISCSI_PARAM_CONN_PORT:
  214. len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
  215. break;
  216. case ISCSI_PARAM_CONN_ADDRESS:
  217. if (beiscsi_ep->ip_type == BE2_IPV4)
  218. len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
  219. else
  220. len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
  221. break;
  222. default:
  223. return iscsi_conn_get_param(cls_conn, param, buf);
  224. }
  225. return len;
  226. }
  227. int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
  228. enum iscsi_param param, char *buf, int buflen)
  229. {
  230. struct iscsi_conn *conn = cls_conn->dd_data;
  231. struct iscsi_session *session = conn->session;
  232. int ret;
  233. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
  234. ret = iscsi_set_param(cls_conn, param, buf, buflen);
  235. if (ret)
  236. return ret;
  237. /*
  238. * If userspace tried to set the value to higher than we can
  239. * support override here.
  240. */
  241. switch (param) {
  242. case ISCSI_PARAM_FIRST_BURST:
  243. if (session->first_burst > 8192)
  244. session->first_burst = 8192;
  245. break;
  246. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  247. if (conn->max_recv_dlength > 65536)
  248. conn->max_recv_dlength = 65536;
  249. break;
  250. case ISCSI_PARAM_MAX_BURST:
  251. if (session->max_burst > 262144)
  252. session->max_burst = 262144;
  253. break;
  254. default:
  255. return 0;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * beiscsi_get_host_param - get the iscsi parameter
  261. * @shost: pointer to scsi_host structure
  262. * @param: parameter type identifier
  263. * @buf: buffer pointer
  264. *
  265. * returns host parameter
  266. */
  267. int beiscsi_get_host_param(struct Scsi_Host *shost,
  268. enum iscsi_host_param param, char *buf)
  269. {
  270. struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
  271. struct be_cmd_resp_get_mac_addr *resp;
  272. struct be_mcc_wrb *wrb;
  273. unsigned int tag, wrb_num;
  274. int len = 0;
  275. unsigned short status, extd_status;
  276. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  277. SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
  278. switch (param) {
  279. case ISCSI_HOST_PARAM_HWADDRESS:
  280. tag = be_cmd_get_mac_addr(phba);
  281. if (!tag) {
  282. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed \n");
  283. return -1;
  284. } else
  285. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  286. phba->ctrl.mcc_numtag[tag]);
  287. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  288. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  289. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  290. if (status || extd_status) {
  291. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
  292. " status = %d extd_status = %d \n",
  293. status, extd_status);
  294. free_mcc_tag(&phba->ctrl, tag);
  295. return -1;
  296. } else {
  297. wrb = queue_get_wrb(mccq, wrb_num);
  298. free_mcc_tag(&phba->ctrl, tag);
  299. resp = embedded_payload(wrb);
  300. memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
  301. len = sysfs_format_mac(buf, phba->mac_address,
  302. ETH_ALEN);
  303. }
  304. break;
  305. default:
  306. return iscsi_host_get_param(shost, param, buf);
  307. }
  308. return len;
  309. }
  310. /**
  311. * beiscsi_conn_get_stats - get the iscsi stats
  312. * @cls_conn: pointer to iscsi cls conn
  313. * @stats: pointer to iscsi_stats structure
  314. *
  315. * returns iscsi stats
  316. */
  317. void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  318. struct iscsi_stats *stats)
  319. {
  320. struct iscsi_conn *conn = cls_conn->dd_data;
  321. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
  322. stats->txdata_octets = conn->txdata_octets;
  323. stats->rxdata_octets = conn->rxdata_octets;
  324. stats->dataout_pdus = conn->dataout_pdus_cnt;
  325. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  326. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  327. stats->datain_pdus = conn->datain_pdus_cnt;
  328. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  329. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  330. stats->r2t_pdus = conn->r2t_pdus_cnt;
  331. stats->digest_err = 0;
  332. stats->timeout_err = 0;
  333. stats->custom_length = 0;
  334. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  335. stats->custom[0].value = conn->eh_abort_cnt;
  336. }
  337. /**
  338. * beiscsi_set_params_for_offld - get the parameters for offload
  339. * @beiscsi_conn: pointer to beiscsi_conn
  340. * @params: pointer to offload_params structure
  341. */
  342. static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
  343. struct beiscsi_offload_params *params)
  344. {
  345. struct iscsi_conn *conn = beiscsi_conn->conn;
  346. struct iscsi_session *session = conn->session;
  347. AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
  348. params, session->max_burst);
  349. AMAP_SET_BITS(struct amap_beiscsi_offload_params,
  350. max_send_data_segment_length, params,
  351. conn->max_xmit_dlength);
  352. AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
  353. params, session->first_burst);
  354. AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
  355. session->erl);
  356. AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
  357. conn->datadgst_en);
  358. AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
  359. conn->hdrdgst_en);
  360. AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
  361. session->initial_r2t_en);
  362. AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
  363. session->imm_data_en);
  364. AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
  365. (conn->exp_statsn - 1));
  366. }
  367. /**
  368. * beiscsi_conn_start - offload of session to chip
  369. * @cls_conn: pointer to beiscsi_conn
  370. */
  371. int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  372. {
  373. struct iscsi_conn *conn = cls_conn->dd_data;
  374. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  375. struct beiscsi_endpoint *beiscsi_ep;
  376. struct beiscsi_offload_params params;
  377. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
  378. memset(&params, 0, sizeof(struct beiscsi_offload_params));
  379. beiscsi_ep = beiscsi_conn->ep;
  380. if (!beiscsi_ep)
  381. SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
  382. beiscsi_conn->login_in_progress = 0;
  383. beiscsi_set_params_for_offld(beiscsi_conn, &params);
  384. beiscsi_offload_connection(beiscsi_conn, &params);
  385. iscsi_conn_start(cls_conn);
  386. return 0;
  387. }
  388. /**
  389. * beiscsi_get_cid - Allocate a cid
  390. * @phba: The phba instance
  391. */
  392. static int beiscsi_get_cid(struct beiscsi_hba *phba)
  393. {
  394. unsigned short cid = 0xFFFF;
  395. if (!phba->avlbl_cids)
  396. return cid;
  397. cid = phba->cid_array[phba->cid_alloc++];
  398. if (phba->cid_alloc == phba->params.cxns_per_ctrl)
  399. phba->cid_alloc = 0;
  400. phba->avlbl_cids--;
  401. return cid;
  402. }
  403. /**
  404. * beiscsi_open_conn - Ask FW to open a TCP connection
  405. * @ep: endpoint to be used
  406. * @src_addr: The source IP address
  407. * @dst_addr: The Destination IP address
  408. *
  409. * Asks the FW to open a TCP connection
  410. */
  411. static int beiscsi_open_conn(struct iscsi_endpoint *ep,
  412. struct sockaddr *src_addr,
  413. struct sockaddr *dst_addr, int non_blocking)
  414. {
  415. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  416. struct beiscsi_hba *phba = beiscsi_ep->phba;
  417. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  418. struct be_mcc_wrb *wrb;
  419. struct tcp_connect_and_offload_out *ptcpcnct_out;
  420. unsigned short status, extd_status;
  421. unsigned int tag, wrb_num;
  422. int ret = -1;
  423. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
  424. beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
  425. if (beiscsi_ep->ep_cid == 0xFFFF) {
  426. SE_DEBUG(DBG_LVL_1, "No free cid available\n");
  427. return ret;
  428. }
  429. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d ",
  430. beiscsi_ep->ep_cid);
  431. phba->ep_array[beiscsi_ep->ep_cid -
  432. phba->fw_config.iscsi_cid_start] = ep;
  433. if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
  434. phba->params.cxns_per_ctrl * 2)) {
  435. SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
  436. return ret;
  437. }
  438. beiscsi_ep->cid_vld = 0;
  439. tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep);
  440. if (!tag) {
  441. SE_DEBUG(DBG_LVL_1,
  442. "mgmt_invalidate_connection Failed for cid=%d \n",
  443. beiscsi_ep->ep_cid);
  444. } else {
  445. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  446. phba->ctrl.mcc_numtag[tag]);
  447. }
  448. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  449. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  450. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  451. if (status || extd_status) {
  452. SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
  453. " status = %d extd_status = %d \n",
  454. status, extd_status);
  455. free_mcc_tag(&phba->ctrl, tag);
  456. return -1;
  457. } else {
  458. wrb = queue_get_wrb(mccq, wrb_num);
  459. free_mcc_tag(&phba->ctrl, tag);
  460. ptcpcnct_out = embedded_payload(wrb);
  461. beiscsi_ep = ep->dd_data;
  462. beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
  463. beiscsi_ep->cid_vld = 1;
  464. SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
  465. }
  466. return 0;
  467. }
  468. /**
  469. * beiscsi_put_cid - Free the cid
  470. * @phba: The phba for which the cid is being freed
  471. * @cid: The cid to free
  472. */
  473. static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
  474. {
  475. phba->avlbl_cids++;
  476. phba->cid_array[phba->cid_free++] = cid;
  477. if (phba->cid_free == phba->params.cxns_per_ctrl)
  478. phba->cid_free = 0;
  479. }
  480. /**
  481. * beiscsi_free_ep - free endpoint
  482. * @ep: pointer to iscsi endpoint structure
  483. */
  484. static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
  485. {
  486. struct beiscsi_hba *phba = beiscsi_ep->phba;
  487. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  488. beiscsi_ep->phba = NULL;
  489. }
  490. /**
  491. * beiscsi_ep_connect - Ask chip to create TCP Conn
  492. * @scsi_host: Pointer to scsi_host structure
  493. * @dst_addr: The IP address of Target
  494. * @non_blocking: blocking or non-blocking call
  495. *
  496. * This routines first asks chip to create a connection and then allocates an EP
  497. */
  498. struct iscsi_endpoint *
  499. beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  500. int non_blocking)
  501. {
  502. struct beiscsi_hba *phba;
  503. struct beiscsi_endpoint *beiscsi_ep;
  504. struct iscsi_endpoint *ep;
  505. int ret;
  506. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect \n");
  507. if (shost)
  508. phba = iscsi_host_priv(shost);
  509. else {
  510. ret = -ENXIO;
  511. SE_DEBUG(DBG_LVL_1, "shost is NULL \n");
  512. return ERR_PTR(ret);
  513. }
  514. if (phba->state != BE_ADAPTER_UP) {
  515. ret = -EBUSY;
  516. SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP \n");
  517. return ERR_PTR(ret);
  518. }
  519. ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
  520. if (!ep) {
  521. ret = -ENOMEM;
  522. return ERR_PTR(ret);
  523. }
  524. beiscsi_ep = ep->dd_data;
  525. beiscsi_ep->phba = phba;
  526. beiscsi_ep->openiscsi_ep = ep;
  527. if (beiscsi_open_conn(ep, NULL, dst_addr, non_blocking)) {
  528. SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn \n");
  529. ret = -ENOMEM;
  530. goto free_ep;
  531. }
  532. return ep;
  533. free_ep:
  534. beiscsi_free_ep(beiscsi_ep);
  535. return ERR_PTR(ret);
  536. }
  537. /**
  538. * beiscsi_ep_poll - Poll to see if connection is established
  539. * @ep: endpoint to be used
  540. * @timeout_ms: timeout specified in millisecs
  541. *
  542. * Poll to see if TCP connection established
  543. */
  544. int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  545. {
  546. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  547. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
  548. if (beiscsi_ep->cid_vld == 1)
  549. return 1;
  550. else
  551. return 0;
  552. }
  553. /**
  554. * beiscsi_close_conn - Upload the connection
  555. * @ep: The iscsi endpoint
  556. * @flag: The type of connection closure
  557. */
  558. static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
  559. {
  560. int ret = 0;
  561. unsigned int tag;
  562. struct beiscsi_hba *phba = beiscsi_ep->phba;
  563. tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
  564. if (!tag) {
  565. SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x",
  566. beiscsi_ep->ep_cid);
  567. ret = -1;
  568. } else {
  569. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  570. phba->ctrl.mcc_numtag[tag]);
  571. free_mcc_tag(&phba->ctrl, tag);
  572. }
  573. return ret;
  574. }
  575. /**
  576. * beiscsi_ep_disconnect - Tears down the TCP connection
  577. * @ep: endpoint to be used
  578. *
  579. * Tears down the TCP connection
  580. */
  581. void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
  582. {
  583. struct beiscsi_conn *beiscsi_conn;
  584. struct beiscsi_endpoint *beiscsi_ep;
  585. struct beiscsi_hba *phba;
  586. beiscsi_ep = ep->dd_data;
  587. phba = beiscsi_ep->phba;
  588. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
  589. beiscsi_ep->ep_cid);
  590. if (beiscsi_ep->conn) {
  591. beiscsi_conn = beiscsi_ep->conn;
  592. iscsi_suspend_queue(beiscsi_conn->conn);
  593. }
  594. }
  595. /**
  596. * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
  597. * @phba: The phba instance
  598. * @cid: The cid to free
  599. */
  600. static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
  601. unsigned int cid)
  602. {
  603. if (phba->conn_table[cid])
  604. phba->conn_table[cid] = NULL;
  605. else {
  606. SE_DEBUG(DBG_LVL_8, "Connection table Not occupied. \n");
  607. return -EINVAL;
  608. }
  609. return 0;
  610. }
  611. /**
  612. * beiscsi_conn_stop - Invalidate and stop the connection
  613. * @cls_conn: pointer to get iscsi_conn
  614. * @flag: The type of connection closure
  615. */
  616. void beiscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  617. {
  618. struct iscsi_conn *conn = cls_conn->dd_data;
  619. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  620. struct beiscsi_endpoint *beiscsi_ep;
  621. struct iscsi_session *session = conn->session;
  622. struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
  623. struct beiscsi_hba *phba = iscsi_host_priv(shost);
  624. unsigned int tag;
  625. unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
  626. beiscsi_ep = beiscsi_conn->ep;
  627. if (!beiscsi_ep) {
  628. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_stop , no beiscsi_ep\n");
  629. return;
  630. }
  631. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_stop ep_cid = %d\n",
  632. beiscsi_ep->ep_cid);
  633. tag = mgmt_invalidate_connection(phba, beiscsi_ep,
  634. beiscsi_ep->ep_cid, 1,
  635. savecfg_flag);
  636. if (!tag) {
  637. SE_DEBUG(DBG_LVL_1,
  638. "mgmt_invalidate_connection Failed for cid=%d \n",
  639. beiscsi_ep->ep_cid);
  640. } else {
  641. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  642. phba->ctrl.mcc_numtag[tag]);
  643. free_mcc_tag(&phba->ctrl, tag);
  644. }
  645. beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
  646. beiscsi_free_ep(beiscsi_ep);
  647. iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
  648. beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
  649. iscsi_conn_stop(cls_conn, flag);
  650. }