bnx2i_iscsi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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->hba = hba;
  329. bnx2i_ep->hba_age = hba->age;
  330. hba->ofld_conns_active++;
  331. init_waitqueue_head(&bnx2i_ep->ofld_wait);
  332. return ep;
  333. }
  334. /**
  335. * bnx2i_free_ep - free endpoint
  336. * @ep: pointer to iscsi endpoint structure
  337. */
  338. static void bnx2i_free_ep(struct iscsi_endpoint *ep)
  339. {
  340. struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
  341. unsigned long flags;
  342. spin_lock_irqsave(&bnx2i_resc_lock, flags);
  343. bnx2i_ep->state = EP_STATE_IDLE;
  344. bnx2i_ep->hba->ofld_conns_active--;
  345. bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
  346. if (bnx2i_ep->conn) {
  347. bnx2i_ep->conn->ep = NULL;
  348. bnx2i_ep->conn = NULL;
  349. }
  350. bnx2i_ep->hba = NULL;
  351. spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
  352. iscsi_destroy_endpoint(ep);
  353. }
  354. /**
  355. * bnx2i_alloc_bdt - allocates buffer descriptor (BD) table for the command
  356. * @hba: adapter instance pointer
  357. * @session: iscsi session pointer
  358. * @cmd: iscsi command structure
  359. */
  360. static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
  361. struct bnx2i_cmd *cmd)
  362. {
  363. struct io_bdt *io = &cmd->io_tbl;
  364. struct iscsi_bd *bd;
  365. io->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
  366. ISCSI_MAX_BDS_PER_CMD * sizeof(*bd),
  367. &io->bd_tbl_dma, GFP_KERNEL);
  368. if (!io->bd_tbl) {
  369. iscsi_session_printk(KERN_ERR, session, "Could not "
  370. "allocate bdt.\n");
  371. return -ENOMEM;
  372. }
  373. io->bd_valid = 0;
  374. return 0;
  375. }
  376. /**
  377. * bnx2i_destroy_cmd_pool - destroys iscsi command pool and release BD table
  378. * @hba: adapter instance pointer
  379. * @session: iscsi session pointer
  380. * @cmd: iscsi command structure
  381. */
  382. static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
  383. struct iscsi_session *session)
  384. {
  385. int i;
  386. for (i = 0; i < session->cmds_max; i++) {
  387. struct iscsi_task *task = session->cmds[i];
  388. struct bnx2i_cmd *cmd = task->dd_data;
  389. if (cmd->io_tbl.bd_tbl)
  390. dma_free_coherent(&hba->pcidev->dev,
  391. ISCSI_MAX_BDS_PER_CMD *
  392. sizeof(struct iscsi_bd),
  393. cmd->io_tbl.bd_tbl,
  394. cmd->io_tbl.bd_tbl_dma);
  395. }
  396. }
  397. /**
  398. * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
  399. * @hba: adapter instance pointer
  400. * @session: iscsi session pointer
  401. */
  402. static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
  403. struct iscsi_session *session)
  404. {
  405. int i;
  406. for (i = 0; i < session->cmds_max; i++) {
  407. struct iscsi_task *task = session->cmds[i];
  408. struct bnx2i_cmd *cmd = task->dd_data;
  409. /* Anil */
  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_identify_device(hba);
  659. bnx2i_setup_host_queue_size(hba, shost);
  660. if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
  661. hba->regview = ioremap_nocache(hba->netdev->base_addr,
  662. BNX2_MQ_CONFIG2);
  663. if (!hba->regview)
  664. goto ioreg_map_err;
  665. } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  666. hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
  667. if (!hba->regview)
  668. goto ioreg_map_err;
  669. }
  670. if (bnx2i_setup_mp_bdt(hba))
  671. goto mp_bdt_mem_err;
  672. INIT_LIST_HEAD(&hba->ep_ofld_list);
  673. INIT_LIST_HEAD(&hba->ep_destroy_list);
  674. rwlock_init(&hba->ep_rdwr_lock);
  675. hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
  676. /* different values for 5708/5709/57710 */
  677. hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
  678. if (bnx2i_setup_free_cid_que(hba))
  679. goto cid_que_err;
  680. /* SQ/RQ/CQ size can be changed via sysfx interface */
  681. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  682. if (sq_size && sq_size <= BNX2I_5770X_SQ_WQES_MAX)
  683. hba->max_sqes = sq_size;
  684. else
  685. hba->max_sqes = BNX2I_5770X_SQ_WQES_DEFAULT;
  686. } else { /* 5706/5708/5709 */
  687. if (sq_size && sq_size <= BNX2I_570X_SQ_WQES_MAX)
  688. hba->max_sqes = sq_size;
  689. else
  690. hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
  691. }
  692. hba->max_rqes = rq_size;
  693. hba->max_cqes = hba->max_sqes + rq_size;
  694. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
  695. if (hba->max_cqes > BNX2I_5770X_CQ_WQES_MAX)
  696. hba->max_cqes = BNX2I_5770X_CQ_WQES_MAX;
  697. } else if (hba->max_cqes > BNX2I_570X_CQ_WQES_MAX)
  698. hba->max_cqes = BNX2I_570X_CQ_WQES_MAX;
  699. hba->num_ccell = hba->max_sqes / 2;
  700. spin_lock_init(&hba->lock);
  701. mutex_init(&hba->net_dev_lock);
  702. if (iscsi_host_add(shost, &hba->pcidev->dev))
  703. goto free_dump_mem;
  704. return hba;
  705. free_dump_mem:
  706. bnx2i_release_free_cid_que(hba);
  707. cid_que_err:
  708. bnx2i_free_mp_bdt(hba);
  709. mp_bdt_mem_err:
  710. if (hba->regview) {
  711. iounmap(hba->regview);
  712. hba->regview = NULL;
  713. }
  714. ioreg_map_err:
  715. pci_dev_put(hba->pcidev);
  716. scsi_host_put(shost);
  717. return NULL;
  718. }
  719. /**
  720. * bnx2i_free_hba- releases hba structure and resources held by the adapter
  721. * @hba: pointer to adapter instance
  722. *
  723. * free adapter structure and call various cleanup routines.
  724. */
  725. void bnx2i_free_hba(struct bnx2i_hba *hba)
  726. {
  727. struct Scsi_Host *shost = hba->shost;
  728. iscsi_host_remove(shost);
  729. INIT_LIST_HEAD(&hba->ep_ofld_list);
  730. INIT_LIST_HEAD(&hba->ep_destroy_list);
  731. pci_dev_put(hba->pcidev);
  732. if (hba->regview) {
  733. iounmap(hba->regview);
  734. hba->regview = NULL;
  735. }
  736. bnx2i_free_mp_bdt(hba);
  737. bnx2i_release_free_cid_que(hba);
  738. iscsi_host_free(shost);
  739. }
  740. /**
  741. * bnx2i_conn_free_login_resources - free DMA resources used for login process
  742. * @hba: pointer to adapter instance
  743. * @bnx2i_conn: iscsi connection pointer
  744. *
  745. * Login related resources, mostly BDT & payload DMA memory is freed
  746. */
  747. static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
  748. struct bnx2i_conn *bnx2i_conn)
  749. {
  750. if (bnx2i_conn->gen_pdu.resp_bd_tbl) {
  751. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  752. bnx2i_conn->gen_pdu.resp_bd_tbl,
  753. bnx2i_conn->gen_pdu.resp_bd_dma);
  754. bnx2i_conn->gen_pdu.resp_bd_tbl = NULL;
  755. }
  756. if (bnx2i_conn->gen_pdu.req_bd_tbl) {
  757. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  758. bnx2i_conn->gen_pdu.req_bd_tbl,
  759. bnx2i_conn->gen_pdu.req_bd_dma);
  760. bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
  761. }
  762. if (bnx2i_conn->gen_pdu.resp_buf) {
  763. dma_free_coherent(&hba->pcidev->dev,
  764. ISCSI_DEF_MAX_RECV_SEG_LEN,
  765. bnx2i_conn->gen_pdu.resp_buf,
  766. bnx2i_conn->gen_pdu.resp_dma_addr);
  767. bnx2i_conn->gen_pdu.resp_buf = NULL;
  768. }
  769. if (bnx2i_conn->gen_pdu.req_buf) {
  770. dma_free_coherent(&hba->pcidev->dev,
  771. ISCSI_DEF_MAX_RECV_SEG_LEN,
  772. bnx2i_conn->gen_pdu.req_buf,
  773. bnx2i_conn->gen_pdu.req_dma_addr);
  774. bnx2i_conn->gen_pdu.req_buf = NULL;
  775. }
  776. }
  777. /**
  778. * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
  779. * @hba: pointer to adapter instance
  780. * @bnx2i_conn: iscsi connection pointer
  781. *
  782. * Mgmt task DNA resources are allocated in this routine.
  783. */
  784. static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
  785. struct bnx2i_conn *bnx2i_conn)
  786. {
  787. /* Allocate memory for login request/response buffers */
  788. bnx2i_conn->gen_pdu.req_buf =
  789. dma_alloc_coherent(&hba->pcidev->dev,
  790. ISCSI_DEF_MAX_RECV_SEG_LEN,
  791. &bnx2i_conn->gen_pdu.req_dma_addr,
  792. GFP_KERNEL);
  793. if (bnx2i_conn->gen_pdu.req_buf == NULL)
  794. goto login_req_buf_failure;
  795. bnx2i_conn->gen_pdu.req_buf_size = 0;
  796. bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
  797. bnx2i_conn->gen_pdu.resp_buf =
  798. dma_alloc_coherent(&hba->pcidev->dev,
  799. ISCSI_DEF_MAX_RECV_SEG_LEN,
  800. &bnx2i_conn->gen_pdu.resp_dma_addr,
  801. GFP_KERNEL);
  802. if (bnx2i_conn->gen_pdu.resp_buf == NULL)
  803. goto login_resp_buf_failure;
  804. bnx2i_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
  805. bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf;
  806. bnx2i_conn->gen_pdu.req_bd_tbl =
  807. dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  808. &bnx2i_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
  809. if (bnx2i_conn->gen_pdu.req_bd_tbl == NULL)
  810. goto login_req_bd_tbl_failure;
  811. bnx2i_conn->gen_pdu.resp_bd_tbl =
  812. dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
  813. &bnx2i_conn->gen_pdu.resp_bd_dma,
  814. GFP_KERNEL);
  815. if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
  816. goto login_resp_bd_tbl_failure;
  817. return 0;
  818. login_resp_bd_tbl_failure:
  819. dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
  820. bnx2i_conn->gen_pdu.req_bd_tbl,
  821. bnx2i_conn->gen_pdu.req_bd_dma);
  822. bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
  823. login_req_bd_tbl_failure:
  824. dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
  825. bnx2i_conn->gen_pdu.resp_buf,
  826. bnx2i_conn->gen_pdu.resp_dma_addr);
  827. bnx2i_conn->gen_pdu.resp_buf = NULL;
  828. login_resp_buf_failure:
  829. dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
  830. bnx2i_conn->gen_pdu.req_buf,
  831. bnx2i_conn->gen_pdu.req_dma_addr);
  832. bnx2i_conn->gen_pdu.req_buf = NULL;
  833. login_req_buf_failure:
  834. iscsi_conn_printk(KERN_ERR, bnx2i_conn->cls_conn->dd_data,
  835. "login resource alloc failed!!\n");
  836. return -ENOMEM;
  837. }
  838. /**
  839. * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
  840. * @bnx2i_conn: iscsi connection pointer
  841. *
  842. * Allocates buffers and BD tables before shipping requests to cnic
  843. * for PDUs prepared by 'iscsid' daemon
  844. */
  845. static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
  846. {
  847. struct iscsi_bd *bd_tbl;
  848. bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
  849. bd_tbl->buffer_addr_hi =
  850. (u32) ((u64) bnx2i_conn->gen_pdu.req_dma_addr >> 32);
  851. bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.req_dma_addr;
  852. bd_tbl->buffer_length = bnx2i_conn->gen_pdu.req_wr_ptr -
  853. bnx2i_conn->gen_pdu.req_buf;
  854. bd_tbl->reserved0 = 0;
  855. bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
  856. ISCSI_BD_FIRST_IN_BD_CHAIN;
  857. bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.resp_bd_tbl;
  858. bd_tbl->buffer_addr_hi = (u64) bnx2i_conn->gen_pdu.resp_dma_addr >> 32;
  859. bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_dma_addr;
  860. bd_tbl->buffer_length = ISCSI_DEF_MAX_RECV_SEG_LEN;
  861. bd_tbl->reserved0 = 0;
  862. bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
  863. ISCSI_BD_FIRST_IN_BD_CHAIN;
  864. }
  865. /**
  866. * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
  867. * @task: transport layer task pointer
  868. *
  869. * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
  870. * Nop-out and Logout requests flow through this path.
  871. */
  872. static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
  873. {
  874. struct bnx2i_cmd *cmd = task->dd_data;
  875. struct bnx2i_conn *bnx2i_conn = cmd->conn;
  876. int rc = 0;
  877. char *buf;
  878. int data_len;
  879. bnx2i_iscsi_prep_generic_pdu_bd(bnx2i_conn);
  880. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  881. case ISCSI_OP_LOGIN:
  882. bnx2i_send_iscsi_login(bnx2i_conn, task);
  883. break;
  884. case ISCSI_OP_NOOP_OUT:
  885. data_len = bnx2i_conn->gen_pdu.req_buf_size;
  886. buf = bnx2i_conn->gen_pdu.req_buf;
  887. if (data_len)
  888. rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
  889. RESERVED_ITT,
  890. buf, data_len, 1);
  891. else
  892. rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
  893. RESERVED_ITT,
  894. NULL, 0, 1);
  895. break;
  896. case ISCSI_OP_LOGOUT:
  897. rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
  898. break;
  899. case ISCSI_OP_SCSI_TMFUNC:
  900. rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
  901. break;
  902. default:
  903. iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
  904. "send_gen: unsupported op 0x%x\n",
  905. task->hdr->opcode);
  906. }
  907. return rc;
  908. }
  909. /**********************************************************************
  910. * SCSI-ML Interface
  911. **********************************************************************/
  912. /**
  913. * bnx2i_cpy_scsi_cdb - copies LUN & CDB fields in required format to sq wqe
  914. * @sc: SCSI-ML command pointer
  915. * @cmd: iscsi cmd pointer
  916. */
  917. static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
  918. {
  919. u32 dword;
  920. int lpcnt;
  921. u8 *srcp;
  922. u32 *dstp;
  923. u32 scsi_lun[2];
  924. int_to_scsilun(sc->device->lun, (struct scsi_lun *) scsi_lun);
  925. cmd->req.lun[0] = be32_to_cpu(scsi_lun[0]);
  926. cmd->req.lun[1] = be32_to_cpu(scsi_lun[1]);
  927. lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
  928. srcp = (u8 *) sc->cmnd;
  929. dstp = (u32 *) cmd->req.cdb;
  930. while (lpcnt--) {
  931. memcpy(&dword, (const void *) srcp, 4);
  932. *dstp = cpu_to_be32(dword);
  933. srcp += 4;
  934. dstp++;
  935. }
  936. if (sc->cmd_len & 0x3) {
  937. dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
  938. *dstp = cpu_to_be32(dword);
  939. }
  940. }
  941. static void bnx2i_cleanup_task(struct iscsi_task *task)
  942. {
  943. struct iscsi_conn *conn = task->conn;
  944. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  945. struct bnx2i_hba *hba = bnx2i_conn->hba;
  946. /*
  947. * mgmt task or cmd was never sent to us to transmit.
  948. */
  949. if (!task->sc || task->state == ISCSI_TASK_PENDING)
  950. return;
  951. /*
  952. * need to clean-up task context to claim dma buffers
  953. */
  954. if (task->state == ISCSI_TASK_ABRT_TMF) {
  955. bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
  956. spin_unlock_bh(&conn->session->lock);
  957. wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
  958. msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
  959. spin_lock_bh(&conn->session->lock);
  960. }
  961. bnx2i_iscsi_unmap_sg_list(task->dd_data);
  962. }
  963. /**
  964. * bnx2i_mtask_xmit - transmit mtask to chip for further processing
  965. * @conn: transport layer conn structure pointer
  966. * @task: transport layer command structure pointer
  967. */
  968. static int
  969. bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
  970. {
  971. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  972. struct bnx2i_cmd *cmd = task->dd_data;
  973. memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
  974. bnx2i_setup_cmd_wqe_template(cmd);
  975. bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
  976. if (task->data_count) {
  977. memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
  978. task->data_count);
  979. bnx2i_conn->gen_pdu.req_wr_ptr =
  980. bnx2i_conn->gen_pdu.req_buf + task->data_count;
  981. }
  982. cmd->conn = conn->dd_data;
  983. cmd->scsi_cmd = NULL;
  984. return bnx2i_iscsi_send_generic_request(task);
  985. }
  986. /**
  987. * bnx2i_task_xmit - transmit iscsi command to chip for further processing
  988. * @task: transport layer command structure pointer
  989. *
  990. * maps SG buffers and send request to chip/firmware in the form of SQ WQE
  991. */
  992. static int bnx2i_task_xmit(struct iscsi_task *task)
  993. {
  994. struct iscsi_conn *conn = task->conn;
  995. struct iscsi_session *session = conn->session;
  996. struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
  997. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  998. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  999. struct scsi_cmnd *sc = task->sc;
  1000. struct bnx2i_cmd *cmd = task->dd_data;
  1001. struct iscsi_cmd *hdr = (struct iscsi_cmd *) task->hdr;
  1002. if (test_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state))
  1003. return -ENOTCONN;
  1004. if (!bnx2i_conn->is_bound)
  1005. return -ENOTCONN;
  1006. /*
  1007. * If there is no scsi_cmnd this must be a mgmt task
  1008. */
  1009. if (!sc)
  1010. return bnx2i_mtask_xmit(conn, task);
  1011. bnx2i_setup_cmd_wqe_template(cmd);
  1012. cmd->req.op_code = ISCSI_OP_SCSI_CMD;
  1013. cmd->conn = bnx2i_conn;
  1014. cmd->scsi_cmd = sc;
  1015. cmd->req.total_data_transfer_length = scsi_bufflen(sc);
  1016. cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
  1017. bnx2i_iscsi_map_sg_list(cmd);
  1018. bnx2i_cpy_scsi_cdb(sc, cmd);
  1019. cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
  1020. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1021. cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
  1022. cmd->req.itt = task->itt |
  1023. (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1024. bnx2i_setup_write_cmd_bd_info(task);
  1025. } else {
  1026. if (scsi_bufflen(sc))
  1027. cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
  1028. cmd->req.itt = task->itt |
  1029. (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1030. }
  1031. cmd->req.num_bds = cmd->io_tbl.bd_valid;
  1032. if (!cmd->io_tbl.bd_valid) {
  1033. cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
  1034. cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
  1035. cmd->req.num_bds = 1;
  1036. }
  1037. bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
  1038. return 0;
  1039. }
  1040. /**
  1041. * bnx2i_session_create - create a new iscsi session
  1042. * @cmds_max: max commands supported
  1043. * @qdepth: scsi queue depth to support
  1044. * @initial_cmdsn: initial iscsi CMDSN to be used for this session
  1045. *
  1046. * Creates a new iSCSI session instance on given device.
  1047. */
  1048. static struct iscsi_cls_session *
  1049. bnx2i_session_create(struct iscsi_endpoint *ep,
  1050. uint16_t cmds_max, uint16_t qdepth,
  1051. uint32_t initial_cmdsn)
  1052. {
  1053. struct Scsi_Host *shost;
  1054. struct iscsi_cls_session *cls_session;
  1055. struct bnx2i_hba *hba;
  1056. struct bnx2i_endpoint *bnx2i_ep;
  1057. if (!ep) {
  1058. printk(KERN_ERR "bnx2i: missing ep.\n");
  1059. return NULL;
  1060. }
  1061. bnx2i_ep = ep->dd_data;
  1062. shost = bnx2i_ep->hba->shost;
  1063. hba = iscsi_host_priv(shost);
  1064. if (bnx2i_adapter_ready(hba))
  1065. return NULL;
  1066. /*
  1067. * user can override hw limit as long as it is within
  1068. * the min/max.
  1069. */
  1070. if (cmds_max > hba->max_sqes)
  1071. cmds_max = hba->max_sqes;
  1072. else if (cmds_max < BNX2I_SQ_WQES_MIN)
  1073. cmds_max = BNX2I_SQ_WQES_MIN;
  1074. cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
  1075. cmds_max, sizeof(struct bnx2i_cmd),
  1076. initial_cmdsn, ISCSI_MAX_TARGET);
  1077. if (!cls_session)
  1078. return NULL;
  1079. if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
  1080. goto session_teardown;
  1081. return cls_session;
  1082. session_teardown:
  1083. iscsi_session_teardown(cls_session);
  1084. return NULL;
  1085. }
  1086. /**
  1087. * bnx2i_session_destroy - destroys iscsi session
  1088. * @cls_session: pointer to iscsi cls session
  1089. *
  1090. * Destroys previously created iSCSI session instance and releases
  1091. * all resources held by it
  1092. */
  1093. static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
  1094. {
  1095. struct iscsi_session *session = cls_session->dd_data;
  1096. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1097. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1098. bnx2i_destroy_cmd_pool(hba, session);
  1099. iscsi_session_teardown(cls_session);
  1100. }
  1101. /**
  1102. * bnx2i_conn_create - create iscsi connection instance
  1103. * @cls_session: pointer to iscsi cls session
  1104. * @cid: iscsi cid as per rfc (not NX2's CID terminology)
  1105. *
  1106. * Creates a new iSCSI connection instance for a given session
  1107. */
  1108. static struct iscsi_cls_conn *
  1109. bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
  1110. {
  1111. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1112. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1113. struct bnx2i_conn *bnx2i_conn;
  1114. struct iscsi_cls_conn *cls_conn;
  1115. struct iscsi_conn *conn;
  1116. cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
  1117. cid);
  1118. if (!cls_conn)
  1119. return NULL;
  1120. conn = cls_conn->dd_data;
  1121. bnx2i_conn = conn->dd_data;
  1122. bnx2i_conn->cls_conn = cls_conn;
  1123. bnx2i_conn->hba = hba;
  1124. /* 'ep' ptr will be assigned in bind() call */
  1125. bnx2i_conn->ep = NULL;
  1126. init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
  1127. if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
  1128. iscsi_conn_printk(KERN_ALERT, conn,
  1129. "conn_new: login resc alloc failed!!\n");
  1130. goto free_conn;
  1131. }
  1132. return cls_conn;
  1133. free_conn:
  1134. iscsi_conn_teardown(cls_conn);
  1135. return NULL;
  1136. }
  1137. /**
  1138. * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
  1139. * @cls_session: pointer to iscsi cls session
  1140. * @cls_conn: pointer to iscsi cls conn
  1141. * @transport_fd: 64-bit EP handle
  1142. * @is_leading: leading connection on this session?
  1143. *
  1144. * Binds together iSCSI session instance, iSCSI connection instance
  1145. * and the TCP connection. This routine returns error code if
  1146. * TCP connection does not belong on the device iSCSI sess/conn
  1147. * is bound
  1148. */
  1149. static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
  1150. struct iscsi_cls_conn *cls_conn,
  1151. uint64_t transport_fd, int is_leading)
  1152. {
  1153. struct iscsi_conn *conn = cls_conn->dd_data;
  1154. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1155. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1156. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1157. struct bnx2i_endpoint *bnx2i_ep;
  1158. struct iscsi_endpoint *ep;
  1159. int ret_code;
  1160. ep = iscsi_lookup_endpoint(transport_fd);
  1161. if (!ep)
  1162. return -EINVAL;
  1163. bnx2i_ep = ep->dd_data;
  1164. if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
  1165. (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD))
  1166. /* Peer disconnect via' FIN or RST */
  1167. return -EINVAL;
  1168. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  1169. return -EINVAL;
  1170. if (bnx2i_ep->hba != hba) {
  1171. /* Error - TCP connection does not belong to this device
  1172. */
  1173. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1174. "conn bind, ep=0x%p (%s) does not",
  1175. bnx2i_ep, bnx2i_ep->hba->netdev->name);
  1176. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1177. "belong to hba (%s)\n",
  1178. hba->netdev->name);
  1179. return -EEXIST;
  1180. }
  1181. bnx2i_ep->conn = bnx2i_conn;
  1182. bnx2i_conn->ep = bnx2i_ep;
  1183. bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
  1184. bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
  1185. bnx2i_conn->is_bound = 1;
  1186. ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
  1187. bnx2i_ep->ep_iscsi_cid);
  1188. /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
  1189. * driver needs to explicitly replenish RQ index during setup.
  1190. */
  1191. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1192. bnx2i_put_rq_buf(bnx2i_conn, 0);
  1193. bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
  1194. return ret_code;
  1195. }
  1196. /**
  1197. * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
  1198. * @cls_conn: pointer to iscsi cls conn
  1199. *
  1200. * Destroy an iSCSI connection instance and release memory resources held by
  1201. * this connection
  1202. */
  1203. static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1204. {
  1205. struct iscsi_conn *conn = cls_conn->dd_data;
  1206. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1207. struct Scsi_Host *shost;
  1208. struct bnx2i_hba *hba;
  1209. shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
  1210. hba = iscsi_host_priv(shost);
  1211. bnx2i_conn_free_login_resources(hba, bnx2i_conn);
  1212. iscsi_conn_teardown(cls_conn);
  1213. }
  1214. /**
  1215. * bnx2i_conn_get_param - return iscsi connection parameter to caller
  1216. * @cls_conn: pointer to iscsi cls conn
  1217. * @param: parameter type identifier
  1218. * @buf: buffer pointer
  1219. *
  1220. * returns iSCSI connection parameters
  1221. */
  1222. static int bnx2i_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1223. enum iscsi_param param, char *buf)
  1224. {
  1225. struct iscsi_conn *conn = cls_conn->dd_data;
  1226. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1227. int len = 0;
  1228. switch (param) {
  1229. case ISCSI_PARAM_CONN_PORT:
  1230. if (bnx2i_conn->ep)
  1231. len = sprintf(buf, "%hu\n",
  1232. bnx2i_conn->ep->cm_sk->dst_port);
  1233. break;
  1234. case ISCSI_PARAM_CONN_ADDRESS:
  1235. if (bnx2i_conn->ep)
  1236. len = sprintf(buf, NIPQUAD_FMT "\n",
  1237. NIPQUAD(bnx2i_conn->ep->cm_sk->dst_ip));
  1238. break;
  1239. default:
  1240. return iscsi_conn_get_param(cls_conn, param, buf);
  1241. }
  1242. return len;
  1243. }
  1244. /**
  1245. * bnx2i_host_get_param - returns host (adapter) related parameters
  1246. * @shost: scsi host pointer
  1247. * @param: parameter type identifier
  1248. * @buf: buffer pointer
  1249. */
  1250. static int bnx2i_host_get_param(struct Scsi_Host *shost,
  1251. enum iscsi_host_param param, char *buf)
  1252. {
  1253. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1254. int len = 0;
  1255. switch (param) {
  1256. case ISCSI_HOST_PARAM_HWADDRESS:
  1257. len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
  1258. break;
  1259. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1260. len = sprintf(buf, "%s\n", hba->netdev->name);
  1261. break;
  1262. default:
  1263. return iscsi_host_get_param(shost, param, buf);
  1264. }
  1265. return len;
  1266. }
  1267. /**
  1268. * bnx2i_conn_start - completes iscsi connection migration to FFP
  1269. * @cls_conn: pointer to iscsi cls conn
  1270. *
  1271. * last call in FFP migration to handover iscsi conn to the driver
  1272. */
  1273. static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
  1274. {
  1275. struct iscsi_conn *conn = cls_conn->dd_data;
  1276. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1277. bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
  1278. bnx2i_update_iscsi_conn(conn);
  1279. /*
  1280. * this should normally not sleep for a long time so it should
  1281. * not disrupt the caller.
  1282. */
  1283. bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
  1284. bnx2i_conn->ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1285. bnx2i_conn->ep->ofld_timer.data = (unsigned long) bnx2i_conn->ep;
  1286. add_timer(&bnx2i_conn->ep->ofld_timer);
  1287. /* update iSCSI context for this conn, wait for CNIC to complete */
  1288. wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
  1289. bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
  1290. if (signal_pending(current))
  1291. flush_signals(current);
  1292. del_timer_sync(&bnx2i_conn->ep->ofld_timer);
  1293. iscsi_conn_start(cls_conn);
  1294. return 0;
  1295. }
  1296. /**
  1297. * bnx2i_conn_get_stats - returns iSCSI stats
  1298. * @cls_conn: pointer to iscsi cls conn
  1299. * @stats: pointer to iscsi statistic struct
  1300. */
  1301. static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  1302. struct iscsi_stats *stats)
  1303. {
  1304. struct iscsi_conn *conn = cls_conn->dd_data;
  1305. stats->txdata_octets = conn->txdata_octets;
  1306. stats->rxdata_octets = conn->rxdata_octets;
  1307. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1308. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1309. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1310. stats->datain_pdus = conn->datain_pdus_cnt;
  1311. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1312. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1313. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1314. stats->custom_length = 3;
  1315. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1316. stats->custom[2].value = conn->eh_abort_cnt;
  1317. stats->digest_err = 0;
  1318. stats->timeout_err = 0;
  1319. stats->custom_length = 0;
  1320. }
  1321. /**
  1322. * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
  1323. * @dst_addr: target IP address
  1324. *
  1325. * check if route resolves to BNX2 device
  1326. */
  1327. static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
  1328. {
  1329. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1330. struct bnx2i_hba *hba;
  1331. struct cnic_dev *cnic = NULL;
  1332. bnx2i_reg_dev_all();
  1333. hba = get_adapter_list_head();
  1334. if (hba && hba->cnic)
  1335. cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
  1336. if (!cnic) {
  1337. printk(KERN_ALERT "bnx2i: no route,"
  1338. "can't connect using cnic\n");
  1339. goto no_nx2_route;
  1340. }
  1341. hba = bnx2i_find_hba_for_cnic(cnic);
  1342. if (!hba)
  1343. goto no_nx2_route;
  1344. if (bnx2i_adapter_ready(hba)) {
  1345. printk(KERN_ALERT "bnx2i: check route, hba not found\n");
  1346. goto no_nx2_route;
  1347. }
  1348. if (hba->netdev->mtu > hba->mtu_supported) {
  1349. printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
  1350. hba->netdev->name, hba->netdev->mtu);
  1351. printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
  1352. hba->mtu_supported);
  1353. goto no_nx2_route;
  1354. }
  1355. return hba;
  1356. no_nx2_route:
  1357. return NULL;
  1358. }
  1359. /**
  1360. * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
  1361. * @hba: pointer to adapter instance
  1362. * @ep: endpoint (transport indentifier) structure
  1363. *
  1364. * destroys cm_sock structure and on chip iscsi context
  1365. */
  1366. static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
  1367. struct bnx2i_endpoint *ep)
  1368. {
  1369. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
  1370. hba->cnic->cm_destroy(ep->cm_sk);
  1371. if (test_bit(ADAPTER_STATE_GOING_DOWN, &ep->hba->adapter_state))
  1372. ep->state = EP_STATE_DISCONN_COMPL;
  1373. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
  1374. ep->state == EP_STATE_DISCONN_TIMEDOUT) {
  1375. printk(KERN_ALERT "bnx2i - ERROR - please submit GRC Dump,"
  1376. " NW/PCIe trace, driver msgs to developers"
  1377. " for analysis\n");
  1378. return 1;
  1379. }
  1380. ep->state = EP_STATE_CLEANUP_START;
  1381. init_timer(&ep->ofld_timer);
  1382. ep->ofld_timer.expires = 10*HZ + jiffies;
  1383. ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1384. ep->ofld_timer.data = (unsigned long) ep;
  1385. add_timer(&ep->ofld_timer);
  1386. bnx2i_ep_destroy_list_add(hba, ep);
  1387. /* destroy iSCSI context, wait for it to complete */
  1388. bnx2i_send_conn_destroy(hba, ep);
  1389. wait_event_interruptible(ep->ofld_wait,
  1390. (ep->state != EP_STATE_CLEANUP_START));
  1391. if (signal_pending(current))
  1392. flush_signals(current);
  1393. del_timer_sync(&ep->ofld_timer);
  1394. bnx2i_ep_destroy_list_del(hba, ep);
  1395. if (ep->state != EP_STATE_CLEANUP_CMPL)
  1396. /* should never happen */
  1397. printk(KERN_ALERT "bnx2i - conn destroy failed\n");
  1398. return 0;
  1399. }
  1400. /**
  1401. * bnx2i_ep_connect - establish TCP connection to target portal
  1402. * @shost: scsi host
  1403. * @dst_addr: target IP address
  1404. * @non_blocking: blocking or non-blocking call
  1405. *
  1406. * this routine initiates the TCP/IP connection by invoking Option-2 i/f
  1407. * with l5_core and the CNIC. This is a multi-step process of resolving
  1408. * route to target, create a iscsi connection context, handshaking with
  1409. * CNIC module to create/initialize the socket struct and finally
  1410. * sending down option-2 request to complete TCP 3-way handshake
  1411. */
  1412. static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
  1413. struct sockaddr *dst_addr,
  1414. int non_blocking)
  1415. {
  1416. u32 iscsi_cid = BNX2I_CID_RESERVED;
  1417. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1418. struct sockaddr_in6 *desti6;
  1419. struct bnx2i_endpoint *bnx2i_ep;
  1420. struct bnx2i_hba *hba;
  1421. struct cnic_dev *cnic;
  1422. struct cnic_sockaddr saddr;
  1423. struct iscsi_endpoint *ep;
  1424. int rc = 0;
  1425. if (shost)
  1426. /* driver is given scsi host to work with */
  1427. hba = iscsi_host_priv(shost);
  1428. else
  1429. /*
  1430. * check if the given destination can be reached through
  1431. * a iscsi capable NetXtreme2 device
  1432. */
  1433. hba = bnx2i_check_route(dst_addr);
  1434. if (!hba) {
  1435. rc = -ENOMEM;
  1436. goto check_busy;
  1437. }
  1438. cnic = hba->cnic;
  1439. ep = bnx2i_alloc_ep(hba);
  1440. if (!ep) {
  1441. rc = -ENOMEM;
  1442. goto check_busy;
  1443. }
  1444. bnx2i_ep = ep->dd_data;
  1445. mutex_lock(&hba->net_dev_lock);
  1446. if (bnx2i_adapter_ready(hba)) {
  1447. rc = -EPERM;
  1448. goto net_if_down;
  1449. }
  1450. bnx2i_ep->state = EP_STATE_IDLE;
  1451. bnx2i_ep->ep_iscsi_cid = (u16) -1;
  1452. bnx2i_ep->num_active_cmds = 0;
  1453. iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
  1454. if (iscsi_cid == -1) {
  1455. printk(KERN_ALERT "alloc_ep: unable to allocate iscsi cid\n");
  1456. rc = -ENOMEM;
  1457. goto iscsi_cid_err;
  1458. }
  1459. bnx2i_ep->hba_age = hba->age;
  1460. rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
  1461. if (rc != 0) {
  1462. printk(KERN_ALERT "bnx2i: ep_conn, alloc QP resc error\n");
  1463. rc = -ENOMEM;
  1464. goto qp_resc_err;
  1465. }
  1466. bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
  1467. bnx2i_ep->state = EP_STATE_OFLD_START;
  1468. bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
  1469. init_timer(&bnx2i_ep->ofld_timer);
  1470. bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
  1471. bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1472. bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
  1473. add_timer(&bnx2i_ep->ofld_timer);
  1474. bnx2i_send_conn_ofld_req(hba, bnx2i_ep);
  1475. /* Wait for CNIC hardware to setup conn context and return 'cid' */
  1476. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1477. bnx2i_ep->state != EP_STATE_OFLD_START);
  1478. if (signal_pending(current))
  1479. flush_signals(current);
  1480. del_timer_sync(&bnx2i_ep->ofld_timer);
  1481. bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
  1482. if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
  1483. rc = -ENOSPC;
  1484. goto conn_failed;
  1485. }
  1486. rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
  1487. iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
  1488. if (rc) {
  1489. rc = -EINVAL;
  1490. goto conn_failed;
  1491. }
  1492. bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
  1493. bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
  1494. clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
  1495. memset(&saddr, 0, sizeof(saddr));
  1496. if (dst_addr->sa_family == AF_INET) {
  1497. desti = (struct sockaddr_in *) dst_addr;
  1498. saddr.remote.v4 = *desti;
  1499. saddr.local.v4.sin_family = desti->sin_family;
  1500. } else if (dst_addr->sa_family == AF_INET6) {
  1501. desti6 = (struct sockaddr_in6 *) dst_addr;
  1502. saddr.remote.v6 = *desti6;
  1503. saddr.local.v6.sin6_family = desti6->sin6_family;
  1504. }
  1505. bnx2i_ep->timestamp = jiffies;
  1506. bnx2i_ep->state = EP_STATE_CONNECT_START;
  1507. if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  1508. rc = -EINVAL;
  1509. goto conn_failed;
  1510. } else
  1511. rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
  1512. if (rc)
  1513. goto release_ep;
  1514. if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
  1515. goto release_ep;
  1516. mutex_unlock(&hba->net_dev_lock);
  1517. return ep;
  1518. release_ep:
  1519. if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
  1520. mutex_unlock(&hba->net_dev_lock);
  1521. return ERR_PTR(rc);
  1522. }
  1523. conn_failed:
  1524. net_if_down:
  1525. iscsi_cid_err:
  1526. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1527. qp_resc_err:
  1528. bnx2i_free_ep(ep);
  1529. mutex_unlock(&hba->net_dev_lock);
  1530. check_busy:
  1531. bnx2i_unreg_dev_all();
  1532. return ERR_PTR(rc);
  1533. }
  1534. /**
  1535. * bnx2i_ep_poll - polls for TCP connection establishement
  1536. * @ep: TCP connection (endpoint) handle
  1537. * @timeout_ms: timeout value in milli secs
  1538. *
  1539. * polls for TCP connect request to complete
  1540. */
  1541. static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  1542. {
  1543. struct bnx2i_endpoint *bnx2i_ep;
  1544. int rc = 0;
  1545. bnx2i_ep = ep->dd_data;
  1546. if ((bnx2i_ep->state == EP_STATE_IDLE) ||
  1547. (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
  1548. (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
  1549. return -1;
  1550. if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
  1551. return 1;
  1552. rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
  1553. ((bnx2i_ep->state ==
  1554. EP_STATE_OFLD_FAILED) ||
  1555. (bnx2i_ep->state ==
  1556. EP_STATE_CONNECT_FAILED) ||
  1557. (bnx2i_ep->state ==
  1558. EP_STATE_CONNECT_COMPL)),
  1559. msecs_to_jiffies(timeout_ms));
  1560. if (!rc || (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
  1561. rc = -1;
  1562. if (rc > 0)
  1563. return 1;
  1564. else if (!rc)
  1565. return 0; /* timeout */
  1566. else
  1567. return rc;
  1568. }
  1569. /**
  1570. * bnx2i_ep_tcp_conn_active - check EP state transition
  1571. * @ep: endpoint pointer
  1572. *
  1573. * check if underlying TCP connection is active
  1574. */
  1575. static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
  1576. {
  1577. int ret;
  1578. int cnic_dev_10g = 0;
  1579. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1580. cnic_dev_10g = 1;
  1581. switch (bnx2i_ep->state) {
  1582. case EP_STATE_CONNECT_START:
  1583. case EP_STATE_CLEANUP_FAILED:
  1584. case EP_STATE_OFLD_FAILED:
  1585. case EP_STATE_DISCONN_TIMEDOUT:
  1586. ret = 0;
  1587. break;
  1588. case EP_STATE_CONNECT_COMPL:
  1589. case EP_STATE_ULP_UPDATE_START:
  1590. case EP_STATE_ULP_UPDATE_COMPL:
  1591. case EP_STATE_TCP_FIN_RCVD:
  1592. case EP_STATE_ULP_UPDATE_FAILED:
  1593. ret = 1;
  1594. break;
  1595. case EP_STATE_TCP_RST_RCVD:
  1596. ret = 0;
  1597. break;
  1598. case EP_STATE_CONNECT_FAILED:
  1599. if (cnic_dev_10g)
  1600. ret = 1;
  1601. else
  1602. ret = 0;
  1603. break;
  1604. default:
  1605. ret = 0;
  1606. }
  1607. return ret;
  1608. }
  1609. /**
  1610. * bnx2i_ep_disconnect - executes TCP connection teardown process
  1611. * @ep: TCP connection (endpoint) handle
  1612. *
  1613. * executes TCP connection teardown process
  1614. */
  1615. static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
  1616. {
  1617. struct bnx2i_endpoint *bnx2i_ep;
  1618. struct bnx2i_conn *bnx2i_conn = NULL;
  1619. struct iscsi_session *session = NULL;
  1620. struct iscsi_conn *conn;
  1621. struct cnic_dev *cnic;
  1622. struct bnx2i_hba *hba;
  1623. bnx2i_ep = ep->dd_data;
  1624. /* driver should not attempt connection cleanup untill TCP_CONNECT
  1625. * completes either successfully or fails. Timeout is 9-secs, so
  1626. * wait for it to complete
  1627. */
  1628. while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
  1629. !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
  1630. msleep(250);
  1631. if (bnx2i_ep->conn) {
  1632. bnx2i_conn = bnx2i_ep->conn;
  1633. conn = bnx2i_conn->cls_conn->dd_data;
  1634. session = conn->session;
  1635. spin_lock_bh(&session->lock);
  1636. bnx2i_conn->is_bound = 0;
  1637. spin_unlock_bh(&session->lock);
  1638. }
  1639. hba = bnx2i_ep->hba;
  1640. if (bnx2i_ep->state == EP_STATE_IDLE)
  1641. goto return_bnx2i_ep;
  1642. cnic = hba->cnic;
  1643. mutex_lock(&hba->net_dev_lock);
  1644. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
  1645. goto free_resc;
  1646. if (bnx2i_ep->hba_age != hba->age)
  1647. goto free_resc;
  1648. if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
  1649. goto destory_conn;
  1650. bnx2i_ep->state = EP_STATE_DISCONN_START;
  1651. init_timer(&bnx2i_ep->ofld_timer);
  1652. bnx2i_ep->ofld_timer.expires = 10*HZ + jiffies;
  1653. bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
  1654. bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
  1655. add_timer(&bnx2i_ep->ofld_timer);
  1656. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  1657. int close = 0;
  1658. if (session) {
  1659. spin_lock_bh(&session->lock);
  1660. if (session->state == ISCSI_STATE_LOGGING_OUT)
  1661. close = 1;
  1662. spin_unlock_bh(&session->lock);
  1663. }
  1664. if (close)
  1665. cnic->cm_close(bnx2i_ep->cm_sk);
  1666. else
  1667. cnic->cm_abort(bnx2i_ep->cm_sk);
  1668. } else
  1669. goto free_resc;
  1670. /* wait for option-2 conn teardown */
  1671. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1672. bnx2i_ep->state != EP_STATE_DISCONN_START);
  1673. if (signal_pending(current))
  1674. flush_signals(current);
  1675. del_timer_sync(&bnx2i_ep->ofld_timer);
  1676. destory_conn:
  1677. if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
  1678. mutex_unlock(&hba->net_dev_lock);
  1679. return;
  1680. }
  1681. free_resc:
  1682. mutex_unlock(&hba->net_dev_lock);
  1683. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1684. return_bnx2i_ep:
  1685. if (bnx2i_conn)
  1686. bnx2i_conn->ep = NULL;
  1687. bnx2i_free_ep(ep);
  1688. if (!hba->ofld_conns_active)
  1689. bnx2i_unreg_dev_all();
  1690. }
  1691. /**
  1692. * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
  1693. * @buf: pointer to buffer containing iscsi path message
  1694. *
  1695. */
  1696. static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
  1697. {
  1698. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1699. char *buf = (char *) params;
  1700. u16 len = sizeof(*params);
  1701. /* handled by cnic driver */
  1702. hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
  1703. len);
  1704. return 0;
  1705. }
  1706. /*
  1707. * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
  1708. * used while registering with the scsi host and iSCSI transport module.
  1709. */
  1710. static struct scsi_host_template bnx2i_host_template = {
  1711. .module = THIS_MODULE,
  1712. .name = "Broadcom Offload iSCSI Initiator",
  1713. .proc_name = "bnx2i",
  1714. .queuecommand = iscsi_queuecommand,
  1715. .eh_abort_handler = iscsi_eh_abort,
  1716. .eh_device_reset_handler = iscsi_eh_device_reset,
  1717. .eh_target_reset_handler = iscsi_eh_target_reset,
  1718. .can_queue = 1024,
  1719. .max_sectors = 127,
  1720. .cmd_per_lun = 32,
  1721. .this_id = -1,
  1722. .use_clustering = ENABLE_CLUSTERING,
  1723. .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
  1724. .shost_attrs = bnx2i_dev_attributes,
  1725. };
  1726. struct iscsi_transport bnx2i_iscsi_transport = {
  1727. .owner = THIS_MODULE,
  1728. .name = "bnx2i",
  1729. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
  1730. CAP_MULTI_R2T | CAP_DATADGST |
  1731. CAP_DATA_PATH_OFFLOAD,
  1732. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1733. ISCSI_MAX_XMIT_DLENGTH |
  1734. ISCSI_HDRDGST_EN |
  1735. ISCSI_DATADGST_EN |
  1736. ISCSI_INITIAL_R2T_EN |
  1737. ISCSI_MAX_R2T |
  1738. ISCSI_IMM_DATA_EN |
  1739. ISCSI_FIRST_BURST |
  1740. ISCSI_MAX_BURST |
  1741. ISCSI_PDU_INORDER_EN |
  1742. ISCSI_DATASEQ_INORDER_EN |
  1743. ISCSI_ERL |
  1744. ISCSI_CONN_PORT |
  1745. ISCSI_CONN_ADDRESS |
  1746. ISCSI_EXP_STATSN |
  1747. ISCSI_PERSISTENT_PORT |
  1748. ISCSI_PERSISTENT_ADDRESS |
  1749. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1750. ISCSI_USERNAME | ISCSI_PASSWORD |
  1751. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1752. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1753. ISCSI_LU_RESET_TMO |
  1754. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  1755. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  1756. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_NETDEV_NAME,
  1757. .create_session = bnx2i_session_create,
  1758. .destroy_session = bnx2i_session_destroy,
  1759. .create_conn = bnx2i_conn_create,
  1760. .bind_conn = bnx2i_conn_bind,
  1761. .destroy_conn = bnx2i_conn_destroy,
  1762. .set_param = iscsi_set_param,
  1763. .get_conn_param = bnx2i_conn_get_param,
  1764. .get_session_param = iscsi_session_get_param,
  1765. .get_host_param = bnx2i_host_get_param,
  1766. .start_conn = bnx2i_conn_start,
  1767. .stop_conn = iscsi_conn_stop,
  1768. .send_pdu = iscsi_conn_send_pdu,
  1769. .xmit_task = bnx2i_task_xmit,
  1770. .get_stats = bnx2i_conn_get_stats,
  1771. /* TCP connect - disconnect - option-2 interface calls */
  1772. .ep_connect = bnx2i_ep_connect,
  1773. .ep_poll = bnx2i_ep_poll,
  1774. .ep_disconnect = bnx2i_ep_disconnect,
  1775. .set_path = bnx2i_nl_set_path,
  1776. /* Error recovery timeout call */
  1777. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1778. .cleanup_task = bnx2i_cleanup_task,
  1779. };