qla_init.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/vmalloc.h>
  10. #include <scsi/scsi_transport_fc.h>
  11. #include "qla_devtbl.h"
  12. /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
  13. #ifndef EXT_IS_LUN_BIT_SET
  14. #define EXT_IS_LUN_BIT_SET(P,L) \
  15. (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
  16. #define EXT_SET_LUN_BIT(P,L) \
  17. ((P)->mask[L/8] |= (0x80 >> (L%8)))
  18. #endif
  19. /*
  20. * QLogic ISP2x00 Hardware Support Function Prototypes.
  21. */
  22. static int qla2x00_isp_firmware(scsi_qla_host_t *);
  23. static void qla2x00_resize_request_q(scsi_qla_host_t *);
  24. static int qla2x00_setup_chip(scsi_qla_host_t *);
  25. static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
  26. static int qla2x00_init_rings(scsi_qla_host_t *);
  27. static int qla2x00_fw_ready(scsi_qla_host_t *);
  28. static int qla2x00_configure_hba(scsi_qla_host_t *);
  29. static int qla2x00_configure_loop(scsi_qla_host_t *);
  30. static int qla2x00_configure_local_loop(scsi_qla_host_t *);
  31. static void qla2x00_update_fcport(scsi_qla_host_t *, fc_port_t *);
  32. static int qla2x00_configure_fabric(scsi_qla_host_t *);
  33. static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
  34. static int qla2x00_device_resync(scsi_qla_host_t *);
  35. static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
  36. uint16_t *);
  37. static int qla2x00_restart_isp(scsi_qla_host_t *);
  38. /****************************************************************************/
  39. /* QLogic ISP2x00 Hardware Support Functions. */
  40. /****************************************************************************/
  41. /*
  42. * qla2x00_initialize_adapter
  43. * Initialize board.
  44. *
  45. * Input:
  46. * ha = adapter block pointer.
  47. *
  48. * Returns:
  49. * 0 = success
  50. */
  51. int
  52. qla2x00_initialize_adapter(scsi_qla_host_t *ha)
  53. {
  54. int rval;
  55. uint8_t restart_risc = 0;
  56. uint8_t retry;
  57. uint32_t wait_time;
  58. /* Clear adapter flags. */
  59. ha->flags.online = 0;
  60. ha->flags.reset_active = 0;
  61. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  62. atomic_set(&ha->loop_state, LOOP_DOWN);
  63. ha->device_flags = 0;
  64. ha->dpc_flags = 0;
  65. ha->flags.management_server_logged_in = 0;
  66. ha->marker_needed = 0;
  67. ha->mbx_flags = 0;
  68. ha->isp_abort_cnt = 0;
  69. ha->beacon_blink_led = 0;
  70. set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
  71. qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  72. rval = ha->isp_ops.pci_config(ha);
  73. if (rval) {
  74. DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
  75. ha->host_no));
  76. return (rval);
  77. }
  78. ha->isp_ops.reset_chip(ha);
  79. qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
  80. ha->isp_ops.nvram_config(ha);
  81. qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
  82. retry = 10;
  83. /*
  84. * Try to configure the loop.
  85. */
  86. do {
  87. restart_risc = 0;
  88. /* If firmware needs to be loaded */
  89. if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
  90. if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
  91. rval = qla2x00_setup_chip(ha);
  92. }
  93. }
  94. if (rval == QLA_SUCCESS &&
  95. (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
  96. check_fw_ready_again:
  97. /*
  98. * Wait for a successful LIP up to a maximum
  99. * of (in seconds): RISC login timeout value,
  100. * RISC retry count value, and port down retry
  101. * value OR a minimum of 4 seconds OR If no
  102. * cable, only 5 seconds.
  103. */
  104. rval = qla2x00_fw_ready(ha);
  105. if (rval == QLA_SUCCESS) {
  106. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  107. /* Issue a marker after FW becomes ready. */
  108. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  109. /*
  110. * Wait at most MAX_TARGET RSCNs for a stable
  111. * link.
  112. */
  113. wait_time = 256;
  114. do {
  115. clear_bit(LOOP_RESYNC_NEEDED,
  116. &ha->dpc_flags);
  117. rval = qla2x00_configure_loop(ha);
  118. if (test_and_clear_bit(ISP_ABORT_NEEDED,
  119. &ha->dpc_flags)) {
  120. restart_risc = 1;
  121. break;
  122. }
  123. /*
  124. * If loop state change while we were
  125. * discoverying devices then wait for
  126. * LIP to complete
  127. */
  128. if (atomic_read(&ha->loop_state) !=
  129. LOOP_READY && retry--) {
  130. goto check_fw_ready_again;
  131. }
  132. wait_time--;
  133. } while (!atomic_read(&ha->loop_down_timer) &&
  134. retry &&
  135. wait_time &&
  136. (test_bit(LOOP_RESYNC_NEEDED,
  137. &ha->dpc_flags)));
  138. if (wait_time == 0)
  139. rval = QLA_FUNCTION_FAILED;
  140. } else if (ha->device_flags & DFLG_NO_CABLE)
  141. /* If no cable, then all is good. */
  142. rval = QLA_SUCCESS;
  143. }
  144. } while (restart_risc && retry--);
  145. if (rval == QLA_SUCCESS) {
  146. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  147. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  148. ha->marker_needed = 0;
  149. ha->flags.online = 1;
  150. } else {
  151. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  152. }
  153. return (rval);
  154. }
  155. /**
  156. * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
  157. * @ha: HA context
  158. *
  159. * Returns 0 on success.
  160. */
  161. int
  162. qla2100_pci_config(scsi_qla_host_t *ha)
  163. {
  164. uint16_t w, mwi;
  165. uint32_t d;
  166. unsigned long flags;
  167. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  168. pci_set_master(ha->pdev);
  169. mwi = 0;
  170. if (pci_set_mwi(ha->pdev))
  171. mwi = PCI_COMMAND_INVALIDATE;
  172. pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
  173. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  174. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  175. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  176. /* Reset expansion ROM address decode enable */
  177. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  178. d &= ~PCI_ROM_ADDRESS_ENABLE;
  179. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  180. /* Get PCI bus information. */
  181. spin_lock_irqsave(&ha->hardware_lock, flags);
  182. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  183. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  184. return QLA_SUCCESS;
  185. }
  186. /**
  187. * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
  188. * @ha: HA context
  189. *
  190. * Returns 0 on success.
  191. */
  192. int
  193. qla2300_pci_config(scsi_qla_host_t *ha)
  194. {
  195. uint16_t w, mwi;
  196. uint32_t d;
  197. unsigned long flags = 0;
  198. uint32_t cnt;
  199. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  200. pci_set_master(ha->pdev);
  201. mwi = 0;
  202. if (pci_set_mwi(ha->pdev))
  203. mwi = PCI_COMMAND_INVALIDATE;
  204. pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
  205. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  206. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  207. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  208. w &= ~PCI_COMMAND_INTX_DISABLE;
  209. /*
  210. * If this is a 2300 card and not 2312, reset the
  211. * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
  212. * the 2310 also reports itself as a 2300 so we need to get the
  213. * fb revision level -- a 6 indicates it really is a 2300 and
  214. * not a 2310.
  215. */
  216. if (IS_QLA2300(ha)) {
  217. spin_lock_irqsave(&ha->hardware_lock, flags);
  218. /* Pause RISC. */
  219. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  220. for (cnt = 0; cnt < 30000; cnt++) {
  221. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  222. break;
  223. udelay(10);
  224. }
  225. /* Select FPM registers. */
  226. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  227. RD_REG_WORD(&reg->ctrl_status);
  228. /* Get the fb rev level */
  229. ha->fb_rev = RD_FB_CMD_REG(ha, reg);
  230. if (ha->fb_rev == FPM_2300)
  231. w &= ~PCI_COMMAND_INVALIDATE;
  232. /* Deselect FPM registers. */
  233. WRT_REG_WORD(&reg->ctrl_status, 0x0);
  234. RD_REG_WORD(&reg->ctrl_status);
  235. /* Release RISC module. */
  236. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  237. for (cnt = 0; cnt < 30000; cnt++) {
  238. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
  239. break;
  240. udelay(10);
  241. }
  242. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  243. }
  244. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  245. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  246. /* Reset expansion ROM address decode enable */
  247. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  248. d &= ~PCI_ROM_ADDRESS_ENABLE;
  249. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  250. /* Get PCI bus information. */
  251. spin_lock_irqsave(&ha->hardware_lock, flags);
  252. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  253. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  254. return QLA_SUCCESS;
  255. }
  256. /**
  257. * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
  258. * @ha: HA context
  259. *
  260. * Returns 0 on success.
  261. */
  262. int
  263. qla24xx_pci_config(scsi_qla_host_t *ha)
  264. {
  265. uint16_t w, mwi;
  266. uint32_t d;
  267. unsigned long flags = 0;
  268. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  269. int pcix_cmd_reg, pcie_dctl_reg;
  270. pci_set_master(ha->pdev);
  271. mwi = 0;
  272. if (pci_set_mwi(ha->pdev))
  273. mwi = PCI_COMMAND_INVALIDATE;
  274. pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
  275. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  276. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  277. w &= ~PCI_COMMAND_INTX_DISABLE;
  278. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  279. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  280. /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
  281. pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
  282. if (pcix_cmd_reg) {
  283. uint16_t pcix_cmd;
  284. pcix_cmd_reg += PCI_X_CMD;
  285. pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
  286. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  287. pcix_cmd |= 0x0008;
  288. pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
  289. }
  290. /* PCIe -- adjust Maximum Read Request Size (2048). */
  291. pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  292. if (pcie_dctl_reg) {
  293. uint16_t pcie_dctl;
  294. pcie_dctl_reg += PCI_EXP_DEVCTL;
  295. pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
  296. pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
  297. pcie_dctl |= 0x4000;
  298. pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
  299. }
  300. /* Reset expansion ROM address decode enable */
  301. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  302. d &= ~PCI_ROM_ADDRESS_ENABLE;
  303. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  304. /* Get PCI bus information. */
  305. spin_lock_irqsave(&ha->hardware_lock, flags);
  306. ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
  307. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  308. return QLA_SUCCESS;
  309. }
  310. /**
  311. * qla2x00_isp_firmware() - Choose firmware image.
  312. * @ha: HA context
  313. *
  314. * Returns 0 on success.
  315. */
  316. static int
  317. qla2x00_isp_firmware(scsi_qla_host_t *ha)
  318. {
  319. int rval;
  320. /* Assume loading risc code */
  321. rval = QLA_FUNCTION_FAILED;
  322. if (ha->flags.disable_risc_code_load) {
  323. DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
  324. ha->host_no));
  325. qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
  326. /* Verify checksum of loaded RISC code. */
  327. rval = qla2x00_verify_checksum(ha,
  328. IS_QLA24XX(ha) || IS_QLA25XX(ha) ? RISC_SADDRESS :
  329. *ha->brd_info->fw_info[0].fwstart);
  330. }
  331. if (rval) {
  332. DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
  333. ha->host_no));
  334. }
  335. return (rval);
  336. }
  337. /**
  338. * qla2x00_reset_chip() - Reset ISP chip.
  339. * @ha: HA context
  340. *
  341. * Returns 0 on success.
  342. */
  343. void
  344. qla2x00_reset_chip(scsi_qla_host_t *ha)
  345. {
  346. unsigned long flags = 0;
  347. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  348. uint32_t cnt;
  349. unsigned long mbx_flags = 0;
  350. uint16_t cmd;
  351. ha->isp_ops.disable_intrs(ha);
  352. spin_lock_irqsave(&ha->hardware_lock, flags);
  353. /* Turn off master enable */
  354. cmd = 0;
  355. pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
  356. cmd &= ~PCI_COMMAND_MASTER;
  357. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  358. if (!IS_QLA2100(ha)) {
  359. /* Pause RISC. */
  360. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  361. if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
  362. for (cnt = 0; cnt < 30000; cnt++) {
  363. if ((RD_REG_WORD(&reg->hccr) &
  364. HCCR_RISC_PAUSE) != 0)
  365. break;
  366. udelay(100);
  367. }
  368. } else {
  369. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  370. udelay(10);
  371. }
  372. /* Select FPM registers. */
  373. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  374. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  375. /* FPM Soft Reset. */
  376. WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
  377. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  378. /* Toggle Fpm Reset. */
  379. if (!IS_QLA2200(ha)) {
  380. WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
  381. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  382. }
  383. /* Select frame buffer registers. */
  384. WRT_REG_WORD(&reg->ctrl_status, 0x10);
  385. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  386. /* Reset frame buffer FIFOs. */
  387. if (IS_QLA2200(ha)) {
  388. WRT_FB_CMD_REG(ha, reg, 0xa000);
  389. RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
  390. } else {
  391. WRT_FB_CMD_REG(ha, reg, 0x00fc);
  392. /* Read back fb_cmd until zero or 3 seconds max */
  393. for (cnt = 0; cnt < 3000; cnt++) {
  394. if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
  395. break;
  396. udelay(100);
  397. }
  398. }
  399. /* Select RISC module registers. */
  400. WRT_REG_WORD(&reg->ctrl_status, 0);
  401. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  402. /* Reset RISC processor. */
  403. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  404. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  405. /* Release RISC processor. */
  406. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  407. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  408. }
  409. WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
  410. WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
  411. /* Reset ISP chip. */
  412. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  413. /* Wait for RISC to recover from reset. */
  414. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  415. /*
  416. * It is necessary to for a delay here since the card doesn't
  417. * respond to PCI reads during a reset. On some architectures
  418. * this will result in an MCA.
  419. */
  420. udelay(20);
  421. for (cnt = 30000; cnt; cnt--) {
  422. if ((RD_REG_WORD(&reg->ctrl_status) &
  423. CSR_ISP_SOFT_RESET) == 0)
  424. break;
  425. udelay(100);
  426. }
  427. } else
  428. udelay(10);
  429. /* Reset RISC processor. */
  430. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  431. WRT_REG_WORD(&reg->semaphore, 0);
  432. /* Release RISC processor. */
  433. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  434. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  435. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  436. for (cnt = 0; cnt < 30000; cnt++) {
  437. if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
  438. spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
  439. if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
  440. if (!(test_bit(ABORT_ISP_ACTIVE,
  441. &ha->dpc_flags)))
  442. spin_unlock_irqrestore(
  443. &ha->mbx_reg_lock, mbx_flags);
  444. break;
  445. }
  446. if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
  447. spin_unlock_irqrestore(&ha->mbx_reg_lock,
  448. mbx_flags);
  449. udelay(100);
  450. }
  451. } else
  452. udelay(100);
  453. /* Turn on master enable */
  454. cmd |= PCI_COMMAND_MASTER;
  455. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  456. /* Disable RISC pause on FPM parity error. */
  457. if (!IS_QLA2100(ha)) {
  458. WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
  459. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  460. }
  461. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  462. }
  463. /**
  464. * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
  465. * @ha: HA context
  466. *
  467. * Returns 0 on success.
  468. */
  469. static inline void
  470. qla24xx_reset_risc(scsi_qla_host_t *ha)
  471. {
  472. unsigned long flags = 0;
  473. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  474. uint32_t cnt, d2;
  475. uint16_t wd;
  476. spin_lock_irqsave(&ha->hardware_lock, flags);
  477. /* Reset RISC. */
  478. WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  479. for (cnt = 0; cnt < 30000; cnt++) {
  480. if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
  481. break;
  482. udelay(10);
  483. }
  484. WRT_REG_DWORD(&reg->ctrl_status,
  485. CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  486. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  487. udelay(100);
  488. /* Wait for firmware to complete NVRAM accesses. */
  489. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  490. for (cnt = 10000 ; cnt && d2; cnt--) {
  491. udelay(5);
  492. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  493. barrier();
  494. }
  495. /* Wait for soft-reset to complete. */
  496. d2 = RD_REG_DWORD(&reg->ctrl_status);
  497. for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
  498. udelay(5);
  499. d2 = RD_REG_DWORD(&reg->ctrl_status);
  500. barrier();
  501. }
  502. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  503. RD_REG_DWORD(&reg->hccr);
  504. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  505. RD_REG_DWORD(&reg->hccr);
  506. WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
  507. RD_REG_DWORD(&reg->hccr);
  508. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  509. for (cnt = 6000000 ; cnt && d2; cnt--) {
  510. udelay(5);
  511. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  512. barrier();
  513. }
  514. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  515. }
  516. /**
  517. * qla24xx_reset_chip() - Reset ISP24xx chip.
  518. * @ha: HA context
  519. *
  520. * Returns 0 on success.
  521. */
  522. void
  523. qla24xx_reset_chip(scsi_qla_host_t *ha)
  524. {
  525. ha->isp_ops.disable_intrs(ha);
  526. /* Perform RISC reset. */
  527. qla24xx_reset_risc(ha);
  528. }
  529. /**
  530. * qla2x00_chip_diag() - Test chip for proper operation.
  531. * @ha: HA context
  532. *
  533. * Returns 0 on success.
  534. */
  535. int
  536. qla2x00_chip_diag(scsi_qla_host_t *ha)
  537. {
  538. int rval;
  539. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  540. unsigned long flags = 0;
  541. uint16_t data;
  542. uint32_t cnt;
  543. uint16_t mb[5];
  544. /* Assume a failed state */
  545. rval = QLA_FUNCTION_FAILED;
  546. DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
  547. ha->host_no, (u_long)&reg->flash_address));
  548. spin_lock_irqsave(&ha->hardware_lock, flags);
  549. /* Reset ISP chip. */
  550. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  551. /*
  552. * We need to have a delay here since the card will not respond while
  553. * in reset causing an MCA on some architectures.
  554. */
  555. udelay(20);
  556. data = qla2x00_debounce_register(&reg->ctrl_status);
  557. for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
  558. udelay(5);
  559. data = RD_REG_WORD(&reg->ctrl_status);
  560. barrier();
  561. }
  562. if (!cnt)
  563. goto chip_diag_failed;
  564. DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
  565. ha->host_no));
  566. /* Reset RISC processor. */
  567. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  568. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  569. /* Workaround for QLA2312 PCI parity error */
  570. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  571. data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
  572. for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
  573. udelay(5);
  574. data = RD_MAILBOX_REG(ha, reg, 0);
  575. barrier();
  576. }
  577. } else
  578. udelay(10);
  579. if (!cnt)
  580. goto chip_diag_failed;
  581. /* Check product ID of chip */
  582. DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
  583. mb[1] = RD_MAILBOX_REG(ha, reg, 1);
  584. mb[2] = RD_MAILBOX_REG(ha, reg, 2);
  585. mb[3] = RD_MAILBOX_REG(ha, reg, 3);
  586. mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
  587. if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
  588. mb[3] != PROD_ID_3) {
  589. qla_printk(KERN_WARNING, ha,
  590. "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
  591. goto chip_diag_failed;
  592. }
  593. ha->product_id[0] = mb[1];
  594. ha->product_id[1] = mb[2];
  595. ha->product_id[2] = mb[3];
  596. ha->product_id[3] = mb[4];
  597. /* Adjust fw RISC transfer size */
  598. if (ha->request_q_length > 1024)
  599. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  600. else
  601. ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
  602. ha->request_q_length;
  603. if (IS_QLA2200(ha) &&
  604. RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
  605. /* Limit firmware transfer size with a 2200A */
  606. DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
  607. ha->host_no));
  608. ha->fw_transfer_size = 128;
  609. }
  610. /* Wrap Incoming Mailboxes Test. */
  611. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  612. DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
  613. rval = qla2x00_mbx_reg_test(ha);
  614. if (rval) {
  615. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  616. ha->host_no));
  617. qla_printk(KERN_WARNING, ha,
  618. "Failed mailbox send register test\n");
  619. }
  620. else {
  621. /* Flag a successful rval */
  622. rval = QLA_SUCCESS;
  623. }
  624. spin_lock_irqsave(&ha->hardware_lock, flags);
  625. chip_diag_failed:
  626. if (rval)
  627. DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
  628. "****\n", ha->host_no));
  629. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  630. return (rval);
  631. }
  632. /**
  633. * qla24xx_chip_diag() - Test ISP24xx for proper operation.
  634. * @ha: HA context
  635. *
  636. * Returns 0 on success.
  637. */
  638. int
  639. qla24xx_chip_diag(scsi_qla_host_t *ha)
  640. {
  641. int rval;
  642. /* Perform RISC reset. */
  643. qla24xx_reset_risc(ha);
  644. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  645. rval = qla2x00_mbx_reg_test(ha);
  646. if (rval) {
  647. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  648. ha->host_no));
  649. qla_printk(KERN_WARNING, ha,
  650. "Failed mailbox send register test\n");
  651. } else {
  652. /* Flag a successful rval */
  653. rval = QLA_SUCCESS;
  654. }
  655. return rval;
  656. }
  657. static void
  658. qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
  659. {
  660. ha->fw_dumped = 0;
  661. ha->fw_dump24_len = sizeof(struct qla24xx_fw_dump);
  662. ha->fw_dump24_len += (ha->fw_memory_size - 0x100000) * sizeof(uint32_t);
  663. ha->fw_dump24 = vmalloc(ha->fw_dump24_len);
  664. if (ha->fw_dump24)
  665. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware "
  666. "dump...\n", ha->fw_dump24_len / 1024);
  667. else
  668. qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
  669. "firmware dump!!!\n", ha->fw_dump24_len / 1024);
  670. }
  671. /**
  672. * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
  673. * @ha: HA context
  674. *
  675. * Returns 0 on success.
  676. */
  677. static void
  678. qla2x00_resize_request_q(scsi_qla_host_t *ha)
  679. {
  680. int rval;
  681. uint16_t fw_iocb_cnt = 0;
  682. uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
  683. dma_addr_t request_dma;
  684. request_t *request_ring;
  685. /* Valid only on recent ISPs. */
  686. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  687. return;
  688. if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
  689. qla2x00_alloc_fw_dump(ha);
  690. /* Retrieve IOCB counts available to the firmware. */
  691. rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
  692. if (rval)
  693. return;
  694. /* No point in continuing if current settings are sufficient. */
  695. if (fw_iocb_cnt < 1024)
  696. return;
  697. if (ha->request_q_length >= request_q_length)
  698. return;
  699. /* Attempt to claim larger area for request queue. */
  700. request_ring = dma_alloc_coherent(&ha->pdev->dev,
  701. (request_q_length + 1) * sizeof(request_t), &request_dma,
  702. GFP_KERNEL);
  703. if (request_ring == NULL)
  704. return;
  705. /* Resize successful, report extensions. */
  706. qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
  707. (ha->fw_memory_size + 1) / 1024);
  708. qla_printk(KERN_INFO, ha, "Resizing request queue depth "
  709. "(%d -> %d)...\n", ha->request_q_length, request_q_length);
  710. /* Clear old allocations. */
  711. dma_free_coherent(&ha->pdev->dev,
  712. (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
  713. ha->request_dma);
  714. /* Begin using larger queue. */
  715. ha->request_q_length = request_q_length;
  716. ha->request_ring = request_ring;
  717. ha->request_dma = request_dma;
  718. }
  719. /**
  720. * qla2x00_setup_chip() - Load and start RISC firmware.
  721. * @ha: HA context
  722. *
  723. * Returns 0 on success.
  724. */
  725. static int
  726. qla2x00_setup_chip(scsi_qla_host_t *ha)
  727. {
  728. int rval;
  729. uint32_t srisc_address = 0;
  730. /* Load firmware sequences */
  731. rval = ha->isp_ops.load_risc(ha, &srisc_address);
  732. if (rval == QLA_SUCCESS) {
  733. DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
  734. "code.\n", ha->host_no));
  735. rval = qla2x00_verify_checksum(ha, srisc_address);
  736. if (rval == QLA_SUCCESS) {
  737. /* Start firmware execution. */
  738. DEBUG(printk("scsi(%ld): Checksum OK, start "
  739. "firmware.\n", ha->host_no));
  740. rval = qla2x00_execute_fw(ha, srisc_address);
  741. /* Retrieve firmware information. */
  742. if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
  743. qla2x00_get_fw_version(ha,
  744. &ha->fw_major_version,
  745. &ha->fw_minor_version,
  746. &ha->fw_subminor_version,
  747. &ha->fw_attributes, &ha->fw_memory_size);
  748. qla2x00_resize_request_q(ha);
  749. }
  750. } else {
  751. DEBUG2(printk(KERN_INFO
  752. "scsi(%ld): ISP Firmware failed checksum.\n",
  753. ha->host_no));
  754. }
  755. }
  756. if (rval) {
  757. DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
  758. ha->host_no));
  759. }
  760. return (rval);
  761. }
  762. /**
  763. * qla2x00_init_response_q_entries() - Initializes response queue entries.
  764. * @ha: HA context
  765. *
  766. * Beginning of request ring has initialization control block already built
  767. * by nvram config routine.
  768. *
  769. * Returns 0 on success.
  770. */
  771. static void
  772. qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
  773. {
  774. uint16_t cnt;
  775. response_t *pkt;
  776. pkt = ha->response_ring_ptr;
  777. for (cnt = 0; cnt < ha->response_q_length; cnt++) {
  778. pkt->signature = RESPONSE_PROCESSED;
  779. pkt++;
  780. }
  781. }
  782. /**
  783. * qla2x00_update_fw_options() - Read and process firmware options.
  784. * @ha: HA context
  785. *
  786. * Returns 0 on success.
  787. */
  788. void
  789. qla2x00_update_fw_options(scsi_qla_host_t *ha)
  790. {
  791. uint16_t swing, emphasis, tx_sens, rx_sens;
  792. memset(ha->fw_options, 0, sizeof(ha->fw_options));
  793. qla2x00_get_fw_options(ha, ha->fw_options);
  794. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  795. return;
  796. /* Serial Link options. */
  797. DEBUG3(printk("scsi(%ld): Serial link options:\n",
  798. ha->host_no));
  799. DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
  800. sizeof(ha->fw_seriallink_options)));
  801. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  802. if (ha->fw_seriallink_options[3] & BIT_2) {
  803. ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
  804. /* 1G settings */
  805. swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
  806. emphasis = (ha->fw_seriallink_options[2] &
  807. (BIT_4 | BIT_3)) >> 3;
  808. tx_sens = ha->fw_seriallink_options[0] &
  809. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  810. rx_sens = (ha->fw_seriallink_options[0] &
  811. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  812. ha->fw_options[10] = (emphasis << 14) | (swing << 8);
  813. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  814. if (rx_sens == 0x0)
  815. rx_sens = 0x3;
  816. ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
  817. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  818. ha->fw_options[10] |= BIT_5 |
  819. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  820. (tx_sens & (BIT_1 | BIT_0));
  821. /* 2G settings */
  822. swing = (ha->fw_seriallink_options[2] &
  823. (BIT_7 | BIT_6 | BIT_5)) >> 5;
  824. emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
  825. tx_sens = ha->fw_seriallink_options[1] &
  826. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  827. rx_sens = (ha->fw_seriallink_options[1] &
  828. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  829. ha->fw_options[11] = (emphasis << 14) | (swing << 8);
  830. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  831. if (rx_sens == 0x0)
  832. rx_sens = 0x3;
  833. ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
  834. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  835. ha->fw_options[11] |= BIT_5 |
  836. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  837. (tx_sens & (BIT_1 | BIT_0));
  838. }
  839. /* FCP2 options. */
  840. /* Return command IOCBs without waiting for an ABTS to complete. */
  841. ha->fw_options[3] |= BIT_13;
  842. /* LED scheme. */
  843. if (ha->flags.enable_led_scheme)
  844. ha->fw_options[2] |= BIT_12;
  845. /* Update firmware options. */
  846. qla2x00_set_fw_options(ha, ha->fw_options);
  847. }
  848. void
  849. qla24xx_update_fw_options(scsi_qla_host_t *ha)
  850. {
  851. int rval;
  852. /* Update Serial Link options. */
  853. if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
  854. return;
  855. rval = qla2x00_set_serdes_params(ha,
  856. le16_to_cpu(ha->fw_seriallink_options24[1]),
  857. le16_to_cpu(ha->fw_seriallink_options24[2]),
  858. le16_to_cpu(ha->fw_seriallink_options24[3]));
  859. if (rval != QLA_SUCCESS) {
  860. qla_printk(KERN_WARNING, ha,
  861. "Unable to update Serial Link options (%x).\n", rval);
  862. }
  863. }
  864. void
  865. qla2x00_config_rings(struct scsi_qla_host *ha)
  866. {
  867. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  868. /* Setup ring parameters in initialization control block. */
  869. ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
  870. ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
  871. ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
  872. ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
  873. ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  874. ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  875. ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  876. ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  877. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
  878. WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
  879. WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
  880. WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
  881. RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
  882. }
  883. void
  884. qla24xx_config_rings(struct scsi_qla_host *ha)
  885. {
  886. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  887. struct init_cb_24xx *icb;
  888. /* Setup ring parameters in initialization control block. */
  889. icb = (struct init_cb_24xx *)ha->init_cb;
  890. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  891. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  892. icb->request_q_length = cpu_to_le16(ha->request_q_length);
  893. icb->response_q_length = cpu_to_le16(ha->response_q_length);
  894. icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  895. icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  896. icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  897. icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  898. WRT_REG_DWORD(&reg->req_q_in, 0);
  899. WRT_REG_DWORD(&reg->req_q_out, 0);
  900. WRT_REG_DWORD(&reg->rsp_q_in, 0);
  901. WRT_REG_DWORD(&reg->rsp_q_out, 0);
  902. RD_REG_DWORD(&reg->rsp_q_out);
  903. }
  904. /**
  905. * qla2x00_init_rings() - Initializes firmware.
  906. * @ha: HA context
  907. *
  908. * Beginning of request ring has initialization control block already built
  909. * by nvram config routine.
  910. *
  911. * Returns 0 on success.
  912. */
  913. static int
  914. qla2x00_init_rings(scsi_qla_host_t *ha)
  915. {
  916. int rval;
  917. unsigned long flags = 0;
  918. int cnt;
  919. spin_lock_irqsave(&ha->hardware_lock, flags);
  920. /* Clear outstanding commands array. */
  921. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
  922. ha->outstanding_cmds[cnt] = NULL;
  923. ha->current_outstanding_cmd = 0;
  924. /* Clear RSCN queue. */
  925. ha->rscn_in_ptr = 0;
  926. ha->rscn_out_ptr = 0;
  927. /* Initialize firmware. */
  928. ha->request_ring_ptr = ha->request_ring;
  929. ha->req_ring_index = 0;
  930. ha->req_q_cnt = ha->request_q_length;
  931. ha->response_ring_ptr = ha->response_ring;
  932. ha->rsp_ring_index = 0;
  933. /* Initialize response queue entries */
  934. qla2x00_init_response_q_entries(ha);
  935. ha->isp_ops.config_rings(ha);
  936. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  937. /* Update any ISP specific firmware options before initialization. */
  938. ha->isp_ops.update_fw_options(ha);
  939. DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
  940. rval = qla2x00_init_firmware(ha, ha->init_cb_size);
  941. if (rval) {
  942. DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
  943. ha->host_no));
  944. } else {
  945. DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
  946. ha->host_no));
  947. }
  948. return (rval);
  949. }
  950. /**
  951. * qla2x00_fw_ready() - Waits for firmware ready.
  952. * @ha: HA context
  953. *
  954. * Returns 0 on success.
  955. */
  956. static int
  957. qla2x00_fw_ready(scsi_qla_host_t *ha)
  958. {
  959. int rval;
  960. unsigned long wtime, mtime;
  961. uint16_t min_wait; /* Minimum wait time if loop is down */
  962. uint16_t wait_time; /* Wait time if loop is coming ready */
  963. uint16_t fw_state;
  964. rval = QLA_SUCCESS;
  965. /* 20 seconds for loop down. */
  966. min_wait = 20;
  967. /*
  968. * Firmware should take at most one RATOV to login, plus 5 seconds for
  969. * our own processing.
  970. */
  971. if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
  972. wait_time = min_wait;
  973. }
  974. /* Min wait time if loop down */
  975. mtime = jiffies + (min_wait * HZ);
  976. /* wait time before firmware ready */
  977. wtime = jiffies + (wait_time * HZ);
  978. /* Wait for ISP to finish LIP */
  979. if (!ha->flags.init_done)
  980. qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
  981. DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
  982. ha->host_no));
  983. do {
  984. rval = qla2x00_get_firmware_state(ha, &fw_state);
  985. if (rval == QLA_SUCCESS) {
  986. if (fw_state < FSTATE_LOSS_OF_SYNC) {
  987. ha->device_flags &= ~DFLG_NO_CABLE;
  988. }
  989. if (fw_state == FSTATE_READY) {
  990. DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
  991. ha->host_no));
  992. qla2x00_get_retry_cnt(ha, &ha->retry_count,
  993. &ha->login_timeout, &ha->r_a_tov);
  994. rval = QLA_SUCCESS;
  995. break;
  996. }
  997. rval = QLA_FUNCTION_FAILED;
  998. if (atomic_read(&ha->loop_down_timer) &&
  999. (fw_state >= FSTATE_LOSS_OF_SYNC ||
  1000. fw_state == FSTATE_WAIT_AL_PA)) {
  1001. /* Loop down. Timeout on min_wait for states
  1002. * other than Wait for Login.
  1003. */
  1004. if (time_after_eq(jiffies, mtime)) {
  1005. qla_printk(KERN_INFO, ha,
  1006. "Cable is unplugged...\n");
  1007. ha->device_flags |= DFLG_NO_CABLE;
  1008. break;
  1009. }
  1010. }
  1011. } else {
  1012. /* Mailbox cmd failed. Timeout on min_wait. */
  1013. if (time_after_eq(jiffies, mtime))
  1014. break;
  1015. }
  1016. if (time_after_eq(jiffies, wtime))
  1017. break;
  1018. /* Delay for a while */
  1019. msleep(500);
  1020. DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1021. ha->host_no, fw_state, jiffies));
  1022. } while (1);
  1023. DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1024. ha->host_no, fw_state, jiffies));
  1025. if (rval) {
  1026. DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
  1027. ha->host_no));
  1028. }
  1029. return (rval);
  1030. }
  1031. /*
  1032. * qla2x00_configure_hba
  1033. * Setup adapter context.
  1034. *
  1035. * Input:
  1036. * ha = adapter state pointer.
  1037. *
  1038. * Returns:
  1039. * 0 = success
  1040. *
  1041. * Context:
  1042. * Kernel context.
  1043. */
  1044. static int
  1045. qla2x00_configure_hba(scsi_qla_host_t *ha)
  1046. {
  1047. int rval;
  1048. uint16_t loop_id;
  1049. uint16_t topo;
  1050. uint8_t al_pa;
  1051. uint8_t area;
  1052. uint8_t domain;
  1053. char connect_type[22];
  1054. /* Get host addresses. */
  1055. rval = qla2x00_get_adapter_id(ha,
  1056. &loop_id, &al_pa, &area, &domain, &topo);
  1057. if (rval != QLA_SUCCESS) {
  1058. if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
  1059. (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
  1060. DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
  1061. __func__, ha->host_no));
  1062. } else {
  1063. qla_printk(KERN_WARNING, ha,
  1064. "ERROR -- Unable to get host loop ID.\n");
  1065. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1066. }
  1067. return (rval);
  1068. }
  1069. if (topo == 4) {
  1070. qla_printk(KERN_INFO, ha,
  1071. "Cannot get topology - retrying.\n");
  1072. return (QLA_FUNCTION_FAILED);
  1073. }
  1074. ha->loop_id = loop_id;
  1075. /* initialize */
  1076. ha->min_external_loopid = SNS_FIRST_LOOP_ID;
  1077. ha->operating_mode = LOOP;
  1078. switch (topo) {
  1079. case 0:
  1080. DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
  1081. ha->host_no));
  1082. ha->current_topology = ISP_CFG_NL;
  1083. strcpy(connect_type, "(Loop)");
  1084. break;
  1085. case 1:
  1086. DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
  1087. ha->host_no));
  1088. ha->current_topology = ISP_CFG_FL;
  1089. strcpy(connect_type, "(FL_Port)");
  1090. break;
  1091. case 2:
  1092. DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
  1093. ha->host_no));
  1094. ha->operating_mode = P2P;
  1095. ha->current_topology = ISP_CFG_N;
  1096. strcpy(connect_type, "(N_Port-to-N_Port)");
  1097. break;
  1098. case 3:
  1099. DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
  1100. ha->host_no));
  1101. ha->operating_mode = P2P;
  1102. ha->current_topology = ISP_CFG_F;
  1103. strcpy(connect_type, "(F_Port)");
  1104. break;
  1105. default:
  1106. DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
  1107. "Using NL.\n",
  1108. ha->host_no, topo));
  1109. ha->current_topology = ISP_CFG_NL;
  1110. strcpy(connect_type, "(Loop)");
  1111. break;
  1112. }
  1113. /* Save Host port and loop ID. */
  1114. /* byte order - Big Endian */
  1115. ha->d_id.b.domain = domain;
  1116. ha->d_id.b.area = area;
  1117. ha->d_id.b.al_pa = al_pa;
  1118. if (!ha->flags.init_done)
  1119. qla_printk(KERN_INFO, ha,
  1120. "Topology - %s, Host Loop address 0x%x\n",
  1121. connect_type, ha->loop_id);
  1122. if (rval) {
  1123. DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
  1124. } else {
  1125. DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
  1126. }
  1127. return(rval);
  1128. }
  1129. /*
  1130. * NVRAM configuration for ISP 2xxx
  1131. *
  1132. * Input:
  1133. * ha = adapter block pointer.
  1134. *
  1135. * Output:
  1136. * initialization control block in response_ring
  1137. * host adapters parameters in host adapter block
  1138. *
  1139. * Returns:
  1140. * 0 = success.
  1141. */
  1142. int
  1143. qla2x00_nvram_config(scsi_qla_host_t *ha)
  1144. {
  1145. int rval;
  1146. uint8_t chksum = 0;
  1147. uint16_t cnt;
  1148. uint8_t *dptr1, *dptr2;
  1149. init_cb_t *icb = ha->init_cb;
  1150. nvram_t *nv = (nvram_t *)ha->request_ring;
  1151. uint8_t *ptr = (uint8_t *)ha->request_ring;
  1152. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1153. rval = QLA_SUCCESS;
  1154. /* Determine NVRAM starting address. */
  1155. ha->nvram_size = sizeof(nvram_t);
  1156. ha->nvram_base = 0;
  1157. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
  1158. if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
  1159. ha->nvram_base = 0x80;
  1160. /* Get NVRAM data and calculate checksum. */
  1161. ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
  1162. for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
  1163. chksum += *ptr++;
  1164. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  1165. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  1166. ha->nvram_size));
  1167. /* Bad NVRAM data, set defaults parameters. */
  1168. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
  1169. nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
  1170. /* Reset NVRAM data. */
  1171. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  1172. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  1173. nv->nvram_version);
  1174. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  1175. "invalid -- WWPN) defaults.\n");
  1176. /*
  1177. * Set default initialization control block.
  1178. */
  1179. memset(nv, 0, ha->nvram_size);
  1180. nv->parameter_block_version = ICB_VERSION;
  1181. if (IS_QLA23XX(ha)) {
  1182. nv->firmware_options[0] = BIT_2 | BIT_1;
  1183. nv->firmware_options[1] = BIT_7 | BIT_5;
  1184. nv->add_firmware_options[0] = BIT_5;
  1185. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1186. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1187. nv->special_options[1] = BIT_7;
  1188. } else if (IS_QLA2200(ha)) {
  1189. nv->firmware_options[0] = BIT_2 | BIT_1;
  1190. nv->firmware_options[1] = BIT_7 | BIT_5;
  1191. nv->add_firmware_options[0] = BIT_5;
  1192. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1193. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1194. } else if (IS_QLA2100(ha)) {
  1195. nv->firmware_options[0] = BIT_3 | BIT_1;
  1196. nv->firmware_options[1] = BIT_5;
  1197. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1198. }
  1199. nv->max_iocb_allocation = __constant_cpu_to_le16(256);
  1200. nv->execution_throttle = __constant_cpu_to_le16(16);
  1201. nv->retry_count = 8;
  1202. nv->retry_delay = 1;
  1203. nv->port_name[0] = 33;
  1204. nv->port_name[3] = 224;
  1205. nv->port_name[4] = 139;
  1206. nv->login_timeout = 4;
  1207. /*
  1208. * Set default host adapter parameters
  1209. */
  1210. nv->host_p[1] = BIT_2;
  1211. nv->reset_delay = 5;
  1212. nv->port_down_retry_count = 8;
  1213. nv->max_luns_per_target = __constant_cpu_to_le16(8);
  1214. nv->link_down_timeout = 60;
  1215. rval = 1;
  1216. }
  1217. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1218. /*
  1219. * The SN2 does not provide BIOS emulation which means you can't change
  1220. * potentially bogus BIOS settings. Force the use of default settings
  1221. * for link rate and frame size. Hope that the rest of the settings
  1222. * are valid.
  1223. */
  1224. if (ia64_platform_is("sn2")) {
  1225. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1226. if (IS_QLA23XX(ha))
  1227. nv->special_options[1] = BIT_7;
  1228. }
  1229. #endif
  1230. /* Reset Initialization control block */
  1231. memset(icb, 0, ha->init_cb_size);
  1232. /*
  1233. * Setup driver NVRAM options.
  1234. */
  1235. nv->firmware_options[0] |= (BIT_6 | BIT_1);
  1236. nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
  1237. nv->firmware_options[1] |= (BIT_5 | BIT_0);
  1238. nv->firmware_options[1] &= ~BIT_4;
  1239. if (IS_QLA23XX(ha)) {
  1240. nv->firmware_options[0] |= BIT_2;
  1241. nv->firmware_options[0] &= ~BIT_3;
  1242. nv->add_firmware_options[1] |= BIT_5 | BIT_4;
  1243. if (IS_QLA2300(ha)) {
  1244. if (ha->fb_rev == FPM_2310) {
  1245. strcpy(ha->model_number, "QLA2310");
  1246. } else {
  1247. strcpy(ha->model_number, "QLA2300");
  1248. }
  1249. } else {
  1250. if (rval == 0 &&
  1251. memcmp(nv->model_number, BINZERO,
  1252. sizeof(nv->model_number)) != 0) {
  1253. char *st, *en;
  1254. strncpy(ha->model_number, nv->model_number,
  1255. sizeof(nv->model_number));
  1256. st = en = ha->model_number;
  1257. en += sizeof(nv->model_number) - 1;
  1258. while (en > st) {
  1259. if (*en != 0x20 && *en != 0x00)
  1260. break;
  1261. *en-- = '\0';
  1262. }
  1263. } else {
  1264. uint16_t index;
  1265. index = (ha->pdev->subsystem_device & 0xff);
  1266. if (index < QLA_MODEL_NAMES) {
  1267. strcpy(ha->model_number,
  1268. qla2x00_model_name[index]);
  1269. ha->model_desc =
  1270. qla2x00_model_desc[index];
  1271. } else {
  1272. strcpy(ha->model_number, "QLA23xx");
  1273. }
  1274. }
  1275. }
  1276. } else if (IS_QLA2200(ha)) {
  1277. nv->firmware_options[0] |= BIT_2;
  1278. /*
  1279. * 'Point-to-point preferred, else loop' is not a safe
  1280. * connection mode setting.
  1281. */
  1282. if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
  1283. (BIT_5 | BIT_4)) {
  1284. /* Force 'loop preferred, else point-to-point'. */
  1285. nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
  1286. nv->add_firmware_options[0] |= BIT_5;
  1287. }
  1288. strcpy(ha->model_number, "QLA22xx");
  1289. } else /*if (IS_QLA2100(ha))*/ {
  1290. strcpy(ha->model_number, "QLA2100");
  1291. }
  1292. /*
  1293. * Copy over NVRAM RISC parameter block to initialization control block.
  1294. */
  1295. dptr1 = (uint8_t *)icb;
  1296. dptr2 = (uint8_t *)&nv->parameter_block_version;
  1297. cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
  1298. while (cnt--)
  1299. *dptr1++ = *dptr2++;
  1300. /* Copy 2nd half. */
  1301. dptr1 = (uint8_t *)icb->add_firmware_options;
  1302. cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
  1303. while (cnt--)
  1304. *dptr1++ = *dptr2++;
  1305. /* Prepare nodename */
  1306. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1307. /*
  1308. * Firmware will apply the following mask if the nodename was
  1309. * not provided.
  1310. */
  1311. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1312. icb->node_name[0] &= 0xF0;
  1313. }
  1314. /*
  1315. * Set host adapter parameters.
  1316. */
  1317. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1318. /* Always load RISC code on non ISP2[12]00 chips. */
  1319. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1320. ha->flags.disable_risc_code_load = 0;
  1321. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1322. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1323. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1324. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1325. ha->operating_mode =
  1326. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1327. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1328. sizeof(ha->fw_seriallink_options));
  1329. /* save HBA serial number */
  1330. ha->serial0 = icb->port_name[5];
  1331. ha->serial1 = icb->port_name[6];
  1332. ha->serial2 = icb->port_name[7];
  1333. ha->node_name = icb->node_name;
  1334. ha->port_name = icb->port_name;
  1335. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1336. ha->retry_count = nv->retry_count;
  1337. /* Set minimum login_timeout to 4 seconds. */
  1338. if (nv->login_timeout < ql2xlogintimeout)
  1339. nv->login_timeout = ql2xlogintimeout;
  1340. if (nv->login_timeout < 4)
  1341. nv->login_timeout = 4;
  1342. ha->login_timeout = nv->login_timeout;
  1343. icb->login_timeout = nv->login_timeout;
  1344. /* Set minimum RATOV to 200 tenths of a second. */
  1345. ha->r_a_tov = 200;
  1346. ha->loop_reset_delay = nv->reset_delay;
  1347. /* Link Down Timeout = 0:
  1348. *
  1349. * When Port Down timer expires we will start returning
  1350. * I/O's to OS with "DID_NO_CONNECT".
  1351. *
  1352. * Link Down Timeout != 0:
  1353. *
  1354. * The driver waits for the link to come up after link down
  1355. * before returning I/Os to OS with "DID_NO_CONNECT".
  1356. */
  1357. if (nv->link_down_timeout == 0) {
  1358. ha->loop_down_abort_time =
  1359. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1360. } else {
  1361. ha->link_down_timeout = nv->link_down_timeout;
  1362. ha->loop_down_abort_time =
  1363. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1364. }
  1365. /*
  1366. * Need enough time to try and get the port back.
  1367. */
  1368. ha->port_down_retry_count = nv->port_down_retry_count;
  1369. if (qlport_down_retry)
  1370. ha->port_down_retry_count = qlport_down_retry;
  1371. /* Set login_retry_count */
  1372. ha->login_retry_count = nv->retry_count;
  1373. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1374. ha->port_down_retry_count > 3)
  1375. ha->login_retry_count = ha->port_down_retry_count;
  1376. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1377. ha->login_retry_count = ha->port_down_retry_count;
  1378. if (ql2xloginretrycount)
  1379. ha->login_retry_count = ql2xloginretrycount;
  1380. icb->lun_enables = __constant_cpu_to_le16(0);
  1381. icb->command_resource_count = 0;
  1382. icb->immediate_notify_resource_count = 0;
  1383. icb->timeout = __constant_cpu_to_le16(0);
  1384. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1385. /* Enable RIO */
  1386. icb->firmware_options[0] &= ~BIT_3;
  1387. icb->add_firmware_options[0] &=
  1388. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1389. icb->add_firmware_options[0] |= BIT_2;
  1390. icb->response_accumulation_timer = 3;
  1391. icb->interrupt_delay_timer = 5;
  1392. ha->flags.process_response_queue = 1;
  1393. } else {
  1394. /* Enable ZIO. */
  1395. if (!ha->flags.init_done) {
  1396. ha->zio_mode = icb->add_firmware_options[0] &
  1397. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1398. ha->zio_timer = icb->interrupt_delay_timer ?
  1399. icb->interrupt_delay_timer: 2;
  1400. }
  1401. icb->add_firmware_options[0] &=
  1402. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1403. ha->flags.process_response_queue = 0;
  1404. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1405. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1406. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1407. ha->zio_timer * 100));
  1408. qla_printk(KERN_INFO, ha,
  1409. "ZIO mode %d enabled; timer delay (%d us).\n",
  1410. ha->zio_mode, ha->zio_timer * 100);
  1411. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1412. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1413. ha->flags.process_response_queue = 1;
  1414. }
  1415. }
  1416. if (rval) {
  1417. DEBUG2_3(printk(KERN_WARNING
  1418. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1419. }
  1420. return (rval);
  1421. }
  1422. static void
  1423. qla2x00_rport_add(void *data)
  1424. {
  1425. fc_port_t *fcport = data;
  1426. qla2x00_reg_remote_port(fcport->ha, fcport);
  1427. }
  1428. static void
  1429. qla2x00_rport_del(void *data)
  1430. {
  1431. fc_port_t *fcport = data;
  1432. if (fcport->rport)
  1433. fc_remote_port_delete(fcport->rport);
  1434. fcport->rport = NULL;
  1435. }
  1436. /**
  1437. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1438. * @ha: HA context
  1439. * @flags: allocation flags
  1440. *
  1441. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1442. */
  1443. fc_port_t *
  1444. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1445. {
  1446. fc_port_t *fcport;
  1447. fcport = kmalloc(sizeof(fc_port_t), flags);
  1448. if (fcport == NULL)
  1449. return (fcport);
  1450. /* Setup fcport template structure. */
  1451. memset(fcport, 0, sizeof (fc_port_t));
  1452. fcport->ha = ha;
  1453. fcport->port_type = FCT_UNKNOWN;
  1454. fcport->loop_id = FC_NO_LOOP_ID;
  1455. fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
  1456. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1457. fcport->flags = FCF_RLC_SUPPORT;
  1458. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1459. INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
  1460. INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
  1461. return (fcport);
  1462. }
  1463. /*
  1464. * qla2x00_configure_loop
  1465. * Updates Fibre Channel Device Database with what is actually on loop.
  1466. *
  1467. * Input:
  1468. * ha = adapter block pointer.
  1469. *
  1470. * Returns:
  1471. * 0 = success.
  1472. * 1 = error.
  1473. * 2 = database was full and device was not configured.
  1474. */
  1475. static int
  1476. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1477. {
  1478. int rval;
  1479. unsigned long flags, save_flags;
  1480. rval = QLA_SUCCESS;
  1481. /* Get Initiator ID */
  1482. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1483. rval = qla2x00_configure_hba(ha);
  1484. if (rval != QLA_SUCCESS) {
  1485. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1486. ha->host_no));
  1487. return (rval);
  1488. }
  1489. }
  1490. save_flags = flags = ha->dpc_flags;
  1491. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1492. ha->host_no, flags));
  1493. /*
  1494. * If we have both an RSCN and PORT UPDATE pending then handle them
  1495. * both at the same time.
  1496. */
  1497. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1498. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1499. /* Determine what we need to do */
  1500. if (ha->current_topology == ISP_CFG_FL &&
  1501. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1502. ha->flags.rscn_queue_overflow = 1;
  1503. set_bit(RSCN_UPDATE, &flags);
  1504. } else if (ha->current_topology == ISP_CFG_F &&
  1505. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1506. ha->flags.rscn_queue_overflow = 1;
  1507. set_bit(RSCN_UPDATE, &flags);
  1508. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1509. } else if (!ha->flags.online ||
  1510. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1511. ha->flags.rscn_queue_overflow = 1;
  1512. set_bit(RSCN_UPDATE, &flags);
  1513. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1514. }
  1515. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1516. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1517. rval = QLA_FUNCTION_FAILED;
  1518. } else {
  1519. rval = qla2x00_configure_local_loop(ha);
  1520. }
  1521. }
  1522. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1523. if (LOOP_TRANSITION(ha)) {
  1524. rval = QLA_FUNCTION_FAILED;
  1525. } else {
  1526. rval = qla2x00_configure_fabric(ha);
  1527. }
  1528. }
  1529. if (rval == QLA_SUCCESS) {
  1530. if (atomic_read(&ha->loop_down_timer) ||
  1531. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1532. rval = QLA_FUNCTION_FAILED;
  1533. } else {
  1534. atomic_set(&ha->loop_state, LOOP_READY);
  1535. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1536. }
  1537. }
  1538. if (rval) {
  1539. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1540. __func__, ha->host_no));
  1541. } else {
  1542. DEBUG3(printk("%s: exiting normally\n", __func__));
  1543. }
  1544. /* Restore state if a resync event occured during processing */
  1545. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1546. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1547. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1548. if (test_bit(RSCN_UPDATE, &save_flags))
  1549. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1550. }
  1551. return (rval);
  1552. }
  1553. /*
  1554. * qla2x00_configure_local_loop
  1555. * Updates Fibre Channel Device Database with local loop devices.
  1556. *
  1557. * Input:
  1558. * ha = adapter block pointer.
  1559. *
  1560. * Returns:
  1561. * 0 = success.
  1562. */
  1563. static int
  1564. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1565. {
  1566. int rval, rval2;
  1567. int found_devs;
  1568. int found;
  1569. fc_port_t *fcport, *new_fcport;
  1570. uint16_t index;
  1571. uint16_t entries;
  1572. char *id_iter;
  1573. uint16_t loop_id;
  1574. uint8_t domain, area, al_pa;
  1575. found_devs = 0;
  1576. new_fcport = NULL;
  1577. entries = MAX_FIBRE_DEVICES;
  1578. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1579. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1580. /* Get list of logged in devices. */
  1581. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1582. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1583. &entries);
  1584. if (rval != QLA_SUCCESS)
  1585. goto cleanup_allocation;
  1586. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1587. ha->host_no, entries));
  1588. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1589. entries * sizeof(struct gid_list_info)));
  1590. /* Allocate temporary fcport for any new fcports discovered. */
  1591. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1592. if (new_fcport == NULL) {
  1593. rval = QLA_MEMORY_ALLOC_FAILED;
  1594. goto cleanup_allocation;
  1595. }
  1596. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1597. /*
  1598. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1599. */
  1600. list_for_each_entry(fcport, &ha->fcports, list) {
  1601. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1602. fcport->port_type != FCT_BROADCAST &&
  1603. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1604. DEBUG(printk("scsi(%ld): Marking port lost, "
  1605. "loop_id=0x%04x\n",
  1606. ha->host_no, fcport->loop_id));
  1607. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1608. fcport->flags &= ~FCF_FARP_DONE;
  1609. }
  1610. }
  1611. /* Add devices to port list. */
  1612. id_iter = (char *)ha->gid_list;
  1613. for (index = 0; index < entries; index++) {
  1614. domain = ((struct gid_list_info *)id_iter)->domain;
  1615. area = ((struct gid_list_info *)id_iter)->area;
  1616. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1617. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1618. loop_id = (uint16_t)
  1619. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1620. else
  1621. loop_id = le16_to_cpu(
  1622. ((struct gid_list_info *)id_iter)->loop_id);
  1623. id_iter += ha->gid_list_info_size;
  1624. /* Bypass reserved domain fields. */
  1625. if ((domain & 0xf0) == 0xf0)
  1626. continue;
  1627. /* Bypass if not same domain and area of adapter. */
  1628. if (area && domain &&
  1629. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1630. continue;
  1631. /* Bypass invalid local loop ID. */
  1632. if (loop_id > LAST_LOCAL_LOOP_ID)
  1633. continue;
  1634. /* Fill in member data. */
  1635. new_fcport->d_id.b.domain = domain;
  1636. new_fcport->d_id.b.area = area;
  1637. new_fcport->d_id.b.al_pa = al_pa;
  1638. new_fcport->loop_id = loop_id;
  1639. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1640. if (rval2 != QLA_SUCCESS) {
  1641. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1642. "information -- get_port_database=%x, "
  1643. "loop_id=0x%04x\n",
  1644. ha->host_no, rval2, new_fcport->loop_id));
  1645. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1646. ha->host_no));
  1647. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1648. continue;
  1649. }
  1650. /* Check for matching device in port list. */
  1651. found = 0;
  1652. fcport = NULL;
  1653. list_for_each_entry(fcport, &ha->fcports, list) {
  1654. if (memcmp(new_fcport->port_name, fcport->port_name,
  1655. WWN_SIZE))
  1656. continue;
  1657. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1658. FCF_PERSISTENT_BOUND);
  1659. fcport->loop_id = new_fcport->loop_id;
  1660. fcport->port_type = new_fcport->port_type;
  1661. fcport->d_id.b24 = new_fcport->d_id.b24;
  1662. memcpy(fcport->node_name, new_fcport->node_name,
  1663. WWN_SIZE);
  1664. found++;
  1665. break;
  1666. }
  1667. if (!found) {
  1668. /* New device, add to fcports list. */
  1669. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1670. list_add_tail(&new_fcport->list, &ha->fcports);
  1671. /* Allocate a new replacement fcport. */
  1672. fcport = new_fcport;
  1673. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1674. if (new_fcport == NULL) {
  1675. rval = QLA_MEMORY_ALLOC_FAILED;
  1676. goto cleanup_allocation;
  1677. }
  1678. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1679. }
  1680. qla2x00_update_fcport(ha, fcport);
  1681. found_devs++;
  1682. }
  1683. cleanup_allocation:
  1684. kfree(new_fcport);
  1685. if (rval != QLA_SUCCESS) {
  1686. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1687. "rval=%x\n", ha->host_no, rval));
  1688. }
  1689. if (found_devs) {
  1690. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1691. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1692. }
  1693. return (rval);
  1694. }
  1695. static void
  1696. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1697. {
  1698. fc_port_t *fcport;
  1699. qla2x00_mark_all_devices_lost(ha);
  1700. list_for_each_entry(fcport, &ha->fcports, list) {
  1701. if (fcport->port_type != FCT_TARGET)
  1702. continue;
  1703. qla2x00_update_fcport(ha, fcport);
  1704. }
  1705. }
  1706. /*
  1707. * qla2x00_update_fcport
  1708. * Updates device on list.
  1709. *
  1710. * Input:
  1711. * ha = adapter block pointer.
  1712. * fcport = port structure pointer.
  1713. *
  1714. * Return:
  1715. * 0 - Success
  1716. * BIT_0 - error
  1717. *
  1718. * Context:
  1719. * Kernel context.
  1720. */
  1721. static void
  1722. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1723. {
  1724. uint16_t index;
  1725. unsigned long flags;
  1726. srb_t *sp;
  1727. fcport->ha = ha;
  1728. fcport->login_retry = 0;
  1729. fcport->port_login_retry_count = ha->port_down_retry_count *
  1730. PORT_RETRY_TIME;
  1731. atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
  1732. PORT_RETRY_TIME);
  1733. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1734. /*
  1735. * Check for outstanding cmd on tape Bypass LUN discovery if active
  1736. * command on tape.
  1737. */
  1738. if (fcport->flags & FCF_TAPE_PRESENT) {
  1739. spin_lock_irqsave(&ha->hardware_lock, flags);
  1740. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1741. fc_port_t *sfcp;
  1742. if ((sp = ha->outstanding_cmds[index]) != 0) {
  1743. sfcp = sp->fcport;
  1744. if (sfcp == fcport) {
  1745. atomic_set(&fcport->state, FCS_ONLINE);
  1746. spin_unlock_irqrestore(
  1747. &ha->hardware_lock, flags);
  1748. return;
  1749. }
  1750. }
  1751. }
  1752. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1753. }
  1754. if (fcport->port_type == FCT_INITIATOR ||
  1755. fcport->port_type == FCT_BROADCAST)
  1756. fcport->device_type = TYPE_PROCESSOR;
  1757. atomic_set(&fcport->state, FCS_ONLINE);
  1758. if (ha->flags.init_done)
  1759. qla2x00_reg_remote_port(ha, fcport);
  1760. }
  1761. void
  1762. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1763. {
  1764. struct fc_rport_identifiers rport_ids;
  1765. struct fc_rport *rport;
  1766. if (fcport->rport) {
  1767. fc_remote_port_delete(fcport->rport);
  1768. fcport->rport = NULL;
  1769. }
  1770. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1771. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1772. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1773. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1774. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1775. fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1776. if (!rport) {
  1777. qla_printk(KERN_WARNING, ha,
  1778. "Unable to allocate fc remote port!\n");
  1779. return;
  1780. }
  1781. *((fc_port_t **)rport->dd_data) = fcport;
  1782. rport->supported_classes = fcport->supported_classes;
  1783. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1784. if (fcport->port_type == FCT_INITIATOR)
  1785. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1786. if (fcport->port_type == FCT_TARGET)
  1787. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1788. fc_remote_port_rolechg(rport, rport_ids.roles);
  1789. if (rport->scsi_target_id != -1 &&
  1790. rport->scsi_target_id < ha->host->max_id)
  1791. fcport->os_target_id = rport->scsi_target_id;
  1792. }
  1793. /*
  1794. * qla2x00_configure_fabric
  1795. * Setup SNS devices with loop ID's.
  1796. *
  1797. * Input:
  1798. * ha = adapter block pointer.
  1799. *
  1800. * Returns:
  1801. * 0 = success.
  1802. * BIT_0 = error
  1803. */
  1804. static int
  1805. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1806. {
  1807. int rval, rval2;
  1808. fc_port_t *fcport, *fcptemp;
  1809. uint16_t next_loopid;
  1810. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1811. uint16_t loop_id;
  1812. LIST_HEAD(new_fcports);
  1813. /* If FL port exists, then SNS is present */
  1814. if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
  1815. loop_id = NPH_F_PORT;
  1816. else
  1817. loop_id = SNS_FL_PORT;
  1818. rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
  1819. if (rval != QLA_SUCCESS) {
  1820. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1821. "Port\n", ha->host_no));
  1822. ha->device_flags &= ~SWITCH_FOUND;
  1823. return (QLA_SUCCESS);
  1824. }
  1825. /* Mark devices that need re-synchronization. */
  1826. rval2 = qla2x00_device_resync(ha);
  1827. if (rval2 == QLA_RSCNS_HANDLED) {
  1828. /* No point doing the scan, just continue. */
  1829. return (QLA_SUCCESS);
  1830. }
  1831. do {
  1832. /* FDMI support. */
  1833. if (ql2xfdmienable &&
  1834. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1835. qla2x00_fdmi_register(ha);
  1836. /* Ensure we are logged into the SNS. */
  1837. if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
  1838. loop_id = NPH_SNS;
  1839. else
  1840. loop_id = SIMPLE_NAME_SERVER;
  1841. ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
  1842. 0xfc, mb, BIT_1 | BIT_0);
  1843. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1844. DEBUG2(qla_printk(KERN_INFO, ha,
  1845. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1846. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1847. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1848. return (QLA_SUCCESS);
  1849. }
  1850. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  1851. if (qla2x00_rft_id(ha)) {
  1852. /* EMPTY */
  1853. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1854. "TYPE failed.\n", ha->host_no));
  1855. }
  1856. if (qla2x00_rff_id(ha)) {
  1857. /* EMPTY */
  1858. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1859. "Features failed.\n", ha->host_no));
  1860. }
  1861. if (qla2x00_rnn_id(ha)) {
  1862. /* EMPTY */
  1863. DEBUG2(printk("scsi(%ld): Register Node Name "
  1864. "failed.\n", ha->host_no));
  1865. } else if (qla2x00_rsnn_nn(ha)) {
  1866. /* EMPTY */
  1867. DEBUG2(printk("scsi(%ld): Register Symbolic "
  1868. "Node Name failed.\n", ha->host_no));
  1869. }
  1870. }
  1871. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  1872. if (rval != QLA_SUCCESS)
  1873. break;
  1874. /*
  1875. * Logout all previous fabric devices marked lost, except
  1876. * tape devices.
  1877. */
  1878. list_for_each_entry(fcport, &ha->fcports, list) {
  1879. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1880. break;
  1881. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  1882. continue;
  1883. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  1884. qla2x00_mark_device_lost(ha, fcport,
  1885. ql2xplogiabsentdevice);
  1886. if (fcport->loop_id != FC_NO_LOOP_ID &&
  1887. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  1888. fcport->port_type != FCT_INITIATOR &&
  1889. fcport->port_type != FCT_BROADCAST) {
  1890. ha->isp_ops.fabric_logout(ha,
  1891. fcport->loop_id,
  1892. fcport->d_id.b.domain,
  1893. fcport->d_id.b.area,
  1894. fcport->d_id.b.al_pa);
  1895. fcport->loop_id = FC_NO_LOOP_ID;
  1896. }
  1897. }
  1898. }
  1899. /* Starting free loop ID. */
  1900. next_loopid = ha->min_external_loopid;
  1901. /*
  1902. * Scan through our port list and login entries that need to be
  1903. * logged in.
  1904. */
  1905. list_for_each_entry(fcport, &ha->fcports, list) {
  1906. if (atomic_read(&ha->loop_down_timer) ||
  1907. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1908. break;
  1909. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  1910. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  1911. continue;
  1912. if (fcport->loop_id == FC_NO_LOOP_ID) {
  1913. fcport->loop_id = next_loopid;
  1914. rval = qla2x00_find_new_loop_id(ha, fcport);
  1915. if (rval != QLA_SUCCESS) {
  1916. /* Ran out of IDs to use */
  1917. break;
  1918. }
  1919. }
  1920. /* Login and update database */
  1921. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1922. }
  1923. /* Exit if out of loop IDs. */
  1924. if (rval != QLA_SUCCESS) {
  1925. break;
  1926. }
  1927. /*
  1928. * Login and add the new devices to our port list.
  1929. */
  1930. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1931. if (atomic_read(&ha->loop_down_timer) ||
  1932. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1933. break;
  1934. /* Find a new loop ID to use. */
  1935. fcport->loop_id = next_loopid;
  1936. rval = qla2x00_find_new_loop_id(ha, fcport);
  1937. if (rval != QLA_SUCCESS) {
  1938. /* Ran out of IDs to use */
  1939. break;
  1940. }
  1941. /* Remove device from the new list and add it to DB */
  1942. list_del(&fcport->list);
  1943. list_add_tail(&fcport->list, &ha->fcports);
  1944. /* Login and update database */
  1945. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1946. }
  1947. } while (0);
  1948. /* Free all new device structures not processed. */
  1949. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1950. list_del(&fcport->list);
  1951. kfree(fcport);
  1952. }
  1953. if (rval) {
  1954. DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
  1955. "rval=%d\n", ha->host_no, rval));
  1956. }
  1957. return (rval);
  1958. }
  1959. /*
  1960. * qla2x00_find_all_fabric_devs
  1961. *
  1962. * Input:
  1963. * ha = adapter block pointer.
  1964. * dev = database device entry pointer.
  1965. *
  1966. * Returns:
  1967. * 0 = success.
  1968. *
  1969. * Context:
  1970. * Kernel context.
  1971. */
  1972. static int
  1973. qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
  1974. {
  1975. int rval;
  1976. uint16_t loop_id;
  1977. fc_port_t *fcport, *new_fcport, *fcptemp;
  1978. int found;
  1979. sw_info_t *swl;
  1980. int swl_idx;
  1981. int first_dev, last_dev;
  1982. port_id_t wrap, nxt_d_id;
  1983. rval = QLA_SUCCESS;
  1984. /* Try GID_PT to get device list, else GAN. */
  1985. swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
  1986. if (swl == NULL) {
  1987. /*EMPTY*/
  1988. DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
  1989. "on GA_NXT\n", ha->host_no));
  1990. } else {
  1991. memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
  1992. if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
  1993. kfree(swl);
  1994. swl = NULL;
  1995. } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
  1996. kfree(swl);
  1997. swl = NULL;
  1998. } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
  1999. kfree(swl);
  2000. swl = NULL;
  2001. }
  2002. }
  2003. swl_idx = 0;
  2004. /* Allocate temporary fcport for any new fcports discovered. */
  2005. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2006. if (new_fcport == NULL) {
  2007. kfree(swl);
  2008. return (QLA_MEMORY_ALLOC_FAILED);
  2009. }
  2010. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2011. /* Set start port ID scan at adapter ID. */
  2012. first_dev = 1;
  2013. last_dev = 0;
  2014. /* Starting free loop ID. */
  2015. loop_id = ha->min_external_loopid;
  2016. for (; loop_id <= ha->last_loop_id; loop_id++) {
  2017. if (qla2x00_is_reserved_id(ha, loop_id))
  2018. continue;
  2019. if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
  2020. break;
  2021. if (swl != NULL) {
  2022. if (last_dev) {
  2023. wrap.b24 = new_fcport->d_id.b24;
  2024. } else {
  2025. new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
  2026. memcpy(new_fcport->node_name,
  2027. swl[swl_idx].node_name, WWN_SIZE);
  2028. memcpy(new_fcport->port_name,
  2029. swl[swl_idx].port_name, WWN_SIZE);
  2030. if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
  2031. last_dev = 1;
  2032. }
  2033. swl_idx++;
  2034. }
  2035. } else {
  2036. /* Send GA_NXT to the switch */
  2037. rval = qla2x00_ga_nxt(ha, new_fcport);
  2038. if (rval != QLA_SUCCESS) {
  2039. qla_printk(KERN_WARNING, ha,
  2040. "SNS scan failed -- assuming zero-entry "
  2041. "result...\n");
  2042. list_for_each_entry_safe(fcport, fcptemp,
  2043. new_fcports, list) {
  2044. list_del(&fcport->list);
  2045. kfree(fcport);
  2046. }
  2047. rval = QLA_SUCCESS;
  2048. break;
  2049. }
  2050. }
  2051. /* If wrap on switch device list, exit. */
  2052. if (first_dev) {
  2053. wrap.b24 = new_fcport->d_id.b24;
  2054. first_dev = 0;
  2055. } else if (new_fcport->d_id.b24 == wrap.b24) {
  2056. DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
  2057. ha->host_no, new_fcport->d_id.b.domain,
  2058. new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
  2059. break;
  2060. }
  2061. /* Bypass if host adapter. */
  2062. if (new_fcport->d_id.b24 == ha->d_id.b24)
  2063. continue;
  2064. /* Bypass if same domain and area of adapter. */
  2065. if (((new_fcport->d_id.b24 & 0xffff00) ==
  2066. (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
  2067. ISP_CFG_FL)
  2068. continue;
  2069. /* Bypass reserved domain fields. */
  2070. if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
  2071. continue;
  2072. /* Locate matching device in database. */
  2073. found = 0;
  2074. list_for_each_entry(fcport, &ha->fcports, list) {
  2075. if (memcmp(new_fcport->port_name, fcport->port_name,
  2076. WWN_SIZE))
  2077. continue;
  2078. found++;
  2079. /*
  2080. * If address the same and state FCS_ONLINE, nothing
  2081. * changed.
  2082. */
  2083. if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
  2084. atomic_read(&fcport->state) == FCS_ONLINE) {
  2085. break;
  2086. }
  2087. /*
  2088. * If device was not a fabric device before.
  2089. */
  2090. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  2091. fcport->d_id.b24 = new_fcport->d_id.b24;
  2092. fcport->loop_id = FC_NO_LOOP_ID;
  2093. fcport->flags |= (FCF_FABRIC_DEVICE |
  2094. FCF_LOGIN_NEEDED);
  2095. fcport->flags &= ~FCF_PERSISTENT_BOUND;
  2096. break;
  2097. }
  2098. /*
  2099. * Port ID changed or device was marked to be updated;
  2100. * Log it out if still logged in and mark it for
  2101. * relogin later.
  2102. */
  2103. fcport->d_id.b24 = new_fcport->d_id.b24;
  2104. fcport->flags |= FCF_LOGIN_NEEDED;
  2105. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2106. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2107. fcport->port_type != FCT_INITIATOR &&
  2108. fcport->port_type != FCT_BROADCAST) {
  2109. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2110. fcport->d_id.b.domain, fcport->d_id.b.area,
  2111. fcport->d_id.b.al_pa);
  2112. fcport->loop_id = FC_NO_LOOP_ID;
  2113. }
  2114. break;
  2115. }
  2116. if (found)
  2117. continue;
  2118. /* If device was not in our fcports list, then add it. */
  2119. list_add_tail(&new_fcport->list, new_fcports);
  2120. /* Allocate a new replacement fcport. */
  2121. nxt_d_id.b24 = new_fcport->d_id.b24;
  2122. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2123. if (new_fcport == NULL) {
  2124. kfree(swl);
  2125. return (QLA_MEMORY_ALLOC_FAILED);
  2126. }
  2127. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2128. new_fcport->d_id.b24 = nxt_d_id.b24;
  2129. }
  2130. kfree(swl);
  2131. kfree(new_fcport);
  2132. if (!list_empty(new_fcports))
  2133. ha->device_flags |= DFLG_FABRIC_DEVICES;
  2134. return (rval);
  2135. }
  2136. /*
  2137. * qla2x00_find_new_loop_id
  2138. * Scan through our port list and find a new usable loop ID.
  2139. *
  2140. * Input:
  2141. * ha: adapter state pointer.
  2142. * dev: port structure pointer.
  2143. *
  2144. * Returns:
  2145. * qla2x00 local function return status code.
  2146. *
  2147. * Context:
  2148. * Kernel context.
  2149. */
  2150. int
  2151. qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
  2152. {
  2153. int rval;
  2154. int found;
  2155. fc_port_t *fcport;
  2156. uint16_t first_loop_id;
  2157. rval = QLA_SUCCESS;
  2158. /* Save starting loop ID. */
  2159. first_loop_id = dev->loop_id;
  2160. for (;;) {
  2161. /* Skip loop ID if already used by adapter. */
  2162. if (dev->loop_id == ha->loop_id) {
  2163. dev->loop_id++;
  2164. }
  2165. /* Skip reserved loop IDs. */
  2166. while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
  2167. dev->loop_id++;
  2168. }
  2169. /* Reset loop ID if passed the end. */
  2170. if (dev->loop_id > ha->last_loop_id) {
  2171. /* first loop ID. */
  2172. dev->loop_id = ha->min_external_loopid;
  2173. }
  2174. /* Check for loop ID being already in use. */
  2175. found = 0;
  2176. fcport = NULL;
  2177. list_for_each_entry(fcport, &ha->fcports, list) {
  2178. if (fcport->loop_id == dev->loop_id && fcport != dev) {
  2179. /* ID possibly in use */
  2180. found++;
  2181. break;
  2182. }
  2183. }
  2184. /* If not in use then it is free to use. */
  2185. if (!found) {
  2186. break;
  2187. }
  2188. /* ID in use. Try next value. */
  2189. dev->loop_id++;
  2190. /* If wrap around. No free ID to use. */
  2191. if (dev->loop_id == first_loop_id) {
  2192. dev->loop_id = FC_NO_LOOP_ID;
  2193. rval = QLA_FUNCTION_FAILED;
  2194. break;
  2195. }
  2196. }
  2197. return (rval);
  2198. }
  2199. /*
  2200. * qla2x00_device_resync
  2201. * Marks devices in the database that needs resynchronization.
  2202. *
  2203. * Input:
  2204. * ha = adapter block pointer.
  2205. *
  2206. * Context:
  2207. * Kernel context.
  2208. */
  2209. static int
  2210. qla2x00_device_resync(scsi_qla_host_t *ha)
  2211. {
  2212. int rval;
  2213. int rval2;
  2214. uint32_t mask;
  2215. fc_port_t *fcport;
  2216. uint32_t rscn_entry;
  2217. uint8_t rscn_out_iter;
  2218. uint8_t format;
  2219. port_id_t d_id;
  2220. rval = QLA_RSCNS_HANDLED;
  2221. while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
  2222. ha->flags.rscn_queue_overflow) {
  2223. rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
  2224. format = MSB(MSW(rscn_entry));
  2225. d_id.b.domain = LSB(MSW(rscn_entry));
  2226. d_id.b.area = MSB(LSW(rscn_entry));
  2227. d_id.b.al_pa = LSB(LSW(rscn_entry));
  2228. DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
  2229. "[%02x/%02x%02x%02x].\n",
  2230. ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
  2231. d_id.b.area, d_id.b.al_pa));
  2232. ha->rscn_out_ptr++;
  2233. if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
  2234. ha->rscn_out_ptr = 0;
  2235. /* Skip duplicate entries. */
  2236. for (rscn_out_iter = ha->rscn_out_ptr;
  2237. !ha->flags.rscn_queue_overflow &&
  2238. rscn_out_iter != ha->rscn_in_ptr;
  2239. rscn_out_iter = (rscn_out_iter ==
  2240. (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
  2241. if (rscn_entry != ha->rscn_queue[rscn_out_iter])
  2242. break;
  2243. DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
  2244. "entry found at [%d].\n", ha->host_no,
  2245. rscn_out_iter));
  2246. ha->rscn_out_ptr = rscn_out_iter;
  2247. }
  2248. /* Queue overflow, set switch default case. */
  2249. if (ha->flags.rscn_queue_overflow) {
  2250. DEBUG(printk("scsi(%ld): device_resync: rscn "
  2251. "overflow.\n", ha->host_no));
  2252. format = 3;
  2253. ha->flags.rscn_queue_overflow = 0;
  2254. }
  2255. switch (format) {
  2256. case 0:
  2257. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
  2258. !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
  2259. !IS_QLA24XX(ha) && !IS_QLA25XX(ha) &&
  2260. ha->flags.init_done) {
  2261. /* Handle port RSCN via asyncronous IOCBs */
  2262. rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
  2263. NULL, 0);
  2264. if (rval2 == QLA_SUCCESS)
  2265. continue;
  2266. }
  2267. mask = 0xffffff;
  2268. break;
  2269. case 1:
  2270. mask = 0xffff00;
  2271. break;
  2272. case 2:
  2273. mask = 0xff0000;
  2274. break;
  2275. default:
  2276. mask = 0x0;
  2277. d_id.b24 = 0;
  2278. ha->rscn_out_ptr = ha->rscn_in_ptr;
  2279. break;
  2280. }
  2281. rval = QLA_SUCCESS;
  2282. /* Abort any outstanding IO descriptors. */
  2283. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  2284. qla2x00_cancel_io_descriptors(ha);
  2285. list_for_each_entry(fcport, &ha->fcports, list) {
  2286. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2287. (fcport->d_id.b24 & mask) != d_id.b24 ||
  2288. fcport->port_type == FCT_BROADCAST)
  2289. continue;
  2290. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  2291. if (format != 3 ||
  2292. fcport->port_type != FCT_INITIATOR) {
  2293. qla2x00_mark_device_lost(ha, fcport, 0);
  2294. }
  2295. }
  2296. fcport->flags &= ~FCF_FARP_DONE;
  2297. }
  2298. }
  2299. return (rval);
  2300. }
  2301. /*
  2302. * qla2x00_fabric_dev_login
  2303. * Login fabric target device and update FC port database.
  2304. *
  2305. * Input:
  2306. * ha: adapter state pointer.
  2307. * fcport: port structure list pointer.
  2308. * next_loopid: contains value of a new loop ID that can be used
  2309. * by the next login attempt.
  2310. *
  2311. * Returns:
  2312. * qla2x00 local function return status code.
  2313. *
  2314. * Context:
  2315. * Kernel context.
  2316. */
  2317. static int
  2318. qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2319. uint16_t *next_loopid)
  2320. {
  2321. int rval;
  2322. int retry;
  2323. uint8_t opts;
  2324. rval = QLA_SUCCESS;
  2325. retry = 0;
  2326. rval = qla2x00_fabric_login(ha, fcport, next_loopid);
  2327. if (rval == QLA_SUCCESS) {
  2328. /* Send an ADISC to tape devices.*/
  2329. opts = 0;
  2330. if (fcport->flags & FCF_TAPE_PRESENT)
  2331. opts |= BIT_1;
  2332. rval = qla2x00_get_port_database(ha, fcport, opts);
  2333. if (rval != QLA_SUCCESS) {
  2334. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2335. fcport->d_id.b.domain, fcport->d_id.b.area,
  2336. fcport->d_id.b.al_pa);
  2337. qla2x00_mark_device_lost(ha, fcport, 1);
  2338. } else {
  2339. qla2x00_update_fcport(ha, fcport);
  2340. }
  2341. }
  2342. return (rval);
  2343. }
  2344. /*
  2345. * qla2x00_fabric_login
  2346. * Issue fabric login command.
  2347. *
  2348. * Input:
  2349. * ha = adapter block pointer.
  2350. * device = pointer to FC device type structure.
  2351. *
  2352. * Returns:
  2353. * 0 - Login successfully
  2354. * 1 - Login failed
  2355. * 2 - Initiator device
  2356. * 3 - Fatal error
  2357. */
  2358. int
  2359. qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2360. uint16_t *next_loopid)
  2361. {
  2362. int rval;
  2363. int retry;
  2364. uint16_t tmp_loopid;
  2365. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2366. retry = 0;
  2367. tmp_loopid = 0;
  2368. for (;;) {
  2369. DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
  2370. "for port %02x%02x%02x.\n",
  2371. ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
  2372. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2373. /* Login fcport on switch. */
  2374. ha->isp_ops.fabric_login(ha, fcport->loop_id,
  2375. fcport->d_id.b.domain, fcport->d_id.b.area,
  2376. fcport->d_id.b.al_pa, mb, BIT_0);
  2377. if (mb[0] == MBS_PORT_ID_USED) {
  2378. /*
  2379. * Device has another loop ID. The firmware team
  2380. * recommends the driver perform an implicit login with
  2381. * the specified ID again. The ID we just used is save
  2382. * here so we return with an ID that can be tried by
  2383. * the next login.
  2384. */
  2385. retry++;
  2386. tmp_loopid = fcport->loop_id;
  2387. fcport->loop_id = mb[1];
  2388. DEBUG(printk("Fabric Login: port in use - next "
  2389. "loop id=0x%04x, port Id=%02x%02x%02x.\n",
  2390. fcport->loop_id, fcport->d_id.b.domain,
  2391. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2392. } else if (mb[0] == MBS_COMMAND_COMPLETE) {
  2393. /*
  2394. * Login succeeded.
  2395. */
  2396. if (retry) {
  2397. /* A retry occurred before. */
  2398. *next_loopid = tmp_loopid;
  2399. } else {
  2400. /*
  2401. * No retry occurred before. Just increment the
  2402. * ID value for next login.
  2403. */
  2404. *next_loopid = (fcport->loop_id + 1);
  2405. }
  2406. if (mb[1] & BIT_0) {
  2407. fcport->port_type = FCT_INITIATOR;
  2408. } else {
  2409. fcport->port_type = FCT_TARGET;
  2410. if (mb[1] & BIT_1) {
  2411. fcport->flags |= FCF_TAPE_PRESENT;
  2412. }
  2413. }
  2414. if (mb[10] & BIT_0)
  2415. fcport->supported_classes |= FC_COS_CLASS2;
  2416. if (mb[10] & BIT_1)
  2417. fcport->supported_classes |= FC_COS_CLASS3;
  2418. rval = QLA_SUCCESS;
  2419. break;
  2420. } else if (mb[0] == MBS_LOOP_ID_USED) {
  2421. /*
  2422. * Loop ID already used, try next loop ID.
  2423. */
  2424. fcport->loop_id++;
  2425. rval = qla2x00_find_new_loop_id(ha, fcport);
  2426. if (rval != QLA_SUCCESS) {
  2427. /* Ran out of loop IDs to use */
  2428. break;
  2429. }
  2430. } else if (mb[0] == MBS_COMMAND_ERROR) {
  2431. /*
  2432. * Firmware possibly timed out during login. If NO
  2433. * retries are left to do then the device is declared
  2434. * dead.
  2435. */
  2436. *next_loopid = fcport->loop_id;
  2437. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2438. fcport->d_id.b.domain, fcport->d_id.b.area,
  2439. fcport->d_id.b.al_pa);
  2440. qla2x00_mark_device_lost(ha, fcport, 1);
  2441. rval = 1;
  2442. break;
  2443. } else {
  2444. /*
  2445. * unrecoverable / not handled error
  2446. */
  2447. DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
  2448. "loop_id=%x jiffies=%lx.\n",
  2449. __func__, ha->host_no, mb[0],
  2450. fcport->d_id.b.domain, fcport->d_id.b.area,
  2451. fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
  2452. *next_loopid = fcport->loop_id;
  2453. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2454. fcport->d_id.b.domain, fcport->d_id.b.area,
  2455. fcport->d_id.b.al_pa);
  2456. fcport->loop_id = FC_NO_LOOP_ID;
  2457. fcport->login_retry = 0;
  2458. rval = 3;
  2459. break;
  2460. }
  2461. }
  2462. return (rval);
  2463. }
  2464. /*
  2465. * qla2x00_local_device_login
  2466. * Issue local device login command.
  2467. *
  2468. * Input:
  2469. * ha = adapter block pointer.
  2470. * loop_id = loop id of device to login to.
  2471. *
  2472. * Returns (Where's the #define!!!!):
  2473. * 0 - Login successfully
  2474. * 1 - Login failed
  2475. * 3 - Fatal error
  2476. */
  2477. int
  2478. qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
  2479. {
  2480. int rval;
  2481. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2482. memset(mb, 0, sizeof(mb));
  2483. rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
  2484. if (rval == QLA_SUCCESS) {
  2485. /* Interrogate mailbox registers for any errors */
  2486. if (mb[0] == MBS_COMMAND_ERROR)
  2487. rval = 1;
  2488. else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
  2489. /* device not in PCB table */
  2490. rval = 3;
  2491. }
  2492. return (rval);
  2493. }
  2494. /*
  2495. * qla2x00_loop_resync
  2496. * Resync with fibre channel devices.
  2497. *
  2498. * Input:
  2499. * ha = adapter block pointer.
  2500. *
  2501. * Returns:
  2502. * 0 = success
  2503. */
  2504. int
  2505. qla2x00_loop_resync(scsi_qla_host_t *ha)
  2506. {
  2507. int rval;
  2508. uint32_t wait_time;
  2509. rval = QLA_SUCCESS;
  2510. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2511. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2512. if (ha->flags.online) {
  2513. if (!(rval = qla2x00_fw_ready(ha))) {
  2514. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2515. wait_time = 256;
  2516. do {
  2517. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2518. /* Issue a marker after FW becomes ready. */
  2519. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2520. ha->marker_needed = 0;
  2521. /* Remap devices on Loop. */
  2522. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2523. qla2x00_configure_loop(ha);
  2524. wait_time--;
  2525. } while (!atomic_read(&ha->loop_down_timer) &&
  2526. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2527. wait_time &&
  2528. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2529. }
  2530. }
  2531. if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  2532. return (QLA_FUNCTION_FAILED);
  2533. }
  2534. if (rval) {
  2535. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  2536. }
  2537. return (rval);
  2538. }
  2539. void
  2540. qla2x00_rescan_fcports(scsi_qla_host_t *ha)
  2541. {
  2542. int rescan_done;
  2543. fc_port_t *fcport;
  2544. rescan_done = 0;
  2545. list_for_each_entry(fcport, &ha->fcports, list) {
  2546. if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
  2547. continue;
  2548. qla2x00_update_fcport(ha, fcport);
  2549. fcport->flags &= ~FCF_RESCAN_NEEDED;
  2550. rescan_done = 1;
  2551. }
  2552. qla2x00_probe_for_all_luns(ha);
  2553. }
  2554. /*
  2555. * qla2x00_abort_isp
  2556. * Resets ISP and aborts all outstanding commands.
  2557. *
  2558. * Input:
  2559. * ha = adapter block pointer.
  2560. *
  2561. * Returns:
  2562. * 0 = success
  2563. */
  2564. int
  2565. qla2x00_abort_isp(scsi_qla_host_t *ha)
  2566. {
  2567. unsigned long flags = 0;
  2568. uint16_t cnt;
  2569. srb_t *sp;
  2570. uint8_t status = 0;
  2571. if (ha->flags.online) {
  2572. ha->flags.online = 0;
  2573. clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2574. qla_printk(KERN_INFO, ha,
  2575. "Performing ISP error recovery - ha= %p.\n", ha);
  2576. ha->isp_ops.reset_chip(ha);
  2577. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  2578. if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
  2579. atomic_set(&ha->loop_state, LOOP_DOWN);
  2580. qla2x00_mark_all_devices_lost(ha);
  2581. } else {
  2582. if (!atomic_read(&ha->loop_down_timer))
  2583. atomic_set(&ha->loop_down_timer,
  2584. LOOP_DOWN_TIME);
  2585. }
  2586. spin_lock_irqsave(&ha->hardware_lock, flags);
  2587. /* Requeue all commands in outstanding command list. */
  2588. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  2589. sp = ha->outstanding_cmds[cnt];
  2590. if (sp) {
  2591. ha->outstanding_cmds[cnt] = NULL;
  2592. sp->flags = 0;
  2593. sp->cmd->result = DID_RESET << 16;
  2594. sp->cmd->host_scribble = (unsigned char *)NULL;
  2595. qla2x00_sp_compl(ha, sp);
  2596. }
  2597. }
  2598. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2599. ha->isp_ops.nvram_config(ha);
  2600. if (!qla2x00_restart_isp(ha)) {
  2601. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2602. if (!atomic_read(&ha->loop_down_timer)) {
  2603. /*
  2604. * Issue marker command only when we are going
  2605. * to start the I/O .
  2606. */
  2607. ha->marker_needed = 1;
  2608. }
  2609. ha->flags.online = 1;
  2610. ha->isp_ops.enable_intrs(ha);
  2611. ha->isp_abort_cnt = 0;
  2612. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2613. } else { /* failed the ISP abort */
  2614. ha->flags.online = 1;
  2615. if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
  2616. if (ha->isp_abort_cnt == 0) {
  2617. qla_printk(KERN_WARNING, ha,
  2618. "ISP error recovery failed - "
  2619. "board disabled\n");
  2620. /*
  2621. * The next call disables the board
  2622. * completely.
  2623. */
  2624. ha->isp_ops.reset_adapter(ha);
  2625. ha->flags.online = 0;
  2626. clear_bit(ISP_ABORT_RETRY,
  2627. &ha->dpc_flags);
  2628. status = 0;
  2629. } else { /* schedule another ISP abort */
  2630. ha->isp_abort_cnt--;
  2631. DEBUG(printk("qla%ld: ISP abort - "
  2632. "retry remaining %d\n",
  2633. ha->host_no, ha->isp_abort_cnt);)
  2634. status = 1;
  2635. }
  2636. } else {
  2637. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  2638. DEBUG(printk("qla2x00(%ld): ISP error recovery "
  2639. "- retrying (%d) more times\n",
  2640. ha->host_no, ha->isp_abort_cnt);)
  2641. set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2642. status = 1;
  2643. }
  2644. }
  2645. }
  2646. if (status) {
  2647. qla_printk(KERN_INFO, ha,
  2648. "qla2x00_abort_isp: **** FAILED ****\n");
  2649. } else {
  2650. DEBUG(printk(KERN_INFO
  2651. "qla2x00_abort_isp(%ld): exiting.\n",
  2652. ha->host_no);)
  2653. }
  2654. return(status);
  2655. }
  2656. /*
  2657. * qla2x00_restart_isp
  2658. * restarts the ISP after a reset
  2659. *
  2660. * Input:
  2661. * ha = adapter block pointer.
  2662. *
  2663. * Returns:
  2664. * 0 = success
  2665. */
  2666. static int
  2667. qla2x00_restart_isp(scsi_qla_host_t *ha)
  2668. {
  2669. uint8_t status = 0;
  2670. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2671. unsigned long flags = 0;
  2672. uint32_t wait_time;
  2673. /* If firmware needs to be loaded */
  2674. if (qla2x00_isp_firmware(ha)) {
  2675. ha->flags.online = 0;
  2676. if (!(status = ha->isp_ops.chip_diag(ha))) {
  2677. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  2678. status = qla2x00_setup_chip(ha);
  2679. goto done;
  2680. }
  2681. spin_lock_irqsave(&ha->hardware_lock, flags);
  2682. if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
  2683. /*
  2684. * Disable SRAM, Instruction RAM and GP RAM
  2685. * parity.
  2686. */
  2687. WRT_REG_WORD(&reg->hccr,
  2688. (HCCR_ENABLE_PARITY + 0x0));
  2689. RD_REG_WORD(&reg->hccr);
  2690. }
  2691. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2692. status = qla2x00_setup_chip(ha);
  2693. spin_lock_irqsave(&ha->hardware_lock, flags);
  2694. if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
  2695. /* Enable proper parity */
  2696. if (IS_QLA2300(ha))
  2697. /* SRAM parity */
  2698. WRT_REG_WORD(&reg->hccr,
  2699. (HCCR_ENABLE_PARITY + 0x1));
  2700. else
  2701. /*
  2702. * SRAM, Instruction RAM and GP RAM
  2703. * parity.
  2704. */
  2705. WRT_REG_WORD(&reg->hccr,
  2706. (HCCR_ENABLE_PARITY + 0x7));
  2707. RD_REG_WORD(&reg->hccr);
  2708. }
  2709. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2710. }
  2711. }
  2712. done:
  2713. if (!status && !(status = qla2x00_init_rings(ha))) {
  2714. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2715. if (!(status = qla2x00_fw_ready(ha))) {
  2716. DEBUG(printk("%s(): Start configure loop, "
  2717. "status = %d\n", __func__, status);)
  2718. /* Issue a marker after FW becomes ready. */
  2719. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2720. ha->flags.online = 1;
  2721. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2722. wait_time = 256;
  2723. do {
  2724. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2725. qla2x00_configure_loop(ha);
  2726. wait_time--;
  2727. } while (!atomic_read(&ha->loop_down_timer) &&
  2728. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2729. wait_time &&
  2730. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2731. }
  2732. /* if no cable then assume it's good */
  2733. if ((ha->device_flags & DFLG_NO_CABLE))
  2734. status = 0;
  2735. DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
  2736. __func__,
  2737. status);)
  2738. }
  2739. return (status);
  2740. }
  2741. /*
  2742. * qla2x00_reset_adapter
  2743. * Reset adapter.
  2744. *
  2745. * Input:
  2746. * ha = adapter block pointer.
  2747. */
  2748. void
  2749. qla2x00_reset_adapter(scsi_qla_host_t *ha)
  2750. {
  2751. unsigned long flags = 0;
  2752. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2753. ha->flags.online = 0;
  2754. ha->isp_ops.disable_intrs(ha);
  2755. spin_lock_irqsave(&ha->hardware_lock, flags);
  2756. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  2757. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2758. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  2759. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2760. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2761. }
  2762. void
  2763. qla24xx_reset_adapter(scsi_qla_host_t *ha)
  2764. {
  2765. unsigned long flags = 0;
  2766. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  2767. ha->flags.online = 0;
  2768. ha->isp_ops.disable_intrs(ha);
  2769. spin_lock_irqsave(&ha->hardware_lock, flags);
  2770. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  2771. RD_REG_DWORD(&reg->hccr);
  2772. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  2773. RD_REG_DWORD(&reg->hccr);
  2774. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2775. }
  2776. int
  2777. qla24xx_nvram_config(scsi_qla_host_t *ha)
  2778. {
  2779. int rval;
  2780. struct init_cb_24xx *icb;
  2781. struct nvram_24xx *nv;
  2782. uint32_t *dptr;
  2783. uint8_t *dptr1, *dptr2;
  2784. uint32_t chksum;
  2785. uint16_t cnt;
  2786. rval = QLA_SUCCESS;
  2787. icb = (struct init_cb_24xx *)ha->init_cb;
  2788. nv = (struct nvram_24xx *)ha->request_ring;
  2789. /* Determine NVRAM starting address. */
  2790. ha->nvram_size = sizeof(struct nvram_24xx);
  2791. ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
  2792. if (PCI_FUNC(ha->pdev->devfn))
  2793. ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
  2794. /* Get NVRAM data and calculate checksum. */
  2795. dptr = (uint32_t *)nv;
  2796. ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
  2797. ha->nvram_size);
  2798. for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
  2799. chksum += le32_to_cpu(*dptr++);
  2800. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  2801. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  2802. ha->nvram_size));
  2803. /* Bad NVRAM data, set defaults parameters. */
  2804. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
  2805. || nv->id[3] != ' ' ||
  2806. nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
  2807. /* Reset NVRAM data. */
  2808. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  2809. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  2810. le16_to_cpu(nv->nvram_version));
  2811. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  2812. "invalid -- WWPN) defaults.\n");
  2813. /*
  2814. * Set default initialization control block.
  2815. */
  2816. memset(nv, 0, ha->nvram_size);
  2817. nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
  2818. nv->version = __constant_cpu_to_le16(ICB_VERSION);
  2819. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  2820. nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2821. nv->exchange_count = __constant_cpu_to_le16(0);
  2822. nv->hard_address = __constant_cpu_to_le16(124);
  2823. nv->port_name[0] = 0x21;
  2824. nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
  2825. nv->port_name[2] = 0x00;
  2826. nv->port_name[3] = 0xe0;
  2827. nv->port_name[4] = 0x8b;
  2828. nv->port_name[5] = 0x1c;
  2829. nv->port_name[6] = 0x55;
  2830. nv->port_name[7] = 0x86;
  2831. nv->node_name[0] = 0x20;
  2832. nv->node_name[1] = 0x00;
  2833. nv->node_name[2] = 0x00;
  2834. nv->node_name[3] = 0xe0;
  2835. nv->node_name[4] = 0x8b;
  2836. nv->node_name[5] = 0x1c;
  2837. nv->node_name[6] = 0x55;
  2838. nv->node_name[7] = 0x86;
  2839. nv->login_retry_count = __constant_cpu_to_le16(8);
  2840. nv->link_down_timeout = __constant_cpu_to_le16(200);
  2841. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  2842. nv->login_timeout = __constant_cpu_to_le16(0);
  2843. nv->firmware_options_1 =
  2844. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  2845. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  2846. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  2847. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  2848. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  2849. nv->efi_parameters = __constant_cpu_to_le32(0);
  2850. nv->reset_delay = 5;
  2851. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  2852. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  2853. nv->link_down_timeout = __constant_cpu_to_le16(30);
  2854. rval = 1;
  2855. }
  2856. /* Reset Initialization control block */
  2857. memset(icb, 0, sizeof(struct init_cb_24xx));
  2858. /* Copy 1st segment. */
  2859. dptr1 = (uint8_t *)icb;
  2860. dptr2 = (uint8_t *)&nv->version;
  2861. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  2862. while (cnt--)
  2863. *dptr1++ = *dptr2++;
  2864. icb->login_retry_count = nv->login_retry_count;
  2865. icb->link_down_timeout = nv->link_down_timeout;
  2866. /* Copy 2nd segment. */
  2867. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  2868. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  2869. cnt = (uint8_t *)&icb->reserved_3 -
  2870. (uint8_t *)&icb->interrupt_delay_timer;
  2871. while (cnt--)
  2872. *dptr1++ = *dptr2++;
  2873. /*
  2874. * Setup driver NVRAM options.
  2875. */
  2876. if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
  2877. char *st, *en;
  2878. uint16_t index;
  2879. strncpy(ha->model_number, nv->model_name,
  2880. sizeof(nv->model_name));
  2881. st = en = ha->model_number;
  2882. en += sizeof(nv->model_name) - 1;
  2883. while (en > st) {
  2884. if (*en != 0x20 && *en != 0x00)
  2885. break;
  2886. *en-- = '\0';
  2887. }
  2888. index = (ha->pdev->subsystem_device & 0xff);
  2889. if (index < QLA_MODEL_NAMES)
  2890. ha->model_desc = qla2x00_model_desc[index];
  2891. } else
  2892. strcpy(ha->model_number, "QLA2462");
  2893. /* Prepare nodename */
  2894. if ((icb->firmware_options_1 & BIT_14) == 0) {
  2895. /*
  2896. * Firmware will apply the following mask if the nodename was
  2897. * not provided.
  2898. */
  2899. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  2900. icb->node_name[0] &= 0xF0;
  2901. }
  2902. /* Set host adapter parameters. */
  2903. ha->flags.disable_risc_code_load = 0;
  2904. ha->flags.enable_lip_reset = 1;
  2905. ha->flags.enable_lip_full_login = 1;
  2906. ha->flags.enable_target_reset = 1;
  2907. ha->flags.enable_led_scheme = 0;
  2908. ha->operating_mode =
  2909. (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  2910. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  2911. sizeof(ha->fw_seriallink_options24));
  2912. /* save HBA serial number */
  2913. ha->serial0 = icb->port_name[5];
  2914. ha->serial1 = icb->port_name[6];
  2915. ha->serial2 = icb->port_name[7];
  2916. ha->node_name = icb->node_name;
  2917. ha->port_name = icb->port_name;
  2918. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2919. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  2920. /* Set minimum login_timeout to 4 seconds. */
  2921. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  2922. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  2923. if (le16_to_cpu(nv->login_timeout) < 4)
  2924. nv->login_timeout = __constant_cpu_to_le16(4);
  2925. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  2926. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  2927. /* Set minimum RATOV to 200 tenths of a second. */
  2928. ha->r_a_tov = 200;
  2929. ha->loop_reset_delay = nv->reset_delay;
  2930. /* Link Down Timeout = 0:
  2931. *
  2932. * When Port Down timer expires we will start returning
  2933. * I/O's to OS with "DID_NO_CONNECT".
  2934. *
  2935. * Link Down Timeout != 0:
  2936. *
  2937. * The driver waits for the link to come up after link down
  2938. * before returning I/Os to OS with "DID_NO_CONNECT".
  2939. */
  2940. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  2941. ha->loop_down_abort_time =
  2942. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  2943. } else {
  2944. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  2945. ha->loop_down_abort_time =
  2946. (LOOP_DOWN_TIME - ha->link_down_timeout);
  2947. }
  2948. /* Need enough time to try and get the port back. */
  2949. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  2950. if (qlport_down_retry)
  2951. ha->port_down_retry_count = qlport_down_retry;
  2952. /* Set login_retry_count */
  2953. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  2954. if (ha->port_down_retry_count ==
  2955. le16_to_cpu(nv->port_down_retry_count) &&
  2956. ha->port_down_retry_count > 3)
  2957. ha->login_retry_count = ha->port_down_retry_count;
  2958. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  2959. ha->login_retry_count = ha->port_down_retry_count;
  2960. if (ql2xloginretrycount)
  2961. ha->login_retry_count = ql2xloginretrycount;
  2962. /* Enable ZIO. */
  2963. if (!ha->flags.init_done) {
  2964. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  2965. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  2966. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  2967. le16_to_cpu(icb->interrupt_delay_timer): 2;
  2968. }
  2969. icb->firmware_options_2 &= __constant_cpu_to_le32(
  2970. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  2971. ha->flags.process_response_queue = 0;
  2972. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  2973. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  2974. "(%d us).\n", ha->host_no, ha->zio_mode,
  2975. ha->zio_timer * 100));
  2976. qla_printk(KERN_INFO, ha,
  2977. "ZIO mode %d enabled; timer delay (%d us).\n",
  2978. ha->zio_mode, ha->zio_timer * 100);
  2979. icb->firmware_options_2 |= cpu_to_le32(
  2980. (uint32_t)ha->zio_mode);
  2981. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  2982. ha->flags.process_response_queue = 1;
  2983. }
  2984. if (rval) {
  2985. DEBUG2_3(printk(KERN_WARNING
  2986. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  2987. }
  2988. return (rval);
  2989. }
  2990. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  2991. int
  2992. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  2993. {
  2994. int rval, num, i;
  2995. uint32_t cnt;
  2996. uint16_t *risc_code;
  2997. uint32_t risc_addr, risc_size;
  2998. uint16_t *req_ring;
  2999. struct qla_fw_info *fw_iter;
  3000. rval = QLA_SUCCESS;
  3001. /* Load firmware sequences */
  3002. fw_iter = ha->brd_info->fw_info;
  3003. *srisc_addr = *ha->brd_info->fw_info->fwstart;
  3004. while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
  3005. risc_code = fw_iter->fwcode;
  3006. risc_size = *fw_iter->fwlen;
  3007. if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)
  3008. risc_addr = *fw_iter->fwstart;
  3009. else
  3010. risc_addr = *fw_iter->lfwstart;
  3011. num = 0;
  3012. rval = 0;
  3013. while (risc_size > 0 && !rval) {
  3014. cnt = (uint16_t)(ha->fw_transfer_size >> 1);
  3015. if (cnt > risc_size)
  3016. cnt = risc_size;
  3017. DEBUG7(printk("scsi(%ld): Loading risc segment@ "
  3018. "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
  3019. ha->host_no, risc_code, cnt, risc_addr));
  3020. req_ring = (uint16_t *)ha->request_ring;
  3021. for (i = 0; i < cnt; i++)
  3022. req_ring[i] = cpu_to_le16(risc_code[i]);
  3023. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3024. cnt);
  3025. if (rval) {
  3026. DEBUG(printk("scsi(%ld): [ERROR] Failed to "
  3027. "load segment %d of firmware\n",
  3028. ha->host_no, num));
  3029. qla_printk(KERN_WARNING, ha,
  3030. "[ERROR] Failed to load segment %d of "
  3031. "firmware\n", num);
  3032. qla2x00_dump_regs(ha);
  3033. break;
  3034. }
  3035. risc_code += cnt;
  3036. risc_addr += cnt;
  3037. risc_size -= cnt;
  3038. num++;
  3039. }
  3040. /* Next firmware sequence */
  3041. fw_iter++;
  3042. }
  3043. return rval;
  3044. }
  3045. int
  3046. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3047. {
  3048. int rval, num, i;
  3049. uint32_t cnt;
  3050. uint32_t *risc_code;
  3051. uint32_t risc_addr, risc_size;
  3052. uint32_t *req_ring;
  3053. struct qla_fw_info *fw_iter;
  3054. rval = QLA_SUCCESS;
  3055. /* Load firmware sequences */
  3056. fw_iter = ha->brd_info->fw_info;
  3057. *srisc_addr = *((uint32_t *)fw_iter->lfwstart);
  3058. while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
  3059. risc_code = (uint32_t *)fw_iter->fwcode;
  3060. risc_size = *((uint32_t *)fw_iter->fwlen);
  3061. risc_addr = *((uint32_t *)fw_iter->lfwstart);
  3062. num = 0;
  3063. rval = 0;
  3064. while (risc_size > 0 && !rval) {
  3065. cnt = (uint32_t)(ha->fw_transfer_size >> 2);
  3066. if (cnt > risc_size)
  3067. cnt = risc_size;
  3068. DEBUG7(printk("scsi(%ld): Loading risc segment@ "
  3069. "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
  3070. ha->host_no, risc_code, cnt, risc_addr));
  3071. req_ring = (uint32_t *)ha->request_ring;
  3072. for (i = 0; i < cnt; i++)
  3073. req_ring[i] = cpu_to_le32(risc_code[i]);
  3074. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3075. cnt);
  3076. if (rval) {
  3077. DEBUG(printk("scsi(%ld): [ERROR] Failed to "
  3078. "load segment %d of firmware\n",
  3079. ha->host_no, num));
  3080. qla_printk(KERN_WARNING, ha,
  3081. "[ERROR] Failed to load segment %d of "
  3082. "firmware\n", num);
  3083. qla2x00_dump_regs(ha);
  3084. break;
  3085. }
  3086. risc_code += cnt;
  3087. risc_addr += cnt;
  3088. risc_size -= cnt;
  3089. num++;
  3090. }
  3091. /* Next firmware sequence */
  3092. fw_iter++;
  3093. }
  3094. return rval;
  3095. }
  3096. int
  3097. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3098. {
  3099. int rval;
  3100. int segments, fragment;
  3101. uint32_t faddr;
  3102. uint32_t *dcode, dlen;
  3103. uint32_t risc_addr;
  3104. uint32_t risc_size;
  3105. uint32_t i;
  3106. rval = QLA_SUCCESS;
  3107. segments = FA_RISC_CODE_SEGMENTS;
  3108. faddr = FA_RISC_CODE_ADDR;
  3109. dcode = (uint32_t *)ha->request_ring;
  3110. *srisc_addr = 0;
  3111. /* Validate firmware image by checking version. */
  3112. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3113. for (i = 0; i < 4; i++)
  3114. dcode[i] = be32_to_cpu(dcode[i]);
  3115. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3116. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3117. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3118. dcode[3] == 0)) {
  3119. qla_printk(KERN_WARNING, ha,
  3120. "Unable to verify integrity of flash firmware image!\n");
  3121. qla_printk(KERN_WARNING, ha,
  3122. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3123. dcode[1], dcode[2], dcode[3]);
  3124. return QLA_FUNCTION_FAILED;
  3125. }
  3126. while (segments && rval == QLA_SUCCESS) {
  3127. /* Read segment's load information. */
  3128. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3129. risc_addr = be32_to_cpu(dcode[2]);
  3130. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3131. risc_size = be32_to_cpu(dcode[3]);
  3132. fragment = 0;
  3133. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3134. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3135. if (dlen > risc_size)
  3136. dlen = risc_size;
  3137. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3138. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3139. ha->host_no, risc_addr, dlen, faddr));
  3140. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3141. for (i = 0; i < dlen; i++)
  3142. dcode[i] = swab32(dcode[i]);
  3143. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3144. dlen);
  3145. if (rval) {
  3146. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3147. "segment %d of firmware\n", ha->host_no,
  3148. fragment));
  3149. qla_printk(KERN_WARNING, ha,
  3150. "[ERROR] Failed to load segment %d of "
  3151. "firmware\n", fragment);
  3152. break;
  3153. }
  3154. faddr += dlen;
  3155. risc_addr += dlen;
  3156. risc_size -= dlen;
  3157. fragment++;
  3158. }
  3159. /* Next segment. */
  3160. segments--;
  3161. }
  3162. return rval;
  3163. }
  3164. #else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
  3165. int
  3166. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3167. {
  3168. int rval;
  3169. int i, fragment;
  3170. uint16_t *wcode, *fwcode;
  3171. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3172. struct fw_blob *blob;
  3173. /* Load firmware blob. */
  3174. blob = qla2x00_request_firmware(ha);
  3175. if (!blob) {
  3176. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3177. return QLA_FUNCTION_FAILED;
  3178. }
  3179. rval = QLA_SUCCESS;
  3180. wcode = (uint16_t *)ha->request_ring;
  3181. *srisc_addr = 0;
  3182. fwcode = (uint16_t *)blob->fw->data;
  3183. fwclen = 0;
  3184. /* Validate firmware image by checking version. */
  3185. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3186. qla_printk(KERN_WARNING, ha,
  3187. "Unable to verify integrity of firmware image (%Zd)!\n",
  3188. blob->fw->size);
  3189. goto fail_fw_integrity;
  3190. }
  3191. for (i = 0; i < 4; i++)
  3192. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3193. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3194. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3195. wcode[2] == 0 && wcode[3] == 0)) {
  3196. qla_printk(KERN_WARNING, ha,
  3197. "Unable to verify integrity of firmware image!\n");
  3198. qla_printk(KERN_WARNING, ha,
  3199. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3200. wcode[1], wcode[2], wcode[3]);
  3201. goto fail_fw_integrity;
  3202. }
  3203. seg = blob->segs;
  3204. while (*seg && rval == QLA_SUCCESS) {
  3205. risc_addr = *seg;
  3206. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3207. risc_size = be16_to_cpu(fwcode[3]);
  3208. /* Validate firmware image size. */
  3209. fwclen += risc_size * sizeof(uint16_t);
  3210. if (blob->fw->size < fwclen) {
  3211. qla_printk(KERN_WARNING, ha,
  3212. "Unable to verify integrity of firmware image "
  3213. "(%Zd)!\n", blob->fw->size);
  3214. goto fail_fw_integrity;
  3215. }
  3216. fragment = 0;
  3217. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3218. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3219. if (wlen > risc_size)
  3220. wlen = risc_size;
  3221. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3222. "addr %x, number of words 0x%x.\n", ha->host_no,
  3223. risc_addr, wlen));
  3224. for (i = 0; i < wlen; i++)
  3225. wcode[i] = swab16(fwcode[i]);
  3226. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3227. wlen);
  3228. if (rval) {
  3229. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3230. "segment %d of firmware\n", ha->host_no,
  3231. fragment));
  3232. qla_printk(KERN_WARNING, ha,
  3233. "[ERROR] Failed to load segment %d of "
  3234. "firmware\n", fragment);
  3235. break;
  3236. }
  3237. fwcode += wlen;
  3238. risc_addr += wlen;
  3239. risc_size -= wlen;
  3240. fragment++;
  3241. }
  3242. /* Next segment. */
  3243. seg++;
  3244. }
  3245. return rval;
  3246. fail_fw_integrity:
  3247. return QLA_FUNCTION_FAILED;
  3248. }
  3249. int
  3250. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3251. {
  3252. int rval;
  3253. int segments, fragment;
  3254. uint32_t *dcode, dlen;
  3255. uint32_t risc_addr;
  3256. uint32_t risc_size;
  3257. uint32_t i;
  3258. struct fw_blob *blob;
  3259. uint32_t *fwcode, fwclen;
  3260. /* Load firmware blob. */
  3261. blob = qla2x00_request_firmware(ha);
  3262. if (!blob) {
  3263. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3264. return QLA_FUNCTION_FAILED;
  3265. }
  3266. rval = QLA_SUCCESS;
  3267. segments = FA_RISC_CODE_SEGMENTS;
  3268. dcode = (uint32_t *)ha->request_ring;
  3269. *srisc_addr = 0;
  3270. fwcode = (uint32_t *)blob->fw->data;
  3271. fwclen = 0;
  3272. /* Validate firmware image by checking version. */
  3273. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3274. qla_printk(KERN_WARNING, ha,
  3275. "Unable to verify integrity of firmware image (%Zd)!\n",
  3276. blob->fw->size);
  3277. goto fail_fw_integrity;
  3278. }
  3279. for (i = 0; i < 4; i++)
  3280. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3281. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3282. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3283. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3284. dcode[3] == 0)) {
  3285. qla_printk(KERN_WARNING, ha,
  3286. "Unable to verify integrity of firmware image!\n");
  3287. qla_printk(KERN_WARNING, ha,
  3288. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3289. dcode[1], dcode[2], dcode[3]);
  3290. goto fail_fw_integrity;
  3291. }
  3292. while (segments && rval == QLA_SUCCESS) {
  3293. risc_addr = be32_to_cpu(fwcode[2]);
  3294. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3295. risc_size = be32_to_cpu(fwcode[3]);
  3296. /* Validate firmware image size. */
  3297. fwclen += risc_size * sizeof(uint32_t);
  3298. if (blob->fw->size < fwclen) {
  3299. qla_printk(KERN_WARNING, ha,
  3300. "Unable to verify integrity of firmware image "
  3301. "(%Zd)!\n", blob->fw->size);
  3302. goto fail_fw_integrity;
  3303. }
  3304. fragment = 0;
  3305. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3306. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3307. if (dlen > risc_size)
  3308. dlen = risc_size;
  3309. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3310. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3311. risc_addr, dlen));
  3312. for (i = 0; i < dlen; i++)
  3313. dcode[i] = swab32(fwcode[i]);
  3314. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3315. dlen);
  3316. if (rval) {
  3317. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3318. "segment %d of firmware\n", ha->host_no,
  3319. fragment));
  3320. qla_printk(KERN_WARNING, ha,
  3321. "[ERROR] Failed to load segment %d of "
  3322. "firmware\n", fragment);
  3323. break;
  3324. }
  3325. fwcode += dlen;
  3326. risc_addr += dlen;
  3327. risc_size -= dlen;
  3328. fragment++;
  3329. }
  3330. /* Next segment. */
  3331. segments--;
  3332. }
  3333. return rval;
  3334. fail_fw_integrity:
  3335. return QLA_FUNCTION_FAILED;
  3336. }
  3337. #endif