be_iscsi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 -ENODEV;
  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. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  255. if ((conn->max_xmit_dlength > 65536) ||
  256. (conn->max_xmit_dlength == 0))
  257. conn->max_xmit_dlength = 65536;
  258. default:
  259. return 0;
  260. }
  261. return 0;
  262. }
  263. /**
  264. * beiscsi_get_host_param - get the iscsi parameter
  265. * @shost: pointer to scsi_host structure
  266. * @param: parameter type identifier
  267. * @buf: buffer pointer
  268. *
  269. * returns host parameter
  270. */
  271. int beiscsi_get_host_param(struct Scsi_Host *shost,
  272. enum iscsi_host_param param, char *buf)
  273. {
  274. struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
  275. struct be_cmd_resp_get_mac_addr *resp;
  276. struct be_mcc_wrb *wrb;
  277. unsigned int tag, wrb_num;
  278. int len = 0;
  279. unsigned short status, extd_status;
  280. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  281. SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
  282. switch (param) {
  283. case ISCSI_HOST_PARAM_HWADDRESS:
  284. tag = be_cmd_get_mac_addr(phba);
  285. if (!tag) {
  286. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
  287. return -EAGAIN;
  288. } else
  289. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  290. phba->ctrl.mcc_numtag[tag]);
  291. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  292. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  293. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  294. if (status || extd_status) {
  295. SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
  296. " status = %d extd_status = %d\n",
  297. status, extd_status);
  298. free_mcc_tag(&phba->ctrl, tag);
  299. return -EAGAIN;
  300. } else {
  301. wrb = queue_get_wrb(mccq, wrb_num);
  302. free_mcc_tag(&phba->ctrl, tag);
  303. resp = embedded_payload(wrb);
  304. memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
  305. len = sysfs_format_mac(buf, phba->mac_address,
  306. ETH_ALEN);
  307. }
  308. break;
  309. default:
  310. return iscsi_host_get_param(shost, param, buf);
  311. }
  312. return len;
  313. }
  314. /**
  315. * beiscsi_conn_get_stats - get the iscsi stats
  316. * @cls_conn: pointer to iscsi cls conn
  317. * @stats: pointer to iscsi_stats structure
  318. *
  319. * returns iscsi stats
  320. */
  321. void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  322. struct iscsi_stats *stats)
  323. {
  324. struct iscsi_conn *conn = cls_conn->dd_data;
  325. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
  326. stats->txdata_octets = conn->txdata_octets;
  327. stats->rxdata_octets = conn->rxdata_octets;
  328. stats->dataout_pdus = conn->dataout_pdus_cnt;
  329. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  330. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  331. stats->datain_pdus = conn->datain_pdus_cnt;
  332. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  333. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  334. stats->r2t_pdus = conn->r2t_pdus_cnt;
  335. stats->digest_err = 0;
  336. stats->timeout_err = 0;
  337. stats->custom_length = 0;
  338. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  339. stats->custom[0].value = conn->eh_abort_cnt;
  340. }
  341. /**
  342. * beiscsi_set_params_for_offld - get the parameters for offload
  343. * @beiscsi_conn: pointer to beiscsi_conn
  344. * @params: pointer to offload_params structure
  345. */
  346. static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
  347. struct beiscsi_offload_params *params)
  348. {
  349. struct iscsi_conn *conn = beiscsi_conn->conn;
  350. struct iscsi_session *session = conn->session;
  351. AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
  352. params, session->max_burst);
  353. AMAP_SET_BITS(struct amap_beiscsi_offload_params,
  354. max_send_data_segment_length, params,
  355. conn->max_xmit_dlength);
  356. AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
  357. params, session->first_burst);
  358. AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
  359. session->erl);
  360. AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
  361. conn->datadgst_en);
  362. AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
  363. conn->hdrdgst_en);
  364. AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
  365. session->initial_r2t_en);
  366. AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
  367. session->imm_data_en);
  368. AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
  369. (conn->exp_statsn - 1));
  370. }
  371. /**
  372. * beiscsi_conn_start - offload of session to chip
  373. * @cls_conn: pointer to beiscsi_conn
  374. */
  375. int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  376. {
  377. struct iscsi_conn *conn = cls_conn->dd_data;
  378. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  379. struct beiscsi_endpoint *beiscsi_ep;
  380. struct beiscsi_offload_params params;
  381. SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
  382. memset(&params, 0, sizeof(struct beiscsi_offload_params));
  383. beiscsi_ep = beiscsi_conn->ep;
  384. if (!beiscsi_ep)
  385. SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
  386. beiscsi_conn->login_in_progress = 0;
  387. beiscsi_set_params_for_offld(beiscsi_conn, &params);
  388. beiscsi_offload_connection(beiscsi_conn, &params);
  389. iscsi_conn_start(cls_conn);
  390. return 0;
  391. }
  392. /**
  393. * beiscsi_get_cid - Allocate a cid
  394. * @phba: The phba instance
  395. */
  396. static int beiscsi_get_cid(struct beiscsi_hba *phba)
  397. {
  398. unsigned short cid = 0xFFFF;
  399. if (!phba->avlbl_cids)
  400. return cid;
  401. cid = phba->cid_array[phba->cid_alloc++];
  402. if (phba->cid_alloc == phba->params.cxns_per_ctrl)
  403. phba->cid_alloc = 0;
  404. phba->avlbl_cids--;
  405. return cid;
  406. }
  407. /**
  408. * beiscsi_put_cid - Free the cid
  409. * @phba: The phba for which the cid is being freed
  410. * @cid: The cid to free
  411. */
  412. static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
  413. {
  414. phba->avlbl_cids++;
  415. phba->cid_array[phba->cid_free++] = cid;
  416. if (phba->cid_free == phba->params.cxns_per_ctrl)
  417. phba->cid_free = 0;
  418. }
  419. /**
  420. * beiscsi_free_ep - free endpoint
  421. * @ep: pointer to iscsi endpoint structure
  422. */
  423. static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
  424. {
  425. struct beiscsi_hba *phba = beiscsi_ep->phba;
  426. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  427. beiscsi_ep->phba = NULL;
  428. }
  429. /**
  430. * beiscsi_open_conn - Ask FW to open a TCP connection
  431. * @ep: endpoint to be used
  432. * @src_addr: The source IP address
  433. * @dst_addr: The Destination IP address
  434. *
  435. * Asks the FW to open a TCP connection
  436. */
  437. static int beiscsi_open_conn(struct iscsi_endpoint *ep,
  438. struct sockaddr *src_addr,
  439. struct sockaddr *dst_addr, int non_blocking)
  440. {
  441. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  442. struct beiscsi_hba *phba = beiscsi_ep->phba;
  443. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  444. struct be_mcc_wrb *wrb;
  445. struct tcp_connect_and_offload_out *ptcpcnct_out;
  446. unsigned short status, extd_status;
  447. struct be_dma_mem nonemb_cmd;
  448. unsigned int tag, wrb_num;
  449. int ret = -ENOMEM;
  450. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
  451. beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
  452. if (beiscsi_ep->ep_cid == 0xFFFF) {
  453. SE_DEBUG(DBG_LVL_1, "No free cid available\n");
  454. return ret;
  455. }
  456. SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
  457. beiscsi_ep->ep_cid);
  458. phba->ep_array[beiscsi_ep->ep_cid -
  459. phba->fw_config.iscsi_cid_start] = ep;
  460. if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
  461. phba->params.cxns_per_ctrl * 2)) {
  462. SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
  463. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  464. goto free_ep;
  465. }
  466. beiscsi_ep->cid_vld = 0;
  467. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  468. sizeof(struct tcp_connect_and_offload_in),
  469. &nonemb_cmd.dma);
  470. if (nonemb_cmd.va == NULL) {
  471. SE_DEBUG(DBG_LVL_1,
  472. "Failed to allocate memory for mgmt_open_connection"
  473. "\n");
  474. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  475. return -ENOMEM;
  476. }
  477. nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
  478. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  479. tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
  480. if (!tag) {
  481. SE_DEBUG(DBG_LVL_1,
  482. "mgmt_open_connection Failed for cid=%d\n",
  483. beiscsi_ep->ep_cid);
  484. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  485. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  486. nonemb_cmd.va, nonemb_cmd.dma);
  487. return -EAGAIN;
  488. } else {
  489. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  490. phba->ctrl.mcc_numtag[tag]);
  491. }
  492. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  493. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  494. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  495. if (status || extd_status) {
  496. SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
  497. " status = %d extd_status = %d\n",
  498. status, extd_status);
  499. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  500. free_mcc_tag(&phba->ctrl, tag);
  501. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  502. nonemb_cmd.va, nonemb_cmd.dma);
  503. goto free_ep;
  504. } else {
  505. wrb = queue_get_wrb(mccq, wrb_num);
  506. free_mcc_tag(&phba->ctrl, tag);
  507. ptcpcnct_out = embedded_payload(wrb);
  508. beiscsi_ep = ep->dd_data;
  509. beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
  510. beiscsi_ep->cid_vld = 1;
  511. SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
  512. }
  513. beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
  514. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  515. nonemb_cmd.va, nonemb_cmd.dma);
  516. return 0;
  517. free_ep:
  518. beiscsi_free_ep(beiscsi_ep);
  519. return -EBUSY;
  520. }
  521. /**
  522. * beiscsi_ep_connect - Ask chip to create TCP Conn
  523. * @scsi_host: Pointer to scsi_host structure
  524. * @dst_addr: The IP address of Target
  525. * @non_blocking: blocking or non-blocking call
  526. *
  527. * This routines first asks chip to create a connection and then allocates an EP
  528. */
  529. struct iscsi_endpoint *
  530. beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  531. int non_blocking)
  532. {
  533. struct beiscsi_hba *phba;
  534. struct beiscsi_endpoint *beiscsi_ep;
  535. struct iscsi_endpoint *ep;
  536. int ret;
  537. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
  538. if (shost)
  539. phba = iscsi_host_priv(shost);
  540. else {
  541. ret = -ENXIO;
  542. SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
  543. return ERR_PTR(ret);
  544. }
  545. if (phba->state != BE_ADAPTER_UP) {
  546. ret = -EBUSY;
  547. SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
  548. return ERR_PTR(ret);
  549. }
  550. ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
  551. if (!ep) {
  552. ret = -ENOMEM;
  553. return ERR_PTR(ret);
  554. }
  555. beiscsi_ep = ep->dd_data;
  556. beiscsi_ep->phba = phba;
  557. beiscsi_ep->openiscsi_ep = ep;
  558. ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
  559. if (ret) {
  560. SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
  561. goto free_ep;
  562. }
  563. return ep;
  564. free_ep:
  565. iscsi_destroy_endpoint(ep);
  566. return ERR_PTR(ret);
  567. }
  568. /**
  569. * beiscsi_ep_poll - Poll to see if connection is established
  570. * @ep: endpoint to be used
  571. * @timeout_ms: timeout specified in millisecs
  572. *
  573. * Poll to see if TCP connection established
  574. */
  575. int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  576. {
  577. struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
  578. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
  579. if (beiscsi_ep->cid_vld == 1)
  580. return 1;
  581. else
  582. return 0;
  583. }
  584. /**
  585. * beiscsi_close_conn - Upload the connection
  586. * @ep: The iscsi endpoint
  587. * @flag: The type of connection closure
  588. */
  589. static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
  590. {
  591. int ret = 0;
  592. unsigned int tag;
  593. struct beiscsi_hba *phba = beiscsi_ep->phba;
  594. tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
  595. if (!tag) {
  596. SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
  597. beiscsi_ep->ep_cid);
  598. ret = -EAGAIN;
  599. } else {
  600. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  601. phba->ctrl.mcc_numtag[tag]);
  602. free_mcc_tag(&phba->ctrl, tag);
  603. }
  604. return ret;
  605. }
  606. /**
  607. * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
  608. * @phba: The phba instance
  609. * @cid: The cid to free
  610. */
  611. static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
  612. unsigned int cid)
  613. {
  614. if (phba->conn_table[cid])
  615. phba->conn_table[cid] = NULL;
  616. else {
  617. SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
  618. return -EINVAL;
  619. }
  620. return 0;
  621. }
  622. /**
  623. * beiscsi_ep_disconnect - Tears down the TCP connection
  624. * @ep: endpoint to be used
  625. *
  626. * Tears down the TCP connection
  627. */
  628. void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
  629. {
  630. struct beiscsi_conn *beiscsi_conn;
  631. struct beiscsi_endpoint *beiscsi_ep;
  632. struct beiscsi_hba *phba;
  633. unsigned int tag;
  634. unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
  635. beiscsi_ep = ep->dd_data;
  636. phba = beiscsi_ep->phba;
  637. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
  638. beiscsi_ep->ep_cid);
  639. if (!beiscsi_ep->conn) {
  640. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
  641. "beiscsi_ep\n");
  642. return;
  643. }
  644. beiscsi_conn = beiscsi_ep->conn;
  645. iscsi_suspend_queue(beiscsi_conn->conn);
  646. SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
  647. beiscsi_ep->ep_cid);
  648. tag = mgmt_invalidate_connection(phba, beiscsi_ep,
  649. beiscsi_ep->ep_cid, 1,
  650. savecfg_flag);
  651. if (!tag) {
  652. SE_DEBUG(DBG_LVL_1,
  653. "mgmt_invalidate_connection Failed for cid=%d\n",
  654. beiscsi_ep->ep_cid);
  655. } else {
  656. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  657. phba->ctrl.mcc_numtag[tag]);
  658. free_mcc_tag(&phba->ctrl, tag);
  659. }
  660. beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
  661. beiscsi_free_ep(beiscsi_ep);
  662. beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
  663. iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
  664. }