bnx2i_iscsi.c 61 KB

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