ql4_os.c 113 KB

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