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