bnx2i_iscsi.c 62 KB

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