ql4_os.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/iscsi_boot_sysfs.h>
  11. #include <scsi/scsi_tcq.h>
  12. #include <scsi/scsicam.h>
  13. #include "ql4_def.h"
  14. #include "ql4_version.h"
  15. #include "ql4_glbl.h"
  16. #include "ql4_dbg.h"
  17. #include "ql4_inline.h"
  18. /*
  19. * Driver version
  20. */
  21. static char qla4xxx_version_str[40];
  22. /*
  23. * SRB allocation cache
  24. */
  25. static struct kmem_cache *srb_cachep;
  26. /*
  27. * Module parameter information and variables
  28. */
  29. int ql4xdontresethba = 0;
  30. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  31. MODULE_PARM_DESC(ql4xdontresethba,
  32. "Don't reset the HBA for driver recovery \n"
  33. " 0 - It will reset HBA (Default)\n"
  34. " 1 - It will NOT reset HBA");
  35. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  36. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  37. MODULE_PARM_DESC(ql4xextended_error_logging,
  38. "Option to enable extended error logging, "
  39. "Default is 0 - no logging, 1 - debug logging");
  40. int ql4xenablemsix = 1;
  41. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  42. MODULE_PARM_DESC(ql4xenablemsix,
  43. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  44. " 0 = enable INTx interrupt mechanism.\n"
  45. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  46. " 2 = enable MSI interrupt mechanism.");
  47. #define QL4_DEF_QDEPTH 32
  48. static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  49. module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  50. MODULE_PARM_DESC(ql4xmaxqdepth,
  51. "Maximum queue depth to report for target devices.\n"
  52. " Default: 32.");
  53. static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  54. module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  55. MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  56. "Target Session Recovery Timeout.\n"
  57. " Default: 30 sec.");
  58. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
  59. /*
  60. * SCSI host template entry points
  61. */
  62. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  63. /*
  64. * iSCSI template entry points
  65. */
  66. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  67. enum iscsi_param param, char *buf);
  68. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  69. enum iscsi_host_param param, char *buf);
  70. static int qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data,
  71. int count);
  72. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  73. enum iscsi_param_type param_type,
  74. int param, char *buf);
  75. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  76. static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
  77. struct sockaddr *dst_addr,
  78. int non_blocking);
  79. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
  80. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
  81. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  82. enum iscsi_param param, char *buf);
  83. static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
  84. static struct iscsi_cls_conn *
  85. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
  86. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  87. struct iscsi_cls_conn *cls_conn,
  88. uint64_t transport_fd, int is_leading);
  89. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
  90. static struct iscsi_cls_session *
  91. qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  92. uint16_t qdepth, uint32_t initial_cmdsn);
  93. static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
  94. static void qla4xxx_task_work(struct work_struct *wdata);
  95. static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
  96. static int qla4xxx_task_xmit(struct iscsi_task *);
  97. static void qla4xxx_task_cleanup(struct iscsi_task *);
  98. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
  99. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  100. struct iscsi_stats *stats);
  101. /*
  102. * SCSI host template entry points
  103. */
  104. static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  105. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  106. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  107. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  108. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  109. static int qla4xxx_slave_alloc(struct scsi_device *device);
  110. static int qla4xxx_slave_configure(struct scsi_device *device);
  111. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  112. static mode_t ql4_attr_is_visible(int param_type, int param);
  113. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  114. QLA82XX_LEGACY_INTR_CONFIG;
  115. static struct scsi_host_template qla4xxx_driver_template = {
  116. .module = THIS_MODULE,
  117. .name = DRIVER_NAME,
  118. .proc_name = DRIVER_NAME,
  119. .queuecommand = qla4xxx_queuecommand,
  120. .eh_abort_handler = qla4xxx_eh_abort,
  121. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  122. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  123. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  124. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  125. .slave_configure = qla4xxx_slave_configure,
  126. .slave_alloc = qla4xxx_slave_alloc,
  127. .slave_destroy = qla4xxx_slave_destroy,
  128. .this_id = -1,
  129. .cmd_per_lun = 3,
  130. .use_clustering = ENABLE_CLUSTERING,
  131. .sg_tablesize = SG_ALL,
  132. .max_sectors = 0xFFFF,
  133. .shost_attrs = qla4xxx_host_attrs,
  134. .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
  135. };
  136. static struct iscsi_transport qla4xxx_iscsi_transport = {
  137. .owner = THIS_MODULE,
  138. .name = DRIVER_NAME,
  139. .caps = CAP_TEXT_NEGO |
  140. CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
  141. CAP_DATADGST | CAP_LOGIN_OFFLOAD |
  142. CAP_MULTI_R2T,
  143. .attr_is_visible = ql4_attr_is_visible,
  144. .create_session = qla4xxx_session_create,
  145. .destroy_session = qla4xxx_session_destroy,
  146. .start_conn = qla4xxx_conn_start,
  147. .create_conn = qla4xxx_conn_create,
  148. .bind_conn = qla4xxx_conn_bind,
  149. .stop_conn = iscsi_conn_stop,
  150. .destroy_conn = qla4xxx_conn_destroy,
  151. .set_param = iscsi_set_param,
  152. .get_conn_param = qla4xxx_conn_get_param,
  153. .get_session_param = iscsi_session_get_param,
  154. .get_ep_param = qla4xxx_get_ep_param,
  155. .ep_connect = qla4xxx_ep_connect,
  156. .ep_poll = qla4xxx_ep_poll,
  157. .ep_disconnect = qla4xxx_ep_disconnect,
  158. .get_stats = qla4xxx_conn_get_stats,
  159. .send_pdu = iscsi_conn_send_pdu,
  160. .xmit_task = qla4xxx_task_xmit,
  161. .cleanup_task = qla4xxx_task_cleanup,
  162. .alloc_pdu = qla4xxx_alloc_pdu,
  163. .get_host_param = qla4xxx_host_get_param,
  164. .set_iface_param = qla4xxx_iface_set_param,
  165. .get_iface_param = qla4xxx_get_iface_param,
  166. .bsg_request = qla4xxx_bsg_request,
  167. };
  168. static struct scsi_transport_template *qla4xxx_scsi_transport;
  169. static mode_t ql4_attr_is_visible(int param_type, int param)
  170. {
  171. switch (param_type) {
  172. case ISCSI_HOST_PARAM:
  173. switch (param) {
  174. case ISCSI_HOST_PARAM_HWADDRESS:
  175. case ISCSI_HOST_PARAM_IPADDRESS:
  176. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  177. return S_IRUGO;
  178. default:
  179. return 0;
  180. }
  181. case ISCSI_PARAM:
  182. switch (param) {
  183. case ISCSI_PARAM_CONN_ADDRESS:
  184. case ISCSI_PARAM_CONN_PORT:
  185. case ISCSI_PARAM_TARGET_NAME:
  186. case ISCSI_PARAM_TPGT:
  187. case ISCSI_PARAM_TARGET_ALIAS:
  188. case ISCSI_PARAM_MAX_BURST:
  189. case ISCSI_PARAM_MAX_R2T:
  190. case ISCSI_PARAM_FIRST_BURST:
  191. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  192. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  193. return S_IRUGO;
  194. default:
  195. return 0;
  196. }
  197. case ISCSI_NET_PARAM:
  198. switch (param) {
  199. case ISCSI_NET_PARAM_IPV4_ADDR:
  200. case ISCSI_NET_PARAM_IPV4_SUBNET:
  201. case ISCSI_NET_PARAM_IPV4_GW:
  202. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  203. case ISCSI_NET_PARAM_IFACE_ENABLE:
  204. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  205. case ISCSI_NET_PARAM_IPV6_ADDR:
  206. case ISCSI_NET_PARAM_IPV6_ROUTER:
  207. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  208. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  209. case ISCSI_NET_PARAM_VLAN_ID:
  210. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  211. case ISCSI_NET_PARAM_VLAN_ENABLED:
  212. return S_IRUGO;
  213. default:
  214. return 0;
  215. }
  216. }
  217. return 0;
  218. }
  219. static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  220. enum iscsi_param_type param_type,
  221. int param, char *buf)
  222. {
  223. struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  224. struct scsi_qla_host *ha = to_qla_host(shost);
  225. int len = -ENOSYS;
  226. if (param_type != ISCSI_NET_PARAM)
  227. return -ENOSYS;
  228. switch (param) {
  229. case ISCSI_NET_PARAM_IPV4_ADDR:
  230. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  231. break;
  232. case ISCSI_NET_PARAM_IPV4_SUBNET:
  233. len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
  234. break;
  235. case ISCSI_NET_PARAM_IPV4_GW:
  236. len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
  237. break;
  238. case ISCSI_NET_PARAM_IFACE_ENABLE:
  239. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  240. len = sprintf(buf, "%s\n",
  241. (ha->ip_config.ipv4_options &
  242. IPOPT_IPV4_PROTOCOL_ENABLE) ?
  243. "enabled" : "disabled");
  244. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  245. len = sprintf(buf, "%s\n",
  246. (ha->ip_config.ipv6_options &
  247. IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  248. "enabled" : "disabled");
  249. break;
  250. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  251. len = sprintf(buf, "%s\n",
  252. (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  253. "dhcp" : "static");
  254. break;
  255. case ISCSI_NET_PARAM_IPV6_ADDR:
  256. if (iface->iface_num == 0)
  257. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
  258. if (iface->iface_num == 1)
  259. len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
  260. break;
  261. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  262. len = sprintf(buf, "%pI6\n",
  263. &ha->ip_config.ipv6_link_local_addr);
  264. break;
  265. case ISCSI_NET_PARAM_IPV6_ROUTER:
  266. len = sprintf(buf, "%pI6\n",
  267. &ha->ip_config.ipv6_default_router_addr);
  268. break;
  269. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  270. len = sprintf(buf, "%s\n",
  271. (ha->ip_config.ipv6_addl_options &
  272. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  273. "nd" : "static");
  274. break;
  275. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  276. len = sprintf(buf, "%s\n",
  277. (ha->ip_config.ipv6_addl_options &
  278. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  279. "auto" : "static");
  280. break;
  281. case ISCSI_NET_PARAM_VLAN_ID:
  282. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  283. len = sprintf(buf, "%d\n",
  284. (ha->ip_config.ipv4_vlan_tag &
  285. ISCSI_MAX_VLAN_ID));
  286. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  287. len = sprintf(buf, "%d\n",
  288. (ha->ip_config.ipv6_vlan_tag &
  289. ISCSI_MAX_VLAN_ID));
  290. break;
  291. case ISCSI_NET_PARAM_VLAN_PRIORITY:
  292. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  293. len = sprintf(buf, "%d\n",
  294. ((ha->ip_config.ipv4_vlan_tag >> 13) &
  295. ISCSI_MAX_VLAN_PRIORITY));
  296. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  297. len = sprintf(buf, "%d\n",
  298. ((ha->ip_config.ipv6_vlan_tag >> 13) &
  299. ISCSI_MAX_VLAN_PRIORITY));
  300. break;
  301. case ISCSI_NET_PARAM_VLAN_ENABLED:
  302. if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  303. len = sprintf(buf, "%s\n",
  304. (ha->ip_config.ipv4_options &
  305. IPOPT_VLAN_TAGGING_ENABLE) ?
  306. "enabled" : "disabled");
  307. else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  308. len = sprintf(buf, "%s\n",
  309. (ha->ip_config.ipv6_options &
  310. IPV6_OPT_VLAN_TAGGING_ENABLE) ?
  311. "enabled" : "disabled");
  312. break;
  313. default:
  314. len = -ENOSYS;
  315. }
  316. return len;
  317. }
  318. static struct iscsi_endpoint *
  319. qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  320. int non_blocking)
  321. {
  322. int ret;
  323. struct iscsi_endpoint *ep;
  324. struct qla_endpoint *qla_ep;
  325. struct scsi_qla_host *ha;
  326. struct sockaddr_in *addr;
  327. struct sockaddr_in6 *addr6;
  328. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  329. if (!shost) {
  330. ret = -ENXIO;
  331. printk(KERN_ERR "%s: shost is NULL\n",
  332. __func__);
  333. return ERR_PTR(ret);
  334. }
  335. ha = iscsi_host_priv(shost);
  336. ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
  337. if (!ep) {
  338. ret = -ENOMEM;
  339. return ERR_PTR(ret);
  340. }
  341. qla_ep = ep->dd_data;
  342. memset(qla_ep, 0, sizeof(struct qla_endpoint));
  343. if (dst_addr->sa_family == AF_INET) {
  344. memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
  345. addr = (struct sockaddr_in *)&qla_ep->dst_addr;
  346. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
  347. (char *)&addr->sin_addr));
  348. } else if (dst_addr->sa_family == AF_INET6) {
  349. memcpy(&qla_ep->dst_addr, dst_addr,
  350. sizeof(struct sockaddr_in6));
  351. addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
  352. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
  353. (char *)&addr6->sin6_addr));
  354. }
  355. qla_ep->host = shost;
  356. return ep;
  357. }
  358. static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  359. {
  360. struct qla_endpoint *qla_ep;
  361. struct scsi_qla_host *ha;
  362. int ret = 0;
  363. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  364. qla_ep = ep->dd_data;
  365. ha = to_qla_host(qla_ep->host);
  366. if (adapter_up(ha))
  367. ret = 1;
  368. return ret;
  369. }
  370. static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
  371. {
  372. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  373. iscsi_destroy_endpoint(ep);
  374. }
  375. static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  376. enum iscsi_param param,
  377. char *buf)
  378. {
  379. struct qla_endpoint *qla_ep = ep->dd_data;
  380. struct sockaddr *dst_addr;
  381. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  382. switch (param) {
  383. case ISCSI_PARAM_CONN_PORT:
  384. case ISCSI_PARAM_CONN_ADDRESS:
  385. if (!qla_ep)
  386. return -ENOTCONN;
  387. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  388. if (!dst_addr)
  389. return -ENOTCONN;
  390. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  391. &qla_ep->dst_addr, param, buf);
  392. default:
  393. return -ENOSYS;
  394. }
  395. }
  396. static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  397. struct iscsi_stats *stats)
  398. {
  399. struct iscsi_session *sess;
  400. struct iscsi_cls_session *cls_sess;
  401. struct ddb_entry *ddb_entry;
  402. struct scsi_qla_host *ha;
  403. struct ql_iscsi_stats *ql_iscsi_stats;
  404. int stats_size;
  405. int ret;
  406. dma_addr_t iscsi_stats_dma;
  407. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  408. cls_sess = iscsi_conn_to_session(cls_conn);
  409. sess = cls_sess->dd_data;
  410. ddb_entry = sess->dd_data;
  411. ha = ddb_entry->ha;
  412. stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
  413. /* Allocate memory */
  414. ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
  415. &iscsi_stats_dma, GFP_KERNEL);
  416. if (!ql_iscsi_stats) {
  417. ql4_printk(KERN_ERR, ha,
  418. "Unable to allocate memory for iscsi stats\n");
  419. goto exit_get_stats;
  420. }
  421. ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
  422. iscsi_stats_dma);
  423. if (ret != QLA_SUCCESS) {
  424. ql4_printk(KERN_ERR, ha,
  425. "Unable to retreive iscsi stats\n");
  426. goto free_stats;
  427. }
  428. /* octets */
  429. stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
  430. stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
  431. /* xmit pdus */
  432. stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
  433. stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
  434. stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
  435. stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
  436. stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
  437. stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
  438. stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
  439. stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
  440. /* recv pdus */
  441. stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
  442. stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
  443. stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
  444. stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
  445. stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
  446. stats->logoutrsp_pdus =
  447. le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
  448. stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
  449. stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
  450. stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
  451. free_stats:
  452. dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
  453. iscsi_stats_dma);
  454. exit_get_stats:
  455. return;
  456. }
  457. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  458. {
  459. struct iscsi_cls_session *session;
  460. struct iscsi_session *sess;
  461. unsigned long flags;
  462. enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
  463. session = starget_to_session(scsi_target(sc->device));
  464. sess = session->dd_data;
  465. spin_lock_irqsave(&session->lock, flags);
  466. if (session->state == ISCSI_SESSION_FAILED)
  467. ret = BLK_EH_RESET_TIMER;
  468. spin_unlock_irqrestore(&session->lock, flags);
  469. return ret;
  470. }
  471. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  472. enum iscsi_host_param param, char *buf)
  473. {
  474. struct scsi_qla_host *ha = to_qla_host(shost);
  475. int len;
  476. switch (param) {
  477. case ISCSI_HOST_PARAM_HWADDRESS:
  478. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  479. break;
  480. case ISCSI_HOST_PARAM_IPADDRESS:
  481. len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
  482. break;
  483. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  484. len = sprintf(buf, "%s\n", ha->name_string);
  485. break;
  486. default:
  487. return -ENOSYS;
  488. }
  489. return len;
  490. }
  491. static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  492. {
  493. if (ha->iface_ipv4)
  494. return;
  495. /* IPv4 */
  496. ha->iface_ipv4 = iscsi_create_iface(ha->host,
  497. &qla4xxx_iscsi_transport,
  498. ISCSI_IFACE_TYPE_IPV4, 0, 0);
  499. if (!ha->iface_ipv4)
  500. ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  501. "iface0.\n");
  502. }
  503. static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  504. {
  505. if (!ha->iface_ipv6_0)
  506. /* IPv6 iface-0 */
  507. ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  508. &qla4xxx_iscsi_transport,
  509. ISCSI_IFACE_TYPE_IPV6, 0,
  510. 0);
  511. if (!ha->iface_ipv6_0)
  512. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  513. "iface0.\n");
  514. if (!ha->iface_ipv6_1)
  515. /* IPv6 iface-1 */
  516. ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  517. &qla4xxx_iscsi_transport,
  518. ISCSI_IFACE_TYPE_IPV6, 1,
  519. 0);
  520. if (!ha->iface_ipv6_1)
  521. ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  522. "iface1.\n");
  523. }
  524. static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  525. {
  526. if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  527. qla4xxx_create_ipv4_iface(ha);
  528. if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  529. qla4xxx_create_ipv6_iface(ha);
  530. }
  531. static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  532. {
  533. if (ha->iface_ipv4) {
  534. iscsi_destroy_iface(ha->iface_ipv4);
  535. ha->iface_ipv4 = NULL;
  536. }
  537. }
  538. static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  539. {
  540. if (ha->iface_ipv6_0) {
  541. iscsi_destroy_iface(ha->iface_ipv6_0);
  542. ha->iface_ipv6_0 = NULL;
  543. }
  544. if (ha->iface_ipv6_1) {
  545. iscsi_destroy_iface(ha->iface_ipv6_1);
  546. ha->iface_ipv6_1 = NULL;
  547. }
  548. }
  549. static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  550. {
  551. qla4xxx_destroy_ipv4_iface(ha);
  552. qla4xxx_destroy_ipv6_iface(ha);
  553. }
  554. static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  555. struct iscsi_iface_param_info *iface_param,
  556. struct addr_ctrl_blk *init_fw_cb)
  557. {
  558. /*
  559. * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  560. * iface_num 1 is valid only for IPv6 Addr.
  561. */
  562. switch (iface_param->param) {
  563. case ISCSI_NET_PARAM_IPV6_ADDR:
  564. if (iface_param->iface_num & 0x1)
  565. /* IPv6 Addr 1 */
  566. memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  567. sizeof(init_fw_cb->ipv6_addr1));
  568. else
  569. /* IPv6 Addr 0 */
  570. memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  571. sizeof(init_fw_cb->ipv6_addr0));
  572. break;
  573. case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  574. if (iface_param->iface_num & 0x1)
  575. break;
  576. memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  577. sizeof(init_fw_cb->ipv6_if_id));
  578. break;
  579. case ISCSI_NET_PARAM_IPV6_ROUTER:
  580. if (iface_param->iface_num & 0x1)
  581. break;
  582. memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  583. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  584. break;
  585. case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  586. /* Autocfg applies to even interface */
  587. if (iface_param->iface_num & 0x1)
  588. break;
  589. if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  590. init_fw_cb->ipv6_addtl_opts &=
  591. cpu_to_le16(
  592. ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  593. else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  594. init_fw_cb->ipv6_addtl_opts |=
  595. cpu_to_le16(
  596. IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  597. else
  598. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  599. "IPv6 addr\n");
  600. break;
  601. case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  602. /* Autocfg applies to even interface */
  603. if (iface_param->iface_num & 0x1)
  604. break;
  605. if (iface_param->value[0] ==
  606. ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  607. init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  608. IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  609. else if (iface_param->value[0] ==
  610. ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  611. init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  612. ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  613. else
  614. ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  615. "IPv6 linklocal addr\n");
  616. break;
  617. case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  618. /* Autocfg applies to even interface */
  619. if (iface_param->iface_num & 0x1)
  620. break;
  621. if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  622. memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  623. sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  624. break;
  625. case ISCSI_NET_PARAM_IFACE_ENABLE:
  626. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  627. init_fw_cb->ipv6_opts |=
  628. cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
  629. qla4xxx_create_ipv6_iface(ha);
  630. } else {
  631. init_fw_cb->ipv6_opts &=
  632. cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  633. 0xFFFF);
  634. qla4xxx_destroy_ipv6_iface(ha);
  635. }
  636. break;
  637. case ISCSI_NET_PARAM_VLAN_ID:
  638. if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  639. break;
  640. init_fw_cb->ipv6_vlan_tag =
  641. cpu_to_be16(*(uint16_t *)iface_param->value);
  642. break;
  643. case ISCSI_NET_PARAM_VLAN_ENABLED:
  644. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  645. init_fw_cb->ipv6_opts |=
  646. cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
  647. else
  648. init_fw_cb->ipv6_opts &=
  649. cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
  650. break;
  651. default:
  652. ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
  653. iface_param->param);
  654. break;
  655. }
  656. }
  657. static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  658. struct iscsi_iface_param_info *iface_param,
  659. struct addr_ctrl_blk *init_fw_cb)
  660. {
  661. switch (iface_param->param) {
  662. case ISCSI_NET_PARAM_IPV4_ADDR:
  663. memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  664. sizeof(init_fw_cb->ipv4_addr));
  665. break;
  666. case ISCSI_NET_PARAM_IPV4_SUBNET:
  667. memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
  668. sizeof(init_fw_cb->ipv4_subnet));
  669. break;
  670. case ISCSI_NET_PARAM_IPV4_GW:
  671. memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  672. sizeof(init_fw_cb->ipv4_gw_addr));
  673. break;
  674. case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  675. if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  676. init_fw_cb->ipv4_tcp_opts |=
  677. cpu_to_le16(TCPOPT_DHCP_ENABLE);
  678. else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  679. init_fw_cb->ipv4_tcp_opts &=
  680. cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  681. else
  682. ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
  683. break;
  684. case ISCSI_NET_PARAM_IFACE_ENABLE:
  685. if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
  686. init_fw_cb->ipv4_ip_opts |=
  687. cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
  688. qla4xxx_create_ipv4_iface(ha);
  689. } else {
  690. init_fw_cb->ipv4_ip_opts &=
  691. cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
  692. 0xFFFF);
  693. qla4xxx_destroy_ipv4_iface(ha);
  694. }
  695. break;
  696. case ISCSI_NET_PARAM_VLAN_ID:
  697. if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  698. break;
  699. init_fw_cb->ipv4_vlan_tag =
  700. cpu_to_be16(*(uint16_t *)iface_param->value);
  701. break;
  702. case ISCSI_NET_PARAM_VLAN_ENABLED:
  703. if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  704. init_fw_cb->ipv4_ip_opts |=
  705. cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
  706. else
  707. init_fw_cb->ipv4_ip_opts &=
  708. cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
  709. break;
  710. default:
  711. ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
  712. iface_param->param);
  713. break;
  714. }
  715. }
  716. static void
  717. qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  718. {
  719. struct addr_ctrl_blk_def *acb;
  720. acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  721. memset(acb->reserved1, 0, sizeof(acb->reserved1));
  722. memset(acb->reserved2, 0, sizeof(acb->reserved2));
  723. memset(acb->reserved3, 0, sizeof(acb->reserved3));
  724. memset(acb->reserved4, 0, sizeof(acb->reserved4));
  725. memset(acb->reserved5, 0, sizeof(acb->reserved5));
  726. memset(acb->reserved6, 0, sizeof(acb->reserved6));
  727. memset(acb->reserved7, 0, sizeof(acb->reserved7));
  728. memset(acb->reserved8, 0, sizeof(acb->reserved8));
  729. memset(acb->reserved9, 0, sizeof(acb->reserved9));
  730. memset(acb->reserved10, 0, sizeof(acb->reserved10));
  731. memset(acb->reserved11, 0, sizeof(acb->reserved11));
  732. memset(acb->reserved12, 0, sizeof(acb->reserved12));
  733. memset(acb->reserved13, 0, sizeof(acb->reserved13));
  734. memset(acb->reserved14, 0, sizeof(acb->reserved14));
  735. memset(acb->reserved15, 0, sizeof(acb->reserved15));
  736. }
  737. static int
  738. qla4xxx_iface_set_param(struct Scsi_Host *shost, char *data, int count)
  739. {
  740. struct scsi_qla_host *ha = to_qla_host(shost);
  741. int rval = 0;
  742. struct iscsi_iface_param_info *iface_param = NULL;
  743. struct addr_ctrl_blk *init_fw_cb = NULL;
  744. dma_addr_t init_fw_cb_dma;
  745. uint32_t mbox_cmd[MBOX_REG_COUNT];
  746. uint32_t mbox_sts[MBOX_REG_COUNT];
  747. uint32_t total_param_count;
  748. uint32_t length;
  749. init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  750. sizeof(struct addr_ctrl_blk),
  751. &init_fw_cb_dma, GFP_KERNEL);
  752. if (!init_fw_cb) {
  753. ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
  754. __func__);
  755. return -ENOMEM;
  756. }
  757. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  758. memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  759. memset(&mbox_sts, 0, sizeof(mbox_sts));
  760. if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  761. ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
  762. rval = -EIO;
  763. goto exit_init_fw_cb;
  764. }
  765. total_param_count = count;
  766. iface_param = (struct iscsi_iface_param_info *)data;
  767. for ( ; total_param_count != 0; total_param_count--) {
  768. length = iface_param->len;
  769. if (iface_param->param_type != ISCSI_NET_PARAM)
  770. continue;
  771. switch (iface_param->iface_type) {
  772. case ISCSI_IFACE_TYPE_IPV4:
  773. switch (iface_param->iface_num) {
  774. case 0:
  775. qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  776. break;
  777. default:
  778. /* Cannot have more than one IPv4 interface */
  779. ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  780. "number = %d\n",
  781. iface_param->iface_num);
  782. break;
  783. }
  784. break;
  785. case ISCSI_IFACE_TYPE_IPV6:
  786. switch (iface_param->iface_num) {
  787. case 0:
  788. case 1:
  789. qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  790. break;
  791. default:
  792. /* Cannot have more than two IPv6 interface */
  793. ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  794. "number = %d\n",
  795. iface_param->iface_num);
  796. break;
  797. }
  798. break;
  799. default:
  800. ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
  801. break;
  802. }
  803. iface_param = (struct iscsi_iface_param_info *)
  804. ((uint8_t *)iface_param +
  805. sizeof(struct iscsi_iface_param_info) + length);
  806. }
  807. init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  808. rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  809. sizeof(struct addr_ctrl_blk),
  810. FLASH_OPT_RMW_COMMIT);
  811. if (rval != QLA_SUCCESS) {
  812. ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
  813. __func__);
  814. rval = -EIO;
  815. goto exit_init_fw_cb;
  816. }
  817. qla4xxx_disable_acb(ha);
  818. qla4xxx_initcb_to_acb(init_fw_cb);
  819. rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  820. if (rval != QLA_SUCCESS) {
  821. ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
  822. __func__);
  823. rval = -EIO;
  824. goto exit_init_fw_cb;
  825. }
  826. memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  827. qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  828. init_fw_cb_dma);
  829. exit_init_fw_cb:
  830. dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  831. init_fw_cb, init_fw_cb_dma);
  832. return rval;
  833. }
  834. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
  835. enum iscsi_param param, char *buf)
  836. {
  837. struct iscsi_conn *conn;
  838. struct qla_conn *qla_conn;
  839. struct sockaddr *dst_addr;
  840. int len = 0;
  841. conn = cls_conn->dd_data;
  842. qla_conn = conn->dd_data;
  843. dst_addr = &qla_conn->qla_ep->dst_addr;
  844. switch (param) {
  845. case ISCSI_PARAM_CONN_PORT:
  846. case ISCSI_PARAM_CONN_ADDRESS:
  847. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  848. dst_addr, param, buf);
  849. default:
  850. return iscsi_conn_get_param(cls_conn, param, buf);
  851. }
  852. return len;
  853. }
  854. static struct iscsi_cls_session *
  855. qla4xxx_session_create(struct iscsi_endpoint *ep,
  856. uint16_t cmds_max, uint16_t qdepth,
  857. uint32_t initial_cmdsn)
  858. {
  859. struct iscsi_cls_session *cls_sess;
  860. struct scsi_qla_host *ha;
  861. struct qla_endpoint *qla_ep;
  862. struct ddb_entry *ddb_entry;
  863. uint32_t ddb_index;
  864. uint32_t mbx_sts = 0;
  865. struct iscsi_session *sess;
  866. struct sockaddr *dst_addr;
  867. int ret;
  868. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  869. if (!ep) {
  870. printk(KERN_ERR "qla4xxx: missing ep.\n");
  871. return NULL;
  872. }
  873. qla_ep = ep->dd_data;
  874. dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  875. ha = to_qla_host(qla_ep->host);
  876. get_ddb_index:
  877. ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
  878. if (ddb_index >= MAX_DDB_ENTRIES) {
  879. DEBUG2(ql4_printk(KERN_INFO, ha,
  880. "Free DDB index not available\n"));
  881. return NULL;
  882. }
  883. if (test_and_set_bit(ddb_index, ha->ddb_idx_map))
  884. goto get_ddb_index;
  885. DEBUG2(ql4_printk(KERN_INFO, ha,
  886. "Found a free DDB index at %d\n", ddb_index));
  887. ret = qla4xxx_req_ddb_entry(ha, ddb_index, &mbx_sts);
  888. if (ret == QLA_ERROR) {
  889. if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
  890. ql4_printk(KERN_INFO, ha,
  891. "DDB index = %d not available trying next\n",
  892. ddb_index);
  893. goto get_ddb_index;
  894. }
  895. DEBUG2(ql4_printk(KERN_INFO, ha,
  896. "Free FW DDB not available\n"));
  897. return NULL;
  898. }
  899. cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
  900. cmds_max, sizeof(struct ddb_entry),
  901. sizeof(struct ql4_task_data),
  902. initial_cmdsn, ddb_index);
  903. if (!cls_sess)
  904. return NULL;
  905. sess = cls_sess->dd_data;
  906. ddb_entry = sess->dd_data;
  907. ddb_entry->fw_ddb_index = ddb_index;
  908. ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  909. ddb_entry->ha = ha;
  910. ddb_entry->sess = cls_sess;
  911. cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  912. ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
  913. ha->tot_ddbs++;
  914. return cls_sess;
  915. }
  916. static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
  917. {
  918. struct iscsi_session *sess;
  919. struct ddb_entry *ddb_entry;
  920. struct scsi_qla_host *ha;
  921. unsigned long flags;
  922. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  923. sess = cls_sess->dd_data;
  924. ddb_entry = sess->dd_data;
  925. ha = ddb_entry->ha;
  926. spin_lock_irqsave(&ha->hardware_lock, flags);
  927. qla4xxx_free_ddb(ha, ddb_entry);
  928. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  929. iscsi_session_teardown(cls_sess);
  930. }
  931. static struct iscsi_cls_conn *
  932. qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  933. {
  934. struct iscsi_cls_conn *cls_conn;
  935. struct iscsi_session *sess;
  936. struct ddb_entry *ddb_entry;
  937. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  938. cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  939. conn_idx);
  940. sess = cls_sess->dd_data;
  941. ddb_entry = sess->dd_data;
  942. ddb_entry->conn = cls_conn;
  943. return cls_conn;
  944. }
  945. static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  946. struct iscsi_cls_conn *cls_conn,
  947. uint64_t transport_fd, int is_leading)
  948. {
  949. struct iscsi_conn *conn;
  950. struct qla_conn *qla_conn;
  951. struct iscsi_endpoint *ep;
  952. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  953. if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  954. return -EINVAL;
  955. ep = iscsi_lookup_endpoint(transport_fd);
  956. conn = cls_conn->dd_data;
  957. qla_conn = conn->dd_data;
  958. qla_conn->qla_ep = ep->dd_data;
  959. return 0;
  960. }
  961. static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  962. {
  963. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  964. struct iscsi_session *sess;
  965. struct ddb_entry *ddb_entry;
  966. struct scsi_qla_host *ha;
  967. struct dev_db_entry *fw_ddb_entry;
  968. dma_addr_t fw_ddb_entry_dma;
  969. uint32_t mbx_sts = 0;
  970. int ret = 0;
  971. int status = QLA_SUCCESS;
  972. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  973. sess = cls_sess->dd_data;
  974. ddb_entry = sess->dd_data;
  975. ha = ddb_entry->ha;
  976. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  977. &fw_ddb_entry_dma, GFP_KERNEL);
  978. if (!fw_ddb_entry) {
  979. ql4_printk(KERN_ERR, ha,
  980. "%s: Unable to allocate dma buffer\n", __func__);
  981. return -ENOMEM;
  982. }
  983. ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  984. if (ret) {
  985. /* If iscsid is stopped and started then no need to do
  986. * set param again since ddb state will be already
  987. * active and FW does not allow set ddb to an
  988. * active session.
  989. */
  990. if (mbx_sts)
  991. if (ddb_entry->fw_ddb_device_state ==
  992. DDB_DS_SESSION_ACTIVE)
  993. goto exit_set_param;
  994. ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
  995. __func__, ddb_entry->fw_ddb_index);
  996. goto exit_conn_start;
  997. }
  998. status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  999. if (status == QLA_ERROR) {
  1000. ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
  1001. sess->targetname);
  1002. ret = -EINVAL;
  1003. goto exit_conn_start;
  1004. }
  1005. ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  1006. exit_set_param:
  1007. iscsi_conn_start(cls_conn);
  1008. ret = 0;
  1009. exit_conn_start:
  1010. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1011. fw_ddb_entry, fw_ddb_entry_dma);
  1012. return ret;
  1013. }
  1014. static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1015. {
  1016. struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  1017. struct iscsi_session *sess;
  1018. struct scsi_qla_host *ha;
  1019. struct ddb_entry *ddb_entry;
  1020. int options;
  1021. DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
  1022. sess = cls_sess->dd_data;
  1023. ddb_entry = sess->dd_data;
  1024. ha = ddb_entry->ha;
  1025. options = LOGOUT_OPTION_CLOSE_SESSION;
  1026. if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  1027. ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
  1028. else
  1029. qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  1030. /*
  1031. * Clear the DDB bit so that next login can use the bit
  1032. * if FW is not clearing the DDB entry then set DDB will fail anyways
  1033. */
  1034. clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
  1035. }
  1036. static void qla4xxx_task_work(struct work_struct *wdata)
  1037. {
  1038. struct ql4_task_data *task_data;
  1039. struct scsi_qla_host *ha;
  1040. struct passthru_status *sts;
  1041. struct iscsi_task *task;
  1042. struct iscsi_hdr *hdr;
  1043. uint8_t *data;
  1044. uint32_t data_len;
  1045. struct iscsi_conn *conn;
  1046. int hdr_len;
  1047. itt_t itt;
  1048. task_data = container_of(wdata, struct ql4_task_data, task_work);
  1049. ha = task_data->ha;
  1050. task = task_data->task;
  1051. sts = &task_data->sts;
  1052. hdr_len = sizeof(struct iscsi_hdr);
  1053. DEBUG3(printk(KERN_INFO "Status returned\n"));
  1054. DEBUG3(qla4xxx_dump_buffer(sts, 64));
  1055. DEBUG3(printk(KERN_INFO "Response buffer"));
  1056. DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  1057. conn = task->conn;
  1058. switch (sts->completionStatus) {
  1059. case PASSTHRU_STATUS_COMPLETE:
  1060. hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  1061. /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  1062. itt = sts->handle;
  1063. hdr->itt = itt;
  1064. data = task_data->resp_buffer + hdr_len;
  1065. data_len = task_data->resp_len - hdr_len;
  1066. iscsi_complete_pdu(conn, hdr, data, data_len);
  1067. break;
  1068. default:
  1069. ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
  1070. sts->completionStatus);
  1071. break;
  1072. }
  1073. return;
  1074. }
  1075. static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  1076. {
  1077. struct ql4_task_data *task_data;
  1078. struct iscsi_session *sess;
  1079. struct ddb_entry *ddb_entry;
  1080. struct scsi_qla_host *ha;
  1081. int hdr_len;
  1082. sess = task->conn->session;
  1083. ddb_entry = sess->dd_data;
  1084. ha = ddb_entry->ha;
  1085. task_data = task->dd_data;
  1086. memset(task_data, 0, sizeof(struct ql4_task_data));
  1087. if (task->sc) {
  1088. ql4_printk(KERN_INFO, ha,
  1089. "%s: SCSI Commands not implemented\n", __func__);
  1090. return -EINVAL;
  1091. }
  1092. hdr_len = sizeof(struct iscsi_hdr);
  1093. task_data->ha = ha;
  1094. task_data->task = task;
  1095. if (task->data_count) {
  1096. task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  1097. task->data_count,
  1098. PCI_DMA_TODEVICE);
  1099. }
  1100. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1101. __func__, task->conn->max_recv_dlength, hdr_len));
  1102. task_data->resp_len = task->conn->max_recv_dlength;
  1103. task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1104. task_data->resp_len,
  1105. &task_data->resp_dma,
  1106. GFP_ATOMIC);
  1107. if (!task_data->resp_buffer)
  1108. goto exit_alloc_pdu;
  1109. task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
  1110. task->data_count + hdr_len,
  1111. &task_data->req_dma,
  1112. GFP_ATOMIC);
  1113. if (!task_data->req_buffer)
  1114. goto exit_alloc_pdu;
  1115. task->hdr = task_data->req_buffer;
  1116. INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  1117. return 0;
  1118. exit_alloc_pdu:
  1119. if (task_data->resp_buffer)
  1120. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1121. task_data->resp_buffer, task_data->resp_dma);
  1122. if (task_data->req_buffer)
  1123. dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
  1124. task_data->req_buffer, task_data->req_dma);
  1125. return -ENOMEM;
  1126. }
  1127. static void qla4xxx_task_cleanup(struct iscsi_task *task)
  1128. {
  1129. struct ql4_task_data *task_data;
  1130. struct iscsi_session *sess;
  1131. struct ddb_entry *ddb_entry;
  1132. struct scsi_qla_host *ha;
  1133. int hdr_len;
  1134. hdr_len = sizeof(struct iscsi_hdr);
  1135. sess = task->conn->session;
  1136. ddb_entry = sess->dd_data;
  1137. ha = ddb_entry->ha;
  1138. task_data = task->dd_data;
  1139. if (task->data_count) {
  1140. dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  1141. task->data_count, PCI_DMA_TODEVICE);
  1142. }
  1143. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
  1144. __func__, task->conn->max_recv_dlength, hdr_len));
  1145. dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  1146. task_data->resp_buffer, task_data->resp_dma);
  1147. dma_free_coherent(&ha->pdev->dev, task->data_count + hdr_len,
  1148. task_data->req_buffer, task_data->req_dma);
  1149. return;
  1150. }
  1151. static int qla4xxx_task_xmit(struct iscsi_task *task)
  1152. {
  1153. struct scsi_cmnd *sc = task->sc;
  1154. struct iscsi_session *sess = task->conn->session;
  1155. struct ddb_entry *ddb_entry = sess->dd_data;
  1156. struct scsi_qla_host *ha = ddb_entry->ha;
  1157. if (!sc)
  1158. return qla4xxx_send_passthru0(task);
  1159. ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
  1160. __func__);
  1161. return -ENOSYS;
  1162. }
  1163. void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  1164. struct ddb_entry *ddb_entry)
  1165. {
  1166. struct iscsi_cls_session *cls_sess;
  1167. struct iscsi_cls_conn *cls_conn;
  1168. struct iscsi_session *sess;
  1169. struct iscsi_conn *conn;
  1170. uint32_t ddb_state;
  1171. dma_addr_t fw_ddb_entry_dma;
  1172. struct dev_db_entry *fw_ddb_entry;
  1173. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  1174. &fw_ddb_entry_dma, GFP_KERNEL);
  1175. if (!fw_ddb_entry) {
  1176. ql4_printk(KERN_ERR, ha,
  1177. "%s: Unable to allocate dma buffer\n", __func__);
  1178. return;
  1179. }
  1180. if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  1181. fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  1182. NULL, NULL, NULL) == QLA_ERROR) {
  1183. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  1184. "get_ddb_entry for fw_ddb_index %d\n",
  1185. ha->host_no, __func__,
  1186. ddb_entry->fw_ddb_index));
  1187. return;
  1188. }
  1189. cls_sess = ddb_entry->sess;
  1190. sess = cls_sess->dd_data;
  1191. cls_conn = ddb_entry->conn;
  1192. conn = cls_conn->dd_data;
  1193. /* Update params */
  1194. conn->max_recv_dlength = BYTE_UNITS *
  1195. le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  1196. conn->max_xmit_dlength = BYTE_UNITS *
  1197. le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  1198. sess->initial_r2t_en =
  1199. (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1200. sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  1201. sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  1202. sess->first_burst = BYTE_UNITS *
  1203. le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  1204. sess->max_burst = BYTE_UNITS *
  1205. le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  1206. sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  1207. sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  1208. sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  1209. memcpy(sess->initiatorname, ha->name_string,
  1210. min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
  1211. }
  1212. /*
  1213. * Timer routines
  1214. */
  1215. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  1216. unsigned long interval)
  1217. {
  1218. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  1219. __func__, ha->host->host_no));
  1220. init_timer(&ha->timer);
  1221. ha->timer.expires = jiffies + interval * HZ;
  1222. ha->timer.data = (unsigned long)ha;
  1223. ha->timer.function = (void (*)(unsigned long))func;
  1224. add_timer(&ha->timer);
  1225. ha->timer_active = 1;
  1226. }
  1227. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  1228. {
  1229. del_timer_sync(&ha->timer);
  1230. ha->timer_active = 0;
  1231. }
  1232. /***
  1233. * qla4xxx_mark_device_missing - blocks the session
  1234. * @cls_session: Pointer to the session to be blocked
  1235. * @ddb_entry: Pointer to device database entry
  1236. *
  1237. * This routine marks a device missing and close connection.
  1238. **/
  1239. void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
  1240. {
  1241. iscsi_block_session(cls_session);
  1242. }
  1243. /**
  1244. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  1245. * @ha: Pointer to host adapter structure.
  1246. *
  1247. * This routine marks a device missing and resets the relogin retry count.
  1248. **/
  1249. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  1250. {
  1251. iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
  1252. }
  1253. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  1254. struct ddb_entry *ddb_entry,
  1255. struct scsi_cmnd *cmd)
  1256. {
  1257. struct srb *srb;
  1258. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  1259. if (!srb)
  1260. return srb;
  1261. kref_init(&srb->srb_ref);
  1262. srb->ha = ha;
  1263. srb->ddb = ddb_entry;
  1264. srb->cmd = cmd;
  1265. srb->flags = 0;
  1266. CMD_SP(cmd) = (void *)srb;
  1267. return srb;
  1268. }
  1269. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  1270. {
  1271. struct scsi_cmnd *cmd = srb->cmd;
  1272. if (srb->flags & SRB_DMA_VALID) {
  1273. scsi_dma_unmap(cmd);
  1274. srb->flags &= ~SRB_DMA_VALID;
  1275. }
  1276. CMD_SP(cmd) = NULL;
  1277. }
  1278. void qla4xxx_srb_compl(struct kref *ref)
  1279. {
  1280. struct srb *srb = container_of(ref, struct srb, srb_ref);
  1281. struct scsi_cmnd *cmd = srb->cmd;
  1282. struct scsi_qla_host *ha = srb->ha;
  1283. qla4xxx_srb_free_dma(ha, srb);
  1284. mempool_free(srb, ha->srb_mempool);
  1285. cmd->scsi_done(cmd);
  1286. }
  1287. /**
  1288. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  1289. * @host: scsi host
  1290. * @cmd: Pointer to Linux's SCSI command structure
  1291. *
  1292. * Remarks:
  1293. * This routine is invoked by Linux to send a SCSI command to the driver.
  1294. * The mid-level driver tries to ensure that queuecommand never gets
  1295. * invoked concurrently with itself or the interrupt handler (although
  1296. * the interrupt handler may call this routine as part of request-
  1297. * completion handling). Unfortunely, it sometimes calls the scheduler
  1298. * in interrupt context which is a big NO! NO!.
  1299. **/
  1300. static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
  1301. {
  1302. struct scsi_qla_host *ha = to_qla_host(host);
  1303. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1304. struct iscsi_cls_session *sess = ddb_entry->sess;
  1305. struct srb *srb;
  1306. int rval;
  1307. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1308. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  1309. cmd->result = DID_NO_CONNECT << 16;
  1310. else
  1311. cmd->result = DID_REQUEUE << 16;
  1312. goto qc_fail_command;
  1313. }
  1314. if (!sess) {
  1315. cmd->result = DID_IMM_RETRY << 16;
  1316. goto qc_fail_command;
  1317. }
  1318. rval = iscsi_session_chkready(sess);
  1319. if (rval) {
  1320. cmd->result = rval;
  1321. goto qc_fail_command;
  1322. }
  1323. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1324. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1325. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1326. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1327. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1328. !test_bit(AF_ONLINE, &ha->flags) ||
  1329. !test_bit(AF_LINK_UP, &ha->flags) ||
  1330. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  1331. goto qc_host_busy;
  1332. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
  1333. if (!srb)
  1334. goto qc_host_busy;
  1335. rval = qla4xxx_send_command_to_isp(ha, srb);
  1336. if (rval != QLA_SUCCESS)
  1337. goto qc_host_busy_free_sp;
  1338. return 0;
  1339. qc_host_busy_free_sp:
  1340. qla4xxx_srb_free_dma(ha, srb);
  1341. mempool_free(srb, ha->srb_mempool);
  1342. qc_host_busy:
  1343. return SCSI_MLQUEUE_HOST_BUSY;
  1344. qc_fail_command:
  1345. cmd->scsi_done(cmd);
  1346. return 0;
  1347. }
  1348. /**
  1349. * qla4xxx_mem_free - frees memory allocated to adapter
  1350. * @ha: Pointer to host adapter structure.
  1351. *
  1352. * Frees memory previously allocated by qla4xxx_mem_alloc
  1353. **/
  1354. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  1355. {
  1356. if (ha->queues)
  1357. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  1358. ha->queues_dma);
  1359. ha->queues_len = 0;
  1360. ha->queues = NULL;
  1361. ha->queues_dma = 0;
  1362. ha->request_ring = NULL;
  1363. ha->request_dma = 0;
  1364. ha->response_ring = NULL;
  1365. ha->response_dma = 0;
  1366. ha->shadow_regs = NULL;
  1367. ha->shadow_regs_dma = 0;
  1368. /* Free srb pool. */
  1369. if (ha->srb_mempool)
  1370. mempool_destroy(ha->srb_mempool);
  1371. ha->srb_mempool = NULL;
  1372. if (ha->chap_dma_pool)
  1373. dma_pool_destroy(ha->chap_dma_pool);
  1374. /* release io space registers */
  1375. if (is_qla8022(ha)) {
  1376. if (ha->nx_pcibase)
  1377. iounmap(
  1378. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  1379. } else if (ha->reg)
  1380. iounmap(ha->reg);
  1381. pci_release_regions(ha->pdev);
  1382. }
  1383. /**
  1384. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  1385. * @ha: Pointer to host adapter structure
  1386. *
  1387. * Allocates DMA memory for request and response queues. Also allocates memory
  1388. * for srbs.
  1389. **/
  1390. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  1391. {
  1392. unsigned long align;
  1393. /* Allocate contiguous block of DMA memory for queues. */
  1394. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1395. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  1396. sizeof(struct shadow_regs) +
  1397. MEM_ALIGN_VALUE +
  1398. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  1399. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  1400. &ha->queues_dma, GFP_KERNEL);
  1401. if (ha->queues == NULL) {
  1402. ql4_printk(KERN_WARNING, ha,
  1403. "Memory Allocation failed - queues.\n");
  1404. goto mem_alloc_error_exit;
  1405. }
  1406. memset(ha->queues, 0, ha->queues_len);
  1407. /*
  1408. * As per RISC alignment requirements -- the bus-address must be a
  1409. * multiple of the request-ring size (in bytes).
  1410. */
  1411. align = 0;
  1412. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  1413. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  1414. (MEM_ALIGN_VALUE - 1));
  1415. /* Update request and response queue pointers. */
  1416. ha->request_dma = ha->queues_dma + align;
  1417. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  1418. ha->response_dma = ha->queues_dma + align +
  1419. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  1420. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  1421. (REQUEST_QUEUE_DEPTH *
  1422. QUEUE_SIZE));
  1423. ha->shadow_regs_dma = ha->queues_dma + align +
  1424. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  1425. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  1426. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  1427. (REQUEST_QUEUE_DEPTH *
  1428. QUEUE_SIZE) +
  1429. (RESPONSE_QUEUE_DEPTH *
  1430. QUEUE_SIZE));
  1431. /* Allocate memory for srb pool. */
  1432. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  1433. mempool_free_slab, srb_cachep);
  1434. if (ha->srb_mempool == NULL) {
  1435. ql4_printk(KERN_WARNING, ha,
  1436. "Memory Allocation failed - SRB Pool.\n");
  1437. goto mem_alloc_error_exit;
  1438. }
  1439. ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  1440. CHAP_DMA_BLOCK_SIZE, 8, 0);
  1441. if (ha->chap_dma_pool == NULL) {
  1442. ql4_printk(KERN_WARNING, ha,
  1443. "%s: chap_dma_pool allocation failed..\n", __func__);
  1444. goto mem_alloc_error_exit;
  1445. }
  1446. return QLA_SUCCESS;
  1447. mem_alloc_error_exit:
  1448. qla4xxx_mem_free(ha);
  1449. return QLA_ERROR;
  1450. }
  1451. /**
  1452. * qla4_8xxx_check_fw_alive - Check firmware health
  1453. * @ha: Pointer to host adapter structure.
  1454. *
  1455. * Context: Interrupt
  1456. **/
  1457. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  1458. {
  1459. uint32_t fw_heartbeat_counter, halt_status;
  1460. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  1461. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  1462. if (fw_heartbeat_counter == 0xffffffff) {
  1463. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  1464. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  1465. ha->host_no, __func__));
  1466. return;
  1467. }
  1468. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  1469. ha->seconds_since_last_heartbeat++;
  1470. /* FW not alive after 2 seconds */
  1471. if (ha->seconds_since_last_heartbeat == 2) {
  1472. ha->seconds_since_last_heartbeat = 0;
  1473. halt_status = qla4_8xxx_rd_32(ha,
  1474. QLA82XX_PEG_HALT_STATUS1);
  1475. ql4_printk(KERN_INFO, ha,
  1476. "scsi(%ld): %s, Dumping hw/fw registers:\n "
  1477. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  1478. " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  1479. " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  1480. " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
  1481. ha->host_no, __func__, halt_status,
  1482. qla4_8xxx_rd_32(ha,
  1483. QLA82XX_PEG_HALT_STATUS2),
  1484. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  1485. 0x3c),
  1486. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  1487. 0x3c),
  1488. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  1489. 0x3c),
  1490. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  1491. 0x3c),
  1492. qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  1493. 0x3c));
  1494. /* Since we cannot change dev_state in interrupt
  1495. * context, set appropriate DPC flag then wakeup
  1496. * DPC */
  1497. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  1498. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  1499. else {
  1500. printk("scsi%ld: %s: detect abort needed!\n",
  1501. ha->host_no, __func__);
  1502. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1503. }
  1504. qla4xxx_wake_dpc(ha);
  1505. qla4xxx_mailbox_premature_completion(ha);
  1506. }
  1507. } else
  1508. ha->seconds_since_last_heartbeat = 0;
  1509. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  1510. }
  1511. /**
  1512. * qla4_8xxx_watchdog - Poll dev state
  1513. * @ha: Pointer to host adapter structure.
  1514. *
  1515. * Context: Interrupt
  1516. **/
  1517. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  1518. {
  1519. uint32_t dev_state;
  1520. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  1521. /* don't poll if reset is going on */
  1522. if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  1523. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1524. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
  1525. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  1526. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1527. if (!ql4xdontresethba) {
  1528. ql4_printk(KERN_INFO, ha, "%s: HW State: "
  1529. "NEED RESET!\n", __func__);
  1530. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1531. qla4xxx_wake_dpc(ha);
  1532. qla4xxx_mailbox_premature_completion(ha);
  1533. }
  1534. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  1535. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1536. ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
  1537. __func__);
  1538. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  1539. qla4xxx_wake_dpc(ha);
  1540. } else {
  1541. /* Check firmware health */
  1542. qla4_8xxx_check_fw_alive(ha);
  1543. }
  1544. }
  1545. }
  1546. /**
  1547. * qla4xxx_timer - checks every second for work to do.
  1548. * @ha: Pointer to host adapter structure.
  1549. **/
  1550. static void qla4xxx_timer(struct scsi_qla_host *ha)
  1551. {
  1552. int start_dpc = 0;
  1553. uint16_t w;
  1554. /* If we are in the middle of AER/EEH processing
  1555. * skip any processing and reschedule the timer
  1556. */
  1557. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1558. mod_timer(&ha->timer, jiffies + HZ);
  1559. return;
  1560. }
  1561. /* Hardware read to trigger an EEH error during mailbox waits. */
  1562. if (!pci_channel_offline(ha->pdev))
  1563. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  1564. if (is_qla8022(ha)) {
  1565. qla4_8xxx_watchdog(ha);
  1566. }
  1567. if (!is_qla8022(ha)) {
  1568. /* Check for heartbeat interval. */
  1569. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  1570. ha->heartbeat_interval != 0) {
  1571. ha->seconds_since_last_heartbeat++;
  1572. if (ha->seconds_since_last_heartbeat >
  1573. ha->heartbeat_interval + 2)
  1574. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1575. }
  1576. }
  1577. /* Wakeup the dpc routine for this adapter, if needed. */
  1578. if (start_dpc ||
  1579. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1580. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  1581. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  1582. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1583. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1584. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  1585. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  1586. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  1587. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  1588. test_bit(DPC_AEN, &ha->dpc_flags)) {
  1589. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  1590. " - dpc flags = 0x%lx\n",
  1591. ha->host_no, __func__, ha->dpc_flags));
  1592. qla4xxx_wake_dpc(ha);
  1593. }
  1594. /* Reschedule timer thread to call us back in one second */
  1595. mod_timer(&ha->timer, jiffies + HZ);
  1596. DEBUG2(ha->seconds_since_last_intr++);
  1597. }
  1598. /**
  1599. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  1600. * @ha: Pointer to host adapter structure.
  1601. *
  1602. * This routine stalls the driver until all outstanding commands are returned.
  1603. * Caller must release the Hardware Lock prior to calling this routine.
  1604. **/
  1605. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  1606. {
  1607. uint32_t index = 0;
  1608. unsigned long flags;
  1609. struct scsi_cmnd *cmd;
  1610. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  1611. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  1612. "complete\n", WAIT_CMD_TOV));
  1613. while (!time_after_eq(jiffies, wtime)) {
  1614. spin_lock_irqsave(&ha->hardware_lock, flags);
  1615. /* Find a command that hasn't completed. */
  1616. for (index = 0; index < ha->host->can_queue; index++) {
  1617. cmd = scsi_host_find_tag(ha->host, index);
  1618. /*
  1619. * We cannot just check if the index is valid,
  1620. * becase if we are run from the scsi eh, then
  1621. * the scsi/block layer is going to prevent
  1622. * the tag from being released.
  1623. */
  1624. if (cmd != NULL && CMD_SP(cmd))
  1625. break;
  1626. }
  1627. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1628. /* If No Commands are pending, wait is complete */
  1629. if (index == ha->host->can_queue)
  1630. return QLA_SUCCESS;
  1631. msleep(1000);
  1632. }
  1633. /* If we timed out on waiting for commands to come back
  1634. * return ERROR. */
  1635. return QLA_ERROR;
  1636. }
  1637. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  1638. {
  1639. uint32_t ctrl_status;
  1640. unsigned long flags = 0;
  1641. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  1642. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  1643. return QLA_ERROR;
  1644. spin_lock_irqsave(&ha->hardware_lock, flags);
  1645. /*
  1646. * If the SCSI Reset Interrupt bit is set, clear it.
  1647. * Otherwise, the Soft Reset won't work.
  1648. */
  1649. ctrl_status = readw(&ha->reg->ctrl_status);
  1650. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  1651. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1652. /* Issue Soft Reset */
  1653. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  1654. readl(&ha->reg->ctrl_status);
  1655. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1656. return QLA_SUCCESS;
  1657. }
  1658. /**
  1659. * qla4xxx_soft_reset - performs soft reset.
  1660. * @ha: Pointer to host adapter structure.
  1661. **/
  1662. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  1663. {
  1664. uint32_t max_wait_time;
  1665. unsigned long flags = 0;
  1666. int status;
  1667. uint32_t ctrl_status;
  1668. status = qla4xxx_hw_reset(ha);
  1669. if (status != QLA_SUCCESS)
  1670. return status;
  1671. status = QLA_ERROR;
  1672. /* Wait until the Network Reset Intr bit is cleared */
  1673. max_wait_time = RESET_INTR_TOV;
  1674. do {
  1675. spin_lock_irqsave(&ha->hardware_lock, flags);
  1676. ctrl_status = readw(&ha->reg->ctrl_status);
  1677. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1678. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  1679. break;
  1680. msleep(1000);
  1681. } while ((--max_wait_time));
  1682. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  1683. DEBUG2(printk(KERN_WARNING
  1684. "scsi%ld: Network Reset Intr not cleared by "
  1685. "Network function, clearing it now!\n",
  1686. ha->host_no));
  1687. spin_lock_irqsave(&ha->hardware_lock, flags);
  1688. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  1689. readl(&ha->reg->ctrl_status);
  1690. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1691. }
  1692. /* Wait until the firmware tells us the Soft Reset is done */
  1693. max_wait_time = SOFT_RESET_TOV;
  1694. do {
  1695. spin_lock_irqsave(&ha->hardware_lock, flags);
  1696. ctrl_status = readw(&ha->reg->ctrl_status);
  1697. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1698. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  1699. status = QLA_SUCCESS;
  1700. break;
  1701. }
  1702. msleep(1000);
  1703. } while ((--max_wait_time));
  1704. /*
  1705. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  1706. * after the soft reset has taken place.
  1707. */
  1708. spin_lock_irqsave(&ha->hardware_lock, flags);
  1709. ctrl_status = readw(&ha->reg->ctrl_status);
  1710. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  1711. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  1712. readl(&ha->reg->ctrl_status);
  1713. }
  1714. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1715. /* If soft reset fails then most probably the bios on other
  1716. * function is also enabled.
  1717. * Since the initialization is sequential the other fn
  1718. * wont be able to acknowledge the soft reset.
  1719. * Issue a force soft reset to workaround this scenario.
  1720. */
  1721. if (max_wait_time == 0) {
  1722. /* Issue Force Soft Reset */
  1723. spin_lock_irqsave(&ha->hardware_lock, flags);
  1724. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  1725. readl(&ha->reg->ctrl_status);
  1726. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1727. /* Wait until the firmware tells us the Soft Reset is done */
  1728. max_wait_time = SOFT_RESET_TOV;
  1729. do {
  1730. spin_lock_irqsave(&ha->hardware_lock, flags);
  1731. ctrl_status = readw(&ha->reg->ctrl_status);
  1732. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1733. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  1734. status = QLA_SUCCESS;
  1735. break;
  1736. }
  1737. msleep(1000);
  1738. } while ((--max_wait_time));
  1739. }
  1740. return status;
  1741. }
  1742. /**
  1743. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  1744. * @ha: Pointer to host adapter structure.
  1745. * @res: returned scsi status
  1746. *
  1747. * This routine is called just prior to a HARD RESET to return all
  1748. * outstanding commands back to the Operating System.
  1749. * Caller should make sure that the following locks are released
  1750. * before this calling routine: Hardware lock, and io_request_lock.
  1751. **/
  1752. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  1753. {
  1754. struct srb *srb;
  1755. int i;
  1756. unsigned long flags;
  1757. spin_lock_irqsave(&ha->hardware_lock, flags);
  1758. for (i = 0; i < ha->host->can_queue; i++) {
  1759. srb = qla4xxx_del_from_active_array(ha, i);
  1760. if (srb != NULL) {
  1761. srb->cmd->result = res;
  1762. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1763. }
  1764. }
  1765. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1766. }
  1767. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  1768. {
  1769. clear_bit(AF_ONLINE, &ha->flags);
  1770. /* Disable the board */
  1771. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  1772. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1773. qla4xxx_mark_all_devices_missing(ha);
  1774. clear_bit(AF_INIT_DONE, &ha->flags);
  1775. }
  1776. static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  1777. {
  1778. struct iscsi_session *sess;
  1779. struct ddb_entry *ddb_entry;
  1780. sess = cls_session->dd_data;
  1781. ddb_entry = sess->dd_data;
  1782. ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
  1783. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
  1784. }
  1785. /**
  1786. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  1787. * @ha: Pointer to host adapter structure.
  1788. **/
  1789. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  1790. {
  1791. int status = QLA_ERROR;
  1792. uint8_t reset_chip = 0;
  1793. /* Stall incoming I/O until we are done */
  1794. scsi_block_requests(ha->host);
  1795. clear_bit(AF_ONLINE, &ha->flags);
  1796. clear_bit(AF_LINK_UP, &ha->flags);
  1797. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  1798. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1799. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  1800. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1801. reset_chip = 1;
  1802. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  1803. * do not reset adapter, jump to initialize_adapter */
  1804. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1805. status = QLA_SUCCESS;
  1806. goto recover_ha_init_adapter;
  1807. }
  1808. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  1809. * from eh_host_reset or ioctl module */
  1810. if (is_qla8022(ha) && !reset_chip &&
  1811. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  1812. DEBUG2(ql4_printk(KERN_INFO, ha,
  1813. "scsi%ld: %s - Performing stop_firmware...\n",
  1814. ha->host_no, __func__));
  1815. status = ha->isp_ops->reset_firmware(ha);
  1816. if (status == QLA_SUCCESS) {
  1817. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1818. qla4xxx_cmd_wait(ha);
  1819. ha->isp_ops->disable_intrs(ha);
  1820. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1821. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1822. } else {
  1823. /* If the stop_firmware fails then
  1824. * reset the entire chip */
  1825. reset_chip = 1;
  1826. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1827. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1828. }
  1829. }
  1830. /* Issue full chip reset if recovering from a catastrophic error,
  1831. * or if stop_firmware fails for ISP-82xx.
  1832. * This is the default case for ISP-4xxx */
  1833. if (!is_qla8022(ha) || reset_chip) {
  1834. if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  1835. qla4xxx_cmd_wait(ha);
  1836. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1837. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1838. DEBUG2(ql4_printk(KERN_INFO, ha,
  1839. "scsi%ld: %s - Performing chip reset..\n",
  1840. ha->host_no, __func__));
  1841. status = ha->isp_ops->reset_chip(ha);
  1842. }
  1843. /* Flush any pending ddb changed AENs */
  1844. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1845. recover_ha_init_adapter:
  1846. /* Upon successful firmware/chip reset, re-initialize the adapter */
  1847. if (status == QLA_SUCCESS) {
  1848. /* For ISP-4xxx, force function 1 to always initialize
  1849. * before function 3 to prevent both funcions from
  1850. * stepping on top of the other */
  1851. if (!is_qla8022(ha) && (ha->mac_index == 3))
  1852. ssleep(6);
  1853. /* NOTE: AF_ONLINE flag set upon successful completion of
  1854. * qla4xxx_initialize_adapter */
  1855. status = qla4xxx_initialize_adapter(ha);
  1856. }
  1857. /* Retry failed adapter initialization, if necessary
  1858. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  1859. * case to prevent ping-pong resets between functions */
  1860. if (!test_bit(AF_ONLINE, &ha->flags) &&
  1861. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1862. /* Adapter initialization failed, see if we can retry
  1863. * resetting the ha.
  1864. * Since we don't want to block the DPC for too long
  1865. * with multiple resets in the same thread,
  1866. * utilize DPC to retry */
  1867. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  1868. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  1869. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  1870. "(%d) more times\n", ha->host_no,
  1871. ha->retry_reset_ha_cnt));
  1872. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1873. status = QLA_ERROR;
  1874. } else {
  1875. if (ha->retry_reset_ha_cnt > 0) {
  1876. /* Schedule another Reset HA--DPC will retry */
  1877. ha->retry_reset_ha_cnt--;
  1878. DEBUG2(printk("scsi%ld: recover adapter - "
  1879. "retry remaining %d\n",
  1880. ha->host_no,
  1881. ha->retry_reset_ha_cnt));
  1882. status = QLA_ERROR;
  1883. }
  1884. if (ha->retry_reset_ha_cnt == 0) {
  1885. /* Recover adapter retries have been exhausted.
  1886. * Adapter DEAD */
  1887. DEBUG2(printk("scsi%ld: recover adapter "
  1888. "failed - board disabled\n",
  1889. ha->host_no));
  1890. qla4xxx_dead_adapter_cleanup(ha);
  1891. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1892. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1893. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1894. &ha->dpc_flags);
  1895. status = QLA_ERROR;
  1896. }
  1897. }
  1898. } else {
  1899. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1900. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1901. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1902. }
  1903. ha->adapter_error_count++;
  1904. if (test_bit(AF_ONLINE, &ha->flags))
  1905. ha->isp_ops->enable_intrs(ha);
  1906. scsi_unblock_requests(ha->host);
  1907. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1908. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1909. status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
  1910. return status;
  1911. }
  1912. static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
  1913. {
  1914. struct iscsi_session *sess;
  1915. struct ddb_entry *ddb_entry;
  1916. struct scsi_qla_host *ha;
  1917. sess = cls_session->dd_data;
  1918. ddb_entry = sess->dd_data;
  1919. ha = ddb_entry->ha;
  1920. if (!iscsi_is_session_online(cls_session)) {
  1921. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  1922. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  1923. " unblock session\n", ha->host_no, __func__,
  1924. ddb_entry->fw_ddb_index);
  1925. iscsi_unblock_session(ddb_entry->sess);
  1926. } else {
  1927. /* Trigger relogin */
  1928. iscsi_session_failure(cls_session->dd_data,
  1929. ISCSI_ERR_CONN_FAILED);
  1930. }
  1931. }
  1932. }
  1933. static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  1934. {
  1935. iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  1936. }
  1937. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1938. {
  1939. if (ha->dpc_thread)
  1940. queue_work(ha->dpc_thread, &ha->dpc_work);
  1941. }
  1942. /**
  1943. * qla4xxx_do_dpc - dpc routine
  1944. * @data: in our case pointer to adapter structure
  1945. *
  1946. * This routine is a task that is schedule by the interrupt handler
  1947. * to perform the background processing for interrupts. We put it
  1948. * on a task queue that is consumed whenever the scheduler runs; that's
  1949. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1950. * the mid-level tries to sleep when it reaches the driver threshold
  1951. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1952. **/
  1953. static void qla4xxx_do_dpc(struct work_struct *work)
  1954. {
  1955. struct scsi_qla_host *ha =
  1956. container_of(work, struct scsi_qla_host, dpc_work);
  1957. int status = QLA_ERROR;
  1958. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1959. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1960. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1961. /* Initialization not yet finished. Don't do anything yet. */
  1962. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1963. return;
  1964. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1965. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1966. ha->host_no, __func__, ha->flags));
  1967. return;
  1968. }
  1969. if (is_qla8022(ha)) {
  1970. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1971. qla4_8xxx_idc_lock(ha);
  1972. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1973. QLA82XX_DEV_FAILED);
  1974. qla4_8xxx_idc_unlock(ha);
  1975. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1976. qla4_8xxx_device_state_handler(ha);
  1977. }
  1978. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1979. qla4_8xxx_need_qsnt_handler(ha);
  1980. }
  1981. }
  1982. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1983. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1984. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1985. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1986. if (ql4xdontresethba) {
  1987. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1988. ha->host_no, __func__));
  1989. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1990. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1991. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1992. goto dpc_post_reset_ha;
  1993. }
  1994. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1995. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1996. qla4xxx_recover_adapter(ha);
  1997. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1998. uint8_t wait_time = RESET_INTR_TOV;
  1999. while ((readw(&ha->reg->ctrl_status) &
  2000. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  2001. if (--wait_time == 0)
  2002. break;
  2003. msleep(1000);
  2004. }
  2005. if (wait_time == 0)
  2006. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  2007. "bit not cleared-- resetting\n",
  2008. ha->host_no, __func__));
  2009. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  2010. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  2011. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  2012. status = qla4xxx_recover_adapter(ha);
  2013. }
  2014. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  2015. if (status == QLA_SUCCESS)
  2016. ha->isp_ops->enable_intrs(ha);
  2017. }
  2018. }
  2019. dpc_post_reset_ha:
  2020. /* ---- process AEN? --- */
  2021. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  2022. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  2023. /* ---- Get DHCP IP Address? --- */
  2024. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  2025. qla4xxx_get_dhcp_ip_address(ha);
  2026. /* ---- link change? --- */
  2027. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  2028. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  2029. /* ---- link down? --- */
  2030. qla4xxx_mark_all_devices_missing(ha);
  2031. } else {
  2032. /* ---- link up? --- *
  2033. * F/W will auto login to all devices ONLY ONCE after
  2034. * link up during driver initialization and runtime
  2035. * fatal error recovery. Therefore, the driver must
  2036. * manually relogin to devices when recovering from
  2037. * connection failures, logouts, expired KATO, etc. */
  2038. qla4xxx_relogin_all_devices(ha);
  2039. }
  2040. }
  2041. }
  2042. /**
  2043. * qla4xxx_free_adapter - release the adapter
  2044. * @ha: pointer to adapter structure
  2045. **/
  2046. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  2047. {
  2048. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  2049. /* Turn-off interrupts on the card. */
  2050. ha->isp_ops->disable_intrs(ha);
  2051. }
  2052. /* Remove timer thread, if present */
  2053. if (ha->timer_active)
  2054. qla4xxx_stop_timer(ha);
  2055. /* Kill the kernel thread for this host */
  2056. if (ha->dpc_thread)
  2057. destroy_workqueue(ha->dpc_thread);
  2058. /* Kill the kernel thread for this host */
  2059. if (ha->task_wq)
  2060. destroy_workqueue(ha->task_wq);
  2061. /* Put firmware in known state */
  2062. ha->isp_ops->reset_firmware(ha);
  2063. if (is_qla8022(ha)) {
  2064. qla4_8xxx_idc_lock(ha);
  2065. qla4_8xxx_clear_drv_active(ha);
  2066. qla4_8xxx_idc_unlock(ha);
  2067. }
  2068. /* Detach interrupts */
  2069. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  2070. qla4xxx_free_irqs(ha);
  2071. /* free extra memory */
  2072. qla4xxx_mem_free(ha);
  2073. }
  2074. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  2075. {
  2076. int status = 0;
  2077. uint8_t revision_id;
  2078. unsigned long mem_base, mem_len, db_base, db_len;
  2079. struct pci_dev *pdev = ha->pdev;
  2080. status = pci_request_regions(pdev, DRIVER_NAME);
  2081. if (status) {
  2082. printk(KERN_WARNING
  2083. "scsi(%ld) Failed to reserve PIO regions (%s) "
  2084. "status=%d\n", ha->host_no, pci_name(pdev), status);
  2085. goto iospace_error_exit;
  2086. }
  2087. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  2088. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  2089. __func__, revision_id));
  2090. ha->revision_id = revision_id;
  2091. /* remap phys address */
  2092. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  2093. mem_len = pci_resource_len(pdev, 0);
  2094. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  2095. __func__, mem_base, mem_len));
  2096. /* mapping of pcibase pointer */
  2097. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  2098. if (!ha->nx_pcibase) {
  2099. printk(KERN_ERR
  2100. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  2101. pci_release_regions(ha->pdev);
  2102. goto iospace_error_exit;
  2103. }
  2104. /* Mapping of IO base pointer, door bell read and write pointer */
  2105. /* mapping of IO base pointer */
  2106. ha->qla4_8xxx_reg =
  2107. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  2108. 0xbc000 + (ha->pdev->devfn << 11));
  2109. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  2110. db_len = pci_resource_len(pdev, 4);
  2111. ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  2112. QLA82XX_CAM_RAM_DB2);
  2113. return 0;
  2114. iospace_error_exit:
  2115. return -ENOMEM;
  2116. }
  2117. /***
  2118. * qla4xxx_iospace_config - maps registers
  2119. * @ha: pointer to adapter structure
  2120. *
  2121. * This routines maps HBA's registers from the pci address space
  2122. * into the kernel virtual address space for memory mapped i/o.
  2123. **/
  2124. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  2125. {
  2126. unsigned long pio, pio_len, pio_flags;
  2127. unsigned long mmio, mmio_len, mmio_flags;
  2128. pio = pci_resource_start(ha->pdev, 0);
  2129. pio_len = pci_resource_len(ha->pdev, 0);
  2130. pio_flags = pci_resource_flags(ha->pdev, 0);
  2131. if (pio_flags & IORESOURCE_IO) {
  2132. if (pio_len < MIN_IOBASE_LEN) {
  2133. ql4_printk(KERN_WARNING, ha,
  2134. "Invalid PCI I/O region size\n");
  2135. pio = 0;
  2136. }
  2137. } else {
  2138. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  2139. pio = 0;
  2140. }
  2141. /* Use MMIO operations for all accesses. */
  2142. mmio = pci_resource_start(ha->pdev, 1);
  2143. mmio_len = pci_resource_len(ha->pdev, 1);
  2144. mmio_flags = pci_resource_flags(ha->pdev, 1);
  2145. if (!(mmio_flags & IORESOURCE_MEM)) {
  2146. ql4_printk(KERN_ERR, ha,
  2147. "region #0 not an MMIO resource, aborting\n");
  2148. goto iospace_error_exit;
  2149. }
  2150. if (mmio_len < MIN_IOBASE_LEN) {
  2151. ql4_printk(KERN_ERR, ha,
  2152. "Invalid PCI mem region size, aborting\n");
  2153. goto iospace_error_exit;
  2154. }
  2155. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  2156. ql4_printk(KERN_WARNING, ha,
  2157. "Failed to reserve PIO/MMIO regions\n");
  2158. goto iospace_error_exit;
  2159. }
  2160. ha->pio_address = pio;
  2161. ha->pio_length = pio_len;
  2162. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  2163. if (!ha->reg) {
  2164. ql4_printk(KERN_ERR, ha,
  2165. "cannot remap MMIO, aborting\n");
  2166. goto iospace_error_exit;
  2167. }
  2168. return 0;
  2169. iospace_error_exit:
  2170. return -ENOMEM;
  2171. }
  2172. static struct isp_operations qla4xxx_isp_ops = {
  2173. .iospace_config = qla4xxx_iospace_config,
  2174. .pci_config = qla4xxx_pci_config,
  2175. .disable_intrs = qla4xxx_disable_intrs,
  2176. .enable_intrs = qla4xxx_enable_intrs,
  2177. .start_firmware = qla4xxx_start_firmware,
  2178. .intr_handler = qla4xxx_intr_handler,
  2179. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  2180. .reset_chip = qla4xxx_soft_reset,
  2181. .reset_firmware = qla4xxx_hw_reset,
  2182. .queue_iocb = qla4xxx_queue_iocb,
  2183. .complete_iocb = qla4xxx_complete_iocb,
  2184. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  2185. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  2186. .get_sys_info = qla4xxx_get_sys_info,
  2187. };
  2188. static struct isp_operations qla4_8xxx_isp_ops = {
  2189. .iospace_config = qla4_8xxx_iospace_config,
  2190. .pci_config = qla4_8xxx_pci_config,
  2191. .disable_intrs = qla4_8xxx_disable_intrs,
  2192. .enable_intrs = qla4_8xxx_enable_intrs,
  2193. .start_firmware = qla4_8xxx_load_risc,
  2194. .intr_handler = qla4_8xxx_intr_handler,
  2195. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  2196. .reset_chip = qla4_8xxx_isp_reset,
  2197. .reset_firmware = qla4_8xxx_stop_firmware,
  2198. .queue_iocb = qla4_8xxx_queue_iocb,
  2199. .complete_iocb = qla4_8xxx_complete_iocb,
  2200. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  2201. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  2202. .get_sys_info = qla4_8xxx_get_sys_info,
  2203. };
  2204. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2205. {
  2206. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  2207. }
  2208. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  2209. {
  2210. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  2211. }
  2212. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2213. {
  2214. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  2215. }
  2216. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  2217. {
  2218. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  2219. }
  2220. static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  2221. {
  2222. struct scsi_qla_host *ha = data;
  2223. char *str = buf;
  2224. int rc;
  2225. switch (type) {
  2226. case ISCSI_BOOT_ETH_FLAGS:
  2227. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  2228. break;
  2229. case ISCSI_BOOT_ETH_INDEX:
  2230. rc = sprintf(str, "0\n");
  2231. break;
  2232. case ISCSI_BOOT_ETH_MAC:
  2233. rc = sysfs_format_mac(str, ha->my_mac,
  2234. MAC_ADDR_LEN);
  2235. break;
  2236. default:
  2237. rc = -ENOSYS;
  2238. break;
  2239. }
  2240. return rc;
  2241. }
  2242. static mode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
  2243. {
  2244. int rc;
  2245. switch (type) {
  2246. case ISCSI_BOOT_ETH_FLAGS:
  2247. case ISCSI_BOOT_ETH_MAC:
  2248. case ISCSI_BOOT_ETH_INDEX:
  2249. rc = S_IRUGO;
  2250. break;
  2251. default:
  2252. rc = 0;
  2253. break;
  2254. }
  2255. return rc;
  2256. }
  2257. static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  2258. {
  2259. struct scsi_qla_host *ha = data;
  2260. char *str = buf;
  2261. int rc;
  2262. switch (type) {
  2263. case ISCSI_BOOT_INI_INITIATOR_NAME:
  2264. rc = sprintf(str, "%s\n", ha->name_string);
  2265. break;
  2266. default:
  2267. rc = -ENOSYS;
  2268. break;
  2269. }
  2270. return rc;
  2271. }
  2272. static mode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
  2273. {
  2274. int rc;
  2275. switch (type) {
  2276. case ISCSI_BOOT_INI_INITIATOR_NAME:
  2277. rc = S_IRUGO;
  2278. break;
  2279. default:
  2280. rc = 0;
  2281. break;
  2282. }
  2283. return rc;
  2284. }
  2285. static ssize_t
  2286. qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  2287. char *buf)
  2288. {
  2289. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  2290. char *str = buf;
  2291. int rc;
  2292. switch (type) {
  2293. case ISCSI_BOOT_TGT_NAME:
  2294. rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
  2295. break;
  2296. case ISCSI_BOOT_TGT_IP_ADDR:
  2297. if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  2298. rc = sprintf(buf, "%pI4\n",
  2299. &boot_conn->dest_ipaddr.ip_address);
  2300. else
  2301. rc = sprintf(str, "%pI6\n",
  2302. &boot_conn->dest_ipaddr.ip_address);
  2303. break;
  2304. case ISCSI_BOOT_TGT_PORT:
  2305. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  2306. break;
  2307. case ISCSI_BOOT_TGT_CHAP_NAME:
  2308. rc = sprintf(str, "%.*s\n",
  2309. boot_conn->chap.target_chap_name_length,
  2310. (char *)&boot_conn->chap.target_chap_name);
  2311. break;
  2312. case ISCSI_BOOT_TGT_CHAP_SECRET:
  2313. rc = sprintf(str, "%.*s\n",
  2314. boot_conn->chap.target_secret_length,
  2315. (char *)&boot_conn->chap.target_secret);
  2316. break;
  2317. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  2318. rc = sprintf(str, "%.*s\n",
  2319. boot_conn->chap.intr_chap_name_length,
  2320. (char *)&boot_conn->chap.intr_chap_name);
  2321. break;
  2322. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  2323. rc = sprintf(str, "%.*s\n",
  2324. boot_conn->chap.intr_secret_length,
  2325. (char *)&boot_conn->chap.intr_secret);
  2326. break;
  2327. case ISCSI_BOOT_TGT_FLAGS:
  2328. rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
  2329. break;
  2330. case ISCSI_BOOT_TGT_NIC_ASSOC:
  2331. rc = sprintf(str, "0\n");
  2332. break;
  2333. default:
  2334. rc = -ENOSYS;
  2335. break;
  2336. }
  2337. return rc;
  2338. }
  2339. static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  2340. {
  2341. struct scsi_qla_host *ha = data;
  2342. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  2343. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  2344. }
  2345. static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  2346. {
  2347. struct scsi_qla_host *ha = data;
  2348. struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  2349. return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  2350. }
  2351. static mode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
  2352. {
  2353. int rc;
  2354. switch (type) {
  2355. case ISCSI_BOOT_TGT_NAME:
  2356. case ISCSI_BOOT_TGT_IP_ADDR:
  2357. case ISCSI_BOOT_TGT_PORT:
  2358. case ISCSI_BOOT_TGT_CHAP_NAME:
  2359. case ISCSI_BOOT_TGT_CHAP_SECRET:
  2360. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  2361. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  2362. case ISCSI_BOOT_TGT_NIC_ASSOC:
  2363. case ISCSI_BOOT_TGT_FLAGS:
  2364. rc = S_IRUGO;
  2365. break;
  2366. default:
  2367. rc = 0;
  2368. break;
  2369. }
  2370. return rc;
  2371. }
  2372. static void qla4xxx_boot_release(void *data)
  2373. {
  2374. struct scsi_qla_host *ha = data;
  2375. scsi_host_put(ha->host);
  2376. }
  2377. static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  2378. {
  2379. dma_addr_t buf_dma;
  2380. uint32_t addr, pri_addr, sec_addr;
  2381. uint32_t offset;
  2382. uint16_t func_num;
  2383. uint8_t val;
  2384. uint8_t *buf = NULL;
  2385. size_t size = 13 * sizeof(uint8_t);
  2386. int ret = QLA_SUCCESS;
  2387. func_num = PCI_FUNC(ha->pdev->devfn);
  2388. DEBUG2(ql4_printk(KERN_INFO, ha,
  2389. "%s: Get FW boot info for 0x%x func %d\n", __func__,
  2390. (is_qla4032(ha) ? PCI_DEVICE_ID_QLOGIC_ISP4032 :
  2391. PCI_DEVICE_ID_QLOGIC_ISP8022), func_num));
  2392. if (is_qla4032(ha)) {
  2393. if (func_num == 1) {
  2394. addr = NVRAM_PORT0_BOOT_MODE;
  2395. pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  2396. sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  2397. } else if (func_num == 3) {
  2398. addr = NVRAM_PORT1_BOOT_MODE;
  2399. pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  2400. sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  2401. } else {
  2402. ret = QLA_ERROR;
  2403. goto exit_boot_info;
  2404. }
  2405. /* Check Boot Mode */
  2406. val = rd_nvram_byte(ha, addr);
  2407. if (!(val & 0x07)) {
  2408. DEBUG2(ql4_printk(KERN_ERR, ha,
  2409. "%s: Failed Boot options : 0x%x\n",
  2410. __func__, val));
  2411. ret = QLA_ERROR;
  2412. goto exit_boot_info;
  2413. }
  2414. /* get primary valid target index */
  2415. val = rd_nvram_byte(ha, pri_addr);
  2416. if (val & BIT_7)
  2417. ddb_index[0] = (val & 0x7f);
  2418. else
  2419. ddb_index[0] = 0;
  2420. /* get secondary valid target index */
  2421. val = rd_nvram_byte(ha, sec_addr);
  2422. if (val & BIT_7)
  2423. ddb_index[1] = (val & 0x7f);
  2424. else
  2425. ddb_index[1] = 1;
  2426. } else if (is_qla8022(ha)) {
  2427. buf = dma_alloc_coherent(&ha->pdev->dev, size,
  2428. &buf_dma, GFP_KERNEL);
  2429. if (!buf) {
  2430. DEBUG2(ql4_printk(KERN_ERR, ha,
  2431. "%s: Unable to allocate dma buffer\n",
  2432. __func__));
  2433. ret = QLA_ERROR;
  2434. goto exit_boot_info;
  2435. }
  2436. if (ha->port_num == 0)
  2437. offset = BOOT_PARAM_OFFSET_PORT0;
  2438. else if (ha->port_num == 1)
  2439. offset = BOOT_PARAM_OFFSET_PORT1;
  2440. else {
  2441. ret = QLA_ERROR;
  2442. goto exit_boot_info_free;
  2443. }
  2444. addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  2445. offset;
  2446. if (qla4xxx_get_flash(ha, buf_dma, addr,
  2447. 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  2448. DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  2449. "failed\n", ha->host_no, __func__));
  2450. ret = QLA_ERROR;
  2451. goto exit_boot_info_free;
  2452. }
  2453. /* Check Boot Mode */
  2454. if (!(buf[1] & 0x07)) {
  2455. DEBUG2(ql4_printk(KERN_INFO, ha,
  2456. "Failed: Boot options : 0x%x\n",
  2457. buf[1]));
  2458. ret = QLA_ERROR;
  2459. goto exit_boot_info_free;
  2460. }
  2461. /* get primary valid target index */
  2462. if (buf[2] & BIT_7)
  2463. ddb_index[0] = buf[2] & 0x7f;
  2464. else
  2465. ddb_index[0] = 0;
  2466. /* get secondary valid target index */
  2467. if (buf[11] & BIT_7)
  2468. ddb_index[1] = buf[11] & 0x7f;
  2469. else
  2470. ddb_index[1] = 1;
  2471. } else {
  2472. ret = QLA_ERROR;
  2473. goto exit_boot_info;
  2474. }
  2475. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  2476. " target ID %d\n", __func__, ddb_index[0],
  2477. ddb_index[1]));
  2478. exit_boot_info_free:
  2479. dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  2480. exit_boot_info:
  2481. return ret;
  2482. }
  2483. static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  2484. struct ql4_boot_session_info *boot_sess,
  2485. uint16_t ddb_index)
  2486. {
  2487. struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  2488. struct dev_db_entry *fw_ddb_entry;
  2489. dma_addr_t fw_ddb_entry_dma;
  2490. uint16_t idx;
  2491. uint16_t options;
  2492. int ret = QLA_SUCCESS;
  2493. fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2494. &fw_ddb_entry_dma, GFP_KERNEL);
  2495. if (!fw_ddb_entry) {
  2496. DEBUG2(ql4_printk(KERN_ERR, ha,
  2497. "%s: Unable to allocate dma buffer.\n",
  2498. __func__));
  2499. ret = QLA_ERROR;
  2500. return ret;
  2501. }
  2502. if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  2503. fw_ddb_entry_dma, ddb_index)) {
  2504. DEBUG2(ql4_printk(KERN_ERR, ha,
  2505. "%s: Flash DDB read Failed\n", __func__));
  2506. ret = QLA_ERROR;
  2507. goto exit_boot_target;
  2508. }
  2509. /* Update target name and IP from DDB */
  2510. memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  2511. min(sizeof(boot_sess->target_name),
  2512. sizeof(fw_ddb_entry->iscsi_name)));
  2513. options = le16_to_cpu(fw_ddb_entry->options);
  2514. if (options & DDB_OPT_IPV6_DEVICE) {
  2515. memcpy(&boot_conn->dest_ipaddr.ip_address,
  2516. &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  2517. } else {
  2518. boot_conn->dest_ipaddr.ip_type = 0x1;
  2519. memcpy(&boot_conn->dest_ipaddr.ip_address,
  2520. &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  2521. }
  2522. boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  2523. /* update chap information */
  2524. idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  2525. if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  2526. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
  2527. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  2528. target_chap_name,
  2529. (char *)&boot_conn->chap.target_secret,
  2530. idx);
  2531. if (ret) {
  2532. ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
  2533. ret = QLA_ERROR;
  2534. goto exit_boot_target;
  2535. }
  2536. boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  2537. boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  2538. }
  2539. if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  2540. DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
  2541. ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  2542. intr_chap_name,
  2543. (char *)&boot_conn->chap.intr_secret,
  2544. (idx + 1));
  2545. if (ret) {
  2546. ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
  2547. ret = QLA_ERROR;
  2548. goto exit_boot_target;
  2549. }
  2550. boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  2551. boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  2552. }
  2553. exit_boot_target:
  2554. dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  2555. fw_ddb_entry, fw_ddb_entry_dma);
  2556. return ret;
  2557. }
  2558. static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  2559. {
  2560. uint16_t ddb_index[2];
  2561. int ret = QLA_SUCCESS;
  2562. memset(ddb_index, 0, sizeof(ddb_index));
  2563. ret = get_fw_boot_info(ha, ddb_index);
  2564. if (ret != QLA_SUCCESS) {
  2565. DEBUG2(ql4_printk(KERN_ERR, ha,
  2566. "%s: Failed to set boot info.\n", __func__));
  2567. return ret;
  2568. }
  2569. ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
  2570. ddb_index[0]);
  2571. if (ret != QLA_SUCCESS) {
  2572. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  2573. "primary target\n", __func__));
  2574. }
  2575. ret = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
  2576. ddb_index[1]);
  2577. if (ret != QLA_SUCCESS) {
  2578. DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  2579. "secondary target\n", __func__));
  2580. }
  2581. return ret;
  2582. }
  2583. static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  2584. {
  2585. struct iscsi_boot_kobj *boot_kobj;
  2586. if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
  2587. return 0;
  2588. ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  2589. if (!ha->boot_kset)
  2590. goto kset_free;
  2591. if (!scsi_host_get(ha->host))
  2592. goto kset_free;
  2593. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  2594. qla4xxx_show_boot_tgt_pri_info,
  2595. qla4xxx_tgt_get_attr_visibility,
  2596. qla4xxx_boot_release);
  2597. if (!boot_kobj)
  2598. goto put_host;
  2599. if (!scsi_host_get(ha->host))
  2600. goto kset_free;
  2601. boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  2602. qla4xxx_show_boot_tgt_sec_info,
  2603. qla4xxx_tgt_get_attr_visibility,
  2604. qla4xxx_boot_release);
  2605. if (!boot_kobj)
  2606. goto put_host;
  2607. if (!scsi_host_get(ha->host))
  2608. goto kset_free;
  2609. boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  2610. qla4xxx_show_boot_ini_info,
  2611. qla4xxx_ini_get_attr_visibility,
  2612. qla4xxx_boot_release);
  2613. if (!boot_kobj)
  2614. goto put_host;
  2615. if (!scsi_host_get(ha->host))
  2616. goto kset_free;
  2617. boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  2618. qla4xxx_show_boot_eth_info,
  2619. qla4xxx_eth_get_attr_visibility,
  2620. qla4xxx_boot_release);
  2621. if (!boot_kobj)
  2622. goto put_host;
  2623. return 0;
  2624. put_host:
  2625. scsi_host_put(ha->host);
  2626. kset_free:
  2627. iscsi_boot_destroy_kset(ha->boot_kset);
  2628. return -ENOMEM;
  2629. }
  2630. /**
  2631. * qla4xxx_probe_adapter - callback function to probe HBA
  2632. * @pdev: pointer to pci_dev structure
  2633. * @pci_device_id: pointer to pci_device entry
  2634. *
  2635. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  2636. * It returns zero if successful. It also initializes all data necessary for
  2637. * the driver.
  2638. **/
  2639. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  2640. const struct pci_device_id *ent)
  2641. {
  2642. int ret = -ENODEV, status;
  2643. struct Scsi_Host *host;
  2644. struct scsi_qla_host *ha;
  2645. uint8_t init_retry_count = 0;
  2646. char buf[34];
  2647. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  2648. uint32_t dev_state;
  2649. if (pci_enable_device(pdev))
  2650. return -1;
  2651. host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
  2652. if (host == NULL) {
  2653. printk(KERN_WARNING
  2654. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  2655. goto probe_disable_device;
  2656. }
  2657. /* Clear our data area */
  2658. ha = to_qla_host(host);
  2659. memset(ha, 0, sizeof(*ha));
  2660. /* Save the information from PCI BIOS. */
  2661. ha->pdev = pdev;
  2662. ha->host = host;
  2663. ha->host_no = host->host_no;
  2664. pci_enable_pcie_error_reporting(pdev);
  2665. /* Setup Runtime configurable options */
  2666. if (is_qla8022(ha)) {
  2667. ha->isp_ops = &qla4_8xxx_isp_ops;
  2668. rwlock_init(&ha->hw_lock);
  2669. ha->qdr_sn_window = -1;
  2670. ha->ddr_mn_window = -1;
  2671. ha->curr_window = 255;
  2672. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  2673. nx_legacy_intr = &legacy_intr[ha->func_num];
  2674. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  2675. ha->nx_legacy_intr.tgt_status_reg =
  2676. nx_legacy_intr->tgt_status_reg;
  2677. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  2678. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  2679. } else {
  2680. ha->isp_ops = &qla4xxx_isp_ops;
  2681. }
  2682. /* Set EEH reset type to fundamental if required by hba */
  2683. if (is_qla8022(ha))
  2684. pdev->needs_freset = 1;
  2685. /* Configure PCI I/O space. */
  2686. ret = ha->isp_ops->iospace_config(ha);
  2687. if (ret)
  2688. goto probe_failed_ioconfig;
  2689. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  2690. pdev->device, pdev->irq, ha->reg);
  2691. qla4xxx_config_dma_addressing(ha);
  2692. /* Initialize lists and spinlocks. */
  2693. INIT_LIST_HEAD(&ha->free_srb_q);
  2694. mutex_init(&ha->mbox_sem);
  2695. init_completion(&ha->mbx_intr_comp);
  2696. spin_lock_init(&ha->hardware_lock);
  2697. /* Allocate dma buffers */
  2698. if (qla4xxx_mem_alloc(ha)) {
  2699. ql4_printk(KERN_WARNING, ha,
  2700. "[ERROR] Failed to allocate memory for adapter\n");
  2701. ret = -ENOMEM;
  2702. goto probe_failed;
  2703. }
  2704. host->cmd_per_lun = 3;
  2705. host->max_channel = 0;
  2706. host->max_lun = MAX_LUNS - 1;
  2707. host->max_id = MAX_TARGETS;
  2708. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  2709. host->can_queue = MAX_SRBS ;
  2710. host->transportt = qla4xxx_scsi_transport;
  2711. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  2712. if (ret) {
  2713. ql4_printk(KERN_WARNING, ha,
  2714. "%s: scsi_init_shared_tag_map failed\n", __func__);
  2715. goto probe_failed;
  2716. }
  2717. pci_set_drvdata(pdev, ha);
  2718. ret = scsi_add_host(host, &pdev->dev);
  2719. if (ret)
  2720. goto probe_failed;
  2721. if (is_qla8022(ha))
  2722. (void) qla4_8xxx_get_flash_info(ha);
  2723. /*
  2724. * Initialize the Host adapter request/response queues and
  2725. * firmware
  2726. * NOTE: interrupts enabled upon successful completion
  2727. */
  2728. status = qla4xxx_initialize_adapter(ha);
  2729. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  2730. init_retry_count++ < MAX_INIT_RETRIES) {
  2731. if (is_qla8022(ha)) {
  2732. qla4_8xxx_idc_lock(ha);
  2733. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  2734. qla4_8xxx_idc_unlock(ha);
  2735. if (dev_state == QLA82XX_DEV_FAILED) {
  2736. ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  2737. "initialize adapter. H/W is in failed state\n",
  2738. __func__);
  2739. break;
  2740. }
  2741. }
  2742. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  2743. "(%d)\n", __func__, init_retry_count));
  2744. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  2745. continue;
  2746. status = qla4xxx_initialize_adapter(ha);
  2747. }
  2748. if (!test_bit(AF_ONLINE, &ha->flags)) {
  2749. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  2750. if (is_qla8022(ha) && ql4xdontresethba) {
  2751. /* Put the device in failed state. */
  2752. DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
  2753. qla4_8xxx_idc_lock(ha);
  2754. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2755. QLA82XX_DEV_FAILED);
  2756. qla4_8xxx_idc_unlock(ha);
  2757. }
  2758. ret = -ENODEV;
  2759. goto remove_host;
  2760. }
  2761. /* Startup the kernel thread for this host adapter. */
  2762. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  2763. "qla4xxx_dpc\n", __func__));
  2764. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  2765. ha->dpc_thread = create_singlethread_workqueue(buf);
  2766. if (!ha->dpc_thread) {
  2767. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  2768. ret = -ENODEV;
  2769. goto remove_host;
  2770. }
  2771. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  2772. sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  2773. ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  2774. if (!ha->task_wq) {
  2775. ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
  2776. ret = -ENODEV;
  2777. goto remove_host;
  2778. }
  2779. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  2780. * (which is called indirectly by qla4xxx_initialize_adapter),
  2781. * so that irqs will be registered after crbinit but before
  2782. * mbx_intr_enable.
  2783. */
  2784. if (!is_qla8022(ha)) {
  2785. ret = qla4xxx_request_irqs(ha);
  2786. if (ret) {
  2787. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  2788. "interrupt %d already in use.\n", pdev->irq);
  2789. goto remove_host;
  2790. }
  2791. }
  2792. pci_save_state(ha->pdev);
  2793. ha->isp_ops->enable_intrs(ha);
  2794. /* Start timer thread. */
  2795. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  2796. set_bit(AF_INIT_DONE, &ha->flags);
  2797. printk(KERN_INFO
  2798. " QLogic iSCSI HBA Driver version: %s\n"
  2799. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  2800. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  2801. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  2802. ha->patch_number, ha->build_number);
  2803. if (qla4xxx_setup_boot_info(ha))
  2804. ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed\n",
  2805. __func__);
  2806. qla4xxx_create_ifaces(ha);
  2807. return 0;
  2808. remove_host:
  2809. scsi_remove_host(ha->host);
  2810. probe_failed:
  2811. qla4xxx_free_adapter(ha);
  2812. probe_failed_ioconfig:
  2813. pci_disable_pcie_error_reporting(pdev);
  2814. scsi_host_put(ha->host);
  2815. probe_disable_device:
  2816. pci_disable_device(pdev);
  2817. return ret;
  2818. }
  2819. /**
  2820. * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
  2821. * @ha: pointer to adapter structure
  2822. *
  2823. * Mark the other ISP-4xxx port to indicate that the driver is being removed,
  2824. * so that the other port will not re-initialize while in the process of
  2825. * removing the ha due to driver unload or hba hotplug.
  2826. **/
  2827. static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  2828. {
  2829. struct scsi_qla_host *other_ha = NULL;
  2830. struct pci_dev *other_pdev = NULL;
  2831. int fn = ISP4XXX_PCI_FN_2;
  2832. /*iscsi function numbers for ISP4xxx is 1 and 3*/
  2833. if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  2834. fn = ISP4XXX_PCI_FN_1;
  2835. other_pdev =
  2836. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  2837. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  2838. fn));
  2839. /* Get other_ha if other_pdev is valid and state is enable*/
  2840. if (other_pdev) {
  2841. if (atomic_read(&other_pdev->enable_cnt)) {
  2842. other_ha = pci_get_drvdata(other_pdev);
  2843. if (other_ha) {
  2844. set_bit(AF_HA_REMOVAL, &other_ha->flags);
  2845. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  2846. "Prevent %s reinit\n", __func__,
  2847. dev_name(&other_ha->pdev->dev)));
  2848. }
  2849. }
  2850. pci_dev_put(other_pdev);
  2851. }
  2852. }
  2853. /**
  2854. * qla4xxx_remove_adapter - calback function to remove adapter.
  2855. * @pci_dev: PCI device pointer
  2856. **/
  2857. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  2858. {
  2859. struct scsi_qla_host *ha;
  2860. ha = pci_get_drvdata(pdev);
  2861. if (!is_qla8022(ha))
  2862. qla4xxx_prevent_other_port_reinit(ha);
  2863. /* destroy iface from sysfs */
  2864. qla4xxx_destroy_ifaces(ha);
  2865. if (ha->boot_kset)
  2866. iscsi_boot_destroy_kset(ha->boot_kset);
  2867. scsi_remove_host(ha->host);
  2868. qla4xxx_free_adapter(ha);
  2869. scsi_host_put(ha->host);
  2870. pci_disable_pcie_error_reporting(pdev);
  2871. pci_disable_device(pdev);
  2872. pci_set_drvdata(pdev, NULL);
  2873. }
  2874. /**
  2875. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  2876. * @ha: HA context
  2877. *
  2878. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  2879. * supported addressing method.
  2880. */
  2881. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  2882. {
  2883. int retval;
  2884. /* Update our PCI device dma_mask for full 64 bit mask */
  2885. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  2886. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  2887. dev_dbg(&ha->pdev->dev,
  2888. "Failed to set 64 bit PCI consistent mask; "
  2889. "using 32 bit.\n");
  2890. retval = pci_set_consistent_dma_mask(ha->pdev,
  2891. DMA_BIT_MASK(32));
  2892. }
  2893. } else
  2894. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  2895. }
  2896. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  2897. {
  2898. struct iscsi_cls_session *cls_sess;
  2899. struct iscsi_session *sess;
  2900. struct ddb_entry *ddb;
  2901. int queue_depth = QL4_DEF_QDEPTH;
  2902. cls_sess = starget_to_session(sdev->sdev_target);
  2903. sess = cls_sess->dd_data;
  2904. ddb = sess->dd_data;
  2905. sdev->hostdata = ddb;
  2906. sdev->tagged_supported = 1;
  2907. if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  2908. queue_depth = ql4xmaxqdepth;
  2909. scsi_activate_tcq(sdev, queue_depth);
  2910. return 0;
  2911. }
  2912. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  2913. {
  2914. sdev->tagged_supported = 1;
  2915. return 0;
  2916. }
  2917. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  2918. {
  2919. scsi_deactivate_tcq(sdev, 1);
  2920. }
  2921. /**
  2922. * qla4xxx_del_from_active_array - returns an active srb
  2923. * @ha: Pointer to host adapter structure.
  2924. * @index: index into the active_array
  2925. *
  2926. * This routine removes and returns the srb at the specified index
  2927. **/
  2928. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  2929. uint32_t index)
  2930. {
  2931. struct srb *srb = NULL;
  2932. struct scsi_cmnd *cmd = NULL;
  2933. cmd = scsi_host_find_tag(ha->host, index);
  2934. if (!cmd)
  2935. return srb;
  2936. srb = (struct srb *)CMD_SP(cmd);
  2937. if (!srb)
  2938. return srb;
  2939. /* update counters */
  2940. if (srb->flags & SRB_DMA_VALID) {
  2941. ha->req_q_count += srb->iocb_cnt;
  2942. ha->iocb_cnt -= srb->iocb_cnt;
  2943. if (srb->cmd)
  2944. srb->cmd->host_scribble =
  2945. (unsigned char *)(unsigned long) MAX_SRBS;
  2946. }
  2947. return srb;
  2948. }
  2949. /**
  2950. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  2951. * @ha: Pointer to host adapter structure.
  2952. * @cmd: Scsi Command to wait on.
  2953. *
  2954. * This routine waits for the command to be returned by the Firmware
  2955. * for some max time.
  2956. **/
  2957. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  2958. struct scsi_cmnd *cmd)
  2959. {
  2960. int done = 0;
  2961. struct srb *rp;
  2962. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  2963. int ret = SUCCESS;
  2964. /* Dont wait on command if PCI error is being handled
  2965. * by PCI AER driver
  2966. */
  2967. if (unlikely(pci_channel_offline(ha->pdev)) ||
  2968. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  2969. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  2970. ha->host_no, __func__);
  2971. return ret;
  2972. }
  2973. do {
  2974. /* Checking to see if its returned to OS */
  2975. rp = (struct srb *) CMD_SP(cmd);
  2976. if (rp == NULL) {
  2977. done++;
  2978. break;
  2979. }
  2980. msleep(2000);
  2981. } while (max_wait_time--);
  2982. return done;
  2983. }
  2984. /**
  2985. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  2986. * @ha: Pointer to host adapter structure
  2987. **/
  2988. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  2989. {
  2990. unsigned long wait_online;
  2991. wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
  2992. while (time_before(jiffies, wait_online)) {
  2993. if (adapter_up(ha))
  2994. return QLA_SUCCESS;
  2995. msleep(2000);
  2996. }
  2997. return QLA_ERROR;
  2998. }
  2999. /**
  3000. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  3001. * @ha: pointer to HBA
  3002. * @t: target id
  3003. * @l: lun id
  3004. *
  3005. * This function waits for all outstanding commands to a lun to complete. It
  3006. * returns 0 if all pending commands are returned and 1 otherwise.
  3007. **/
  3008. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  3009. struct scsi_target *stgt,
  3010. struct scsi_device *sdev)
  3011. {
  3012. int cnt;
  3013. int status = 0;
  3014. struct scsi_cmnd *cmd;
  3015. /*
  3016. * Waiting for all commands for the designated target or dev
  3017. * in the active array
  3018. */
  3019. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  3020. cmd = scsi_host_find_tag(ha->host, cnt);
  3021. if (cmd && stgt == scsi_target(cmd->device) &&
  3022. (!sdev || sdev == cmd->device)) {
  3023. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  3024. status++;
  3025. break;
  3026. }
  3027. }
  3028. }
  3029. return status;
  3030. }
  3031. /**
  3032. * qla4xxx_eh_abort - callback for abort task.
  3033. * @cmd: Pointer to Linux's SCSI command structure
  3034. *
  3035. * This routine is called by the Linux OS to abort the specified
  3036. * command.
  3037. **/
  3038. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  3039. {
  3040. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  3041. unsigned int id = cmd->device->id;
  3042. unsigned int lun = cmd->device->lun;
  3043. unsigned long flags;
  3044. struct srb *srb = NULL;
  3045. int ret = SUCCESS;
  3046. int wait = 0;
  3047. ql4_printk(KERN_INFO, ha,
  3048. "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
  3049. ha->host_no, id, lun, cmd);
  3050. spin_lock_irqsave(&ha->hardware_lock, flags);
  3051. srb = (struct srb *) CMD_SP(cmd);
  3052. if (!srb) {
  3053. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3054. return SUCCESS;
  3055. }
  3056. kref_get(&srb->srb_ref);
  3057. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3058. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  3059. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  3060. ha->host_no, id, lun));
  3061. ret = FAILED;
  3062. } else {
  3063. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  3064. ha->host_no, id, lun));
  3065. wait = 1;
  3066. }
  3067. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  3068. /* Wait for command to complete */
  3069. if (wait) {
  3070. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  3071. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  3072. ha->host_no, id, lun));
  3073. ret = FAILED;
  3074. }
  3075. }
  3076. ql4_printk(KERN_INFO, ha,
  3077. "scsi%ld:%d:%d: Abort command - %s\n",
  3078. ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
  3079. return ret;
  3080. }
  3081. /**
  3082. * qla4xxx_eh_device_reset - callback for target reset.
  3083. * @cmd: Pointer to Linux's SCSI command structure
  3084. *
  3085. * This routine is called by the Linux OS to reset all luns on the
  3086. * specified target.
  3087. **/
  3088. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  3089. {
  3090. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  3091. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  3092. int ret = FAILED, stat;
  3093. if (!ddb_entry)
  3094. return ret;
  3095. ret = iscsi_block_scsi_eh(cmd);
  3096. if (ret)
  3097. return ret;
  3098. ret = FAILED;
  3099. ql4_printk(KERN_INFO, ha,
  3100. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  3101. cmd->device->channel, cmd->device->id, cmd->device->lun);
  3102. DEBUG2(printk(KERN_INFO
  3103. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  3104. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  3105. cmd, jiffies, cmd->request->timeout / HZ,
  3106. ha->dpc_flags, cmd->result, cmd->allowed));
  3107. /* FIXME: wait for hba to go online */
  3108. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  3109. if (stat != QLA_SUCCESS) {
  3110. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  3111. goto eh_dev_reset_done;
  3112. }
  3113. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  3114. cmd->device)) {
  3115. ql4_printk(KERN_INFO, ha,
  3116. "DEVICE RESET FAILED - waiting for "
  3117. "commands.\n");
  3118. goto eh_dev_reset_done;
  3119. }
  3120. /* Send marker. */
  3121. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  3122. MM_LUN_RESET) != QLA_SUCCESS)
  3123. goto eh_dev_reset_done;
  3124. ql4_printk(KERN_INFO, ha,
  3125. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  3126. ha->host_no, cmd->device->channel, cmd->device->id,
  3127. cmd->device->lun);
  3128. ret = SUCCESS;
  3129. eh_dev_reset_done:
  3130. return ret;
  3131. }
  3132. /**
  3133. * qla4xxx_eh_target_reset - callback for target reset.
  3134. * @cmd: Pointer to Linux's SCSI command structure
  3135. *
  3136. * This routine is called by the Linux OS to reset the target.
  3137. **/
  3138. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  3139. {
  3140. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  3141. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  3142. int stat, ret;
  3143. if (!ddb_entry)
  3144. return FAILED;
  3145. ret = iscsi_block_scsi_eh(cmd);
  3146. if (ret)
  3147. return ret;
  3148. starget_printk(KERN_INFO, scsi_target(cmd->device),
  3149. "WARM TARGET RESET ISSUED.\n");
  3150. DEBUG2(printk(KERN_INFO
  3151. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  3152. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  3153. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  3154. ha->dpc_flags, cmd->result, cmd->allowed));
  3155. stat = qla4xxx_reset_target(ha, ddb_entry);
  3156. if (stat != QLA_SUCCESS) {
  3157. starget_printk(KERN_INFO, scsi_target(cmd->device),
  3158. "WARM TARGET RESET FAILED.\n");
  3159. return FAILED;
  3160. }
  3161. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  3162. NULL)) {
  3163. starget_printk(KERN_INFO, scsi_target(cmd->device),
  3164. "WARM TARGET DEVICE RESET FAILED - "
  3165. "waiting for commands.\n");
  3166. return FAILED;
  3167. }
  3168. /* Send marker. */
  3169. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  3170. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  3171. starget_printk(KERN_INFO, scsi_target(cmd->device),
  3172. "WARM TARGET DEVICE RESET FAILED - "
  3173. "marker iocb failed.\n");
  3174. return FAILED;
  3175. }
  3176. starget_printk(KERN_INFO, scsi_target(cmd->device),
  3177. "WARM TARGET RESET SUCCEEDED.\n");
  3178. return SUCCESS;
  3179. }
  3180. /**
  3181. * qla4xxx_eh_host_reset - kernel callback
  3182. * @cmd: Pointer to Linux's SCSI command structure
  3183. *
  3184. * This routine is invoked by the Linux kernel to perform fatal error
  3185. * recovery on the specified adapter.
  3186. **/
  3187. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  3188. {
  3189. int return_status = FAILED;
  3190. struct scsi_qla_host *ha;
  3191. ha = to_qla_host(cmd->device->host);
  3192. if (ql4xdontresethba) {
  3193. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  3194. ha->host_no, __func__));
  3195. return FAILED;
  3196. }
  3197. ql4_printk(KERN_INFO, ha,
  3198. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  3199. cmd->device->channel, cmd->device->id, cmd->device->lun);
  3200. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  3201. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  3202. "DEAD.\n", ha->host_no, cmd->device->channel,
  3203. __func__));
  3204. return FAILED;
  3205. }
  3206. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  3207. if (is_qla8022(ha))
  3208. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  3209. else
  3210. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  3211. }
  3212. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  3213. return_status = SUCCESS;
  3214. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  3215. return_status == FAILED ? "FAILED" : "SUCCEEDED");
  3216. return return_status;
  3217. }
  3218. /* PCI AER driver recovers from all correctable errors w/o
  3219. * driver intervention. For uncorrectable errors PCI AER
  3220. * driver calls the following device driver's callbacks
  3221. *
  3222. * - Fatal Errors - link_reset
  3223. * - Non-Fatal Errors - driver's pci_error_detected() which
  3224. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  3225. *
  3226. * PCI AER driver calls
  3227. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  3228. * returns RECOVERED or NEED_RESET if fw_hung
  3229. * NEED_RESET - driver's slot_reset()
  3230. * DISCONNECT - device is dead & cannot recover
  3231. * RECOVERED - driver's pci_resume()
  3232. */
  3233. static pci_ers_result_t
  3234. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  3235. {
  3236. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  3237. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  3238. ha->host_no, __func__, state);
  3239. if (!is_aer_supported(ha))
  3240. return PCI_ERS_RESULT_NONE;
  3241. switch (state) {
  3242. case pci_channel_io_normal:
  3243. clear_bit(AF_EEH_BUSY, &ha->flags);
  3244. return PCI_ERS_RESULT_CAN_RECOVER;
  3245. case pci_channel_io_frozen:
  3246. set_bit(AF_EEH_BUSY, &ha->flags);
  3247. qla4xxx_mailbox_premature_completion(ha);
  3248. qla4xxx_free_irqs(ha);
  3249. pci_disable_device(pdev);
  3250. /* Return back all IOs */
  3251. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  3252. return PCI_ERS_RESULT_NEED_RESET;
  3253. case pci_channel_io_perm_failure:
  3254. set_bit(AF_EEH_BUSY, &ha->flags);
  3255. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  3256. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  3257. return PCI_ERS_RESULT_DISCONNECT;
  3258. }
  3259. return PCI_ERS_RESULT_NEED_RESET;
  3260. }
  3261. /**
  3262. * qla4xxx_pci_mmio_enabled() gets called if
  3263. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  3264. * and read/write to the device still works.
  3265. **/
  3266. static pci_ers_result_t
  3267. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  3268. {
  3269. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  3270. if (!is_aer_supported(ha))
  3271. return PCI_ERS_RESULT_NONE;
  3272. return PCI_ERS_RESULT_RECOVERED;
  3273. }
  3274. static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  3275. {
  3276. uint32_t rval = QLA_ERROR;
  3277. uint32_t ret = 0;
  3278. int fn;
  3279. struct pci_dev *other_pdev = NULL;
  3280. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  3281. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  3282. if (test_bit(AF_ONLINE, &ha->flags)) {
  3283. clear_bit(AF_ONLINE, &ha->flags);
  3284. clear_bit(AF_LINK_UP, &ha->flags);
  3285. iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
  3286. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  3287. }
  3288. fn = PCI_FUNC(ha->pdev->devfn);
  3289. while (fn > 0) {
  3290. fn--;
  3291. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  3292. "func %x\n", ha->host_no, __func__, fn);
  3293. /* Get the pci device given the domain, bus,
  3294. * slot/function number */
  3295. other_pdev =
  3296. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  3297. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  3298. fn));
  3299. if (!other_pdev)
  3300. continue;
  3301. if (atomic_read(&other_pdev->enable_cnt)) {
  3302. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  3303. "func in enabled state%x\n", ha->host_no,
  3304. __func__, fn);
  3305. pci_dev_put(other_pdev);
  3306. break;
  3307. }
  3308. pci_dev_put(other_pdev);
  3309. }
  3310. /* The first function on the card, the reset owner will
  3311. * start & initialize the firmware. The other functions
  3312. * on the card will reset the firmware context
  3313. */
  3314. if (!fn) {
  3315. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  3316. "0x%x is the owner\n", ha->host_no, __func__,
  3317. ha->pdev->devfn);
  3318. qla4_8xxx_idc_lock(ha);
  3319. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3320. QLA82XX_DEV_COLD);
  3321. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  3322. QLA82XX_IDC_VERSION);
  3323. qla4_8xxx_idc_unlock(ha);
  3324. clear_bit(AF_FW_RECOVERY, &ha->flags);
  3325. rval = qla4xxx_initialize_adapter(ha);
  3326. qla4_8xxx_idc_lock(ha);
  3327. if (rval != QLA_SUCCESS) {
  3328. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  3329. "FAILED\n", ha->host_no, __func__);
  3330. qla4_8xxx_clear_drv_active(ha);
  3331. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3332. QLA82XX_DEV_FAILED);
  3333. } else {
  3334. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  3335. "READY\n", ha->host_no, __func__);
  3336. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3337. QLA82XX_DEV_READY);
  3338. /* Clear driver state register */
  3339. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  3340. qla4_8xxx_set_drv_active(ha);
  3341. ret = qla4xxx_request_irqs(ha);
  3342. if (ret) {
  3343. ql4_printk(KERN_WARNING, ha, "Failed to "
  3344. "reserve interrupt %d already in use.\n",
  3345. ha->pdev->irq);
  3346. rval = QLA_ERROR;
  3347. } else {
  3348. ha->isp_ops->enable_intrs(ha);
  3349. rval = QLA_SUCCESS;
  3350. }
  3351. }
  3352. qla4_8xxx_idc_unlock(ha);
  3353. } else {
  3354. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  3355. "the reset owner\n", ha->host_no, __func__,
  3356. ha->pdev->devfn);
  3357. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  3358. QLA82XX_DEV_READY)) {
  3359. clear_bit(AF_FW_RECOVERY, &ha->flags);
  3360. rval = qla4xxx_initialize_adapter(ha);
  3361. if (rval == QLA_SUCCESS) {
  3362. ret = qla4xxx_request_irqs(ha);
  3363. if (ret) {
  3364. ql4_printk(KERN_WARNING, ha, "Failed to"
  3365. " reserve interrupt %d already in"
  3366. " use.\n", ha->pdev->irq);
  3367. rval = QLA_ERROR;
  3368. } else {
  3369. ha->isp_ops->enable_intrs(ha);
  3370. rval = QLA_SUCCESS;
  3371. }
  3372. }
  3373. qla4_8xxx_idc_lock(ha);
  3374. qla4_8xxx_set_drv_active(ha);
  3375. qla4_8xxx_idc_unlock(ha);
  3376. }
  3377. }
  3378. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  3379. return rval;
  3380. }
  3381. static pci_ers_result_t
  3382. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  3383. {
  3384. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  3385. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  3386. int rc;
  3387. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  3388. ha->host_no, __func__);
  3389. if (!is_aer_supported(ha))
  3390. return PCI_ERS_RESULT_NONE;
  3391. /* Restore the saved state of PCIe device -
  3392. * BAR registers, PCI Config space, PCIX, MSI,
  3393. * IOV states
  3394. */
  3395. pci_restore_state(pdev);
  3396. /* pci_restore_state() clears the saved_state flag of the device
  3397. * save restored state which resets saved_state flag
  3398. */
  3399. pci_save_state(pdev);
  3400. /* Initialize device or resume if in suspended state */
  3401. rc = pci_enable_device(pdev);
  3402. if (rc) {
  3403. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
  3404. "device after reset\n", ha->host_no, __func__);
  3405. goto exit_slot_reset;
  3406. }
  3407. ha->isp_ops->disable_intrs(ha);
  3408. if (is_qla8022(ha)) {
  3409. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  3410. ret = PCI_ERS_RESULT_RECOVERED;
  3411. goto exit_slot_reset;
  3412. } else
  3413. goto exit_slot_reset;
  3414. }
  3415. exit_slot_reset:
  3416. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  3417. "device after reset\n", ha->host_no, __func__, ret);
  3418. return ret;
  3419. }
  3420. static void
  3421. qla4xxx_pci_resume(struct pci_dev *pdev)
  3422. {
  3423. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  3424. int ret;
  3425. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  3426. ha->host_no, __func__);
  3427. ret = qla4xxx_wait_for_hba_online(ha);
  3428. if (ret != QLA_SUCCESS) {
  3429. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  3430. "resume I/O from slot/link_reset\n", ha->host_no,
  3431. __func__);
  3432. }
  3433. pci_cleanup_aer_uncorrect_error_status(pdev);
  3434. clear_bit(AF_EEH_BUSY, &ha->flags);
  3435. }
  3436. static struct pci_error_handlers qla4xxx_err_handler = {
  3437. .error_detected = qla4xxx_pci_error_detected,
  3438. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  3439. .slot_reset = qla4xxx_pci_slot_reset,
  3440. .resume = qla4xxx_pci_resume,
  3441. };
  3442. static struct pci_device_id qla4xxx_pci_tbl[] = {
  3443. {
  3444. .vendor = PCI_VENDOR_ID_QLOGIC,
  3445. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  3446. .subvendor = PCI_ANY_ID,
  3447. .subdevice = PCI_ANY_ID,
  3448. },
  3449. {
  3450. .vendor = PCI_VENDOR_ID_QLOGIC,
  3451. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  3452. .subvendor = PCI_ANY_ID,
  3453. .subdevice = PCI_ANY_ID,
  3454. },
  3455. {
  3456. .vendor = PCI_VENDOR_ID_QLOGIC,
  3457. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  3458. .subvendor = PCI_ANY_ID,
  3459. .subdevice = PCI_ANY_ID,
  3460. },
  3461. {
  3462. .vendor = PCI_VENDOR_ID_QLOGIC,
  3463. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  3464. .subvendor = PCI_ANY_ID,
  3465. .subdevice = PCI_ANY_ID,
  3466. },
  3467. {0, 0},
  3468. };
  3469. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  3470. static struct pci_driver qla4xxx_pci_driver = {
  3471. .name = DRIVER_NAME,
  3472. .id_table = qla4xxx_pci_tbl,
  3473. .probe = qla4xxx_probe_adapter,
  3474. .remove = qla4xxx_remove_adapter,
  3475. .err_handler = &qla4xxx_err_handler,
  3476. };
  3477. static int __init qla4xxx_module_init(void)
  3478. {
  3479. int ret;
  3480. /* Allocate cache for SRBs. */
  3481. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  3482. SLAB_HWCACHE_ALIGN, NULL);
  3483. if (srb_cachep == NULL) {
  3484. printk(KERN_ERR
  3485. "%s: Unable to allocate SRB cache..."
  3486. "Failing load!\n", DRIVER_NAME);
  3487. ret = -ENOMEM;
  3488. goto no_srp_cache;
  3489. }
  3490. /* Derive version string. */
  3491. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  3492. if (ql4xextended_error_logging)
  3493. strcat(qla4xxx_version_str, "-debug");
  3494. qla4xxx_scsi_transport =
  3495. iscsi_register_transport(&qla4xxx_iscsi_transport);
  3496. if (!qla4xxx_scsi_transport){
  3497. ret = -ENODEV;
  3498. goto release_srb_cache;
  3499. }
  3500. ret = pci_register_driver(&qla4xxx_pci_driver);
  3501. if (ret)
  3502. goto unregister_transport;
  3503. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  3504. return 0;
  3505. unregister_transport:
  3506. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  3507. release_srb_cache:
  3508. kmem_cache_destroy(srb_cachep);
  3509. no_srp_cache:
  3510. return ret;
  3511. }
  3512. static void __exit qla4xxx_module_exit(void)
  3513. {
  3514. pci_unregister_driver(&qla4xxx_pci_driver);
  3515. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  3516. kmem_cache_destroy(srb_cachep);
  3517. }
  3518. module_init(qla4xxx_module_init);
  3519. module_exit(qla4xxx_module_exit);
  3520. MODULE_AUTHOR("QLogic Corporation");
  3521. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  3522. MODULE_LICENSE("GPL");
  3523. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);