bnx2i_iscsi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. /*
  2. * bnx2i_iscsi.c: Broadcom NetXtreme II iSCSI driver.
  3. *
  4. * Copyright (c) 2006 - 2009 Broadcom Corporation
  5. * Copyright (c) 2007, 2008 Red Hat, Inc. All rights reserved.
  6. * Copyright (c) 2007, 2008 Mike Christie
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. *
  12. * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
  13. */
  14. #include <scsi/scsi_tcq.h>
  15. #include <scsi/libiscsi.h>
  16. #include "bnx2i.h"
  17. struct scsi_transport_template *bnx2i_scsi_xport_template;
  18. struct iscsi_transport bnx2i_iscsi_transport;
  19. static struct scsi_host_template bnx2i_host_template;
  20. /*
  21. * Global endpoint resource info
  22. */
  23. static DEFINE_SPINLOCK(bnx2i_resc_lock); /* protects global resources */
  24. static int bnx2i_adapter_ready(struct bnx2i_hba *hba)
  25. {
  26. int retval = 0;
  27. if (!hba || !test_bit(ADAPTER_STATE_UP, &hba->adapter_state) ||
  28. test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
  29. test_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state))
  30. retval = -EPERM;
  31. return retval;
  32. }
  33. /**
  34. * bnx2i_get_write_cmd_bd_idx - identifies various BD bookmarks
  35. * @cmd: iscsi cmd struct pointer
  36. * @buf_off: absolute buffer offset
  37. * @start_bd_off: u32 pointer to return the offset within the BD
  38. * indicated by 'start_bd_idx' on which 'buf_off' falls
  39. * @start_bd_idx: index of the BD on which 'buf_off' falls
  40. *
  41. * identifies & marks various bd info for scsi command's imm data,
  42. * unsolicited data and the first solicited data seq.
  43. */
  44. static void bnx2i_get_write_cmd_bd_idx(struct bnx2i_cmd *cmd, u32 buf_off,
  45. u32 *start_bd_off, u32 *start_bd_idx)
  46. {
  47. struct iscsi_bd *bd_tbl = cmd->io_tbl.bd_tbl;
  48. u32 cur_offset = 0;
  49. u32 cur_bd_idx = 0;
  50. if (buf_off) {
  51. while (buf_off >= (cur_offset + bd_tbl->buffer_length)) {
  52. cur_offset += bd_tbl->buffer_length;
  53. cur_bd_idx++;
  54. bd_tbl++;
  55. }
  56. }
  57. *start_bd_off = buf_off - cur_offset;
  58. *start_bd_idx = cur_bd_idx;
  59. }
  60. /**
  61. * bnx2i_setup_write_cmd_bd_info - sets up BD various information
  62. * @task: transport layer's cmd struct pointer
  63. *
  64. * identifies & marks various bd info for scsi command's immediate data,
  65. * unsolicited data and first solicited data seq which includes BD start
  66. * index & BD buf off. his function takes into account iscsi parameter such
  67. * as immediate data and unsolicited data is support on this connection.
  68. */
  69. static void bnx2i_setup_write_cmd_bd_info(struct iscsi_task *task)
  70. {
  71. struct bnx2i_cmd *cmd = task->dd_data;
  72. u32 start_bd_offset;
  73. u32 start_bd_idx;
  74. u32 buffer_offset = 0;
  75. u32 cmd_len = cmd->req.total_data_transfer_length;
  76. /* if ImmediateData is turned off & IntialR2T is turned on,
  77. * there will be no immediate or unsolicited data, just return.
  78. */
  79. if (!iscsi_task_has_unsol_data(task) && !task->imm_count)
  80. return;
  81. /* Immediate data */
  82. buffer_offset += task->imm_count;
  83. if (task->imm_count == cmd_len)
  84. return;
  85. if (iscsi_task_has_unsol_data(task)) {
  86. bnx2i_get_write_cmd_bd_idx(cmd, buffer_offset,
  87. &start_bd_offset, &start_bd_idx);
  88. cmd->req.ud_buffer_offset = start_bd_offset;
  89. cmd->req.ud_start_bd_index = start_bd_idx;
  90. buffer_offset += task->unsol_r2t.data_length;
  91. }
  92. if (buffer_offset != cmd_len) {
  93. bnx2i_get_write_cmd_bd_idx(cmd, buffer_offset,
  94. &start_bd_offset, &start_bd_idx);
  95. if ((start_bd_offset > task->conn->session->first_burst) ||
  96. (start_bd_idx > scsi_sg_count(cmd->scsi_cmd))) {
  97. int i = 0;
  98. iscsi_conn_printk(KERN_ALERT, task->conn,
  99. "bnx2i- error, buf offset 0x%x "
  100. "bd_valid %d use_sg %d\n",
  101. buffer_offset, cmd->io_tbl.bd_valid,
  102. scsi_sg_count(cmd->scsi_cmd));
  103. for (i = 0; i < cmd->io_tbl.bd_valid; i++)
  104. iscsi_conn_printk(KERN_ALERT, task->conn,
  105. "bnx2i err, bd[%d]: len %x\n",
  106. i, cmd->io_tbl.bd_tbl[i].\
  107. buffer_length);
  108. }
  109. cmd->req.sd_buffer_offset = start_bd_offset;
  110. cmd->req.sd_start_bd_index = start_bd_idx;
  111. }
  112. }
  113. /**
  114. * bnx2i_map_scsi_sg - maps IO buffer and prepares the BD table
  115. * @hba: adapter instance
  116. * @cmd: iscsi cmd struct pointer
  117. *
  118. * map SG list
  119. */
  120. static int bnx2i_map_scsi_sg(struct bnx2i_hba *hba, struct bnx2i_cmd *cmd)
  121. {
  122. struct scsi_cmnd *sc = cmd->scsi_cmd;
  123. struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
  124. struct scatterlist *sg;
  125. int byte_count = 0;
  126. int bd_count = 0;
  127. int sg_count;
  128. int sg_len;
  129. u64 addr;
  130. int i;
  131. BUG_ON(scsi_sg_count(sc) > ISCSI_MAX_BDS_PER_CMD);
  132. sg_count = scsi_dma_map(sc);
  133. scsi_for_each_sg(sc, sg, sg_count, i) {
  134. sg_len = sg_dma_len(sg);
  135. addr = (u64) sg_dma_address(sg);
  136. bd[bd_count].buffer_addr_lo = addr & 0xffffffff;
  137. bd[bd_count].buffer_addr_hi = addr >> 32;
  138. bd[bd_count].buffer_length = sg_len;
  139. bd[bd_count].flags = 0;
  140. if (bd_count == 0)
  141. bd[bd_count].flags = ISCSI_BD_FIRST_IN_BD_CHAIN;
  142. byte_count += sg_len;
  143. bd_count++;
  144. }
  145. if (bd_count)
  146. bd[bd_count - 1].flags |= ISCSI_BD_LAST_IN_BD_CHAIN;
  147. BUG_ON(byte_count != scsi_bufflen(sc));
  148. return bd_count;
  149. }
  150. /**
  151. * bnx2i_iscsi_map_sg_list - maps SG list
  152. * @cmd: iscsi cmd struct pointer
  153. *
  154. * creates BD list table for the command
  155. */
  156. static void bnx2i_iscsi_map_sg_list(struct bnx2i_cmd *cmd)
  157. {
  158. int bd_count;
  159. bd_count = bnx2i_map_scsi_sg(cmd->conn->hba, cmd);
  160. if (!bd_count) {
  161. struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
  162. bd[0].buffer_addr_lo = bd[0].buffer_addr_hi = 0;
  163. bd[0].buffer_length = bd[0].flags = 0;
  164. }
  165. cmd->io_tbl.bd_valid = bd_count;
  166. }
  167. /**
  168. * bnx2i_iscsi_unmap_sg_list - unmaps SG list
  169. * @cmd: iscsi cmd struct pointer
  170. *
  171. * unmap IO buffers and invalidate the BD table
  172. */
  173. void bnx2i_iscsi_unmap_sg_list(struct bnx2i_cmd *cmd)
  174. {
  175. struct scsi_cmnd *sc = cmd->scsi_cmd;
  176. if (cmd->io_tbl.bd_valid && sc) {
  177. scsi_dma_unmap(sc);
  178. cmd->io_tbl.bd_valid = 0;
  179. }
  180. }
  181. static void bnx2i_setup_cmd_wqe_template(struct bnx2i_cmd *cmd)
  182. {
  183. memset(&cmd->req, 0x00, sizeof(cmd->req));
  184. cmd->req.op_code = 0xFF;
  185. cmd->req.bd_list_addr_lo = (u32) cmd->io_tbl.bd_tbl_dma;
  186. cmd->req.bd_list_addr_hi =
  187. (u32) ((u64) cmd->io_tbl.bd_tbl_dma >> 32);
  188. }
  189. /**
  190. * bnx2i_bind_conn_to_iscsi_cid - bind conn structure to 'iscsi_cid'
  191. * @hba: pointer to adapter instance
  192. * @conn: pointer to iscsi connection
  193. * @iscsi_cid: iscsi context ID, range 0 - (MAX_CONN - 1)
  194. *
  195. * update iscsi cid table entry with connection pointer. This enables
  196. * driver to quickly get hold of connection structure pointer in
  197. * completion/interrupt thread using iscsi context ID
  198. */
  199. static int bnx2i_bind_conn_to_iscsi_cid(struct bnx2i_hba *hba,
  200. struct bnx2i_conn *bnx2i_conn,
  201. u32 iscsi_cid)
  202. {
  203. if (hba && hba->cid_que.conn_cid_tbl[iscsi_cid]) {
  204. iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
  205. "conn bind - entry #%d not free\n", iscsi_cid);
  206. return -EBUSY;
  207. }
  208. hba->cid_que.conn_cid_tbl[iscsi_cid] = bnx2i_conn;
  209. return 0;
  210. }
  211. /**
  212. * bnx2i_get_conn_from_id - maps an iscsi cid to corresponding conn ptr
  213. * @hba: pointer to adapter instance
  214. * @iscsi_cid: iscsi context ID, range 0 - (MAX_CONN - 1)
  215. */
  216. struct bnx2i_conn *bnx2i_get_conn_from_id(struct bnx2i_hba *hba,
  217. u16 iscsi_cid)
  218. {
  219. if (!hba->cid_que.conn_cid_tbl) {
  220. printk(KERN_ERR "bnx2i: ERROR - missing conn<->cid table\n");
  221. return NULL;
  222. } else if (iscsi_cid >= hba->max_active_conns) {
  223. printk(KERN_ERR "bnx2i: wrong cid #%d\n", iscsi_cid);
  224. return NULL;
  225. }
  226. return hba->cid_que.conn_cid_tbl[iscsi_cid];
  227. }
  228. /**
  229. * bnx2i_alloc_iscsi_cid - allocates a iscsi_cid from free pool
  230. * @hba: pointer to adapter instance
  231. */
  232. static u32 bnx2i_alloc_iscsi_cid(struct bnx2i_hba *hba)
  233. {
  234. int idx;
  235. if (!hba->cid_que.cid_free_cnt)
  236. return -1;
  237. idx = hba->cid_que.cid_q_cons_idx;
  238. hba->cid_que.cid_q_cons_idx++;
  239. if (hba->cid_que.cid_q_cons_idx == hba->cid_que.cid_q_max_idx)
  240. hba->cid_que.cid_q_cons_idx = 0;
  241. hba->cid_que.cid_free_cnt--;
  242. return hba->cid_que.cid_que[idx];
  243. }
  244. /**
  245. * bnx2i_free_iscsi_cid - returns tcp port to free list
  246. * @hba: pointer to adapter instance
  247. * @iscsi_cid: iscsi context ID to free
  248. */
  249. static void bnx2i_free_iscsi_cid(struct bnx2i_hba *hba, u16 iscsi_cid)
  250. {
  251. int idx;
  252. if (iscsi_cid == (u16) -1)
  253. return;
  254. hba->cid_que.cid_free_cnt++;
  255. idx = hba->cid_que.cid_q_prod_idx;
  256. hba->cid_que.cid_que[idx] = iscsi_cid;
  257. hba->cid_que.conn_cid_tbl[iscsi_cid] = NULL;
  258. hba->cid_que.cid_q_prod_idx++;
  259. if (hba->cid_que.cid_q_prod_idx == hba->cid_que.cid_q_max_idx)
  260. hba->cid_que.cid_q_prod_idx = 0;
  261. }
  262. /**
  263. * bnx2i_setup_free_cid_que - sets up free iscsi cid queue
  264. * @hba: pointer to adapter instance
  265. *
  266. * allocates memory for iscsi cid queue & 'cid - conn ptr' mapping table,
  267. * and initialize table attributes
  268. */
  269. static int bnx2i_setup_free_cid_que(struct bnx2i_hba *hba)
  270. {
  271. int mem_size;
  272. int i;
  273. mem_size = hba->max_active_conns * sizeof(u32);
  274. mem_size = (mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  275. hba->cid_que.cid_que_base = kmalloc(mem_size, GFP_KERNEL);
  276. if (!hba->cid_que.cid_que_base)
  277. return -ENOMEM;
  278. mem_size = hba->max_active_conns * sizeof(struct bnx2i_conn *);
  279. mem_size = (mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
  280. hba->cid_que.conn_cid_tbl = kmalloc(mem_size, GFP_KERNEL);
  281. if (!hba->cid_que.conn_cid_tbl) {
  282. kfree(hba->cid_que.cid_que_base);
  283. hba->cid_que.cid_que_base = NULL;
  284. return -ENOMEM;
  285. }
  286. hba->cid_que.cid_que = (u32 *)hba->cid_que.cid_que_base;
  287. hba->cid_que.cid_q_prod_idx = 0;
  288. hba->cid_que.cid_q_cons_idx = 0;
  289. hba->cid_que.cid_q_max_idx = hba->max_active_conns;
  290. hba->cid_que.cid_free_cnt = hba->max_active_conns;
  291. for (i = 0; i < hba->max_active_conns; i++) {
  292. hba->cid_que.cid_que[i] = i;
  293. hba->cid_que.conn_cid_tbl[i] = NULL;
  294. }
  295. return 0;
  296. }
  297. /**
  298. * bnx2i_release_free_cid_que - releases 'iscsi_cid' queue resources
  299. * @hba: pointer to adapter instance
  300. */
  301. static void bnx2i_release_free_cid_que(struct bnx2i_hba *hba)
  302. {
  303. kfree(hba->cid_que.cid_que_base);
  304. hba->cid_que.cid_que_base = NULL;
  305. kfree(hba->cid_que.conn_cid_tbl);
  306. hba->cid_que.conn_cid_tbl = NULL;
  307. }
  308. /**
  309. * bnx2i_alloc_ep - allocates ep structure from global pool
  310. * @hba: pointer to adapter instance
  311. *
  312. * routine allocates a free endpoint structure from global pool and
  313. * a tcp port to be used for this connection. Global resource lock,
  314. * 'bnx2i_resc_lock' is held while accessing shared global data structures
  315. */
  316. static struct iscsi_endpoint *bnx2i_alloc_ep(struct bnx2i_hba *hba)
  317. {
  318. struct iscsi_endpoint *ep;
  319. struct bnx2i_endpoint *bnx2i_ep;
  320. ep = iscsi_create_endpoint(sizeof(*bnx2i_ep));
  321. if (!ep) {
  322. printk(KERN_ERR "bnx2i: Could not allocate ep\n");
  323. return NULL;
  324. }
  325. bnx2i_ep = ep->dd_data;
  326. INIT_LIST_HEAD(&bnx2i_ep->link);
  327. bnx2i_ep->state = EP_STATE_IDLE;
  328. bnx2i_ep->ep_iscsi_cid = (u16) -1;
  329. bnx2i_ep->hba = hba;
  330. bnx2i_ep->hba_age = hba->age;
  331. hba->ofld_conns_active++;
  332. init_waitqueue_head(&bnx2i_ep->ofld_wait);
  333. return ep;
  334. }
  335. /**
  336. * bnx2i_free_ep - free endpoint
  337. * @ep: pointer to iscsi endpoint structure
  338. */
  339. static void bnx2i_free_ep(struct iscsi_endpoint *ep)
  340. {
  341. struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
  342. unsigned long flags;
  343. spin_lock_irqsave(&bnx2i_resc_lock, flags);
  344. bnx2i_ep->state = EP_STATE_IDLE;
  345. bnx2i_ep->hba->ofld_conns_active--;
  346. bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
  347. if (bnx2i_ep->conn) {
  348. bnx2i_ep->conn->ep = NULL;
  349. bnx2i_ep->conn = NULL;
  350. }
  351. bnx2i_ep->hba = NULL;
  352. spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
  353. iscsi_destroy_endpoint(ep);
  354. }
  355. /**
  356. * bnx2i_alloc_bdt - allocates buffer descriptor (BD) table for the command
  357. * @hba: adapter instance pointer
  358. * @session: iscsi session pointer
  359. * @cmd: iscsi command structure
  360. */
  361. static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
  362. struct bnx2i_cmd *cmd)
  363. {
  364. struct io_bdt *io = &cmd->io_tbl;
  365. struct iscsi_bd *bd;
  366. io->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
  367. ISCSI_MAX_BDS_PER_CMD * sizeof(*bd),
  368. &io->bd_tbl_dma, GFP_KERNEL);
  369. if (!io->bd_tbl) {
  370. iscsi_session_printk(KERN_ERR, session, "Could not "
  371. "allocate bdt.\n");
  372. return -ENOMEM;
  373. }
  374. io->bd_valid = 0;
  375. return 0;
  376. }
  377. /**
  378. * bnx2i_destroy_cmd_pool - destroys iscsi command pool and release BD table
  379. * @hba: adapter instance pointer
  380. * @session: iscsi session pointer
  381. * @cmd: iscsi command structure
  382. */
  383. static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
  384. struct iscsi_session *session)
  385. {
  386. int i;
  387. for (i = 0; i < session->cmds_max; i++) {
  388. struct iscsi_task *task = session->cmds[i];
  389. struct bnx2i_cmd *cmd = task->dd_data;
  390. if (cmd->io_tbl.bd_tbl)
  391. dma_free_coherent(&hba->pcidev->dev,
  392. ISCSI_MAX_BDS_PER_CMD *
  393. sizeof(struct iscsi_bd),
  394. cmd->io_tbl.bd_tbl,
  395. cmd->io_tbl.bd_tbl_dma);
  396. }
  397. }
  398. /**
  399. * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
  400. * @hba: adapter instance pointer
  401. * @session: iscsi session pointer
  402. */
  403. static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
  404. struct iscsi_session *session)
  405. {
  406. int i;
  407. for (i = 0; i < session->cmds_max; i++) {
  408. struct iscsi_task *task = session->cmds[i];
  409. struct bnx2i_cmd *cmd = task->dd_data;
  410. task->hdr = &cmd->hdr;
  411. task->hdr_max = sizeof(struct iscsi_hdr);
  412. if (bnx2i_alloc_bdt(hba, session, cmd))
  413. goto free_bdts;
  414. }
  415. return 0;
  416. free_bdts:
  417. bnx2i_destroy_cmd_pool(hba, session);
  418. return -ENOMEM;
  419. }
  420. /**
  421. * bnx2i_setup_mp_bdt - allocate BD table resources
  422. * @hba: pointer to adapter structure
  423. *
  424. * Allocate memory for dummy buffer and associated BD
  425. * table to be used by middle path (MP) requests
  426. */
  427. static int bnx2i_setup_mp_bdt(struct bnx2i_hba *hba)
  428. {
  429. int rc = 0;
  430. struct iscsi_bd *mp_bdt;
  431. u64 addr;
  432. hba->mp_bd_tbl = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  433. &hba->mp_bd_dma, GFP_KERNEL);
  434. if (!hba->mp_bd_tbl) {
  435. printk(KERN_ERR "unable to allocate Middle Path BDT\n");
  436. rc = -1;
  437. goto out;
  438. }
  439. hba->dummy_buffer = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  440. &hba->dummy_buf_dma, GFP_KERNEL);
  441. if (!hba->dummy_buffer) {
  442. printk(KERN_ERR "unable to alloc Middle Path Dummy Buffer\n");
  443. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  444. hba->mp_bd_tbl, hba->mp_bd_dma);
  445. hba->mp_bd_tbl = NULL;
  446. rc = -1;
  447. goto out;
  448. }
  449. mp_bdt = (struct iscsi_bd *) hba->mp_bd_tbl;
  450. addr = (unsigned long) hba->dummy_buf_dma;
  451. mp_bdt->buffer_addr_lo = addr & 0xffffffff;
  452. mp_bdt->buffer_addr_hi = addr >> 32;
  453. mp_bdt->buffer_length = PAGE_SIZE;
  454. mp_bdt->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
  455. ISCSI_BD_FIRST_IN_BD_CHAIN;
  456. out:
  457. return rc;
  458. }
  459. /**
  460. * bnx2i_free_mp_bdt - releases ITT back to free pool
  461. * @hba: pointer to adapter instance
  462. *
  463. * free MP dummy buffer and associated BD table
  464. */
  465. static void bnx2i_free_mp_bdt(struct bnx2i_hba *hba)
  466. {
  467. if (hba->mp_bd_tbl) {
  468. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  469. hba->mp_bd_tbl, hba->mp_bd_dma);
  470. hba->mp_bd_tbl = NULL;
  471. }
  472. if (hba->dummy_buffer) {
  473. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  474. hba->dummy_buffer, hba->dummy_buf_dma);
  475. hba->dummy_buffer = NULL;
  476. }
  477. return;
  478. }
  479. /**
  480. * bnx2i_drop_session - notifies iscsid of connection error.
  481. * @hba: adapter instance pointer
  482. * @session: iscsi session pointer
  483. *
  484. * This notifies iscsid that there is a error, so it can initiate
  485. * recovery.
  486. *
  487. * This relies on caller using the iscsi class iterator so the object
  488. * is refcounted and does not disapper from under us.
  489. */
  490. void bnx2i_drop_session(struct iscsi_cls_session *cls_session)
  491. {
  492. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
  493. }
  494. /**
  495. * bnx2i_ep_destroy_list_add - add an entry to EP destroy list
  496. * @hba: pointer to adapter instance
  497. * @ep: pointer to endpoint (transport indentifier) structure
  498. *
  499. * EP destroy queue manager
  500. */
  501. static int bnx2i_ep_destroy_list_add(struct bnx2i_hba *hba,
  502. struct bnx2i_endpoint *ep)
  503. {
  504. write_lock_bh(&hba->ep_rdwr_lock);
  505. list_add_tail(&ep->link, &hba->ep_destroy_list);
  506. write_unlock_bh(&hba->ep_rdwr_lock);
  507. return 0;
  508. }
  509. /**
  510. * bnx2i_ep_destroy_list_del - add an entry to EP destroy list
  511. *
  512. * @hba: pointer to adapter instance
  513. * @ep: pointer to endpoint (transport indentifier) structure
  514. *
  515. * EP destroy queue manager
  516. */
  517. static int bnx2i_ep_destroy_list_del(struct bnx2i_hba *hba,
  518. struct bnx2i_endpoint *ep)
  519. {
  520. write_lock_bh(&hba->ep_rdwr_lock);
  521. list_del_init(&ep->link);
  522. write_unlock_bh(&hba->ep_rdwr_lock);
  523. return 0;
  524. }
  525. /**
  526. * bnx2i_ep_ofld_list_add - add an entry to ep offload pending list
  527. * @hba: pointer to adapter instance
  528. * @ep: pointer to endpoint (transport indentifier) structure
  529. *
  530. * pending conn offload completion queue manager
  531. */
  532. static int bnx2i_ep_ofld_list_add(struct bnx2i_hba *hba,
  533. struct bnx2i_endpoint *ep)
  534. {
  535. write_lock_bh(&hba->ep_rdwr_lock);
  536. list_add_tail(&ep->link, &hba->ep_ofld_list);
  537. write_unlock_bh(&hba->ep_rdwr_lock);
  538. return 0;
  539. }
  540. /**
  541. * bnx2i_ep_ofld_list_del - add an entry to ep offload pending list
  542. * @hba: pointer to adapter instance
  543. * @ep: pointer to endpoint (transport indentifier) structure
  544. *
  545. * pending conn offload completion queue manager
  546. */
  547. static int bnx2i_ep_ofld_list_del(struct bnx2i_hba *hba,
  548. struct bnx2i_endpoint *ep)
  549. {
  550. write_lock_bh(&hba->ep_rdwr_lock);
  551. list_del_init(&ep->link);
  552. write_unlock_bh(&hba->ep_rdwr_lock);
  553. return 0;
  554. }
  555. /**
  556. * bnx2i_find_ep_in_ofld_list - find iscsi_cid in pending list of endpoints
  557. *
  558. * @hba: pointer to adapter instance
  559. * @iscsi_cid: iscsi context ID to find
  560. *
  561. */
  562. struct bnx2i_endpoint *
  563. bnx2i_find_ep_in_ofld_list(struct bnx2i_hba *hba, u32 iscsi_cid)
  564. {
  565. struct list_head *list;
  566. struct list_head *tmp;
  567. struct bnx2i_endpoint *ep;
  568. read_lock_bh(&hba->ep_rdwr_lock);
  569. list_for_each_safe(list, tmp, &hba->ep_ofld_list) {
  570. ep = (struct bnx2i_endpoint *)list;
  571. if (ep->ep_iscsi_cid == iscsi_cid)
  572. break;
  573. ep = NULL;
  574. }
  575. read_unlock_bh(&hba->ep_rdwr_lock);
  576. if (!ep)
  577. printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
  578. return ep;
  579. }
  580. /**
  581. * bnx2i_find_ep_in_destroy_list - find iscsi_cid in destroy list
  582. * @hba: pointer to adapter instance
  583. * @iscsi_cid: iscsi context ID to find
  584. *
  585. */
  586. struct bnx2i_endpoint *
  587. bnx2i_find_ep_in_destroy_list(struct bnx2i_hba *hba, u32 iscsi_cid)
  588. {
  589. struct list_head *list;
  590. struct list_head *tmp;
  591. struct bnx2i_endpoint *ep;
  592. read_lock_bh(&hba->ep_rdwr_lock);
  593. list_for_each_safe(list, tmp, &hba->ep_destroy_list) {
  594. ep = (struct bnx2i_endpoint *)list;
  595. if (ep->ep_iscsi_cid == iscsi_cid)
  596. break;
  597. ep = NULL;
  598. }
  599. read_unlock_bh(&hba->ep_rdwr_lock);
  600. if (!ep)
  601. printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
  602. return ep;
  603. }
  604. /**
  605. * bnx2i_setup_host_queue_size - assigns shost->can_queue param
  606. * @hba: pointer to adapter instance
  607. * @shost: scsi host pointer
  608. *
  609. * Initializes 'can_queue' parameter based on how many outstanding commands
  610. * the device can handle. Each device 5708/5709/57710 has different
  611. * capabilities
  612. */
  613. static void bnx2i_setup_host_queue_size(struct bnx2i_hba *hba,
  614. struct Scsi_Host *shost)
  615. {
  616. if (test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type))
  617. shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
  618. else if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type))
  619. shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5709;
  620. else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
  621. shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_57710;
  622. else
  623. shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
  624. }
  625. /**
  626. * bnx2i_alloc_hba - allocate and init adapter instance
  627. * @cnic: cnic device pointer
  628. *
  629. * allocate & initialize adapter structure and call other
  630. * support routines to do per adapter initialization
  631. */
  632. struct bnx2i_hba *bnx2i_alloc_hba(struct cnic_dev *cnic)
  633. {
  634. struct Scsi_Host *shost;
  635. struct bnx2i_hba *hba;
  636. shost = iscsi_host_alloc(&bnx2i_host_template, sizeof(*hba), 0);
  637. if (!shost)
  638. return NULL;
  639. shost->dma_boundary = cnic->pcidev->dma_mask;
  640. shost->transportt = bnx2i_scsi_xport_template;
  641. shost->max_id = ISCSI_MAX_CONNS_PER_HBA;
  642. shost->max_channel = 0;
  643. shost->max_lun = 512;
  644. shost->max_cmd_len = 16;
  645. hba = iscsi_host_priv(shost);
  646. hba->shost = shost;
  647. hba->netdev = cnic->netdev;
  648. /* Get PCI related information and update hba struct members */
  649. hba->pcidev = cnic->pcidev;
  650. pci_dev_get(hba->pcidev);
  651. hba->pci_did = hba->pcidev->device;
  652. hba->pci_vid = hba->pcidev->vendor;
  653. hba->pci_sdid = hba->pcidev->subsystem_device;
  654. hba->pci_svid = hba->pcidev->subsystem_vendor;
  655. hba->pci_func = PCI_FUNC(hba->pcidev->devfn);
  656. hba->pci_devno = PCI_SLOT(hba->pcidev->devfn);
  657. bnx2i_identify_device(hba);
  658. bnx2i_setup_host_queue_size(hba, shost);
  659. if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
  660. hba->regview = ioremap_nocache(hba->netdev->base_addr,
  661. BNX2_MQ_CONFIG2);
  662. if (!hba->regview)
  663. goto ioreg_map_err;
  664. } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  665. hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
  666. if (!hba->regview)
  667. goto ioreg_map_err;
  668. }
  669. if (bnx2i_setup_mp_bdt(hba))
  670. goto mp_bdt_mem_err;
  671. INIT_LIST_HEAD(&hba->ep_ofld_list);
  672. INIT_LIST_HEAD(&hba->ep_destroy_list);
  673. rwlock_init(&hba->ep_rdwr_lock);
  674. hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
  675. /* different values for 5708/5709/57710 */
  676. hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
  677. if (bnx2i_setup_free_cid_que(hba))
  678. goto cid_que_err;
  679. /* SQ/RQ/CQ size can be changed via sysfx interface */
  680. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  681. if (sq_size && sq_size <= BNX2I_5770X_SQ_WQES_MAX)
  682. hba->max_sqes = sq_size;
  683. else
  684. hba->max_sqes = BNX2I_5770X_SQ_WQES_DEFAULT;
  685. } else { /* 5706/5708/5709 */
  686. if (sq_size && sq_size <= BNX2I_570X_SQ_WQES_MAX)
  687. hba->max_sqes = sq_size;
  688. else
  689. hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
  690. }
  691. hba->max_rqes = rq_size;
  692. hba->max_cqes = hba->max_sqes + rq_size;
  693. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  694. if (hba->max_cqes > BNX2I_5770X_CQ_WQES_MAX)
  695. hba->max_cqes = BNX2I_5770X_CQ_WQES_MAX;
  696. } else if (hba->max_cqes > BNX2I_570X_CQ_WQES_MAX)
  697. hba->max_cqes = BNX2I_570X_CQ_WQES_MAX;
  698. hba->num_ccell = hba->max_sqes / 2;
  699. spin_lock_init(&hba->lock);
  700. mutex_init(&hba->net_dev_lock);
  701. if (iscsi_host_add(shost, &hba->pcidev->dev))
  702. goto free_dump_mem;
  703. return hba;
  704. free_dump_mem:
  705. bnx2i_release_free_cid_que(hba);
  706. cid_que_err:
  707. bnx2i_free_mp_bdt(hba);
  708. mp_bdt_mem_err:
  709. if (hba->regview) {
  710. iounmap(hba->regview);
  711. hba->regview = NULL;
  712. }
  713. ioreg_map_err:
  714. pci_dev_put(hba->pcidev);
  715. scsi_host_put(shost);
  716. return NULL;
  717. }
  718. /**
  719. * bnx2i_free_hba- releases hba structure and resources held by the adapter
  720. * @hba: pointer to adapter instance
  721. *
  722. * free adapter structure and call various cleanup routines.
  723. */
  724. void bnx2i_free_hba(struct bnx2i_hba *hba)
  725. {
  726. struct Scsi_Host *shost = hba->shost;
  727. iscsi_host_remove(shost);
  728. INIT_LIST_HEAD(&hba->ep_ofld_list);
  729. INIT_LIST_HEAD(&hba->ep_destroy_list);
  730. pci_dev_put(hba->pcidev);
  731. if (hba->regview) {
  732. iounmap(hba->regview);
  733. hba->regview = NULL;
  734. }
  735. bnx2i_free_mp_bdt(hba);
  736. bnx2i_release_free_cid_que(hba);
  737. iscsi_host_free(shost);
  738. }
  739. /**
  740. * bnx2i_conn_free_login_resources - free DMA resources used for login process
  741. * @hba: pointer to adapter instance
  742. * @bnx2i_conn: iscsi connection pointer
  743. *
  744. * Login related resources, mostly BDT & payload DMA memory is freed
  745. */
  746. static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
  747. struct bnx2i_conn *bnx2i_conn)
  748. {
  749. if (bnx2i_conn->gen_pdu.resp_bd_tbl) {
  750. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  751. bnx2i_conn->gen_pdu.resp_bd_tbl,
  752. bnx2i_conn->gen_pdu.resp_bd_dma);
  753. bnx2i_conn->gen_pdu.resp_bd_tbl = NULL;
  754. }
  755. if (bnx2i_conn->gen_pdu.req_bd_tbl) {
  756. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  757. bnx2i_conn->gen_pdu.req_bd_tbl,
  758. bnx2i_conn->gen_pdu.req_bd_dma);
  759. bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
  760. }
  761. if (bnx2i_conn->gen_pdu.resp_buf) {
  762. dma_free_coherent(&hba->pcidev->dev,
  763. ISCSI_DEF_MAX_RECV_SEG_LEN,
  764. bnx2i_conn->gen_pdu.resp_buf,
  765. bnx2i_conn->gen_pdu.resp_dma_addr);
  766. bnx2i_conn->gen_pdu.resp_buf = NULL;
  767. }
  768. if (bnx2i_conn->gen_pdu.req_buf) {
  769. dma_free_coherent(&hba->pcidev->dev,
  770. ISCSI_DEF_MAX_RECV_SEG_LEN,
  771. bnx2i_conn->gen_pdu.req_buf,
  772. bnx2i_conn->gen_pdu.req_dma_addr);
  773. bnx2i_conn->gen_pdu.req_buf = NULL;
  774. }
  775. }
  776. /**
  777. * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
  778. * @hba: pointer to adapter instance
  779. * @bnx2i_conn: iscsi connection pointer
  780. *
  781. * Mgmt task DNA resources are allocated in this routine.
  782. */
  783. static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
  784. struct bnx2i_conn *bnx2i_conn)
  785. {
  786. /* Allocate memory for login request/response buffers */
  787. bnx2i_conn->gen_pdu.req_buf =
  788. dma_alloc_coherent(&hba->pcidev->dev,
  789. ISCSI_DEF_MAX_RECV_SEG_LEN,
  790. &bnx2i_conn->gen_pdu.req_dma_addr,
  791. GFP_KERNEL);
  792. if (bnx2i_conn->gen_pdu.req_buf == NULL)
  793. goto login_req_buf_failure;
  794. bnx2i_conn->gen_pdu.req_buf_size = 0;
  795. bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
  796. bnx2i_conn->gen_pdu.resp_buf =
  797. dma_alloc_coherent(&hba->pcidev->dev,
  798. ISCSI_DEF_MAX_RECV_SEG_LEN,
  799. &bnx2i_conn->gen_pdu.resp_dma_addr,
  800. GFP_KERNEL);
  801. if (bnx2i_conn->gen_pdu.resp_buf == NULL)
  802. goto login_resp_buf_failure;
  803. bnx2i_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
  804. bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf;
  805. bnx2i_conn->gen_pdu.req_bd_tbl =
  806. dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  807. &bnx2i_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
  808. if (bnx2i_conn->gen_pdu.req_bd_tbl == NULL)
  809. goto login_req_bd_tbl_failure;
  810. bnx2i_conn->gen_pdu.resp_bd_tbl =
  811. dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  812. &bnx2i_conn->gen_pdu.resp_bd_dma,
  813. GFP_KERNEL);
  814. if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
  815. goto login_resp_bd_tbl_failure;
  816. return 0;
  817. login_resp_bd_tbl_failure:
  818. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  819. bnx2i_conn->gen_pdu.req_bd_tbl,
  820. bnx2i_conn->gen_pdu.req_bd_dma);
  821. bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
  822. login_req_bd_tbl_failure:
  823. dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
  824. bnx2i_conn->gen_pdu.resp_buf,
  825. bnx2i_conn->gen_pdu.resp_dma_addr);
  826. bnx2i_conn->gen_pdu.resp_buf = NULL;
  827. login_resp_buf_failure:
  828. dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
  829. bnx2i_conn->gen_pdu.req_buf,
  830. bnx2i_conn->gen_pdu.req_dma_addr);
  831. bnx2i_conn->gen_pdu.req_buf = NULL;
  832. login_req_buf_failure:
  833. iscsi_conn_printk(KERN_ERR, bnx2i_conn->cls_conn->dd_data,
  834. "login resource alloc failed!!\n");
  835. return -ENOMEM;
  836. }
  837. /**
  838. * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
  839. * @bnx2i_conn: iscsi connection pointer
  840. *
  841. * Allocates buffers and BD tables before shipping requests to cnic
  842. * for PDUs prepared by 'iscsid' daemon
  843. */
  844. static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
  845. {
  846. struct iscsi_bd *bd_tbl;
  847. bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
  848. bd_tbl->buffer_addr_hi =
  849. (u32) ((u64) bnx2i_conn->gen_pdu.req_dma_addr >> 32);
  850. bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.req_dma_addr;
  851. bd_tbl->buffer_length = bnx2i_conn->gen_pdu.req_wr_ptr -
  852. bnx2i_conn->gen_pdu.req_buf;
  853. bd_tbl->reserved0 = 0;
  854. bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
  855. ISCSI_BD_FIRST_IN_BD_CHAIN;
  856. bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.resp_bd_tbl;
  857. bd_tbl->buffer_addr_hi = (u64) bnx2i_conn->gen_pdu.resp_dma_addr >> 32;
  858. bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_dma_addr;
  859. bd_tbl->buffer_length = ISCSI_DEF_MAX_RECV_SEG_LEN;
  860. bd_tbl->reserved0 = 0;
  861. bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
  862. ISCSI_BD_FIRST_IN_BD_CHAIN;
  863. }
  864. /**
  865. * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
  866. * @task: transport layer task pointer
  867. *
  868. * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
  869. * Nop-out and Logout requests flow through this path.
  870. */
  871. static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
  872. {
  873. struct bnx2i_cmd *cmd = task->dd_data;
  874. struct bnx2i_conn *bnx2i_conn = cmd->conn;
  875. int rc = 0;
  876. char *buf;
  877. int data_len;
  878. bnx2i_iscsi_prep_generic_pdu_bd(bnx2i_conn);
  879. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  880. case ISCSI_OP_LOGIN:
  881. bnx2i_send_iscsi_login(bnx2i_conn, task);
  882. break;
  883. case ISCSI_OP_NOOP_OUT:
  884. data_len = bnx2i_conn->gen_pdu.req_buf_size;
  885. buf = bnx2i_conn->gen_pdu.req_buf;
  886. if (data_len)
  887. rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
  888. RESERVED_ITT,
  889. buf, data_len, 1);
  890. else
  891. rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
  892. RESERVED_ITT,
  893. NULL, 0, 1);
  894. break;
  895. case ISCSI_OP_LOGOUT:
  896. rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
  897. break;
  898. case ISCSI_OP_SCSI_TMFUNC:
  899. rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
  900. break;
  901. default:
  902. iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
  903. "send_gen: unsupported op 0x%x\n",
  904. task->hdr->opcode);
  905. }
  906. return rc;
  907. }
  908. /**********************************************************************
  909. * SCSI-ML Interface
  910. **********************************************************************/
  911. /**
  912. * bnx2i_cpy_scsi_cdb - copies LUN & CDB fields in required format to sq wqe
  913. * @sc: SCSI-ML command pointer
  914. * @cmd: iscsi cmd pointer
  915. */
  916. static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
  917. {
  918. u32 dword;
  919. int lpcnt;
  920. u8 *srcp;
  921. u32 *dstp;
  922. u32 scsi_lun[2];
  923. int_to_scsilun(sc->device->lun, (struct scsi_lun *) scsi_lun);
  924. cmd->req.lun[0] = be32_to_cpu(scsi_lun[0]);
  925. cmd->req.lun[1] = be32_to_cpu(scsi_lun[1]);
  926. lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
  927. srcp = (u8 *) sc->cmnd;
  928. dstp = (u32 *) cmd->req.cdb;
  929. while (lpcnt--) {
  930. memcpy(&dword, (const void *) srcp, 4);
  931. *dstp = cpu_to_be32(dword);
  932. srcp += 4;
  933. dstp++;
  934. }
  935. if (sc->cmd_len & 0x3) {
  936. dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
  937. *dstp = cpu_to_be32(dword);
  938. }
  939. }
  940. static void bnx2i_cleanup_task(struct iscsi_task *task)
  941. {
  942. struct iscsi_conn *conn = task->conn;
  943. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  944. struct bnx2i_hba *hba = bnx2i_conn->hba;
  945. /*
  946. * mgmt task or cmd was never sent to us to transmit.
  947. */
  948. if (!task->sc || task->state == ISCSI_TASK_PENDING)
  949. return;
  950. /*
  951. * need to clean-up task context to claim dma buffers
  952. */
  953. if (task->state == ISCSI_TASK_ABRT_TMF) {
  954. bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
  955. spin_unlock_bh(&conn->session->lock);
  956. wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
  957. msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
  958. spin_lock_bh(&conn->session->lock);
  959. }
  960. bnx2i_iscsi_unmap_sg_list(task->dd_data);
  961. }
  962. /**
  963. * bnx2i_mtask_xmit - transmit mtask to chip for further processing
  964. * @conn: transport layer conn structure pointer
  965. * @task: transport layer command structure pointer
  966. */
  967. static int
  968. bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
  969. {
  970. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  971. struct bnx2i_cmd *cmd = task->dd_data;
  972. memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
  973. bnx2i_setup_cmd_wqe_template(cmd);
  974. bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
  975. if (task->data_count) {
  976. memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
  977. task->data_count);
  978. bnx2i_conn->gen_pdu.req_wr_ptr =
  979. bnx2i_conn->gen_pdu.req_buf + task->data_count;
  980. }
  981. cmd->conn = conn->dd_data;
  982. cmd->scsi_cmd = NULL;
  983. return bnx2i_iscsi_send_generic_request(task);
  984. }
  985. /**
  986. * bnx2i_task_xmit - transmit iscsi command to chip for further processing
  987. * @task: transport layer command structure pointer
  988. *
  989. * maps SG buffers and send request to chip/firmware in the form of SQ WQE
  990. */
  991. static int bnx2i_task_xmit(struct iscsi_task *task)
  992. {
  993. struct iscsi_conn *conn = task->conn;
  994. struct iscsi_session *session = conn->session;
  995. struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
  996. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  997. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  998. struct scsi_cmnd *sc = task->sc;
  999. struct bnx2i_cmd *cmd = task->dd_data;
  1000. struct iscsi_cmd *hdr = (struct iscsi_cmd *) task->hdr;
  1001. /*
  1002. * If there is no scsi_cmnd this must be a mgmt task
  1003. */
  1004. if (!sc)
  1005. return bnx2i_mtask_xmit(conn, task);
  1006. bnx2i_setup_cmd_wqe_template(cmd);
  1007. cmd->req.op_code = ISCSI_OP_SCSI_CMD;
  1008. cmd->conn = bnx2i_conn;
  1009. cmd->scsi_cmd = sc;
  1010. cmd->req.total_data_transfer_length = scsi_bufflen(sc);
  1011. cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
  1012. bnx2i_iscsi_map_sg_list(cmd);
  1013. bnx2i_cpy_scsi_cdb(sc, cmd);
  1014. cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
  1015. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1016. cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
  1017. cmd->req.itt = task->itt |
  1018. (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1019. bnx2i_setup_write_cmd_bd_info(task);
  1020. } else {
  1021. if (scsi_bufflen(sc))
  1022. cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
  1023. cmd->req.itt = task->itt |
  1024. (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1025. }
  1026. cmd->req.num_bds = cmd->io_tbl.bd_valid;
  1027. if (!cmd->io_tbl.bd_valid) {
  1028. cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
  1029. cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
  1030. cmd->req.num_bds = 1;
  1031. }
  1032. bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
  1033. return 0;
  1034. }
  1035. /**
  1036. * bnx2i_session_create - create a new iscsi session
  1037. * @cmds_max: max commands supported
  1038. * @qdepth: scsi queue depth to support
  1039. * @initial_cmdsn: initial iscsi CMDSN to be used for this session
  1040. *
  1041. * Creates a new iSCSI session instance on given device.
  1042. */
  1043. static struct iscsi_cls_session *
  1044. bnx2i_session_create(struct iscsi_endpoint *ep,
  1045. uint16_t cmds_max, uint16_t qdepth,
  1046. uint32_t initial_cmdsn)
  1047. {
  1048. struct Scsi_Host *shost;
  1049. struct iscsi_cls_session *cls_session;
  1050. struct bnx2i_hba *hba;
  1051. struct bnx2i_endpoint *bnx2i_ep;
  1052. if (!ep) {
  1053. printk(KERN_ERR "bnx2i: missing ep.\n");
  1054. return NULL;
  1055. }
  1056. bnx2i_ep = ep->dd_data;
  1057. shost = bnx2i_ep->hba->shost;
  1058. hba = iscsi_host_priv(shost);
  1059. if (bnx2i_adapter_ready(hba))
  1060. return NULL;
  1061. /*
  1062. * user can override hw limit as long as it is within
  1063. * the min/max.
  1064. */
  1065. if (cmds_max > hba->max_sqes)
  1066. cmds_max = hba->max_sqes;
  1067. else if (cmds_max < BNX2I_SQ_WQES_MIN)
  1068. cmds_max = BNX2I_SQ_WQES_MIN;
  1069. cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
  1070. cmds_max, 0, sizeof(struct bnx2i_cmd),
  1071. initial_cmdsn, ISCSI_MAX_TARGET);
  1072. if (!cls_session)
  1073. return NULL;
  1074. if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
  1075. goto session_teardown;
  1076. return cls_session;
  1077. session_teardown:
  1078. iscsi_session_teardown(cls_session);
  1079. return NULL;
  1080. }
  1081. /**
  1082. * bnx2i_session_destroy - destroys iscsi session
  1083. * @cls_session: pointer to iscsi cls session
  1084. *
  1085. * Destroys previously created iSCSI session instance and releases
  1086. * all resources held by it
  1087. */
  1088. static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
  1089. {
  1090. struct iscsi_session *session = cls_session->dd_data;
  1091. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1092. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1093. bnx2i_destroy_cmd_pool(hba, session);
  1094. iscsi_session_teardown(cls_session);
  1095. }
  1096. /**
  1097. * bnx2i_conn_create - create iscsi connection instance
  1098. * @cls_session: pointer to iscsi cls session
  1099. * @cid: iscsi cid as per rfc (not NX2's CID terminology)
  1100. *
  1101. * Creates a new iSCSI connection instance for a given session
  1102. */
  1103. static struct iscsi_cls_conn *
  1104. bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
  1105. {
  1106. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1107. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1108. struct bnx2i_conn *bnx2i_conn;
  1109. struct iscsi_cls_conn *cls_conn;
  1110. struct iscsi_conn *conn;
  1111. cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
  1112. cid);
  1113. if (!cls_conn)
  1114. return NULL;
  1115. conn = cls_conn->dd_data;
  1116. bnx2i_conn = conn->dd_data;
  1117. bnx2i_conn->cls_conn = cls_conn;
  1118. bnx2i_conn->hba = hba;
  1119. /* 'ep' ptr will be assigned in bind() call */
  1120. bnx2i_conn->ep = NULL;
  1121. init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
  1122. if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
  1123. iscsi_conn_printk(KERN_ALERT, conn,
  1124. "conn_new: login resc alloc failed!!\n");
  1125. goto free_conn;
  1126. }
  1127. return cls_conn;
  1128. free_conn:
  1129. iscsi_conn_teardown(cls_conn);
  1130. return NULL;
  1131. }
  1132. /**
  1133. * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
  1134. * @cls_session: pointer to iscsi cls session
  1135. * @cls_conn: pointer to iscsi cls conn
  1136. * @transport_fd: 64-bit EP handle
  1137. * @is_leading: leading connection on this session?
  1138. *
  1139. * Binds together iSCSI session instance, iSCSI connection instance
  1140. * and the TCP connection. This routine returns error code if
  1141. * TCP connection does not belong on the device iSCSI sess/conn
  1142. * is bound
  1143. */
  1144. static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
  1145. struct iscsi_cls_conn *cls_conn,
  1146. uint64_t transport_fd, int is_leading)
  1147. {
  1148. struct iscsi_conn *conn = cls_conn->dd_data;
  1149. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1150. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1151. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1152. struct bnx2i_endpoint *bnx2i_ep;
  1153. struct iscsi_endpoint *ep;
  1154. int ret_code;
  1155. ep = iscsi_lookup_endpoint(transport_fd);
  1156. if (!ep)
  1157. return -EINVAL;
  1158. bnx2i_ep = ep->dd_data;
  1159. if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
  1160. (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD))
  1161. /* Peer disconnect via' FIN or RST */
  1162. return -EINVAL;
  1163. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  1164. return -EINVAL;
  1165. if (bnx2i_ep->hba != hba) {
  1166. /* Error - TCP connection does not belong to this device
  1167. */
  1168. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1169. "conn bind, ep=0x%p (%s) does not",
  1170. bnx2i_ep, bnx2i_ep->hba->netdev->name);
  1171. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1172. "belong to hba (%s)\n",
  1173. hba->netdev->name);
  1174. return -EEXIST;
  1175. }
  1176. bnx2i_ep->conn = bnx2i_conn;
  1177. bnx2i_conn->ep = bnx2i_ep;
  1178. bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
  1179. bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
  1180. ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
  1181. bnx2i_ep->ep_iscsi_cid);
  1182. /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
  1183. * driver needs to explicitly replenish RQ index during setup.
  1184. */
  1185. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1186. bnx2i_put_rq_buf(bnx2i_conn, 0);
  1187. bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
  1188. return ret_code;
  1189. }
  1190. /**
  1191. * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
  1192. * @cls_conn: pointer to iscsi cls conn
  1193. *
  1194. * Destroy an iSCSI connection instance and release memory resources held by
  1195. * this connection
  1196. */
  1197. static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1198. {
  1199. struct iscsi_conn *conn = cls_conn->dd_data;
  1200. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1201. struct Scsi_Host *shost;
  1202. struct bnx2i_hba *hba;
  1203. shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
  1204. hba = iscsi_host_priv(shost);
  1205. bnx2i_conn_free_login_resources(hba, bnx2i_conn);
  1206. iscsi_conn_teardown(cls_conn);
  1207. }
  1208. /**
  1209. * bnx2i_conn_get_param - return iscsi connection parameter to caller
  1210. * @cls_conn: pointer to iscsi cls conn
  1211. * @param: parameter type identifier
  1212. * @buf: buffer pointer
  1213. *
  1214. * returns iSCSI connection parameters
  1215. */
  1216. static int bnx2i_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1217. enum iscsi_param param, char *buf)
  1218. {
  1219. struct iscsi_conn *conn = cls_conn->dd_data;
  1220. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1221. int len = 0;
  1222. switch (param) {
  1223. case ISCSI_PARAM_CONN_PORT:
  1224. if (bnx2i_conn->ep)
  1225. len = sprintf(buf, "%hu\n",
  1226. bnx2i_conn->ep->cm_sk->dst_port);
  1227. break;
  1228. case ISCSI_PARAM_CONN_ADDRESS:
  1229. if (bnx2i_conn->ep)
  1230. len = sprintf(buf, NIPQUAD_FMT "\n",
  1231. NIPQUAD(bnx2i_conn->ep->cm_sk->dst_ip));
  1232. break;
  1233. default:
  1234. return iscsi_conn_get_param(cls_conn, param, buf);
  1235. }
  1236. return len;
  1237. }
  1238. /**
  1239. * bnx2i_host_get_param - returns host (adapter) related parameters
  1240. * @shost: scsi host pointer
  1241. * @param: parameter type identifier
  1242. * @buf: buffer pointer
  1243. */
  1244. static int bnx2i_host_get_param(struct Scsi_Host *shost,
  1245. enum iscsi_host_param param, char *buf)
  1246. {
  1247. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1248. int len = 0;
  1249. switch (param) {
  1250. case ISCSI_HOST_PARAM_HWADDRESS:
  1251. len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
  1252. break;
  1253. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1254. len = sprintf(buf, "%s\n", hba->netdev->name);
  1255. break;
  1256. default:
  1257. return iscsi_host_get_param(shost, param, buf);
  1258. }
  1259. return len;
  1260. }
  1261. /**
  1262. * bnx2i_conn_start - completes iscsi connection migration to FFP
  1263. * @cls_conn: pointer to iscsi cls conn
  1264. *
  1265. * last call in FFP migration to handover iscsi conn to the driver
  1266. */
  1267. static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
  1268. {
  1269. struct iscsi_conn *conn = cls_conn->dd_data;
  1270. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1271. bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
  1272. bnx2i_update_iscsi_conn(conn);
  1273. /*
  1274. * this should normally not sleep for a long time so it should
  1275. * not disrupt the caller.
  1276. */
  1277. bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
  1278. bnx2i_conn->ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1279. bnx2i_conn->ep->ofld_timer.data = (unsigned long) bnx2i_conn->ep;
  1280. add_timer(&bnx2i_conn->ep->ofld_timer);
  1281. /* update iSCSI context for this conn, wait for CNIC to complete */
  1282. wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
  1283. bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
  1284. if (signal_pending(current))
  1285. flush_signals(current);
  1286. del_timer_sync(&bnx2i_conn->ep->ofld_timer);
  1287. iscsi_conn_start(cls_conn);
  1288. return 0;
  1289. }
  1290. /**
  1291. * bnx2i_conn_get_stats - returns iSCSI stats
  1292. * @cls_conn: pointer to iscsi cls conn
  1293. * @stats: pointer to iscsi statistic struct
  1294. */
  1295. static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  1296. struct iscsi_stats *stats)
  1297. {
  1298. struct iscsi_conn *conn = cls_conn->dd_data;
  1299. stats->txdata_octets = conn->txdata_octets;
  1300. stats->rxdata_octets = conn->rxdata_octets;
  1301. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1302. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1303. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1304. stats->datain_pdus = conn->datain_pdus_cnt;
  1305. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1306. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1307. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1308. stats->custom_length = 3;
  1309. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1310. stats->custom[2].value = conn->eh_abort_cnt;
  1311. stats->digest_err = 0;
  1312. stats->timeout_err = 0;
  1313. stats->custom_length = 0;
  1314. }
  1315. /**
  1316. * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
  1317. * @dst_addr: target IP address
  1318. *
  1319. * check if route resolves to BNX2 device
  1320. */
  1321. static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
  1322. {
  1323. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1324. struct bnx2i_hba *hba;
  1325. struct cnic_dev *cnic = NULL;
  1326. bnx2i_reg_dev_all();
  1327. hba = get_adapter_list_head();
  1328. if (hba && hba->cnic)
  1329. cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
  1330. if (!cnic) {
  1331. printk(KERN_ALERT "bnx2i: no route,"
  1332. "can't connect using cnic\n");
  1333. goto no_nx2_route;
  1334. }
  1335. hba = bnx2i_find_hba_for_cnic(cnic);
  1336. if (!hba)
  1337. goto no_nx2_route;
  1338. if (bnx2i_adapter_ready(hba)) {
  1339. printk(KERN_ALERT "bnx2i: check route, hba not found\n");
  1340. goto no_nx2_route;
  1341. }
  1342. if (hba->netdev->mtu > hba->mtu_supported) {
  1343. printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
  1344. hba->netdev->name, hba->netdev->mtu);
  1345. printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
  1346. hba->mtu_supported);
  1347. goto no_nx2_route;
  1348. }
  1349. return hba;
  1350. no_nx2_route:
  1351. return NULL;
  1352. }
  1353. /**
  1354. * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
  1355. * @hba: pointer to adapter instance
  1356. * @ep: endpoint (transport indentifier) structure
  1357. *
  1358. * destroys cm_sock structure and on chip iscsi context
  1359. */
  1360. static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
  1361. struct bnx2i_endpoint *ep)
  1362. {
  1363. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
  1364. hba->cnic->cm_destroy(ep->cm_sk);
  1365. if (test_bit(ADAPTER_STATE_GOING_DOWN, &ep->hba->adapter_state))
  1366. ep->state = EP_STATE_DISCONN_COMPL;
  1367. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
  1368. ep->state == EP_STATE_DISCONN_TIMEDOUT) {
  1369. printk(KERN_ALERT "bnx2i - ERROR - please submit GRC Dump,"
  1370. " NW/PCIe trace, driver msgs to developers"
  1371. " for analysis\n");
  1372. return 1;
  1373. }
  1374. ep->state = EP_STATE_CLEANUP_START;
  1375. init_timer(&ep->ofld_timer);
  1376. ep->ofld_timer.expires = 10*HZ + jiffies;
  1377. ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1378. ep->ofld_timer.data = (unsigned long) ep;
  1379. add_timer(&ep->ofld_timer);
  1380. bnx2i_ep_destroy_list_add(hba, ep);
  1381. /* destroy iSCSI context, wait for it to complete */
  1382. bnx2i_send_conn_destroy(hba, ep);
  1383. wait_event_interruptible(ep->ofld_wait,
  1384. (ep->state != EP_STATE_CLEANUP_START));
  1385. if (signal_pending(current))
  1386. flush_signals(current);
  1387. del_timer_sync(&ep->ofld_timer);
  1388. bnx2i_ep_destroy_list_del(hba, ep);
  1389. if (ep->state != EP_STATE_CLEANUP_CMPL)
  1390. /* should never happen */
  1391. printk(KERN_ALERT "bnx2i - conn destroy failed\n");
  1392. return 0;
  1393. }
  1394. /**
  1395. * bnx2i_ep_connect - establish TCP connection to target portal
  1396. * @shost: scsi host
  1397. * @dst_addr: target IP address
  1398. * @non_blocking: blocking or non-blocking call
  1399. *
  1400. * this routine initiates the TCP/IP connection by invoking Option-2 i/f
  1401. * with l5_core and the CNIC. This is a multi-step process of resolving
  1402. * route to target, create a iscsi connection context, handshaking with
  1403. * CNIC module to create/initialize the socket struct and finally
  1404. * sending down option-2 request to complete TCP 3-way handshake
  1405. */
  1406. static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
  1407. struct sockaddr *dst_addr,
  1408. int non_blocking)
  1409. {
  1410. u32 iscsi_cid = BNX2I_CID_RESERVED;
  1411. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1412. struct sockaddr_in6 *desti6;
  1413. struct bnx2i_endpoint *bnx2i_ep;
  1414. struct bnx2i_hba *hba;
  1415. struct cnic_dev *cnic;
  1416. struct cnic_sockaddr saddr;
  1417. struct iscsi_endpoint *ep;
  1418. int rc = 0;
  1419. if (shost) {
  1420. /* driver is given scsi host to work with */
  1421. hba = iscsi_host_priv(shost);
  1422. /* Register the device with cnic if not already done so */
  1423. bnx2i_register_device(hba);
  1424. } else
  1425. /*
  1426. * check if the given destination can be reached through
  1427. * a iscsi capable NetXtreme2 device
  1428. */
  1429. hba = bnx2i_check_route(dst_addr);
  1430. if (!hba) {
  1431. rc = -ENOMEM;
  1432. goto check_busy;
  1433. }
  1434. cnic = hba->cnic;
  1435. ep = bnx2i_alloc_ep(hba);
  1436. if (!ep) {
  1437. rc = -ENOMEM;
  1438. goto check_busy;
  1439. }
  1440. bnx2i_ep = ep->dd_data;
  1441. mutex_lock(&hba->net_dev_lock);
  1442. if (bnx2i_adapter_ready(hba)) {
  1443. rc = -EPERM;
  1444. goto net_if_down;
  1445. }
  1446. bnx2i_ep->num_active_cmds = 0;
  1447. iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
  1448. if (iscsi_cid == -1) {
  1449. printk(KERN_ALERT "alloc_ep: unable to allocate iscsi cid\n");
  1450. rc = -ENOMEM;
  1451. goto iscsi_cid_err;
  1452. }
  1453. bnx2i_ep->hba_age = hba->age;
  1454. rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
  1455. if (rc != 0) {
  1456. printk(KERN_ALERT "bnx2i: ep_conn, alloc QP resc error\n");
  1457. rc = -ENOMEM;
  1458. goto qp_resc_err;
  1459. }
  1460. bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
  1461. bnx2i_ep->state = EP_STATE_OFLD_START;
  1462. bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
  1463. init_timer(&bnx2i_ep->ofld_timer);
  1464. bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
  1465. bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1466. bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
  1467. add_timer(&bnx2i_ep->ofld_timer);
  1468. bnx2i_send_conn_ofld_req(hba, bnx2i_ep);
  1469. /* Wait for CNIC hardware to setup conn context and return 'cid' */
  1470. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1471. bnx2i_ep->state != EP_STATE_OFLD_START);
  1472. if (signal_pending(current))
  1473. flush_signals(current);
  1474. del_timer_sync(&bnx2i_ep->ofld_timer);
  1475. bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
  1476. if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
  1477. rc = -ENOSPC;
  1478. goto conn_failed;
  1479. }
  1480. rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
  1481. iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
  1482. if (rc) {
  1483. rc = -EINVAL;
  1484. goto conn_failed;
  1485. }
  1486. bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
  1487. bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
  1488. clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
  1489. memset(&saddr, 0, sizeof(saddr));
  1490. if (dst_addr->sa_family == AF_INET) {
  1491. desti = (struct sockaddr_in *) dst_addr;
  1492. saddr.remote.v4 = *desti;
  1493. saddr.local.v4.sin_family = desti->sin_family;
  1494. } else if (dst_addr->sa_family == AF_INET6) {
  1495. desti6 = (struct sockaddr_in6 *) dst_addr;
  1496. saddr.remote.v6 = *desti6;
  1497. saddr.local.v6.sin6_family = desti6->sin6_family;
  1498. }
  1499. bnx2i_ep->timestamp = jiffies;
  1500. bnx2i_ep->state = EP_STATE_CONNECT_START;
  1501. if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  1502. rc = -EINVAL;
  1503. goto conn_failed;
  1504. } else
  1505. rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
  1506. if (rc)
  1507. goto release_ep;
  1508. if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
  1509. goto release_ep;
  1510. mutex_unlock(&hba->net_dev_lock);
  1511. return ep;
  1512. release_ep:
  1513. if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
  1514. mutex_unlock(&hba->net_dev_lock);
  1515. return ERR_PTR(rc);
  1516. }
  1517. conn_failed:
  1518. net_if_down:
  1519. iscsi_cid_err:
  1520. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1521. qp_resc_err:
  1522. bnx2i_free_ep(ep);
  1523. mutex_unlock(&hba->net_dev_lock);
  1524. check_busy:
  1525. bnx2i_unreg_dev_all();
  1526. return ERR_PTR(rc);
  1527. }
  1528. /**
  1529. * bnx2i_ep_poll - polls for TCP connection establishement
  1530. * @ep: TCP connection (endpoint) handle
  1531. * @timeout_ms: timeout value in milli secs
  1532. *
  1533. * polls for TCP connect request to complete
  1534. */
  1535. static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  1536. {
  1537. struct bnx2i_endpoint *bnx2i_ep;
  1538. int rc = 0;
  1539. bnx2i_ep = ep->dd_data;
  1540. if ((bnx2i_ep->state == EP_STATE_IDLE) ||
  1541. (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
  1542. (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
  1543. return -1;
  1544. if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
  1545. return 1;
  1546. rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
  1547. ((bnx2i_ep->state ==
  1548. EP_STATE_OFLD_FAILED) ||
  1549. (bnx2i_ep->state ==
  1550. EP_STATE_CONNECT_FAILED) ||
  1551. (bnx2i_ep->state ==
  1552. EP_STATE_CONNECT_COMPL)),
  1553. msecs_to_jiffies(timeout_ms));
  1554. if (!rc || (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
  1555. rc = -1;
  1556. if (rc > 0)
  1557. return 1;
  1558. else if (!rc)
  1559. return 0; /* timeout */
  1560. else
  1561. return rc;
  1562. }
  1563. /**
  1564. * bnx2i_ep_tcp_conn_active - check EP state transition
  1565. * @ep: endpoint pointer
  1566. *
  1567. * check if underlying TCP connection is active
  1568. */
  1569. static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
  1570. {
  1571. int ret;
  1572. int cnic_dev_10g = 0;
  1573. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1574. cnic_dev_10g = 1;
  1575. switch (bnx2i_ep->state) {
  1576. case EP_STATE_CONNECT_START:
  1577. case EP_STATE_CLEANUP_FAILED:
  1578. case EP_STATE_OFLD_FAILED:
  1579. case EP_STATE_DISCONN_TIMEDOUT:
  1580. ret = 0;
  1581. break;
  1582. case EP_STATE_CONNECT_COMPL:
  1583. case EP_STATE_ULP_UPDATE_START:
  1584. case EP_STATE_ULP_UPDATE_COMPL:
  1585. case EP_STATE_TCP_FIN_RCVD:
  1586. case EP_STATE_ULP_UPDATE_FAILED:
  1587. ret = 1;
  1588. break;
  1589. case EP_STATE_TCP_RST_RCVD:
  1590. ret = 0;
  1591. break;
  1592. case EP_STATE_CONNECT_FAILED:
  1593. if (cnic_dev_10g)
  1594. ret = 1;
  1595. else
  1596. ret = 0;
  1597. break;
  1598. default:
  1599. ret = 0;
  1600. }
  1601. return ret;
  1602. }
  1603. /**
  1604. * bnx2i_ep_disconnect - executes TCP connection teardown process
  1605. * @ep: TCP connection (endpoint) handle
  1606. *
  1607. * executes TCP connection teardown process
  1608. */
  1609. static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
  1610. {
  1611. struct bnx2i_endpoint *bnx2i_ep;
  1612. struct bnx2i_conn *bnx2i_conn = NULL;
  1613. struct iscsi_session *session = NULL;
  1614. struct iscsi_conn *conn;
  1615. struct cnic_dev *cnic;
  1616. struct bnx2i_hba *hba;
  1617. bnx2i_ep = ep->dd_data;
  1618. /* driver should not attempt connection cleanup until TCP_CONNECT
  1619. * completes either successfully or fails. Timeout is 9-secs, so
  1620. * wait for it to complete
  1621. */
  1622. while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
  1623. !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
  1624. msleep(250);
  1625. if (bnx2i_ep->conn) {
  1626. bnx2i_conn = bnx2i_ep->conn;
  1627. conn = bnx2i_conn->cls_conn->dd_data;
  1628. session = conn->session;
  1629. iscsi_suspend_queue(conn);
  1630. }
  1631. hba = bnx2i_ep->hba;
  1632. if (bnx2i_ep->state == EP_STATE_IDLE)
  1633. goto return_bnx2i_ep;
  1634. cnic = hba->cnic;
  1635. mutex_lock(&hba->net_dev_lock);
  1636. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
  1637. goto free_resc;
  1638. if (bnx2i_ep->hba_age != hba->age)
  1639. goto free_resc;
  1640. if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
  1641. goto destory_conn;
  1642. bnx2i_ep->state = EP_STATE_DISCONN_START;
  1643. init_timer(&bnx2i_ep->ofld_timer);
  1644. bnx2i_ep->ofld_timer.expires = 10*HZ + jiffies;
  1645. bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1646. bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
  1647. add_timer(&bnx2i_ep->ofld_timer);
  1648. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  1649. int close = 0;
  1650. if (session) {
  1651. spin_lock_bh(&session->lock);
  1652. if (session->state == ISCSI_STATE_LOGGING_OUT)
  1653. close = 1;
  1654. spin_unlock_bh(&session->lock);
  1655. }
  1656. if (close)
  1657. cnic->cm_close(bnx2i_ep->cm_sk);
  1658. else
  1659. cnic->cm_abort(bnx2i_ep->cm_sk);
  1660. } else
  1661. goto free_resc;
  1662. /* wait for option-2 conn teardown */
  1663. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1664. bnx2i_ep->state != EP_STATE_DISCONN_START);
  1665. if (signal_pending(current))
  1666. flush_signals(current);
  1667. del_timer_sync(&bnx2i_ep->ofld_timer);
  1668. destory_conn:
  1669. if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
  1670. mutex_unlock(&hba->net_dev_lock);
  1671. return;
  1672. }
  1673. free_resc:
  1674. mutex_unlock(&hba->net_dev_lock);
  1675. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1676. return_bnx2i_ep:
  1677. if (bnx2i_conn)
  1678. bnx2i_conn->ep = NULL;
  1679. bnx2i_free_ep(ep);
  1680. if (!hba->ofld_conns_active)
  1681. bnx2i_unreg_dev_all();
  1682. }
  1683. /**
  1684. * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
  1685. * @buf: pointer to buffer containing iscsi path message
  1686. *
  1687. */
  1688. static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
  1689. {
  1690. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1691. char *buf = (char *) params;
  1692. u16 len = sizeof(*params);
  1693. /* handled by cnic driver */
  1694. hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
  1695. len);
  1696. return 0;
  1697. }
  1698. /*
  1699. * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
  1700. * used while registering with the scsi host and iSCSI transport module.
  1701. */
  1702. static struct scsi_host_template bnx2i_host_template = {
  1703. .module = THIS_MODULE,
  1704. .name = "Broadcom Offload iSCSI Initiator",
  1705. .proc_name = "bnx2i",
  1706. .queuecommand = iscsi_queuecommand,
  1707. .eh_abort_handler = iscsi_eh_abort,
  1708. .eh_device_reset_handler = iscsi_eh_device_reset,
  1709. .eh_target_reset_handler = iscsi_eh_target_reset,
  1710. .can_queue = 1024,
  1711. .max_sectors = 127,
  1712. .cmd_per_lun = 32,
  1713. .this_id = -1,
  1714. .use_clustering = ENABLE_CLUSTERING,
  1715. .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
  1716. .shost_attrs = bnx2i_dev_attributes,
  1717. };
  1718. struct iscsi_transport bnx2i_iscsi_transport = {
  1719. .owner = THIS_MODULE,
  1720. .name = "bnx2i",
  1721. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
  1722. CAP_MULTI_R2T | CAP_DATADGST |
  1723. CAP_DATA_PATH_OFFLOAD,
  1724. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1725. ISCSI_MAX_XMIT_DLENGTH |
  1726. ISCSI_HDRDGST_EN |
  1727. ISCSI_DATADGST_EN |
  1728. ISCSI_INITIAL_R2T_EN |
  1729. ISCSI_MAX_R2T |
  1730. ISCSI_IMM_DATA_EN |
  1731. ISCSI_FIRST_BURST |
  1732. ISCSI_MAX_BURST |
  1733. ISCSI_PDU_INORDER_EN |
  1734. ISCSI_DATASEQ_INORDER_EN |
  1735. ISCSI_ERL |
  1736. ISCSI_CONN_PORT |
  1737. ISCSI_CONN_ADDRESS |
  1738. ISCSI_EXP_STATSN |
  1739. ISCSI_PERSISTENT_PORT |
  1740. ISCSI_PERSISTENT_ADDRESS |
  1741. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1742. ISCSI_USERNAME | ISCSI_PASSWORD |
  1743. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1744. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1745. ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
  1746. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  1747. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  1748. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_NETDEV_NAME,
  1749. .create_session = bnx2i_session_create,
  1750. .destroy_session = bnx2i_session_destroy,
  1751. .create_conn = bnx2i_conn_create,
  1752. .bind_conn = bnx2i_conn_bind,
  1753. .destroy_conn = bnx2i_conn_destroy,
  1754. .set_param = iscsi_set_param,
  1755. .get_conn_param = bnx2i_conn_get_param,
  1756. .get_session_param = iscsi_session_get_param,
  1757. .get_host_param = bnx2i_host_get_param,
  1758. .start_conn = bnx2i_conn_start,
  1759. .stop_conn = iscsi_conn_stop,
  1760. .send_pdu = iscsi_conn_send_pdu,
  1761. .xmit_task = bnx2i_task_xmit,
  1762. .get_stats = bnx2i_conn_get_stats,
  1763. /* TCP connect - disconnect - option-2 interface calls */
  1764. .ep_connect = bnx2i_ep_connect,
  1765. .ep_poll = bnx2i_ep_poll,
  1766. .ep_disconnect = bnx2i_ep_disconnect,
  1767. .set_path = bnx2i_nl_set_path,
  1768. /* Error recovery timeout call */
  1769. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1770. .cleanup_task = bnx2i_cleanup_task,
  1771. };