qedi_main.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic iSCSI Offload Driver
  4. * Copyright (c) 2016 Cavium Inc.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/kernel.h>
  9. #include <linux/if_arp.h>
  10. #include <scsi/iscsi_if.h>
  11. #include <linux/inet.h>
  12. #include <net/arp.h>
  13. #include <linux/list.h>
  14. #include <linux/kthread.h>
  15. #include <linux/mm.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/cpu.h>
  18. #include <linux/iscsi_boot_sysfs.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_eh.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi.h>
  24. #include "qedi.h"
  25. #include "qedi_gbl.h"
  26. #include "qedi_iscsi.h"
  27. static uint qedi_qed_debug;
  28. module_param(qedi_qed_debug, uint, 0644);
  29. MODULE_PARM_DESC(qedi_qed_debug, " QED debug level 0 (default)");
  30. static uint qedi_fw_debug;
  31. module_param(qedi_fw_debug, uint, 0644);
  32. MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
  33. uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
  34. module_param(qedi_dbg_log, uint, 0644);
  35. MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
  36. uint qedi_io_tracing;
  37. module_param(qedi_io_tracing, uint, 0644);
  38. MODULE_PARM_DESC(qedi_io_tracing,
  39. " Enable logging of SCSI requests/completions into trace buffer. (default off).");
  40. static uint qedi_ll2_buf_size = 0x400;
  41. module_param(qedi_ll2_buf_size, uint, 0644);
  42. MODULE_PARM_DESC(qedi_ll2_buf_size,
  43. "parameter to set ping packet size, default - 0x400, Jumbo packets - 0x2400.");
  44. static uint qedi_flags_override;
  45. module_param(qedi_flags_override, uint, 0644);
  46. MODULE_PARM_DESC(qedi_flags_override, "Disable/Enable MFW error flags bits action.");
  47. const struct qed_iscsi_ops *qedi_ops;
  48. static struct scsi_transport_template *qedi_scsi_transport;
  49. static struct pci_driver qedi_pci_driver;
  50. static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
  51. static LIST_HEAD(qedi_udev_list);
  52. /* Static function declaration */
  53. static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
  54. static void qedi_free_global_queues(struct qedi_ctx *qedi);
  55. static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
  56. static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
  57. static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
  58. static struct nvm_iscsi_block *qedi_get_nvram_block(struct qedi_ctx *qedi);
  59. static void qedi_recovery_handler(struct work_struct *work);
  60. static void qedi_schedule_hw_err_handler(void *dev,
  61. enum qed_hw_err_type err_type);
  62. static int qedi_suspend(struct pci_dev *pdev, pm_message_t state);
  63. static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
  64. {
  65. struct qedi_ctx *qedi;
  66. struct qedi_endpoint *qedi_ep;
  67. struct iscsi_eqe_data *data;
  68. int rval = 0;
  69. if (!context || !fw_handle) {
  70. QEDI_ERR(NULL, "Recv event with ctx NULL\n");
  71. return -EINVAL;
  72. }
  73. qedi = (struct qedi_ctx *)context;
  74. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  75. "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
  76. data = (struct iscsi_eqe_data *)fw_handle;
  77. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  78. "icid=0x%x conn_id=0x%x err-code=0x%x error-pdu-opcode-reserved=0x%x\n",
  79. data->icid, data->conn_id, data->error_code,
  80. data->error_pdu_opcode_reserved);
  81. qedi_ep = qedi->ep_tbl[data->icid];
  82. if (!qedi_ep) {
  83. QEDI_WARN(&qedi->dbg_ctx,
  84. "Cannot process event, ep already disconnected, cid=0x%x\n",
  85. data->icid);
  86. WARN_ON(1);
  87. return -ENODEV;
  88. }
  89. switch (fw_event_code) {
  90. case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
  91. if (qedi_ep->state == EP_STATE_OFLDCONN_START)
  92. qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
  93. wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
  94. break;
  95. case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
  96. qedi_ep->state = EP_STATE_DISCONN_COMPL;
  97. wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
  98. break;
  99. case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
  100. qedi_process_iscsi_error(qedi_ep, data);
  101. break;
  102. case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
  103. case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
  104. case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
  105. case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
  106. case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
  107. case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
  108. case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
  109. qedi_process_tcp_error(qedi_ep, data);
  110. break;
  111. default:
  112. QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
  113. fw_event_code);
  114. }
  115. return rval;
  116. }
  117. static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
  118. {
  119. struct qedi_uio_dev *udev = uinfo->priv;
  120. struct qedi_ctx *qedi = udev->qedi;
  121. if (!capable(CAP_NET_ADMIN))
  122. return -EPERM;
  123. if (udev->uio_dev != -1)
  124. return -EBUSY;
  125. rtnl_lock();
  126. udev->uio_dev = iminor(inode);
  127. qedi_reset_uio_rings(udev);
  128. set_bit(UIO_DEV_OPENED, &qedi->flags);
  129. rtnl_unlock();
  130. return 0;
  131. }
  132. static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
  133. {
  134. struct qedi_uio_dev *udev = uinfo->priv;
  135. struct qedi_ctx *qedi = udev->qedi;
  136. udev->uio_dev = -1;
  137. clear_bit(UIO_DEV_OPENED, &qedi->flags);
  138. qedi_ll2_free_skbs(qedi);
  139. return 0;
  140. }
  141. static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
  142. {
  143. if (udev->uctrl) {
  144. free_page((unsigned long)udev->uctrl);
  145. udev->uctrl = NULL;
  146. }
  147. if (udev->ll2_ring) {
  148. free_page((unsigned long)udev->ll2_ring);
  149. udev->ll2_ring = NULL;
  150. }
  151. if (udev->ll2_buf) {
  152. free_pages((unsigned long)udev->ll2_buf, 2);
  153. udev->ll2_buf = NULL;
  154. }
  155. }
  156. static void __qedi_free_uio(struct qedi_uio_dev *udev)
  157. {
  158. uio_unregister_device(&udev->qedi_uinfo);
  159. __qedi_free_uio_rings(udev);
  160. pci_dev_put(udev->pdev);
  161. kfree(udev);
  162. }
  163. static void qedi_free_uio(struct qedi_uio_dev *udev)
  164. {
  165. if (!udev)
  166. return;
  167. list_del_init(&udev->list);
  168. __qedi_free_uio(udev);
  169. }
  170. static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
  171. {
  172. struct qedi_ctx *qedi = NULL;
  173. struct qedi_uio_ctrl *uctrl = NULL;
  174. qedi = udev->qedi;
  175. uctrl = udev->uctrl;
  176. spin_lock_bh(&qedi->ll2_lock);
  177. uctrl->host_rx_cons = 0;
  178. uctrl->hw_rx_prod = 0;
  179. uctrl->hw_rx_bd_prod = 0;
  180. uctrl->host_rx_bd_cons = 0;
  181. memset(udev->ll2_ring, 0, udev->ll2_ring_size);
  182. memset(udev->ll2_buf, 0, udev->ll2_buf_size);
  183. spin_unlock_bh(&qedi->ll2_lock);
  184. }
  185. static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
  186. {
  187. int rc = 0;
  188. if (udev->ll2_ring || udev->ll2_buf)
  189. return rc;
  190. /* Memory for control area. */
  191. udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL);
  192. if (!udev->uctrl)
  193. return -ENOMEM;
  194. /* Allocating memory for LL2 ring */
  195. udev->ll2_ring_size = QEDI_PAGE_SIZE;
  196. udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
  197. if (!udev->ll2_ring) {
  198. rc = -ENOMEM;
  199. goto exit_alloc_ring;
  200. }
  201. /* Allocating memory for Tx/Rx pkt buffer */
  202. udev->ll2_buf_size = TX_RX_RING * qedi_ll2_buf_size;
  203. udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
  204. udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
  205. __GFP_ZERO, 2);
  206. if (!udev->ll2_buf) {
  207. rc = -ENOMEM;
  208. goto exit_alloc_buf;
  209. }
  210. return rc;
  211. exit_alloc_buf:
  212. free_page((unsigned long)udev->ll2_ring);
  213. udev->ll2_ring = NULL;
  214. exit_alloc_ring:
  215. return rc;
  216. }
  217. static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
  218. {
  219. struct qedi_uio_dev *udev = NULL;
  220. int rc = 0;
  221. list_for_each_entry(udev, &qedi_udev_list, list) {
  222. if (udev->pdev == qedi->pdev) {
  223. udev->qedi = qedi;
  224. if (__qedi_alloc_uio_rings(udev)) {
  225. udev->qedi = NULL;
  226. return -ENOMEM;
  227. }
  228. qedi->udev = udev;
  229. return 0;
  230. }
  231. }
  232. udev = kzalloc(sizeof(*udev), GFP_KERNEL);
  233. if (!udev)
  234. goto err_udev;
  235. udev->uio_dev = -1;
  236. udev->qedi = qedi;
  237. udev->pdev = qedi->pdev;
  238. rc = __qedi_alloc_uio_rings(udev);
  239. if (rc)
  240. goto err_uctrl;
  241. list_add(&udev->list, &qedi_udev_list);
  242. pci_dev_get(udev->pdev);
  243. qedi->udev = udev;
  244. udev->tx_pkt = udev->ll2_buf;
  245. udev->rx_pkt = udev->ll2_buf + qedi_ll2_buf_size;
  246. return 0;
  247. err_uctrl:
  248. kfree(udev);
  249. err_udev:
  250. return -ENOMEM;
  251. }
  252. static int qedi_init_uio(struct qedi_ctx *qedi)
  253. {
  254. struct qedi_uio_dev *udev = qedi->udev;
  255. struct uio_info *uinfo;
  256. int ret = 0;
  257. if (!udev)
  258. return -ENOMEM;
  259. uinfo = &udev->qedi_uinfo;
  260. uinfo->mem[0].addr = (unsigned long)udev->uctrl;
  261. uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
  262. uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
  263. uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
  264. uinfo->mem[1].size = udev->ll2_ring_size;
  265. uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
  266. uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
  267. uinfo->mem[2].size = udev->ll2_buf_size;
  268. uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
  269. uinfo->name = "qedi_uio";
  270. uinfo->version = QEDI_MODULE_VERSION;
  271. uinfo->irq = UIO_IRQ_CUSTOM;
  272. uinfo->open = qedi_uio_open;
  273. uinfo->release = qedi_uio_close;
  274. if (udev->uio_dev == -1) {
  275. if (!uinfo->priv) {
  276. uinfo->priv = udev;
  277. ret = uio_register_device(&udev->pdev->dev, uinfo);
  278. if (ret) {
  279. QEDI_ERR(&qedi->dbg_ctx,
  280. "UIO registration failed\n");
  281. }
  282. }
  283. }
  284. return ret;
  285. }
  286. static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
  287. struct qed_sb_info *sb_info, u16 sb_id)
  288. {
  289. struct status_block *sb_virt;
  290. dma_addr_t sb_phys;
  291. int ret;
  292. sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
  293. sizeof(struct status_block), &sb_phys,
  294. GFP_KERNEL);
  295. if (!sb_virt) {
  296. QEDI_ERR(&qedi->dbg_ctx,
  297. "Status block allocation failed for id = %d.\n",
  298. sb_id);
  299. return -ENOMEM;
  300. }
  301. ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
  302. sb_id, QED_SB_TYPE_STORAGE);
  303. if (ret) {
  304. QEDI_ERR(&qedi->dbg_ctx,
  305. "Status block initialization failed for id = %d.\n",
  306. sb_id);
  307. return ret;
  308. }
  309. return 0;
  310. }
  311. static void qedi_free_sb(struct qedi_ctx *qedi)
  312. {
  313. struct qed_sb_info *sb_info;
  314. int id;
  315. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  316. sb_info = &qedi->sb_array[id];
  317. if (sb_info->sb_virt)
  318. dma_free_coherent(&qedi->pdev->dev,
  319. sizeof(*sb_info->sb_virt),
  320. (void *)sb_info->sb_virt,
  321. sb_info->sb_phys);
  322. }
  323. }
  324. static void qedi_free_fp(struct qedi_ctx *qedi)
  325. {
  326. kfree(qedi->fp_array);
  327. kfree(qedi->sb_array);
  328. }
  329. static void qedi_destroy_fp(struct qedi_ctx *qedi)
  330. {
  331. qedi_free_sb(qedi);
  332. qedi_free_fp(qedi);
  333. }
  334. static int qedi_alloc_fp(struct qedi_ctx *qedi)
  335. {
  336. int ret = 0;
  337. qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
  338. sizeof(struct qedi_fastpath), GFP_KERNEL);
  339. if (!qedi->fp_array) {
  340. QEDI_ERR(&qedi->dbg_ctx,
  341. "fastpath fp array allocation failed.\n");
  342. return -ENOMEM;
  343. }
  344. qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
  345. sizeof(struct qed_sb_info), GFP_KERNEL);
  346. if (!qedi->sb_array) {
  347. QEDI_ERR(&qedi->dbg_ctx,
  348. "fastpath sb array allocation failed.\n");
  349. ret = -ENOMEM;
  350. goto free_fp;
  351. }
  352. return ret;
  353. free_fp:
  354. qedi_free_fp(qedi);
  355. return ret;
  356. }
  357. static void qedi_int_fp(struct qedi_ctx *qedi)
  358. {
  359. struct qedi_fastpath *fp;
  360. int id;
  361. memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
  362. sizeof(*qedi->fp_array));
  363. memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
  364. sizeof(*qedi->sb_array));
  365. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  366. fp = &qedi->fp_array[id];
  367. fp->sb_info = &qedi->sb_array[id];
  368. fp->sb_id = id;
  369. fp->qedi = qedi;
  370. snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
  371. "qedi", id);
  372. /* fp_array[i] ---- irq cookie
  373. * So init data which is needed in int ctx
  374. */
  375. }
  376. }
  377. static int qedi_prepare_fp(struct qedi_ctx *qedi)
  378. {
  379. struct qedi_fastpath *fp;
  380. int id, ret = 0;
  381. ret = qedi_alloc_fp(qedi);
  382. if (ret)
  383. goto err;
  384. qedi_int_fp(qedi);
  385. for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
  386. fp = &qedi->fp_array[id];
  387. ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
  388. if (ret) {
  389. QEDI_ERR(&qedi->dbg_ctx,
  390. "SB allocation and initialization failed.\n");
  391. ret = -EIO;
  392. goto err_init;
  393. }
  394. }
  395. return 0;
  396. err_init:
  397. qedi_free_sb(qedi);
  398. qedi_free_fp(qedi);
  399. err:
  400. return ret;
  401. }
  402. static int qedi_setup_cid_que(struct qedi_ctx *qedi)
  403. {
  404. int i;
  405. qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
  406. sizeof(u32), GFP_KERNEL);
  407. if (!qedi->cid_que.cid_que_base)
  408. return -ENOMEM;
  409. qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
  410. sizeof(struct qedi_conn *),
  411. GFP_KERNEL);
  412. if (!qedi->cid_que.conn_cid_tbl) {
  413. kfree(qedi->cid_que.cid_que_base);
  414. qedi->cid_que.cid_que_base = NULL;
  415. return -ENOMEM;
  416. }
  417. qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
  418. qedi->cid_que.cid_q_prod_idx = 0;
  419. qedi->cid_que.cid_q_cons_idx = 0;
  420. qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
  421. qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
  422. for (i = 0; i < qedi->max_active_conns; i++) {
  423. qedi->cid_que.cid_que[i] = i;
  424. qedi->cid_que.conn_cid_tbl[i] = NULL;
  425. }
  426. return 0;
  427. }
  428. static void qedi_release_cid_que(struct qedi_ctx *qedi)
  429. {
  430. kfree(qedi->cid_que.cid_que_base);
  431. qedi->cid_que.cid_que_base = NULL;
  432. kfree(qedi->cid_que.conn_cid_tbl);
  433. qedi->cid_que.conn_cid_tbl = NULL;
  434. }
  435. static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
  436. u16 start_id, u16 next)
  437. {
  438. id_tbl->start = start_id;
  439. id_tbl->max = size;
  440. id_tbl->next = next;
  441. spin_lock_init(&id_tbl->lock);
  442. id_tbl->table = kcalloc(BITS_TO_LONGS(size), sizeof(long), GFP_KERNEL);
  443. if (!id_tbl->table)
  444. return -ENOMEM;
  445. return 0;
  446. }
  447. static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
  448. {
  449. kfree(id_tbl->table);
  450. id_tbl->table = NULL;
  451. }
  452. int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
  453. {
  454. int ret = -1;
  455. id -= id_tbl->start;
  456. if (id >= id_tbl->max)
  457. return ret;
  458. spin_lock(&id_tbl->lock);
  459. if (!test_bit(id, id_tbl->table)) {
  460. set_bit(id, id_tbl->table);
  461. ret = 0;
  462. }
  463. spin_unlock(&id_tbl->lock);
  464. return ret;
  465. }
  466. u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
  467. {
  468. u16 id;
  469. spin_lock(&id_tbl->lock);
  470. id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
  471. if (id >= id_tbl->max) {
  472. id = QEDI_LOCAL_PORT_INVALID;
  473. if (id_tbl->next != 0) {
  474. id = find_first_zero_bit(id_tbl->table, id_tbl->next);
  475. if (id >= id_tbl->next)
  476. id = QEDI_LOCAL_PORT_INVALID;
  477. }
  478. }
  479. if (id < id_tbl->max) {
  480. set_bit(id, id_tbl->table);
  481. id_tbl->next = (id + 1) & (id_tbl->max - 1);
  482. id += id_tbl->start;
  483. }
  484. spin_unlock(&id_tbl->lock);
  485. return id;
  486. }
  487. void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
  488. {
  489. if (id == QEDI_LOCAL_PORT_INVALID)
  490. return;
  491. id -= id_tbl->start;
  492. if (id >= id_tbl->max)
  493. return;
  494. clear_bit(id, id_tbl->table);
  495. }
  496. static void qedi_cm_free_mem(struct qedi_ctx *qedi)
  497. {
  498. kfree(qedi->ep_tbl);
  499. qedi->ep_tbl = NULL;
  500. qedi_free_id_tbl(&qedi->lcl_port_tbl);
  501. }
  502. static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
  503. {
  504. u16 port_id;
  505. qedi->ep_tbl = kzalloc((qedi->max_active_conns *
  506. sizeof(struct qedi_endpoint *)), GFP_KERNEL);
  507. if (!qedi->ep_tbl)
  508. return -ENOMEM;
  509. port_id = prandom_u32_max(QEDI_LOCAL_PORT_RANGE);
  510. if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
  511. QEDI_LOCAL_PORT_MIN, port_id)) {
  512. qedi_cm_free_mem(qedi);
  513. return -ENOMEM;
  514. }
  515. return 0;
  516. }
  517. static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
  518. {
  519. struct Scsi_Host *shost;
  520. struct qedi_ctx *qedi = NULL;
  521. shost = iscsi_host_alloc(&qedi_host_template,
  522. sizeof(struct qedi_ctx), 0);
  523. if (!shost) {
  524. QEDI_ERR(NULL, "Could not allocate shost\n");
  525. goto exit_setup_shost;
  526. }
  527. shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA - 1;
  528. shost->max_channel = 0;
  529. shost->max_lun = ~0;
  530. shost->max_cmd_len = 16;
  531. shost->transportt = qedi_scsi_transport;
  532. qedi = iscsi_host_priv(shost);
  533. memset(qedi, 0, sizeof(*qedi));
  534. qedi->shost = shost;
  535. qedi->dbg_ctx.host_no = shost->host_no;
  536. qedi->pdev = pdev;
  537. qedi->dbg_ctx.pdev = pdev;
  538. qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
  539. qedi->max_sqes = QEDI_SQ_SIZE;
  540. shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
  541. pci_set_drvdata(pdev, qedi);
  542. exit_setup_shost:
  543. return qedi;
  544. }
  545. static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
  546. {
  547. struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
  548. struct skb_work_list *work;
  549. struct ethhdr *eh;
  550. if (!qedi) {
  551. QEDI_ERR(NULL, "qedi is NULL\n");
  552. return -1;
  553. }
  554. if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
  555. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
  556. "UIO DEV is not opened\n");
  557. kfree_skb(skb);
  558. return 0;
  559. }
  560. eh = (struct ethhdr *)skb->data;
  561. /* Undo VLAN encapsulation */
  562. if (eh->h_proto == htons(ETH_P_8021Q)) {
  563. memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
  564. eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
  565. skb_reset_mac_header(skb);
  566. }
  567. /* Filter out non FIP/FCoE frames here to free them faster */
  568. if (eh->h_proto != htons(ETH_P_ARP) &&
  569. eh->h_proto != htons(ETH_P_IP) &&
  570. eh->h_proto != htons(ETH_P_IPV6)) {
  571. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
  572. "Dropping frame ethertype [0x%x] len [0x%x].\n",
  573. eh->h_proto, skb->len);
  574. kfree_skb(skb);
  575. return 0;
  576. }
  577. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
  578. "Allowed frame ethertype [0x%x] len [0x%x].\n",
  579. eh->h_proto, skb->len);
  580. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  581. if (!work) {
  582. QEDI_WARN(&qedi->dbg_ctx,
  583. "Could not allocate work so dropping frame.\n");
  584. kfree_skb(skb);
  585. return 0;
  586. }
  587. INIT_LIST_HEAD(&work->list);
  588. work->skb = skb;
  589. if (skb_vlan_tag_present(skb))
  590. work->vlan_id = skb_vlan_tag_get(skb);
  591. if (work->vlan_id)
  592. __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
  593. spin_lock_bh(&qedi->ll2_lock);
  594. list_add_tail(&work->list, &qedi->ll2_skb_list);
  595. spin_unlock_bh(&qedi->ll2_lock);
  596. wake_up_process(qedi->ll2_recv_thread);
  597. return 0;
  598. }
  599. /* map this skb to iscsiuio mmaped region */
  600. static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
  601. u16 vlan_id)
  602. {
  603. struct qedi_uio_dev *udev = NULL;
  604. struct qedi_uio_ctrl *uctrl = NULL;
  605. struct qedi_rx_bd rxbd;
  606. struct qedi_rx_bd *p_rxbd;
  607. u32 rx_bd_prod;
  608. void *pkt;
  609. int len = 0;
  610. u32 prod;
  611. if (!qedi) {
  612. QEDI_ERR(NULL, "qedi is NULL\n");
  613. return -1;
  614. }
  615. udev = qedi->udev;
  616. uctrl = udev->uctrl;
  617. ++uctrl->hw_rx_prod_cnt;
  618. prod = (uctrl->hw_rx_prod + 1) % RX_RING;
  619. pkt = udev->rx_pkt + (prod * qedi_ll2_buf_size);
  620. len = min_t(u32, skb->len, (u32)qedi_ll2_buf_size);
  621. memcpy(pkt, skb->data, len);
  622. memset(&rxbd, 0, sizeof(rxbd));
  623. rxbd.rx_pkt_index = prod;
  624. rxbd.rx_pkt_len = len;
  625. rxbd.vlan_id = vlan_id;
  626. uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
  627. rx_bd_prod = uctrl->hw_rx_bd_prod;
  628. p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
  629. p_rxbd += rx_bd_prod;
  630. memcpy(p_rxbd, &rxbd, sizeof(rxbd));
  631. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
  632. "hw_rx_prod [%d] prod [%d] hw_rx_bd_prod [%d] rx_pkt_idx [%d] rx_len [%d].\n",
  633. uctrl->hw_rx_prod, prod, uctrl->hw_rx_bd_prod,
  634. rxbd.rx_pkt_index, rxbd.rx_pkt_len);
  635. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
  636. "host_rx_cons [%d] hw_rx_bd_cons [%d].\n",
  637. uctrl->host_rx_cons, uctrl->host_rx_bd_cons);
  638. uctrl->hw_rx_prod = prod;
  639. /* notify the iscsiuio about new packet */
  640. uio_event_notify(&udev->qedi_uinfo);
  641. return 0;
  642. }
  643. static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
  644. {
  645. struct skb_work_list *work, *work_tmp;
  646. spin_lock_bh(&qedi->ll2_lock);
  647. list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
  648. list_del(&work->list);
  649. kfree_skb(work->skb);
  650. kfree(work);
  651. }
  652. spin_unlock_bh(&qedi->ll2_lock);
  653. }
  654. static int qedi_ll2_recv_thread(void *arg)
  655. {
  656. struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
  657. struct skb_work_list *work, *work_tmp;
  658. set_user_nice(current, -20);
  659. while (!kthread_should_stop()) {
  660. spin_lock_bh(&qedi->ll2_lock);
  661. list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
  662. list) {
  663. list_del(&work->list);
  664. qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
  665. kfree_skb(work->skb);
  666. kfree(work);
  667. }
  668. set_current_state(TASK_INTERRUPTIBLE);
  669. spin_unlock_bh(&qedi->ll2_lock);
  670. schedule();
  671. }
  672. __set_current_state(TASK_RUNNING);
  673. return 0;
  674. }
  675. static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
  676. {
  677. u8 num_sq_pages;
  678. u32 log_page_size;
  679. int rval = 0;
  680. num_sq_pages = (MAX_OUTSTANDING_TASKS_PER_CON * 8) / QEDI_PAGE_SIZE;
  681. qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
  682. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  683. "Number of CQ count is %d\n", qedi->num_queues);
  684. memset(&qedi->pf_params.iscsi_pf_params, 0,
  685. sizeof(qedi->pf_params.iscsi_pf_params));
  686. qedi->p_cpuq = dma_alloc_coherent(&qedi->pdev->dev,
  687. qedi->num_queues * sizeof(struct qedi_glbl_q_params),
  688. &qedi->hw_p_cpuq, GFP_KERNEL);
  689. if (!qedi->p_cpuq) {
  690. QEDI_ERR(&qedi->dbg_ctx, "dma_alloc_coherent fail\n");
  691. rval = -1;
  692. goto err_alloc_mem;
  693. }
  694. rval = qedi_alloc_global_queues(qedi);
  695. if (rval) {
  696. QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
  697. rval = -1;
  698. goto err_alloc_mem;
  699. }
  700. qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
  701. qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
  702. qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
  703. qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
  704. qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
  705. qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
  706. qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
  707. qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
  708. qedi->pf_params.iscsi_pf_params.two_msl_timer = QED_TWO_MSL_TIMER_DFLT;
  709. qedi->pf_params.iscsi_pf_params.tx_sws_timer = QED_TX_SWS_TIMER_DFLT;
  710. qedi->pf_params.iscsi_pf_params.max_fin_rt = 2;
  711. for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
  712. if ((1 << log_page_size) == QEDI_PAGE_SIZE)
  713. break;
  714. }
  715. qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
  716. qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
  717. (u64)qedi->hw_p_cpuq;
  718. /* RQ BDQ initializations.
  719. * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
  720. * rqe_log_size: 8 for 256B RQE
  721. */
  722. qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
  723. /* BDQ address and size */
  724. qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
  725. qedi->bdq_pbl_list_dma;
  726. qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
  727. qedi->bdq_pbl_list_num_entries;
  728. qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
  729. /* cq_num_entries: num_tasks + rq_num_entries */
  730. qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
  731. qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
  732. qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
  733. err_alloc_mem:
  734. return rval;
  735. }
  736. /* Free DMA coherent memory for array of queue pointers we pass to qed */
  737. static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
  738. {
  739. size_t size = 0;
  740. if (qedi->p_cpuq) {
  741. size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
  742. dma_free_coherent(&qedi->pdev->dev, size, qedi->p_cpuq,
  743. qedi->hw_p_cpuq);
  744. }
  745. qedi_free_global_queues(qedi);
  746. kfree(qedi->global_queues);
  747. }
  748. static void qedi_get_boot_tgt_info(struct nvm_iscsi_block *block,
  749. struct qedi_boot_target *tgt, u8 index)
  750. {
  751. u32 ipv6_en;
  752. ipv6_en = !!(block->generic.ctrl_flags &
  753. NVM_ISCSI_CFG_GEN_IPV6_ENABLED);
  754. snprintf(tgt->iscsi_name, sizeof(tgt->iscsi_name), "%s",
  755. block->target[index].target_name.byte);
  756. tgt->ipv6_en = ipv6_en;
  757. if (ipv6_en)
  758. snprintf(tgt->ip_addr, IPV6_LEN, "%pI6\n",
  759. block->target[index].ipv6_addr.byte);
  760. else
  761. snprintf(tgt->ip_addr, IPV4_LEN, "%pI4\n",
  762. block->target[index].ipv4_addr.byte);
  763. }
  764. static int qedi_find_boot_info(struct qedi_ctx *qedi,
  765. struct qed_mfw_tlv_iscsi *iscsi,
  766. struct nvm_iscsi_block *block)
  767. {
  768. struct qedi_boot_target *pri_tgt = NULL, *sec_tgt = NULL;
  769. u32 pri_ctrl_flags = 0, sec_ctrl_flags = 0, found = 0;
  770. struct iscsi_cls_session *cls_sess;
  771. struct iscsi_cls_conn *cls_conn;
  772. struct qedi_conn *qedi_conn;
  773. struct iscsi_session *sess;
  774. struct iscsi_conn *conn;
  775. char ep_ip_addr[64];
  776. int i, ret = 0;
  777. pri_ctrl_flags = !!(block->target[0].ctrl_flags &
  778. NVM_ISCSI_CFG_TARGET_ENABLED);
  779. if (pri_ctrl_flags) {
  780. pri_tgt = kzalloc(sizeof(*pri_tgt), GFP_KERNEL);
  781. if (!pri_tgt)
  782. return -1;
  783. qedi_get_boot_tgt_info(block, pri_tgt, 0);
  784. }
  785. sec_ctrl_flags = !!(block->target[1].ctrl_flags &
  786. NVM_ISCSI_CFG_TARGET_ENABLED);
  787. if (sec_ctrl_flags) {
  788. sec_tgt = kzalloc(sizeof(*sec_tgt), GFP_KERNEL);
  789. if (!sec_tgt) {
  790. ret = -1;
  791. goto free_tgt;
  792. }
  793. qedi_get_boot_tgt_info(block, sec_tgt, 1);
  794. }
  795. for (i = 0; i < qedi->max_active_conns; i++) {
  796. qedi_conn = qedi_get_conn_from_id(qedi, i);
  797. if (!qedi_conn)
  798. continue;
  799. if (qedi_conn->ep->ip_type == TCP_IPV4)
  800. snprintf(ep_ip_addr, IPV4_LEN, "%pI4\n",
  801. qedi_conn->ep->dst_addr);
  802. else
  803. snprintf(ep_ip_addr, IPV6_LEN, "%pI6\n",
  804. qedi_conn->ep->dst_addr);
  805. cls_conn = qedi_conn->cls_conn;
  806. conn = cls_conn->dd_data;
  807. cls_sess = iscsi_conn_to_session(cls_conn);
  808. sess = cls_sess->dd_data;
  809. if (!iscsi_is_session_online(cls_sess))
  810. continue;
  811. if (!sess->targetname)
  812. continue;
  813. if (pri_ctrl_flags) {
  814. if (!strcmp(pri_tgt->iscsi_name, sess->targetname) &&
  815. !strcmp(pri_tgt->ip_addr, ep_ip_addr)) {
  816. found = 1;
  817. break;
  818. }
  819. }
  820. if (sec_ctrl_flags) {
  821. if (!strcmp(sec_tgt->iscsi_name, sess->targetname) &&
  822. !strcmp(sec_tgt->ip_addr, ep_ip_addr)) {
  823. found = 1;
  824. break;
  825. }
  826. }
  827. }
  828. if (found) {
  829. if (conn->hdrdgst_en) {
  830. iscsi->header_digest_set = true;
  831. iscsi->header_digest = 1;
  832. }
  833. if (conn->datadgst_en) {
  834. iscsi->data_digest_set = true;
  835. iscsi->data_digest = 1;
  836. }
  837. iscsi->boot_taget_portal_set = true;
  838. iscsi->boot_taget_portal = sess->tpgt;
  839. } else {
  840. ret = -1;
  841. }
  842. if (sec_ctrl_flags)
  843. kfree(sec_tgt);
  844. free_tgt:
  845. if (pri_ctrl_flags)
  846. kfree(pri_tgt);
  847. return ret;
  848. }
  849. static void qedi_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
  850. {
  851. struct qedi_ctx *qedi;
  852. if (!dev) {
  853. QEDI_INFO(NULL, QEDI_LOG_EVT,
  854. "dev is NULL so ignoring get_generic_tlv_data request.\n");
  855. return;
  856. }
  857. qedi = (struct qedi_ctx *)dev;
  858. memset(data, 0, sizeof(struct qed_generic_tlvs));
  859. ether_addr_copy(data->mac[0], qedi->mac);
  860. }
  861. /*
  862. * Protocol TLV handler
  863. */
  864. static void qedi_get_protocol_tlv_data(void *dev, void *data)
  865. {
  866. struct qed_mfw_tlv_iscsi *iscsi = data;
  867. struct qed_iscsi_stats *fw_iscsi_stats;
  868. struct nvm_iscsi_block *block = NULL;
  869. u32 chap_en = 0, mchap_en = 0;
  870. struct qedi_ctx *qedi = dev;
  871. int rval = 0;
  872. fw_iscsi_stats = kmalloc(sizeof(*fw_iscsi_stats), GFP_KERNEL);
  873. if (!fw_iscsi_stats) {
  874. QEDI_ERR(&qedi->dbg_ctx,
  875. "Could not allocate memory for fw_iscsi_stats.\n");
  876. goto exit_get_data;
  877. }
  878. mutex_lock(&qedi->stats_lock);
  879. /* Query firmware for offload stats */
  880. qedi_ops->get_stats(qedi->cdev, fw_iscsi_stats);
  881. mutex_unlock(&qedi->stats_lock);
  882. iscsi->rx_frames_set = true;
  883. iscsi->rx_frames = fw_iscsi_stats->iscsi_rx_packet_cnt;
  884. iscsi->rx_bytes_set = true;
  885. iscsi->rx_bytes = fw_iscsi_stats->iscsi_rx_bytes_cnt;
  886. iscsi->tx_frames_set = true;
  887. iscsi->tx_frames = fw_iscsi_stats->iscsi_tx_packet_cnt;
  888. iscsi->tx_bytes_set = true;
  889. iscsi->tx_bytes = fw_iscsi_stats->iscsi_tx_bytes_cnt;
  890. iscsi->frame_size_set = true;
  891. iscsi->frame_size = qedi->ll2_mtu;
  892. block = qedi_get_nvram_block(qedi);
  893. if (block) {
  894. chap_en = !!(block->generic.ctrl_flags &
  895. NVM_ISCSI_CFG_GEN_CHAP_ENABLED);
  896. mchap_en = !!(block->generic.ctrl_flags &
  897. NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED);
  898. iscsi->auth_method_set = (chap_en || mchap_en) ? true : false;
  899. iscsi->auth_method = 1;
  900. if (chap_en)
  901. iscsi->auth_method = 2;
  902. if (mchap_en)
  903. iscsi->auth_method = 3;
  904. iscsi->tx_desc_size_set = true;
  905. iscsi->tx_desc_size = QEDI_SQ_SIZE;
  906. iscsi->rx_desc_size_set = true;
  907. iscsi->rx_desc_size = QEDI_CQ_SIZE;
  908. /* tpgt, hdr digest, data digest */
  909. rval = qedi_find_boot_info(qedi, iscsi, block);
  910. if (rval)
  911. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  912. "Boot target not set");
  913. }
  914. kfree(fw_iscsi_stats);
  915. exit_get_data:
  916. return;
  917. }
  918. void qedi_schedule_hw_err_handler(void *dev,
  919. enum qed_hw_err_type err_type)
  920. {
  921. struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
  922. unsigned long override_flags = qedi_flags_override;
  923. if (override_flags && test_bit(QEDI_ERR_OVERRIDE_EN, &override_flags))
  924. qedi->qedi_err_flags = qedi_flags_override;
  925. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  926. "HW error handler scheduled, err=%d err_flags=0x%x\n",
  927. err_type, qedi->qedi_err_flags);
  928. switch (err_type) {
  929. case QED_HW_ERR_FAN_FAIL:
  930. schedule_delayed_work(&qedi->board_disable_work, 0);
  931. break;
  932. case QED_HW_ERR_MFW_RESP_FAIL:
  933. case QED_HW_ERR_HW_ATTN:
  934. case QED_HW_ERR_DMAE_FAIL:
  935. case QED_HW_ERR_RAMROD_FAIL:
  936. case QED_HW_ERR_FW_ASSERT:
  937. /* Prevent HW attentions from being reasserted */
  938. if (test_bit(QEDI_ERR_ATTN_CLR_EN, &qedi->qedi_err_flags))
  939. qedi_ops->common->attn_clr_enable(qedi->cdev, true);
  940. if (err_type == QED_HW_ERR_RAMROD_FAIL &&
  941. test_bit(QEDI_ERR_IS_RECOVERABLE, &qedi->qedi_err_flags))
  942. qedi_ops->common->recovery_process(qedi->cdev);
  943. break;
  944. default:
  945. break;
  946. }
  947. }
  948. static void qedi_schedule_recovery_handler(void *dev)
  949. {
  950. struct qedi_ctx *qedi = dev;
  951. QEDI_ERR(&qedi->dbg_ctx, "Recovery handler scheduled.\n");
  952. if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags))
  953. return;
  954. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  955. schedule_delayed_work(&qedi->recovery_work, 0);
  956. }
  957. static void qedi_set_conn_recovery(struct iscsi_cls_session *cls_session)
  958. {
  959. struct iscsi_session *session = cls_session->dd_data;
  960. struct iscsi_conn *conn = session->leadconn;
  961. struct qedi_conn *qedi_conn = conn->dd_data;
  962. qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
  963. }
  964. static void qedi_link_update(void *dev, struct qed_link_output *link)
  965. {
  966. struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
  967. if (link->link_up) {
  968. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
  969. atomic_set(&qedi->link_state, QEDI_LINK_UP);
  970. } else {
  971. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  972. "Link Down event.\n");
  973. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  974. iscsi_host_for_each_session(qedi->shost, qedi_set_conn_recovery);
  975. }
  976. }
  977. static struct qed_iscsi_cb_ops qedi_cb_ops = {
  978. {
  979. .link_update = qedi_link_update,
  980. .schedule_recovery_handler = qedi_schedule_recovery_handler,
  981. .schedule_hw_err_handler = qedi_schedule_hw_err_handler,
  982. .get_protocol_tlv_data = qedi_get_protocol_tlv_data,
  983. .get_generic_tlv_data = qedi_get_generic_tlv_data,
  984. }
  985. };
  986. static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
  987. u16 que_idx, struct qedi_percpu_s *p)
  988. {
  989. struct qedi_work *qedi_work;
  990. struct qedi_conn *q_conn;
  991. struct qedi_cmd *qedi_cmd;
  992. u32 iscsi_cid;
  993. int rc = 0;
  994. iscsi_cid = cqe->cqe_common.conn_id;
  995. q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
  996. if (!q_conn) {
  997. QEDI_WARN(&qedi->dbg_ctx,
  998. "Session no longer exists for cid=0x%x!!\n",
  999. iscsi_cid);
  1000. return -1;
  1001. }
  1002. switch (cqe->cqe_common.cqe_type) {
  1003. case ISCSI_CQE_TYPE_SOLICITED:
  1004. case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
  1005. qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
  1006. if (!qedi_cmd) {
  1007. rc = -1;
  1008. break;
  1009. }
  1010. INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
  1011. qedi_cmd->cqe_work.qedi = qedi;
  1012. memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
  1013. qedi_cmd->cqe_work.que_idx = que_idx;
  1014. qedi_cmd->cqe_work.is_solicited = true;
  1015. list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
  1016. break;
  1017. case ISCSI_CQE_TYPE_UNSOLICITED:
  1018. case ISCSI_CQE_TYPE_DUMMY:
  1019. case ISCSI_CQE_TYPE_TASK_CLEANUP:
  1020. qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
  1021. if (!qedi_work) {
  1022. rc = -1;
  1023. break;
  1024. }
  1025. INIT_LIST_HEAD(&qedi_work->list);
  1026. qedi_work->qedi = qedi;
  1027. memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
  1028. qedi_work->que_idx = que_idx;
  1029. qedi_work->is_solicited = false;
  1030. list_add_tail(&qedi_work->list, &p->work_list);
  1031. break;
  1032. default:
  1033. rc = -1;
  1034. QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
  1035. }
  1036. return rc;
  1037. }
  1038. static bool qedi_process_completions(struct qedi_fastpath *fp)
  1039. {
  1040. struct qedi_ctx *qedi = fp->qedi;
  1041. struct qed_sb_info *sb_info = fp->sb_info;
  1042. struct status_block *sb = sb_info->sb_virt;
  1043. struct qedi_percpu_s *p = NULL;
  1044. struct global_queue *que;
  1045. u16 prod_idx;
  1046. unsigned long flags;
  1047. union iscsi_cqe *cqe;
  1048. int cpu;
  1049. int ret;
  1050. /* Get the current firmware producer index */
  1051. prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
  1052. if (prod_idx >= QEDI_CQ_SIZE)
  1053. prod_idx = prod_idx % QEDI_CQ_SIZE;
  1054. que = qedi->global_queues[fp->sb_id];
  1055. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
  1056. "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
  1057. que, prod_idx, que->cq_cons_idx, fp->sb_id);
  1058. qedi->intr_cpu = fp->sb_id;
  1059. cpu = smp_processor_id();
  1060. p = &per_cpu(qedi_percpu, cpu);
  1061. if (unlikely(!p->iothread))
  1062. WARN_ON(1);
  1063. spin_lock_irqsave(&p->p_work_lock, flags);
  1064. while (que->cq_cons_idx != prod_idx) {
  1065. cqe = &que->cq[que->cq_cons_idx];
  1066. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
  1067. "cqe=%p prod_idx=%d cons_idx=%d.\n",
  1068. cqe, prod_idx, que->cq_cons_idx);
  1069. ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
  1070. if (ret)
  1071. QEDI_WARN(&qedi->dbg_ctx,
  1072. "Dropping CQE 0x%x for cid=0x%x.\n",
  1073. que->cq_cons_idx, cqe->cqe_common.conn_id);
  1074. que->cq_cons_idx++;
  1075. if (que->cq_cons_idx == QEDI_CQ_SIZE)
  1076. que->cq_cons_idx = 0;
  1077. }
  1078. wake_up_process(p->iothread);
  1079. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1080. return true;
  1081. }
  1082. static bool qedi_fp_has_work(struct qedi_fastpath *fp)
  1083. {
  1084. struct qedi_ctx *qedi = fp->qedi;
  1085. struct global_queue *que;
  1086. struct qed_sb_info *sb_info = fp->sb_info;
  1087. struct status_block *sb = sb_info->sb_virt;
  1088. u16 prod_idx;
  1089. barrier();
  1090. /* Get the current firmware producer index */
  1091. prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
  1092. /* Get the pointer to the global CQ this completion is on */
  1093. que = qedi->global_queues[fp->sb_id];
  1094. /* prod idx wrap around uint16 */
  1095. if (prod_idx >= QEDI_CQ_SIZE)
  1096. prod_idx = prod_idx % QEDI_CQ_SIZE;
  1097. return (que->cq_cons_idx != prod_idx);
  1098. }
  1099. /* MSI-X fastpath handler code */
  1100. static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
  1101. {
  1102. struct qedi_fastpath *fp = dev_id;
  1103. struct qedi_ctx *qedi = fp->qedi;
  1104. bool wake_io_thread = true;
  1105. qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
  1106. process_again:
  1107. wake_io_thread = qedi_process_completions(fp);
  1108. if (wake_io_thread) {
  1109. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1110. "process already running\n");
  1111. }
  1112. if (!qedi_fp_has_work(fp))
  1113. qed_sb_update_sb_idx(fp->sb_info);
  1114. /* Check for more work */
  1115. rmb();
  1116. if (!qedi_fp_has_work(fp))
  1117. qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
  1118. else
  1119. goto process_again;
  1120. return IRQ_HANDLED;
  1121. }
  1122. /* simd handler for MSI/INTa */
  1123. static void qedi_simd_int_handler(void *cookie)
  1124. {
  1125. /* Cookie is qedi_ctx struct */
  1126. struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
  1127. QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
  1128. }
  1129. #define QEDI_SIMD_HANDLER_NUM 0
  1130. static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
  1131. {
  1132. int i;
  1133. u16 idx;
  1134. if (qedi->int_info.msix_cnt) {
  1135. for (i = 0; i < qedi->int_info.used_cnt; i++) {
  1136. idx = i * qedi->dev_info.common.num_hwfns +
  1137. qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
  1138. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  1139. "Freeing IRQ #%d vector_idx=%d.\n", i, idx);
  1140. synchronize_irq(qedi->int_info.msix[idx].vector);
  1141. irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
  1142. NULL);
  1143. free_irq(qedi->int_info.msix[idx].vector,
  1144. &qedi->fp_array[i]);
  1145. }
  1146. } else {
  1147. qedi_ops->common->simd_handler_clean(qedi->cdev,
  1148. QEDI_SIMD_HANDLER_NUM);
  1149. }
  1150. qedi->int_info.used_cnt = 0;
  1151. qedi_ops->common->set_fp_int(qedi->cdev, 0);
  1152. }
  1153. static int qedi_request_msix_irq(struct qedi_ctx *qedi)
  1154. {
  1155. int i, rc, cpu;
  1156. u16 idx;
  1157. cpu = cpumask_first(cpu_online_mask);
  1158. for (i = 0; i < qedi->msix_count; i++) {
  1159. idx = i * qedi->dev_info.common.num_hwfns +
  1160. qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
  1161. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  1162. "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
  1163. qedi->dev_info.common.num_hwfns,
  1164. qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
  1165. rc = request_irq(qedi->int_info.msix[idx].vector,
  1166. qedi_msix_handler, 0, "qedi",
  1167. &qedi->fp_array[i]);
  1168. if (rc) {
  1169. QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
  1170. qedi_sync_free_irqs(qedi);
  1171. return rc;
  1172. }
  1173. qedi->int_info.used_cnt++;
  1174. rc = irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
  1175. get_cpu_mask(cpu));
  1176. cpu = cpumask_next(cpu, cpu_online_mask);
  1177. }
  1178. return 0;
  1179. }
  1180. static int qedi_setup_int(struct qedi_ctx *qedi)
  1181. {
  1182. int rc = 0;
  1183. rc = qedi_ops->common->set_fp_int(qedi->cdev, qedi->num_queues);
  1184. if (rc < 0)
  1185. goto exit_setup_int;
  1186. qedi->msix_count = rc;
  1187. rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
  1188. if (rc)
  1189. goto exit_setup_int;
  1190. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1191. "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
  1192. qedi->int_info.msix_cnt, num_online_cpus());
  1193. if (qedi->int_info.msix_cnt) {
  1194. rc = qedi_request_msix_irq(qedi);
  1195. goto exit_setup_int;
  1196. } else {
  1197. qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
  1198. QEDI_SIMD_HANDLER_NUM,
  1199. qedi_simd_int_handler);
  1200. qedi->int_info.used_cnt = 1;
  1201. }
  1202. exit_setup_int:
  1203. return rc;
  1204. }
  1205. static void qedi_free_nvm_iscsi_cfg(struct qedi_ctx *qedi)
  1206. {
  1207. if (qedi->iscsi_image)
  1208. dma_free_coherent(&qedi->pdev->dev,
  1209. sizeof(struct qedi_nvm_iscsi_image),
  1210. qedi->iscsi_image, qedi->nvm_buf_dma);
  1211. }
  1212. static int qedi_alloc_nvm_iscsi_cfg(struct qedi_ctx *qedi)
  1213. {
  1214. qedi->iscsi_image = dma_alloc_coherent(&qedi->pdev->dev,
  1215. sizeof(struct qedi_nvm_iscsi_image),
  1216. &qedi->nvm_buf_dma, GFP_KERNEL);
  1217. if (!qedi->iscsi_image) {
  1218. QEDI_ERR(&qedi->dbg_ctx, "Could not allocate NVM BUF.\n");
  1219. return -ENOMEM;
  1220. }
  1221. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  1222. "NVM BUF addr=0x%p dma=0x%llx.\n", qedi->iscsi_image,
  1223. qedi->nvm_buf_dma);
  1224. return 0;
  1225. }
  1226. static void qedi_free_bdq(struct qedi_ctx *qedi)
  1227. {
  1228. int i;
  1229. if (qedi->bdq_pbl_list)
  1230. dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
  1231. qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
  1232. if (qedi->bdq_pbl)
  1233. dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
  1234. qedi->bdq_pbl, qedi->bdq_pbl_dma);
  1235. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  1236. if (qedi->bdq[i].buf_addr) {
  1237. dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
  1238. qedi->bdq[i].buf_addr,
  1239. qedi->bdq[i].buf_dma);
  1240. }
  1241. }
  1242. }
  1243. static void qedi_free_global_queues(struct qedi_ctx *qedi)
  1244. {
  1245. int i;
  1246. struct global_queue **gl = qedi->global_queues;
  1247. for (i = 0; i < qedi->num_queues; i++) {
  1248. if (!gl[i])
  1249. continue;
  1250. if (gl[i]->cq)
  1251. dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
  1252. gl[i]->cq, gl[i]->cq_dma);
  1253. if (gl[i]->cq_pbl)
  1254. dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
  1255. gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
  1256. kfree(gl[i]);
  1257. }
  1258. qedi_free_bdq(qedi);
  1259. qedi_free_nvm_iscsi_cfg(qedi);
  1260. }
  1261. static int qedi_alloc_bdq(struct qedi_ctx *qedi)
  1262. {
  1263. int i;
  1264. struct scsi_bd *pbl;
  1265. u64 *list;
  1266. /* Alloc dma memory for BDQ buffers */
  1267. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  1268. qedi->bdq[i].buf_addr =
  1269. dma_alloc_coherent(&qedi->pdev->dev,
  1270. QEDI_BDQ_BUF_SIZE,
  1271. &qedi->bdq[i].buf_dma,
  1272. GFP_KERNEL);
  1273. if (!qedi->bdq[i].buf_addr) {
  1274. QEDI_ERR(&qedi->dbg_ctx,
  1275. "Could not allocate BDQ buffer %d.\n", i);
  1276. return -ENOMEM;
  1277. }
  1278. }
  1279. /* Alloc dma memory for BDQ page buffer list */
  1280. qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
  1281. qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, QEDI_PAGE_SIZE);
  1282. qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
  1283. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
  1284. qedi->rq_num_entries);
  1285. qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
  1286. qedi->bdq_pbl_mem_size,
  1287. &qedi->bdq_pbl_dma, GFP_KERNEL);
  1288. if (!qedi->bdq_pbl) {
  1289. QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
  1290. return -ENOMEM;
  1291. }
  1292. /*
  1293. * Populate BDQ PBL with physical and virtual address of individual
  1294. * BDQ buffers
  1295. */
  1296. pbl = (struct scsi_bd *)qedi->bdq_pbl;
  1297. for (i = 0; i < QEDI_BDQ_NUM; i++) {
  1298. pbl->address.hi =
  1299. cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
  1300. pbl->address.lo =
  1301. cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
  1302. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1303. "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
  1304. pbl, pbl->address.hi, pbl->address.lo, i);
  1305. pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
  1306. pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
  1307. pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
  1308. pbl->opaque.iscsi_opaque.opaque = cpu_to_le16(i);
  1309. pbl++;
  1310. }
  1311. /* Allocate list of PBL pages */
  1312. qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
  1313. QEDI_PAGE_SIZE,
  1314. &qedi->bdq_pbl_list_dma,
  1315. GFP_KERNEL);
  1316. if (!qedi->bdq_pbl_list) {
  1317. QEDI_ERR(&qedi->dbg_ctx,
  1318. "Could not allocate list of PBL pages.\n");
  1319. return -ENOMEM;
  1320. }
  1321. /*
  1322. * Now populate PBL list with pages that contain pointers to the
  1323. * individual buffers.
  1324. */
  1325. qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size /
  1326. QEDI_PAGE_SIZE;
  1327. list = (u64 *)qedi->bdq_pbl_list;
  1328. for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
  1329. *list = qedi->bdq_pbl_dma;
  1330. list++;
  1331. }
  1332. return 0;
  1333. }
  1334. static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
  1335. {
  1336. u32 *list;
  1337. int i;
  1338. int status;
  1339. u32 *pbl;
  1340. dma_addr_t page;
  1341. int num_pages;
  1342. /*
  1343. * Number of global queues (CQ / RQ). This should
  1344. * be <= number of available MSIX vectors for the PF
  1345. */
  1346. if (!qedi->num_queues) {
  1347. QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
  1348. return -ENOMEM;
  1349. }
  1350. /* Make sure we allocated the PBL that will contain the physical
  1351. * addresses of our queues
  1352. */
  1353. if (!qedi->p_cpuq) {
  1354. status = -EINVAL;
  1355. goto mem_alloc_failure;
  1356. }
  1357. qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
  1358. qedi->num_queues), GFP_KERNEL);
  1359. if (!qedi->global_queues) {
  1360. QEDI_ERR(&qedi->dbg_ctx,
  1361. "Unable to allocate global queues array ptr memory\n");
  1362. return -ENOMEM;
  1363. }
  1364. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  1365. "qedi->global_queues=%p.\n", qedi->global_queues);
  1366. /* Allocate DMA coherent buffers for BDQ */
  1367. status = qedi_alloc_bdq(qedi);
  1368. if (status)
  1369. goto mem_alloc_failure;
  1370. /* Allocate DMA coherent buffers for NVM_ISCSI_CFG */
  1371. status = qedi_alloc_nvm_iscsi_cfg(qedi);
  1372. if (status)
  1373. goto mem_alloc_failure;
  1374. /* Allocate a CQ and an associated PBL for each MSI-X
  1375. * vector.
  1376. */
  1377. for (i = 0; i < qedi->num_queues; i++) {
  1378. qedi->global_queues[i] =
  1379. kzalloc(sizeof(*qedi->global_queues[0]),
  1380. GFP_KERNEL);
  1381. if (!qedi->global_queues[i]) {
  1382. QEDI_ERR(&qedi->dbg_ctx,
  1383. "Unable to allocation global queue %d.\n", i);
  1384. status = -ENOMEM;
  1385. goto mem_alloc_failure;
  1386. }
  1387. qedi->global_queues[i]->cq_mem_size =
  1388. (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
  1389. qedi->global_queues[i]->cq_mem_size =
  1390. (qedi->global_queues[i]->cq_mem_size +
  1391. (QEDI_PAGE_SIZE - 1));
  1392. qedi->global_queues[i]->cq_pbl_size =
  1393. (qedi->global_queues[i]->cq_mem_size /
  1394. QEDI_PAGE_SIZE) * sizeof(void *);
  1395. qedi->global_queues[i]->cq_pbl_size =
  1396. (qedi->global_queues[i]->cq_pbl_size +
  1397. (QEDI_PAGE_SIZE - 1));
  1398. qedi->global_queues[i]->cq = dma_alloc_coherent(&qedi->pdev->dev,
  1399. qedi->global_queues[i]->cq_mem_size,
  1400. &qedi->global_queues[i]->cq_dma,
  1401. GFP_KERNEL);
  1402. if (!qedi->global_queues[i]->cq) {
  1403. QEDI_WARN(&qedi->dbg_ctx,
  1404. "Could not allocate cq.\n");
  1405. status = -ENOMEM;
  1406. goto mem_alloc_failure;
  1407. }
  1408. qedi->global_queues[i]->cq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
  1409. qedi->global_queues[i]->cq_pbl_size,
  1410. &qedi->global_queues[i]->cq_pbl_dma,
  1411. GFP_KERNEL);
  1412. if (!qedi->global_queues[i]->cq_pbl) {
  1413. QEDI_WARN(&qedi->dbg_ctx,
  1414. "Could not allocate cq PBL.\n");
  1415. status = -ENOMEM;
  1416. goto mem_alloc_failure;
  1417. }
  1418. /* Create PBL */
  1419. num_pages = qedi->global_queues[i]->cq_mem_size /
  1420. QEDI_PAGE_SIZE;
  1421. page = qedi->global_queues[i]->cq_dma;
  1422. pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
  1423. while (num_pages--) {
  1424. *pbl = (u32)page;
  1425. pbl++;
  1426. *pbl = (u32)((u64)page >> 32);
  1427. pbl++;
  1428. page += QEDI_PAGE_SIZE;
  1429. }
  1430. }
  1431. list = (u32 *)qedi->p_cpuq;
  1432. /*
  1433. * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
  1434. * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
  1435. * to the physical address which contains an array of pointers to the
  1436. * physical addresses of the specific queue pages.
  1437. */
  1438. for (i = 0; i < qedi->num_queues; i++) {
  1439. *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
  1440. list++;
  1441. *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
  1442. list++;
  1443. *list = (u32)0;
  1444. list++;
  1445. *list = (u32)((u64)0 >> 32);
  1446. list++;
  1447. }
  1448. return 0;
  1449. mem_alloc_failure:
  1450. qedi_free_global_queues(qedi);
  1451. return status;
  1452. }
  1453. int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
  1454. {
  1455. int rval = 0;
  1456. u32 *pbl;
  1457. dma_addr_t page;
  1458. int num_pages;
  1459. if (!ep)
  1460. return -EIO;
  1461. /* Calculate appropriate queue and PBL sizes */
  1462. ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
  1463. ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
  1464. ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
  1465. ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
  1466. ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
  1467. &ep->sq_dma, GFP_KERNEL);
  1468. if (!ep->sq) {
  1469. QEDI_WARN(&qedi->dbg_ctx,
  1470. "Could not allocate send queue.\n");
  1471. rval = -ENOMEM;
  1472. goto out;
  1473. }
  1474. ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
  1475. &ep->sq_pbl_dma, GFP_KERNEL);
  1476. if (!ep->sq_pbl) {
  1477. QEDI_WARN(&qedi->dbg_ctx,
  1478. "Could not allocate send queue PBL.\n");
  1479. rval = -ENOMEM;
  1480. goto out_free_sq;
  1481. }
  1482. /* Create PBL */
  1483. num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
  1484. page = ep->sq_dma;
  1485. pbl = (u32 *)ep->sq_pbl;
  1486. while (num_pages--) {
  1487. *pbl = (u32)page;
  1488. pbl++;
  1489. *pbl = (u32)((u64)page >> 32);
  1490. pbl++;
  1491. page += QEDI_PAGE_SIZE;
  1492. }
  1493. return rval;
  1494. out_free_sq:
  1495. dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
  1496. ep->sq_dma);
  1497. out:
  1498. return rval;
  1499. }
  1500. void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
  1501. {
  1502. if (ep->sq_pbl)
  1503. dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
  1504. ep->sq_pbl_dma);
  1505. if (ep->sq)
  1506. dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
  1507. ep->sq_dma);
  1508. }
  1509. int qedi_get_task_idx(struct qedi_ctx *qedi)
  1510. {
  1511. s16 tmp_idx;
  1512. again:
  1513. tmp_idx = find_first_zero_bit(qedi->task_idx_map,
  1514. MAX_ISCSI_TASK_ENTRIES);
  1515. if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
  1516. QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
  1517. tmp_idx = -1;
  1518. goto err_idx;
  1519. }
  1520. if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
  1521. goto again;
  1522. err_idx:
  1523. return tmp_idx;
  1524. }
  1525. void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
  1526. {
  1527. if (!test_and_clear_bit(idx, qedi->task_idx_map))
  1528. QEDI_ERR(&qedi->dbg_ctx,
  1529. "FW task context, already cleared, tid=0x%x\n", idx);
  1530. }
  1531. void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
  1532. struct qedi_cmd *cmd)
  1533. {
  1534. qedi->itt_map[tid].itt = proto_itt;
  1535. qedi->itt_map[tid].p_cmd = cmd;
  1536. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1537. "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
  1538. qedi->itt_map[tid].itt);
  1539. }
  1540. void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
  1541. {
  1542. u16 i;
  1543. for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
  1544. if (qedi->itt_map[i].itt == itt) {
  1545. *tid = i;
  1546. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1547. "Ref itt=0x%x, found at tid=0x%x\n",
  1548. itt, *tid);
  1549. return;
  1550. }
  1551. }
  1552. WARN_ON(1);
  1553. }
  1554. void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
  1555. {
  1556. *proto_itt = qedi->itt_map[tid].itt;
  1557. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
  1558. "Get itt map tid [0x%x with proto itt[0x%x]",
  1559. tid, *proto_itt);
  1560. }
  1561. struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
  1562. {
  1563. struct qedi_cmd *cmd = NULL;
  1564. if (tid >= MAX_ISCSI_TASK_ENTRIES)
  1565. return NULL;
  1566. cmd = qedi->itt_map[tid].p_cmd;
  1567. if (cmd->task_id != tid)
  1568. return NULL;
  1569. qedi->itt_map[tid].p_cmd = NULL;
  1570. return cmd;
  1571. }
  1572. static int qedi_alloc_itt(struct qedi_ctx *qedi)
  1573. {
  1574. qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
  1575. sizeof(struct qedi_itt_map), GFP_KERNEL);
  1576. if (!qedi->itt_map) {
  1577. QEDI_ERR(&qedi->dbg_ctx,
  1578. "Unable to allocate itt map array memory\n");
  1579. return -ENOMEM;
  1580. }
  1581. return 0;
  1582. }
  1583. static void qedi_free_itt(struct qedi_ctx *qedi)
  1584. {
  1585. kfree(qedi->itt_map);
  1586. }
  1587. static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
  1588. .rx_cb = qedi_ll2_rx,
  1589. .tx_cb = NULL,
  1590. };
  1591. static int qedi_percpu_io_thread(void *arg)
  1592. {
  1593. struct qedi_percpu_s *p = arg;
  1594. struct qedi_work *work, *tmp;
  1595. unsigned long flags;
  1596. LIST_HEAD(work_list);
  1597. set_user_nice(current, -20);
  1598. while (!kthread_should_stop()) {
  1599. spin_lock_irqsave(&p->p_work_lock, flags);
  1600. while (!list_empty(&p->work_list)) {
  1601. list_splice_init(&p->work_list, &work_list);
  1602. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1603. list_for_each_entry_safe(work, tmp, &work_list, list) {
  1604. list_del_init(&work->list);
  1605. qedi_fp_process_cqes(work);
  1606. if (!work->is_solicited)
  1607. kfree(work);
  1608. }
  1609. cond_resched();
  1610. spin_lock_irqsave(&p->p_work_lock, flags);
  1611. }
  1612. set_current_state(TASK_INTERRUPTIBLE);
  1613. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1614. schedule();
  1615. }
  1616. __set_current_state(TASK_RUNNING);
  1617. return 0;
  1618. }
  1619. static int qedi_cpu_online(unsigned int cpu)
  1620. {
  1621. struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
  1622. struct task_struct *thread;
  1623. thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
  1624. cpu_to_node(cpu),
  1625. "qedi_thread/%d", cpu);
  1626. if (IS_ERR(thread))
  1627. return PTR_ERR(thread);
  1628. kthread_bind(thread, cpu);
  1629. p->iothread = thread;
  1630. wake_up_process(thread);
  1631. return 0;
  1632. }
  1633. static int qedi_cpu_offline(unsigned int cpu)
  1634. {
  1635. struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
  1636. struct qedi_work *work, *tmp;
  1637. struct task_struct *thread;
  1638. unsigned long flags;
  1639. spin_lock_irqsave(&p->p_work_lock, flags);
  1640. thread = p->iothread;
  1641. p->iothread = NULL;
  1642. list_for_each_entry_safe(work, tmp, &p->work_list, list) {
  1643. list_del_init(&work->list);
  1644. qedi_fp_process_cqes(work);
  1645. if (!work->is_solicited)
  1646. kfree(work);
  1647. }
  1648. spin_unlock_irqrestore(&p->p_work_lock, flags);
  1649. if (thread)
  1650. kthread_stop(thread);
  1651. return 0;
  1652. }
  1653. void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
  1654. {
  1655. struct qed_ll2_params params;
  1656. qedi_recover_all_conns(qedi);
  1657. qedi_ops->ll2->stop(qedi->cdev);
  1658. qedi_ll2_free_skbs(qedi);
  1659. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
  1660. qedi->ll2_mtu, mtu);
  1661. memset(&params, 0, sizeof(params));
  1662. qedi->ll2_mtu = mtu;
  1663. params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
  1664. params.drop_ttl0_packets = 0;
  1665. params.rx_vlan_stripping = 1;
  1666. ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
  1667. qedi_ops->ll2->start(qedi->cdev, &params);
  1668. }
  1669. /*
  1670. * qedi_get_nvram_block: - Scan through the iSCSI NVRAM block (while accounting
  1671. * for gaps) for the matching absolute-pf-id of the QEDI device.
  1672. */
  1673. static struct nvm_iscsi_block *
  1674. qedi_get_nvram_block(struct qedi_ctx *qedi)
  1675. {
  1676. int i;
  1677. u8 pf;
  1678. u32 flags;
  1679. struct nvm_iscsi_block *block;
  1680. pf = qedi->dev_info.common.abs_pf_id;
  1681. block = &qedi->iscsi_image->iscsi_cfg.block[0];
  1682. for (i = 0; i < NUM_OF_ISCSI_PF_SUPPORTED; i++, block++) {
  1683. flags = ((block->id) & NVM_ISCSI_CFG_BLK_CTRL_FLAG_MASK) >>
  1684. NVM_ISCSI_CFG_BLK_CTRL_FLAG_OFFSET;
  1685. if (flags & (NVM_ISCSI_CFG_BLK_CTRL_FLAG_IS_NOT_EMPTY |
  1686. NVM_ISCSI_CFG_BLK_CTRL_FLAG_PF_MAPPED) &&
  1687. (pf == (block->id & NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_MASK)
  1688. >> NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_OFFSET))
  1689. return block;
  1690. }
  1691. return NULL;
  1692. }
  1693. static ssize_t qedi_show_boot_eth_info(void *data, int type, char *buf)
  1694. {
  1695. struct qedi_ctx *qedi = data;
  1696. struct nvm_iscsi_initiator *initiator;
  1697. int rc = 1;
  1698. u32 ipv6_en, dhcp_en, ip_len;
  1699. struct nvm_iscsi_block *block;
  1700. char *fmt, *ip, *sub, *gw;
  1701. block = qedi_get_nvram_block(qedi);
  1702. if (!block)
  1703. return 0;
  1704. initiator = &block->initiator;
  1705. ipv6_en = block->generic.ctrl_flags &
  1706. NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
  1707. dhcp_en = block->generic.ctrl_flags &
  1708. NVM_ISCSI_CFG_GEN_DHCP_TCPIP_CONFIG_ENABLED;
  1709. /* Static IP assignments. */
  1710. fmt = ipv6_en ? "%pI6\n" : "%pI4\n";
  1711. ip = ipv6_en ? initiator->ipv6.addr.byte : initiator->ipv4.addr.byte;
  1712. ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
  1713. sub = ipv6_en ? initiator->ipv6.subnet_mask.byte :
  1714. initiator->ipv4.subnet_mask.byte;
  1715. gw = ipv6_en ? initiator->ipv6.gateway.byte :
  1716. initiator->ipv4.gateway.byte;
  1717. /* DHCP IP adjustments. */
  1718. fmt = dhcp_en ? "%s\n" : fmt;
  1719. if (dhcp_en) {
  1720. ip = ipv6_en ? "0::0" : "0.0.0.0";
  1721. sub = ip;
  1722. gw = ip;
  1723. ip_len = ipv6_en ? 5 : 8;
  1724. }
  1725. switch (type) {
  1726. case ISCSI_BOOT_ETH_IP_ADDR:
  1727. rc = snprintf(buf, ip_len, fmt, ip);
  1728. break;
  1729. case ISCSI_BOOT_ETH_SUBNET_MASK:
  1730. rc = snprintf(buf, ip_len, fmt, sub);
  1731. break;
  1732. case ISCSI_BOOT_ETH_GATEWAY:
  1733. rc = snprintf(buf, ip_len, fmt, gw);
  1734. break;
  1735. case ISCSI_BOOT_ETH_FLAGS:
  1736. rc = snprintf(buf, 3, "%d\n", (char)SYSFS_FLAG_FW_SEL_BOOT);
  1737. break;
  1738. case ISCSI_BOOT_ETH_INDEX:
  1739. rc = snprintf(buf, 3, "0\n");
  1740. break;
  1741. case ISCSI_BOOT_ETH_MAC:
  1742. rc = sysfs_format_mac(buf, qedi->mac, ETH_ALEN);
  1743. break;
  1744. case ISCSI_BOOT_ETH_VLAN:
  1745. rc = snprintf(buf, 12, "%d\n",
  1746. GET_FIELD2(initiator->generic_cont0,
  1747. NVM_ISCSI_CFG_INITIATOR_VLAN));
  1748. break;
  1749. case ISCSI_BOOT_ETH_ORIGIN:
  1750. if (dhcp_en)
  1751. rc = snprintf(buf, 3, "3\n");
  1752. break;
  1753. default:
  1754. rc = 0;
  1755. break;
  1756. }
  1757. return rc;
  1758. }
  1759. static umode_t qedi_eth_get_attr_visibility(void *data, int type)
  1760. {
  1761. int rc = 1;
  1762. switch (type) {
  1763. case ISCSI_BOOT_ETH_FLAGS:
  1764. case ISCSI_BOOT_ETH_MAC:
  1765. case ISCSI_BOOT_ETH_INDEX:
  1766. case ISCSI_BOOT_ETH_IP_ADDR:
  1767. case ISCSI_BOOT_ETH_SUBNET_MASK:
  1768. case ISCSI_BOOT_ETH_GATEWAY:
  1769. case ISCSI_BOOT_ETH_ORIGIN:
  1770. case ISCSI_BOOT_ETH_VLAN:
  1771. rc = 0444;
  1772. break;
  1773. default:
  1774. rc = 0;
  1775. break;
  1776. }
  1777. return rc;
  1778. }
  1779. static ssize_t qedi_show_boot_ini_info(void *data, int type, char *buf)
  1780. {
  1781. struct qedi_ctx *qedi = data;
  1782. struct nvm_iscsi_initiator *initiator;
  1783. int rc;
  1784. struct nvm_iscsi_block *block;
  1785. block = qedi_get_nvram_block(qedi);
  1786. if (!block)
  1787. return 0;
  1788. initiator = &block->initiator;
  1789. switch (type) {
  1790. case ISCSI_BOOT_INI_INITIATOR_NAME:
  1791. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
  1792. initiator->initiator_name.byte);
  1793. break;
  1794. default:
  1795. rc = 0;
  1796. break;
  1797. }
  1798. return rc;
  1799. }
  1800. static umode_t qedi_ini_get_attr_visibility(void *data, int type)
  1801. {
  1802. int rc;
  1803. switch (type) {
  1804. case ISCSI_BOOT_INI_INITIATOR_NAME:
  1805. rc = 0444;
  1806. break;
  1807. default:
  1808. rc = 0;
  1809. break;
  1810. }
  1811. return rc;
  1812. }
  1813. static ssize_t
  1814. qedi_show_boot_tgt_info(struct qedi_ctx *qedi, int type,
  1815. char *buf, enum qedi_nvm_tgts idx)
  1816. {
  1817. int rc = 1;
  1818. u32 ctrl_flags, ipv6_en, chap_en, mchap_en, ip_len;
  1819. struct nvm_iscsi_block *block;
  1820. char *chap_name, *chap_secret;
  1821. char *mchap_name, *mchap_secret;
  1822. block = qedi_get_nvram_block(qedi);
  1823. if (!block)
  1824. goto exit_show_tgt_info;
  1825. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
  1826. "Port:%d, tgt_idx:%d\n",
  1827. GET_FIELD2(block->id, NVM_ISCSI_CFG_BLK_MAPPED_PF_ID), idx);
  1828. ctrl_flags = block->target[idx].ctrl_flags &
  1829. NVM_ISCSI_CFG_TARGET_ENABLED;
  1830. if (!ctrl_flags) {
  1831. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
  1832. "Target disabled\n");
  1833. goto exit_show_tgt_info;
  1834. }
  1835. ipv6_en = block->generic.ctrl_flags &
  1836. NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
  1837. ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
  1838. chap_en = block->generic.ctrl_flags &
  1839. NVM_ISCSI_CFG_GEN_CHAP_ENABLED;
  1840. chap_name = chap_en ? block->initiator.chap_name.byte : NULL;
  1841. chap_secret = chap_en ? block->initiator.chap_password.byte : NULL;
  1842. mchap_en = block->generic.ctrl_flags &
  1843. NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED;
  1844. mchap_name = mchap_en ? block->target[idx].chap_name.byte : NULL;
  1845. mchap_secret = mchap_en ? block->target[idx].chap_password.byte : NULL;
  1846. switch (type) {
  1847. case ISCSI_BOOT_TGT_NAME:
  1848. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
  1849. block->target[idx].target_name.byte);
  1850. break;
  1851. case ISCSI_BOOT_TGT_IP_ADDR:
  1852. if (ipv6_en)
  1853. rc = snprintf(buf, ip_len, "%pI6\n",
  1854. block->target[idx].ipv6_addr.byte);
  1855. else
  1856. rc = snprintf(buf, ip_len, "%pI4\n",
  1857. block->target[idx].ipv4_addr.byte);
  1858. break;
  1859. case ISCSI_BOOT_TGT_PORT:
  1860. rc = snprintf(buf, 12, "%d\n",
  1861. GET_FIELD2(block->target[idx].generic_cont0,
  1862. NVM_ISCSI_CFG_TARGET_TCP_PORT));
  1863. break;
  1864. case ISCSI_BOOT_TGT_LUN:
  1865. rc = snprintf(buf, 22, "%.*d\n",
  1866. block->target[idx].lun.value[1],
  1867. block->target[idx].lun.value[0]);
  1868. break;
  1869. case ISCSI_BOOT_TGT_CHAP_NAME:
  1870. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
  1871. chap_name);
  1872. break;
  1873. case ISCSI_BOOT_TGT_CHAP_SECRET:
  1874. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
  1875. chap_secret);
  1876. break;
  1877. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  1878. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
  1879. mchap_name);
  1880. break;
  1881. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  1882. rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_PWD_MAX_LEN,
  1883. mchap_secret);
  1884. break;
  1885. case ISCSI_BOOT_TGT_FLAGS:
  1886. rc = snprintf(buf, 3, "%d\n", (char)SYSFS_FLAG_FW_SEL_BOOT);
  1887. break;
  1888. case ISCSI_BOOT_TGT_NIC_ASSOC:
  1889. rc = snprintf(buf, 3, "0\n");
  1890. break;
  1891. default:
  1892. rc = 0;
  1893. break;
  1894. }
  1895. exit_show_tgt_info:
  1896. return rc;
  1897. }
  1898. static ssize_t qedi_show_boot_tgt_pri_info(void *data, int type, char *buf)
  1899. {
  1900. struct qedi_ctx *qedi = data;
  1901. return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_PRI);
  1902. }
  1903. static ssize_t qedi_show_boot_tgt_sec_info(void *data, int type, char *buf)
  1904. {
  1905. struct qedi_ctx *qedi = data;
  1906. return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_SEC);
  1907. }
  1908. static umode_t qedi_tgt_get_attr_visibility(void *data, int type)
  1909. {
  1910. int rc;
  1911. switch (type) {
  1912. case ISCSI_BOOT_TGT_NAME:
  1913. case ISCSI_BOOT_TGT_IP_ADDR:
  1914. case ISCSI_BOOT_TGT_PORT:
  1915. case ISCSI_BOOT_TGT_LUN:
  1916. case ISCSI_BOOT_TGT_CHAP_NAME:
  1917. case ISCSI_BOOT_TGT_CHAP_SECRET:
  1918. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  1919. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  1920. case ISCSI_BOOT_TGT_NIC_ASSOC:
  1921. case ISCSI_BOOT_TGT_FLAGS:
  1922. rc = 0444;
  1923. break;
  1924. default:
  1925. rc = 0;
  1926. break;
  1927. }
  1928. return rc;
  1929. }
  1930. static void qedi_boot_release(void *data)
  1931. {
  1932. struct qedi_ctx *qedi = data;
  1933. scsi_host_put(qedi->shost);
  1934. }
  1935. static int qedi_get_boot_info(struct qedi_ctx *qedi)
  1936. {
  1937. int ret = 1;
  1938. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  1939. "Get NVM iSCSI CFG image\n");
  1940. ret = qedi_ops->common->nvm_get_image(qedi->cdev,
  1941. QED_NVM_IMAGE_ISCSI_CFG,
  1942. (char *)qedi->iscsi_image,
  1943. sizeof(struct qedi_nvm_iscsi_image));
  1944. if (ret)
  1945. QEDI_ERR(&qedi->dbg_ctx,
  1946. "Could not get NVM image. ret = %d\n", ret);
  1947. return ret;
  1948. }
  1949. static int qedi_setup_boot_info(struct qedi_ctx *qedi)
  1950. {
  1951. struct iscsi_boot_kobj *boot_kobj;
  1952. if (qedi_get_boot_info(qedi))
  1953. return -EPERM;
  1954. qedi->boot_kset = iscsi_boot_create_host_kset(qedi->shost->host_no);
  1955. if (!qedi->boot_kset)
  1956. goto kset_free;
  1957. if (!scsi_host_get(qedi->shost))
  1958. goto kset_free;
  1959. boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 0, qedi,
  1960. qedi_show_boot_tgt_pri_info,
  1961. qedi_tgt_get_attr_visibility,
  1962. qedi_boot_release);
  1963. if (!boot_kobj)
  1964. goto put_host;
  1965. if (!scsi_host_get(qedi->shost))
  1966. goto kset_free;
  1967. boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 1, qedi,
  1968. qedi_show_boot_tgt_sec_info,
  1969. qedi_tgt_get_attr_visibility,
  1970. qedi_boot_release);
  1971. if (!boot_kobj)
  1972. goto put_host;
  1973. if (!scsi_host_get(qedi->shost))
  1974. goto kset_free;
  1975. boot_kobj = iscsi_boot_create_initiator(qedi->boot_kset, 0, qedi,
  1976. qedi_show_boot_ini_info,
  1977. qedi_ini_get_attr_visibility,
  1978. qedi_boot_release);
  1979. if (!boot_kobj)
  1980. goto put_host;
  1981. if (!scsi_host_get(qedi->shost))
  1982. goto kset_free;
  1983. boot_kobj = iscsi_boot_create_ethernet(qedi->boot_kset, 0, qedi,
  1984. qedi_show_boot_eth_info,
  1985. qedi_eth_get_attr_visibility,
  1986. qedi_boot_release);
  1987. if (!boot_kobj)
  1988. goto put_host;
  1989. return 0;
  1990. put_host:
  1991. scsi_host_put(qedi->shost);
  1992. kset_free:
  1993. iscsi_boot_destroy_kset(qedi->boot_kset);
  1994. return -ENOMEM;
  1995. }
  1996. static pci_ers_result_t qedi_io_error_detected(struct pci_dev *pdev,
  1997. pci_channel_state_t state)
  1998. {
  1999. struct qedi_ctx *qedi = pci_get_drvdata(pdev);
  2000. QEDI_ERR(&qedi->dbg_ctx, "%s: PCI error detected [%d]\n",
  2001. __func__, state);
  2002. if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
  2003. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  2004. "Recovery already in progress.\n");
  2005. return PCI_ERS_RESULT_NONE;
  2006. }
  2007. qedi_ops->common->recovery_process(qedi->cdev);
  2008. return PCI_ERS_RESULT_CAN_RECOVER;
  2009. }
  2010. static void __qedi_remove(struct pci_dev *pdev, int mode)
  2011. {
  2012. struct qedi_ctx *qedi = pci_get_drvdata(pdev);
  2013. int rval;
  2014. u16 retry = 10;
  2015. if (mode == QEDI_MODE_NORMAL)
  2016. iscsi_host_remove(qedi->shost, false);
  2017. else if (mode == QEDI_MODE_SHUTDOWN)
  2018. iscsi_host_remove(qedi->shost, true);
  2019. if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
  2020. if (qedi->tmf_thread) {
  2021. destroy_workqueue(qedi->tmf_thread);
  2022. qedi->tmf_thread = NULL;
  2023. }
  2024. if (qedi->offload_thread) {
  2025. destroy_workqueue(qedi->offload_thread);
  2026. qedi->offload_thread = NULL;
  2027. }
  2028. }
  2029. #ifdef CONFIG_DEBUG_FS
  2030. qedi_dbg_host_exit(&qedi->dbg_ctx);
  2031. #endif
  2032. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
  2033. qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
  2034. qedi_sync_free_irqs(qedi);
  2035. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
  2036. while (retry--) {
  2037. rval = qedi_ops->stop(qedi->cdev);
  2038. if (rval < 0)
  2039. msleep(1000);
  2040. else
  2041. break;
  2042. }
  2043. qedi_ops->ll2->stop(qedi->cdev);
  2044. }
  2045. cancel_delayed_work_sync(&qedi->recovery_work);
  2046. cancel_delayed_work_sync(&qedi->board_disable_work);
  2047. qedi_free_iscsi_pf_param(qedi);
  2048. rval = qedi_ops->common->update_drv_state(qedi->cdev, false);
  2049. if (rval)
  2050. QEDI_ERR(&qedi->dbg_ctx, "Failed to send drv state to MFW\n");
  2051. if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
  2052. qedi_ops->common->slowpath_stop(qedi->cdev);
  2053. qedi_ops->common->remove(qedi->cdev);
  2054. }
  2055. qedi_destroy_fp(qedi);
  2056. if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
  2057. qedi_release_cid_que(qedi);
  2058. qedi_cm_free_mem(qedi);
  2059. qedi_free_uio(qedi->udev);
  2060. qedi_free_itt(qedi);
  2061. if (qedi->ll2_recv_thread) {
  2062. kthread_stop(qedi->ll2_recv_thread);
  2063. qedi->ll2_recv_thread = NULL;
  2064. }
  2065. qedi_ll2_free_skbs(qedi);
  2066. if (qedi->boot_kset)
  2067. iscsi_boot_destroy_kset(qedi->boot_kset);
  2068. iscsi_host_free(qedi->shost);
  2069. }
  2070. }
  2071. static void qedi_board_disable_work(struct work_struct *work)
  2072. {
  2073. struct qedi_ctx *qedi =
  2074. container_of(work, struct qedi_ctx,
  2075. board_disable_work.work);
  2076. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  2077. "Fan failure, Unloading firmware context.\n");
  2078. if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
  2079. return;
  2080. __qedi_remove(qedi->pdev, QEDI_MODE_NORMAL);
  2081. }
  2082. static void qedi_shutdown(struct pci_dev *pdev)
  2083. {
  2084. struct qedi_ctx *qedi = pci_get_drvdata(pdev);
  2085. QEDI_ERR(&qedi->dbg_ctx, "%s: Shutdown qedi\n", __func__);
  2086. if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
  2087. return;
  2088. __qedi_remove(pdev, QEDI_MODE_SHUTDOWN);
  2089. }
  2090. static int qedi_suspend(struct pci_dev *pdev, pm_message_t state)
  2091. {
  2092. struct qedi_ctx *qedi;
  2093. if (!pdev) {
  2094. QEDI_ERR(NULL, "pdev is NULL.\n");
  2095. return -ENODEV;
  2096. }
  2097. qedi = pci_get_drvdata(pdev);
  2098. QEDI_ERR(&qedi->dbg_ctx, "%s: Device does not support suspend operation\n", __func__);
  2099. return -EPERM;
  2100. }
  2101. static int __qedi_probe(struct pci_dev *pdev, int mode)
  2102. {
  2103. struct qedi_ctx *qedi;
  2104. struct qed_ll2_params params;
  2105. u8 dp_level = 0;
  2106. bool is_vf = false;
  2107. char host_buf[16];
  2108. struct qed_link_params link_params;
  2109. struct qed_slowpath_params sp_params;
  2110. struct qed_probe_params qed_params;
  2111. void *task_start, *task_end;
  2112. int rc;
  2113. u16 retry = 10;
  2114. if (mode != QEDI_MODE_RECOVERY) {
  2115. qedi = qedi_host_alloc(pdev);
  2116. if (!qedi) {
  2117. rc = -ENOMEM;
  2118. goto exit_probe;
  2119. }
  2120. } else {
  2121. qedi = pci_get_drvdata(pdev);
  2122. }
  2123. retry_probe:
  2124. if (mode == QEDI_MODE_RECOVERY)
  2125. msleep(2000);
  2126. memset(&qed_params, 0, sizeof(qed_params));
  2127. qed_params.protocol = QED_PROTOCOL_ISCSI;
  2128. qed_params.dp_module = qedi_qed_debug;
  2129. qed_params.dp_level = dp_level;
  2130. qed_params.is_vf = is_vf;
  2131. qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
  2132. if (!qedi->cdev) {
  2133. if (mode == QEDI_MODE_RECOVERY && retry) {
  2134. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  2135. "Retry %d initialize hardware\n", retry);
  2136. retry--;
  2137. goto retry_probe;
  2138. }
  2139. rc = -ENODEV;
  2140. QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
  2141. goto free_host;
  2142. }
  2143. set_bit(QEDI_ERR_ATTN_CLR_EN, &qedi->qedi_err_flags);
  2144. set_bit(QEDI_ERR_IS_RECOVERABLE, &qedi->qedi_err_flags);
  2145. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  2146. rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
  2147. if (rc)
  2148. goto free_host;
  2149. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  2150. "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
  2151. qedi->dev_info.common.num_hwfns,
  2152. qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
  2153. rc = qedi_set_iscsi_pf_param(qedi);
  2154. if (rc) {
  2155. rc = -ENOMEM;
  2156. QEDI_ERR(&qedi->dbg_ctx,
  2157. "Set iSCSI pf param fail\n");
  2158. goto free_host;
  2159. }
  2160. qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
  2161. rc = qedi_prepare_fp(qedi);
  2162. if (rc) {
  2163. QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
  2164. goto free_pf_params;
  2165. }
  2166. /* Start the Slowpath-process */
  2167. memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
  2168. sp_params.int_mode = QED_INT_MODE_MSIX;
  2169. sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
  2170. sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
  2171. sp_params.drv_rev = QEDI_DRIVER_REV_VER;
  2172. sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
  2173. strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
  2174. rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
  2175. if (rc) {
  2176. QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
  2177. goto stop_hw;
  2178. }
  2179. /* update_pf_params needs to be called before and after slowpath
  2180. * start
  2181. */
  2182. qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
  2183. rc = qedi_setup_int(qedi);
  2184. if (rc)
  2185. goto stop_iscsi_func;
  2186. qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
  2187. /* Learn information crucial for qedi to progress */
  2188. rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
  2189. if (rc)
  2190. goto stop_iscsi_func;
  2191. /* Record BDQ producer doorbell addresses */
  2192. qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
  2193. qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
  2194. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  2195. "BDQ primary_prod=%p secondary_prod=%p.\n",
  2196. qedi->bdq_primary_prod,
  2197. qedi->bdq_secondary_prod);
  2198. /*
  2199. * We need to write the number of BDs in the BDQ we've preallocated so
  2200. * the f/w will do a prefetch and we'll get an unsolicited CQE when a
  2201. * packet arrives.
  2202. */
  2203. qedi->bdq_prod_idx = QEDI_BDQ_NUM;
  2204. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  2205. "Writing %d to primary and secondary BDQ doorbell registers.\n",
  2206. qedi->bdq_prod_idx);
  2207. writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
  2208. readw(qedi->bdq_primary_prod);
  2209. writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
  2210. readw(qedi->bdq_secondary_prod);
  2211. ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
  2212. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
  2213. qedi->mac);
  2214. snprintf(host_buf, sizeof(host_buf), "host_%d", qedi->shost->host_no);
  2215. qedi_ops->common->set_name(qedi->cdev, host_buf);
  2216. qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
  2217. memset(&params, 0, sizeof(params));
  2218. params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
  2219. qedi->ll2_mtu = DEF_PATH_MTU;
  2220. params.drop_ttl0_packets = 0;
  2221. params.rx_vlan_stripping = 1;
  2222. ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
  2223. if (mode != QEDI_MODE_RECOVERY) {
  2224. /* set up rx path */
  2225. INIT_LIST_HEAD(&qedi->ll2_skb_list);
  2226. spin_lock_init(&qedi->ll2_lock);
  2227. /* start qedi context */
  2228. spin_lock_init(&qedi->hba_lock);
  2229. spin_lock_init(&qedi->task_idx_lock);
  2230. mutex_init(&qedi->stats_lock);
  2231. }
  2232. qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
  2233. qedi_ops->ll2->start(qedi->cdev, &params);
  2234. if (mode != QEDI_MODE_RECOVERY) {
  2235. qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
  2236. (void *)qedi,
  2237. "qedi_ll2_thread");
  2238. }
  2239. rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
  2240. qedi, qedi_iscsi_event_cb);
  2241. if (rc) {
  2242. rc = -ENODEV;
  2243. QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
  2244. goto stop_slowpath;
  2245. }
  2246. task_start = qedi_get_task_mem(&qedi->tasks, 0);
  2247. task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
  2248. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
  2249. "Task context start=%p, end=%p block_size=%u.\n",
  2250. task_start, task_end, qedi->tasks.size);
  2251. memset(&link_params, 0, sizeof(link_params));
  2252. link_params.link_up = true;
  2253. rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
  2254. if (rc) {
  2255. QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
  2256. atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
  2257. }
  2258. #ifdef CONFIG_DEBUG_FS
  2259. qedi_dbg_host_init(&qedi->dbg_ctx, qedi_debugfs_ops,
  2260. qedi_dbg_fops);
  2261. #endif
  2262. QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  2263. "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
  2264. QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
  2265. FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
  2266. if (mode == QEDI_MODE_NORMAL) {
  2267. if (iscsi_host_add(qedi->shost, &pdev->dev)) {
  2268. QEDI_ERR(&qedi->dbg_ctx,
  2269. "Could not add iscsi host\n");
  2270. rc = -ENOMEM;
  2271. goto remove_host;
  2272. }
  2273. /* Allocate uio buffers */
  2274. rc = qedi_alloc_uio_rings(qedi);
  2275. if (rc) {
  2276. QEDI_ERR(&qedi->dbg_ctx,
  2277. "UIO alloc ring failed err=%d\n", rc);
  2278. goto remove_host;
  2279. }
  2280. rc = qedi_init_uio(qedi);
  2281. if (rc) {
  2282. QEDI_ERR(&qedi->dbg_ctx,
  2283. "UIO init failed, err=%d\n", rc);
  2284. goto free_uio;
  2285. }
  2286. /* host the array on iscsi_conn */
  2287. rc = qedi_setup_cid_que(qedi);
  2288. if (rc) {
  2289. QEDI_ERR(&qedi->dbg_ctx,
  2290. "Could not setup cid que\n");
  2291. goto free_uio;
  2292. }
  2293. rc = qedi_cm_alloc_mem(qedi);
  2294. if (rc) {
  2295. QEDI_ERR(&qedi->dbg_ctx,
  2296. "Could not alloc cm memory\n");
  2297. goto free_cid_que;
  2298. }
  2299. rc = qedi_alloc_itt(qedi);
  2300. if (rc) {
  2301. QEDI_ERR(&qedi->dbg_ctx,
  2302. "Could not alloc itt memory\n");
  2303. goto free_cid_que;
  2304. }
  2305. sprintf(host_buf, "host_%d", qedi->shost->host_no);
  2306. qedi->tmf_thread = create_singlethread_workqueue(host_buf);
  2307. if (!qedi->tmf_thread) {
  2308. QEDI_ERR(&qedi->dbg_ctx,
  2309. "Unable to start tmf thread!\n");
  2310. rc = -ENODEV;
  2311. goto free_cid_que;
  2312. }
  2313. sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
  2314. qedi->offload_thread = create_workqueue(host_buf);
  2315. if (!qedi->offload_thread) {
  2316. QEDI_ERR(&qedi->dbg_ctx,
  2317. "Unable to start offload thread!\n");
  2318. rc = -ENODEV;
  2319. goto free_tmf_thread;
  2320. }
  2321. INIT_DELAYED_WORK(&qedi->recovery_work, qedi_recovery_handler);
  2322. INIT_DELAYED_WORK(&qedi->board_disable_work,
  2323. qedi_board_disable_work);
  2324. /* F/w needs 1st task context memory entry for performance */
  2325. set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
  2326. atomic_set(&qedi->num_offloads, 0);
  2327. if (qedi_setup_boot_info(qedi))
  2328. QEDI_ERR(&qedi->dbg_ctx,
  2329. "No iSCSI boot target configured\n");
  2330. rc = qedi_ops->common->update_drv_state(qedi->cdev, true);
  2331. if (rc)
  2332. QEDI_ERR(&qedi->dbg_ctx,
  2333. "Failed to send drv state to MFW\n");
  2334. }
  2335. return 0;
  2336. free_tmf_thread:
  2337. destroy_workqueue(qedi->tmf_thread);
  2338. free_cid_que:
  2339. qedi_release_cid_que(qedi);
  2340. free_uio:
  2341. qedi_free_uio(qedi->udev);
  2342. remove_host:
  2343. #ifdef CONFIG_DEBUG_FS
  2344. qedi_dbg_host_exit(&qedi->dbg_ctx);
  2345. #endif
  2346. iscsi_host_remove(qedi->shost, false);
  2347. stop_iscsi_func:
  2348. qedi_ops->stop(qedi->cdev);
  2349. stop_slowpath:
  2350. qedi_ops->common->slowpath_stop(qedi->cdev);
  2351. stop_hw:
  2352. qedi_ops->common->remove(qedi->cdev);
  2353. free_pf_params:
  2354. qedi_free_iscsi_pf_param(qedi);
  2355. free_host:
  2356. iscsi_host_free(qedi->shost);
  2357. exit_probe:
  2358. return rc;
  2359. }
  2360. static void qedi_mark_conn_recovery(struct iscsi_cls_session *cls_session)
  2361. {
  2362. struct iscsi_session *session = cls_session->dd_data;
  2363. struct iscsi_conn *conn = session->leadconn;
  2364. struct qedi_conn *qedi_conn = conn->dd_data;
  2365. iscsi_conn_failure(qedi_conn->cls_conn->dd_data, ISCSI_ERR_CONN_FAILED);
  2366. }
  2367. static void qedi_recovery_handler(struct work_struct *work)
  2368. {
  2369. struct qedi_ctx *qedi =
  2370. container_of(work, struct qedi_ctx, recovery_work.work);
  2371. iscsi_host_for_each_session(qedi->shost, qedi_mark_conn_recovery);
  2372. /* Call common_ops->recovery_prolog to allow the MFW to quiesce
  2373. * any PCI transactions.
  2374. */
  2375. qedi_ops->common->recovery_prolog(qedi->cdev);
  2376. __qedi_remove(qedi->pdev, QEDI_MODE_RECOVERY);
  2377. __qedi_probe(qedi->pdev, QEDI_MODE_RECOVERY);
  2378. clear_bit(QEDI_IN_RECOVERY, &qedi->flags);
  2379. }
  2380. static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2381. {
  2382. return __qedi_probe(pdev, QEDI_MODE_NORMAL);
  2383. }
  2384. static void qedi_remove(struct pci_dev *pdev)
  2385. {
  2386. __qedi_remove(pdev, QEDI_MODE_NORMAL);
  2387. }
  2388. static struct pci_device_id qedi_pci_tbl[] = {
  2389. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
  2390. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
  2391. { 0 },
  2392. };
  2393. MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
  2394. static enum cpuhp_state qedi_cpuhp_state;
  2395. static struct pci_error_handlers qedi_err_handler = {
  2396. .error_detected = qedi_io_error_detected,
  2397. };
  2398. static struct pci_driver qedi_pci_driver = {
  2399. .name = QEDI_MODULE_NAME,
  2400. .id_table = qedi_pci_tbl,
  2401. .probe = qedi_probe,
  2402. .remove = qedi_remove,
  2403. .shutdown = qedi_shutdown,
  2404. .err_handler = &qedi_err_handler,
  2405. .suspend = qedi_suspend,
  2406. };
  2407. static int __init qedi_init(void)
  2408. {
  2409. struct qedi_percpu_s *p;
  2410. int cpu, rc = 0;
  2411. qedi_ops = qed_get_iscsi_ops();
  2412. if (!qedi_ops) {
  2413. QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
  2414. return -EINVAL;
  2415. }
  2416. #ifdef CONFIG_DEBUG_FS
  2417. qedi_dbg_init("qedi");
  2418. #endif
  2419. qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
  2420. if (!qedi_scsi_transport) {
  2421. QEDI_ERR(NULL, "Could not register qedi transport");
  2422. rc = -ENOMEM;
  2423. goto exit_qedi_init_1;
  2424. }
  2425. for_each_possible_cpu(cpu) {
  2426. p = &per_cpu(qedi_percpu, cpu);
  2427. INIT_LIST_HEAD(&p->work_list);
  2428. spin_lock_init(&p->p_work_lock);
  2429. p->iothread = NULL;
  2430. }
  2431. rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
  2432. qedi_cpu_online, qedi_cpu_offline);
  2433. if (rc < 0)
  2434. goto exit_qedi_init_2;
  2435. qedi_cpuhp_state = rc;
  2436. rc = pci_register_driver(&qedi_pci_driver);
  2437. if (rc) {
  2438. QEDI_ERR(NULL, "Failed to register driver\n");
  2439. goto exit_qedi_hp;
  2440. }
  2441. return 0;
  2442. exit_qedi_hp:
  2443. cpuhp_remove_state(qedi_cpuhp_state);
  2444. exit_qedi_init_2:
  2445. iscsi_unregister_transport(&qedi_iscsi_transport);
  2446. exit_qedi_init_1:
  2447. #ifdef CONFIG_DEBUG_FS
  2448. qedi_dbg_exit();
  2449. #endif
  2450. qed_put_iscsi_ops();
  2451. return rc;
  2452. }
  2453. static void __exit qedi_cleanup(void)
  2454. {
  2455. pci_unregister_driver(&qedi_pci_driver);
  2456. cpuhp_remove_state(qedi_cpuhp_state);
  2457. iscsi_unregister_transport(&qedi_iscsi_transport);
  2458. #ifdef CONFIG_DEBUG_FS
  2459. qedi_dbg_exit();
  2460. #endif
  2461. qed_put_iscsi_ops();
  2462. }
  2463. MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
  2464. MODULE_LICENSE("GPL");
  2465. MODULE_AUTHOR("QLogic Corporation");
  2466. MODULE_VERSION(QEDI_MODULE_VERSION);
  2467. module_init(qedi_init);
  2468. module_exit(qedi_cleanup);