qedi_main.c 73 KB

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