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