qedi_main.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * QLogic iSCSI Offload Driver
  3. * Copyright (c) 2016 Cavium Inc.
  4. *
  5. * This software is available under the terms of the GNU General Public License
  6. * (GPL) Version 2, available from the file COPYING in the main directory of
  7. * this source tree.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/kernel.h>
  12. #include <linux/if_arp.h>
  13. #include <scsi/iscsi_if.h>
  14. #include <linux/inet.h>
  15. #include <net/arp.h>
  16. #include <linux/list.h>
  17. #include <linux/kthread.h>
  18. #include <linux/mm.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/cpu.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_device.h>
  23. #include <scsi/scsi_eh.h>
  24. #include <scsi/scsi_host.h>
  25. #include <scsi/scsi.h>
  26. #include "qedi.h"
  27. #include "qedi_gbl.h"
  28. #include "qedi_iscsi.h"
  29. static uint qedi_fw_debug;
  30. module_param(qedi_fw_debug, uint, 0644);
  31. MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
  32. uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
  33. module_param(qedi_dbg_log, uint, 0644);
  34. MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
  35. uint qedi_io_tracing;
  36. module_param(qedi_io_tracing, uint, 0644);
  37. MODULE_PARM_DESC(qedi_io_tracing,
  38. " Enable logging of SCSI requests/completions into trace buffer. (default off).");
  39. const struct qed_iscsi_ops *qedi_ops;
  40. static struct scsi_transport_template *qedi_scsi_transport;
  41. static struct pci_driver qedi_pci_driver;
  42. static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
  43. static LIST_HEAD(qedi_udev_list);
  44. /* Static function declaration */
  45. static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
  46. static void qedi_free_global_queues(struct qedi_ctx *qedi);
  47. static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
  48. static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
  49. static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
  50. static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
  51. {
  52. struct qedi_ctx *qedi;
  53. struct qedi_endpoint *qedi_ep;
  54. struct async_data *data;
  55. int rval = 0;
  56. if (!context || !fw_handle) {
  57. QEDI_ERR(NULL, "Recv event with ctx NULL\n");
  58. return -EINVAL;
  59. }
  60. qedi = (struct qedi_ctx *)context;
  61. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  62. "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
  63. data = (struct async_data *)fw_handle;
  64. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  65. "cid=0x%x tid=0x%x err-code=0x%x fw-dbg-param=0x%x\n",
  66. data->cid, data->itid, data->error_code,
  67. data->fw_debug_param);
  68. qedi_ep = qedi->ep_tbl[data->cid];
  69. if (!qedi_ep) {
  70. QEDI_WARN(&qedi->dbg_ctx,
  71. "Cannot process event, ep already disconnected, cid=0x%x\n",
  72. data->cid);
  73. WARN_ON(1);
  74. return -ENODEV;
  75. }
  76. switch (fw_event_code) {
  77. case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
  78. if (qedi_ep->state == EP_STATE_OFLDCONN_START)
  79. qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
  80. wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
  81. break;
  82. case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
  83. qedi_ep->state = EP_STATE_DISCONN_COMPL;
  84. wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
  85. break;
  86. case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
  87. qedi_process_iscsi_error(qedi_ep, data);
  88. break;
  89. case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
  90. case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
  91. case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
  92. case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
  93. case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
  94. case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
  95. case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
  96. qedi_process_tcp_error(qedi_ep, data);
  97. break;
  98. default:
  99. QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
  100. fw_event_code);
  101. }
  102. return rval;
  103. }
  104. static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
  105. {
  106. struct qedi_uio_dev *udev = uinfo->priv;
  107. struct qedi_ctx *qedi = udev->qedi;
  108. if (!capable(CAP_NET_ADMIN))
  109. return -EPERM;
  110. if (udev->uio_dev != -1)
  111. return -EBUSY;
  112. rtnl_lock();
  113. udev->uio_dev = iminor(inode);
  114. qedi_reset_uio_rings(udev);
  115. set_bit(UIO_DEV_OPENED, &qedi->flags);
  116. rtnl_unlock();
  117. return 0;
  118. }
  119. static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
  120. {
  121. struct qedi_uio_dev *udev = uinfo->priv;
  122. struct qedi_ctx *qedi = udev->qedi;
  123. udev->uio_dev = -1;
  124. clear_bit(UIO_DEV_OPENED, &qedi->flags);
  125. qedi_ll2_free_skbs(qedi);
  126. return 0;
  127. }
  128. static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
  129. {
  130. if (udev->ll2_ring) {
  131. free_page((unsigned long)udev->ll2_ring);
  132. udev->ll2_ring = NULL;
  133. }
  134. if (udev->ll2_buf) {
  135. free_pages((unsigned long)udev->ll2_buf, 2);
  136. udev->ll2_buf = NULL;
  137. }
  138. }
  139. static void __qedi_free_uio(struct qedi_uio_dev *udev)
  140. {
  141. uio_unregister_device(&udev->qedi_uinfo);
  142. __qedi_free_uio_rings(udev);
  143. pci_dev_put(udev->pdev);
  144. kfree(udev->uctrl);
  145. kfree(udev);
  146. }
  147. static void qedi_free_uio(struct qedi_uio_dev *udev)
  148. {
  149. if (!udev)
  150. return;
  151. list_del_init(&udev->list);
  152. __qedi_free_uio(udev);
  153. }
  154. static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
  155. {
  156. struct qedi_ctx *qedi = NULL;
  157. struct qedi_uio_ctrl *uctrl = NULL;
  158. qedi = udev->qedi;
  159. uctrl = udev->uctrl;
  160. spin_lock_bh(&qedi->ll2_lock);
  161. uctrl->host_rx_cons = 0;
  162. uctrl->hw_rx_prod = 0;
  163. uctrl->hw_rx_bd_prod = 0;
  164. uctrl->host_rx_bd_cons = 0;
  165. memset(udev->ll2_ring, 0, udev->ll2_ring_size);
  166. memset(udev->ll2_buf, 0, udev->ll2_buf_size);
  167. spin_unlock_bh(&qedi->ll2_lock);
  168. }
  169. static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
  170. {
  171. int rc = 0;
  172. if (udev->ll2_ring || udev->ll2_buf)
  173. return rc;
  174. /* Allocating memory for LL2 ring */
  175. udev->ll2_ring_size = QEDI_PAGE_SIZE;
  176. udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
  177. if (!udev->ll2_ring) {
  178. rc = -ENOMEM;
  179. goto exit_alloc_ring;
  180. }
  181. /* Allocating memory for Tx/Rx pkt buffer */
  182. udev->ll2_buf_size = TX_RX_RING * LL2_SINGLE_BUF_SIZE;
  183. udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
  184. udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
  185. __GFP_ZERO, 2);
  186. if (!udev->ll2_buf) {
  187. rc = -ENOMEM;
  188. goto exit_alloc_buf;
  189. }
  190. return rc;
  191. exit_alloc_buf:
  192. free_page((unsigned long)udev->ll2_ring);
  193. udev->ll2_ring = NULL;
  194. exit_alloc_ring:
  195. return rc;
  196. }
  197. static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
  198. {
  199. struct qedi_uio_dev *udev = NULL;
  200. struct qedi_uio_ctrl *uctrl = NULL;
  201. int rc = 0;
  202. list_for_each_entry(udev, &qedi_udev_list, list) {
  203. if (udev->pdev == qedi->pdev) {
  204. udev->qedi = qedi;
  205. if (__qedi_alloc_uio_rings(udev)) {
  206. udev->qedi = NULL;
  207. return -ENOMEM;
  208. }
  209. qedi->udev = udev;
  210. return 0;
  211. }
  212. }
  213. udev = kzalloc(sizeof(*udev), GFP_KERNEL);
  214. if (!udev) {
  215. rc = -ENOMEM;
  216. goto err_udev;
  217. }
  218. uctrl = kzalloc(sizeof(*uctrl), GFP_KERNEL);
  219. if (!uctrl) {
  220. rc = -ENOMEM;
  221. goto err_uctrl;
  222. }
  223. udev->uio_dev = -1;
  224. udev->qedi = qedi;
  225. udev->pdev = qedi->pdev;
  226. udev->uctrl = uctrl;
  227. rc = __qedi_alloc_uio_rings(udev);
  228. if (rc)
  229. goto err_uio_rings;
  230. list_add(&udev->list, &qedi_udev_list);
  231. pci_dev_get(udev->pdev);
  232. qedi->udev = udev;
  233. udev->tx_pkt = udev->ll2_buf;
  234. udev->rx_pkt = udev->ll2_buf + LL2_SINGLE_BUF_SIZE;
  235. return 0;
  236. err_uio_rings:
  237. kfree(uctrl);
  238. err_uctrl:
  239. kfree(udev);
  240. err_udev:
  241. return -ENOMEM;
  242. }
  243. static int qedi_init_uio(struct qedi_ctx *qedi)
  244. {
  245. struct qedi_uio_dev *udev = qedi->udev;
  246. struct uio_info *uinfo;
  247. int ret = 0;
  248. if (!udev)
  249. return -ENOMEM;
  250. uinfo = &udev->qedi_uinfo;
  251. uinfo->mem[0].addr = (unsigned long)udev->uctrl;
  252. uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
  253. uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
  254. uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
  255. uinfo->mem[1].size = udev->ll2_ring_size;
  256. uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
  257. uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
  258. uinfo->mem[2].size = udev->ll2_buf_size;
  259. uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
  260. uinfo->name = "qedi_uio";
  261. uinfo->version = QEDI_MODULE_VERSION;
  262. uinfo->irq = UIO_IRQ_CUSTOM;
  263. uinfo->open = qedi_uio_open;
  264. uinfo->release = qedi_uio_close;
  265. if (udev->uio_dev == -1) {
  266. if (!uinfo->priv) {
  267. uinfo->priv = udev;
  268. ret = uio_register_device(&udev->pdev->dev, uinfo);
  269. if (ret) {
  270. QEDI_ERR(&qedi->dbg_ctx,
  271. "UIO registration failed\n");
  272. }
  273. }
  274. }
  275. return ret;
  276. }
  277. static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
  278. struct qed_sb_info *sb_info, u16 sb_id)
  279. {
  280. struct status_block *sb_virt;
  281. dma_addr_t sb_phys;
  282. int ret;
  283. sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
  284. sizeof(struct status_block), &sb_phys,
  285. GFP_KERNEL);
  286. if (!sb_virt) {
  287. QEDI_ERR(&qedi->dbg_ctx,
  288. "Status block allocation failed for id = %d.\n",
  289. sb_id);
  290. return -ENOMEM;
  291. }
  292. ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
  293. sb_id, QED_SB_TYPE_STORAGE);
  294. if (ret) {
  295. QEDI_ERR(&qedi->dbg_ctx,
  296. "Status block initialization failed for id = %d.\n",
  297. sb_id);
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static void qedi_free_sb(struct qedi_ctx *qedi)
  303. {
  304. struct qed_sb_info *sb_info;
  305. int id;
  306. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  307. sb_info = &qedi->sb_array[id];
  308. if (sb_info->sb_virt)
  309. dma_free_coherent(&qedi->pdev->dev,
  310. sizeof(*sb_info->sb_virt),
  311. (void *)sb_info->sb_virt,
  312. sb_info->sb_phys);
  313. }
  314. }
  315. static void qedi_free_fp(struct qedi_ctx *qedi)
  316. {
  317. kfree(qedi->fp_array);
  318. kfree(qedi->sb_array);
  319. }
  320. static void qedi_destroy_fp(struct qedi_ctx *qedi)
  321. {
  322. qedi_free_sb(qedi);
  323. qedi_free_fp(qedi);
  324. }
  325. static int qedi_alloc_fp(struct qedi_ctx *qedi)
  326. {
  327. int ret = 0;
  328. qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
  329. sizeof(struct qedi_fastpath), GFP_KERNEL);
  330. if (!qedi->fp_array) {
  331. QEDI_ERR(&qedi->dbg_ctx,
  332. "fastpath fp array allocation failed.\n");
  333. return -ENOMEM;
  334. }
  335. qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
  336. sizeof(struct qed_sb_info), GFP_KERNEL);
  337. if (!qedi->sb_array) {
  338. QEDI_ERR(&qedi->dbg_ctx,
  339. "fastpath sb array allocation failed.\n");
  340. ret = -ENOMEM;
  341. goto free_fp;
  342. }
  343. return ret;
  344. free_fp:
  345. qedi_free_fp(qedi);
  346. return ret;
  347. }
  348. static void qedi_int_fp(struct qedi_ctx *qedi)
  349. {
  350. struct qedi_fastpath *fp;
  351. int id;
  352. memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
  353. sizeof(*qedi->fp_array));
  354. memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
  355. sizeof(*qedi->sb_array));
  356. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  357. fp = &qedi->fp_array[id];
  358. fp->sb_info = &qedi->sb_array[id];
  359. fp->sb_id = id;
  360. fp->qedi = qedi;
  361. snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
  362. "qedi", id);
  363. /* fp_array[i] ---- irq cookie
  364. * So init data which is needed in int ctx
  365. */
  366. }
  367. }
  368. static int qedi_prepare_fp(struct qedi_ctx *qedi)
  369. {
  370. struct qedi_fastpath *fp;
  371. int id, ret = 0;
  372. ret = qedi_alloc_fp(qedi);
  373. if (ret)
  374. goto err;
  375. qedi_int_fp(qedi);
  376. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  377. fp = &qedi->fp_array[id];
  378. ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
  379. if (ret) {
  380. QEDI_ERR(&qedi->dbg_ctx,
  381. "SB allocation and initialization failed.\n");
  382. ret = -EIO;
  383. goto err_init;
  384. }
  385. }
  386. return 0;
  387. err_init:
  388. qedi_free_sb(qedi);
  389. qedi_free_fp(qedi);
  390. err:
  391. return ret;
  392. }
  393. static int qedi_setup_cid_que(struct qedi_ctx *qedi)
  394. {
  395. int i;
  396. qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
  397. sizeof(u32), GFP_KERNEL);
  398. if (!qedi->cid_que.cid_que_base)
  399. return -ENOMEM;
  400. qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
  401. sizeof(struct qedi_conn *),
  402. GFP_KERNEL);
  403. if (!qedi->cid_que.conn_cid_tbl) {
  404. kfree(qedi->cid_que.cid_que_base);
  405. qedi->cid_que.cid_que_base = NULL;
  406. return -ENOMEM;
  407. }
  408. qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
  409. qedi->cid_que.cid_q_prod_idx = 0;
  410. qedi->cid_que.cid_q_cons_idx = 0;
  411. qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
  412. qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
  413. for (i = 0; i < qedi->max_active_conns; i++) {
  414. qedi->cid_que.cid_que[i] = i;
  415. qedi->cid_que.conn_cid_tbl[i] = NULL;
  416. }
  417. return 0;
  418. }
  419. static void qedi_release_cid_que(struct qedi_ctx *qedi)
  420. {
  421. kfree(qedi->cid_que.cid_que_base);
  422. qedi->cid_que.cid_que_base = NULL;
  423. kfree(qedi->cid_que.conn_cid_tbl);
  424. qedi->cid_que.conn_cid_tbl = NULL;
  425. }
  426. static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
  427. u16 start_id, u16 next)
  428. {
  429. id_tbl->start = start_id;
  430. id_tbl->max = size;
  431. id_tbl->next = next;
  432. spin_lock_init(&id_tbl->lock);
  433. id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
  434. if (!id_tbl->table)
  435. return -ENOMEM;
  436. return 0;
  437. }
  438. static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
  439. {
  440. kfree(id_tbl->table);
  441. id_tbl->table = NULL;
  442. }
  443. int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
  444. {
  445. int ret = -1;
  446. id -= id_tbl->start;
  447. if (id >= id_tbl->max)
  448. return ret;
  449. spin_lock(&id_tbl->lock);
  450. if (!test_bit(id, id_tbl->table)) {
  451. set_bit(id, id_tbl->table);
  452. ret = 0;
  453. }
  454. spin_unlock(&id_tbl->lock);
  455. return ret;
  456. }
  457. u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
  458. {
  459. u16 id;
  460. spin_lock(&id_tbl->lock);
  461. id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
  462. if (id >= id_tbl->max) {
  463. id = QEDI_LOCAL_PORT_INVALID;
  464. if (id_tbl->next != 0) {
  465. id = find_first_zero_bit(id_tbl->table, id_tbl->next);
  466. if (id >= id_tbl->next)
  467. id = QEDI_LOCAL_PORT_INVALID;
  468. }
  469. }
  470. if (id < id_tbl->max) {
  471. set_bit(id, id_tbl->table);
  472. id_tbl->next = (id + 1) & (id_tbl->max - 1);
  473. id += id_tbl->start;
  474. }
  475. spin_unlock(&id_tbl->lock);
  476. return id;
  477. }
  478. void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
  479. {
  480. if (id == QEDI_LOCAL_PORT_INVALID)
  481. return;
  482. id -= id_tbl->start;
  483. if (id >= id_tbl->max)
  484. return;
  485. clear_bit(id, id_tbl->table);
  486. }
  487. static void qedi_cm_free_mem(struct qedi_ctx *qedi)
  488. {
  489. kfree(qedi->ep_tbl);
  490. qedi->ep_tbl = NULL;
  491. qedi_free_id_tbl(&qedi->lcl_port_tbl);
  492. }
  493. static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
  494. {
  495. u16 port_id;
  496. qedi->ep_tbl = kzalloc((qedi->max_active_conns *
  497. sizeof(struct qedi_endpoint *)), GFP_KERNEL);
  498. if (!qedi->ep_tbl)
  499. return -ENOMEM;
  500. port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
  501. if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
  502. QEDI_LOCAL_PORT_MIN, port_id)) {
  503. qedi_cm_free_mem(qedi);
  504. return -ENOMEM;
  505. }
  506. return 0;
  507. }
  508. static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
  509. {
  510. struct Scsi_Host *shost;
  511. struct qedi_ctx *qedi = NULL;
  512. shost = iscsi_host_alloc(&qedi_host_template,
  513. sizeof(struct qedi_ctx), 0);
  514. if (!shost) {
  515. QEDI_ERR(NULL, "Could not allocate shost\n");
  516. goto exit_setup_shost;
  517. }
  518. shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA;
  519. shost->max_channel = 0;
  520. shost->max_lun = ~0;
  521. shost->max_cmd_len = 16;
  522. shost->transportt = qedi_scsi_transport;
  523. qedi = iscsi_host_priv(shost);
  524. memset(qedi, 0, sizeof(*qedi));
  525. qedi->shost = shost;
  526. qedi->dbg_ctx.host_no = shost->host_no;
  527. qedi->pdev = pdev;
  528. qedi->dbg_ctx.pdev = pdev;
  529. qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
  530. qedi->max_sqes = QEDI_SQ_SIZE;
  531. if (shost_use_blk_mq(shost))
  532. shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
  533. pci_set_drvdata(pdev, qedi);
  534. exit_setup_shost:
  535. return qedi;
  536. }
  537. static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
  538. {
  539. struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
  540. struct qedi_uio_dev *udev;
  541. struct qedi_uio_ctrl *uctrl;
  542. struct skb_work_list *work;
  543. u32 prod;
  544. if (!qedi) {
  545. QEDI_ERR(NULL, "qedi is NULL\n");
  546. return -1;
  547. }
  548. if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
  549. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
  550. "UIO DEV is not opened\n");
  551. kfree_skb(skb);
  552. return 0;
  553. }
  554. udev = qedi->udev;
  555. uctrl = udev->uctrl;
  556. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  557. if (!work) {
  558. QEDI_WARN(&qedi->dbg_ctx,
  559. "Could not allocate work so dropping frame.\n");
  560. kfree_skb(skb);
  561. return 0;
  562. }
  563. INIT_LIST_HEAD(&work->list);
  564. work->skb = skb;
  565. if (skb_vlan_tag_present(skb))
  566. work->vlan_id = skb_vlan_tag_get(skb);
  567. if (work->vlan_id)
  568. __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
  569. spin_lock_bh(&qedi->ll2_lock);
  570. list_add_tail(&work->list, &qedi->ll2_skb_list);
  571. ++uctrl->hw_rx_prod_cnt;
  572. prod = (uctrl->hw_rx_prod + 1) % RX_RING;
  573. if (prod != uctrl->host_rx_cons) {
  574. uctrl->hw_rx_prod = prod;
  575. spin_unlock_bh(&qedi->ll2_lock);
  576. wake_up_process(qedi->ll2_recv_thread);
  577. return 0;
  578. }
  579. spin_unlock_bh(&qedi->ll2_lock);
  580. return 0;
  581. }
  582. /* map this skb to iscsiuio mmaped region */
  583. static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
  584. u16 vlan_id)
  585. {
  586. struct qedi_uio_dev *udev = NULL;
  587. struct qedi_uio_ctrl *uctrl = NULL;
  588. struct qedi_rx_bd rxbd;
  589. struct qedi_rx_bd *p_rxbd;
  590. u32 rx_bd_prod;
  591. void *pkt;
  592. int len = 0;
  593. if (!qedi) {
  594. QEDI_ERR(NULL, "qedi is NULL\n");
  595. return -1;
  596. }
  597. udev = qedi->udev;
  598. uctrl = udev->uctrl;
  599. pkt = udev->rx_pkt + (uctrl->hw_rx_prod * LL2_SINGLE_BUF_SIZE);
  600. len = min_t(u32, skb->len, (u32)LL2_SINGLE_BUF_SIZE);
  601. memcpy(pkt, skb->data, len);
  602. memset(&rxbd, 0, sizeof(rxbd));
  603. rxbd.rx_pkt_index = uctrl->hw_rx_prod;
  604. rxbd.rx_pkt_len = len;
  605. rxbd.vlan_id = vlan_id;
  606. uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
  607. rx_bd_prod = uctrl->hw_rx_bd_prod;
  608. p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
  609. p_rxbd += rx_bd_prod;
  610. memcpy(p_rxbd, &rxbd, sizeof(rxbd));
  611. /* notify the iscsiuio about new packet */
  612. uio_event_notify(&udev->qedi_uinfo);
  613. return 0;
  614. }
  615. static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
  616. {
  617. struct skb_work_list *work, *work_tmp;
  618. spin_lock_bh(&qedi->ll2_lock);
  619. list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
  620. list_del(&work->list);
  621. if (work->skb)
  622. kfree_skb(work->skb);
  623. kfree(work);
  624. }
  625. spin_unlock_bh(&qedi->ll2_lock);
  626. }
  627. static int qedi_ll2_recv_thread(void *arg)
  628. {
  629. struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
  630. struct skb_work_list *work, *work_tmp;
  631. set_user_nice(current, -20);
  632. while (!kthread_should_stop()) {
  633. spin_lock_bh(&qedi->ll2_lock);
  634. list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
  635. list) {
  636. list_del(&work->list);
  637. qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
  638. kfree_skb(work->skb);
  639. kfree(work);
  640. }
  641. set_current_state(TASK_INTERRUPTIBLE);
  642. spin_unlock_bh(&qedi->ll2_lock);
  643. schedule();
  644. }
  645. __set_current_state(TASK_RUNNING);
  646. return 0;
  647. }
  648. static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
  649. {
  650. u8 num_sq_pages;
  651. u32 log_page_size;
  652. int rval = 0;
  653. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "Min number of MSIX %d\n",
  654. MIN_NUM_CPUS_MSIX(qedi));
  655. num_sq_pages = (MAX_OUSTANDING_TASKS_PER_CON * 8) / PAGE_SIZE;
  656. qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
  657. memset(&qedi->pf_params.iscsi_pf_params, 0,
  658. sizeof(qedi->pf_params.iscsi_pf_params));
  659. qedi->p_cpuq = pci_alloc_consistent(qedi->pdev,
  660. qedi->num_queues * sizeof(struct qedi_glbl_q_params),
  661. &qedi->hw_p_cpuq);
  662. if (!qedi->p_cpuq) {
  663. QEDI_ERR(&qedi->dbg_ctx, "pci_alloc_consistent fail\n");
  664. rval = -1;
  665. goto err_alloc_mem;
  666. }
  667. rval = qedi_alloc_global_queues(qedi);
  668. if (rval) {
  669. QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
  670. rval = -1;
  671. goto err_alloc_mem;
  672. }
  673. qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
  674. qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
  675. qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
  676. qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
  677. qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
  678. qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
  679. qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
  680. qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
  681. for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
  682. if ((1 << log_page_size) == PAGE_SIZE)
  683. break;
  684. }
  685. qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
  686. qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
  687. (u64)qedi->hw_p_cpuq;
  688. /* RQ BDQ initializations.
  689. * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
  690. * rqe_log_size: 8 for 256B RQE
  691. */
  692. qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
  693. /* BDQ address and size */
  694. qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
  695. qedi->bdq_pbl_list_dma;
  696. qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
  697. qedi->bdq_pbl_list_num_entries;
  698. qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
  699. /* cq_num_entries: num_tasks + rq_num_entries */
  700. qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
  701. qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
  702. qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
  703. qedi->pf_params.iscsi_pf_params.ooo_enable = 1;
  704. err_alloc_mem:
  705. return rval;
  706. }
  707. /* Free DMA coherent memory for array of queue pointers we pass to qed */
  708. static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
  709. {
  710. size_t size = 0;
  711. if (qedi->p_cpuq) {
  712. size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
  713. pci_free_consistent(qedi->pdev, size, qedi->p_cpuq,
  714. qedi->hw_p_cpuq);
  715. }
  716. qedi_free_global_queues(qedi);
  717. kfree(qedi->global_queues);
  718. }
  719. static void qedi_link_update(void *dev, struct qed_link_output *link)
  720. {
  721. struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
  722. if (link->link_up) {
  723. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
  724. atomic_set(&qedi->link_state, QEDI_LINK_UP);
  725. } else {
  726. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  727. "Link Down event.\n");
  728. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  729. }
  730. }
  731. static struct qed_iscsi_cb_ops qedi_cb_ops = {
  732. {
  733. .link_update = qedi_link_update,
  734. }
  735. };
  736. static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
  737. u16 que_idx, struct qedi_percpu_s *p)
  738. {
  739. struct qedi_work *qedi_work;
  740. struct qedi_conn *q_conn;
  741. struct iscsi_conn *conn;
  742. struct qedi_cmd *qedi_cmd;
  743. u32 iscsi_cid;
  744. int rc = 0;
  745. iscsi_cid = cqe->cqe_common.conn_id;
  746. q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
  747. if (!q_conn) {
  748. QEDI_WARN(&qedi->dbg_ctx,
  749. "Session no longer exists for cid=0x%x!!\n",
  750. iscsi_cid);
  751. return -1;
  752. }
  753. conn = q_conn->cls_conn->dd_data;
  754. switch (cqe->cqe_common.cqe_type) {
  755. case ISCSI_CQE_TYPE_SOLICITED:
  756. case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
  757. qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
  758. if (!qedi_cmd) {
  759. rc = -1;
  760. break;
  761. }
  762. INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
  763. qedi_cmd->cqe_work.qedi = qedi;
  764. memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
  765. qedi_cmd->cqe_work.que_idx = que_idx;
  766. qedi_cmd->cqe_work.is_solicited = true;
  767. list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
  768. break;
  769. case ISCSI_CQE_TYPE_UNSOLICITED:
  770. case ISCSI_CQE_TYPE_DUMMY:
  771. case ISCSI_CQE_TYPE_TASK_CLEANUP:
  772. qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
  773. if (!qedi_work) {
  774. rc = -1;
  775. break;
  776. }
  777. INIT_LIST_HEAD(&qedi_work->list);
  778. qedi_work->qedi = qedi;
  779. memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
  780. qedi_work->que_idx = que_idx;
  781. qedi_work->is_solicited = false;
  782. list_add_tail(&qedi_work->list, &p->work_list);
  783. break;
  784. default:
  785. rc = -1;
  786. QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
  787. }
  788. return rc;
  789. }
  790. static bool qedi_process_completions(struct qedi_fastpath *fp)
  791. {
  792. struct qedi_ctx *qedi = fp->qedi;
  793. struct qed_sb_info *sb_info = fp->sb_info;
  794. struct status_block *sb = sb_info->sb_virt;
  795. struct qedi_percpu_s *p = NULL;
  796. struct global_queue *que;
  797. u16 prod_idx;
  798. unsigned long flags;
  799. union iscsi_cqe *cqe;
  800. int cpu;
  801. int ret;
  802. /* Get the current firmware producer index */
  803. prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
  804. if (prod_idx >= QEDI_CQ_SIZE)
  805. prod_idx = prod_idx % QEDI_CQ_SIZE;
  806. que = qedi->global_queues[fp->sb_id];
  807. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
  808. "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
  809. que, prod_idx, que->cq_cons_idx, fp->sb_id);
  810. qedi->intr_cpu = fp->sb_id;
  811. cpu = smp_processor_id();
  812. p = &per_cpu(qedi_percpu, cpu);
  813. if (unlikely(!p->iothread))
  814. WARN_ON(1);
  815. spin_lock_irqsave(&p->p_work_lock, flags);
  816. while (que->cq_cons_idx != prod_idx) {
  817. cqe = &que->cq[que->cq_cons_idx];
  818. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
  819. "cqe=%p prod_idx=%d cons_idx=%d.\n",
  820. cqe, prod_idx, que->cq_cons_idx);
  821. ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
  822. if (ret)
  823. continue;
  824. que->cq_cons_idx++;
  825. if (que->cq_cons_idx == QEDI_CQ_SIZE)
  826. que->cq_cons_idx = 0;
  827. }
  828. wake_up_process(p->iothread);
  829. spin_unlock_irqrestore(&p->p_work_lock, flags);
  830. return true;
  831. }
  832. static bool qedi_fp_has_work(struct qedi_fastpath *fp)
  833. {
  834. struct qedi_ctx *qedi = fp->qedi;
  835. struct global_queue *que;
  836. struct qed_sb_info *sb_info = fp->sb_info;
  837. struct status_block *sb = sb_info->sb_virt;
  838. u16 prod_idx;
  839. barrier();
  840. /* Get the current firmware producer index */
  841. prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
  842. /* Get the pointer to the global CQ this completion is on */
  843. que = qedi->global_queues[fp->sb_id];
  844. /* prod idx wrap around uint16 */
  845. if (prod_idx >= QEDI_CQ_SIZE)
  846. prod_idx = prod_idx % QEDI_CQ_SIZE;
  847. return (que->cq_cons_idx != prod_idx);
  848. }
  849. /* MSI-X fastpath handler code */
  850. static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
  851. {
  852. struct qedi_fastpath *fp = dev_id;
  853. struct qedi_ctx *qedi = fp->qedi;
  854. bool wake_io_thread = true;
  855. qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
  856. process_again:
  857. wake_io_thread = qedi_process_completions(fp);
  858. if (wake_io_thread) {
  859. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  860. "process already running\n");
  861. }
  862. if (qedi_fp_has_work(fp) == 0)
  863. qed_sb_update_sb_idx(fp->sb_info);
  864. /* Check for more work */
  865. rmb();
  866. if (qedi_fp_has_work(fp) == 0)
  867. qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
  868. else
  869. goto process_again;
  870. return IRQ_HANDLED;
  871. }
  872. /* simd handler for MSI/INTa */
  873. static void qedi_simd_int_handler(void *cookie)
  874. {
  875. /* Cookie is qedi_ctx struct */
  876. struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
  877. QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
  878. }
  879. #define QEDI_SIMD_HANDLER_NUM 0
  880. static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
  881. {
  882. int i;
  883. if (qedi->int_info.msix_cnt) {
  884. for (i = 0; i < qedi->int_info.used_cnt; i++) {
  885. synchronize_irq(qedi->int_info.msix[i].vector);
  886. irq_set_affinity_hint(qedi->int_info.msix[i].vector,
  887. NULL);
  888. free_irq(qedi->int_info.msix[i].vector,
  889. &qedi->fp_array[i]);
  890. }
  891. } else {
  892. qedi_ops->common->simd_handler_clean(qedi->cdev,
  893. QEDI_SIMD_HANDLER_NUM);
  894. }
  895. qedi->int_info.used_cnt = 0;
  896. qedi_ops->common->set_fp_int(qedi->cdev, 0);
  897. }
  898. static int qedi_request_msix_irq(struct qedi_ctx *qedi)
  899. {
  900. int i, rc, cpu;
  901. cpu = cpumask_first(cpu_online_mask);
  902. for (i = 0; i < MIN_NUM_CPUS_MSIX(qedi); i++) {
  903. rc = request_irq(qedi->int_info.msix[i].vector,
  904. qedi_msix_handler, 0, "qedi",
  905. &qedi->fp_array[i]);
  906. if (rc) {
  907. QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
  908. qedi_sync_free_irqs(qedi);
  909. return rc;
  910. }
  911. qedi->int_info.used_cnt++;
  912. rc = irq_set_affinity_hint(qedi->int_info.msix[i].vector,
  913. get_cpu_mask(cpu));
  914. cpu = cpumask_next(cpu, cpu_online_mask);
  915. }
  916. return 0;
  917. }
  918. static int qedi_setup_int(struct qedi_ctx *qedi)
  919. {
  920. int rc = 0;
  921. rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus());
  922. rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
  923. if (rc)
  924. goto exit_setup_int;
  925. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  926. "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
  927. qedi->int_info.msix_cnt, num_online_cpus());
  928. if (qedi->int_info.msix_cnt) {
  929. rc = qedi_request_msix_irq(qedi);
  930. goto exit_setup_int;
  931. } else {
  932. qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
  933. QEDI_SIMD_HANDLER_NUM,
  934. qedi_simd_int_handler);
  935. qedi->int_info.used_cnt = 1;
  936. }
  937. exit_setup_int:
  938. return rc;
  939. }
  940. static void qedi_free_bdq(struct qedi_ctx *qedi)
  941. {
  942. int i;
  943. if (qedi->bdq_pbl_list)
  944. dma_free_coherent(&qedi->pdev->dev, PAGE_SIZE,
  945. qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
  946. if (qedi->bdq_pbl)
  947. dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
  948. qedi->bdq_pbl, qedi->bdq_pbl_dma);
  949. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  950. if (qedi->bdq[i].buf_addr) {
  951. dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
  952. qedi->bdq[i].buf_addr,
  953. qedi->bdq[i].buf_dma);
  954. }
  955. }
  956. }
  957. static void qedi_free_global_queues(struct qedi_ctx *qedi)
  958. {
  959. int i;
  960. struct global_queue **gl = qedi->global_queues;
  961. for (i = 0; i < qedi->num_queues; i++) {
  962. if (!gl[i])
  963. continue;
  964. if (gl[i]->cq)
  965. dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
  966. gl[i]->cq, gl[i]->cq_dma);
  967. if (gl[i]->cq_pbl)
  968. dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
  969. gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
  970. kfree(gl[i]);
  971. }
  972. qedi_free_bdq(qedi);
  973. }
  974. static int qedi_alloc_bdq(struct qedi_ctx *qedi)
  975. {
  976. int i;
  977. struct scsi_bd *pbl;
  978. u64 *list;
  979. dma_addr_t page;
  980. /* Alloc dma memory for BDQ buffers */
  981. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  982. qedi->bdq[i].buf_addr =
  983. dma_alloc_coherent(&qedi->pdev->dev,
  984. QEDI_BDQ_BUF_SIZE,
  985. &qedi->bdq[i].buf_dma,
  986. GFP_KERNEL);
  987. if (!qedi->bdq[i].buf_addr) {
  988. QEDI_ERR(&qedi->dbg_ctx,
  989. "Could not allocate BDQ buffer %d.\n", i);
  990. return -ENOMEM;
  991. }
  992. }
  993. /* Alloc dma memory for BDQ page buffer list */
  994. qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
  995. qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, PAGE_SIZE);
  996. qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
  997. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
  998. qedi->rq_num_entries);
  999. qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
  1000. qedi->bdq_pbl_mem_size,
  1001. &qedi->bdq_pbl_dma, GFP_KERNEL);
  1002. if (!qedi->bdq_pbl) {
  1003. QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
  1004. return -ENOMEM;
  1005. }
  1006. /*
  1007. * Populate BDQ PBL with physical and virtual address of individual
  1008. * BDQ buffers
  1009. */
  1010. pbl = (struct scsi_bd *)qedi->bdq_pbl;
  1011. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  1012. pbl->address.hi =
  1013. cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
  1014. pbl->address.lo =
  1015. cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
  1016. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1017. "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
  1018. pbl, pbl->address.hi, pbl->address.lo, i);
  1019. pbl->opaque.hi = 0;
  1020. pbl->opaque.lo = cpu_to_le32(QEDI_U64_LO(i));
  1021. pbl++;
  1022. }
  1023. /* Allocate list of PBL pages */
  1024. qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
  1025. PAGE_SIZE,
  1026. &qedi->bdq_pbl_list_dma,
  1027. GFP_KERNEL);
  1028. if (!qedi->bdq_pbl_list) {
  1029. QEDI_ERR(&qedi->dbg_ctx,
  1030. "Could not allocate list of PBL pages.\n");
  1031. return -ENOMEM;
  1032. }
  1033. memset(qedi->bdq_pbl_list, 0, PAGE_SIZE);
  1034. /*
  1035. * Now populate PBL list with pages that contain pointers to the
  1036. * individual buffers.
  1037. */
  1038. qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size / PAGE_SIZE;
  1039. list = (u64 *)qedi->bdq_pbl_list;
  1040. page = qedi->bdq_pbl_list_dma;
  1041. for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
  1042. *list = qedi->bdq_pbl_dma;
  1043. list++;
  1044. page += PAGE_SIZE;
  1045. }
  1046. return 0;
  1047. }
  1048. static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
  1049. {
  1050. u32 *list;
  1051. int i;
  1052. int status = 0, rc;
  1053. u32 *pbl;
  1054. dma_addr_t page;
  1055. int num_pages;
  1056. /*
  1057. * Number of global queues (CQ / RQ). This should
  1058. * be <= number of available MSIX vectors for the PF
  1059. */
  1060. if (!qedi->num_queues) {
  1061. QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
  1062. return 1;
  1063. }
  1064. /* Make sure we allocated the PBL that will contain the physical
  1065. * addresses of our queues
  1066. */
  1067. if (!qedi->p_cpuq) {
  1068. status = 1;
  1069. goto mem_alloc_failure;
  1070. }
  1071. qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
  1072. qedi->num_queues), GFP_KERNEL);
  1073. if (!qedi->global_queues) {
  1074. QEDI_ERR(&qedi->dbg_ctx,
  1075. "Unable to allocate global queues array ptr memory\n");
  1076. return -ENOMEM;
  1077. }
  1078. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1079. "qedi->global_queues=%p.\n", qedi->global_queues);
  1080. /* Allocate DMA coherent buffers for BDQ */
  1081. rc = qedi_alloc_bdq(qedi);
  1082. if (rc)
  1083. goto mem_alloc_failure;
  1084. /* Allocate a CQ and an associated PBL for each MSI-X
  1085. * vector.
  1086. */
  1087. for (i = 0; i < qedi->num_queues; i++) {
  1088. qedi->global_queues[i] =
  1089. kzalloc(sizeof(*qedi->global_queues[0]),
  1090. GFP_KERNEL);
  1091. if (!qedi->global_queues[i]) {
  1092. QEDI_ERR(&qedi->dbg_ctx,
  1093. "Unable to allocation global queue %d.\n", i);
  1094. goto mem_alloc_failure;
  1095. }
  1096. qedi->global_queues[i]->cq_mem_size =
  1097. (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
  1098. qedi->global_queues[i]->cq_mem_size =
  1099. (qedi->global_queues[i]->cq_mem_size +
  1100. (QEDI_PAGE_SIZE - 1));
  1101. qedi->global_queues[i]->cq_pbl_size =
  1102. (qedi->global_queues[i]->cq_mem_size /
  1103. QEDI_PAGE_SIZE) * sizeof(void *);
  1104. qedi->global_queues[i]->cq_pbl_size =
  1105. (qedi->global_queues[i]->cq_pbl_size +
  1106. (QEDI_PAGE_SIZE - 1));
  1107. qedi->global_queues[i]->cq =
  1108. dma_alloc_coherent(&qedi->pdev->dev,
  1109. qedi->global_queues[i]->cq_mem_size,
  1110. &qedi->global_queues[i]->cq_dma,
  1111. GFP_KERNEL);
  1112. if (!qedi->global_queues[i]->cq) {
  1113. QEDI_WARN(&qedi->dbg_ctx,
  1114. "Could not allocate cq.\n");
  1115. status = -ENOMEM;
  1116. goto mem_alloc_failure;
  1117. }
  1118. memset(qedi->global_queues[i]->cq, 0,
  1119. qedi->global_queues[i]->cq_mem_size);
  1120. qedi->global_queues[i]->cq_pbl =
  1121. dma_alloc_coherent(&qedi->pdev->dev,
  1122. qedi->global_queues[i]->cq_pbl_size,
  1123. &qedi->global_queues[i]->cq_pbl_dma,
  1124. GFP_KERNEL);
  1125. if (!qedi->global_queues[i]->cq_pbl) {
  1126. QEDI_WARN(&qedi->dbg_ctx,
  1127. "Could not allocate cq PBL.\n");
  1128. status = -ENOMEM;
  1129. goto mem_alloc_failure;
  1130. }
  1131. memset(qedi->global_queues[i]->cq_pbl, 0,
  1132. qedi->global_queues[i]->cq_pbl_size);
  1133. /* Create PBL */
  1134. num_pages = qedi->global_queues[i]->cq_mem_size /
  1135. QEDI_PAGE_SIZE;
  1136. page = qedi->global_queues[i]->cq_dma;
  1137. pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
  1138. while (num_pages--) {
  1139. *pbl = (u32)page;
  1140. pbl++;
  1141. *pbl = (u32)((u64)page >> 32);
  1142. pbl++;
  1143. page += QEDI_PAGE_SIZE;
  1144. }
  1145. }
  1146. list = (u32 *)qedi->p_cpuq;
  1147. /*
  1148. * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
  1149. * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
  1150. * to the physical address which contains an array of pointers to the
  1151. * physical addresses of the specific queue pages.
  1152. */
  1153. for (i = 0; i < qedi->num_queues; i++) {
  1154. *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
  1155. list++;
  1156. *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
  1157. list++;
  1158. *list = (u32)0;
  1159. list++;
  1160. *list = (u32)((u64)0 >> 32);
  1161. list++;
  1162. }
  1163. return 0;
  1164. mem_alloc_failure:
  1165. qedi_free_global_queues(qedi);
  1166. return status;
  1167. }
  1168. int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
  1169. {
  1170. int rval = 0;
  1171. u32 *pbl;
  1172. dma_addr_t page;
  1173. int num_pages;
  1174. if (!ep)
  1175. return -EIO;
  1176. /* Calculate appropriate queue and PBL sizes */
  1177. ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
  1178. ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
  1179. ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
  1180. ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
  1181. ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
  1182. &ep->sq_dma, GFP_KERNEL);
  1183. if (!ep->sq) {
  1184. QEDI_WARN(&qedi->dbg_ctx,
  1185. "Could not allocate send queue.\n");
  1186. rval = -ENOMEM;
  1187. goto out;
  1188. }
  1189. memset(ep->sq, 0, ep->sq_mem_size);
  1190. ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
  1191. &ep->sq_pbl_dma, GFP_KERNEL);
  1192. if (!ep->sq_pbl) {
  1193. QEDI_WARN(&qedi->dbg_ctx,
  1194. "Could not allocate send queue PBL.\n");
  1195. rval = -ENOMEM;
  1196. goto out_free_sq;
  1197. }
  1198. memset(ep->sq_pbl, 0, ep->sq_pbl_size);
  1199. /* Create PBL */
  1200. num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
  1201. page = ep->sq_dma;
  1202. pbl = (u32 *)ep->sq_pbl;
  1203. while (num_pages--) {
  1204. *pbl = (u32)page;
  1205. pbl++;
  1206. *pbl = (u32)((u64)page >> 32);
  1207. pbl++;
  1208. page += QEDI_PAGE_SIZE;
  1209. }
  1210. return rval;
  1211. out_free_sq:
  1212. dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
  1213. ep->sq_dma);
  1214. out:
  1215. return rval;
  1216. }
  1217. void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
  1218. {
  1219. if (ep->sq_pbl)
  1220. dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
  1221. ep->sq_pbl_dma);
  1222. if (ep->sq)
  1223. dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
  1224. ep->sq_dma);
  1225. }
  1226. int qedi_get_task_idx(struct qedi_ctx *qedi)
  1227. {
  1228. s16 tmp_idx;
  1229. again:
  1230. tmp_idx = find_first_zero_bit(qedi->task_idx_map,
  1231. MAX_ISCSI_TASK_ENTRIES);
  1232. if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
  1233. QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
  1234. tmp_idx = -1;
  1235. goto err_idx;
  1236. }
  1237. if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
  1238. goto again;
  1239. err_idx:
  1240. return tmp_idx;
  1241. }
  1242. void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
  1243. {
  1244. if (!test_and_clear_bit(idx, qedi->task_idx_map)) {
  1245. QEDI_ERR(&qedi->dbg_ctx,
  1246. "FW task context, already cleared, tid=0x%x\n", idx);
  1247. WARN_ON(1);
  1248. }
  1249. }
  1250. void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
  1251. struct qedi_cmd *cmd)
  1252. {
  1253. qedi->itt_map[tid].itt = proto_itt;
  1254. qedi->itt_map[tid].p_cmd = cmd;
  1255. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1256. "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
  1257. qedi->itt_map[tid].itt);
  1258. }
  1259. void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
  1260. {
  1261. u16 i;
  1262. for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
  1263. if (qedi->itt_map[i].itt == itt) {
  1264. *tid = i;
  1265. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1266. "Ref itt=0x%x, found at tid=0x%x\n",
  1267. itt, *tid);
  1268. return;
  1269. }
  1270. }
  1271. WARN_ON(1);
  1272. }
  1273. void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
  1274. {
  1275. *proto_itt = qedi->itt_map[tid].itt;
  1276. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1277. "Get itt map tid [0x%x with proto itt[0x%x]",
  1278. tid, *proto_itt);
  1279. }
  1280. struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
  1281. {
  1282. struct qedi_cmd *cmd = NULL;
  1283. if (tid > MAX_ISCSI_TASK_ENTRIES)
  1284. return NULL;
  1285. cmd = qedi->itt_map[tid].p_cmd;
  1286. if (cmd->task_id != tid)
  1287. return NULL;
  1288. qedi->itt_map[tid].p_cmd = NULL;
  1289. return cmd;
  1290. }
  1291. static int qedi_alloc_itt(struct qedi_ctx *qedi)
  1292. {
  1293. qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
  1294. sizeof(struct qedi_itt_map), GFP_KERNEL);
  1295. if (!qedi->itt_map) {
  1296. QEDI_ERR(&qedi->dbg_ctx,
  1297. "Unable to allocate itt map array memory\n");
  1298. return -ENOMEM;
  1299. }
  1300. return 0;
  1301. }
  1302. static void qedi_free_itt(struct qedi_ctx *qedi)
  1303. {
  1304. kfree(qedi->itt_map);
  1305. }
  1306. static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
  1307. .rx_cb = qedi_ll2_rx,
  1308. .tx_cb = NULL,
  1309. };
  1310. static int qedi_percpu_io_thread(void *arg)
  1311. {
  1312. struct qedi_percpu_s *p = arg;
  1313. struct qedi_work *work, *tmp;
  1314. unsigned long flags;
  1315. LIST_HEAD(work_list);
  1316. set_user_nice(current, -20);
  1317. while (!kthread_should_stop()) {
  1318. spin_lock_irqsave(&p->p_work_lock, flags);
  1319. while (!list_empty(&p->work_list)) {
  1320. list_splice_init(&p->work_list, &work_list);
  1321. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1322. list_for_each_entry_safe(work, tmp, &work_list, list) {
  1323. list_del_init(&work->list);
  1324. qedi_fp_process_cqes(work);
  1325. if (!work->is_solicited)
  1326. kfree(work);
  1327. }
  1328. cond_resched();
  1329. spin_lock_irqsave(&p->p_work_lock, flags);
  1330. }
  1331. set_current_state(TASK_INTERRUPTIBLE);
  1332. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1333. schedule();
  1334. }
  1335. __set_current_state(TASK_RUNNING);
  1336. return 0;
  1337. }
  1338. static int qedi_cpu_online(unsigned int cpu)
  1339. {
  1340. struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
  1341. struct task_struct *thread;
  1342. thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
  1343. cpu_to_node(cpu),
  1344. "qedi_thread/%d", cpu);
  1345. if (IS_ERR(thread))
  1346. return PTR_ERR(thread);
  1347. kthread_bind(thread, cpu);
  1348. p->iothread = thread;
  1349. wake_up_process(thread);
  1350. return 0;
  1351. }
  1352. static int qedi_cpu_offline(unsigned int cpu)
  1353. {
  1354. struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
  1355. struct qedi_work *work, *tmp;
  1356. struct task_struct *thread;
  1357. spin_lock_bh(&p->p_work_lock);
  1358. thread = p->iothread;
  1359. p->iothread = NULL;
  1360. list_for_each_entry_safe(work, tmp, &p->work_list, list) {
  1361. list_del_init(&work->list);
  1362. qedi_fp_process_cqes(work);
  1363. if (!work->is_solicited)
  1364. kfree(work);
  1365. }
  1366. spin_unlock_bh(&p->p_work_lock);
  1367. if (thread)
  1368. kthread_stop(thread);
  1369. return 0;
  1370. }
  1371. void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
  1372. {
  1373. struct qed_ll2_params params;
  1374. qedi_recover_all_conns(qedi);
  1375. qedi_ops->ll2->stop(qedi->cdev);
  1376. qedi_ll2_free_skbs(qedi);
  1377. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
  1378. qedi->ll2_mtu, mtu);
  1379. memset(&params, 0, sizeof(params));
  1380. qedi->ll2_mtu = mtu;
  1381. params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
  1382. params.drop_ttl0_packets = 0;
  1383. params.rx_vlan_stripping = 1;
  1384. ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
  1385. qedi_ops->ll2->start(qedi->cdev, &params);
  1386. }
  1387. static void __qedi_remove(struct pci_dev *pdev, int mode)
  1388. {
  1389. struct qedi_ctx *qedi = pci_get_drvdata(pdev);
  1390. if (qedi->tmf_thread) {
  1391. flush_workqueue(qedi->tmf_thread);
  1392. destroy_workqueue(qedi->tmf_thread);
  1393. qedi->tmf_thread = NULL;
  1394. }
  1395. if (qedi->offload_thread) {
  1396. flush_workqueue(qedi->offload_thread);
  1397. destroy_workqueue(qedi->offload_thread);
  1398. qedi->offload_thread = NULL;
  1399. }
  1400. #ifdef CONFIG_DEBUG_FS
  1401. qedi_dbg_host_exit(&qedi->dbg_ctx);
  1402. #endif
  1403. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
  1404. qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
  1405. qedi_sync_free_irqs(qedi);
  1406. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
  1407. qedi_ops->stop(qedi->cdev);
  1408. qedi_ops->ll2->stop(qedi->cdev);
  1409. }
  1410. if (mode == QEDI_MODE_NORMAL)
  1411. qedi_free_iscsi_pf_param(qedi);
  1412. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
  1413. qedi_ops->common->slowpath_stop(qedi->cdev);
  1414. qedi_ops->common->remove(qedi->cdev);
  1415. }
  1416. qedi_destroy_fp(qedi);
  1417. if (mode == QEDI_MODE_NORMAL) {
  1418. qedi_release_cid_que(qedi);
  1419. qedi_cm_free_mem(qedi);
  1420. qedi_free_uio(qedi->udev);
  1421. qedi_free_itt(qedi);
  1422. iscsi_host_remove(qedi->shost);
  1423. iscsi_host_free(qedi->shost);
  1424. if (qedi->ll2_recv_thread) {
  1425. kthread_stop(qedi->ll2_recv_thread);
  1426. qedi->ll2_recv_thread = NULL;
  1427. }
  1428. qedi_ll2_free_skbs(qedi);
  1429. }
  1430. }
  1431. static int __qedi_probe(struct pci_dev *pdev, int mode)
  1432. {
  1433. struct qedi_ctx *qedi;
  1434. struct qed_ll2_params params;
  1435. u32 dp_module = 0;
  1436. u8 dp_level = 0;
  1437. bool is_vf = false;
  1438. char host_buf[16];
  1439. struct qed_link_params link_params;
  1440. struct qed_slowpath_params sp_params;
  1441. struct qed_probe_params qed_params;
  1442. void *task_start, *task_end;
  1443. int rc;
  1444. u16 tmp;
  1445. if (mode != QEDI_MODE_RECOVERY) {
  1446. qedi = qedi_host_alloc(pdev);
  1447. if (!qedi) {
  1448. rc = -ENOMEM;
  1449. goto exit_probe;
  1450. }
  1451. } else {
  1452. qedi = pci_get_drvdata(pdev);
  1453. }
  1454. memset(&qed_params, 0, sizeof(qed_params));
  1455. qed_params.protocol = QED_PROTOCOL_ISCSI;
  1456. qed_params.dp_module = dp_module;
  1457. qed_params.dp_level = dp_level;
  1458. qed_params.is_vf = is_vf;
  1459. qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
  1460. if (!qedi->cdev) {
  1461. rc = -ENODEV;
  1462. QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
  1463. goto free_host;
  1464. }
  1465. qedi->msix_count = MAX_NUM_MSIX_PF;
  1466. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  1467. if (mode != QEDI_MODE_RECOVERY) {
  1468. rc = qedi_set_iscsi_pf_param(qedi);
  1469. if (rc) {
  1470. rc = -ENOMEM;
  1471. QEDI_ERR(&qedi->dbg_ctx,
  1472. "Set iSCSI pf param fail\n");
  1473. goto free_host;
  1474. }
  1475. }
  1476. qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
  1477. rc = qedi_prepare_fp(qedi);
  1478. if (rc) {
  1479. QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
  1480. goto free_pf_params;
  1481. }
  1482. /* Start the Slowpath-process */
  1483. memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
  1484. sp_params.int_mode = QED_INT_MODE_MSIX;
  1485. sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
  1486. sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
  1487. sp_params.drv_rev = QEDI_DRIVER_REV_VER;
  1488. sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
  1489. strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
  1490. rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
  1491. if (rc) {
  1492. QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
  1493. goto stop_hw;
  1494. }
  1495. /* update_pf_params needs to be called before and after slowpath
  1496. * start
  1497. */
  1498. qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
  1499. qedi_setup_int(qedi);
  1500. if (rc)
  1501. goto stop_iscsi_func;
  1502. qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
  1503. /* Learn information crucial for qedi to progress */
  1504. rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
  1505. if (rc)
  1506. goto stop_iscsi_func;
  1507. /* Record BDQ producer doorbell addresses */
  1508. qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
  1509. qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
  1510. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1511. "BDQ primary_prod=%p secondary_prod=%p.\n",
  1512. qedi->bdq_primary_prod,
  1513. qedi->bdq_secondary_prod);
  1514. /*
  1515. * We need to write the number of BDs in the BDQ we've preallocated so
  1516. * the f/w will do a prefetch and we'll get an unsolicited CQE when a
  1517. * packet arrives.
  1518. */
  1519. qedi->bdq_prod_idx = QEDI_BDQ_NUM;
  1520. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1521. "Writing %d to primary and secondary BDQ doorbell registers.\n",
  1522. qedi->bdq_prod_idx);
  1523. writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
  1524. tmp = readw(qedi->bdq_primary_prod);
  1525. writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
  1526. tmp = readw(qedi->bdq_secondary_prod);
  1527. ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
  1528. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
  1529. qedi->mac);
  1530. sprintf(host_buf, "host_%d", qedi->shost->host_no);
  1531. qedi_ops->common->set_id(qedi->cdev, host_buf, QEDI_MODULE_VERSION);
  1532. qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
  1533. memset(&params, 0, sizeof(params));
  1534. params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
  1535. qedi->ll2_mtu = DEF_PATH_MTU;
  1536. params.drop_ttl0_packets = 0;
  1537. params.rx_vlan_stripping = 1;
  1538. ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
  1539. if (mode != QEDI_MODE_RECOVERY) {
  1540. /* set up rx path */
  1541. INIT_LIST_HEAD(&qedi->ll2_skb_list);
  1542. spin_lock_init(&qedi->ll2_lock);
  1543. /* start qedi context */
  1544. spin_lock_init(&qedi->hba_lock);
  1545. spin_lock_init(&qedi->task_idx_lock);
  1546. }
  1547. qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
  1548. qedi_ops->ll2->start(qedi->cdev, &params);
  1549. if (mode != QEDI_MODE_RECOVERY) {
  1550. qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
  1551. (void *)qedi,
  1552. "qedi_ll2_thread");
  1553. }
  1554. rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
  1555. qedi, qedi_iscsi_event_cb);
  1556. if (rc) {
  1557. rc = -ENODEV;
  1558. QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
  1559. goto stop_slowpath;
  1560. }
  1561. task_start = qedi_get_task_mem(&qedi->tasks, 0);
  1562. task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
  1563. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1564. "Task context start=%p, end=%p block_size=%u.\n",
  1565. task_start, task_end, qedi->tasks.size);
  1566. memset(&link_params, 0, sizeof(link_params));
  1567. link_params.link_up = true;
  1568. rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
  1569. if (rc) {
  1570. QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
  1571. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  1572. }
  1573. #ifdef CONFIG_DEBUG_FS
  1574. qedi_dbg_host_init(&qedi->dbg_ctx, &qedi_debugfs_ops,
  1575. &qedi_dbg_fops);
  1576. #endif
  1577. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  1578. "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
  1579. QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
  1580. FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
  1581. if (mode == QEDI_MODE_NORMAL) {
  1582. if (iscsi_host_add(qedi->shost, &pdev->dev)) {
  1583. QEDI_ERR(&qedi->dbg_ctx,
  1584. "Could not add iscsi host\n");
  1585. rc = -ENOMEM;
  1586. goto remove_host;
  1587. }
  1588. /* Allocate uio buffers */
  1589. rc = qedi_alloc_uio_rings(qedi);
  1590. if (rc) {
  1591. QEDI_ERR(&qedi->dbg_ctx,
  1592. "UIO alloc ring failed err=%d\n", rc);
  1593. goto remove_host;
  1594. }
  1595. rc = qedi_init_uio(qedi);
  1596. if (rc) {
  1597. QEDI_ERR(&qedi->dbg_ctx,
  1598. "UIO init failed, err=%d\n", rc);
  1599. goto free_uio;
  1600. }
  1601. /* host the array on iscsi_conn */
  1602. rc = qedi_setup_cid_que(qedi);
  1603. if (rc) {
  1604. QEDI_ERR(&qedi->dbg_ctx,
  1605. "Could not setup cid que\n");
  1606. goto free_uio;
  1607. }
  1608. rc = qedi_cm_alloc_mem(qedi);
  1609. if (rc) {
  1610. QEDI_ERR(&qedi->dbg_ctx,
  1611. "Could not alloc cm memory\n");
  1612. goto free_cid_que;
  1613. }
  1614. rc = qedi_alloc_itt(qedi);
  1615. if (rc) {
  1616. QEDI_ERR(&qedi->dbg_ctx,
  1617. "Could not alloc itt memory\n");
  1618. goto free_cid_que;
  1619. }
  1620. sprintf(host_buf, "host_%d", qedi->shost->host_no);
  1621. qedi->tmf_thread = create_singlethread_workqueue(host_buf);
  1622. if (!qedi->tmf_thread) {
  1623. QEDI_ERR(&qedi->dbg_ctx,
  1624. "Unable to start tmf thread!\n");
  1625. rc = -ENODEV;
  1626. goto free_cid_que;
  1627. }
  1628. sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
  1629. qedi->offload_thread = create_workqueue(host_buf);
  1630. if (!qedi->offload_thread) {
  1631. QEDI_ERR(&qedi->dbg_ctx,
  1632. "Unable to start offload thread!\n");
  1633. rc = -ENODEV;
  1634. goto free_cid_que;
  1635. }
  1636. /* F/w needs 1st task context memory entry for performance */
  1637. set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
  1638. atomic_set(&qedi->num_offloads, 0);
  1639. }
  1640. return 0;
  1641. free_cid_que:
  1642. qedi_release_cid_que(qedi);
  1643. free_uio:
  1644. qedi_free_uio(qedi->udev);
  1645. remove_host:
  1646. #ifdef CONFIG_DEBUG_FS
  1647. qedi_dbg_host_exit(&qedi->dbg_ctx);
  1648. #endif
  1649. iscsi_host_remove(qedi->shost);
  1650. stop_iscsi_func:
  1651. qedi_ops->stop(qedi->cdev);
  1652. stop_slowpath:
  1653. qedi_ops->common->slowpath_stop(qedi->cdev);
  1654. stop_hw:
  1655. qedi_ops->common->remove(qedi->cdev);
  1656. free_pf_params:
  1657. qedi_free_iscsi_pf_param(qedi);
  1658. free_host:
  1659. iscsi_host_free(qedi->shost);
  1660. exit_probe:
  1661. return rc;
  1662. }
  1663. static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1664. {
  1665. return __qedi_probe(pdev, QEDI_MODE_NORMAL);
  1666. }
  1667. static void qedi_remove(struct pci_dev *pdev)
  1668. {
  1669. __qedi_remove(pdev, QEDI_MODE_NORMAL);
  1670. }
  1671. static struct pci_device_id qedi_pci_tbl[] = {
  1672. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
  1673. { 0 },
  1674. };
  1675. MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
  1676. static enum cpuhp_state qedi_cpuhp_state;
  1677. static struct pci_driver qedi_pci_driver = {
  1678. .name = QEDI_MODULE_NAME,
  1679. .id_table = qedi_pci_tbl,
  1680. .probe = qedi_probe,
  1681. .remove = qedi_remove,
  1682. };
  1683. static int __init qedi_init(void)
  1684. {
  1685. struct qedi_percpu_s *p;
  1686. int cpu, rc = 0;
  1687. qedi_ops = qed_get_iscsi_ops();
  1688. if (!qedi_ops) {
  1689. QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
  1690. return -EINVAL;
  1691. }
  1692. #ifdef CONFIG_DEBUG_FS
  1693. qedi_dbg_init("qedi");
  1694. #endif
  1695. qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
  1696. if (!qedi_scsi_transport) {
  1697. QEDI_ERR(NULL, "Could not register qedi transport");
  1698. rc = -ENOMEM;
  1699. goto exit_qedi_init_1;
  1700. }
  1701. for_each_possible_cpu(cpu) {
  1702. p = &per_cpu(qedi_percpu, cpu);
  1703. INIT_LIST_HEAD(&p->work_list);
  1704. spin_lock_init(&p->p_work_lock);
  1705. p->iothread = NULL;
  1706. }
  1707. rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
  1708. qedi_cpu_online, qedi_cpu_offline);
  1709. if (rc < 0)
  1710. goto exit_qedi_init_2;
  1711. qedi_cpuhp_state = rc;
  1712. rc = pci_register_driver(&qedi_pci_driver);
  1713. if (rc) {
  1714. QEDI_ERR(NULL, "Failed to register driver\n");
  1715. goto exit_qedi_hp;
  1716. }
  1717. return 0;
  1718. exit_qedi_hp:
  1719. cpuhp_remove_state(qedi_cpuhp_state);
  1720. exit_qedi_init_2:
  1721. iscsi_unregister_transport(&qedi_iscsi_transport);
  1722. exit_qedi_init_1:
  1723. #ifdef CONFIG_DEBUG_FS
  1724. qedi_dbg_exit();
  1725. #endif
  1726. qed_put_iscsi_ops();
  1727. return rc;
  1728. }
  1729. static void __exit qedi_cleanup(void)
  1730. {
  1731. pci_unregister_driver(&qedi_pci_driver);
  1732. cpuhp_remove_state(qedi_cpuhp_state);
  1733. iscsi_unregister_transport(&qedi_iscsi_transport);
  1734. #ifdef CONFIG_DEBUG_FS
  1735. qedi_dbg_exit();
  1736. #endif
  1737. qed_put_iscsi_ops();
  1738. }
  1739. MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
  1740. MODULE_LICENSE("GPL");
  1741. MODULE_AUTHOR("QLogic Corporation");
  1742. MODULE_VERSION(QEDI_MODULE_VERSION);
  1743. module_init(qedi_init);
  1744. module_exit(qedi_cleanup);