be_iscsi.c 20 KB

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