bnx2i_iscsi.c 62 KB

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