qla_os.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  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/moduleparam.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/delay.h>
  12. #include <scsi/scsi_tcq.h>
  13. #include <scsi/scsicam.h>
  14. #include <scsi/scsi_transport.h>
  15. #include <scsi/scsi_transport_fc.h>
  16. /*
  17. * Driver version
  18. */
  19. char qla2x00_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static kmem_cache_t *srb_cachep;
  24. /*
  25. * Ioctl related information.
  26. */
  27. static int num_hosts;
  28. int ql2xlogintimeout = 20;
  29. module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
  30. MODULE_PARM_DESC(ql2xlogintimeout,
  31. "Login timeout value in seconds.");
  32. int qlport_down_retry = 30;
  33. module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
  34. MODULE_PARM_DESC(qlport_down_retry,
  35. "Maximum number of command retries to a port that returns"
  36. "a PORT-DOWN status.");
  37. int ql2xplogiabsentdevice;
  38. module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
  39. MODULE_PARM_DESC(ql2xplogiabsentdevice,
  40. "Option to enable PLOGI to devices that are not present after "
  41. "a Fabric scan. This is needed for several broken switches."
  42. "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
  43. int ql2xloginretrycount = 0;
  44. module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
  45. MODULE_PARM_DESC(ql2xloginretrycount,
  46. "Specify an alternate value for the NVRAM login retry count.");
  47. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  48. int ql2xfwloadflash;
  49. module_param(ql2xfwloadflash, int, S_IRUGO|S_IRUSR);
  50. MODULE_PARM_DESC(ql2xfwloadflash,
  51. "Load ISP24xx firmware image from FLASH (onboard memory).");
  52. #endif
  53. static void qla2x00_free_device(scsi_qla_host_t *);
  54. static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
  55. int ql2xfdmienable;
  56. module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
  57. MODULE_PARM_DESC(ql2xfdmienable,
  58. "Enables FDMI registratons "
  59. "Default is 0 - no FDMI. 1 - perfom FDMI.");
  60. int ql2xprocessrscn;
  61. module_param(ql2xprocessrscn, int, S_IRUGO|S_IRUSR);
  62. MODULE_PARM_DESC(ql2xprocessrscn,
  63. "Option to enable port RSCN handling via a series of less"
  64. "fabric intrusive ADISCs and PLOGIs.");
  65. /*
  66. * SCSI host template entry points
  67. */
  68. static int qla2xxx_slave_configure(struct scsi_device * device);
  69. static int qla2xxx_slave_alloc(struct scsi_device *);
  70. static void qla2xxx_slave_destroy(struct scsi_device *);
  71. static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
  72. void (*fn)(struct scsi_cmnd *));
  73. static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
  74. void (*fn)(struct scsi_cmnd *));
  75. static int qla2xxx_eh_abort(struct scsi_cmnd *);
  76. static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
  77. static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
  78. static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
  79. static int qla2x00_loop_reset(scsi_qla_host_t *ha);
  80. static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
  81. static int qla2x00_change_queue_depth(struct scsi_device *, int);
  82. static int qla2x00_change_queue_type(struct scsi_device *, int);
  83. static struct scsi_host_template qla2x00_driver_template = {
  84. .module = THIS_MODULE,
  85. .name = "qla2xxx",
  86. .queuecommand = qla2x00_queuecommand,
  87. .eh_abort_handler = qla2xxx_eh_abort,
  88. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  89. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  90. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  91. .slave_configure = qla2xxx_slave_configure,
  92. .slave_alloc = qla2xxx_slave_alloc,
  93. .slave_destroy = qla2xxx_slave_destroy,
  94. .change_queue_depth = qla2x00_change_queue_depth,
  95. .change_queue_type = qla2x00_change_queue_type,
  96. .this_id = -1,
  97. .cmd_per_lun = 3,
  98. .use_clustering = ENABLE_CLUSTERING,
  99. .sg_tablesize = SG_ALL,
  100. /*
  101. * The RISC allows for each command to transfer (2^32-1) bytes of data,
  102. * which equates to 0x800000 sectors.
  103. */
  104. .max_sectors = 0xFFFF,
  105. .shost_attrs = qla2x00_host_attrs,
  106. };
  107. static struct scsi_host_template qla24xx_driver_template = {
  108. .module = THIS_MODULE,
  109. .name = "qla2xxx",
  110. .queuecommand = qla24xx_queuecommand,
  111. .eh_abort_handler = qla2xxx_eh_abort,
  112. .eh_device_reset_handler = qla2xxx_eh_device_reset,
  113. .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
  114. .eh_host_reset_handler = qla2xxx_eh_host_reset,
  115. .slave_configure = qla2xxx_slave_configure,
  116. .slave_alloc = qla2xxx_slave_alloc,
  117. .slave_destroy = qla2xxx_slave_destroy,
  118. .change_queue_depth = qla2x00_change_queue_depth,
  119. .change_queue_type = qla2x00_change_queue_type,
  120. .this_id = -1,
  121. .cmd_per_lun = 3,
  122. .use_clustering = ENABLE_CLUSTERING,
  123. .sg_tablesize = SG_ALL,
  124. .max_sectors = 0xFFFF,
  125. .shost_attrs = qla2x00_host_attrs,
  126. };
  127. static struct scsi_transport_template *qla2xxx_transport_template = NULL;
  128. /* TODO Convert to inlines
  129. *
  130. * Timer routines
  131. */
  132. #define WATCH_INTERVAL 1 /* number of seconds */
  133. static void qla2x00_timer(scsi_qla_host_t *);
  134. static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
  135. void *, unsigned long);
  136. static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
  137. static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
  138. static inline void
  139. qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
  140. {
  141. init_timer(&ha->timer);
  142. ha->timer.expires = jiffies + interval * HZ;
  143. ha->timer.data = (unsigned long)ha;
  144. ha->timer.function = (void (*)(unsigned long))func;
  145. add_timer(&ha->timer);
  146. ha->timer_active = 1;
  147. }
  148. static inline void
  149. qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
  150. {
  151. mod_timer(&ha->timer, jiffies + interval * HZ);
  152. }
  153. static __inline__ void
  154. qla2x00_stop_timer(scsi_qla_host_t *ha)
  155. {
  156. del_timer_sync(&ha->timer);
  157. ha->timer_active = 0;
  158. }
  159. static int qla2x00_do_dpc(void *data);
  160. static void qla2x00_rst_aen(scsi_qla_host_t *);
  161. static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
  162. static void qla2x00_mem_free(scsi_qla_host_t *ha);
  163. static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
  164. static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
  165. static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
  166. void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
  167. /* -------------------------------------------------------------------------- */
  168. static char *
  169. qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
  170. {
  171. static char *pci_bus_modes[] = {
  172. "33", "66", "100", "133",
  173. };
  174. uint16_t pci_bus;
  175. strcpy(str, "PCI");
  176. pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
  177. if (pci_bus) {
  178. strcat(str, "-X (");
  179. strcat(str, pci_bus_modes[pci_bus]);
  180. } else {
  181. pci_bus = (ha->pci_attr & BIT_8) >> 8;
  182. strcat(str, " (");
  183. strcat(str, pci_bus_modes[pci_bus]);
  184. }
  185. strcat(str, " MHz)");
  186. return (str);
  187. }
  188. static char *
  189. qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
  190. {
  191. static char *pci_bus_modes[] = { "33", "66", "100", "133", };
  192. uint32_t pci_bus;
  193. int pcie_reg;
  194. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  195. if (pcie_reg) {
  196. char lwstr[6];
  197. uint16_t pcie_lstat, lspeed, lwidth;
  198. pcie_reg += 0x12;
  199. pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
  200. lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
  201. lwidth = (pcie_lstat &
  202. (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
  203. strcpy(str, "PCIe (");
  204. if (lspeed == 1)
  205. strcat(str, "2.5Gb/s ");
  206. else
  207. strcat(str, "<unknown> ");
  208. snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
  209. strcat(str, lwstr);
  210. return str;
  211. }
  212. strcpy(str, "PCI");
  213. pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
  214. if (pci_bus == 0 || pci_bus == 8) {
  215. strcat(str, " (");
  216. strcat(str, pci_bus_modes[pci_bus >> 3]);
  217. } else {
  218. strcat(str, "-X ");
  219. if (pci_bus & BIT_2)
  220. strcat(str, "Mode 2");
  221. else
  222. strcat(str, "Mode 1");
  223. strcat(str, " (");
  224. strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
  225. }
  226. strcat(str, " MHz)");
  227. return str;
  228. }
  229. char *
  230. qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
  231. {
  232. char un_str[10];
  233. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  234. ha->fw_minor_version,
  235. ha->fw_subminor_version);
  236. if (ha->fw_attributes & BIT_9) {
  237. strcat(str, "FLX");
  238. return (str);
  239. }
  240. switch (ha->fw_attributes & 0xFF) {
  241. case 0x7:
  242. strcat(str, "EF");
  243. break;
  244. case 0x17:
  245. strcat(str, "TP");
  246. break;
  247. case 0x37:
  248. strcat(str, "IP");
  249. break;
  250. case 0x77:
  251. strcat(str, "VI");
  252. break;
  253. default:
  254. sprintf(un_str, "(%x)", ha->fw_attributes);
  255. strcat(str, un_str);
  256. break;
  257. }
  258. if (ha->fw_attributes & 0x100)
  259. strcat(str, "X");
  260. return (str);
  261. }
  262. char *
  263. qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
  264. {
  265. sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
  266. ha->fw_minor_version,
  267. ha->fw_subminor_version);
  268. if (ha->fw_attributes & BIT_0)
  269. strcat(str, "[Class 2] ");
  270. if (ha->fw_attributes & BIT_1)
  271. strcat(str, "[IP] ");
  272. if (ha->fw_attributes & BIT_2)
  273. strcat(str, "[Multi-ID] ");
  274. if (ha->fw_attributes & BIT_13)
  275. strcat(str, "[Experimental]");
  276. return str;
  277. }
  278. static inline srb_t *
  279. qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
  280. struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  281. {
  282. srb_t *sp;
  283. sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  284. if (!sp)
  285. return sp;
  286. atomic_set(&sp->ref_count, 1);
  287. sp->ha = ha;
  288. sp->fcport = fcport;
  289. sp->cmd = cmd;
  290. sp->flags = 0;
  291. CMD_SP(cmd) = (void *)sp;
  292. cmd->scsi_done = done;
  293. return sp;
  294. }
  295. static int
  296. qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  297. {
  298. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  299. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  300. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  301. srb_t *sp;
  302. int rval;
  303. rval = fc_remote_port_chkready(rport);
  304. if (rval) {
  305. cmd->result = rval;
  306. goto qc_fail_command;
  307. }
  308. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  309. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  310. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  311. cmd->result = DID_NO_CONNECT << 16;
  312. goto qc_fail_command;
  313. }
  314. goto qc_host_busy;
  315. }
  316. spin_unlock_irq(ha->host->host_lock);
  317. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  318. if (!sp)
  319. goto qc_host_busy_lock;
  320. rval = qla2x00_start_scsi(sp);
  321. if (rval != QLA_SUCCESS)
  322. goto qc_host_busy_free_sp;
  323. spin_lock_irq(ha->host->host_lock);
  324. return 0;
  325. qc_host_busy_free_sp:
  326. qla2x00_sp_free_dma(ha, sp);
  327. mempool_free(sp, ha->srb_mempool);
  328. qc_host_busy_lock:
  329. spin_lock_irq(ha->host->host_lock);
  330. qc_host_busy:
  331. return SCSI_MLQUEUE_HOST_BUSY;
  332. qc_fail_command:
  333. done(cmd);
  334. return 0;
  335. }
  336. static int
  337. qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  338. {
  339. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  340. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  341. struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
  342. srb_t *sp;
  343. int rval;
  344. rval = fc_remote_port_chkready(rport);
  345. if (rval) {
  346. cmd->result = rval;
  347. goto qc24_fail_command;
  348. }
  349. if (atomic_read(&fcport->state) != FCS_ONLINE) {
  350. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
  351. atomic_read(&ha->loop_state) == LOOP_DEAD) {
  352. cmd->result = DID_NO_CONNECT << 16;
  353. goto qc24_fail_command;
  354. }
  355. goto qc24_host_busy;
  356. }
  357. spin_unlock_irq(ha->host->host_lock);
  358. sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
  359. if (!sp)
  360. goto qc24_host_busy_lock;
  361. rval = qla24xx_start_scsi(sp);
  362. if (rval != QLA_SUCCESS)
  363. goto qc24_host_busy_free_sp;
  364. spin_lock_irq(ha->host->host_lock);
  365. return 0;
  366. qc24_host_busy_free_sp:
  367. qla2x00_sp_free_dma(ha, sp);
  368. mempool_free(sp, ha->srb_mempool);
  369. qc24_host_busy_lock:
  370. spin_lock_irq(ha->host->host_lock);
  371. qc24_host_busy:
  372. return SCSI_MLQUEUE_HOST_BUSY;
  373. qc24_fail_command:
  374. done(cmd);
  375. return 0;
  376. }
  377. /*
  378. * qla2x00_eh_wait_on_command
  379. * Waits for the command to be returned by the Firmware for some
  380. * max time.
  381. *
  382. * Input:
  383. * ha = actual ha whose done queue will contain the command
  384. * returned by firmware.
  385. * cmd = Scsi Command to wait on.
  386. * flag = Abort/Reset(Bus or Device Reset)
  387. *
  388. * Return:
  389. * Not Found : 0
  390. * Found : 1
  391. */
  392. static int
  393. qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
  394. {
  395. #define ABORT_POLLING_PERIOD 1000
  396. #define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
  397. unsigned long wait_iter = ABORT_WAIT_ITER;
  398. int ret = QLA_SUCCESS;
  399. while (CMD_SP(cmd)) {
  400. msleep(ABORT_POLLING_PERIOD);
  401. if (--wait_iter)
  402. break;
  403. }
  404. if (CMD_SP(cmd))
  405. ret = QLA_FUNCTION_FAILED;
  406. return ret;
  407. }
  408. /*
  409. * qla2x00_wait_for_hba_online
  410. * Wait till the HBA is online after going through
  411. * <= MAX_RETRIES_OF_ISP_ABORT or
  412. * finally HBA is disabled ie marked offline
  413. *
  414. * Input:
  415. * ha - pointer to host adapter structure
  416. *
  417. * Note:
  418. * Does context switching-Release SPIN_LOCK
  419. * (if any) before calling this routine.
  420. *
  421. * Return:
  422. * Success (Adapter is online) : 0
  423. * Failed (Adapter is offline/disabled) : 1
  424. */
  425. static int
  426. qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
  427. {
  428. int return_status;
  429. unsigned long wait_online;
  430. wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  431. while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
  432. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  433. test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
  434. ha->dpc_active) && time_before(jiffies, wait_online)) {
  435. msleep(1000);
  436. }
  437. if (ha->flags.online)
  438. return_status = QLA_SUCCESS;
  439. else
  440. return_status = QLA_FUNCTION_FAILED;
  441. DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
  442. return (return_status);
  443. }
  444. /*
  445. * qla2x00_wait_for_loop_ready
  446. * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
  447. * to be in LOOP_READY state.
  448. * Input:
  449. * ha - pointer to host adapter structure
  450. *
  451. * Note:
  452. * Does context switching-Release SPIN_LOCK
  453. * (if any) before calling this routine.
  454. *
  455. *
  456. * Return:
  457. * Success (LOOP_READY) : 0
  458. * Failed (LOOP_NOT_READY) : 1
  459. */
  460. static inline int
  461. qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
  462. {
  463. int return_status = QLA_SUCCESS;
  464. unsigned long loop_timeout ;
  465. /* wait for 5 min at the max for loop to be ready */
  466. loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  467. while ((!atomic_read(&ha->loop_down_timer) &&
  468. atomic_read(&ha->loop_state) == LOOP_DOWN) ||
  469. atomic_read(&ha->loop_state) != LOOP_READY) {
  470. msleep(1000);
  471. if (time_after_eq(jiffies, loop_timeout)) {
  472. return_status = QLA_FUNCTION_FAILED;
  473. break;
  474. }
  475. }
  476. return (return_status);
  477. }
  478. /**************************************************************************
  479. * qla2xxx_eh_abort
  480. *
  481. * Description:
  482. * The abort function will abort the specified command.
  483. *
  484. * Input:
  485. * cmd = Linux SCSI command packet to be aborted.
  486. *
  487. * Returns:
  488. * Either SUCCESS or FAILED.
  489. *
  490. * Note:
  491. **************************************************************************/
  492. int
  493. qla2xxx_eh_abort(struct scsi_cmnd *cmd)
  494. {
  495. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  496. srb_t *sp;
  497. int ret, i;
  498. unsigned int id, lun;
  499. unsigned long serial;
  500. unsigned long flags;
  501. if (!CMD_SP(cmd))
  502. return FAILED;
  503. ret = FAILED;
  504. id = cmd->device->id;
  505. lun = cmd->device->lun;
  506. serial = cmd->serial_number;
  507. /* Check active list for command command. */
  508. spin_lock_irqsave(&ha->hardware_lock, flags);
  509. for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
  510. sp = ha->outstanding_cmds[i];
  511. if (sp == NULL)
  512. continue;
  513. if (sp->cmd != cmd)
  514. continue;
  515. DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
  516. "sp->state=%x\n", __func__, ha->host_no, sp, serial,
  517. sp->state));
  518. DEBUG3(qla2x00_print_scsi_cmd(cmd);)
  519. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  520. if (ha->isp_ops.abort_command(ha, sp)) {
  521. DEBUG2(printk("%s(%ld): abort_command "
  522. "mbx failed.\n", __func__, ha->host_no));
  523. } else {
  524. DEBUG3(printk("%s(%ld): abort_command "
  525. "mbx success.\n", __func__, ha->host_no));
  526. ret = SUCCESS;
  527. }
  528. spin_lock_irqsave(&ha->hardware_lock, flags);
  529. break;
  530. }
  531. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  532. /* Wait for the command to be returned. */
  533. if (ret == SUCCESS) {
  534. if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
  535. qla_printk(KERN_ERR, ha,
  536. "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
  537. "%x.\n", ha->host_no, id, lun, serial, ret);
  538. }
  539. }
  540. qla_printk(KERN_INFO, ha,
  541. "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
  542. id, lun, serial, ret);
  543. return ret;
  544. }
  545. /**************************************************************************
  546. * qla2x00_eh_wait_for_pending_target_commands
  547. *
  548. * Description:
  549. * Waits for all the commands to come back from the specified target.
  550. *
  551. * Input:
  552. * ha - pointer to scsi_qla_host structure.
  553. * t - target
  554. * Returns:
  555. * Either SUCCESS or FAILED.
  556. *
  557. * Note:
  558. **************************************************************************/
  559. static int
  560. qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
  561. {
  562. int cnt;
  563. int status;
  564. srb_t *sp;
  565. struct scsi_cmnd *cmd;
  566. unsigned long flags;
  567. status = 0;
  568. /*
  569. * Waiting for all commands for the designated target in the active
  570. * array
  571. */
  572. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  573. spin_lock_irqsave(&ha->hardware_lock, flags);
  574. sp = ha->outstanding_cmds[cnt];
  575. if (sp) {
  576. cmd = sp->cmd;
  577. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  578. if (cmd->device->id == t) {
  579. if (!qla2x00_eh_wait_on_command(ha, cmd)) {
  580. status = 1;
  581. break;
  582. }
  583. }
  584. } else {
  585. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  586. }
  587. }
  588. return (status);
  589. }
  590. /**************************************************************************
  591. * qla2xxx_eh_device_reset
  592. *
  593. * Description:
  594. * The device reset function will reset the target and abort any
  595. * executing commands.
  596. *
  597. * NOTE: The use of SP is undefined within this context. Do *NOT*
  598. * attempt to use this value, even if you determine it is
  599. * non-null.
  600. *
  601. * Input:
  602. * cmd = Linux SCSI command packet of the command that cause the
  603. * bus device reset.
  604. *
  605. * Returns:
  606. * SUCCESS/FAILURE (defined as macro in scsi.h).
  607. *
  608. **************************************************************************/
  609. int
  610. qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
  611. {
  612. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  613. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  614. srb_t *sp;
  615. int ret;
  616. unsigned int id, lun;
  617. unsigned long serial;
  618. ret = FAILED;
  619. id = cmd->device->id;
  620. lun = cmd->device->lun;
  621. serial = cmd->serial_number;
  622. sp = (srb_t *) CMD_SP(cmd);
  623. if (!sp || !fcport)
  624. return ret;
  625. qla_printk(KERN_INFO, ha,
  626. "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
  627. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  628. goto eh_dev_reset_done;
  629. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  630. if (qla2x00_device_reset(ha, fcport) == 0)
  631. ret = SUCCESS;
  632. #if defined(LOGOUT_AFTER_DEVICE_RESET)
  633. if (ret == SUCCESS) {
  634. if (fcport->flags & FC_FABRIC_DEVICE) {
  635. ha->isp_ops.fabric_logout(ha, fcport->loop_id);
  636. qla2x00_mark_device_lost(ha, fcport, 0, 0);
  637. }
  638. }
  639. #endif
  640. } else {
  641. DEBUG2(printk(KERN_INFO
  642. "%s failed: loop not ready\n",__func__);)
  643. }
  644. if (ret == FAILED) {
  645. DEBUG3(printk("%s(%ld): device reset failed\n",
  646. __func__, ha->host_no));
  647. qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
  648. __func__);
  649. goto eh_dev_reset_done;
  650. }
  651. /* Flush outstanding commands. */
  652. if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
  653. ret = FAILED;
  654. if (ret == FAILED) {
  655. DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
  656. __func__, ha->host_no));
  657. qla_printk(KERN_INFO, ha,
  658. "%s: failed while waiting for commands\n", __func__);
  659. } else
  660. qla_printk(KERN_INFO, ha,
  661. "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
  662. id, lun);
  663. eh_dev_reset_done:
  664. return ret;
  665. }
  666. /**************************************************************************
  667. * qla2x00_eh_wait_for_pending_commands
  668. *
  669. * Description:
  670. * Waits for all the commands to come back from the specified host.
  671. *
  672. * Input:
  673. * ha - pointer to scsi_qla_host structure.
  674. *
  675. * Returns:
  676. * 1 : SUCCESS
  677. * 0 : FAILED
  678. *
  679. * Note:
  680. **************************************************************************/
  681. static int
  682. qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
  683. {
  684. int cnt;
  685. int status;
  686. srb_t *sp;
  687. struct scsi_cmnd *cmd;
  688. unsigned long flags;
  689. status = 1;
  690. /*
  691. * Waiting for all commands for the designated target in the active
  692. * array
  693. */
  694. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  695. spin_lock_irqsave(&ha->hardware_lock, flags);
  696. sp = ha->outstanding_cmds[cnt];
  697. if (sp) {
  698. cmd = sp->cmd;
  699. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  700. status = qla2x00_eh_wait_on_command(ha, cmd);
  701. if (status == 0)
  702. break;
  703. }
  704. else {
  705. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  706. }
  707. }
  708. return (status);
  709. }
  710. /**************************************************************************
  711. * qla2xxx_eh_bus_reset
  712. *
  713. * Description:
  714. * The bus reset function will reset the bus and abort any executing
  715. * commands.
  716. *
  717. * Input:
  718. * cmd = Linux SCSI command packet of the command that cause the
  719. * bus reset.
  720. *
  721. * Returns:
  722. * SUCCESS/FAILURE (defined as macro in scsi.h).
  723. *
  724. **************************************************************************/
  725. int
  726. qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
  727. {
  728. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  729. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  730. srb_t *sp;
  731. int ret;
  732. unsigned int id, lun;
  733. unsigned long serial;
  734. ret = FAILED;
  735. id = cmd->device->id;
  736. lun = cmd->device->lun;
  737. serial = cmd->serial_number;
  738. sp = (srb_t *) CMD_SP(cmd);
  739. if (!sp || !fcport)
  740. return ret;
  741. qla_printk(KERN_INFO, ha,
  742. "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
  743. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
  744. DEBUG2(printk("%s failed:board disabled\n",__func__));
  745. goto eh_bus_reset_done;
  746. }
  747. if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
  748. if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
  749. ret = SUCCESS;
  750. }
  751. if (ret == FAILED)
  752. goto eh_bus_reset_done;
  753. /* Flush outstanding commands. */
  754. if (!qla2x00_eh_wait_for_pending_commands(ha))
  755. ret = FAILED;
  756. eh_bus_reset_done:
  757. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  758. (ret == FAILED) ? "failed" : "succeded");
  759. return ret;
  760. }
  761. /**************************************************************************
  762. * qla2xxx_eh_host_reset
  763. *
  764. * Description:
  765. * The reset function will reset the Adapter.
  766. *
  767. * Input:
  768. * cmd = Linux SCSI command packet of the command that cause the
  769. * adapter reset.
  770. *
  771. * Returns:
  772. * Either SUCCESS or FAILED.
  773. *
  774. * Note:
  775. **************************************************************************/
  776. int
  777. qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
  778. {
  779. scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
  780. fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
  781. srb_t *sp;
  782. int ret;
  783. unsigned int id, lun;
  784. unsigned long serial;
  785. ret = FAILED;
  786. id = cmd->device->id;
  787. lun = cmd->device->lun;
  788. serial = cmd->serial_number;
  789. sp = (srb_t *) CMD_SP(cmd);
  790. if (!sp || !fcport)
  791. return ret;
  792. qla_printk(KERN_INFO, ha,
  793. "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
  794. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  795. goto eh_host_reset_lock;
  796. /*
  797. * Fixme-may be dpc thread is active and processing
  798. * loop_resync,so wait a while for it to
  799. * be completed and then issue big hammer.Otherwise
  800. * it may cause I/O failure as big hammer marks the
  801. * devices as lost kicking of the port_down_timer
  802. * while dpc is stuck for the mailbox to complete.
  803. */
  804. qla2x00_wait_for_loop_ready(ha);
  805. set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  806. if (qla2x00_abort_isp(ha)) {
  807. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  808. /* failed. schedule dpc to try */
  809. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  810. if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
  811. goto eh_host_reset_lock;
  812. }
  813. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  814. /* Waiting for our command in done_queue to be returned to OS.*/
  815. if (qla2x00_eh_wait_for_pending_commands(ha))
  816. ret = SUCCESS;
  817. eh_host_reset_lock:
  818. qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
  819. (ret == FAILED) ? "failed" : "succeded");
  820. return ret;
  821. }
  822. /*
  823. * qla2x00_loop_reset
  824. * Issue loop reset.
  825. *
  826. * Input:
  827. * ha = adapter block pointer.
  828. *
  829. * Returns:
  830. * 0 = success
  831. */
  832. static int
  833. qla2x00_loop_reset(scsi_qla_host_t *ha)
  834. {
  835. int status = QLA_SUCCESS;
  836. struct fc_port *fcport;
  837. if (ha->flags.enable_lip_reset) {
  838. status = qla2x00_lip_reset(ha);
  839. }
  840. if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
  841. list_for_each_entry(fcport, &ha->fcports, list) {
  842. if (fcport->port_type != FCT_TARGET)
  843. continue;
  844. status = qla2x00_device_reset(ha, fcport);
  845. if (status != QLA_SUCCESS)
  846. break;
  847. }
  848. }
  849. if (status == QLA_SUCCESS &&
  850. ((!ha->flags.enable_target_reset &&
  851. !ha->flags.enable_lip_reset) ||
  852. ha->flags.enable_lip_full_login)) {
  853. status = qla2x00_full_login_lip(ha);
  854. }
  855. /* Issue marker command only when we are going to start the I/O */
  856. ha->marker_needed = 1;
  857. if (status) {
  858. /* Empty */
  859. DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
  860. __func__,
  861. ha->host_no);)
  862. } else {
  863. /* Empty */
  864. DEBUG3(printk("%s(%ld): exiting normally.\n",
  865. __func__,
  866. ha->host_no);)
  867. }
  868. return(status);
  869. }
  870. /*
  871. * qla2x00_device_reset
  872. * Issue bus device reset message to the target.
  873. *
  874. * Input:
  875. * ha = adapter block pointer.
  876. * t = SCSI ID.
  877. * TARGET_QUEUE_LOCK must be released.
  878. * ADAPTER_STATE_LOCK must be released.
  879. *
  880. * Context:
  881. * Kernel context.
  882. */
  883. static int
  884. qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
  885. {
  886. /* Abort Target command will clear Reservation */
  887. return ha->isp_ops.abort_target(reset_fcport);
  888. }
  889. static int
  890. qla2xxx_slave_alloc(struct scsi_device *sdev)
  891. {
  892. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  893. if (!rport || fc_remote_port_chkready(rport))
  894. return -ENXIO;
  895. sdev->hostdata = *(fc_port_t **)rport->dd_data;
  896. return 0;
  897. }
  898. static int
  899. qla2xxx_slave_configure(struct scsi_device *sdev)
  900. {
  901. scsi_qla_host_t *ha = to_qla_host(sdev->host);
  902. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  903. if (sdev->tagged_supported)
  904. scsi_activate_tcq(sdev, 32);
  905. else
  906. scsi_deactivate_tcq(sdev, 32);
  907. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  908. return 0;
  909. }
  910. static void
  911. qla2xxx_slave_destroy(struct scsi_device *sdev)
  912. {
  913. sdev->hostdata = NULL;
  914. }
  915. static int
  916. qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
  917. {
  918. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  919. return sdev->queue_depth;
  920. }
  921. static int
  922. qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
  923. {
  924. if (sdev->tagged_supported) {
  925. scsi_set_tag_type(sdev, tag_type);
  926. if (tag_type)
  927. scsi_activate_tcq(sdev, sdev->queue_depth);
  928. else
  929. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  930. } else
  931. tag_type = 0;
  932. return tag_type;
  933. }
  934. /**
  935. * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
  936. * @ha: HA context
  937. *
  938. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  939. * supported addressing method.
  940. */
  941. static void
  942. qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
  943. {
  944. /* Assume a 32bit DMA mask. */
  945. ha->flags.enable_64bit_addressing = 0;
  946. if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
  947. /* Any upper-dword bits set? */
  948. if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
  949. !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
  950. /* Ok, a 64bit DMA mask is applicable. */
  951. ha->flags.enable_64bit_addressing = 1;
  952. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
  953. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
  954. return;
  955. }
  956. }
  957. dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
  958. pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
  959. }
  960. static int
  961. qla2x00_iospace_config(scsi_qla_host_t *ha)
  962. {
  963. unsigned long pio, pio_len, pio_flags;
  964. unsigned long mmio, mmio_len, mmio_flags;
  965. /* We only need PIO for Flash operations on ISP2312 v2 chips. */
  966. pio = pci_resource_start(ha->pdev, 0);
  967. pio_len = pci_resource_len(ha->pdev, 0);
  968. pio_flags = pci_resource_flags(ha->pdev, 0);
  969. if (pio_flags & IORESOURCE_IO) {
  970. if (pio_len < MIN_IOBASE_LEN) {
  971. qla_printk(KERN_WARNING, ha,
  972. "Invalid PCI I/O region size (%s)...\n",
  973. pci_name(ha->pdev));
  974. pio = 0;
  975. }
  976. } else {
  977. qla_printk(KERN_WARNING, ha,
  978. "region #0 not a PIO resource (%s)...\n",
  979. pci_name(ha->pdev));
  980. pio = 0;
  981. }
  982. /* Use MMIO operations for all accesses. */
  983. mmio = pci_resource_start(ha->pdev, 1);
  984. mmio_len = pci_resource_len(ha->pdev, 1);
  985. mmio_flags = pci_resource_flags(ha->pdev, 1);
  986. if (!(mmio_flags & IORESOURCE_MEM)) {
  987. qla_printk(KERN_ERR, ha,
  988. "region #0 not an MMIO resource (%s), aborting\n",
  989. pci_name(ha->pdev));
  990. goto iospace_error_exit;
  991. }
  992. if (mmio_len < MIN_IOBASE_LEN) {
  993. qla_printk(KERN_ERR, ha,
  994. "Invalid PCI mem region size (%s), aborting\n",
  995. pci_name(ha->pdev));
  996. goto iospace_error_exit;
  997. }
  998. if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
  999. qla_printk(KERN_WARNING, ha,
  1000. "Failed to reserve PIO/MMIO regions (%s)\n",
  1001. pci_name(ha->pdev));
  1002. goto iospace_error_exit;
  1003. }
  1004. ha->pio_address = pio;
  1005. ha->pio_length = pio_len;
  1006. ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
  1007. if (!ha->iobase) {
  1008. qla_printk(KERN_ERR, ha,
  1009. "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
  1010. goto iospace_error_exit;
  1011. }
  1012. return (0);
  1013. iospace_error_exit:
  1014. return (-ENOMEM);
  1015. }
  1016. static void
  1017. qla2x00_enable_intrs(scsi_qla_host_t *ha)
  1018. {
  1019. unsigned long flags = 0;
  1020. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1021. spin_lock_irqsave(&ha->hardware_lock, flags);
  1022. ha->interrupts_on = 1;
  1023. /* enable risc and host interrupts */
  1024. WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
  1025. RD_REG_WORD(&reg->ictrl);
  1026. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1027. }
  1028. static void
  1029. qla2x00_disable_intrs(scsi_qla_host_t *ha)
  1030. {
  1031. unsigned long flags = 0;
  1032. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1033. spin_lock_irqsave(&ha->hardware_lock, flags);
  1034. ha->interrupts_on = 0;
  1035. /* disable risc and host interrupts */
  1036. WRT_REG_WORD(&reg->ictrl, 0);
  1037. RD_REG_WORD(&reg->ictrl);
  1038. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1039. }
  1040. static void
  1041. qla24xx_enable_intrs(scsi_qla_host_t *ha)
  1042. {
  1043. unsigned long flags = 0;
  1044. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1045. spin_lock_irqsave(&ha->hardware_lock, flags);
  1046. ha->interrupts_on = 1;
  1047. WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
  1048. RD_REG_DWORD(&reg->ictrl);
  1049. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1050. }
  1051. static void
  1052. qla24xx_disable_intrs(scsi_qla_host_t *ha)
  1053. {
  1054. unsigned long flags = 0;
  1055. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1056. spin_lock_irqsave(&ha->hardware_lock, flags);
  1057. ha->interrupts_on = 0;
  1058. WRT_REG_DWORD(&reg->ictrl, 0);
  1059. RD_REG_DWORD(&reg->ictrl);
  1060. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1061. }
  1062. /*
  1063. * PCI driver interface
  1064. */
  1065. int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
  1066. {
  1067. int ret = -ENODEV;
  1068. device_reg_t __iomem *reg;
  1069. struct Scsi_Host *host;
  1070. scsi_qla_host_t *ha;
  1071. unsigned long flags = 0;
  1072. unsigned long wait_switch = 0;
  1073. char pci_info[20];
  1074. char fw_str[30];
  1075. fc_port_t *fcport;
  1076. struct scsi_host_template *sht;
  1077. if (pci_enable_device(pdev))
  1078. goto probe_out;
  1079. sht = &qla2x00_driver_template;
  1080. if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
  1081. pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
  1082. sht = &qla24xx_driver_template;
  1083. host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
  1084. if (host == NULL) {
  1085. printk(KERN_WARNING
  1086. "qla2xxx: Couldn't allocate host from scsi layer!\n");
  1087. goto probe_disable_device;
  1088. }
  1089. /* Clear our data area */
  1090. ha = (scsi_qla_host_t *)host->hostdata;
  1091. memset(ha, 0, sizeof(scsi_qla_host_t));
  1092. ha->pdev = pdev;
  1093. ha->host = host;
  1094. ha->host_no = host->host_no;
  1095. ha->brd_info = brd_info;
  1096. sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
  1097. ha->dpc_pid = -1;
  1098. /* Configure PCI I/O space */
  1099. ret = qla2x00_iospace_config(ha);
  1100. if (ret)
  1101. goto probe_failed;
  1102. qla_printk(KERN_INFO, ha,
  1103. "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
  1104. ha->iobase);
  1105. spin_lock_init(&ha->hardware_lock);
  1106. ha->prev_topology = 0;
  1107. ha->ports = MAX_BUSES;
  1108. ha->init_cb_size = sizeof(init_cb_t);
  1109. ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
  1110. /* Assign ISP specific operations. */
  1111. ha->isp_ops.pci_config = qla2100_pci_config;
  1112. ha->isp_ops.reset_chip = qla2x00_reset_chip;
  1113. ha->isp_ops.chip_diag = qla2x00_chip_diag;
  1114. ha->isp_ops.config_rings = qla2x00_config_rings;
  1115. ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
  1116. ha->isp_ops.nvram_config = qla2x00_nvram_config;
  1117. ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
  1118. ha->isp_ops.load_risc = qla2x00_load_risc;
  1119. ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
  1120. ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
  1121. ha->isp_ops.intr_handler = qla2100_intr_handler;
  1122. ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
  1123. ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
  1124. ha->isp_ops.abort_command = qla2x00_abort_command;
  1125. ha->isp_ops.abort_target = qla2x00_abort_target;
  1126. ha->isp_ops.fabric_login = qla2x00_login_fabric;
  1127. ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
  1128. ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
  1129. ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
  1130. ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
  1131. ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
  1132. ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
  1133. ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
  1134. ha->isp_ops.fw_dump = qla2100_fw_dump;
  1135. ha->isp_ops.ascii_fw_dump = qla2100_ascii_fw_dump;
  1136. if (IS_QLA2100(ha)) {
  1137. host->max_id = MAX_TARGETS_2100;
  1138. ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
  1139. ha->request_q_length = REQUEST_ENTRY_CNT_2100;
  1140. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1141. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1142. host->sg_tablesize = 32;
  1143. ha->gid_list_info_size = 4;
  1144. } else if (IS_QLA2200(ha)) {
  1145. host->max_id = MAX_TARGETS_2200;
  1146. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1147. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1148. ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
  1149. ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
  1150. ha->gid_list_info_size = 4;
  1151. } else if (IS_QLA23XX(ha)) {
  1152. host->max_id = MAX_TARGETS_2200;
  1153. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1154. ha->request_q_length = REQUEST_ENTRY_CNT_2200;
  1155. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1156. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1157. ha->isp_ops.pci_config = qla2300_pci_config;
  1158. ha->isp_ops.intr_handler = qla2300_intr_handler;
  1159. ha->isp_ops.fw_dump = qla2300_fw_dump;
  1160. ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
  1161. ha->gid_list_info_size = 6;
  1162. } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
  1163. host->max_id = MAX_TARGETS_2200;
  1164. ha->mbx_count = MAILBOX_REGISTER_COUNT;
  1165. ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
  1166. ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
  1167. ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
  1168. ha->init_cb_size = sizeof(struct init_cb_24xx);
  1169. ha->mgmt_svr_loop_id = 10;
  1170. ha->isp_ops.pci_config = qla24xx_pci_config;
  1171. ha->isp_ops.reset_chip = qla24xx_reset_chip;
  1172. ha->isp_ops.chip_diag = qla24xx_chip_diag;
  1173. ha->isp_ops.config_rings = qla24xx_config_rings;
  1174. ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
  1175. ha->isp_ops.nvram_config = qla24xx_nvram_config;
  1176. ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
  1177. ha->isp_ops.load_risc = qla24xx_load_risc;
  1178. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  1179. if (ql2xfwloadflash)
  1180. ha->isp_ops.load_risc = qla24xx_load_risc_flash;
  1181. #endif
  1182. ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
  1183. ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
  1184. ha->isp_ops.intr_handler = qla24xx_intr_handler;
  1185. ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
  1186. ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
  1187. ha->isp_ops.abort_command = qla24xx_abort_command;
  1188. ha->isp_ops.abort_target = qla24xx_abort_target;
  1189. ha->isp_ops.fabric_login = qla24xx_login_fabric;
  1190. ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
  1191. ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
  1192. ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
  1193. ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
  1194. ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
  1195. ha->isp_ops.fw_dump = qla24xx_fw_dump;
  1196. ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
  1197. ha->gid_list_info_size = 8;
  1198. }
  1199. host->can_queue = ha->request_q_length + 128;
  1200. /* load the F/W, read paramaters, and init the H/W */
  1201. ha->instance = num_hosts;
  1202. init_MUTEX(&ha->mbx_cmd_sem);
  1203. init_MUTEX_LOCKED(&ha->mbx_intr_sem);
  1204. INIT_LIST_HEAD(&ha->list);
  1205. INIT_LIST_HEAD(&ha->fcports);
  1206. INIT_LIST_HEAD(&ha->rscn_fcports);
  1207. /*
  1208. * These locks are used to prevent more than one CPU
  1209. * from modifying the queue at the same time. The
  1210. * higher level "host_lock" will reduce most
  1211. * contention for these locks.
  1212. */
  1213. spin_lock_init(&ha->mbx_reg_lock);
  1214. init_completion(&ha->dpc_inited);
  1215. init_completion(&ha->dpc_exited);
  1216. qla2x00_config_dma_addressing(ha);
  1217. if (qla2x00_mem_alloc(ha)) {
  1218. qla_printk(KERN_WARNING, ha,
  1219. "[ERROR] Failed to allocate memory for adapter\n");
  1220. ret = -ENOMEM;
  1221. goto probe_failed;
  1222. }
  1223. if (qla2x00_initialize_adapter(ha) &&
  1224. !(ha->device_flags & DFLG_NO_CABLE)) {
  1225. qla_printk(KERN_WARNING, ha,
  1226. "Failed to initialize adapter\n");
  1227. DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
  1228. "Adapter flags %x.\n",
  1229. ha->host_no, ha->device_flags));
  1230. ret = -ENODEV;
  1231. goto probe_failed;
  1232. }
  1233. /*
  1234. * Startup the kernel thread for this host adapter
  1235. */
  1236. ha->dpc_should_die = 0;
  1237. ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
  1238. if (ha->dpc_pid < 0) {
  1239. qla_printk(KERN_WARNING, ha,
  1240. "Unable to start DPC thread!\n");
  1241. ret = -ENODEV;
  1242. goto probe_failed;
  1243. }
  1244. wait_for_completion(&ha->dpc_inited);
  1245. host->this_id = 255;
  1246. host->cmd_per_lun = 3;
  1247. host->unique_id = ha->instance;
  1248. host->max_cmd_len = MAX_CMDSZ;
  1249. host->max_channel = ha->ports - 1;
  1250. host->max_lun = MAX_LUNS;
  1251. host->transportt = qla2xxx_transport_template;
  1252. ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
  1253. SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
  1254. if (ret) {
  1255. qla_printk(KERN_WARNING, ha,
  1256. "Failed to reserve interrupt %d already in use.\n",
  1257. pdev->irq);
  1258. goto probe_failed;
  1259. }
  1260. host->irq = pdev->irq;
  1261. /* Initialized the timer */
  1262. qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
  1263. DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
  1264. ha->host_no, ha));
  1265. ha->isp_ops.disable_intrs(ha);
  1266. spin_lock_irqsave(&ha->hardware_lock, flags);
  1267. reg = ha->iobase;
  1268. if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
  1269. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
  1270. WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
  1271. } else {
  1272. WRT_REG_WORD(&reg->isp.semaphore, 0);
  1273. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
  1274. WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
  1275. /* Enable proper parity */
  1276. if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
  1277. if (IS_QLA2300(ha))
  1278. /* SRAM parity */
  1279. WRT_REG_WORD(&reg->isp.hccr,
  1280. (HCCR_ENABLE_PARITY + 0x1));
  1281. else
  1282. /* SRAM, Instruction RAM and GP RAM parity */
  1283. WRT_REG_WORD(&reg->isp.hccr,
  1284. (HCCR_ENABLE_PARITY + 0x7));
  1285. }
  1286. }
  1287. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1288. ha->isp_ops.enable_intrs(ha);
  1289. /* v2.19.5b6 */
  1290. /*
  1291. * Wait around max loop_reset_delay secs for the devices to come
  1292. * on-line. We don't want Linux scanning before we are ready.
  1293. *
  1294. */
  1295. for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
  1296. time_before(jiffies,wait_switch) &&
  1297. !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
  1298. && (ha->device_flags & SWITCH_FOUND) ;) {
  1299. qla2x00_check_fabric_devices(ha);
  1300. msleep(10);
  1301. }
  1302. pci_set_drvdata(pdev, ha);
  1303. ha->flags.init_done = 1;
  1304. num_hosts++;
  1305. ret = scsi_add_host(host, &pdev->dev);
  1306. if (ret)
  1307. goto probe_failed;
  1308. qla2x00_alloc_sysfs_attr(ha);
  1309. qla2x00_init_host_attr(ha);
  1310. qla_printk(KERN_INFO, ha, "\n"
  1311. " QLogic Fibre Channel HBA Driver: %s\n"
  1312. " QLogic %s - %s\n"
  1313. " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
  1314. qla2x00_version_str, ha->model_number,
  1315. ha->model_desc ? ha->model_desc: "", pdev->device,
  1316. ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
  1317. ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
  1318. ha->isp_ops.fw_version_str(ha, fw_str));
  1319. /* Go with fc_rport registration. */
  1320. list_for_each_entry(fcport, &ha->fcports, list)
  1321. qla2x00_reg_remote_port(ha, fcport);
  1322. return 0;
  1323. probe_failed:
  1324. qla2x00_free_device(ha);
  1325. scsi_host_put(host);
  1326. probe_disable_device:
  1327. pci_disable_device(pdev);
  1328. probe_out:
  1329. return ret;
  1330. }
  1331. EXPORT_SYMBOL_GPL(qla2x00_probe_one);
  1332. void qla2x00_remove_one(struct pci_dev *pdev)
  1333. {
  1334. scsi_qla_host_t *ha;
  1335. ha = pci_get_drvdata(pdev);
  1336. qla2x00_free_sysfs_attr(ha);
  1337. fc_remove_host(ha->host);
  1338. scsi_remove_host(ha->host);
  1339. qla2x00_free_device(ha);
  1340. scsi_host_put(ha->host);
  1341. pci_set_drvdata(pdev, NULL);
  1342. }
  1343. EXPORT_SYMBOL_GPL(qla2x00_remove_one);
  1344. static void
  1345. qla2x00_free_device(scsi_qla_host_t *ha)
  1346. {
  1347. int ret;
  1348. /* Abort any outstanding IO descriptors. */
  1349. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1350. qla2x00_cancel_io_descriptors(ha);
  1351. /* Disable timer */
  1352. if (ha->timer_active)
  1353. qla2x00_stop_timer(ha);
  1354. /* Kill the kernel thread for this host */
  1355. if (ha->dpc_pid >= 0) {
  1356. ha->dpc_should_die = 1;
  1357. wmb();
  1358. ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
  1359. if (ret) {
  1360. qla_printk(KERN_ERR, ha,
  1361. "Unable to signal DPC thread -- (%d)\n", ret);
  1362. /* TODO: SOMETHING MORE??? */
  1363. } else {
  1364. wait_for_completion(&ha->dpc_exited);
  1365. }
  1366. }
  1367. /* Stop currently executing firmware. */
  1368. qla2x00_stop_firmware(ha);
  1369. /* turn-off interrupts on the card */
  1370. if (ha->interrupts_on)
  1371. ha->isp_ops.disable_intrs(ha);
  1372. qla2x00_mem_free(ha);
  1373. ha->flags.online = 0;
  1374. /* Detach interrupts */
  1375. if (ha->pdev->irq)
  1376. free_irq(ha->pdev->irq, ha);
  1377. /* release io space registers */
  1378. if (ha->iobase)
  1379. iounmap(ha->iobase);
  1380. pci_release_regions(ha->pdev);
  1381. pci_disable_device(ha->pdev);
  1382. }
  1383. static inline void
  1384. qla2x00_schedule_rport_del(struct scsi_qla_host *ha, fc_port_t *fcport,
  1385. int defer)
  1386. {
  1387. unsigned long flags;
  1388. struct fc_rport *rport;
  1389. if (!fcport->rport)
  1390. return;
  1391. rport = fcport->rport;
  1392. if (defer) {
  1393. spin_lock_irqsave(&fcport->rport_lock, flags);
  1394. fcport->drport = rport;
  1395. fcport->rport = NULL;
  1396. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1397. set_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags);
  1398. } else {
  1399. spin_lock_irqsave(&fcport->rport_lock, flags);
  1400. fcport->rport = NULL;
  1401. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1402. fc_remote_port_delete(rport);
  1403. }
  1404. }
  1405. /*
  1406. * qla2x00_mark_device_lost Updates fcport state when device goes offline.
  1407. *
  1408. * Input: ha = adapter block pointer. fcport = port structure pointer.
  1409. *
  1410. * Return: None.
  1411. *
  1412. * Context:
  1413. */
  1414. void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
  1415. int do_login, int defer)
  1416. {
  1417. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1418. qla2x00_schedule_rport_del(ha, fcport, defer);
  1419. /*
  1420. * We may need to retry the login, so don't change the state of the
  1421. * port but do the retries.
  1422. */
  1423. if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
  1424. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1425. if (!do_login)
  1426. return;
  1427. if (fcport->login_retry == 0) {
  1428. fcport->login_retry = ha->login_retry_count;
  1429. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1430. DEBUG(printk("scsi(%ld): Port login retry: "
  1431. "%02x%02x%02x%02x%02x%02x%02x%02x, "
  1432. "id = 0x%04x retry cnt=%d\n",
  1433. ha->host_no,
  1434. fcport->port_name[0],
  1435. fcport->port_name[1],
  1436. fcport->port_name[2],
  1437. fcport->port_name[3],
  1438. fcport->port_name[4],
  1439. fcport->port_name[5],
  1440. fcport->port_name[6],
  1441. fcport->port_name[7],
  1442. fcport->loop_id,
  1443. fcport->login_retry));
  1444. }
  1445. }
  1446. /*
  1447. * qla2x00_mark_all_devices_lost
  1448. * Updates fcport state when device goes offline.
  1449. *
  1450. * Input:
  1451. * ha = adapter block pointer.
  1452. * fcport = port structure pointer.
  1453. *
  1454. * Return:
  1455. * None.
  1456. *
  1457. * Context:
  1458. */
  1459. void
  1460. qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha, int defer)
  1461. {
  1462. fc_port_t *fcport;
  1463. list_for_each_entry(fcport, &ha->fcports, list) {
  1464. if (fcport->port_type != FCT_TARGET)
  1465. continue;
  1466. /*
  1467. * No point in marking the device as lost, if the device is
  1468. * already DEAD.
  1469. */
  1470. if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
  1471. continue;
  1472. if (atomic_read(&fcport->state) == FCS_ONLINE)
  1473. qla2x00_schedule_rport_del(ha, fcport, defer);
  1474. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1475. }
  1476. if (defer && ha->dpc_wait && !ha->dpc_active)
  1477. up(ha->dpc_wait);
  1478. }
  1479. /*
  1480. * qla2x00_mem_alloc
  1481. * Allocates adapter memory.
  1482. *
  1483. * Returns:
  1484. * 0 = success.
  1485. * 1 = failure.
  1486. */
  1487. static uint8_t
  1488. qla2x00_mem_alloc(scsi_qla_host_t *ha)
  1489. {
  1490. char name[16];
  1491. uint8_t status = 1;
  1492. int retry= 10;
  1493. do {
  1494. /*
  1495. * This will loop only once if everything goes well, else some
  1496. * number of retries will be performed to get around a kernel
  1497. * bug where available mem is not allocated until after a
  1498. * little delay and a retry.
  1499. */
  1500. ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
  1501. (ha->request_q_length + 1) * sizeof(request_t),
  1502. &ha->request_dma, GFP_KERNEL);
  1503. if (ha->request_ring == NULL) {
  1504. qla_printk(KERN_WARNING, ha,
  1505. "Memory Allocation failed - request_ring\n");
  1506. qla2x00_mem_free(ha);
  1507. msleep(100);
  1508. continue;
  1509. }
  1510. ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
  1511. (ha->response_q_length + 1) * sizeof(response_t),
  1512. &ha->response_dma, GFP_KERNEL);
  1513. if (ha->response_ring == NULL) {
  1514. qla_printk(KERN_WARNING, ha,
  1515. "Memory Allocation failed - response_ring\n");
  1516. qla2x00_mem_free(ha);
  1517. msleep(100);
  1518. continue;
  1519. }
  1520. ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
  1521. &ha->gid_list_dma, GFP_KERNEL);
  1522. if (ha->gid_list == NULL) {
  1523. qla_printk(KERN_WARNING, ha,
  1524. "Memory Allocation failed - gid_list\n");
  1525. qla2x00_mem_free(ha);
  1526. msleep(100);
  1527. continue;
  1528. }
  1529. ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
  1530. sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
  1531. if (ha->rlc_rsp == NULL) {
  1532. qla_printk(KERN_WARNING, ha,
  1533. "Memory Allocation failed - rlc");
  1534. qla2x00_mem_free(ha);
  1535. msleep(100);
  1536. continue;
  1537. }
  1538. snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
  1539. ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
  1540. DMA_POOL_SIZE, 8, 0);
  1541. if (ha->s_dma_pool == NULL) {
  1542. qla_printk(KERN_WARNING, ha,
  1543. "Memory Allocation failed - s_dma_pool\n");
  1544. qla2x00_mem_free(ha);
  1545. msleep(100);
  1546. continue;
  1547. }
  1548. /* get consistent memory allocated for init control block */
  1549. ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1550. &ha->init_cb_dma);
  1551. if (ha->init_cb == NULL) {
  1552. qla_printk(KERN_WARNING, ha,
  1553. "Memory Allocation failed - init_cb\n");
  1554. qla2x00_mem_free(ha);
  1555. msleep(100);
  1556. continue;
  1557. }
  1558. memset(ha->init_cb, 0, ha->init_cb_size);
  1559. /* Get consistent memory allocated for Get Port Database cmd */
  1560. ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1561. &ha->iodesc_pd_dma);
  1562. if (ha->iodesc_pd == NULL) {
  1563. /* error */
  1564. qla_printk(KERN_WARNING, ha,
  1565. "Memory Allocation failed - iodesc_pd\n");
  1566. qla2x00_mem_free(ha);
  1567. msleep(100);
  1568. continue;
  1569. }
  1570. memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
  1571. /* Allocate ioctl related memory. */
  1572. if (qla2x00_alloc_ioctl_mem(ha)) {
  1573. qla_printk(KERN_WARNING, ha,
  1574. "Memory Allocation failed - ioctl_mem\n");
  1575. qla2x00_mem_free(ha);
  1576. msleep(100);
  1577. continue;
  1578. }
  1579. if (qla2x00_allocate_sp_pool(ha)) {
  1580. qla_printk(KERN_WARNING, ha,
  1581. "Memory Allocation failed - "
  1582. "qla2x00_allocate_sp_pool()\n");
  1583. qla2x00_mem_free(ha);
  1584. msleep(100);
  1585. continue;
  1586. }
  1587. /* Allocate memory for SNS commands */
  1588. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1589. /* Get consistent memory allocated for SNS commands */
  1590. ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
  1591. sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
  1592. GFP_KERNEL);
  1593. if (ha->sns_cmd == NULL) {
  1594. /* error */
  1595. qla_printk(KERN_WARNING, ha,
  1596. "Memory Allocation failed - sns_cmd\n");
  1597. qla2x00_mem_free(ha);
  1598. msleep(100);
  1599. continue;
  1600. }
  1601. memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
  1602. } else {
  1603. /* Get consistent memory allocated for MS IOCB */
  1604. ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
  1605. &ha->ms_iocb_dma);
  1606. if (ha->ms_iocb == NULL) {
  1607. /* error */
  1608. qla_printk(KERN_WARNING, ha,
  1609. "Memory Allocation failed - ms_iocb\n");
  1610. qla2x00_mem_free(ha);
  1611. msleep(100);
  1612. continue;
  1613. }
  1614. memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
  1615. /*
  1616. * Get consistent memory allocated for CT SNS
  1617. * commands
  1618. */
  1619. ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
  1620. sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
  1621. GFP_KERNEL);
  1622. if (ha->ct_sns == NULL) {
  1623. /* error */
  1624. qla_printk(KERN_WARNING, ha,
  1625. "Memory Allocation failed - ct_sns\n");
  1626. qla2x00_mem_free(ha);
  1627. msleep(100);
  1628. continue;
  1629. }
  1630. memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
  1631. }
  1632. /* Done all allocations without any error. */
  1633. status = 0;
  1634. } while (retry-- && status != 0);
  1635. if (status) {
  1636. printk(KERN_WARNING
  1637. "%s(): **** FAILED ****\n", __func__);
  1638. }
  1639. return(status);
  1640. }
  1641. /*
  1642. * qla2x00_mem_free
  1643. * Frees all adapter allocated memory.
  1644. *
  1645. * Input:
  1646. * ha = adapter block pointer.
  1647. */
  1648. static void
  1649. qla2x00_mem_free(scsi_qla_host_t *ha)
  1650. {
  1651. struct list_head *fcpl, *fcptemp;
  1652. fc_port_t *fcport;
  1653. unsigned int wtime;/* max wait time if mbx cmd is busy. */
  1654. if (ha == NULL) {
  1655. /* error */
  1656. DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
  1657. return;
  1658. }
  1659. /* Make sure all other threads are stopped. */
  1660. wtime = 60 * 1000;
  1661. while (ha->dpc_wait && wtime)
  1662. wtime = msleep_interruptible(wtime);
  1663. /* free ioctl memory */
  1664. qla2x00_free_ioctl_mem(ha);
  1665. /* free sp pool */
  1666. qla2x00_free_sp_pool(ha);
  1667. if (ha->sns_cmd)
  1668. dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
  1669. ha->sns_cmd, ha->sns_cmd_dma);
  1670. if (ha->ct_sns)
  1671. dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
  1672. ha->ct_sns, ha->ct_sns_dma);
  1673. if (ha->ms_iocb)
  1674. dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
  1675. if (ha->iodesc_pd)
  1676. dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
  1677. if (ha->init_cb)
  1678. dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
  1679. if (ha->s_dma_pool)
  1680. dma_pool_destroy(ha->s_dma_pool);
  1681. if (ha->rlc_rsp)
  1682. dma_free_coherent(&ha->pdev->dev,
  1683. sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
  1684. ha->rlc_rsp_dma);
  1685. if (ha->gid_list)
  1686. dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
  1687. ha->gid_list_dma);
  1688. if (ha->response_ring)
  1689. dma_free_coherent(&ha->pdev->dev,
  1690. (ha->response_q_length + 1) * sizeof(response_t),
  1691. ha->response_ring, ha->response_dma);
  1692. if (ha->request_ring)
  1693. dma_free_coherent(&ha->pdev->dev,
  1694. (ha->request_q_length + 1) * sizeof(request_t),
  1695. ha->request_ring, ha->request_dma);
  1696. ha->sns_cmd = NULL;
  1697. ha->sns_cmd_dma = 0;
  1698. ha->ct_sns = NULL;
  1699. ha->ct_sns_dma = 0;
  1700. ha->ms_iocb = NULL;
  1701. ha->ms_iocb_dma = 0;
  1702. ha->iodesc_pd = NULL;
  1703. ha->iodesc_pd_dma = 0;
  1704. ha->init_cb = NULL;
  1705. ha->init_cb_dma = 0;
  1706. ha->s_dma_pool = NULL;
  1707. ha->rlc_rsp = NULL;
  1708. ha->rlc_rsp_dma = 0;
  1709. ha->gid_list = NULL;
  1710. ha->gid_list_dma = 0;
  1711. ha->response_ring = NULL;
  1712. ha->response_dma = 0;
  1713. ha->request_ring = NULL;
  1714. ha->request_dma = 0;
  1715. list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
  1716. fcport = list_entry(fcpl, fc_port_t, list);
  1717. /* fc ports */
  1718. list_del_init(&fcport->list);
  1719. kfree(fcport);
  1720. }
  1721. INIT_LIST_HEAD(&ha->fcports);
  1722. if (ha->fw_dump)
  1723. free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
  1724. vfree(ha->fw_dump24);
  1725. vfree(ha->fw_dump_buffer);
  1726. ha->fw_dump = NULL;
  1727. ha->fw_dump24 = NULL;
  1728. ha->fw_dumped = 0;
  1729. ha->fw_dump_reading = 0;
  1730. ha->fw_dump_buffer = NULL;
  1731. }
  1732. /*
  1733. * qla2x00_allocate_sp_pool
  1734. * This routine is called during initialization to allocate
  1735. * memory for local srb_t.
  1736. *
  1737. * Input:
  1738. * ha = adapter block pointer.
  1739. *
  1740. * Context:
  1741. * Kernel context.
  1742. *
  1743. * Note: Sets the ref_count for non Null sp to one.
  1744. */
  1745. static int
  1746. qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
  1747. {
  1748. int rval;
  1749. rval = QLA_SUCCESS;
  1750. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  1751. mempool_free_slab, srb_cachep);
  1752. if (ha->srb_mempool == NULL) {
  1753. qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
  1754. rval = QLA_FUNCTION_FAILED;
  1755. }
  1756. return (rval);
  1757. }
  1758. /*
  1759. * This routine frees all adapter allocated memory.
  1760. *
  1761. */
  1762. static void
  1763. qla2x00_free_sp_pool( scsi_qla_host_t *ha)
  1764. {
  1765. if (ha->srb_mempool) {
  1766. mempool_destroy(ha->srb_mempool);
  1767. ha->srb_mempool = NULL;
  1768. }
  1769. }
  1770. /**************************************************************************
  1771. * qla2x00_do_dpc
  1772. * This kernel thread is a task that is schedule by the interrupt handler
  1773. * to perform the background processing for interrupts.
  1774. *
  1775. * Notes:
  1776. * This task always run in the context of a kernel thread. It
  1777. * is kick-off by the driver's detect code and starts up
  1778. * up one per adapter. It immediately goes to sleep and waits for
  1779. * some fibre event. When either the interrupt handler or
  1780. * the timer routine detects a event it will one of the task
  1781. * bits then wake us up.
  1782. **************************************************************************/
  1783. static int
  1784. qla2x00_do_dpc(void *data)
  1785. {
  1786. DECLARE_MUTEX_LOCKED(sem);
  1787. scsi_qla_host_t *ha;
  1788. fc_port_t *fcport;
  1789. uint8_t status;
  1790. uint16_t next_loopid;
  1791. ha = (scsi_qla_host_t *)data;
  1792. lock_kernel();
  1793. daemonize("%s_dpc", ha->host_str);
  1794. allow_signal(SIGHUP);
  1795. ha->dpc_wait = &sem;
  1796. set_user_nice(current, -20);
  1797. unlock_kernel();
  1798. complete(&ha->dpc_inited);
  1799. while (1) {
  1800. DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
  1801. if (down_interruptible(&sem))
  1802. break;
  1803. if (ha->dpc_should_die)
  1804. break;
  1805. DEBUG3(printk("qla2x00: DPC handler waking up\n"));
  1806. /* Initialization not yet finished. Don't do anything yet. */
  1807. if (!ha->flags.init_done || ha->dpc_active)
  1808. continue;
  1809. DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
  1810. ha->dpc_active = 1;
  1811. if (ha->flags.mbox_busy) {
  1812. ha->dpc_active = 0;
  1813. continue;
  1814. }
  1815. if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  1816. DEBUG(printk("scsi(%ld): dpc: sched "
  1817. "qla2x00_abort_isp ha = %p\n",
  1818. ha->host_no, ha));
  1819. if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
  1820. &ha->dpc_flags))) {
  1821. if (qla2x00_abort_isp(ha)) {
  1822. /* failed. retry later */
  1823. set_bit(ISP_ABORT_NEEDED,
  1824. &ha->dpc_flags);
  1825. }
  1826. clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  1827. }
  1828. DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
  1829. ha->host_no));
  1830. }
  1831. if (test_and_clear_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags))
  1832. qla2x00_update_fcports(ha);
  1833. if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
  1834. DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
  1835. ha->host_no));
  1836. qla2x00_loop_reset(ha);
  1837. }
  1838. if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
  1839. (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
  1840. DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
  1841. ha->host_no));
  1842. qla2x00_rst_aen(ha);
  1843. clear_bit(RESET_ACTIVE, &ha->dpc_flags);
  1844. }
  1845. /* Retry each device up to login retry count */
  1846. if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  1847. !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
  1848. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1849. DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
  1850. ha->host_no));
  1851. next_loopid = 0;
  1852. list_for_each_entry(fcport, &ha->fcports, list) {
  1853. if (fcport->port_type != FCT_TARGET)
  1854. continue;
  1855. /*
  1856. * If the port is not ONLINE then try to login
  1857. * to it if we haven't run out of retries.
  1858. */
  1859. if (atomic_read(&fcport->state) != FCS_ONLINE &&
  1860. fcport->login_retry) {
  1861. fcport->login_retry--;
  1862. if (fcport->flags & FCF_FABRIC_DEVICE) {
  1863. if (fcport->flags &
  1864. FCF_TAPE_PRESENT)
  1865. ha->isp_ops.fabric_logout(
  1866. ha, fcport->loop_id,
  1867. fcport->d_id.b.domain,
  1868. fcport->d_id.b.area,
  1869. fcport->d_id.b.al_pa);
  1870. status = qla2x00_fabric_login(
  1871. ha, fcport, &next_loopid);
  1872. } else
  1873. status =
  1874. qla2x00_local_device_login(
  1875. ha, fcport->loop_id);
  1876. if (status == QLA_SUCCESS) {
  1877. fcport->old_loop_id = fcport->loop_id;
  1878. DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
  1879. ha->host_no, fcport->loop_id));
  1880. qla2x00_update_fcport(ha,
  1881. fcport);
  1882. } else if (status == 1) {
  1883. set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
  1884. /* retry the login again */
  1885. DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
  1886. ha->host_no,
  1887. fcport->login_retry, fcport->loop_id));
  1888. } else {
  1889. fcport->login_retry = 0;
  1890. }
  1891. }
  1892. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1893. break;
  1894. }
  1895. DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
  1896. ha->host_no));
  1897. }
  1898. if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
  1899. atomic_read(&ha->loop_state) != LOOP_DOWN) {
  1900. clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
  1901. DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
  1902. ha->host_no));
  1903. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1904. DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
  1905. ha->host_no));
  1906. }
  1907. if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1908. DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
  1909. ha->host_no));
  1910. if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
  1911. &ha->dpc_flags))) {
  1912. qla2x00_loop_resync(ha);
  1913. clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
  1914. }
  1915. DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
  1916. ha->host_no));
  1917. }
  1918. if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
  1919. DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
  1920. ha->host_no));
  1921. qla2x00_rescan_fcports(ha);
  1922. DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
  1923. "end.\n",
  1924. ha->host_no));
  1925. }
  1926. if (!ha->interrupts_on)
  1927. ha->isp_ops.enable_intrs(ha);
  1928. ha->dpc_active = 0;
  1929. } /* End of while(1) */
  1930. DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
  1931. /*
  1932. * Make sure that nobody tries to wake us up again.
  1933. */
  1934. ha->dpc_wait = NULL;
  1935. ha->dpc_active = 0;
  1936. complete_and_exit(&ha->dpc_exited, 0);
  1937. }
  1938. /*
  1939. * qla2x00_rst_aen
  1940. * Processes asynchronous reset.
  1941. *
  1942. * Input:
  1943. * ha = adapter block pointer.
  1944. */
  1945. static void
  1946. qla2x00_rst_aen(scsi_qla_host_t *ha)
  1947. {
  1948. if (ha->flags.online && !ha->flags.reset_active &&
  1949. !atomic_read(&ha->loop_down_timer) &&
  1950. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
  1951. do {
  1952. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  1953. /*
  1954. * Issue marker command only when we are going to start
  1955. * the I/O.
  1956. */
  1957. ha->marker_needed = 1;
  1958. } while (!atomic_read(&ha->loop_down_timer) &&
  1959. (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
  1960. }
  1961. }
  1962. static void
  1963. qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
  1964. {
  1965. struct scsi_cmnd *cmd = sp->cmd;
  1966. if (sp->flags & SRB_DMA_VALID) {
  1967. if (cmd->use_sg) {
  1968. dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
  1969. cmd->use_sg, cmd->sc_data_direction);
  1970. } else if (cmd->request_bufflen) {
  1971. dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
  1972. cmd->request_bufflen, cmd->sc_data_direction);
  1973. }
  1974. sp->flags &= ~SRB_DMA_VALID;
  1975. }
  1976. CMD_SP(cmd) = NULL;
  1977. }
  1978. void
  1979. qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
  1980. {
  1981. struct scsi_cmnd *cmd = sp->cmd;
  1982. qla2x00_sp_free_dma(ha, sp);
  1983. mempool_free(sp, ha->srb_mempool);
  1984. cmd->scsi_done(cmd);
  1985. }
  1986. /**************************************************************************
  1987. * qla2x00_timer
  1988. *
  1989. * Description:
  1990. * One second timer
  1991. *
  1992. * Context: Interrupt
  1993. ***************************************************************************/
  1994. static void
  1995. qla2x00_timer(scsi_qla_host_t *ha)
  1996. {
  1997. unsigned long cpu_flags = 0;
  1998. fc_port_t *fcport;
  1999. int start_dpc = 0;
  2000. int index;
  2001. srb_t *sp;
  2002. int t;
  2003. /*
  2004. * Ports - Port down timer.
  2005. *
  2006. * Whenever, a port is in the LOST state we start decrementing its port
  2007. * down timer every second until it reaches zero. Once it reaches zero
  2008. * the port it marked DEAD.
  2009. */
  2010. t = 0;
  2011. list_for_each_entry(fcport, &ha->fcports, list) {
  2012. if (fcport->port_type != FCT_TARGET)
  2013. continue;
  2014. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  2015. if (atomic_read(&fcport->port_down_timer) == 0)
  2016. continue;
  2017. if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
  2018. atomic_set(&fcport->state, FCS_DEVICE_DEAD);
  2019. DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
  2020. "%d remaining\n",
  2021. ha->host_no,
  2022. t, atomic_read(&fcport->port_down_timer)));
  2023. }
  2024. t++;
  2025. } /* End of for fcport */
  2026. /* Loop down handler. */
  2027. if (atomic_read(&ha->loop_down_timer) > 0 &&
  2028. !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
  2029. if (atomic_read(&ha->loop_down_timer) ==
  2030. ha->loop_down_abort_time) {
  2031. DEBUG(printk("scsi(%ld): Loop Down - aborting the "
  2032. "queues before time expire\n",
  2033. ha->host_no));
  2034. if (!IS_QLA2100(ha) && ha->link_down_timeout)
  2035. atomic_set(&ha->loop_state, LOOP_DEAD);
  2036. /* Schedule an ISP abort to return any tape commands. */
  2037. spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
  2038. for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
  2039. index++) {
  2040. fc_port_t *sfcp;
  2041. sp = ha->outstanding_cmds[index];
  2042. if (!sp)
  2043. continue;
  2044. sfcp = sp->fcport;
  2045. if (!(sfcp->flags & FCF_TAPE_PRESENT))
  2046. continue;
  2047. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2048. break;
  2049. }
  2050. spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
  2051. set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
  2052. start_dpc++;
  2053. }
  2054. /* if the loop has been down for 4 minutes, reinit adapter */
  2055. if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
  2056. DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
  2057. "restarting queues.\n",
  2058. ha->host_no));
  2059. set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
  2060. start_dpc++;
  2061. if (!(ha->device_flags & DFLG_NO_CABLE)) {
  2062. DEBUG(printk("scsi(%ld): Loop down - "
  2063. "aborting ISP.\n",
  2064. ha->host_no));
  2065. qla_printk(KERN_WARNING, ha,
  2066. "Loop down - aborting ISP.\n");
  2067. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2068. }
  2069. }
  2070. DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
  2071. ha->host_no,
  2072. atomic_read(&ha->loop_down_timer)));
  2073. }
  2074. /* Schedule the DPC routine if needed */
  2075. if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
  2076. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
  2077. test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
  2078. test_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags) ||
  2079. start_dpc ||
  2080. test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
  2081. test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
  2082. test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
  2083. ha->dpc_wait && !ha->dpc_active) {
  2084. up(ha->dpc_wait);
  2085. }
  2086. qla2x00_restart_timer(ha, WATCH_INTERVAL);
  2087. }
  2088. /* XXX(hch): crude hack to emulate a down_timeout() */
  2089. int
  2090. qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
  2091. {
  2092. const unsigned int step = 100; /* msecs */
  2093. unsigned int iterations = jiffies_to_msecs(timeout)/100;
  2094. do {
  2095. if (!down_trylock(sema))
  2096. return 0;
  2097. if (msleep_interruptible(step))
  2098. break;
  2099. } while (--iterations >= 0);
  2100. return -ETIMEDOUT;
  2101. }
  2102. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  2103. #define qla2x00_release_firmware() do { } while (0)
  2104. #define qla2x00_pci_module_init() (0)
  2105. #define qla2x00_pci_module_exit() do { } while (0)
  2106. #else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
  2107. /* Firmware interface routines. */
  2108. #define FW_BLOBS 6
  2109. #define FW_ISP21XX 0
  2110. #define FW_ISP22XX 1
  2111. #define FW_ISP2300 2
  2112. #define FW_ISP2322 3
  2113. #define FW_ISP63XX 4
  2114. #define FW_ISP24XX 5
  2115. static DECLARE_MUTEX(qla_fw_lock);
  2116. static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
  2117. { .name = "ql2100_fw.bin", .segs = { 0x1000, 0 }, },
  2118. { .name = "ql2200_fw.bin", .segs = { 0x1000, 0 }, },
  2119. { .name = "ql2300_fw.bin", .segs = { 0x800, 0 }, },
  2120. { .name = "ql2322_fw.bin", .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
  2121. { .name = "ql6312_fw.bin", .segs = { 0x800, 0 }, },
  2122. { .name = "ql2400_fw.bin", },
  2123. };
  2124. struct fw_blob *
  2125. qla2x00_request_firmware(scsi_qla_host_t *ha)
  2126. {
  2127. struct fw_blob *blob;
  2128. blob = NULL;
  2129. if (IS_QLA2100(ha)) {
  2130. blob = &qla_fw_blobs[FW_ISP21XX];
  2131. } else if (IS_QLA2200(ha)) {
  2132. blob = &qla_fw_blobs[FW_ISP22XX];
  2133. } else if (IS_QLA2300(ha) || IS_QLA2312(ha)) {
  2134. blob = &qla_fw_blobs[FW_ISP2300];
  2135. } else if (IS_QLA2322(ha)) {
  2136. blob = &qla_fw_blobs[FW_ISP2322];
  2137. } else if (IS_QLA6312(ha) || IS_QLA6322(ha)) {
  2138. blob = &qla_fw_blobs[FW_ISP63XX];
  2139. } else if (IS_QLA24XX(ha)) {
  2140. blob = &qla_fw_blobs[FW_ISP24XX];
  2141. }
  2142. down(&qla_fw_lock);
  2143. if (blob->fw)
  2144. goto out;
  2145. if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
  2146. DEBUG2(printk("scsi(%ld): Failed to load firmware image "
  2147. "(%s).\n", ha->host_no, blob->name));
  2148. blob->fw = NULL;
  2149. blob = NULL;
  2150. goto out;
  2151. }
  2152. out:
  2153. up(&qla_fw_lock);
  2154. return blob;
  2155. }
  2156. static void
  2157. qla2x00_release_firmware(void)
  2158. {
  2159. int idx;
  2160. down(&qla_fw_lock);
  2161. for (idx = 0; idx < FW_BLOBS; idx++)
  2162. if (qla_fw_blobs[idx].fw)
  2163. release_firmware(qla_fw_blobs[idx].fw);
  2164. up(&qla_fw_lock);
  2165. }
  2166. static struct qla_board_info qla_board_tbl = {
  2167. .drv_name = "qla2xxx",
  2168. };
  2169. static struct pci_device_id qla2xxx_pci_tbl[] = {
  2170. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100,
  2171. PCI_ANY_ID, PCI_ANY_ID, },
  2172. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200,
  2173. PCI_ANY_ID, PCI_ANY_ID, },
  2174. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300,
  2175. PCI_ANY_ID, PCI_ANY_ID, },
  2176. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312,
  2177. PCI_ANY_ID, PCI_ANY_ID, },
  2178. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322,
  2179. PCI_ANY_ID, PCI_ANY_ID, },
  2180. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312,
  2181. PCI_ANY_ID, PCI_ANY_ID, },
  2182. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322,
  2183. PCI_ANY_ID, PCI_ANY_ID, },
  2184. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422,
  2185. PCI_ANY_ID, PCI_ANY_ID, },
  2186. { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432,
  2187. PCI_ANY_ID, PCI_ANY_ID, },
  2188. { 0 },
  2189. };
  2190. MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
  2191. static int __devinit
  2192. qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2193. {
  2194. return qla2x00_probe_one(pdev, &qla_board_tbl);
  2195. }
  2196. static void __devexit
  2197. qla2xxx_remove_one(struct pci_dev *pdev)
  2198. {
  2199. qla2x00_remove_one(pdev);
  2200. }
  2201. static struct pci_driver qla2xxx_pci_driver = {
  2202. .name = "qla2xxx",
  2203. .driver = {
  2204. .owner = THIS_MODULE,
  2205. },
  2206. .id_table = qla2xxx_pci_tbl,
  2207. .probe = qla2xxx_probe_one,
  2208. .remove = __devexit_p(qla2xxx_remove_one),
  2209. };
  2210. static inline int
  2211. qla2x00_pci_module_init(void)
  2212. {
  2213. return pci_module_init(&qla2xxx_pci_driver);
  2214. }
  2215. static inline void
  2216. qla2x00_pci_module_exit(void)
  2217. {
  2218. pci_unregister_driver(&qla2xxx_pci_driver);
  2219. }
  2220. #endif
  2221. /**
  2222. * qla2x00_module_init - Module initialization.
  2223. **/
  2224. static int __init
  2225. qla2x00_module_init(void)
  2226. {
  2227. int ret = 0;
  2228. /* Allocate cache for SRBs. */
  2229. srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
  2230. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2231. if (srb_cachep == NULL) {
  2232. printk(KERN_ERR
  2233. "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
  2234. return -ENOMEM;
  2235. }
  2236. /* Derive version string. */
  2237. strcpy(qla2x00_version_str, QLA2XXX_VERSION);
  2238. #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
  2239. strcat(qla2x00_version_str, "-fw");
  2240. #endif
  2241. #if DEBUG_QLA2100
  2242. strcat(qla2x00_version_str, "-debug");
  2243. #endif
  2244. qla2xxx_transport_template =
  2245. fc_attach_transport(&qla2xxx_transport_functions);
  2246. if (!qla2xxx_transport_template)
  2247. return -ENODEV;
  2248. printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
  2249. ret = qla2x00_pci_module_init();
  2250. if (ret) {
  2251. kmem_cache_destroy(srb_cachep);
  2252. fc_release_transport(qla2xxx_transport_template);
  2253. }
  2254. return ret;
  2255. }
  2256. /**
  2257. * qla2x00_module_exit - Module cleanup.
  2258. **/
  2259. static void __exit
  2260. qla2x00_module_exit(void)
  2261. {
  2262. qla2x00_pci_module_exit();
  2263. qla2x00_release_firmware();
  2264. kmem_cache_destroy(srb_cachep);
  2265. fc_release_transport(qla2xxx_transport_template);
  2266. }
  2267. module_init(qla2x00_module_init);
  2268. module_exit(qla2x00_module_exit);
  2269. MODULE_AUTHOR("QLogic Corporation");
  2270. MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
  2271. MODULE_LICENSE("GPL");
  2272. MODULE_VERSION(QLA2XXX_VERSION);