bnx2i_iscsi.c 55 KB

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