be_main.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019
  1. /**
  2. * Copyright (C) 2005 - 2010 ServerEngines
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@serverengines.com
  14. *
  15. * ServerEngines
  16. * 209 N. Fair Oaks Ave
  17. * Sunnyvale, CA 94085
  18. *
  19. */
  20. #include <linux/reboot.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/pci.h>
  26. #include <linux/string.h>
  27. #include <linux/kernel.h>
  28. #include <linux/semaphore.h>
  29. #include <scsi/libiscsi.h>
  30. #include <scsi/scsi_transport_iscsi.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include "be_main.h"
  37. #include "be_iscsi.h"
  38. #include "be_mgmt.h"
  39. static unsigned int be_iopoll_budget = 10;
  40. static unsigned int be_max_phys_size = 64;
  41. static unsigned int enable_msix = 1;
  42. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  43. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  44. MODULE_AUTHOR("ServerEngines Corporation");
  45. MODULE_LICENSE("GPL");
  46. module_param(be_iopoll_budget, int, 0);
  47. module_param(enable_msix, int, 0);
  48. module_param(be_max_phys_size, uint, S_IRUGO);
  49. MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
  50. "contiguous memory that can be allocated."
  51. "Range is 16 - 128");
  52. static int beiscsi_slave_configure(struct scsi_device *sdev)
  53. {
  54. blk_queue_max_segment_size(sdev->request_queue, 65536);
  55. return 0;
  56. }
  57. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  58. {
  59. struct iscsi_cls_session *cls_session;
  60. struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
  61. struct beiscsi_io_task *aborted_io_task;
  62. struct iscsi_conn *conn;
  63. struct beiscsi_conn *beiscsi_conn;
  64. struct beiscsi_hba *phba;
  65. struct iscsi_session *session;
  66. struct invalidate_command_table *inv_tbl;
  67. unsigned int cid, tag, num_invalidate;
  68. cls_session = starget_to_session(scsi_target(sc->device));
  69. session = cls_session->dd_data;
  70. spin_lock_bh(&session->lock);
  71. if (!aborted_task || !aborted_task->sc) {
  72. /* we raced */
  73. spin_unlock_bh(&session->lock);
  74. return SUCCESS;
  75. }
  76. aborted_io_task = aborted_task->dd_data;
  77. if (!aborted_io_task->scsi_cmnd) {
  78. /* raced or invalid command */
  79. spin_unlock_bh(&session->lock);
  80. return SUCCESS;
  81. }
  82. spin_unlock_bh(&session->lock);
  83. conn = aborted_task->conn;
  84. beiscsi_conn = conn->dd_data;
  85. phba = beiscsi_conn->phba;
  86. /* invalidate iocb */
  87. cid = beiscsi_conn->beiscsi_conn_cid;
  88. inv_tbl = phba->inv_tbl;
  89. memset(inv_tbl, 0x0, sizeof(*inv_tbl));
  90. inv_tbl->cid = cid;
  91. inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
  92. num_invalidate = 1;
  93. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate, cid);
  94. if (!tag) {
  95. shost_printk(KERN_WARNING, phba->shost,
  96. "mgmt_invalidate_icds could not be"
  97. " submitted\n");
  98. return FAILED;
  99. } else {
  100. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  101. phba->ctrl.mcc_numtag[tag]);
  102. free_mcc_tag(&phba->ctrl, tag);
  103. }
  104. return iscsi_eh_abort(sc);
  105. }
  106. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  107. {
  108. struct iscsi_task *abrt_task;
  109. struct beiscsi_io_task *abrt_io_task;
  110. struct iscsi_conn *conn;
  111. struct beiscsi_conn *beiscsi_conn;
  112. struct beiscsi_hba *phba;
  113. struct iscsi_session *session;
  114. struct iscsi_cls_session *cls_session;
  115. struct invalidate_command_table *inv_tbl;
  116. unsigned int cid, tag, i, num_invalidate;
  117. int rc = FAILED;
  118. /* invalidate iocbs */
  119. cls_session = starget_to_session(scsi_target(sc->device));
  120. session = cls_session->dd_data;
  121. spin_lock_bh(&session->lock);
  122. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  123. goto unlock;
  124. conn = session->leadconn;
  125. beiscsi_conn = conn->dd_data;
  126. phba = beiscsi_conn->phba;
  127. cid = beiscsi_conn->beiscsi_conn_cid;
  128. inv_tbl = phba->inv_tbl;
  129. memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
  130. num_invalidate = 0;
  131. for (i = 0; i < conn->session->cmds_max; i++) {
  132. abrt_task = conn->session->cmds[i];
  133. abrt_io_task = abrt_task->dd_data;
  134. if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
  135. continue;
  136. if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
  137. continue;
  138. inv_tbl->cid = cid;
  139. inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
  140. num_invalidate++;
  141. inv_tbl++;
  142. }
  143. spin_unlock_bh(&session->lock);
  144. inv_tbl = phba->inv_tbl;
  145. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate, cid);
  146. if (!tag) {
  147. shost_printk(KERN_WARNING, phba->shost,
  148. "mgmt_invalidate_icds could not be"
  149. " submitted\n");
  150. return FAILED;
  151. } else {
  152. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  153. phba->ctrl.mcc_numtag[tag]);
  154. free_mcc_tag(&phba->ctrl, tag);
  155. }
  156. return iscsi_eh_device_reset(sc);
  157. unlock:
  158. spin_unlock_bh(&session->lock);
  159. return rc;
  160. }
  161. /*------------------- PCI Driver operations and data ----------------- */
  162. static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
  163. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  164. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  165. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  166. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  167. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  168. { 0 }
  169. };
  170. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  171. static struct scsi_host_template beiscsi_sht = {
  172. .module = THIS_MODULE,
  173. .name = "ServerEngines 10Gbe open-iscsi Initiator Driver",
  174. .proc_name = DRV_NAME,
  175. .queuecommand = iscsi_queuecommand,
  176. .change_queue_depth = iscsi_change_queue_depth,
  177. .slave_configure = beiscsi_slave_configure,
  178. .target_alloc = iscsi_target_alloc,
  179. .eh_abort_handler = beiscsi_eh_abort,
  180. .eh_device_reset_handler = beiscsi_eh_device_reset,
  181. .eh_target_reset_handler = iscsi_eh_session_reset,
  182. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  183. .can_queue = BE2_IO_DEPTH,
  184. .this_id = -1,
  185. .max_sectors = BEISCSI_MAX_SECTORS,
  186. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  187. .use_clustering = ENABLE_CLUSTERING,
  188. };
  189. static struct scsi_transport_template *beiscsi_scsi_transport;
  190. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  191. {
  192. struct beiscsi_hba *phba;
  193. struct Scsi_Host *shost;
  194. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  195. if (!shost) {
  196. dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
  197. "iscsi_host_alloc failed\n");
  198. return NULL;
  199. }
  200. shost->dma_boundary = pcidev->dma_mask;
  201. shost->max_id = BE2_MAX_SESSIONS;
  202. shost->max_channel = 0;
  203. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  204. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  205. shost->transportt = beiscsi_scsi_transport;
  206. phba = iscsi_host_priv(shost);
  207. memset(phba, 0, sizeof(*phba));
  208. phba->shost = shost;
  209. phba->pcidev = pci_dev_get(pcidev);
  210. pci_set_drvdata(pcidev, phba);
  211. if (iscsi_host_add(shost, &phba->pcidev->dev))
  212. goto free_devices;
  213. return phba;
  214. free_devices:
  215. pci_dev_put(phba->pcidev);
  216. iscsi_host_free(phba->shost);
  217. return NULL;
  218. }
  219. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  220. {
  221. if (phba->csr_va) {
  222. iounmap(phba->csr_va);
  223. phba->csr_va = NULL;
  224. }
  225. if (phba->db_va) {
  226. iounmap(phba->db_va);
  227. phba->db_va = NULL;
  228. }
  229. if (phba->pci_va) {
  230. iounmap(phba->pci_va);
  231. phba->pci_va = NULL;
  232. }
  233. }
  234. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  235. struct pci_dev *pcidev)
  236. {
  237. u8 __iomem *addr;
  238. int pcicfg_reg;
  239. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  240. pci_resource_len(pcidev, 2));
  241. if (addr == NULL)
  242. return -ENOMEM;
  243. phba->ctrl.csr = addr;
  244. phba->csr_va = addr;
  245. phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
  246. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  247. if (addr == NULL)
  248. goto pci_map_err;
  249. phba->ctrl.db = addr;
  250. phba->db_va = addr;
  251. phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
  252. if (phba->generation == BE_GEN2)
  253. pcicfg_reg = 1;
  254. else
  255. pcicfg_reg = 0;
  256. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  257. pci_resource_len(pcidev, pcicfg_reg));
  258. if (addr == NULL)
  259. goto pci_map_err;
  260. phba->ctrl.pcicfg = addr;
  261. phba->pci_va = addr;
  262. phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
  263. return 0;
  264. pci_map_err:
  265. beiscsi_unmap_pci_function(phba);
  266. return -ENOMEM;
  267. }
  268. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  269. {
  270. int ret;
  271. ret = pci_enable_device(pcidev);
  272. if (ret) {
  273. dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
  274. "failed. Returning -ENODEV\n");
  275. return ret;
  276. }
  277. pci_set_master(pcidev);
  278. if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
  279. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
  280. if (ret) {
  281. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  282. pci_disable_device(pcidev);
  283. return ret;
  284. }
  285. }
  286. return 0;
  287. }
  288. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  289. {
  290. struct be_ctrl_info *ctrl = &phba->ctrl;
  291. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  292. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  293. int status = 0;
  294. ctrl->pdev = pdev;
  295. status = beiscsi_map_pci_bars(phba, pdev);
  296. if (status)
  297. return status;
  298. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  299. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  300. mbox_mem_alloc->size,
  301. &mbox_mem_alloc->dma);
  302. if (!mbox_mem_alloc->va) {
  303. beiscsi_unmap_pci_function(phba);
  304. status = -ENOMEM;
  305. return status;
  306. }
  307. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  308. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  309. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  310. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  311. spin_lock_init(&ctrl->mbox_lock);
  312. spin_lock_init(&phba->ctrl.mcc_lock);
  313. spin_lock_init(&phba->ctrl.mcc_cq_lock);
  314. return status;
  315. }
  316. static void beiscsi_get_params(struct beiscsi_hba *phba)
  317. {
  318. phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
  319. - (phba->fw_config.iscsi_cid_count
  320. + BE2_TMFS
  321. + BE2_NOPOUT_REQ));
  322. phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
  323. phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
  324. phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;;
  325. phba->params.num_sge_per_io = BE2_SGE;
  326. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  327. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  328. phba->params.eq_timer = 64;
  329. phba->params.num_eq_entries =
  330. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  331. + BE2_TMFS) / 512) + 1) * 512;
  332. phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
  333. ? 1024 : phba->params.num_eq_entries;
  334. SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
  335. phba->params.num_eq_entries);
  336. phba->params.num_cq_entries =
  337. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  338. + BE2_TMFS) / 512) + 1) * 512;
  339. phba->params.wrbs_per_cxn = 256;
  340. }
  341. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  342. unsigned int id, unsigned int clr_interrupt,
  343. unsigned int num_processed,
  344. unsigned char rearm, unsigned char event)
  345. {
  346. u32 val = 0;
  347. val |= id & DB_EQ_RING_ID_MASK;
  348. if (rearm)
  349. val |= 1 << DB_EQ_REARM_SHIFT;
  350. if (clr_interrupt)
  351. val |= 1 << DB_EQ_CLR_SHIFT;
  352. if (event)
  353. val |= 1 << DB_EQ_EVNT_SHIFT;
  354. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  355. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  356. }
  357. /**
  358. * be_isr_mcc - The isr routine of the driver.
  359. * @irq: Not used
  360. * @dev_id: Pointer to host adapter structure
  361. */
  362. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  363. {
  364. struct beiscsi_hba *phba;
  365. struct be_eq_entry *eqe = NULL;
  366. struct be_queue_info *eq;
  367. struct be_queue_info *mcc;
  368. unsigned int num_eq_processed;
  369. struct be_eq_obj *pbe_eq;
  370. unsigned long flags;
  371. pbe_eq = dev_id;
  372. eq = &pbe_eq->q;
  373. phba = pbe_eq->phba;
  374. mcc = &phba->ctrl.mcc_obj.cq;
  375. eqe = queue_tail_node(eq);
  376. if (!eqe)
  377. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  378. num_eq_processed = 0;
  379. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  380. & EQE_VALID_MASK) {
  381. if (((eqe->dw[offsetof(struct amap_eq_entry,
  382. resource_id) / 32] &
  383. EQE_RESID_MASK) >> 16) == mcc->id) {
  384. spin_lock_irqsave(&phba->isr_lock, flags);
  385. phba->todo_mcc_cq = 1;
  386. spin_unlock_irqrestore(&phba->isr_lock, flags);
  387. }
  388. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  389. queue_tail_inc(eq);
  390. eqe = queue_tail_node(eq);
  391. num_eq_processed++;
  392. }
  393. if (phba->todo_mcc_cq)
  394. queue_work(phba->wq, &phba->work_cqs);
  395. if (num_eq_processed)
  396. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  397. return IRQ_HANDLED;
  398. }
  399. /**
  400. * be_isr_msix - The isr routine of the driver.
  401. * @irq: Not used
  402. * @dev_id: Pointer to host adapter structure
  403. */
  404. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  405. {
  406. struct beiscsi_hba *phba;
  407. struct be_eq_entry *eqe = NULL;
  408. struct be_queue_info *eq;
  409. struct be_queue_info *cq;
  410. unsigned int num_eq_processed;
  411. struct be_eq_obj *pbe_eq;
  412. unsigned long flags;
  413. pbe_eq = dev_id;
  414. eq = &pbe_eq->q;
  415. cq = pbe_eq->cq;
  416. eqe = queue_tail_node(eq);
  417. if (!eqe)
  418. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  419. phba = pbe_eq->phba;
  420. num_eq_processed = 0;
  421. if (blk_iopoll_enabled) {
  422. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  423. & EQE_VALID_MASK) {
  424. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  425. blk_iopoll_sched(&pbe_eq->iopoll);
  426. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  427. queue_tail_inc(eq);
  428. eqe = queue_tail_node(eq);
  429. num_eq_processed++;
  430. }
  431. if (num_eq_processed)
  432. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
  433. return IRQ_HANDLED;
  434. } else {
  435. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  436. & EQE_VALID_MASK) {
  437. spin_lock_irqsave(&phba->isr_lock, flags);
  438. phba->todo_cq = 1;
  439. spin_unlock_irqrestore(&phba->isr_lock, flags);
  440. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  441. queue_tail_inc(eq);
  442. eqe = queue_tail_node(eq);
  443. num_eq_processed++;
  444. }
  445. if (phba->todo_cq)
  446. queue_work(phba->wq, &phba->work_cqs);
  447. if (num_eq_processed)
  448. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  449. return IRQ_HANDLED;
  450. }
  451. }
  452. /**
  453. * be_isr - The isr routine of the driver.
  454. * @irq: Not used
  455. * @dev_id: Pointer to host adapter structure
  456. */
  457. static irqreturn_t be_isr(int irq, void *dev_id)
  458. {
  459. struct beiscsi_hba *phba;
  460. struct hwi_controller *phwi_ctrlr;
  461. struct hwi_context_memory *phwi_context;
  462. struct be_eq_entry *eqe = NULL;
  463. struct be_queue_info *eq;
  464. struct be_queue_info *cq;
  465. struct be_queue_info *mcc;
  466. unsigned long flags, index;
  467. unsigned int num_mcceq_processed, num_ioeq_processed;
  468. struct be_ctrl_info *ctrl;
  469. struct be_eq_obj *pbe_eq;
  470. int isr;
  471. phba = dev_id;
  472. ctrl = &phba->ctrl;;
  473. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  474. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  475. if (!isr)
  476. return IRQ_NONE;
  477. phwi_ctrlr = phba->phwi_ctrlr;
  478. phwi_context = phwi_ctrlr->phwi_ctxt;
  479. pbe_eq = &phwi_context->be_eq[0];
  480. eq = &phwi_context->be_eq[0].q;
  481. mcc = &phba->ctrl.mcc_obj.cq;
  482. index = 0;
  483. eqe = queue_tail_node(eq);
  484. if (!eqe)
  485. SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
  486. num_ioeq_processed = 0;
  487. num_mcceq_processed = 0;
  488. if (blk_iopoll_enabled) {
  489. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  490. & EQE_VALID_MASK) {
  491. if (((eqe->dw[offsetof(struct amap_eq_entry,
  492. resource_id) / 32] &
  493. EQE_RESID_MASK) >> 16) == mcc->id) {
  494. spin_lock_irqsave(&phba->isr_lock, flags);
  495. phba->todo_mcc_cq = 1;
  496. spin_unlock_irqrestore(&phba->isr_lock, flags);
  497. num_mcceq_processed++;
  498. } else {
  499. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  500. blk_iopoll_sched(&pbe_eq->iopoll);
  501. num_ioeq_processed++;
  502. }
  503. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  504. queue_tail_inc(eq);
  505. eqe = queue_tail_node(eq);
  506. }
  507. if (num_ioeq_processed || num_mcceq_processed) {
  508. if (phba->todo_mcc_cq)
  509. queue_work(phba->wq, &phba->work_cqs);
  510. if ((num_mcceq_processed) && (!num_ioeq_processed))
  511. hwi_ring_eq_db(phba, eq->id, 0,
  512. (num_ioeq_processed +
  513. num_mcceq_processed) , 1, 1);
  514. else
  515. hwi_ring_eq_db(phba, eq->id, 0,
  516. (num_ioeq_processed +
  517. num_mcceq_processed), 0, 1);
  518. return IRQ_HANDLED;
  519. } else
  520. return IRQ_NONE;
  521. } else {
  522. cq = &phwi_context->be_cq[0];
  523. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  524. & EQE_VALID_MASK) {
  525. if (((eqe->dw[offsetof(struct amap_eq_entry,
  526. resource_id) / 32] &
  527. EQE_RESID_MASK) >> 16) != cq->id) {
  528. spin_lock_irqsave(&phba->isr_lock, flags);
  529. phba->todo_mcc_cq = 1;
  530. spin_unlock_irqrestore(&phba->isr_lock, flags);
  531. } else {
  532. spin_lock_irqsave(&phba->isr_lock, flags);
  533. phba->todo_cq = 1;
  534. spin_unlock_irqrestore(&phba->isr_lock, flags);
  535. }
  536. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  537. queue_tail_inc(eq);
  538. eqe = queue_tail_node(eq);
  539. num_ioeq_processed++;
  540. }
  541. if (phba->todo_cq || phba->todo_mcc_cq)
  542. queue_work(phba->wq, &phba->work_cqs);
  543. if (num_ioeq_processed) {
  544. hwi_ring_eq_db(phba, eq->id, 0,
  545. num_ioeq_processed, 1, 1);
  546. return IRQ_HANDLED;
  547. } else
  548. return IRQ_NONE;
  549. }
  550. }
  551. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  552. {
  553. struct pci_dev *pcidev = phba->pcidev;
  554. struct hwi_controller *phwi_ctrlr;
  555. struct hwi_context_memory *phwi_context;
  556. int ret, msix_vec, i = 0;
  557. char desc[32];
  558. phwi_ctrlr = phba->phwi_ctrlr;
  559. phwi_context = phwi_ctrlr->phwi_ctxt;
  560. if (phba->msix_enabled) {
  561. for (i = 0; i < phba->num_cpus; i++) {
  562. sprintf(desc, "beiscsi_msix_%04x", i);
  563. msix_vec = phba->msix_entries[i].vector;
  564. ret = request_irq(msix_vec, be_isr_msix, 0, desc,
  565. &phwi_context->be_eq[i]);
  566. }
  567. msix_vec = phba->msix_entries[i].vector;
  568. ret = request_irq(msix_vec, be_isr_mcc, 0, "beiscsi_msix_mcc",
  569. &phwi_context->be_eq[i]);
  570. } else {
  571. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  572. "beiscsi", phba);
  573. if (ret) {
  574. shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
  575. "Failed to register irq\\n");
  576. return ret;
  577. }
  578. }
  579. return 0;
  580. }
  581. static void hwi_ring_cq_db(struct beiscsi_hba *phba,
  582. unsigned int id, unsigned int num_processed,
  583. unsigned char rearm, unsigned char event)
  584. {
  585. u32 val = 0;
  586. val |= id & DB_CQ_RING_ID_MASK;
  587. if (rearm)
  588. val |= 1 << DB_CQ_REARM_SHIFT;
  589. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  590. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  591. }
  592. static unsigned int
  593. beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
  594. struct beiscsi_hba *phba,
  595. unsigned short cid,
  596. struct pdu_base *ppdu,
  597. unsigned long pdu_len,
  598. void *pbuffer, unsigned long buf_len)
  599. {
  600. struct iscsi_conn *conn = beiscsi_conn->conn;
  601. struct iscsi_session *session = conn->session;
  602. struct iscsi_task *task;
  603. struct beiscsi_io_task *io_task;
  604. struct iscsi_hdr *login_hdr;
  605. switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
  606. PDUBASE_OPCODE_MASK) {
  607. case ISCSI_OP_NOOP_IN:
  608. pbuffer = NULL;
  609. buf_len = 0;
  610. break;
  611. case ISCSI_OP_ASYNC_EVENT:
  612. break;
  613. case ISCSI_OP_REJECT:
  614. WARN_ON(!pbuffer);
  615. WARN_ON(!(buf_len == 48));
  616. SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
  617. break;
  618. case ISCSI_OP_LOGIN_RSP:
  619. case ISCSI_OP_TEXT_RSP:
  620. task = conn->login_task;
  621. io_task = task->dd_data;
  622. login_hdr = (struct iscsi_hdr *)ppdu;
  623. login_hdr->itt = io_task->libiscsi_itt;
  624. break;
  625. default:
  626. shost_printk(KERN_WARNING, phba->shost,
  627. "Unrecognized opcode 0x%x in async msg\n",
  628. (ppdu->
  629. dw[offsetof(struct amap_pdu_base, opcode) / 32]
  630. & PDUBASE_OPCODE_MASK));
  631. return 1;
  632. }
  633. spin_lock_bh(&session->lock);
  634. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
  635. spin_unlock_bh(&session->lock);
  636. return 0;
  637. }
  638. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  639. {
  640. struct sgl_handle *psgl_handle;
  641. if (phba->io_sgl_hndl_avbl) {
  642. SE_DEBUG(DBG_LVL_8,
  643. "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
  644. phba->io_sgl_alloc_index);
  645. psgl_handle = phba->io_sgl_hndl_base[phba->
  646. io_sgl_alloc_index];
  647. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  648. phba->io_sgl_hndl_avbl--;
  649. if (phba->io_sgl_alloc_index == (phba->params.
  650. ios_per_ctrl - 1))
  651. phba->io_sgl_alloc_index = 0;
  652. else
  653. phba->io_sgl_alloc_index++;
  654. } else
  655. psgl_handle = NULL;
  656. return psgl_handle;
  657. }
  658. static void
  659. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  660. {
  661. SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
  662. phba->io_sgl_free_index);
  663. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  664. /*
  665. * this can happen if clean_task is called on a task that
  666. * failed in xmit_task or alloc_pdu.
  667. */
  668. SE_DEBUG(DBG_LVL_8,
  669. "Double Free in IO SGL io_sgl_free_index=%d,"
  670. "value there=%p\n", phba->io_sgl_free_index,
  671. phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
  672. return;
  673. }
  674. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  675. phba->io_sgl_hndl_avbl++;
  676. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  677. phba->io_sgl_free_index = 0;
  678. else
  679. phba->io_sgl_free_index++;
  680. }
  681. /**
  682. * alloc_wrb_handle - To allocate a wrb handle
  683. * @phba: The hba pointer
  684. * @cid: The cid to use for allocation
  685. *
  686. * This happens under session_lock until submission to chip
  687. */
  688. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
  689. {
  690. struct hwi_wrb_context *pwrb_context;
  691. struct hwi_controller *phwi_ctrlr;
  692. struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
  693. phwi_ctrlr = phba->phwi_ctrlr;
  694. pwrb_context = &phwi_ctrlr->wrb_context[cid];
  695. if (pwrb_context->wrb_handles_available >= 2) {
  696. pwrb_handle = pwrb_context->pwrb_handle_base[
  697. pwrb_context->alloc_index];
  698. pwrb_context->wrb_handles_available--;
  699. if (pwrb_context->alloc_index ==
  700. (phba->params.wrbs_per_cxn - 1))
  701. pwrb_context->alloc_index = 0;
  702. else
  703. pwrb_context->alloc_index++;
  704. pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
  705. pwrb_context->alloc_index];
  706. pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
  707. } else
  708. pwrb_handle = NULL;
  709. return pwrb_handle;
  710. }
  711. /**
  712. * free_wrb_handle - To free the wrb handle back to pool
  713. * @phba: The hba pointer
  714. * @pwrb_context: The context to free from
  715. * @pwrb_handle: The wrb_handle to free
  716. *
  717. * This happens under session_lock until submission to chip
  718. */
  719. static void
  720. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  721. struct wrb_handle *pwrb_handle)
  722. {
  723. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  724. pwrb_context->wrb_handles_available++;
  725. if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
  726. pwrb_context->free_index = 0;
  727. else
  728. pwrb_context->free_index++;
  729. SE_DEBUG(DBG_LVL_8,
  730. "FREE WRB: pwrb_handle=%p free_index=0x%x"
  731. "wrb_handles_available=%d\n",
  732. pwrb_handle, pwrb_context->free_index,
  733. pwrb_context->wrb_handles_available);
  734. }
  735. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  736. {
  737. struct sgl_handle *psgl_handle;
  738. if (phba->eh_sgl_hndl_avbl) {
  739. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  740. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  741. SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
  742. phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
  743. phba->eh_sgl_hndl_avbl--;
  744. if (phba->eh_sgl_alloc_index ==
  745. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  746. 1))
  747. phba->eh_sgl_alloc_index = 0;
  748. else
  749. phba->eh_sgl_alloc_index++;
  750. } else
  751. psgl_handle = NULL;
  752. return psgl_handle;
  753. }
  754. void
  755. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  756. {
  757. SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
  758. phba->eh_sgl_free_index);
  759. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  760. /*
  761. * this can happen if clean_task is called on a task that
  762. * failed in xmit_task or alloc_pdu.
  763. */
  764. SE_DEBUG(DBG_LVL_8,
  765. "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
  766. phba->eh_sgl_free_index);
  767. return;
  768. }
  769. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  770. phba->eh_sgl_hndl_avbl++;
  771. if (phba->eh_sgl_free_index ==
  772. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  773. phba->eh_sgl_free_index = 0;
  774. else
  775. phba->eh_sgl_free_index++;
  776. }
  777. static void
  778. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  779. struct iscsi_task *task, struct sol_cqe *psol)
  780. {
  781. struct beiscsi_io_task *io_task = task->dd_data;
  782. struct be_status_bhs *sts_bhs =
  783. (struct be_status_bhs *)io_task->cmd_bhs;
  784. struct iscsi_conn *conn = beiscsi_conn->conn;
  785. unsigned int sense_len;
  786. unsigned char *sense;
  787. u32 resid = 0, exp_cmdsn, max_cmdsn;
  788. u8 rsp, status, flags;
  789. exp_cmdsn = (psol->
  790. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  791. & SOL_EXP_CMD_SN_MASK);
  792. max_cmdsn = ((psol->
  793. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  794. & SOL_EXP_CMD_SN_MASK) +
  795. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  796. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  797. rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
  798. & SOL_RESP_MASK) >> 16);
  799. status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
  800. & SOL_STS_MASK) >> 8);
  801. flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  802. & SOL_FLAGS_MASK) >> 24) | 0x80;
  803. task->sc->result = (DID_OK << 16) | status;
  804. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  805. task->sc->result = DID_ERROR << 16;
  806. goto unmap;
  807. }
  808. /* bidi not initially supported */
  809. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  810. resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
  811. 32] & SOL_RES_CNT_MASK);
  812. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  813. task->sc->result = DID_ERROR << 16;
  814. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  815. scsi_set_resid(task->sc, resid);
  816. if (!status && (scsi_bufflen(task->sc) - resid <
  817. task->sc->underflow))
  818. task->sc->result = DID_ERROR << 16;
  819. }
  820. }
  821. if (status == SAM_STAT_CHECK_CONDITION) {
  822. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  823. sense = sts_bhs->sense_info + sizeof(unsigned short);
  824. sense_len = cpu_to_be16(*slen);
  825. memcpy(task->sc->sense_buffer, sense,
  826. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  827. }
  828. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
  829. if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  830. & SOL_RES_CNT_MASK)
  831. conn->rxdata_octets += (psol->
  832. dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  833. & SOL_RES_CNT_MASK);
  834. }
  835. unmap:
  836. scsi_dma_unmap(io_task->scsi_cmnd);
  837. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  838. }
  839. static void
  840. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  841. struct iscsi_task *task, struct sol_cqe *psol)
  842. {
  843. struct iscsi_logout_rsp *hdr;
  844. struct beiscsi_io_task *io_task = task->dd_data;
  845. struct iscsi_conn *conn = beiscsi_conn->conn;
  846. hdr = (struct iscsi_logout_rsp *)task->hdr;
  847. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  848. hdr->t2wait = 5;
  849. hdr->t2retain = 0;
  850. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  851. & SOL_FLAGS_MASK) >> 24) | 0x80;
  852. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  853. 32] & SOL_RESP_MASK);
  854. hdr->exp_cmdsn = cpu_to_be32(psol->
  855. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  856. & SOL_EXP_CMD_SN_MASK);
  857. hdr->max_cmdsn = be32_to_cpu((psol->
  858. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  859. & SOL_EXP_CMD_SN_MASK) +
  860. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  861. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  862. hdr->dlength[0] = 0;
  863. hdr->dlength[1] = 0;
  864. hdr->dlength[2] = 0;
  865. hdr->hlength = 0;
  866. hdr->itt = io_task->libiscsi_itt;
  867. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  868. }
  869. static void
  870. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  871. struct iscsi_task *task, struct sol_cqe *psol)
  872. {
  873. struct iscsi_tm_rsp *hdr;
  874. struct iscsi_conn *conn = beiscsi_conn->conn;
  875. struct beiscsi_io_task *io_task = task->dd_data;
  876. hdr = (struct iscsi_tm_rsp *)task->hdr;
  877. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  878. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  879. & SOL_FLAGS_MASK) >> 24) | 0x80;
  880. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  881. 32] & SOL_RESP_MASK);
  882. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  883. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  884. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  885. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  886. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  887. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  888. hdr->itt = io_task->libiscsi_itt;
  889. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  890. }
  891. static void
  892. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  893. struct beiscsi_hba *phba, struct sol_cqe *psol)
  894. {
  895. struct hwi_wrb_context *pwrb_context;
  896. struct wrb_handle *pwrb_handle = NULL;
  897. struct hwi_controller *phwi_ctrlr;
  898. struct iscsi_task *task;
  899. struct beiscsi_io_task *io_task;
  900. struct iscsi_conn *conn = beiscsi_conn->conn;
  901. struct iscsi_session *session = conn->session;
  902. phwi_ctrlr = phba->phwi_ctrlr;
  903. pwrb_context = &phwi_ctrlr->wrb_context[((psol->
  904. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  905. SOL_CID_MASK) >> 6) -
  906. phba->fw_config.iscsi_cid_start];
  907. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  908. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  909. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  910. task = pwrb_handle->pio_handle;
  911. io_task = task->dd_data;
  912. spin_lock(&phba->mgmt_sgl_lock);
  913. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  914. spin_unlock(&phba->mgmt_sgl_lock);
  915. spin_lock_bh(&session->lock);
  916. free_wrb_handle(phba, pwrb_context, pwrb_handle);
  917. spin_unlock_bh(&session->lock);
  918. }
  919. static void
  920. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  921. struct iscsi_task *task, struct sol_cqe *psol)
  922. {
  923. struct iscsi_nopin *hdr;
  924. struct iscsi_conn *conn = beiscsi_conn->conn;
  925. struct beiscsi_io_task *io_task = task->dd_data;
  926. hdr = (struct iscsi_nopin *)task->hdr;
  927. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  928. & SOL_FLAGS_MASK) >> 24) | 0x80;
  929. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  930. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  931. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  932. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  933. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  934. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  935. hdr->opcode = ISCSI_OP_NOOP_IN;
  936. hdr->itt = io_task->libiscsi_itt;
  937. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  938. }
  939. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  940. struct beiscsi_hba *phba, struct sol_cqe *psol)
  941. {
  942. struct hwi_wrb_context *pwrb_context;
  943. struct wrb_handle *pwrb_handle;
  944. struct iscsi_wrb *pwrb = NULL;
  945. struct hwi_controller *phwi_ctrlr;
  946. struct iscsi_task *task;
  947. unsigned int type;
  948. struct iscsi_conn *conn = beiscsi_conn->conn;
  949. struct iscsi_session *session = conn->session;
  950. phwi_ctrlr = phba->phwi_ctrlr;
  951. pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
  952. (struct amap_sol_cqe, cid) / 32]
  953. & SOL_CID_MASK) >> 6) -
  954. phba->fw_config.iscsi_cid_start];
  955. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  956. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  957. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  958. task = pwrb_handle->pio_handle;
  959. pwrb = pwrb_handle->pwrb;
  960. type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
  961. WRB_TYPE_MASK) >> 28;
  962. spin_lock_bh(&session->lock);
  963. switch (type) {
  964. case HWH_TYPE_IO:
  965. case HWH_TYPE_IO_RD:
  966. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  967. ISCSI_OP_NOOP_OUT)
  968. be_complete_nopin_resp(beiscsi_conn, task, psol);
  969. else
  970. be_complete_io(beiscsi_conn, task, psol);
  971. break;
  972. case HWH_TYPE_LOGOUT:
  973. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  974. be_complete_logout(beiscsi_conn, task, psol);
  975. else
  976. be_complete_tmf(beiscsi_conn, task, psol);
  977. break;
  978. case HWH_TYPE_LOGIN:
  979. SE_DEBUG(DBG_LVL_1,
  980. "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
  981. "- Solicited path\n");
  982. break;
  983. case HWH_TYPE_NOP:
  984. be_complete_nopin_resp(beiscsi_conn, task, psol);
  985. break;
  986. default:
  987. shost_printk(KERN_WARNING, phba->shost,
  988. "In hwi_complete_cmd, unknown type = %d"
  989. "wrb_index 0x%x CID 0x%x\n", type,
  990. ((psol->dw[offsetof(struct amap_iscsi_wrb,
  991. type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
  992. ((psol->dw[offsetof(struct amap_sol_cqe,
  993. cid) / 32] & SOL_CID_MASK) >> 6));
  994. break;
  995. }
  996. spin_unlock_bh(&session->lock);
  997. }
  998. static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
  999. *pasync_ctx, unsigned int is_header,
  1000. unsigned int host_write_ptr)
  1001. {
  1002. if (is_header)
  1003. return &pasync_ctx->async_entry[host_write_ptr].
  1004. header_busy_list;
  1005. else
  1006. return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
  1007. }
  1008. static struct async_pdu_handle *
  1009. hwi_get_async_handle(struct beiscsi_hba *phba,
  1010. struct beiscsi_conn *beiscsi_conn,
  1011. struct hwi_async_pdu_context *pasync_ctx,
  1012. struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
  1013. {
  1014. struct be_bus_address phys_addr;
  1015. struct list_head *pbusy_list;
  1016. struct async_pdu_handle *pasync_handle = NULL;
  1017. int buffer_len = 0;
  1018. unsigned char buffer_index = -1;
  1019. unsigned char is_header = 0;
  1020. phys_addr.u.a32.address_lo =
  1021. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
  1022. ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1023. & PDUCQE_DPL_MASK) >> 16);
  1024. phys_addr.u.a32.address_hi =
  1025. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
  1026. phys_addr.u.a64.address =
  1027. *((unsigned long long *)(&phys_addr.u.a64.address));
  1028. switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
  1029. & PDUCQE_CODE_MASK) {
  1030. case UNSOL_HDR_NOTIFY:
  1031. is_header = 1;
  1032. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
  1033. (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1034. index) / 32] & PDUCQE_INDEX_MASK));
  1035. buffer_len = (unsigned int)(phys_addr.u.a64.address -
  1036. pasync_ctx->async_header.pa_base.u.a64.address);
  1037. buffer_index = buffer_len /
  1038. pasync_ctx->async_header.buffer_size;
  1039. break;
  1040. case UNSOL_DATA_NOTIFY:
  1041. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
  1042. dw[offsetof(struct amap_i_t_dpdu_cqe,
  1043. index) / 32] & PDUCQE_INDEX_MASK));
  1044. buffer_len = (unsigned long)(phys_addr.u.a64.address -
  1045. pasync_ctx->async_data.pa_base.u.
  1046. a64.address);
  1047. buffer_index = buffer_len / pasync_ctx->async_data.buffer_size;
  1048. break;
  1049. default:
  1050. pbusy_list = NULL;
  1051. shost_printk(KERN_WARNING, phba->shost,
  1052. "Unexpected code=%d\n",
  1053. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1054. code) / 32] & PDUCQE_CODE_MASK);
  1055. return NULL;
  1056. }
  1057. WARN_ON(!(buffer_index <= pasync_ctx->async_data.num_entries));
  1058. WARN_ON(list_empty(pbusy_list));
  1059. list_for_each_entry(pasync_handle, pbusy_list, link) {
  1060. WARN_ON(pasync_handle->consumed);
  1061. if (pasync_handle->index == buffer_index)
  1062. break;
  1063. }
  1064. WARN_ON(!pasync_handle);
  1065. pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
  1066. phba->fw_config.iscsi_cid_start;
  1067. pasync_handle->is_header = is_header;
  1068. pasync_handle->buffer_len = ((pdpdu_cqe->
  1069. dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1070. & PDUCQE_DPL_MASK) >> 16);
  1071. *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1072. index) / 32] & PDUCQE_INDEX_MASK);
  1073. return pasync_handle;
  1074. }
  1075. static unsigned int
  1076. hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
  1077. unsigned int is_header, unsigned int cq_index)
  1078. {
  1079. struct list_head *pbusy_list;
  1080. struct async_pdu_handle *pasync_handle;
  1081. unsigned int num_entries, writables = 0;
  1082. unsigned int *pep_read_ptr, *pwritables;
  1083. if (is_header) {
  1084. pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
  1085. pwritables = &pasync_ctx->async_header.writables;
  1086. num_entries = pasync_ctx->async_header.num_entries;
  1087. } else {
  1088. pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
  1089. pwritables = &pasync_ctx->async_data.writables;
  1090. num_entries = pasync_ctx->async_data.num_entries;
  1091. }
  1092. while ((*pep_read_ptr) != cq_index) {
  1093. (*pep_read_ptr)++;
  1094. *pep_read_ptr = (*pep_read_ptr) % num_entries;
  1095. pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
  1096. *pep_read_ptr);
  1097. if (writables == 0)
  1098. WARN_ON(list_empty(pbusy_list));
  1099. if (!list_empty(pbusy_list)) {
  1100. pasync_handle = list_entry(pbusy_list->next,
  1101. struct async_pdu_handle,
  1102. link);
  1103. WARN_ON(!pasync_handle);
  1104. pasync_handle->consumed = 1;
  1105. }
  1106. writables++;
  1107. }
  1108. if (!writables) {
  1109. SE_DEBUG(DBG_LVL_1,
  1110. "Duplicate notification received - index 0x%x!!\n",
  1111. cq_index);
  1112. WARN_ON(1);
  1113. }
  1114. *pwritables = *pwritables + writables;
  1115. return 0;
  1116. }
  1117. static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
  1118. unsigned int cri)
  1119. {
  1120. struct hwi_controller *phwi_ctrlr;
  1121. struct hwi_async_pdu_context *pasync_ctx;
  1122. struct async_pdu_handle *pasync_handle, *tmp_handle;
  1123. struct list_head *plist;
  1124. unsigned int i = 0;
  1125. phwi_ctrlr = phba->phwi_ctrlr;
  1126. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1127. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1128. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
  1129. list_del(&pasync_handle->link);
  1130. if (i == 0) {
  1131. list_add_tail(&pasync_handle->link,
  1132. &pasync_ctx->async_header.free_list);
  1133. pasync_ctx->async_header.free_entries++;
  1134. i++;
  1135. } else {
  1136. list_add_tail(&pasync_handle->link,
  1137. &pasync_ctx->async_data.free_list);
  1138. pasync_ctx->async_data.free_entries++;
  1139. i++;
  1140. }
  1141. }
  1142. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
  1143. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
  1144. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1145. return 0;
  1146. }
  1147. static struct phys_addr *
  1148. hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
  1149. unsigned int is_header, unsigned int host_write_ptr)
  1150. {
  1151. struct phys_addr *pasync_sge = NULL;
  1152. if (is_header)
  1153. pasync_sge = pasync_ctx->async_header.ring_base;
  1154. else
  1155. pasync_sge = pasync_ctx->async_data.ring_base;
  1156. return pasync_sge + host_write_ptr;
  1157. }
  1158. static void hwi_post_async_buffers(struct beiscsi_hba *phba,
  1159. unsigned int is_header)
  1160. {
  1161. struct hwi_controller *phwi_ctrlr;
  1162. struct hwi_async_pdu_context *pasync_ctx;
  1163. struct async_pdu_handle *pasync_handle;
  1164. struct list_head *pfree_link, *pbusy_list;
  1165. struct phys_addr *pasync_sge;
  1166. unsigned int ring_id, num_entries;
  1167. unsigned int host_write_num;
  1168. unsigned int writables;
  1169. unsigned int i = 0;
  1170. u32 doorbell = 0;
  1171. phwi_ctrlr = phba->phwi_ctrlr;
  1172. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1173. if (is_header) {
  1174. num_entries = pasync_ctx->async_header.num_entries;
  1175. writables = min(pasync_ctx->async_header.writables,
  1176. pasync_ctx->async_header.free_entries);
  1177. pfree_link = pasync_ctx->async_header.free_list.next;
  1178. host_write_num = pasync_ctx->async_header.host_write_ptr;
  1179. ring_id = phwi_ctrlr->default_pdu_hdr.id;
  1180. } else {
  1181. num_entries = pasync_ctx->async_data.num_entries;
  1182. writables = min(pasync_ctx->async_data.writables,
  1183. pasync_ctx->async_data.free_entries);
  1184. pfree_link = pasync_ctx->async_data.free_list.next;
  1185. host_write_num = pasync_ctx->async_data.host_write_ptr;
  1186. ring_id = phwi_ctrlr->default_pdu_data.id;
  1187. }
  1188. writables = (writables / 8) * 8;
  1189. if (writables) {
  1190. for (i = 0; i < writables; i++) {
  1191. pbusy_list =
  1192. hwi_get_async_busy_list(pasync_ctx, is_header,
  1193. host_write_num);
  1194. pasync_handle =
  1195. list_entry(pfree_link, struct async_pdu_handle,
  1196. link);
  1197. WARN_ON(!pasync_handle);
  1198. pasync_handle->consumed = 0;
  1199. pfree_link = pfree_link->next;
  1200. pasync_sge = hwi_get_ring_address(pasync_ctx,
  1201. is_header, host_write_num);
  1202. pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
  1203. pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
  1204. list_move(&pasync_handle->link, pbusy_list);
  1205. host_write_num++;
  1206. host_write_num = host_write_num % num_entries;
  1207. }
  1208. if (is_header) {
  1209. pasync_ctx->async_header.host_write_ptr =
  1210. host_write_num;
  1211. pasync_ctx->async_header.free_entries -= writables;
  1212. pasync_ctx->async_header.writables -= writables;
  1213. pasync_ctx->async_header.busy_entries += writables;
  1214. } else {
  1215. pasync_ctx->async_data.host_write_ptr = host_write_num;
  1216. pasync_ctx->async_data.free_entries -= writables;
  1217. pasync_ctx->async_data.writables -= writables;
  1218. pasync_ctx->async_data.busy_entries += writables;
  1219. }
  1220. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1221. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1222. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1223. doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
  1224. << DB_DEF_PDU_CQPROC_SHIFT;
  1225. iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
  1226. }
  1227. }
  1228. static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
  1229. struct beiscsi_conn *beiscsi_conn,
  1230. struct i_t_dpdu_cqe *pdpdu_cqe)
  1231. {
  1232. struct hwi_controller *phwi_ctrlr;
  1233. struct hwi_async_pdu_context *pasync_ctx;
  1234. struct async_pdu_handle *pasync_handle = NULL;
  1235. unsigned int cq_index = -1;
  1236. phwi_ctrlr = phba->phwi_ctrlr;
  1237. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1238. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1239. pdpdu_cqe, &cq_index);
  1240. BUG_ON(pasync_handle->is_header != 0);
  1241. if (pasync_handle->consumed == 0)
  1242. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1243. cq_index);
  1244. hwi_free_async_msg(phba, pasync_handle->cri);
  1245. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1246. }
  1247. static unsigned int
  1248. hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
  1249. struct beiscsi_hba *phba,
  1250. struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
  1251. {
  1252. struct list_head *plist;
  1253. struct async_pdu_handle *pasync_handle;
  1254. void *phdr = NULL;
  1255. unsigned int hdr_len = 0, buf_len = 0;
  1256. unsigned int status, index = 0, offset = 0;
  1257. void *pfirst_buffer = NULL;
  1258. unsigned int num_buf = 0;
  1259. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1260. list_for_each_entry(pasync_handle, plist, link) {
  1261. if (index == 0) {
  1262. phdr = pasync_handle->pbuffer;
  1263. hdr_len = pasync_handle->buffer_len;
  1264. } else {
  1265. buf_len = pasync_handle->buffer_len;
  1266. if (!num_buf) {
  1267. pfirst_buffer = pasync_handle->pbuffer;
  1268. num_buf++;
  1269. }
  1270. memcpy(pfirst_buffer + offset,
  1271. pasync_handle->pbuffer, buf_len);
  1272. offset = buf_len;
  1273. }
  1274. index++;
  1275. }
  1276. status = beiscsi_process_async_pdu(beiscsi_conn, phba,
  1277. (beiscsi_conn->beiscsi_conn_cid -
  1278. phba->fw_config.iscsi_cid_start),
  1279. phdr, hdr_len, pfirst_buffer,
  1280. buf_len);
  1281. if (status == 0)
  1282. hwi_free_async_msg(phba, cri);
  1283. return 0;
  1284. }
  1285. static unsigned int
  1286. hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
  1287. struct beiscsi_hba *phba,
  1288. struct async_pdu_handle *pasync_handle)
  1289. {
  1290. struct hwi_async_pdu_context *pasync_ctx;
  1291. struct hwi_controller *phwi_ctrlr;
  1292. unsigned int bytes_needed = 0, status = 0;
  1293. unsigned short cri = pasync_handle->cri;
  1294. struct pdu_base *ppdu;
  1295. phwi_ctrlr = phba->phwi_ctrlr;
  1296. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1297. list_del(&pasync_handle->link);
  1298. if (pasync_handle->is_header) {
  1299. pasync_ctx->async_header.busy_entries--;
  1300. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1301. hwi_free_async_msg(phba, cri);
  1302. BUG();
  1303. }
  1304. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1305. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
  1306. pasync_ctx->async_entry[cri].wait_queue.hdr_len =
  1307. (unsigned short)pasync_handle->buffer_len;
  1308. list_add_tail(&pasync_handle->link,
  1309. &pasync_ctx->async_entry[cri].wait_queue.list);
  1310. ppdu = pasync_handle->pbuffer;
  1311. bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
  1312. data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
  1313. 0xFFFF0000) | ((be16_to_cpu((ppdu->
  1314. dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
  1315. & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
  1316. if (status == 0) {
  1317. pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
  1318. bytes_needed;
  1319. if (bytes_needed == 0)
  1320. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1321. pasync_ctx, cri);
  1322. }
  1323. } else {
  1324. pasync_ctx->async_data.busy_entries--;
  1325. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1326. list_add_tail(&pasync_handle->link,
  1327. &pasync_ctx->async_entry[cri].wait_queue.
  1328. list);
  1329. pasync_ctx->async_entry[cri].wait_queue.
  1330. bytes_received +=
  1331. (unsigned short)pasync_handle->buffer_len;
  1332. if (pasync_ctx->async_entry[cri].wait_queue.
  1333. bytes_received >=
  1334. pasync_ctx->async_entry[cri].wait_queue.
  1335. bytes_needed)
  1336. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1337. pasync_ctx, cri);
  1338. }
  1339. }
  1340. return status;
  1341. }
  1342. static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
  1343. struct beiscsi_hba *phba,
  1344. struct i_t_dpdu_cqe *pdpdu_cqe)
  1345. {
  1346. struct hwi_controller *phwi_ctrlr;
  1347. struct hwi_async_pdu_context *pasync_ctx;
  1348. struct async_pdu_handle *pasync_handle = NULL;
  1349. unsigned int cq_index = -1;
  1350. phwi_ctrlr = phba->phwi_ctrlr;
  1351. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1352. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1353. pdpdu_cqe, &cq_index);
  1354. if (pasync_handle->consumed == 0)
  1355. hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
  1356. cq_index);
  1357. hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
  1358. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1359. }
  1360. static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
  1361. {
  1362. struct be_queue_info *mcc_cq;
  1363. struct be_mcc_compl *mcc_compl;
  1364. unsigned int num_processed = 0;
  1365. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1366. mcc_compl = queue_tail_node(mcc_cq);
  1367. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1368. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1369. if (num_processed >= 32) {
  1370. hwi_ring_cq_db(phba, mcc_cq->id,
  1371. num_processed, 0, 0);
  1372. num_processed = 0;
  1373. }
  1374. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1375. /* Interpret flags as an async trailer */
  1376. if (is_link_state_evt(mcc_compl->flags))
  1377. /* Interpret compl as a async link evt */
  1378. beiscsi_async_link_state_process(phba,
  1379. (struct be_async_event_link_state *) mcc_compl);
  1380. else
  1381. SE_DEBUG(DBG_LVL_1,
  1382. " Unsupported Async Event, flags"
  1383. " = 0x%08x\n", mcc_compl->flags);
  1384. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1385. be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
  1386. atomic_dec(&phba->ctrl.mcc_obj.q.used);
  1387. }
  1388. mcc_compl->flags = 0;
  1389. queue_tail_inc(mcc_cq);
  1390. mcc_compl = queue_tail_node(mcc_cq);
  1391. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1392. num_processed++;
  1393. }
  1394. if (num_processed > 0)
  1395. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
  1396. }
  1397. static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
  1398. {
  1399. struct be_queue_info *cq;
  1400. struct sol_cqe *sol;
  1401. struct dmsg_cqe *dmsg;
  1402. unsigned int num_processed = 0;
  1403. unsigned int tot_nump = 0;
  1404. struct beiscsi_conn *beiscsi_conn;
  1405. struct beiscsi_endpoint *beiscsi_ep;
  1406. struct iscsi_endpoint *ep;
  1407. struct beiscsi_hba *phba;
  1408. cq = pbe_eq->cq;
  1409. sol = queue_tail_node(cq);
  1410. phba = pbe_eq->phba;
  1411. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1412. CQE_VALID_MASK) {
  1413. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1414. ep = phba->ep_array[(u32) ((sol->
  1415. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  1416. SOL_CID_MASK) >> 6) -
  1417. phba->fw_config.iscsi_cid_start];
  1418. beiscsi_ep = ep->dd_data;
  1419. beiscsi_conn = beiscsi_ep->conn;
  1420. if (num_processed >= 32) {
  1421. hwi_ring_cq_db(phba, cq->id,
  1422. num_processed, 0, 0);
  1423. tot_nump += num_processed;
  1424. num_processed = 0;
  1425. }
  1426. switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
  1427. 32] & CQE_CODE_MASK) {
  1428. case SOL_CMD_COMPLETE:
  1429. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1430. break;
  1431. case DRIVERMSG_NOTIFY:
  1432. SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
  1433. dmsg = (struct dmsg_cqe *)sol;
  1434. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1435. break;
  1436. case UNSOL_HDR_NOTIFY:
  1437. SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
  1438. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1439. (struct i_t_dpdu_cqe *)sol);
  1440. break;
  1441. case UNSOL_DATA_NOTIFY:
  1442. SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
  1443. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1444. (struct i_t_dpdu_cqe *)sol);
  1445. break;
  1446. case CXN_INVALIDATE_INDEX_NOTIFY:
  1447. case CMD_INVALIDATED_NOTIFY:
  1448. case CXN_INVALIDATE_NOTIFY:
  1449. SE_DEBUG(DBG_LVL_1,
  1450. "Ignoring CQ Error notification for cmd/cxn"
  1451. "invalidate\n");
  1452. break;
  1453. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1454. case CMD_KILLED_INVALID_STATSN_RCVD:
  1455. case CMD_KILLED_INVALID_R2T_RCVD:
  1456. case CMD_CXN_KILLED_LUN_INVALID:
  1457. case CMD_CXN_KILLED_ICD_INVALID:
  1458. case CMD_CXN_KILLED_ITT_INVALID:
  1459. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1460. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1461. SE_DEBUG(DBG_LVL_1,
  1462. "CQ Error notification for cmd.. "
  1463. "code %d cid 0x%x\n",
  1464. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1465. 32] & CQE_CODE_MASK,
  1466. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1467. 32] & SOL_CID_MASK));
  1468. break;
  1469. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1470. SE_DEBUG(DBG_LVL_1,
  1471. "Digest error on def pdu ring, dropping..\n");
  1472. hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
  1473. (struct i_t_dpdu_cqe *) sol);
  1474. break;
  1475. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1476. case CXN_KILLED_BURST_LEN_MISMATCH:
  1477. case CXN_KILLED_AHS_RCVD:
  1478. case CXN_KILLED_HDR_DIGEST_ERR:
  1479. case CXN_KILLED_UNKNOWN_HDR:
  1480. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1481. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1482. case CXN_KILLED_TIMED_OUT:
  1483. case CXN_KILLED_FIN_RCVD:
  1484. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1485. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1486. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1487. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1488. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1489. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
  1490. "0x%x...\n",
  1491. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1492. 32] & CQE_CODE_MASK,
  1493. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1494. 32] & CQE_CID_MASK));
  1495. iscsi_conn_failure(beiscsi_conn->conn,
  1496. ISCSI_ERR_CONN_FAILED);
  1497. break;
  1498. case CXN_KILLED_RST_SENT:
  1499. case CXN_KILLED_RST_RCVD:
  1500. SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
  1501. "received/sent on CID 0x%x...\n",
  1502. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1503. 32] & CQE_CODE_MASK,
  1504. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1505. 32] & CQE_CID_MASK));
  1506. iscsi_conn_failure(beiscsi_conn->conn,
  1507. ISCSI_ERR_CONN_FAILED);
  1508. break;
  1509. default:
  1510. SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
  1511. "received on CID 0x%x...\n",
  1512. sol->dw[offsetof(struct amap_sol_cqe, code) /
  1513. 32] & CQE_CODE_MASK,
  1514. (sol->dw[offsetof(struct amap_sol_cqe, cid) /
  1515. 32] & CQE_CID_MASK));
  1516. break;
  1517. }
  1518. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1519. queue_tail_inc(cq);
  1520. sol = queue_tail_node(cq);
  1521. num_processed++;
  1522. }
  1523. if (num_processed > 0) {
  1524. tot_nump += num_processed;
  1525. hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
  1526. }
  1527. return tot_nump;
  1528. }
  1529. void beiscsi_process_all_cqs(struct work_struct *work)
  1530. {
  1531. unsigned long flags;
  1532. struct hwi_controller *phwi_ctrlr;
  1533. struct hwi_context_memory *phwi_context;
  1534. struct be_eq_obj *pbe_eq;
  1535. struct beiscsi_hba *phba =
  1536. container_of(work, struct beiscsi_hba, work_cqs);
  1537. phwi_ctrlr = phba->phwi_ctrlr;
  1538. phwi_context = phwi_ctrlr->phwi_ctxt;
  1539. if (phba->msix_enabled)
  1540. pbe_eq = &phwi_context->be_eq[phba->num_cpus];
  1541. else
  1542. pbe_eq = &phwi_context->be_eq[0];
  1543. if (phba->todo_mcc_cq) {
  1544. spin_lock_irqsave(&phba->isr_lock, flags);
  1545. phba->todo_mcc_cq = 0;
  1546. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1547. beiscsi_process_mcc_isr(phba);
  1548. }
  1549. if (phba->todo_cq) {
  1550. spin_lock_irqsave(&phba->isr_lock, flags);
  1551. phba->todo_cq = 0;
  1552. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1553. beiscsi_process_cq(pbe_eq);
  1554. }
  1555. }
  1556. static int be_iopoll(struct blk_iopoll *iop, int budget)
  1557. {
  1558. static unsigned int ret;
  1559. struct beiscsi_hba *phba;
  1560. struct be_eq_obj *pbe_eq;
  1561. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1562. ret = beiscsi_process_cq(pbe_eq);
  1563. if (ret < budget) {
  1564. phba = pbe_eq->phba;
  1565. blk_iopoll_complete(iop);
  1566. SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
  1567. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1568. }
  1569. return ret;
  1570. }
  1571. static void
  1572. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1573. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1574. {
  1575. struct iscsi_sge *psgl;
  1576. unsigned short sg_len, index;
  1577. unsigned int sge_len = 0;
  1578. unsigned long long addr;
  1579. struct scatterlist *l_sg;
  1580. unsigned int offset;
  1581. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1582. io_task->bhs_pa.u.a32.address_lo);
  1583. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1584. io_task->bhs_pa.u.a32.address_hi);
  1585. l_sg = sg;
  1586. for (index = 0; (index < num_sg) && (index < 2); index++,
  1587. sg = sg_next(sg)) {
  1588. if (index == 0) {
  1589. sg_len = sg_dma_len(sg);
  1590. addr = (u64) sg_dma_address(sg);
  1591. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1592. ((u32)(addr & 0xFFFFFFFF)));
  1593. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1594. ((u32)(addr >> 32)));
  1595. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1596. sg_len);
  1597. sge_len = sg_len;
  1598. } else {
  1599. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1600. pwrb, sge_len);
  1601. sg_len = sg_dma_len(sg);
  1602. addr = (u64) sg_dma_address(sg);
  1603. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1604. ((u32)(addr & 0xFFFFFFFF)));
  1605. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  1606. ((u32)(addr >> 32)));
  1607. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  1608. sg_len);
  1609. }
  1610. }
  1611. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1612. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1613. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1614. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1615. io_task->bhs_pa.u.a32.address_hi);
  1616. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1617. io_task->bhs_pa.u.a32.address_lo);
  1618. if (num_sg == 1) {
  1619. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1620. 1);
  1621. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1622. 0);
  1623. } else if (num_sg == 2) {
  1624. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1625. 0);
  1626. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1627. 1);
  1628. } else {
  1629. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1630. 0);
  1631. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1632. 0);
  1633. }
  1634. sg = l_sg;
  1635. psgl++;
  1636. psgl++;
  1637. offset = 0;
  1638. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1639. sg_len = sg_dma_len(sg);
  1640. addr = (u64) sg_dma_address(sg);
  1641. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1642. (addr & 0xFFFFFFFF));
  1643. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1644. (addr >> 32));
  1645. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1646. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1647. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1648. offset += sg_len;
  1649. }
  1650. psgl--;
  1651. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1652. }
  1653. static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  1654. {
  1655. struct iscsi_sge *psgl;
  1656. unsigned long long addr;
  1657. struct beiscsi_io_task *io_task = task->dd_data;
  1658. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  1659. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1660. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  1661. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1662. io_task->bhs_pa.u.a32.address_lo);
  1663. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1664. io_task->bhs_pa.u.a32.address_hi);
  1665. if (task->data) {
  1666. if (task->data_count) {
  1667. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  1668. addr = (u64) pci_map_single(phba->pcidev,
  1669. task->data,
  1670. task->data_count, 1);
  1671. } else {
  1672. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1673. addr = 0;
  1674. }
  1675. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1676. ((u32)(addr & 0xFFFFFFFF)));
  1677. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1678. ((u32)(addr >> 32)));
  1679. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1680. task->data_count);
  1681. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  1682. } else {
  1683. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  1684. addr = 0;
  1685. }
  1686. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1687. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  1688. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1689. io_task->bhs_pa.u.a32.address_hi);
  1690. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1691. io_task->bhs_pa.u.a32.address_lo);
  1692. if (task->data) {
  1693. psgl++;
  1694. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  1695. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  1696. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  1697. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  1698. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  1699. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1700. psgl++;
  1701. if (task->data) {
  1702. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1703. ((u32)(addr & 0xFFFFFFFF)));
  1704. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1705. ((u32)(addr >> 32)));
  1706. }
  1707. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  1708. }
  1709. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1710. }
  1711. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  1712. {
  1713. unsigned int num_cq_pages, num_async_pdu_buf_pages;
  1714. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  1715. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  1716. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  1717. sizeof(struct sol_cqe));
  1718. num_async_pdu_buf_pages =
  1719. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1720. phba->params.defpdu_hdr_sz);
  1721. num_async_pdu_buf_sgl_pages =
  1722. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1723. sizeof(struct phys_addr));
  1724. num_async_pdu_data_pages =
  1725. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1726. phba->params.defpdu_data_sz);
  1727. num_async_pdu_data_sgl_pages =
  1728. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  1729. sizeof(struct phys_addr));
  1730. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  1731. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  1732. BE_ISCSI_PDU_HEADER_SIZE;
  1733. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  1734. sizeof(struct hwi_context_memory);
  1735. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  1736. * (phba->params.wrbs_per_cxn)
  1737. * phba->params.cxns_per_ctrl;
  1738. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  1739. (phba->params.wrbs_per_cxn);
  1740. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  1741. phba->params.cxns_per_ctrl);
  1742. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  1743. phba->params.icds_per_ctrl;
  1744. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  1745. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  1746. phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
  1747. num_async_pdu_buf_pages * PAGE_SIZE;
  1748. phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
  1749. num_async_pdu_data_pages * PAGE_SIZE;
  1750. phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
  1751. num_async_pdu_buf_sgl_pages * PAGE_SIZE;
  1752. phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
  1753. num_async_pdu_data_sgl_pages * PAGE_SIZE;
  1754. phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
  1755. phba->params.asyncpdus_per_ctrl *
  1756. sizeof(struct async_pdu_handle);
  1757. phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
  1758. phba->params.asyncpdus_per_ctrl *
  1759. sizeof(struct async_pdu_handle);
  1760. phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
  1761. sizeof(struct hwi_async_pdu_context) +
  1762. (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
  1763. }
  1764. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  1765. {
  1766. struct be_mem_descriptor *mem_descr;
  1767. dma_addr_t bus_add;
  1768. struct mem_array *mem_arr, *mem_arr_orig;
  1769. unsigned int i, j, alloc_size, curr_alloc_size;
  1770. phba->phwi_ctrlr = kmalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  1771. if (!phba->phwi_ctrlr)
  1772. return -ENOMEM;
  1773. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  1774. GFP_KERNEL);
  1775. if (!phba->init_mem) {
  1776. kfree(phba->phwi_ctrlr);
  1777. return -ENOMEM;
  1778. }
  1779. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  1780. GFP_KERNEL);
  1781. if (!mem_arr_orig) {
  1782. kfree(phba->init_mem);
  1783. kfree(phba->phwi_ctrlr);
  1784. return -ENOMEM;
  1785. }
  1786. mem_descr = phba->init_mem;
  1787. for (i = 0; i < SE_MEM_MAX; i++) {
  1788. j = 0;
  1789. mem_arr = mem_arr_orig;
  1790. alloc_size = phba->mem_req[i];
  1791. memset(mem_arr, 0, sizeof(struct mem_array) *
  1792. BEISCSI_MAX_FRAGS_INIT);
  1793. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  1794. do {
  1795. mem_arr->virtual_address = pci_alloc_consistent(
  1796. phba->pcidev,
  1797. curr_alloc_size,
  1798. &bus_add);
  1799. if (!mem_arr->virtual_address) {
  1800. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  1801. goto free_mem;
  1802. if (curr_alloc_size -
  1803. rounddown_pow_of_two(curr_alloc_size))
  1804. curr_alloc_size = rounddown_pow_of_two
  1805. (curr_alloc_size);
  1806. else
  1807. curr_alloc_size = curr_alloc_size / 2;
  1808. } else {
  1809. mem_arr->bus_address.u.
  1810. a64.address = (__u64) bus_add;
  1811. mem_arr->size = curr_alloc_size;
  1812. alloc_size -= curr_alloc_size;
  1813. curr_alloc_size = min(be_max_phys_size *
  1814. 1024, alloc_size);
  1815. j++;
  1816. mem_arr++;
  1817. }
  1818. } while (alloc_size);
  1819. mem_descr->num_elements = j;
  1820. mem_descr->size_in_bytes = phba->mem_req[i];
  1821. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  1822. GFP_KERNEL);
  1823. if (!mem_descr->mem_array)
  1824. goto free_mem;
  1825. memcpy(mem_descr->mem_array, mem_arr_orig,
  1826. sizeof(struct mem_array) * j);
  1827. mem_descr++;
  1828. }
  1829. kfree(mem_arr_orig);
  1830. return 0;
  1831. free_mem:
  1832. mem_descr->num_elements = j;
  1833. while ((i) || (j)) {
  1834. for (j = mem_descr->num_elements; j > 0; j--) {
  1835. pci_free_consistent(phba->pcidev,
  1836. mem_descr->mem_array[j - 1].size,
  1837. mem_descr->mem_array[j - 1].
  1838. virtual_address,
  1839. (unsigned long)mem_descr->
  1840. mem_array[j - 1].
  1841. bus_address.u.a64.address);
  1842. }
  1843. if (i) {
  1844. i--;
  1845. kfree(mem_descr->mem_array);
  1846. mem_descr--;
  1847. }
  1848. }
  1849. kfree(mem_arr_orig);
  1850. kfree(phba->init_mem);
  1851. kfree(phba->phwi_ctrlr);
  1852. return -ENOMEM;
  1853. }
  1854. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  1855. {
  1856. beiscsi_find_mem_req(phba);
  1857. return beiscsi_alloc_mem(phba);
  1858. }
  1859. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  1860. {
  1861. struct pdu_data_out *pdata_out;
  1862. struct pdu_nop_out *pnop_out;
  1863. struct be_mem_descriptor *mem_descr;
  1864. mem_descr = phba->init_mem;
  1865. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  1866. pdata_out =
  1867. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  1868. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  1869. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  1870. IIOC_SCSI_DATA);
  1871. pnop_out =
  1872. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  1873. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  1874. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  1875. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  1876. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  1877. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  1878. }
  1879. static void beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  1880. {
  1881. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  1882. struct wrb_handle *pwrb_handle;
  1883. struct hwi_controller *phwi_ctrlr;
  1884. struct hwi_wrb_context *pwrb_context;
  1885. struct iscsi_wrb *pwrb;
  1886. unsigned int num_cxn_wrbh;
  1887. unsigned int num_cxn_wrb, j, idx, index;
  1888. mem_descr_wrbh = phba->init_mem;
  1889. mem_descr_wrbh += HWI_MEM_WRBH;
  1890. mem_descr_wrb = phba->init_mem;
  1891. mem_descr_wrb += HWI_MEM_WRB;
  1892. idx = 0;
  1893. pwrb_handle = mem_descr_wrbh->mem_array[idx].virtual_address;
  1894. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  1895. ((sizeof(struct wrb_handle)) *
  1896. phba->params.wrbs_per_cxn));
  1897. phwi_ctrlr = phba->phwi_ctrlr;
  1898. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  1899. pwrb_context = &phwi_ctrlr->wrb_context[index];
  1900. pwrb_context->pwrb_handle_base =
  1901. kzalloc(sizeof(struct wrb_handle *) *
  1902. phba->params.wrbs_per_cxn, GFP_KERNEL);
  1903. pwrb_context->pwrb_handle_basestd =
  1904. kzalloc(sizeof(struct wrb_handle *) *
  1905. phba->params.wrbs_per_cxn, GFP_KERNEL);
  1906. if (num_cxn_wrbh) {
  1907. pwrb_context->alloc_index = 0;
  1908. pwrb_context->wrb_handles_available = 0;
  1909. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1910. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  1911. pwrb_context->pwrb_handle_basestd[j] =
  1912. pwrb_handle;
  1913. pwrb_context->wrb_handles_available++;
  1914. pwrb_handle->wrb_index = j;
  1915. pwrb_handle++;
  1916. }
  1917. pwrb_context->free_index = 0;
  1918. num_cxn_wrbh--;
  1919. } else {
  1920. idx++;
  1921. pwrb_handle =
  1922. mem_descr_wrbh->mem_array[idx].virtual_address;
  1923. num_cxn_wrbh =
  1924. ((mem_descr_wrbh->mem_array[idx].size) /
  1925. ((sizeof(struct wrb_handle)) *
  1926. phba->params.wrbs_per_cxn));
  1927. pwrb_context->alloc_index = 0;
  1928. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1929. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  1930. pwrb_context->pwrb_handle_basestd[j] =
  1931. pwrb_handle;
  1932. pwrb_context->wrb_handles_available++;
  1933. pwrb_handle->wrb_index = j;
  1934. pwrb_handle++;
  1935. }
  1936. pwrb_context->free_index = 0;
  1937. num_cxn_wrbh--;
  1938. }
  1939. }
  1940. idx = 0;
  1941. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  1942. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  1943. ((sizeof(struct iscsi_wrb) *
  1944. phba->params.wrbs_per_cxn));
  1945. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  1946. pwrb_context = &phwi_ctrlr->wrb_context[index];
  1947. if (num_cxn_wrb) {
  1948. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1949. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  1950. pwrb_handle->pwrb = pwrb;
  1951. pwrb++;
  1952. }
  1953. num_cxn_wrb--;
  1954. } else {
  1955. idx++;
  1956. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  1957. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  1958. ((sizeof(struct iscsi_wrb) *
  1959. phba->params.wrbs_per_cxn));
  1960. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  1961. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  1962. pwrb_handle->pwrb = pwrb;
  1963. pwrb++;
  1964. }
  1965. num_cxn_wrb--;
  1966. }
  1967. }
  1968. }
  1969. static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  1970. {
  1971. struct hwi_controller *phwi_ctrlr;
  1972. struct hba_parameters *p = &phba->params;
  1973. struct hwi_async_pdu_context *pasync_ctx;
  1974. struct async_pdu_handle *pasync_header_h, *pasync_data_h;
  1975. unsigned int index;
  1976. struct be_mem_descriptor *mem_descr;
  1977. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  1978. mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
  1979. phwi_ctrlr = phba->phwi_ctrlr;
  1980. phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
  1981. mem_descr->mem_array[0].virtual_address;
  1982. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
  1983. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  1984. pasync_ctx->async_header.num_entries = p->asyncpdus_per_ctrl;
  1985. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  1986. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  1987. pasync_ctx->async_data.num_entries = p->asyncpdus_per_ctrl;
  1988. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  1989. mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
  1990. if (mem_descr->mem_array[0].virtual_address) {
  1991. SE_DEBUG(DBG_LVL_8,
  1992. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
  1993. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  1994. } else
  1995. shost_printk(KERN_WARNING, phba->shost,
  1996. "No Virtual address\n");
  1997. pasync_ctx->async_header.va_base =
  1998. mem_descr->mem_array[0].virtual_address;
  1999. pasync_ctx->async_header.pa_base.u.a64.address =
  2000. mem_descr->mem_array[0].bus_address.u.a64.address;
  2001. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2002. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2003. if (mem_descr->mem_array[0].virtual_address) {
  2004. SE_DEBUG(DBG_LVL_8,
  2005. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
  2006. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2007. } else
  2008. shost_printk(KERN_WARNING, phba->shost,
  2009. "No Virtual address\n");
  2010. pasync_ctx->async_header.ring_base =
  2011. mem_descr->mem_array[0].virtual_address;
  2012. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2013. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
  2014. if (mem_descr->mem_array[0].virtual_address) {
  2015. SE_DEBUG(DBG_LVL_8,
  2016. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
  2017. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2018. } else
  2019. shost_printk(KERN_WARNING, phba->shost,
  2020. "No Virtual address\n");
  2021. pasync_ctx->async_header.handle_base =
  2022. mem_descr->mem_array[0].virtual_address;
  2023. pasync_ctx->async_header.writables = 0;
  2024. INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
  2025. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2026. mem_descr += HWI_MEM_ASYNC_DATA_BUF;
  2027. if (mem_descr->mem_array[0].virtual_address) {
  2028. SE_DEBUG(DBG_LVL_8,
  2029. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
  2030. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2031. } else
  2032. shost_printk(KERN_WARNING, phba->shost,
  2033. "No Virtual address\n");
  2034. pasync_ctx->async_data.va_base =
  2035. mem_descr->mem_array[0].virtual_address;
  2036. pasync_ctx->async_data.pa_base.u.a64.address =
  2037. mem_descr->mem_array[0].bus_address.u.a64.address;
  2038. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2039. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2040. if (mem_descr->mem_array[0].virtual_address) {
  2041. SE_DEBUG(DBG_LVL_8,
  2042. "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
  2043. "va=%p\n", mem_descr->mem_array[0].virtual_address);
  2044. } else
  2045. shost_printk(KERN_WARNING, phba->shost,
  2046. "No Virtual address\n");
  2047. pasync_ctx->async_data.ring_base =
  2048. mem_descr->mem_array[0].virtual_address;
  2049. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2050. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
  2051. if (!mem_descr->mem_array[0].virtual_address)
  2052. shost_printk(KERN_WARNING, phba->shost,
  2053. "No Virtual address\n");
  2054. pasync_ctx->async_data.handle_base =
  2055. mem_descr->mem_array[0].virtual_address;
  2056. pasync_ctx->async_data.writables = 0;
  2057. INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
  2058. pasync_header_h =
  2059. (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
  2060. pasync_data_h =
  2061. (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
  2062. for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
  2063. pasync_header_h->cri = -1;
  2064. pasync_header_h->index = (char)index;
  2065. INIT_LIST_HEAD(&pasync_header_h->link);
  2066. pasync_header_h->pbuffer =
  2067. (void *)((unsigned long)
  2068. (pasync_ctx->async_header.va_base) +
  2069. (p->defpdu_hdr_sz * index));
  2070. pasync_header_h->pa.u.a64.address =
  2071. pasync_ctx->async_header.pa_base.u.a64.address +
  2072. (p->defpdu_hdr_sz * index);
  2073. list_add_tail(&pasync_header_h->link,
  2074. &pasync_ctx->async_header.free_list);
  2075. pasync_header_h++;
  2076. pasync_ctx->async_header.free_entries++;
  2077. pasync_ctx->async_header.writables++;
  2078. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
  2079. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2080. header_busy_list);
  2081. pasync_data_h->cri = -1;
  2082. pasync_data_h->index = (char)index;
  2083. INIT_LIST_HEAD(&pasync_data_h->link);
  2084. pasync_data_h->pbuffer =
  2085. (void *)((unsigned long)
  2086. (pasync_ctx->async_data.va_base) +
  2087. (p->defpdu_data_sz * index));
  2088. pasync_data_h->pa.u.a64.address =
  2089. pasync_ctx->async_data.pa_base.u.a64.address +
  2090. (p->defpdu_data_sz * index);
  2091. list_add_tail(&pasync_data_h->link,
  2092. &pasync_ctx->async_data.free_list);
  2093. pasync_data_h++;
  2094. pasync_ctx->async_data.free_entries++;
  2095. pasync_ctx->async_data.writables++;
  2096. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
  2097. }
  2098. pasync_ctx->async_header.host_write_ptr = 0;
  2099. pasync_ctx->async_header.ep_read_ptr = -1;
  2100. pasync_ctx->async_data.host_write_ptr = 0;
  2101. pasync_ctx->async_data.ep_read_ptr = -1;
  2102. }
  2103. static int
  2104. be_sgl_create_contiguous(void *virtual_address,
  2105. u64 physical_address, u32 length,
  2106. struct be_dma_mem *sgl)
  2107. {
  2108. WARN_ON(!virtual_address);
  2109. WARN_ON(!physical_address);
  2110. WARN_ON(!length > 0);
  2111. WARN_ON(!sgl);
  2112. sgl->va = virtual_address;
  2113. sgl->dma = (unsigned long)physical_address;
  2114. sgl->size = length;
  2115. return 0;
  2116. }
  2117. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2118. {
  2119. memset(sgl, 0, sizeof(*sgl));
  2120. }
  2121. static void
  2122. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2123. struct mem_array *pmem, struct be_dma_mem *sgl)
  2124. {
  2125. if (sgl->va)
  2126. be_sgl_destroy_contiguous(sgl);
  2127. be_sgl_create_contiguous(pmem->virtual_address,
  2128. pmem->bus_address.u.a64.address,
  2129. pmem->size, sgl);
  2130. }
  2131. static void
  2132. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2133. struct mem_array *pmem, struct be_dma_mem *sgl)
  2134. {
  2135. if (sgl->va)
  2136. be_sgl_destroy_contiguous(sgl);
  2137. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2138. pmem->bus_address.u.a64.address,
  2139. pmem->size, sgl);
  2140. }
  2141. static int be_fill_queue(struct be_queue_info *q,
  2142. u16 len, u16 entry_size, void *vaddress)
  2143. {
  2144. struct be_dma_mem *mem = &q->dma_mem;
  2145. memset(q, 0, sizeof(*q));
  2146. q->len = len;
  2147. q->entry_size = entry_size;
  2148. mem->size = len * entry_size;
  2149. mem->va = vaddress;
  2150. if (!mem->va)
  2151. return -ENOMEM;
  2152. memset(mem->va, 0, mem->size);
  2153. return 0;
  2154. }
  2155. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2156. struct hwi_context_memory *phwi_context)
  2157. {
  2158. unsigned int i, num_eq_pages;
  2159. int ret, eq_for_mcc;
  2160. struct be_queue_info *eq;
  2161. struct be_dma_mem *mem;
  2162. void *eq_vaddress;
  2163. dma_addr_t paddr;
  2164. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2165. sizeof(struct be_eq_entry));
  2166. if (phba->msix_enabled)
  2167. eq_for_mcc = 1;
  2168. else
  2169. eq_for_mcc = 0;
  2170. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2171. eq = &phwi_context->be_eq[i].q;
  2172. mem = &eq->dma_mem;
  2173. phwi_context->be_eq[i].phba = phba;
  2174. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2175. num_eq_pages * PAGE_SIZE,
  2176. &paddr);
  2177. if (!eq_vaddress)
  2178. goto create_eq_error;
  2179. mem->va = eq_vaddress;
  2180. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2181. sizeof(struct be_eq_entry), eq_vaddress);
  2182. if (ret) {
  2183. shost_printk(KERN_ERR, phba->shost,
  2184. "be_fill_queue Failed for EQ\n");
  2185. goto create_eq_error;
  2186. }
  2187. mem->dma = paddr;
  2188. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2189. phwi_context->cur_eqd);
  2190. if (ret) {
  2191. shost_printk(KERN_ERR, phba->shost,
  2192. "beiscsi_cmd_eq_create"
  2193. "Failedfor EQ\n");
  2194. goto create_eq_error;
  2195. }
  2196. SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
  2197. }
  2198. return 0;
  2199. create_eq_error:
  2200. for (i = 0; i < (phba->num_cpus + 1); i++) {
  2201. eq = &phwi_context->be_eq[i].q;
  2202. mem = &eq->dma_mem;
  2203. if (mem->va)
  2204. pci_free_consistent(phba->pcidev, num_eq_pages
  2205. * PAGE_SIZE,
  2206. mem->va, mem->dma);
  2207. }
  2208. return ret;
  2209. }
  2210. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2211. struct hwi_context_memory *phwi_context)
  2212. {
  2213. unsigned int i, num_cq_pages;
  2214. int ret;
  2215. struct be_queue_info *cq, *eq;
  2216. struct be_dma_mem *mem;
  2217. struct be_eq_obj *pbe_eq;
  2218. void *cq_vaddress;
  2219. dma_addr_t paddr;
  2220. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2221. sizeof(struct sol_cqe));
  2222. for (i = 0; i < phba->num_cpus; i++) {
  2223. cq = &phwi_context->be_cq[i];
  2224. eq = &phwi_context->be_eq[i].q;
  2225. pbe_eq = &phwi_context->be_eq[i];
  2226. pbe_eq->cq = cq;
  2227. pbe_eq->phba = phba;
  2228. mem = &cq->dma_mem;
  2229. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2230. num_cq_pages * PAGE_SIZE,
  2231. &paddr);
  2232. if (!cq_vaddress)
  2233. goto create_cq_error;
  2234. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2235. sizeof(struct sol_cqe), cq_vaddress);
  2236. if (ret) {
  2237. shost_printk(KERN_ERR, phba->shost,
  2238. "be_fill_queue Failed for ISCSI CQ\n");
  2239. goto create_cq_error;
  2240. }
  2241. mem->dma = paddr;
  2242. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2243. false, 0);
  2244. if (ret) {
  2245. shost_printk(KERN_ERR, phba->shost,
  2246. "beiscsi_cmd_eq_create"
  2247. "Failed for ISCSI CQ\n");
  2248. goto create_cq_error;
  2249. }
  2250. SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
  2251. cq->id, eq->id);
  2252. SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
  2253. }
  2254. return 0;
  2255. create_cq_error:
  2256. for (i = 0; i < phba->num_cpus; i++) {
  2257. cq = &phwi_context->be_cq[i];
  2258. mem = &cq->dma_mem;
  2259. if (mem->va)
  2260. pci_free_consistent(phba->pcidev, num_cq_pages
  2261. * PAGE_SIZE,
  2262. mem->va, mem->dma);
  2263. }
  2264. return ret;
  2265. }
  2266. static int
  2267. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2268. struct hwi_context_memory *phwi_context,
  2269. struct hwi_controller *phwi_ctrlr,
  2270. unsigned int def_pdu_ring_sz)
  2271. {
  2272. unsigned int idx;
  2273. int ret;
  2274. struct be_queue_info *dq, *cq;
  2275. struct be_dma_mem *mem;
  2276. struct be_mem_descriptor *mem_descr;
  2277. void *dq_vaddress;
  2278. idx = 0;
  2279. dq = &phwi_context->be_def_hdrq;
  2280. cq = &phwi_context->be_cq[0];
  2281. mem = &dq->dma_mem;
  2282. mem_descr = phba->init_mem;
  2283. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2284. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2285. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2286. sizeof(struct phys_addr),
  2287. sizeof(struct phys_addr), dq_vaddress);
  2288. if (ret) {
  2289. shost_printk(KERN_ERR, phba->shost,
  2290. "be_fill_queue Failed for DEF PDU HDR\n");
  2291. return ret;
  2292. }
  2293. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2294. bus_address.u.a64.address;
  2295. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2296. def_pdu_ring_sz,
  2297. phba->params.defpdu_hdr_sz);
  2298. if (ret) {
  2299. shost_printk(KERN_ERR, phba->shost,
  2300. "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
  2301. return ret;
  2302. }
  2303. phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
  2304. SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
  2305. phwi_context->be_def_hdrq.id);
  2306. hwi_post_async_buffers(phba, 1);
  2307. return 0;
  2308. }
  2309. static int
  2310. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2311. struct hwi_context_memory *phwi_context,
  2312. struct hwi_controller *phwi_ctrlr,
  2313. unsigned int def_pdu_ring_sz)
  2314. {
  2315. unsigned int idx;
  2316. int ret;
  2317. struct be_queue_info *dataq, *cq;
  2318. struct be_dma_mem *mem;
  2319. struct be_mem_descriptor *mem_descr;
  2320. void *dq_vaddress;
  2321. idx = 0;
  2322. dataq = &phwi_context->be_def_dataq;
  2323. cq = &phwi_context->be_cq[0];
  2324. mem = &dataq->dma_mem;
  2325. mem_descr = phba->init_mem;
  2326. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2327. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2328. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2329. sizeof(struct phys_addr),
  2330. sizeof(struct phys_addr), dq_vaddress);
  2331. if (ret) {
  2332. shost_printk(KERN_ERR, phba->shost,
  2333. "be_fill_queue Failed for DEF PDU DATA\n");
  2334. return ret;
  2335. }
  2336. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2337. bus_address.u.a64.address;
  2338. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2339. def_pdu_ring_sz,
  2340. phba->params.defpdu_data_sz);
  2341. if (ret) {
  2342. shost_printk(KERN_ERR, phba->shost,
  2343. "be_cmd_create_default_pdu_queue Failed"
  2344. " for DEF PDU DATA\n");
  2345. return ret;
  2346. }
  2347. phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
  2348. SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
  2349. phwi_context->be_def_dataq.id);
  2350. hwi_post_async_buffers(phba, 0);
  2351. SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
  2352. return 0;
  2353. }
  2354. static int
  2355. beiscsi_post_pages(struct beiscsi_hba *phba)
  2356. {
  2357. struct be_mem_descriptor *mem_descr;
  2358. struct mem_array *pm_arr;
  2359. unsigned int page_offset, i;
  2360. struct be_dma_mem sgl;
  2361. int status;
  2362. mem_descr = phba->init_mem;
  2363. mem_descr += HWI_MEM_SGE;
  2364. pm_arr = mem_descr->mem_array;
  2365. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2366. phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
  2367. for (i = 0; i < mem_descr->num_elements; i++) {
  2368. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2369. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2370. page_offset,
  2371. (pm_arr->size / PAGE_SIZE));
  2372. page_offset += pm_arr->size / PAGE_SIZE;
  2373. if (status != 0) {
  2374. shost_printk(KERN_ERR, phba->shost,
  2375. "post sgl failed.\n");
  2376. return status;
  2377. }
  2378. pm_arr++;
  2379. }
  2380. SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
  2381. return 0;
  2382. }
  2383. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2384. {
  2385. struct be_dma_mem *mem = &q->dma_mem;
  2386. if (mem->va)
  2387. pci_free_consistent(phba->pcidev, mem->size,
  2388. mem->va, mem->dma);
  2389. }
  2390. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2391. u16 len, u16 entry_size)
  2392. {
  2393. struct be_dma_mem *mem = &q->dma_mem;
  2394. memset(q, 0, sizeof(*q));
  2395. q->len = len;
  2396. q->entry_size = entry_size;
  2397. mem->size = len * entry_size;
  2398. mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2399. if (!mem->va)
  2400. return -1;
  2401. memset(mem->va, 0, mem->size);
  2402. return 0;
  2403. }
  2404. static int
  2405. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  2406. struct hwi_context_memory *phwi_context,
  2407. struct hwi_controller *phwi_ctrlr)
  2408. {
  2409. unsigned int wrb_mem_index, offset, size, num_wrb_rings;
  2410. u64 pa_addr_lo;
  2411. unsigned int idx, num, i;
  2412. struct mem_array *pwrb_arr;
  2413. void *wrb_vaddr;
  2414. struct be_dma_mem sgl;
  2415. struct be_mem_descriptor *mem_descr;
  2416. int status;
  2417. idx = 0;
  2418. mem_descr = phba->init_mem;
  2419. mem_descr += HWI_MEM_WRB;
  2420. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  2421. GFP_KERNEL);
  2422. if (!pwrb_arr) {
  2423. shost_printk(KERN_ERR, phba->shost,
  2424. "Memory alloc failed in create wrb ring.\n");
  2425. return -ENOMEM;
  2426. }
  2427. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2428. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  2429. num_wrb_rings = mem_descr->mem_array[idx].size /
  2430. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  2431. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  2432. if (num_wrb_rings) {
  2433. pwrb_arr[num].virtual_address = wrb_vaddr;
  2434. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  2435. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2436. sizeof(struct iscsi_wrb);
  2437. wrb_vaddr += pwrb_arr[num].size;
  2438. pa_addr_lo += pwrb_arr[num].size;
  2439. num_wrb_rings--;
  2440. } else {
  2441. idx++;
  2442. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2443. pa_addr_lo = mem_descr->mem_array[idx].\
  2444. bus_address.u.a64.address;
  2445. num_wrb_rings = mem_descr->mem_array[idx].size /
  2446. (phba->params.wrbs_per_cxn *
  2447. sizeof(struct iscsi_wrb));
  2448. pwrb_arr[num].virtual_address = wrb_vaddr;
  2449. pwrb_arr[num].bus_address.u.a64.address\
  2450. = pa_addr_lo;
  2451. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2452. sizeof(struct iscsi_wrb);
  2453. wrb_vaddr += pwrb_arr[num].size;
  2454. pa_addr_lo += pwrb_arr[num].size;
  2455. num_wrb_rings--;
  2456. }
  2457. }
  2458. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2459. wrb_mem_index = 0;
  2460. offset = 0;
  2461. size = 0;
  2462. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  2463. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  2464. &phwi_context->be_wrbq[i]);
  2465. if (status != 0) {
  2466. shost_printk(KERN_ERR, phba->shost,
  2467. "wrbq create failed.");
  2468. kfree(pwrb_arr);
  2469. return status;
  2470. }
  2471. phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
  2472. id;
  2473. }
  2474. kfree(pwrb_arr);
  2475. return 0;
  2476. }
  2477. static void free_wrb_handles(struct beiscsi_hba *phba)
  2478. {
  2479. unsigned int index;
  2480. struct hwi_controller *phwi_ctrlr;
  2481. struct hwi_wrb_context *pwrb_context;
  2482. phwi_ctrlr = phba->phwi_ctrlr;
  2483. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2484. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2485. kfree(pwrb_context->pwrb_handle_base);
  2486. kfree(pwrb_context->pwrb_handle_basestd);
  2487. }
  2488. }
  2489. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  2490. {
  2491. struct be_queue_info *q;
  2492. struct be_ctrl_info *ctrl = &phba->ctrl;
  2493. q = &phba->ctrl.mcc_obj.q;
  2494. if (q->created)
  2495. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  2496. be_queue_free(phba, q);
  2497. q = &phba->ctrl.mcc_obj.cq;
  2498. if (q->created)
  2499. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2500. be_queue_free(phba, q);
  2501. }
  2502. static void hwi_cleanup(struct beiscsi_hba *phba)
  2503. {
  2504. struct be_queue_info *q;
  2505. struct be_ctrl_info *ctrl = &phba->ctrl;
  2506. struct hwi_controller *phwi_ctrlr;
  2507. struct hwi_context_memory *phwi_context;
  2508. int i, eq_num;
  2509. phwi_ctrlr = phba->phwi_ctrlr;
  2510. phwi_context = phwi_ctrlr->phwi_ctxt;
  2511. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2512. q = &phwi_context->be_wrbq[i];
  2513. if (q->created)
  2514. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  2515. }
  2516. free_wrb_handles(phba);
  2517. q = &phwi_context->be_def_hdrq;
  2518. if (q->created)
  2519. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2520. q = &phwi_context->be_def_dataq;
  2521. if (q->created)
  2522. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2523. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  2524. for (i = 0; i < (phba->num_cpus); i++) {
  2525. q = &phwi_context->be_cq[i];
  2526. if (q->created)
  2527. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2528. }
  2529. if (phba->msix_enabled)
  2530. eq_num = 1;
  2531. else
  2532. eq_num = 0;
  2533. for (i = 0; i < (phba->num_cpus + eq_num); i++) {
  2534. q = &phwi_context->be_eq[i].q;
  2535. if (q->created)
  2536. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  2537. }
  2538. be_mcc_queues_destroy(phba);
  2539. }
  2540. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  2541. struct hwi_context_memory *phwi_context)
  2542. {
  2543. struct be_queue_info *q, *cq;
  2544. struct be_ctrl_info *ctrl = &phba->ctrl;
  2545. /* Alloc MCC compl queue */
  2546. cq = &phba->ctrl.mcc_obj.cq;
  2547. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  2548. sizeof(struct be_mcc_compl)))
  2549. goto err;
  2550. /* Ask BE to create MCC compl queue; */
  2551. if (phba->msix_enabled) {
  2552. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
  2553. [phba->num_cpus].q, false, true, 0))
  2554. goto mcc_cq_free;
  2555. } else {
  2556. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  2557. false, true, 0))
  2558. goto mcc_cq_free;
  2559. }
  2560. /* Alloc MCC queue */
  2561. q = &phba->ctrl.mcc_obj.q;
  2562. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  2563. goto mcc_cq_destroy;
  2564. /* Ask BE to create MCC queue */
  2565. if (beiscsi_cmd_mccq_create(phba, q, cq))
  2566. goto mcc_q_free;
  2567. return 0;
  2568. mcc_q_free:
  2569. be_queue_free(phba, q);
  2570. mcc_cq_destroy:
  2571. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  2572. mcc_cq_free:
  2573. be_queue_free(phba, cq);
  2574. err:
  2575. return -1;
  2576. }
  2577. static int find_num_cpus(void)
  2578. {
  2579. int num_cpus = 0;
  2580. num_cpus = num_online_cpus();
  2581. if (num_cpus >= MAX_CPUS)
  2582. num_cpus = MAX_CPUS - 1;
  2583. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
  2584. return num_cpus;
  2585. }
  2586. static int hwi_init_port(struct beiscsi_hba *phba)
  2587. {
  2588. struct hwi_controller *phwi_ctrlr;
  2589. struct hwi_context_memory *phwi_context;
  2590. unsigned int def_pdu_ring_sz;
  2591. struct be_ctrl_info *ctrl = &phba->ctrl;
  2592. int status;
  2593. def_pdu_ring_sz =
  2594. phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
  2595. phwi_ctrlr = phba->phwi_ctrlr;
  2596. phwi_context = phwi_ctrlr->phwi_ctxt;
  2597. phwi_context->max_eqd = 0;
  2598. phwi_context->min_eqd = 0;
  2599. phwi_context->cur_eqd = 64;
  2600. be_cmd_fw_initialize(&phba->ctrl);
  2601. status = beiscsi_create_eqs(phba, phwi_context);
  2602. if (status != 0) {
  2603. shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
  2604. goto error;
  2605. }
  2606. status = be_mcc_queues_create(phba, phwi_context);
  2607. if (status != 0)
  2608. goto error;
  2609. status = mgmt_check_supported_fw(ctrl, phba);
  2610. if (status != 0) {
  2611. shost_printk(KERN_ERR, phba->shost,
  2612. "Unsupported fw version\n");
  2613. goto error;
  2614. }
  2615. status = beiscsi_create_cqs(phba, phwi_context);
  2616. if (status != 0) {
  2617. shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
  2618. goto error;
  2619. }
  2620. status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
  2621. def_pdu_ring_sz);
  2622. if (status != 0) {
  2623. shost_printk(KERN_ERR, phba->shost,
  2624. "Default Header not created\n");
  2625. goto error;
  2626. }
  2627. status = beiscsi_create_def_data(phba, phwi_context,
  2628. phwi_ctrlr, def_pdu_ring_sz);
  2629. if (status != 0) {
  2630. shost_printk(KERN_ERR, phba->shost,
  2631. "Default Data not created\n");
  2632. goto error;
  2633. }
  2634. status = beiscsi_post_pages(phba);
  2635. if (status != 0) {
  2636. shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
  2637. goto error;
  2638. }
  2639. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  2640. if (status != 0) {
  2641. shost_printk(KERN_ERR, phba->shost,
  2642. "WRB Rings not created\n");
  2643. goto error;
  2644. }
  2645. SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
  2646. return 0;
  2647. error:
  2648. shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
  2649. hwi_cleanup(phba);
  2650. return -ENOMEM;
  2651. }
  2652. static int hwi_init_controller(struct beiscsi_hba *phba)
  2653. {
  2654. struct hwi_controller *phwi_ctrlr;
  2655. phwi_ctrlr = phba->phwi_ctrlr;
  2656. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  2657. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  2658. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  2659. SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
  2660. phwi_ctrlr->phwi_ctxt);
  2661. } else {
  2662. shost_printk(KERN_ERR, phba->shost,
  2663. "HWI_MEM_ADDN_CONTEXT is more than one element."
  2664. "Failing to load\n");
  2665. return -ENOMEM;
  2666. }
  2667. iscsi_init_global_templates(phba);
  2668. beiscsi_init_wrb_handle(phba);
  2669. hwi_init_async_pdu_ctx(phba);
  2670. if (hwi_init_port(phba) != 0) {
  2671. shost_printk(KERN_ERR, phba->shost,
  2672. "hwi_init_controller failed\n");
  2673. return -ENOMEM;
  2674. }
  2675. return 0;
  2676. }
  2677. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  2678. {
  2679. struct be_mem_descriptor *mem_descr;
  2680. int i, j;
  2681. mem_descr = phba->init_mem;
  2682. i = 0;
  2683. j = 0;
  2684. for (i = 0; i < SE_MEM_MAX; i++) {
  2685. for (j = mem_descr->num_elements; j > 0; j--) {
  2686. pci_free_consistent(phba->pcidev,
  2687. mem_descr->mem_array[j - 1].size,
  2688. mem_descr->mem_array[j - 1].virtual_address,
  2689. (unsigned long)mem_descr->mem_array[j - 1].
  2690. bus_address.u.a64.address);
  2691. }
  2692. kfree(mem_descr->mem_array);
  2693. mem_descr++;
  2694. }
  2695. kfree(phba->init_mem);
  2696. kfree(phba->phwi_ctrlr);
  2697. }
  2698. static int beiscsi_init_controller(struct beiscsi_hba *phba)
  2699. {
  2700. int ret = -ENOMEM;
  2701. ret = beiscsi_get_memory(phba);
  2702. if (ret < 0) {
  2703. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
  2704. "Failed in beiscsi_alloc_memory\n");
  2705. return ret;
  2706. }
  2707. ret = hwi_init_controller(phba);
  2708. if (ret)
  2709. goto free_init;
  2710. SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
  2711. return 0;
  2712. free_init:
  2713. beiscsi_free_mem(phba);
  2714. return -ENOMEM;
  2715. }
  2716. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  2717. {
  2718. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  2719. struct sgl_handle *psgl_handle;
  2720. struct iscsi_sge *pfrag;
  2721. unsigned int arr_index, i, idx;
  2722. phba->io_sgl_hndl_avbl = 0;
  2723. phba->eh_sgl_hndl_avbl = 0;
  2724. mem_descr_sglh = phba->init_mem;
  2725. mem_descr_sglh += HWI_MEM_SGLH;
  2726. if (1 == mem_descr_sglh->num_elements) {
  2727. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2728. phba->params.ios_per_ctrl,
  2729. GFP_KERNEL);
  2730. if (!phba->io_sgl_hndl_base) {
  2731. shost_printk(KERN_ERR, phba->shost,
  2732. "Mem Alloc Failed. Failing to load\n");
  2733. return -ENOMEM;
  2734. }
  2735. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  2736. (phba->params.icds_per_ctrl -
  2737. phba->params.ios_per_ctrl),
  2738. GFP_KERNEL);
  2739. if (!phba->eh_sgl_hndl_base) {
  2740. kfree(phba->io_sgl_hndl_base);
  2741. shost_printk(KERN_ERR, phba->shost,
  2742. "Mem Alloc Failed. Failing to load\n");
  2743. return -ENOMEM;
  2744. }
  2745. } else {
  2746. shost_printk(KERN_ERR, phba->shost,
  2747. "HWI_MEM_SGLH is more than one element."
  2748. "Failing to load\n");
  2749. return -ENOMEM;
  2750. }
  2751. arr_index = 0;
  2752. idx = 0;
  2753. while (idx < mem_descr_sglh->num_elements) {
  2754. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  2755. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  2756. sizeof(struct sgl_handle)); i++) {
  2757. if (arr_index < phba->params.ios_per_ctrl) {
  2758. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  2759. phba->io_sgl_hndl_avbl++;
  2760. arr_index++;
  2761. } else {
  2762. phba->eh_sgl_hndl_base[arr_index -
  2763. phba->params.ios_per_ctrl] =
  2764. psgl_handle;
  2765. arr_index++;
  2766. phba->eh_sgl_hndl_avbl++;
  2767. }
  2768. psgl_handle++;
  2769. }
  2770. idx++;
  2771. }
  2772. SE_DEBUG(DBG_LVL_8,
  2773. "phba->io_sgl_hndl_avbl=%d"
  2774. "phba->eh_sgl_hndl_avbl=%d\n",
  2775. phba->io_sgl_hndl_avbl,
  2776. phba->eh_sgl_hndl_avbl);
  2777. mem_descr_sg = phba->init_mem;
  2778. mem_descr_sg += HWI_MEM_SGE;
  2779. SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
  2780. mem_descr_sg->num_elements);
  2781. arr_index = 0;
  2782. idx = 0;
  2783. while (idx < mem_descr_sg->num_elements) {
  2784. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  2785. for (i = 0;
  2786. i < (mem_descr_sg->mem_array[idx].size) /
  2787. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  2788. i++) {
  2789. if (arr_index < phba->params.ios_per_ctrl)
  2790. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  2791. else
  2792. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  2793. phba->params.ios_per_ctrl];
  2794. psgl_handle->pfrag = pfrag;
  2795. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  2796. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  2797. pfrag += phba->params.num_sge_per_io;
  2798. psgl_handle->sgl_index =
  2799. phba->fw_config.iscsi_icd_start + arr_index++;
  2800. }
  2801. idx++;
  2802. }
  2803. phba->io_sgl_free_index = 0;
  2804. phba->io_sgl_alloc_index = 0;
  2805. phba->eh_sgl_free_index = 0;
  2806. phba->eh_sgl_alloc_index = 0;
  2807. return 0;
  2808. }
  2809. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  2810. {
  2811. int i, new_cid;
  2812. phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
  2813. GFP_KERNEL);
  2814. if (!phba->cid_array) {
  2815. shost_printk(KERN_ERR, phba->shost,
  2816. "Failed to allocate memory in "
  2817. "hba_setup_cid_tbls\n");
  2818. return -ENOMEM;
  2819. }
  2820. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  2821. phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
  2822. if (!phba->ep_array) {
  2823. shost_printk(KERN_ERR, phba->shost,
  2824. "Failed to allocate memory in "
  2825. "hba_setup_cid_tbls\n");
  2826. kfree(phba->cid_array);
  2827. return -ENOMEM;
  2828. }
  2829. new_cid = phba->fw_config.iscsi_cid_start;
  2830. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2831. phba->cid_array[i] = new_cid;
  2832. new_cid += 2;
  2833. }
  2834. phba->avlbl_cids = phba->params.cxns_per_ctrl;
  2835. return 0;
  2836. }
  2837. static unsigned char hwi_enable_intr(struct beiscsi_hba *phba)
  2838. {
  2839. struct be_ctrl_info *ctrl = &phba->ctrl;
  2840. struct hwi_controller *phwi_ctrlr;
  2841. struct hwi_context_memory *phwi_context;
  2842. struct be_queue_info *eq;
  2843. u8 __iomem *addr;
  2844. u32 reg, i;
  2845. u32 enabled;
  2846. phwi_ctrlr = phba->phwi_ctrlr;
  2847. phwi_context = phwi_ctrlr->phwi_ctxt;
  2848. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  2849. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  2850. reg = ioread32(addr);
  2851. SE_DEBUG(DBG_LVL_8, "reg =x%08x\n", reg);
  2852. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2853. if (!enabled) {
  2854. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2855. SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
  2856. iowrite32(reg, addr);
  2857. if (!phba->msix_enabled) {
  2858. eq = &phwi_context->be_eq[0].q;
  2859. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  2860. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  2861. } else {
  2862. for (i = 0; i <= phba->num_cpus; i++) {
  2863. eq = &phwi_context->be_eq[i].q;
  2864. SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
  2865. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  2866. }
  2867. }
  2868. }
  2869. return true;
  2870. }
  2871. static void hwi_disable_intr(struct beiscsi_hba *phba)
  2872. {
  2873. struct be_ctrl_info *ctrl = &phba->ctrl;
  2874. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  2875. u32 reg = ioread32(addr);
  2876. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2877. if (enabled) {
  2878. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  2879. iowrite32(reg, addr);
  2880. } else
  2881. shost_printk(KERN_WARNING, phba->shost,
  2882. "In hwi_disable_intr, Already Disabled\n");
  2883. }
  2884. static int beiscsi_init_port(struct beiscsi_hba *phba)
  2885. {
  2886. int ret;
  2887. ret = beiscsi_init_controller(phba);
  2888. if (ret < 0) {
  2889. shost_printk(KERN_ERR, phba->shost,
  2890. "beiscsi_dev_probe - Failed in"
  2891. "beiscsi_init_controller\n");
  2892. return ret;
  2893. }
  2894. ret = beiscsi_init_sgl_handle(phba);
  2895. if (ret < 0) {
  2896. shost_printk(KERN_ERR, phba->shost,
  2897. "beiscsi_dev_probe - Failed in"
  2898. "beiscsi_init_sgl_handle\n");
  2899. goto do_cleanup_ctrlr;
  2900. }
  2901. if (hba_setup_cid_tbls(phba)) {
  2902. shost_printk(KERN_ERR, phba->shost,
  2903. "Failed in hba_setup_cid_tbls\n");
  2904. kfree(phba->io_sgl_hndl_base);
  2905. kfree(phba->eh_sgl_hndl_base);
  2906. goto do_cleanup_ctrlr;
  2907. }
  2908. return ret;
  2909. do_cleanup_ctrlr:
  2910. hwi_cleanup(phba);
  2911. return ret;
  2912. }
  2913. static void hwi_purge_eq(struct beiscsi_hba *phba)
  2914. {
  2915. struct hwi_controller *phwi_ctrlr;
  2916. struct hwi_context_memory *phwi_context;
  2917. struct be_queue_info *eq;
  2918. struct be_eq_entry *eqe = NULL;
  2919. int i, eq_msix;
  2920. unsigned int num_processed;
  2921. phwi_ctrlr = phba->phwi_ctrlr;
  2922. phwi_context = phwi_ctrlr->phwi_ctxt;
  2923. if (phba->msix_enabled)
  2924. eq_msix = 1;
  2925. else
  2926. eq_msix = 0;
  2927. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  2928. eq = &phwi_context->be_eq[i].q;
  2929. eqe = queue_tail_node(eq);
  2930. num_processed = 0;
  2931. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  2932. & EQE_VALID_MASK) {
  2933. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  2934. queue_tail_inc(eq);
  2935. eqe = queue_tail_node(eq);
  2936. num_processed++;
  2937. }
  2938. if (num_processed)
  2939. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  2940. }
  2941. }
  2942. static void beiscsi_clean_port(struct beiscsi_hba *phba)
  2943. {
  2944. unsigned char mgmt_status;
  2945. mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
  2946. if (mgmt_status)
  2947. shost_printk(KERN_WARNING, phba->shost,
  2948. "mgmt_epfw_cleanup FAILED\n");
  2949. hwi_purge_eq(phba);
  2950. hwi_cleanup(phba);
  2951. kfree(phba->io_sgl_hndl_base);
  2952. kfree(phba->eh_sgl_hndl_base);
  2953. kfree(phba->cid_array);
  2954. kfree(phba->ep_array);
  2955. }
  2956. void
  2957. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  2958. struct beiscsi_offload_params *params)
  2959. {
  2960. struct wrb_handle *pwrb_handle;
  2961. struct iscsi_target_context_update_wrb *pwrb = NULL;
  2962. struct be_mem_descriptor *mem_descr;
  2963. struct beiscsi_hba *phba = beiscsi_conn->phba;
  2964. u32 doorbell = 0;
  2965. /*
  2966. * We can always use 0 here because it is reserved by libiscsi for
  2967. * login/startup related tasks.
  2968. */
  2969. pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
  2970. phba->fw_config.iscsi_cid_start));
  2971. pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
  2972. memset(pwrb, 0, sizeof(*pwrb));
  2973. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  2974. max_burst_length, pwrb, params->dw[offsetof
  2975. (struct amap_beiscsi_offload_params,
  2976. max_burst_length) / 32]);
  2977. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  2978. max_send_data_segment_length, pwrb,
  2979. params->dw[offsetof(struct amap_beiscsi_offload_params,
  2980. max_send_data_segment_length) / 32]);
  2981. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  2982. first_burst_length,
  2983. pwrb,
  2984. params->dw[offsetof(struct amap_beiscsi_offload_params,
  2985. first_burst_length) / 32]);
  2986. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
  2987. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  2988. erl) / 32] & OFFLD_PARAMS_ERL));
  2989. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
  2990. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  2991. dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
  2992. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
  2993. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  2994. hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
  2995. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
  2996. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  2997. ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
  2998. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
  2999. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3000. imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
  3001. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
  3002. pwrb,
  3003. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3004. exp_statsn) / 32] + 1));
  3005. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
  3006. 0x7);
  3007. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
  3008. pwrb, pwrb_handle->wrb_index);
  3009. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
  3010. pwrb, pwrb_handle->nxt_wrb_index);
  3011. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3012. session_state, pwrb, 0);
  3013. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
  3014. pwrb, 1);
  3015. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
  3016. pwrb, 0);
  3017. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
  3018. 0);
  3019. mem_descr = phba->init_mem;
  3020. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  3021. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3022. pad_buffer_addr_hi, pwrb,
  3023. mem_descr->mem_array[0].bus_address.u.a32.address_hi);
  3024. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3025. pad_buffer_addr_lo, pwrb,
  3026. mem_descr->mem_array[0].bus_address.u.a32.address_lo);
  3027. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
  3028. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3029. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3030. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3031. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3032. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3033. }
  3034. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3035. int *index, int *age)
  3036. {
  3037. *index = (int)itt;
  3038. if (age)
  3039. *age = conn->session->age;
  3040. }
  3041. /**
  3042. * beiscsi_alloc_pdu - allocates pdu and related resources
  3043. * @task: libiscsi task
  3044. * @opcode: opcode of pdu for task
  3045. *
  3046. * This is called with the session lock held. It will allocate
  3047. * the wrb and sgl if needed for the command. And it will prep
  3048. * the pdu's itt. beiscsi_parse_pdu will later translate
  3049. * the pdu itt to the libiscsi task itt.
  3050. */
  3051. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3052. {
  3053. struct beiscsi_io_task *io_task = task->dd_data;
  3054. struct iscsi_conn *conn = task->conn;
  3055. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3056. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3057. struct hwi_wrb_context *pwrb_context;
  3058. struct hwi_controller *phwi_ctrlr;
  3059. itt_t itt;
  3060. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3061. dma_addr_t paddr;
  3062. io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
  3063. GFP_KERNEL, &paddr);
  3064. if (!io_task->cmd_bhs)
  3065. return -ENOMEM;
  3066. io_task->bhs_pa.u.a64.address = paddr;
  3067. io_task->libiscsi_itt = (itt_t)task->itt;
  3068. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3069. beiscsi_conn->beiscsi_conn_cid -
  3070. phba->fw_config.iscsi_cid_start
  3071. );
  3072. io_task->conn = beiscsi_conn;
  3073. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3074. task->hdr_max = sizeof(struct be_cmd_bhs);
  3075. if (task->sc) {
  3076. spin_lock(&phba->io_sgl_lock);
  3077. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3078. spin_unlock(&phba->io_sgl_lock);
  3079. if (!io_task->psgl_handle)
  3080. goto free_hndls;
  3081. } else {
  3082. io_task->scsi_cmnd = NULL;
  3083. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3084. if (!beiscsi_conn->login_in_progress) {
  3085. spin_lock(&phba->mgmt_sgl_lock);
  3086. io_task->psgl_handle = (struct sgl_handle *)
  3087. alloc_mgmt_sgl_handle(phba);
  3088. spin_unlock(&phba->mgmt_sgl_lock);
  3089. if (!io_task->psgl_handle)
  3090. goto free_hndls;
  3091. beiscsi_conn->login_in_progress = 1;
  3092. beiscsi_conn->plogin_sgl_handle =
  3093. io_task->psgl_handle;
  3094. } else {
  3095. io_task->psgl_handle =
  3096. beiscsi_conn->plogin_sgl_handle;
  3097. }
  3098. } else {
  3099. spin_lock(&phba->mgmt_sgl_lock);
  3100. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3101. spin_unlock(&phba->mgmt_sgl_lock);
  3102. if (!io_task->psgl_handle)
  3103. goto free_hndls;
  3104. }
  3105. }
  3106. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  3107. wrb_index << 16) | (unsigned int)
  3108. (io_task->psgl_handle->sgl_index));
  3109. io_task->pwrb_handle->pio_handle = task;
  3110. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  3111. return 0;
  3112. free_hndls:
  3113. phwi_ctrlr = phba->phwi_ctrlr;
  3114. pwrb_context = &phwi_ctrlr->wrb_context[
  3115. beiscsi_conn->beiscsi_conn_cid -
  3116. phba->fw_config.iscsi_cid_start];
  3117. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3118. io_task->pwrb_handle = NULL;
  3119. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3120. io_task->bhs_pa.u.a64.address);
  3121. SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
  3122. return -ENOMEM;
  3123. }
  3124. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3125. {
  3126. struct beiscsi_io_task *io_task = task->dd_data;
  3127. struct iscsi_conn *conn = task->conn;
  3128. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3129. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3130. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3131. struct hwi_wrb_context *pwrb_context;
  3132. struct hwi_controller *phwi_ctrlr;
  3133. phwi_ctrlr = phba->phwi_ctrlr;
  3134. pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
  3135. - phba->fw_config.iscsi_cid_start];
  3136. if (io_task->pwrb_handle) {
  3137. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3138. io_task->pwrb_handle = NULL;
  3139. }
  3140. if (io_task->cmd_bhs) {
  3141. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3142. io_task->bhs_pa.u.a64.address);
  3143. }
  3144. if (task->sc) {
  3145. if (io_task->psgl_handle) {
  3146. spin_lock(&phba->io_sgl_lock);
  3147. free_io_sgl_handle(phba, io_task->psgl_handle);
  3148. spin_unlock(&phba->io_sgl_lock);
  3149. io_task->psgl_handle = NULL;
  3150. }
  3151. } else {
  3152. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN)
  3153. return;
  3154. if (io_task->psgl_handle) {
  3155. spin_lock(&phba->mgmt_sgl_lock);
  3156. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3157. spin_unlock(&phba->mgmt_sgl_lock);
  3158. io_task->psgl_handle = NULL;
  3159. }
  3160. }
  3161. }
  3162. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  3163. unsigned int num_sg, unsigned int xferlen,
  3164. unsigned int writedir)
  3165. {
  3166. struct beiscsi_io_task *io_task = task->dd_data;
  3167. struct iscsi_conn *conn = task->conn;
  3168. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3169. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3170. struct iscsi_wrb *pwrb = NULL;
  3171. unsigned int doorbell = 0;
  3172. pwrb = io_task->pwrb_handle->pwrb;
  3173. io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
  3174. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  3175. if (writedir) {
  3176. memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
  3177. AMAP_SET_BITS(struct amap_pdu_data_out, itt,
  3178. &io_task->cmd_bhs->iscsi_data_pdu,
  3179. (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
  3180. AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
  3181. &io_task->cmd_bhs->iscsi_data_pdu,
  3182. ISCSI_OPCODE_SCSI_DATA_OUT);
  3183. AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
  3184. &io_task->cmd_bhs->iscsi_data_pdu, 1);
  3185. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3186. INI_WR_CMD);
  3187. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  3188. } else {
  3189. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3190. INI_RD_CMD);
  3191. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  3192. }
  3193. memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
  3194. dw[offsetof(struct amap_pdu_data_out, lun) / 32],
  3195. io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
  3196. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  3197. cpu_to_be16((unsigned short)io_task->cmd_bhs->iscsi_hdr.
  3198. lun[0]));
  3199. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  3200. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3201. io_task->pwrb_handle->wrb_index);
  3202. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3203. be32_to_cpu(task->cmdsn));
  3204. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3205. io_task->psgl_handle->sgl_index);
  3206. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  3207. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3208. io_task->pwrb_handle->nxt_wrb_index);
  3209. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3210. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3211. doorbell |= (io_task->pwrb_handle->wrb_index &
  3212. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3213. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3214. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3215. return 0;
  3216. }
  3217. static int beiscsi_mtask(struct iscsi_task *task)
  3218. {
  3219. struct beiscsi_io_task *io_task = task->dd_data;
  3220. struct iscsi_conn *conn = task->conn;
  3221. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3222. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3223. struct iscsi_wrb *pwrb = NULL;
  3224. unsigned int doorbell = 0;
  3225. unsigned int cid;
  3226. cid = beiscsi_conn->beiscsi_conn_cid;
  3227. pwrb = io_task->pwrb_handle->pwrb;
  3228. memset(pwrb, 0, sizeof(*pwrb));
  3229. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3230. be32_to_cpu(task->cmdsn));
  3231. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3232. io_task->pwrb_handle->wrb_index);
  3233. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3234. io_task->psgl_handle->sgl_index);
  3235. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  3236. case ISCSI_OP_LOGIN:
  3237. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3238. TGT_DM_CMD);
  3239. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3240. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  3241. hwi_write_buffer(pwrb, task);
  3242. break;
  3243. case ISCSI_OP_NOOP_OUT:
  3244. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3245. INI_RD_CMD);
  3246. if (task->hdr->ttt == ISCSI_RESERVED_TAG)
  3247. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3248. else
  3249. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
  3250. hwi_write_buffer(pwrb, task);
  3251. break;
  3252. case ISCSI_OP_TEXT:
  3253. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3254. TGT_DM_CMD);
  3255. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3256. hwi_write_buffer(pwrb, task);
  3257. break;
  3258. case ISCSI_OP_SCSI_TMFUNC:
  3259. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3260. INI_TMF_CMD);
  3261. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3262. hwi_write_buffer(pwrb, task);
  3263. break;
  3264. case ISCSI_OP_LOGOUT:
  3265. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3266. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3267. HWH_TYPE_LOGOUT);
  3268. hwi_write_buffer(pwrb, task);
  3269. break;
  3270. default:
  3271. SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
  3272. task->hdr->opcode & ISCSI_OPCODE_MASK);
  3273. return -EINVAL;
  3274. }
  3275. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  3276. task->data_count);
  3277. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3278. io_task->pwrb_handle->nxt_wrb_index);
  3279. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3280. doorbell |= cid & DB_WRB_POST_CID_MASK;
  3281. doorbell |= (io_task->pwrb_handle->wrb_index &
  3282. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3283. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3284. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3285. return 0;
  3286. }
  3287. static int beiscsi_task_xmit(struct iscsi_task *task)
  3288. {
  3289. struct beiscsi_io_task *io_task = task->dd_data;
  3290. struct scsi_cmnd *sc = task->sc;
  3291. struct scatterlist *sg;
  3292. int num_sg;
  3293. unsigned int writedir = 0, xferlen = 0;
  3294. if (!sc)
  3295. return beiscsi_mtask(task);
  3296. io_task->scsi_cmnd = sc;
  3297. num_sg = scsi_dma_map(sc);
  3298. if (num_sg < 0) {
  3299. SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
  3300. return num_sg;
  3301. }
  3302. SE_DEBUG(DBG_LVL_4, "xferlen=0x%08x scmd=%p num_sg=%d sernum=%lu\n",
  3303. (scsi_bufflen(sc)), sc, num_sg, sc->serial_number);
  3304. xferlen = scsi_bufflen(sc);
  3305. sg = scsi_sglist(sc);
  3306. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  3307. writedir = 1;
  3308. SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
  3309. task->imm_count);
  3310. } else
  3311. writedir = 0;
  3312. return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
  3313. }
  3314. static void beiscsi_remove(struct pci_dev *pcidev)
  3315. {
  3316. struct beiscsi_hba *phba = NULL;
  3317. struct hwi_controller *phwi_ctrlr;
  3318. struct hwi_context_memory *phwi_context;
  3319. struct be_eq_obj *pbe_eq;
  3320. unsigned int i, msix_vec;
  3321. phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
  3322. if (!phba) {
  3323. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  3324. return;
  3325. }
  3326. phwi_ctrlr = phba->phwi_ctrlr;
  3327. phwi_context = phwi_ctrlr->phwi_ctxt;
  3328. hwi_disable_intr(phba);
  3329. if (phba->msix_enabled) {
  3330. for (i = 0; i <= phba->num_cpus; i++) {
  3331. msix_vec = phba->msix_entries[i].vector;
  3332. free_irq(msix_vec, &phwi_context->be_eq[i]);
  3333. }
  3334. } else
  3335. if (phba->pcidev->irq)
  3336. free_irq(phba->pcidev->irq, phba);
  3337. pci_disable_msix(phba->pcidev);
  3338. destroy_workqueue(phba->wq);
  3339. if (blk_iopoll_enabled)
  3340. for (i = 0; i < phba->num_cpus; i++) {
  3341. pbe_eq = &phwi_context->be_eq[i];
  3342. blk_iopoll_disable(&pbe_eq->iopoll);
  3343. }
  3344. beiscsi_clean_port(phba);
  3345. beiscsi_free_mem(phba);
  3346. beiscsi_unmap_pci_function(phba);
  3347. pci_free_consistent(phba->pcidev,
  3348. phba->ctrl.mbox_mem_alloced.size,
  3349. phba->ctrl.mbox_mem_alloced.va,
  3350. phba->ctrl.mbox_mem_alloced.dma);
  3351. iscsi_host_remove(phba->shost);
  3352. pci_dev_put(phba->pcidev);
  3353. iscsi_host_free(phba->shost);
  3354. }
  3355. static void beiscsi_msix_enable(struct beiscsi_hba *phba)
  3356. {
  3357. int i, status;
  3358. for (i = 0; i <= phba->num_cpus; i++)
  3359. phba->msix_entries[i].entry = i;
  3360. status = pci_enable_msix(phba->pcidev, phba->msix_entries,
  3361. (phba->num_cpus + 1));
  3362. if (!status)
  3363. phba->msix_enabled = true;
  3364. return;
  3365. }
  3366. static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
  3367. const struct pci_device_id *id)
  3368. {
  3369. struct beiscsi_hba *phba = NULL;
  3370. struct hwi_controller *phwi_ctrlr;
  3371. struct hwi_context_memory *phwi_context;
  3372. struct be_eq_obj *pbe_eq;
  3373. int ret, msix_vec, num_cpus, i;
  3374. ret = beiscsi_enable_pci(pcidev);
  3375. if (ret < 0) {
  3376. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3377. " Failed to enable pci device\n");
  3378. return ret;
  3379. }
  3380. phba = beiscsi_hba_alloc(pcidev);
  3381. if (!phba) {
  3382. dev_err(&pcidev->dev, "beiscsi_dev_probe-"
  3383. " Failed in beiscsi_hba_alloc\n");
  3384. goto disable_pci;
  3385. }
  3386. switch (pcidev->device) {
  3387. case BE_DEVICE_ID1:
  3388. case OC_DEVICE_ID1:
  3389. case OC_DEVICE_ID2:
  3390. phba->generation = BE_GEN2;
  3391. break;
  3392. case BE_DEVICE_ID2:
  3393. case OC_DEVICE_ID3:
  3394. phba->generation = BE_GEN3;
  3395. break;
  3396. default:
  3397. phba->generation = 0;
  3398. }
  3399. if (enable_msix)
  3400. num_cpus = find_num_cpus();
  3401. else
  3402. num_cpus = 1;
  3403. phba->num_cpus = num_cpus;
  3404. SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
  3405. if (enable_msix)
  3406. beiscsi_msix_enable(phba);
  3407. ret = be_ctrl_init(phba, pcidev);
  3408. if (ret) {
  3409. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3410. "Failed in be_ctrl_init\n");
  3411. goto hba_free;
  3412. }
  3413. spin_lock_init(&phba->io_sgl_lock);
  3414. spin_lock_init(&phba->mgmt_sgl_lock);
  3415. spin_lock_init(&phba->isr_lock);
  3416. ret = mgmt_get_fw_config(&phba->ctrl, phba);
  3417. if (ret != 0) {
  3418. shost_printk(KERN_ERR, phba->shost,
  3419. "Error getting fw config\n");
  3420. goto free_port;
  3421. }
  3422. phba->shost->max_id = phba->fw_config.iscsi_cid_count;
  3423. beiscsi_get_params(phba);
  3424. phba->shost->can_queue = phba->params.ios_per_ctrl;
  3425. ret = beiscsi_init_port(phba);
  3426. if (ret < 0) {
  3427. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3428. "Failed in beiscsi_init_port\n");
  3429. goto free_port;
  3430. }
  3431. for (i = 0; i < MAX_MCC_CMD ; i++) {
  3432. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  3433. phba->ctrl.mcc_tag[i] = i + 1;
  3434. phba->ctrl.mcc_numtag[i + 1] = 0;
  3435. phba->ctrl.mcc_tag_available++;
  3436. }
  3437. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  3438. snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
  3439. phba->shost->host_no);
  3440. phba->wq = create_workqueue(phba->wq_name);
  3441. if (!phba->wq) {
  3442. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3443. "Failed to allocate work queue\n");
  3444. goto free_twq;
  3445. }
  3446. INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
  3447. phwi_ctrlr = phba->phwi_ctrlr;
  3448. phwi_context = phwi_ctrlr->phwi_ctxt;
  3449. if (blk_iopoll_enabled) {
  3450. for (i = 0; i < phba->num_cpus; i++) {
  3451. pbe_eq = &phwi_context->be_eq[i];
  3452. blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
  3453. be_iopoll);
  3454. blk_iopoll_enable(&pbe_eq->iopoll);
  3455. }
  3456. }
  3457. ret = beiscsi_init_irqs(phba);
  3458. if (ret < 0) {
  3459. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3460. "Failed to beiscsi_init_irqs\n");
  3461. goto free_blkenbld;
  3462. }
  3463. ret = hwi_enable_intr(phba);
  3464. if (ret < 0) {
  3465. shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
  3466. "Failed to hwi_enable_intr\n");
  3467. goto free_ctrlr;
  3468. }
  3469. SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
  3470. return 0;
  3471. free_ctrlr:
  3472. if (phba->msix_enabled) {
  3473. for (i = 0; i <= phba->num_cpus; i++) {
  3474. msix_vec = phba->msix_entries[i].vector;
  3475. free_irq(msix_vec, &phwi_context->be_eq[i]);
  3476. }
  3477. } else
  3478. if (phba->pcidev->irq)
  3479. free_irq(phba->pcidev->irq, phba);
  3480. pci_disable_msix(phba->pcidev);
  3481. free_blkenbld:
  3482. destroy_workqueue(phba->wq);
  3483. if (blk_iopoll_enabled)
  3484. for (i = 0; i < phba->num_cpus; i++) {
  3485. pbe_eq = &phwi_context->be_eq[i];
  3486. blk_iopoll_disable(&pbe_eq->iopoll);
  3487. }
  3488. free_twq:
  3489. beiscsi_clean_port(phba);
  3490. beiscsi_free_mem(phba);
  3491. free_port:
  3492. pci_free_consistent(phba->pcidev,
  3493. phba->ctrl.mbox_mem_alloced.size,
  3494. phba->ctrl.mbox_mem_alloced.va,
  3495. phba->ctrl.mbox_mem_alloced.dma);
  3496. beiscsi_unmap_pci_function(phba);
  3497. hba_free:
  3498. iscsi_host_remove(phba->shost);
  3499. pci_dev_put(phba->pcidev);
  3500. iscsi_host_free(phba->shost);
  3501. disable_pci:
  3502. pci_disable_device(pcidev);
  3503. return ret;
  3504. }
  3505. struct iscsi_transport beiscsi_iscsi_transport = {
  3506. .owner = THIS_MODULE,
  3507. .name = DRV_NAME,
  3508. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  3509. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  3510. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  3511. ISCSI_MAX_XMIT_DLENGTH |
  3512. ISCSI_HDRDGST_EN |
  3513. ISCSI_DATADGST_EN |
  3514. ISCSI_INITIAL_R2T_EN |
  3515. ISCSI_MAX_R2T |
  3516. ISCSI_IMM_DATA_EN |
  3517. ISCSI_FIRST_BURST |
  3518. ISCSI_MAX_BURST |
  3519. ISCSI_PDU_INORDER_EN |
  3520. ISCSI_DATASEQ_INORDER_EN |
  3521. ISCSI_ERL |
  3522. ISCSI_CONN_PORT |
  3523. ISCSI_CONN_ADDRESS |
  3524. ISCSI_EXP_STATSN |
  3525. ISCSI_PERSISTENT_PORT |
  3526. ISCSI_PERSISTENT_ADDRESS |
  3527. ISCSI_TARGET_NAME | ISCSI_TPGT |
  3528. ISCSI_USERNAME | ISCSI_PASSWORD |
  3529. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  3530. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  3531. ISCSI_LU_RESET_TMO |
  3532. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  3533. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  3534. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  3535. ISCSI_HOST_INITIATOR_NAME,
  3536. .create_session = beiscsi_session_create,
  3537. .destroy_session = beiscsi_session_destroy,
  3538. .create_conn = beiscsi_conn_create,
  3539. .bind_conn = beiscsi_conn_bind,
  3540. .destroy_conn = iscsi_conn_teardown,
  3541. .set_param = beiscsi_set_param,
  3542. .get_conn_param = beiscsi_conn_get_param,
  3543. .get_session_param = iscsi_session_get_param,
  3544. .get_host_param = beiscsi_get_host_param,
  3545. .start_conn = beiscsi_conn_start,
  3546. .stop_conn = iscsi_conn_stop,
  3547. .send_pdu = iscsi_conn_send_pdu,
  3548. .xmit_task = beiscsi_task_xmit,
  3549. .cleanup_task = beiscsi_cleanup_task,
  3550. .alloc_pdu = beiscsi_alloc_pdu,
  3551. .parse_pdu_itt = beiscsi_parse_pdu,
  3552. .get_stats = beiscsi_conn_get_stats,
  3553. .ep_connect = beiscsi_ep_connect,
  3554. .ep_poll = beiscsi_ep_poll,
  3555. .ep_disconnect = beiscsi_ep_disconnect,
  3556. .session_recovery_timedout = iscsi_session_recovery_timedout,
  3557. };
  3558. static struct pci_driver beiscsi_pci_driver = {
  3559. .name = DRV_NAME,
  3560. .probe = beiscsi_dev_probe,
  3561. .remove = beiscsi_remove,
  3562. .id_table = beiscsi_pci_id_table
  3563. };
  3564. static int __init beiscsi_module_init(void)
  3565. {
  3566. int ret;
  3567. beiscsi_scsi_transport =
  3568. iscsi_register_transport(&beiscsi_iscsi_transport);
  3569. if (!beiscsi_scsi_transport) {
  3570. SE_DEBUG(DBG_LVL_1,
  3571. "beiscsi_module_init - Unable to register beiscsi"
  3572. "transport.\n");
  3573. return -ENOMEM;
  3574. }
  3575. SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
  3576. &beiscsi_iscsi_transport);
  3577. ret = pci_register_driver(&beiscsi_pci_driver);
  3578. if (ret) {
  3579. SE_DEBUG(DBG_LVL_1,
  3580. "beiscsi_module_init - Unable to register"
  3581. "beiscsi pci driver.\n");
  3582. goto unregister_iscsi_transport;
  3583. }
  3584. return 0;
  3585. unregister_iscsi_transport:
  3586. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3587. return ret;
  3588. }
  3589. static void __exit beiscsi_module_exit(void)
  3590. {
  3591. pci_unregister_driver(&beiscsi_pci_driver);
  3592. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  3593. }
  3594. module_init(beiscsi_module_init);
  3595. module_exit(beiscsi_module_exit);