bnx2i_iscsi.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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 ([email protected])
  14. * Previously Maintained by: Eddie Wai ([email protected])
  15. * Maintained by: [email protected]
  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, false);
  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. wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
  1012. msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
  1013. spin_lock_bh(&conn->session->back_lock);
  1014. }
  1015. bnx2i_iscsi_unmap_sg_list(task->dd_data);
  1016. }
  1017. /**
  1018. * bnx2i_mtask_xmit - transmit mtask to chip for further processing
  1019. * @conn: transport layer conn structure pointer
  1020. * @task: transport layer command structure pointer
  1021. */
  1022. static int
  1023. bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
  1024. {
  1025. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1026. struct bnx2i_hba *hba = bnx2i_conn->hba;
  1027. struct bnx2i_cmd *cmd = task->dd_data;
  1028. memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
  1029. bnx2i_setup_cmd_wqe_template(cmd);
  1030. bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
  1031. /* Tx PDU/data length count */
  1032. ADD_STATS_64(hba, tx_pdus, 1);
  1033. ADD_STATS_64(hba, tx_bytes, task->data_count);
  1034. if (task->data_count) {
  1035. memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
  1036. task->data_count);
  1037. bnx2i_conn->gen_pdu.req_wr_ptr =
  1038. bnx2i_conn->gen_pdu.req_buf + task->data_count;
  1039. }
  1040. cmd->conn = conn->dd_data;
  1041. cmd->scsi_cmd = NULL;
  1042. return bnx2i_iscsi_send_generic_request(task);
  1043. }
  1044. /**
  1045. * bnx2i_task_xmit - transmit iscsi command to chip for further processing
  1046. * @task: transport layer command structure pointer
  1047. *
  1048. * maps SG buffers and send request to chip/firmware in the form of SQ WQE
  1049. */
  1050. static int bnx2i_task_xmit(struct iscsi_task *task)
  1051. {
  1052. struct iscsi_conn *conn = task->conn;
  1053. struct iscsi_session *session = conn->session;
  1054. struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
  1055. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1056. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1057. struct scsi_cmnd *sc = task->sc;
  1058. struct bnx2i_cmd *cmd = task->dd_data;
  1059. struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
  1060. if (atomic_read(&bnx2i_conn->ep->num_active_cmds) + 1 >
  1061. hba->max_sqes)
  1062. return -ENOMEM;
  1063. /*
  1064. * If there is no scsi_cmnd this must be a mgmt task
  1065. */
  1066. if (!sc)
  1067. return bnx2i_mtask_xmit(conn, task);
  1068. bnx2i_setup_cmd_wqe_template(cmd);
  1069. cmd->req.op_code = ISCSI_OP_SCSI_CMD;
  1070. cmd->conn = bnx2i_conn;
  1071. cmd->scsi_cmd = sc;
  1072. cmd->req.total_data_transfer_length = scsi_bufflen(sc);
  1073. cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
  1074. bnx2i_iscsi_map_sg_list(cmd);
  1075. bnx2i_cpy_scsi_cdb(sc, cmd);
  1076. cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
  1077. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1078. cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
  1079. cmd->req.itt = task->itt |
  1080. (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1081. bnx2i_setup_write_cmd_bd_info(task);
  1082. } else {
  1083. if (scsi_bufflen(sc))
  1084. cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
  1085. cmd->req.itt = task->itt |
  1086. (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
  1087. }
  1088. cmd->req.num_bds = cmd->io_tbl.bd_valid;
  1089. if (!cmd->io_tbl.bd_valid) {
  1090. cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
  1091. cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
  1092. cmd->req.num_bds = 1;
  1093. }
  1094. bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
  1095. return 0;
  1096. }
  1097. /**
  1098. * bnx2i_session_create - create a new iscsi session
  1099. * @ep: pointer to iscsi endpoint
  1100. * @cmds_max: user specified maximum commands
  1101. * @qdepth: scsi queue depth to support
  1102. * @initial_cmdsn: initial iscsi CMDSN to be used for this session
  1103. *
  1104. * Creates a new iSCSI session instance on given device.
  1105. */
  1106. static struct iscsi_cls_session *
  1107. bnx2i_session_create(struct iscsi_endpoint *ep,
  1108. uint16_t cmds_max, uint16_t qdepth,
  1109. uint32_t initial_cmdsn)
  1110. {
  1111. struct Scsi_Host *shost;
  1112. struct iscsi_cls_session *cls_session;
  1113. struct bnx2i_hba *hba;
  1114. struct bnx2i_endpoint *bnx2i_ep;
  1115. if (!ep) {
  1116. printk(KERN_ERR "bnx2i: missing ep.\n");
  1117. return NULL;
  1118. }
  1119. bnx2i_ep = ep->dd_data;
  1120. shost = bnx2i_ep->hba->shost;
  1121. hba = iscsi_host_priv(shost);
  1122. if (bnx2i_adapter_ready(hba))
  1123. return NULL;
  1124. /*
  1125. * user can override hw limit as long as it is within
  1126. * the min/max.
  1127. */
  1128. if (cmds_max > hba->max_sqes)
  1129. cmds_max = hba->max_sqes;
  1130. else if (cmds_max < BNX2I_SQ_WQES_MIN)
  1131. cmds_max = BNX2I_SQ_WQES_MIN;
  1132. cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
  1133. cmds_max, 0, sizeof(struct bnx2i_cmd),
  1134. initial_cmdsn, ISCSI_MAX_TARGET);
  1135. if (!cls_session)
  1136. return NULL;
  1137. if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
  1138. goto session_teardown;
  1139. return cls_session;
  1140. session_teardown:
  1141. iscsi_session_teardown(cls_session);
  1142. return NULL;
  1143. }
  1144. /**
  1145. * bnx2i_session_destroy - destroys iscsi session
  1146. * @cls_session: pointer to iscsi cls session
  1147. *
  1148. * Destroys previously created iSCSI session instance and releases
  1149. * all resources held by it
  1150. */
  1151. static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
  1152. {
  1153. struct iscsi_session *session = cls_session->dd_data;
  1154. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1155. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1156. bnx2i_destroy_cmd_pool(hba, session);
  1157. iscsi_session_teardown(cls_session);
  1158. }
  1159. /**
  1160. * bnx2i_conn_create - create iscsi connection instance
  1161. * @cls_session: pointer to iscsi cls session
  1162. * @cid: iscsi cid as per rfc (not NX2's CID terminology)
  1163. *
  1164. * Creates a new iSCSI connection instance for a given session
  1165. */
  1166. static struct iscsi_cls_conn *
  1167. bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
  1168. {
  1169. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1170. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1171. struct bnx2i_conn *bnx2i_conn;
  1172. struct iscsi_cls_conn *cls_conn;
  1173. struct iscsi_conn *conn;
  1174. cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
  1175. cid);
  1176. if (!cls_conn)
  1177. return NULL;
  1178. conn = cls_conn->dd_data;
  1179. bnx2i_conn = conn->dd_data;
  1180. bnx2i_conn->cls_conn = cls_conn;
  1181. bnx2i_conn->hba = hba;
  1182. atomic_set(&bnx2i_conn->work_cnt, 0);
  1183. /* 'ep' ptr will be assigned in bind() call */
  1184. bnx2i_conn->ep = NULL;
  1185. init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
  1186. if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
  1187. iscsi_conn_printk(KERN_ALERT, conn,
  1188. "conn_new: login resc alloc failed!!\n");
  1189. goto free_conn;
  1190. }
  1191. return cls_conn;
  1192. free_conn:
  1193. iscsi_conn_teardown(cls_conn);
  1194. return NULL;
  1195. }
  1196. /**
  1197. * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
  1198. * @cls_session: pointer to iscsi cls session
  1199. * @cls_conn: pointer to iscsi cls conn
  1200. * @transport_fd: 64-bit EP handle
  1201. * @is_leading: leading connection on this session?
  1202. *
  1203. * Binds together iSCSI session instance, iSCSI connection instance
  1204. * and the TCP connection. This routine returns error code if
  1205. * TCP connection does not belong on the device iSCSI sess/conn
  1206. * is bound
  1207. */
  1208. static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
  1209. struct iscsi_cls_conn *cls_conn,
  1210. uint64_t transport_fd, int is_leading)
  1211. {
  1212. struct iscsi_conn *conn = cls_conn->dd_data;
  1213. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1214. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1215. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1216. struct bnx2i_endpoint *bnx2i_ep;
  1217. struct iscsi_endpoint *ep;
  1218. int ret_code;
  1219. ep = iscsi_lookup_endpoint(transport_fd);
  1220. if (!ep)
  1221. return -EINVAL;
  1222. /*
  1223. * Forcefully terminate all in progress connection recovery at the
  1224. * earliest, either in bind(), send_pdu(LOGIN), or conn_start()
  1225. */
  1226. if (bnx2i_adapter_ready(hba)) {
  1227. ret_code = -EIO;
  1228. goto put_ep;
  1229. }
  1230. bnx2i_ep = ep->dd_data;
  1231. if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
  1232. (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD)) {
  1233. /* Peer disconnect via' FIN or RST */
  1234. ret_code = -EINVAL;
  1235. goto put_ep;
  1236. }
  1237. if (iscsi_conn_bind(cls_session, cls_conn, is_leading)) {
  1238. ret_code = -EINVAL;
  1239. goto put_ep;
  1240. }
  1241. if (bnx2i_ep->hba != hba) {
  1242. /* Error - TCP connection does not belong to this device
  1243. */
  1244. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1245. "conn bind, ep=0x%p (%s) does not",
  1246. bnx2i_ep, bnx2i_ep->hba->netdev->name);
  1247. iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
  1248. "belong to hba (%s)\n",
  1249. hba->netdev->name);
  1250. ret_code = -EEXIST;
  1251. goto put_ep;
  1252. }
  1253. bnx2i_ep->conn = bnx2i_conn;
  1254. bnx2i_conn->ep = bnx2i_ep;
  1255. bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
  1256. bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
  1257. ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
  1258. bnx2i_ep->ep_iscsi_cid);
  1259. /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
  1260. * driver needs to explicitly replenish RQ index during setup.
  1261. */
  1262. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1263. bnx2i_put_rq_buf(bnx2i_conn, 0);
  1264. bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
  1265. put_ep:
  1266. iscsi_put_endpoint(ep);
  1267. return ret_code;
  1268. }
  1269. /**
  1270. * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
  1271. * @cls_conn: pointer to iscsi cls conn
  1272. *
  1273. * Destroy an iSCSI connection instance and release memory resources held by
  1274. * this connection
  1275. */
  1276. static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1277. {
  1278. struct iscsi_conn *conn = cls_conn->dd_data;
  1279. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1280. struct Scsi_Host *shost;
  1281. struct bnx2i_hba *hba;
  1282. struct bnx2i_work *work, *tmp;
  1283. unsigned cpu = 0;
  1284. struct bnx2i_percpu_s *p;
  1285. shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
  1286. hba = iscsi_host_priv(shost);
  1287. bnx2i_conn_free_login_resources(hba, bnx2i_conn);
  1288. if (atomic_read(&bnx2i_conn->work_cnt)) {
  1289. for_each_online_cpu(cpu) {
  1290. p = &per_cpu(bnx2i_percpu, cpu);
  1291. spin_lock_bh(&p->p_work_lock);
  1292. list_for_each_entry_safe(work, tmp,
  1293. &p->work_list, list) {
  1294. if (work->session == conn->session &&
  1295. work->bnx2i_conn == bnx2i_conn) {
  1296. list_del_init(&work->list);
  1297. kfree(work);
  1298. if (!atomic_dec_and_test(
  1299. &bnx2i_conn->work_cnt))
  1300. break;
  1301. }
  1302. }
  1303. spin_unlock_bh(&p->p_work_lock);
  1304. }
  1305. }
  1306. iscsi_conn_teardown(cls_conn);
  1307. }
  1308. /**
  1309. * bnx2i_ep_get_param - return iscsi ep parameter to caller
  1310. * @ep: pointer to iscsi endpoint
  1311. * @param: parameter type identifier
  1312. * @buf: buffer pointer
  1313. *
  1314. * returns iSCSI ep parameters
  1315. */
  1316. static int bnx2i_ep_get_param(struct iscsi_endpoint *ep,
  1317. enum iscsi_param param, char *buf)
  1318. {
  1319. struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
  1320. struct bnx2i_hba *hba = bnx2i_ep->hba;
  1321. int len = -ENOTCONN;
  1322. if (!hba)
  1323. return -ENOTCONN;
  1324. switch (param) {
  1325. case ISCSI_PARAM_CONN_PORT:
  1326. mutex_lock(&hba->net_dev_lock);
  1327. if (bnx2i_ep->cm_sk)
  1328. len = sprintf(buf, "%hu\n", bnx2i_ep->cm_sk->dst_port);
  1329. mutex_unlock(&hba->net_dev_lock);
  1330. break;
  1331. case ISCSI_PARAM_CONN_ADDRESS:
  1332. mutex_lock(&hba->net_dev_lock);
  1333. if (bnx2i_ep->cm_sk)
  1334. len = sprintf(buf, "%pI4\n", &bnx2i_ep->cm_sk->dst_ip);
  1335. mutex_unlock(&hba->net_dev_lock);
  1336. break;
  1337. default:
  1338. return -ENOSYS;
  1339. }
  1340. return len;
  1341. }
  1342. /**
  1343. * bnx2i_host_get_param - returns host (adapter) related parameters
  1344. * @shost: scsi host pointer
  1345. * @param: parameter type identifier
  1346. * @buf: buffer pointer
  1347. */
  1348. static int bnx2i_host_get_param(struct Scsi_Host *shost,
  1349. enum iscsi_host_param param, char *buf)
  1350. {
  1351. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1352. int len = 0;
  1353. switch (param) {
  1354. case ISCSI_HOST_PARAM_HWADDRESS:
  1355. len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
  1356. break;
  1357. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1358. len = sprintf(buf, "%s\n", hba->netdev->name);
  1359. break;
  1360. case ISCSI_HOST_PARAM_IPADDRESS: {
  1361. struct list_head *active_list = &hba->ep_active_list;
  1362. read_lock_bh(&hba->ep_rdwr_lock);
  1363. if (!list_empty(&hba->ep_active_list)) {
  1364. struct bnx2i_endpoint *bnx2i_ep;
  1365. struct cnic_sock *csk;
  1366. bnx2i_ep = list_first_entry(active_list,
  1367. struct bnx2i_endpoint,
  1368. link);
  1369. csk = bnx2i_ep->cm_sk;
  1370. if (test_bit(SK_F_IPV6, &csk->flags))
  1371. len = sprintf(buf, "%pI6\n", csk->src_ip);
  1372. else
  1373. len = sprintf(buf, "%pI4\n", csk->src_ip);
  1374. }
  1375. read_unlock_bh(&hba->ep_rdwr_lock);
  1376. break;
  1377. }
  1378. default:
  1379. return iscsi_host_get_param(shost, param, buf);
  1380. }
  1381. return len;
  1382. }
  1383. /**
  1384. * bnx2i_conn_start - completes iscsi connection migration to FFP
  1385. * @cls_conn: pointer to iscsi cls conn
  1386. *
  1387. * last call in FFP migration to handover iscsi conn to the driver
  1388. */
  1389. static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
  1390. {
  1391. struct iscsi_conn *conn = cls_conn->dd_data;
  1392. struct bnx2i_conn *bnx2i_conn = conn->dd_data;
  1393. bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
  1394. bnx2i_update_iscsi_conn(conn);
  1395. /*
  1396. * this should normally not sleep for a long time so it should
  1397. * not disrupt the caller.
  1398. */
  1399. timer_setup(&bnx2i_conn->ep->ofld_timer, bnx2i_ep_ofld_timer, 0);
  1400. bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
  1401. add_timer(&bnx2i_conn->ep->ofld_timer);
  1402. /* update iSCSI context for this conn, wait for CNIC to complete */
  1403. wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
  1404. bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
  1405. if (signal_pending(current))
  1406. flush_signals(current);
  1407. del_timer_sync(&bnx2i_conn->ep->ofld_timer);
  1408. iscsi_conn_start(cls_conn);
  1409. return 0;
  1410. }
  1411. /**
  1412. * bnx2i_conn_get_stats - returns iSCSI stats
  1413. * @cls_conn: pointer to iscsi cls conn
  1414. * @stats: pointer to iscsi statistic struct
  1415. */
  1416. static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  1417. struct iscsi_stats *stats)
  1418. {
  1419. struct iscsi_conn *conn = cls_conn->dd_data;
  1420. stats->txdata_octets = conn->txdata_octets;
  1421. stats->rxdata_octets = conn->rxdata_octets;
  1422. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1423. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1424. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1425. stats->datain_pdus = conn->datain_pdus_cnt;
  1426. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1427. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1428. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1429. stats->digest_err = 0;
  1430. stats->timeout_err = 0;
  1431. strcpy(stats->custom[0].desc, "eh_abort_cnt");
  1432. stats->custom[0].value = conn->eh_abort_cnt;
  1433. stats->custom_length = 1;
  1434. }
  1435. /**
  1436. * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
  1437. * @dst_addr: target IP address
  1438. *
  1439. * check if route resolves to BNX2 device
  1440. */
  1441. static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
  1442. {
  1443. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1444. struct bnx2i_hba *hba;
  1445. struct cnic_dev *cnic = NULL;
  1446. hba = get_adapter_list_head();
  1447. if (hba && hba->cnic)
  1448. cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
  1449. if (!cnic) {
  1450. printk(KERN_ALERT "bnx2i: no route,"
  1451. "can't connect using cnic\n");
  1452. goto no_nx2_route;
  1453. }
  1454. hba = bnx2i_find_hba_for_cnic(cnic);
  1455. if (!hba)
  1456. goto no_nx2_route;
  1457. if (bnx2i_adapter_ready(hba)) {
  1458. printk(KERN_ALERT "bnx2i: check route, hba not found\n");
  1459. goto no_nx2_route;
  1460. }
  1461. if (hba->netdev->mtu > hba->mtu_supported) {
  1462. printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
  1463. hba->netdev->name, hba->netdev->mtu);
  1464. printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
  1465. hba->mtu_supported);
  1466. goto no_nx2_route;
  1467. }
  1468. return hba;
  1469. no_nx2_route:
  1470. return NULL;
  1471. }
  1472. /**
  1473. * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
  1474. * @hba: pointer to adapter instance
  1475. * @ep: endpoint (transport identifier) structure
  1476. *
  1477. * destroys cm_sock structure and on chip iscsi context
  1478. */
  1479. static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
  1480. struct bnx2i_endpoint *ep)
  1481. {
  1482. if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) && ep->cm_sk)
  1483. hba->cnic->cm_destroy(ep->cm_sk);
  1484. if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
  1485. ep->state == EP_STATE_DISCONN_TIMEDOUT) {
  1486. if (ep->conn && ep->conn->cls_conn &&
  1487. ep->conn->cls_conn->dd_data) {
  1488. struct iscsi_conn *conn = ep->conn->cls_conn->dd_data;
  1489. /* Must suspend all rx queue activity for this ep */
  1490. set_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags);
  1491. }
  1492. /* CONN_DISCONNECT timeout may or may not be an issue depending
  1493. * on what transcribed in TCP layer, different targets behave
  1494. * differently
  1495. */
  1496. printk(KERN_ALERT "bnx2i (%s): - WARN - CONN_DISCON timed out, "
  1497. "please submit GRC Dump, NW/PCIe trace, "
  1498. "driver msgs to developers for analysis\n",
  1499. hba->netdev->name);
  1500. }
  1501. ep->state = EP_STATE_CLEANUP_START;
  1502. timer_setup(&ep->ofld_timer, bnx2i_ep_ofld_timer, 0);
  1503. ep->ofld_timer.expires = hba->conn_ctx_destroy_tmo + jiffies;
  1504. add_timer(&ep->ofld_timer);
  1505. bnx2i_ep_destroy_list_add(hba, ep);
  1506. /* destroy iSCSI context, wait for it to complete */
  1507. if (bnx2i_send_conn_destroy(hba, ep))
  1508. ep->state = EP_STATE_CLEANUP_CMPL;
  1509. wait_event_interruptible(ep->ofld_wait,
  1510. (ep->state != EP_STATE_CLEANUP_START));
  1511. if (signal_pending(current))
  1512. flush_signals(current);
  1513. del_timer_sync(&ep->ofld_timer);
  1514. bnx2i_ep_destroy_list_del(hba, ep);
  1515. if (ep->state != EP_STATE_CLEANUP_CMPL)
  1516. /* should never happen */
  1517. printk(KERN_ALERT "bnx2i - conn destroy failed\n");
  1518. return 0;
  1519. }
  1520. /**
  1521. * bnx2i_ep_connect - establish TCP connection to target portal
  1522. * @shost: scsi host
  1523. * @dst_addr: target IP address
  1524. * @non_blocking: blocking or non-blocking call
  1525. *
  1526. * this routine initiates the TCP/IP connection by invoking Option-2 i/f
  1527. * with l5_core and the CNIC. This is a multi-step process of resolving
  1528. * route to target, create a iscsi connection context, handshaking with
  1529. * CNIC module to create/initialize the socket struct and finally
  1530. * sending down option-2 request to complete TCP 3-way handshake
  1531. */
  1532. static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
  1533. struct sockaddr *dst_addr,
  1534. int non_blocking)
  1535. {
  1536. u32 iscsi_cid = BNX2I_CID_RESERVED;
  1537. struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
  1538. struct sockaddr_in6 *desti6;
  1539. struct bnx2i_endpoint *bnx2i_ep;
  1540. struct bnx2i_hba *hba;
  1541. struct cnic_dev *cnic;
  1542. struct cnic_sockaddr saddr;
  1543. struct iscsi_endpoint *ep;
  1544. int rc = 0;
  1545. if (shost) {
  1546. /* driver is given scsi host to work with */
  1547. hba = iscsi_host_priv(shost);
  1548. } else
  1549. /*
  1550. * check if the given destination can be reached through
  1551. * a iscsi capable NetXtreme2 device
  1552. */
  1553. hba = bnx2i_check_route(dst_addr);
  1554. if (!hba) {
  1555. rc = -EINVAL;
  1556. goto nohba;
  1557. }
  1558. mutex_lock(&hba->net_dev_lock);
  1559. if (bnx2i_adapter_ready(hba) || !hba->cid_que.cid_free_cnt) {
  1560. rc = -EPERM;
  1561. goto check_busy;
  1562. }
  1563. cnic = hba->cnic;
  1564. ep = bnx2i_alloc_ep(hba);
  1565. if (!ep) {
  1566. rc = -ENOMEM;
  1567. goto check_busy;
  1568. }
  1569. bnx2i_ep = ep->dd_data;
  1570. atomic_set(&bnx2i_ep->num_active_cmds, 0);
  1571. iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
  1572. if (iscsi_cid == -1) {
  1573. printk(KERN_ALERT "bnx2i (%s): alloc_ep - unable to allocate "
  1574. "iscsi cid\n", hba->netdev->name);
  1575. rc = -ENOMEM;
  1576. bnx2i_free_ep(ep);
  1577. goto check_busy;
  1578. }
  1579. bnx2i_ep->hba_age = hba->age;
  1580. rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
  1581. if (rc != 0) {
  1582. printk(KERN_ALERT "bnx2i (%s): ep_conn - alloc QP resc error"
  1583. "\n", hba->netdev->name);
  1584. rc = -ENOMEM;
  1585. goto qp_resc_err;
  1586. }
  1587. bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
  1588. bnx2i_ep->state = EP_STATE_OFLD_START;
  1589. bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
  1590. timer_setup(&bnx2i_ep->ofld_timer, bnx2i_ep_ofld_timer, 0);
  1591. bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
  1592. add_timer(&bnx2i_ep->ofld_timer);
  1593. if (bnx2i_send_conn_ofld_req(hba, bnx2i_ep)) {
  1594. if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
  1595. printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
  1596. hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
  1597. rc = -EBUSY;
  1598. } else
  1599. rc = -ENOSPC;
  1600. printk(KERN_ALERT "bnx2i (%s): unable to send conn offld kwqe"
  1601. "\n", hba->netdev->name);
  1602. bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
  1603. goto conn_failed;
  1604. }
  1605. /* Wait for CNIC hardware to setup conn context and return 'cid' */
  1606. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1607. bnx2i_ep->state != EP_STATE_OFLD_START);
  1608. if (signal_pending(current))
  1609. flush_signals(current);
  1610. del_timer_sync(&bnx2i_ep->ofld_timer);
  1611. bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
  1612. if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
  1613. if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
  1614. printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
  1615. hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
  1616. rc = -EBUSY;
  1617. } else
  1618. rc = -ENOSPC;
  1619. goto conn_failed;
  1620. }
  1621. rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
  1622. iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
  1623. if (rc) {
  1624. rc = -EINVAL;
  1625. /* Need to terminate and cleanup the connection */
  1626. goto release_ep;
  1627. }
  1628. bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
  1629. bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
  1630. clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
  1631. memset(&saddr, 0, sizeof(saddr));
  1632. if (dst_addr->sa_family == AF_INET) {
  1633. desti = (struct sockaddr_in *) dst_addr;
  1634. saddr.remote.v4 = *desti;
  1635. saddr.local.v4.sin_family = desti->sin_family;
  1636. } else if (dst_addr->sa_family == AF_INET6) {
  1637. desti6 = (struct sockaddr_in6 *) dst_addr;
  1638. saddr.remote.v6 = *desti6;
  1639. saddr.local.v6.sin6_family = desti6->sin6_family;
  1640. }
  1641. bnx2i_ep->timestamp = jiffies;
  1642. bnx2i_ep->state = EP_STATE_CONNECT_START;
  1643. if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
  1644. rc = -EINVAL;
  1645. goto conn_failed;
  1646. } else
  1647. rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
  1648. if (rc)
  1649. goto release_ep;
  1650. bnx2i_ep_active_list_add(hba, bnx2i_ep);
  1651. rc = bnx2i_map_ep_dbell_regs(bnx2i_ep);
  1652. if (rc)
  1653. goto del_active_ep;
  1654. mutex_unlock(&hba->net_dev_lock);
  1655. return ep;
  1656. del_active_ep:
  1657. bnx2i_ep_active_list_del(hba, bnx2i_ep);
  1658. release_ep:
  1659. if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
  1660. mutex_unlock(&hba->net_dev_lock);
  1661. return ERR_PTR(rc);
  1662. }
  1663. conn_failed:
  1664. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1665. qp_resc_err:
  1666. bnx2i_free_ep(ep);
  1667. check_busy:
  1668. mutex_unlock(&hba->net_dev_lock);
  1669. nohba:
  1670. return ERR_PTR(rc);
  1671. }
  1672. /**
  1673. * bnx2i_ep_poll - polls for TCP connection establishement
  1674. * @ep: TCP connection (endpoint) handle
  1675. * @timeout_ms: timeout value in milli secs
  1676. *
  1677. * polls for TCP connect request to complete
  1678. */
  1679. static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  1680. {
  1681. struct bnx2i_endpoint *bnx2i_ep;
  1682. int rc = 0;
  1683. bnx2i_ep = ep->dd_data;
  1684. if ((bnx2i_ep->state == EP_STATE_IDLE) ||
  1685. (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
  1686. (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
  1687. return -1;
  1688. if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
  1689. return 1;
  1690. rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
  1691. ((bnx2i_ep->state ==
  1692. EP_STATE_OFLD_FAILED) ||
  1693. (bnx2i_ep->state ==
  1694. EP_STATE_CONNECT_FAILED) ||
  1695. (bnx2i_ep->state ==
  1696. EP_STATE_CONNECT_COMPL)),
  1697. msecs_to_jiffies(timeout_ms));
  1698. if (bnx2i_ep->state == EP_STATE_OFLD_FAILED)
  1699. rc = -1;
  1700. if (rc > 0)
  1701. return 1;
  1702. else if (!rc)
  1703. return 0; /* timeout */
  1704. else
  1705. return rc;
  1706. }
  1707. /**
  1708. * bnx2i_ep_tcp_conn_active - check EP state transition
  1709. * @bnx2i_ep: endpoint pointer
  1710. *
  1711. * check if underlying TCP connection is active
  1712. */
  1713. static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
  1714. {
  1715. int ret;
  1716. int cnic_dev_10g = 0;
  1717. if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
  1718. cnic_dev_10g = 1;
  1719. switch (bnx2i_ep->state) {
  1720. case EP_STATE_CLEANUP_FAILED:
  1721. case EP_STATE_OFLD_FAILED:
  1722. case EP_STATE_DISCONN_TIMEDOUT:
  1723. ret = 0;
  1724. break;
  1725. case EP_STATE_CONNECT_START:
  1726. case EP_STATE_CONNECT_FAILED:
  1727. case EP_STATE_CONNECT_COMPL:
  1728. case EP_STATE_ULP_UPDATE_START:
  1729. case EP_STATE_ULP_UPDATE_COMPL:
  1730. case EP_STATE_TCP_FIN_RCVD:
  1731. case EP_STATE_LOGOUT_SENT:
  1732. case EP_STATE_LOGOUT_RESP_RCVD:
  1733. case EP_STATE_ULP_UPDATE_FAILED:
  1734. ret = 1;
  1735. break;
  1736. case EP_STATE_TCP_RST_RCVD:
  1737. if (cnic_dev_10g)
  1738. ret = 0;
  1739. else
  1740. ret = 1;
  1741. break;
  1742. default:
  1743. ret = 0;
  1744. }
  1745. return ret;
  1746. }
  1747. /**
  1748. * bnx2i_hw_ep_disconnect - executes TCP connection teardown process in the hw
  1749. * @bnx2i_ep: TCP connection (bnx2i endpoint) handle
  1750. *
  1751. * executes TCP connection teardown process
  1752. */
  1753. int bnx2i_hw_ep_disconnect(struct bnx2i_endpoint *bnx2i_ep)
  1754. {
  1755. struct bnx2i_hba *hba = bnx2i_ep->hba;
  1756. struct cnic_dev *cnic;
  1757. struct iscsi_session *session = NULL;
  1758. struct iscsi_conn *conn = NULL;
  1759. int ret = 0;
  1760. int close = 0;
  1761. int close_ret = 0;
  1762. if (!hba)
  1763. return 0;
  1764. cnic = hba->cnic;
  1765. if (!cnic)
  1766. return 0;
  1767. if (bnx2i_ep->state == EP_STATE_IDLE ||
  1768. bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
  1769. return 0;
  1770. if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
  1771. goto destroy_conn;
  1772. if (bnx2i_ep->conn) {
  1773. conn = bnx2i_ep->conn->cls_conn->dd_data;
  1774. session = conn->session;
  1775. }
  1776. timer_setup(&bnx2i_ep->ofld_timer, bnx2i_ep_ofld_timer, 0);
  1777. bnx2i_ep->ofld_timer.expires = hba->conn_teardown_tmo + jiffies;
  1778. add_timer(&bnx2i_ep->ofld_timer);
  1779. if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
  1780. goto out;
  1781. if (session) {
  1782. spin_lock_bh(&session->frwd_lock);
  1783. if (bnx2i_ep->state != EP_STATE_TCP_FIN_RCVD) {
  1784. if (session->state == ISCSI_STATE_LOGGING_OUT) {
  1785. if (bnx2i_ep->state == EP_STATE_LOGOUT_SENT) {
  1786. /* Logout sent, but no resp */
  1787. printk(KERN_ALERT "bnx2i (%s): WARNING"
  1788. " logout response was not "
  1789. "received!\n",
  1790. bnx2i_ep->hba->netdev->name);
  1791. } else if (bnx2i_ep->state ==
  1792. EP_STATE_LOGOUT_RESP_RCVD)
  1793. close = 1;
  1794. }
  1795. } else
  1796. close = 1;
  1797. spin_unlock_bh(&session->frwd_lock);
  1798. }
  1799. bnx2i_ep->state = EP_STATE_DISCONN_START;
  1800. if (close)
  1801. close_ret = cnic->cm_close(bnx2i_ep->cm_sk);
  1802. else
  1803. close_ret = cnic->cm_abort(bnx2i_ep->cm_sk);
  1804. if (close_ret)
  1805. printk(KERN_ALERT "bnx2i (%s): close/abort(%d) returned %d\n",
  1806. bnx2i_ep->hba->netdev->name, close, close_ret);
  1807. else
  1808. /* wait for option-2 conn teardown */
  1809. wait_event_interruptible(bnx2i_ep->ofld_wait,
  1810. ((bnx2i_ep->state != EP_STATE_DISCONN_START)
  1811. && (bnx2i_ep->state != EP_STATE_TCP_FIN_RCVD)));
  1812. if (signal_pending(current))
  1813. flush_signals(current);
  1814. del_timer_sync(&bnx2i_ep->ofld_timer);
  1815. destroy_conn:
  1816. bnx2i_ep_active_list_del(hba, bnx2i_ep);
  1817. if (bnx2i_tear_down_conn(hba, bnx2i_ep))
  1818. return -EINVAL;
  1819. out:
  1820. bnx2i_ep->state = EP_STATE_IDLE;
  1821. return ret;
  1822. }
  1823. /**
  1824. * bnx2i_ep_disconnect - executes TCP connection teardown process
  1825. * @ep: TCP connection (iscsi endpoint) handle
  1826. *
  1827. * executes TCP connection teardown process
  1828. */
  1829. static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
  1830. {
  1831. struct bnx2i_endpoint *bnx2i_ep;
  1832. struct bnx2i_conn *bnx2i_conn = NULL;
  1833. struct bnx2i_hba *hba;
  1834. bnx2i_ep = ep->dd_data;
  1835. /* driver should not attempt connection cleanup until TCP_CONNECT
  1836. * completes either successfully or fails. Timeout is 9-secs, so
  1837. * wait for it to complete
  1838. */
  1839. while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
  1840. !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
  1841. msleep(250);
  1842. if (bnx2i_ep->conn)
  1843. bnx2i_conn = bnx2i_ep->conn;
  1844. hba = bnx2i_ep->hba;
  1845. mutex_lock(&hba->net_dev_lock);
  1846. if (bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
  1847. goto out;
  1848. if (bnx2i_ep->state == EP_STATE_IDLE)
  1849. goto free_resc;
  1850. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) ||
  1851. (bnx2i_ep->hba_age != hba->age)) {
  1852. bnx2i_ep_active_list_del(hba, bnx2i_ep);
  1853. goto free_resc;
  1854. }
  1855. /* Do all chip cleanup here */
  1856. if (bnx2i_hw_ep_disconnect(bnx2i_ep)) {
  1857. mutex_unlock(&hba->net_dev_lock);
  1858. return;
  1859. }
  1860. free_resc:
  1861. bnx2i_free_qp_resc(hba, bnx2i_ep);
  1862. if (bnx2i_conn)
  1863. bnx2i_conn->ep = NULL;
  1864. bnx2i_free_ep(ep);
  1865. out:
  1866. mutex_unlock(&hba->net_dev_lock);
  1867. wake_up_interruptible(&hba->eh_wait);
  1868. }
  1869. /**
  1870. * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
  1871. * @shost: scsi host pointer
  1872. * @params: pointer to buffer containing iscsi path message
  1873. */
  1874. static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
  1875. {
  1876. struct bnx2i_hba *hba = iscsi_host_priv(shost);
  1877. char *buf = (char *) params;
  1878. u16 len = sizeof(*params);
  1879. /* handled by cnic driver */
  1880. hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
  1881. len);
  1882. return 0;
  1883. }
  1884. static umode_t bnx2i_attr_is_visible(int param_type, int param)
  1885. {
  1886. switch (param_type) {
  1887. case ISCSI_HOST_PARAM:
  1888. switch (param) {
  1889. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1890. case ISCSI_HOST_PARAM_HWADDRESS:
  1891. case ISCSI_HOST_PARAM_IPADDRESS:
  1892. return S_IRUGO;
  1893. default:
  1894. return 0;
  1895. }
  1896. case ISCSI_PARAM:
  1897. switch (param) {
  1898. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1899. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1900. case ISCSI_PARAM_HDRDGST_EN:
  1901. case ISCSI_PARAM_DATADGST_EN:
  1902. case ISCSI_PARAM_CONN_ADDRESS:
  1903. case ISCSI_PARAM_CONN_PORT:
  1904. case ISCSI_PARAM_EXP_STATSN:
  1905. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1906. case ISCSI_PARAM_PERSISTENT_PORT:
  1907. case ISCSI_PARAM_PING_TMO:
  1908. case ISCSI_PARAM_RECV_TMO:
  1909. case ISCSI_PARAM_INITIAL_R2T_EN:
  1910. case ISCSI_PARAM_MAX_R2T:
  1911. case ISCSI_PARAM_IMM_DATA_EN:
  1912. case ISCSI_PARAM_FIRST_BURST:
  1913. case ISCSI_PARAM_MAX_BURST:
  1914. case ISCSI_PARAM_PDU_INORDER_EN:
  1915. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1916. case ISCSI_PARAM_ERL:
  1917. case ISCSI_PARAM_TARGET_NAME:
  1918. case ISCSI_PARAM_TPGT:
  1919. case ISCSI_PARAM_USERNAME:
  1920. case ISCSI_PARAM_PASSWORD:
  1921. case ISCSI_PARAM_USERNAME_IN:
  1922. case ISCSI_PARAM_PASSWORD_IN:
  1923. case ISCSI_PARAM_FAST_ABORT:
  1924. case ISCSI_PARAM_ABORT_TMO:
  1925. case ISCSI_PARAM_LU_RESET_TMO:
  1926. case ISCSI_PARAM_TGT_RESET_TMO:
  1927. case ISCSI_PARAM_IFACE_NAME:
  1928. case ISCSI_PARAM_INITIATOR_NAME:
  1929. case ISCSI_PARAM_BOOT_ROOT:
  1930. case ISCSI_PARAM_BOOT_NIC:
  1931. case ISCSI_PARAM_BOOT_TARGET:
  1932. return S_IRUGO;
  1933. default:
  1934. return 0;
  1935. }
  1936. }
  1937. return 0;
  1938. }
  1939. /*
  1940. * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
  1941. * used while registering with the scsi host and iSCSI transport module.
  1942. */
  1943. static struct scsi_host_template bnx2i_host_template = {
  1944. .module = THIS_MODULE,
  1945. .name = "QLogic Offload iSCSI Initiator",
  1946. .proc_name = "bnx2i",
  1947. .queuecommand = iscsi_queuecommand,
  1948. .eh_timed_out = iscsi_eh_cmd_timed_out,
  1949. .eh_abort_handler = iscsi_eh_abort,
  1950. .eh_device_reset_handler = iscsi_eh_device_reset,
  1951. .eh_target_reset_handler = iscsi_eh_recover_target,
  1952. .change_queue_depth = scsi_change_queue_depth,
  1953. .target_alloc = iscsi_target_alloc,
  1954. .can_queue = 2048,
  1955. .max_sectors = 127,
  1956. .cmd_per_lun = 128,
  1957. .this_id = -1,
  1958. .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
  1959. .shost_groups = bnx2i_dev_groups,
  1960. .track_queue_depth = 1,
  1961. .cmd_size = sizeof(struct iscsi_cmd),
  1962. };
  1963. struct iscsi_transport bnx2i_iscsi_transport = {
  1964. .owner = THIS_MODULE,
  1965. .name = "bnx2i",
  1966. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
  1967. CAP_MULTI_R2T | CAP_DATADGST |
  1968. CAP_DATA_PATH_OFFLOAD |
  1969. CAP_TEXT_NEGO,
  1970. .create_session = bnx2i_session_create,
  1971. .destroy_session = bnx2i_session_destroy,
  1972. .create_conn = bnx2i_conn_create,
  1973. .bind_conn = bnx2i_conn_bind,
  1974. .unbind_conn = iscsi_conn_unbind,
  1975. .destroy_conn = bnx2i_conn_destroy,
  1976. .attr_is_visible = bnx2i_attr_is_visible,
  1977. .set_param = iscsi_set_param,
  1978. .get_conn_param = iscsi_conn_get_param,
  1979. .get_session_param = iscsi_session_get_param,
  1980. .get_host_param = bnx2i_host_get_param,
  1981. .start_conn = bnx2i_conn_start,
  1982. .stop_conn = iscsi_conn_stop,
  1983. .send_pdu = iscsi_conn_send_pdu,
  1984. .xmit_task = bnx2i_task_xmit,
  1985. .get_stats = bnx2i_conn_get_stats,
  1986. /* TCP connect - disconnect - option-2 interface calls */
  1987. .get_ep_param = bnx2i_ep_get_param,
  1988. .ep_connect = bnx2i_ep_connect,
  1989. .ep_poll = bnx2i_ep_poll,
  1990. .ep_disconnect = bnx2i_ep_disconnect,
  1991. .set_path = bnx2i_nl_set_path,
  1992. /* Error recovery timeout call */
  1993. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1994. .cleanup_task = bnx2i_cleanup_task,
  1995. };