bnx2i_iscsi.c 55 KB

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