ql4_os.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2006 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/slab.h>
  9. #include <scsi/scsi_tcq.h>
  10. #include <scsi/scsicam.h>
  11. #include "ql4_def.h"
  12. #include "ql4_version.h"
  13. #include "ql4_glbl.h"
  14. #include "ql4_dbg.h"
  15. #include "ql4_inline.h"
  16. /*
  17. * Driver version
  18. */
  19. static char qla4xxx_version_str[40];
  20. /*
  21. * SRB allocation cache
  22. */
  23. static struct kmem_cache *srb_cachep;
  24. /*
  25. * Module parameter information and variables
  26. */
  27. int ql4xdiscoverywait = 60;
  28. module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
  30. int ql4xdontresethba = 0;
  31. module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(ql4xdontresethba,
  33. "Don't reset the HBA for driver recovery \n"
  34. " 0 - It will reset HBA (Default)\n"
  35. " 1 - It will NOT reset HBA");
  36. int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
  37. module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
  38. MODULE_PARM_DESC(ql4xextended_error_logging,
  39. "Option to enable extended error logging, "
  40. "Default is 0 - no logging, 1 - debug logging");
  41. int ql4xenablemsix = 1;
  42. module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  43. MODULE_PARM_DESC(ql4xenablemsix,
  44. "Set to enable MSI or MSI-X interrupt mechanism.\n"
  45. " 0 = enable INTx interrupt mechanism.\n"
  46. " 1 = enable MSI-X interrupt mechanism (Default).\n"
  47. " 2 = enable MSI interrupt mechanism.");
  48. #define QL4_DEF_QDEPTH 32
  49. /*
  50. * SCSI host template entry points
  51. */
  52. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
  53. /*
  54. * iSCSI template entry points
  55. */
  56. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  57. enum iscsi_tgt_dscvr type, uint32_t enable,
  58. struct sockaddr *dst_addr);
  59. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  60. enum iscsi_param param, char *buf);
  61. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  62. enum iscsi_param param, char *buf);
  63. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  64. enum iscsi_host_param param, char *buf);
  65. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
  66. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
  67. /*
  68. * SCSI host template entry points
  69. */
  70. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  71. void (*done) (struct scsi_cmnd *));
  72. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
  73. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
  74. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
  75. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  76. static int qla4xxx_slave_alloc(struct scsi_device *device);
  77. static int qla4xxx_slave_configure(struct scsi_device *device);
  78. static void qla4xxx_slave_destroy(struct scsi_device *sdev);
  79. static void qla4xxx_scan_start(struct Scsi_Host *shost);
  80. static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
  81. QLA82XX_LEGACY_INTR_CONFIG;
  82. static struct scsi_host_template qla4xxx_driver_template = {
  83. .module = THIS_MODULE,
  84. .name = DRIVER_NAME,
  85. .proc_name = DRIVER_NAME,
  86. .queuecommand = qla4xxx_queuecommand,
  87. .eh_abort_handler = qla4xxx_eh_abort,
  88. .eh_device_reset_handler = qla4xxx_eh_device_reset,
  89. .eh_target_reset_handler = qla4xxx_eh_target_reset,
  90. .eh_host_reset_handler = qla4xxx_eh_host_reset,
  91. .eh_timed_out = qla4xxx_eh_cmd_timed_out,
  92. .slave_configure = qla4xxx_slave_configure,
  93. .slave_alloc = qla4xxx_slave_alloc,
  94. .slave_destroy = qla4xxx_slave_destroy,
  95. .scan_finished = iscsi_scan_finished,
  96. .scan_start = qla4xxx_scan_start,
  97. .this_id = -1,
  98. .cmd_per_lun = 3,
  99. .use_clustering = ENABLE_CLUSTERING,
  100. .sg_tablesize = SG_ALL,
  101. .max_sectors = 0xFFFF,
  102. };
  103. static struct iscsi_transport qla4xxx_iscsi_transport = {
  104. .owner = THIS_MODULE,
  105. .name = DRIVER_NAME,
  106. .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
  107. CAP_DATA_PATH_OFFLOAD,
  108. .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
  109. ISCSI_TARGET_NAME | ISCSI_TPGT |
  110. ISCSI_TARGET_ALIAS,
  111. .host_param_mask = ISCSI_HOST_HWADDRESS |
  112. ISCSI_HOST_IPADDRESS |
  113. ISCSI_HOST_INITIATOR_NAME,
  114. .tgt_dscvr = qla4xxx_tgt_dscvr,
  115. .get_conn_param = qla4xxx_conn_get_param,
  116. .get_session_param = qla4xxx_sess_get_param,
  117. .get_host_param = qla4xxx_host_get_param,
  118. .session_recovery_timedout = qla4xxx_recovery_timedout,
  119. };
  120. static struct scsi_transport_template *qla4xxx_scsi_transport;
  121. static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  122. {
  123. struct iscsi_cls_session *session;
  124. struct ddb_entry *ddb_entry;
  125. session = starget_to_session(scsi_target(sc->device));
  126. ddb_entry = session->dd_data;
  127. /* if we are not logged in then the LLD is going to clean up the cmd */
  128. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  129. return BLK_EH_RESET_TIMER;
  130. else
  131. return BLK_EH_NOT_HANDLED;
  132. }
  133. static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
  134. {
  135. struct ddb_entry *ddb_entry = session->dd_data;
  136. struct scsi_qla_host *ha = ddb_entry->ha;
  137. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  138. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  139. DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
  140. "of (%d) secs exhausted, marking device DEAD.\n",
  141. ha->host_no, __func__, ddb_entry->fw_ddb_index,
  142. QL4_SESS_RECOVERY_TMO));
  143. qla4xxx_wake_dpc(ha);
  144. }
  145. }
  146. static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  147. enum iscsi_host_param param, char *buf)
  148. {
  149. struct scsi_qla_host *ha = to_qla_host(shost);
  150. int len;
  151. switch (param) {
  152. case ISCSI_HOST_PARAM_HWADDRESS:
  153. len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
  154. break;
  155. case ISCSI_HOST_PARAM_IPADDRESS:
  156. len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
  157. ha->ip_address[1], ha->ip_address[2],
  158. ha->ip_address[3]);
  159. break;
  160. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  161. len = sprintf(buf, "%s\n", ha->name_string);
  162. break;
  163. default:
  164. return -ENOSYS;
  165. }
  166. return len;
  167. }
  168. static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
  169. enum iscsi_param param, char *buf)
  170. {
  171. struct ddb_entry *ddb_entry = sess->dd_data;
  172. int len;
  173. switch (param) {
  174. case ISCSI_PARAM_TARGET_NAME:
  175. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  176. ddb_entry->iscsi_name);
  177. break;
  178. case ISCSI_PARAM_TPGT:
  179. len = sprintf(buf, "%u\n", ddb_entry->tpgt);
  180. break;
  181. case ISCSI_PARAM_TARGET_ALIAS:
  182. len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
  183. ddb_entry->iscsi_alias);
  184. break;
  185. default:
  186. return -ENOSYS;
  187. }
  188. return len;
  189. }
  190. static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  191. enum iscsi_param param, char *buf)
  192. {
  193. struct iscsi_cls_session *session;
  194. struct ddb_entry *ddb_entry;
  195. int len;
  196. session = iscsi_dev_to_session(conn->dev.parent);
  197. ddb_entry = session->dd_data;
  198. switch (param) {
  199. case ISCSI_PARAM_CONN_PORT:
  200. len = sprintf(buf, "%hu\n", ddb_entry->port);
  201. break;
  202. case ISCSI_PARAM_CONN_ADDRESS:
  203. /* TODO: what are the ipv6 bits */
  204. len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
  205. break;
  206. default:
  207. return -ENOSYS;
  208. }
  209. return len;
  210. }
  211. static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
  212. enum iscsi_tgt_dscvr type, uint32_t enable,
  213. struct sockaddr *dst_addr)
  214. {
  215. struct scsi_qla_host *ha;
  216. struct sockaddr_in *addr;
  217. struct sockaddr_in6 *addr6;
  218. int ret = 0;
  219. ha = (struct scsi_qla_host *) shost->hostdata;
  220. switch (type) {
  221. case ISCSI_TGT_DSCVR_SEND_TARGETS:
  222. if (dst_addr->sa_family == AF_INET) {
  223. addr = (struct sockaddr_in *)dst_addr;
  224. if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
  225. addr->sin_port) != QLA_SUCCESS)
  226. ret = -EIO;
  227. } else if (dst_addr->sa_family == AF_INET6) {
  228. /*
  229. * TODO: fix qla4xxx_send_tgts
  230. */
  231. addr6 = (struct sockaddr_in6 *)dst_addr;
  232. if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
  233. addr6->sin6_port) != QLA_SUCCESS)
  234. ret = -EIO;
  235. } else
  236. ret = -ENOSYS;
  237. break;
  238. default:
  239. ret = -ENOSYS;
  240. }
  241. return ret;
  242. }
  243. void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
  244. {
  245. if (!ddb_entry->sess)
  246. return;
  247. if (ddb_entry->conn) {
  248. atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
  249. iscsi_remove_session(ddb_entry->sess);
  250. }
  251. iscsi_free_session(ddb_entry->sess);
  252. }
  253. int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
  254. {
  255. int err;
  256. ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
  257. err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
  258. if (err) {
  259. DEBUG2(printk(KERN_ERR "Could not add session.\n"));
  260. return err;
  261. }
  262. ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
  263. if (!ddb_entry->conn) {
  264. iscsi_remove_session(ddb_entry->sess);
  265. DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
  266. return -ENOMEM;
  267. }
  268. /* finally ready to go */
  269. iscsi_unblock_session(ddb_entry->sess);
  270. return 0;
  271. }
  272. struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
  273. {
  274. struct ddb_entry *ddb_entry;
  275. struct iscsi_cls_session *sess;
  276. sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
  277. sizeof(struct ddb_entry));
  278. if (!sess)
  279. return NULL;
  280. ddb_entry = sess->dd_data;
  281. memset(ddb_entry, 0, sizeof(*ddb_entry));
  282. ddb_entry->ha = ha;
  283. ddb_entry->sess = sess;
  284. return ddb_entry;
  285. }
  286. static void qla4xxx_scan_start(struct Scsi_Host *shost)
  287. {
  288. struct scsi_qla_host *ha = shost_priv(shost);
  289. struct ddb_entry *ddb_entry, *ddbtemp;
  290. /* finish setup of sessions that were already setup in firmware */
  291. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  292. if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
  293. qla4xxx_add_sess(ddb_entry);
  294. }
  295. }
  296. /*
  297. * Timer routines
  298. */
  299. static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  300. unsigned long interval)
  301. {
  302. DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
  303. __func__, ha->host->host_no));
  304. init_timer(&ha->timer);
  305. ha->timer.expires = jiffies + interval * HZ;
  306. ha->timer.data = (unsigned long)ha;
  307. ha->timer.function = (void (*)(unsigned long))func;
  308. add_timer(&ha->timer);
  309. ha->timer_active = 1;
  310. }
  311. static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  312. {
  313. del_timer_sync(&ha->timer);
  314. ha->timer_active = 0;
  315. }
  316. /***
  317. * qla4xxx_mark_device_missing - mark a device as missing.
  318. * @ha: Pointer to host adapter structure.
  319. * @ddb_entry: Pointer to device database entry
  320. *
  321. * This routine marks a device missing and close connection.
  322. **/
  323. void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
  324. struct ddb_entry *ddb_entry)
  325. {
  326. if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
  327. atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
  328. DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
  329. ha->host_no, ddb_entry->fw_ddb_index));
  330. } else
  331. DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
  332. ddb_entry->fw_ddb_index))
  333. iscsi_block_session(ddb_entry->sess);
  334. iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
  335. }
  336. /**
  337. * qla4xxx_mark_all_devices_missing - mark all devices as missing.
  338. * @ha: Pointer to host adapter structure.
  339. *
  340. * This routine marks a device missing and resets the relogin retry count.
  341. **/
  342. void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  343. {
  344. struct ddb_entry *ddb_entry, *ddbtemp;
  345. list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
  346. qla4xxx_mark_device_missing(ha, ddb_entry);
  347. }
  348. }
  349. static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  350. struct ddb_entry *ddb_entry,
  351. struct scsi_cmnd *cmd,
  352. void (*done)(struct scsi_cmnd *))
  353. {
  354. struct srb *srb;
  355. srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  356. if (!srb)
  357. return srb;
  358. kref_init(&srb->srb_ref);
  359. srb->ha = ha;
  360. srb->ddb = ddb_entry;
  361. srb->cmd = cmd;
  362. srb->flags = 0;
  363. CMD_SP(cmd) = (void *)srb;
  364. cmd->scsi_done = done;
  365. return srb;
  366. }
  367. static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  368. {
  369. struct scsi_cmnd *cmd = srb->cmd;
  370. if (srb->flags & SRB_DMA_VALID) {
  371. scsi_dma_unmap(cmd);
  372. srb->flags &= ~SRB_DMA_VALID;
  373. }
  374. CMD_SP(cmd) = NULL;
  375. }
  376. void qla4xxx_srb_compl(struct kref *ref)
  377. {
  378. struct srb *srb = container_of(ref, struct srb, srb_ref);
  379. struct scsi_cmnd *cmd = srb->cmd;
  380. struct scsi_qla_host *ha = srb->ha;
  381. qla4xxx_srb_free_dma(ha, srb);
  382. mempool_free(srb, ha->srb_mempool);
  383. cmd->scsi_done(cmd);
  384. }
  385. /**
  386. * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
  387. * @cmd: Pointer to Linux's SCSI command structure
  388. * @done_fn: Function that the driver calls to notify the SCSI mid-layer
  389. * that the command has been processed.
  390. *
  391. * Remarks:
  392. * This routine is invoked by Linux to send a SCSI command to the driver.
  393. * The mid-level driver tries to ensure that queuecommand never gets
  394. * invoked concurrently with itself or the interrupt handler (although
  395. * the interrupt handler may call this routine as part of request-
  396. * completion handling). Unfortunely, it sometimes calls the scheduler
  397. * in interrupt context which is a big NO! NO!.
  398. **/
  399. static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
  400. void (*done)(struct scsi_cmnd *))
  401. {
  402. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  403. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  404. struct iscsi_cls_session *sess = ddb_entry->sess;
  405. struct srb *srb;
  406. int rval;
  407. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  408. if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  409. cmd->result = DID_NO_CONNECT << 16;
  410. else
  411. cmd->result = DID_REQUEUE << 16;
  412. goto qc_fail_command;
  413. }
  414. if (!sess) {
  415. cmd->result = DID_IMM_RETRY << 16;
  416. goto qc_fail_command;
  417. }
  418. rval = iscsi_session_chkready(sess);
  419. if (rval) {
  420. cmd->result = rval;
  421. goto qc_fail_command;
  422. }
  423. if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  424. if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
  425. cmd->result = DID_NO_CONNECT << 16;
  426. goto qc_fail_command;
  427. }
  428. return SCSI_MLQUEUE_TARGET_BUSY;
  429. }
  430. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  431. test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  432. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  433. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  434. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  435. !test_bit(AF_ONLINE, &ha->flags) ||
  436. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
  437. goto qc_host_busy;
  438. spin_unlock_irq(ha->host->host_lock);
  439. srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
  440. if (!srb)
  441. goto qc_host_busy_lock;
  442. rval = qla4xxx_send_command_to_isp(ha, srb);
  443. if (rval != QLA_SUCCESS)
  444. goto qc_host_busy_free_sp;
  445. spin_lock_irq(ha->host->host_lock);
  446. return 0;
  447. qc_host_busy_free_sp:
  448. qla4xxx_srb_free_dma(ha, srb);
  449. mempool_free(srb, ha->srb_mempool);
  450. qc_host_busy_lock:
  451. spin_lock_irq(ha->host->host_lock);
  452. qc_host_busy:
  453. return SCSI_MLQUEUE_HOST_BUSY;
  454. qc_fail_command:
  455. done(cmd);
  456. return 0;
  457. }
  458. /**
  459. * qla4xxx_mem_free - frees memory allocated to adapter
  460. * @ha: Pointer to host adapter structure.
  461. *
  462. * Frees memory previously allocated by qla4xxx_mem_alloc
  463. **/
  464. static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  465. {
  466. if (ha->queues)
  467. dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  468. ha->queues_dma);
  469. ha->queues_len = 0;
  470. ha->queues = NULL;
  471. ha->queues_dma = 0;
  472. ha->request_ring = NULL;
  473. ha->request_dma = 0;
  474. ha->response_ring = NULL;
  475. ha->response_dma = 0;
  476. ha->shadow_regs = NULL;
  477. ha->shadow_regs_dma = 0;
  478. /* Free srb pool. */
  479. if (ha->srb_mempool)
  480. mempool_destroy(ha->srb_mempool);
  481. ha->srb_mempool = NULL;
  482. /* release io space registers */
  483. if (is_qla8022(ha)) {
  484. if (ha->nx_pcibase)
  485. iounmap(
  486. (struct device_reg_82xx __iomem *)ha->nx_pcibase);
  487. if (ha->nx_db_wr_ptr)
  488. iounmap(
  489. (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
  490. } else if (ha->reg)
  491. iounmap(ha->reg);
  492. pci_release_regions(ha->pdev);
  493. }
  494. /**
  495. * qla4xxx_mem_alloc - allocates memory for use by adapter.
  496. * @ha: Pointer to host adapter structure
  497. *
  498. * Allocates DMA memory for request and response queues. Also allocates memory
  499. * for srbs.
  500. **/
  501. static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  502. {
  503. unsigned long align;
  504. /* Allocate contiguous block of DMA memory for queues. */
  505. ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  506. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  507. sizeof(struct shadow_regs) +
  508. MEM_ALIGN_VALUE +
  509. (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  510. ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  511. &ha->queues_dma, GFP_KERNEL);
  512. if (ha->queues == NULL) {
  513. ql4_printk(KERN_WARNING, ha,
  514. "Memory Allocation failed - queues.\n");
  515. goto mem_alloc_error_exit;
  516. }
  517. memset(ha->queues, 0, ha->queues_len);
  518. /*
  519. * As per RISC alignment requirements -- the bus-address must be a
  520. * multiple of the request-ring size (in bytes).
  521. */
  522. align = 0;
  523. if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  524. align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  525. (MEM_ALIGN_VALUE - 1));
  526. /* Update request and response queue pointers. */
  527. ha->request_dma = ha->queues_dma + align;
  528. ha->request_ring = (struct queue_entry *) (ha->queues + align);
  529. ha->response_dma = ha->queues_dma + align +
  530. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  531. ha->response_ring = (struct queue_entry *) (ha->queues + align +
  532. (REQUEST_QUEUE_DEPTH *
  533. QUEUE_SIZE));
  534. ha->shadow_regs_dma = ha->queues_dma + align +
  535. (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  536. (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  537. ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  538. (REQUEST_QUEUE_DEPTH *
  539. QUEUE_SIZE) +
  540. (RESPONSE_QUEUE_DEPTH *
  541. QUEUE_SIZE));
  542. /* Allocate memory for srb pool. */
  543. ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  544. mempool_free_slab, srb_cachep);
  545. if (ha->srb_mempool == NULL) {
  546. ql4_printk(KERN_WARNING, ha,
  547. "Memory Allocation failed - SRB Pool.\n");
  548. goto mem_alloc_error_exit;
  549. }
  550. return QLA_SUCCESS;
  551. mem_alloc_error_exit:
  552. qla4xxx_mem_free(ha);
  553. return QLA_ERROR;
  554. }
  555. /**
  556. * qla4_8xxx_check_fw_alive - Check firmware health
  557. * @ha: Pointer to host adapter structure.
  558. *
  559. * Context: Interrupt
  560. **/
  561. static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
  562. {
  563. uint32_t fw_heartbeat_counter, halt_status;
  564. fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  565. /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  566. if (fw_heartbeat_counter == 0xffffffff) {
  567. DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  568. "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
  569. ha->host_no, __func__));
  570. return;
  571. }
  572. if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  573. ha->seconds_since_last_heartbeat++;
  574. /* FW not alive after 2 seconds */
  575. if (ha->seconds_since_last_heartbeat == 2) {
  576. ha->seconds_since_last_heartbeat = 0;
  577. halt_status = qla4_8xxx_rd_32(ha,
  578. QLA82XX_PEG_HALT_STATUS1);
  579. /* Since we cannot change dev_state in interrupt
  580. * context, set appropriate DPC flag then wakeup
  581. * DPC */
  582. if (halt_status & HALT_STATUS_UNRECOVERABLE)
  583. set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
  584. else {
  585. printk("scsi%ld: %s: detect abort needed!\n",
  586. ha->host_no, __func__);
  587. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  588. }
  589. qla4xxx_wake_dpc(ha);
  590. qla4xxx_mailbox_premature_completion(ha);
  591. }
  592. }
  593. ha->fw_heartbeat_counter = fw_heartbeat_counter;
  594. }
  595. /**
  596. * qla4_8xxx_watchdog - Poll dev state
  597. * @ha: Pointer to host adapter structure.
  598. *
  599. * Context: Interrupt
  600. **/
  601. void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  602. {
  603. uint32_t dev_state;
  604. dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  605. /* don't poll if reset is going on */
  606. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
  607. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  608. !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  609. printk("scsi%ld: %s: HW State: NEED RESET!\n",
  610. ha->host_no, __func__);
  611. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  612. qla4xxx_wake_dpc(ha);
  613. qla4xxx_mailbox_premature_completion(ha);
  614. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  615. !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  616. printk("scsi%ld: %s: HW State: NEED QUIES!\n",
  617. ha->host_no, __func__);
  618. set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  619. qla4xxx_wake_dpc(ha);
  620. } else {
  621. /* Check firmware health */
  622. qla4_8xxx_check_fw_alive(ha);
  623. }
  624. }
  625. }
  626. /**
  627. * qla4xxx_timer - checks every second for work to do.
  628. * @ha: Pointer to host adapter structure.
  629. **/
  630. static void qla4xxx_timer(struct scsi_qla_host *ha)
  631. {
  632. struct ddb_entry *ddb_entry, *dtemp;
  633. int start_dpc = 0;
  634. uint16_t w;
  635. /* If we are in the middle of AER/EEH processing
  636. * skip any processing and reschedule the timer
  637. */
  638. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  639. mod_timer(&ha->timer, jiffies + HZ);
  640. return;
  641. }
  642. /* Hardware read to trigger an EEH error during mailbox waits. */
  643. if (!pci_channel_offline(ha->pdev))
  644. pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
  645. if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
  646. DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
  647. __func__));
  648. return;
  649. }
  650. if (is_qla8022(ha)) {
  651. qla4_8xxx_watchdog(ha);
  652. }
  653. /* Search for relogin's to time-out and port down retry. */
  654. list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
  655. /* Count down time between sending relogins */
  656. if (adapter_up(ha) &&
  657. !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  658. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
  659. if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  660. INVALID_ENTRY) {
  661. if (atomic_read(&ddb_entry->retry_relogin_timer)
  662. == 0) {
  663. atomic_set(&ddb_entry->
  664. retry_relogin_timer,
  665. INVALID_ENTRY);
  666. set_bit(DPC_RELOGIN_DEVICE,
  667. &ha->dpc_flags);
  668. set_bit(DF_RELOGIN, &ddb_entry->flags);
  669. DEBUG2(printk("scsi%ld: %s: ddb [%d]"
  670. " login device\n",
  671. ha->host_no, __func__,
  672. ddb_entry->fw_ddb_index));
  673. } else
  674. atomic_dec(&ddb_entry->
  675. retry_relogin_timer);
  676. }
  677. }
  678. /* Wait for relogin to timeout */
  679. if (atomic_read(&ddb_entry->relogin_timer) &&
  680. (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  681. /*
  682. * If the relogin times out and the device is
  683. * still NOT ONLINE then try and relogin again.
  684. */
  685. if (atomic_read(&ddb_entry->state) !=
  686. DDB_STATE_ONLINE &&
  687. ddb_entry->fw_ddb_device_state ==
  688. DDB_DS_SESSION_FAILED) {
  689. /* Reset retry relogin timer */
  690. atomic_inc(&ddb_entry->relogin_retry_count);
  691. DEBUG2(printk("scsi%ld: ddb [%d] relogin"
  692. " timed out-retrying"
  693. " relogin (%d)\n",
  694. ha->host_no,
  695. ddb_entry->fw_ddb_index,
  696. atomic_read(&ddb_entry->
  697. relogin_retry_count))
  698. );
  699. start_dpc++;
  700. DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
  701. "initate relogin after"
  702. " %d seconds\n",
  703. ha->host_no, ddb_entry->bus,
  704. ddb_entry->target,
  705. ddb_entry->fw_ddb_index,
  706. ddb_entry->default_time2wait + 4)
  707. );
  708. atomic_set(&ddb_entry->retry_relogin_timer,
  709. ddb_entry->default_time2wait + 4);
  710. }
  711. }
  712. }
  713. if (!is_qla8022(ha)) {
  714. /* Check for heartbeat interval. */
  715. if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  716. ha->heartbeat_interval != 0) {
  717. ha->seconds_since_last_heartbeat++;
  718. if (ha->seconds_since_last_heartbeat >
  719. ha->heartbeat_interval + 2)
  720. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  721. }
  722. }
  723. /* Wakeup the dpc routine for this adapter, if needed. */
  724. if ((start_dpc ||
  725. test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  726. test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  727. test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
  728. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  729. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  730. test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
  731. test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
  732. test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  733. test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  734. test_bit(DPC_AEN, &ha->dpc_flags)) &&
  735. !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
  736. ha->dpc_thread) {
  737. DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  738. " - dpc flags = 0x%lx\n",
  739. ha->host_no, __func__, ha->dpc_flags));
  740. qla4xxx_wake_dpc(ha);
  741. }
  742. /* Reschedule timer thread to call us back in one second */
  743. mod_timer(&ha->timer, jiffies + HZ);
  744. DEBUG2(ha->seconds_since_last_intr++);
  745. }
  746. /**
  747. * qla4xxx_cmd_wait - waits for all outstanding commands to complete
  748. * @ha: Pointer to host adapter structure.
  749. *
  750. * This routine stalls the driver until all outstanding commands are returned.
  751. * Caller must release the Hardware Lock prior to calling this routine.
  752. **/
  753. static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  754. {
  755. uint32_t index = 0;
  756. unsigned long flags;
  757. struct scsi_cmnd *cmd;
  758. unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  759. DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  760. "complete\n", WAIT_CMD_TOV));
  761. while (!time_after_eq(jiffies, wtime)) {
  762. spin_lock_irqsave(&ha->hardware_lock, flags);
  763. /* Find a command that hasn't completed. */
  764. for (index = 0; index < ha->host->can_queue; index++) {
  765. cmd = scsi_host_find_tag(ha->host, index);
  766. if (cmd != NULL)
  767. break;
  768. }
  769. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  770. /* If No Commands are pending, wait is complete */
  771. if (index == ha->host->can_queue)
  772. return QLA_SUCCESS;
  773. msleep(1000);
  774. }
  775. /* If we timed out on waiting for commands to come back
  776. * return ERROR. */
  777. return QLA_ERROR;
  778. }
  779. int qla4xxx_hw_reset(struct scsi_qla_host *ha)
  780. {
  781. uint32_t ctrl_status;
  782. unsigned long flags = 0;
  783. DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
  784. if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  785. return QLA_ERROR;
  786. spin_lock_irqsave(&ha->hardware_lock, flags);
  787. /*
  788. * If the SCSI Reset Interrupt bit is set, clear it.
  789. * Otherwise, the Soft Reset won't work.
  790. */
  791. ctrl_status = readw(&ha->reg->ctrl_status);
  792. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  793. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  794. /* Issue Soft Reset */
  795. writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  796. readl(&ha->reg->ctrl_status);
  797. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  798. return QLA_SUCCESS;
  799. }
  800. /**
  801. * qla4xxx_soft_reset - performs soft reset.
  802. * @ha: Pointer to host adapter structure.
  803. **/
  804. int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  805. {
  806. uint32_t max_wait_time;
  807. unsigned long flags = 0;
  808. int status = QLA_ERROR;
  809. uint32_t ctrl_status;
  810. qla4xxx_hw_reset(ha);
  811. /* Wait until the Network Reset Intr bit is cleared */
  812. max_wait_time = RESET_INTR_TOV;
  813. do {
  814. spin_lock_irqsave(&ha->hardware_lock, flags);
  815. ctrl_status = readw(&ha->reg->ctrl_status);
  816. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  817. if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  818. break;
  819. msleep(1000);
  820. } while ((--max_wait_time));
  821. if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  822. DEBUG2(printk(KERN_WARNING
  823. "scsi%ld: Network Reset Intr not cleared by "
  824. "Network function, clearing it now!\n",
  825. ha->host_no));
  826. spin_lock_irqsave(&ha->hardware_lock, flags);
  827. writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  828. readl(&ha->reg->ctrl_status);
  829. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  830. }
  831. /* Wait until the firmware tells us the Soft Reset is done */
  832. max_wait_time = SOFT_RESET_TOV;
  833. do {
  834. spin_lock_irqsave(&ha->hardware_lock, flags);
  835. ctrl_status = readw(&ha->reg->ctrl_status);
  836. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  837. if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  838. status = QLA_SUCCESS;
  839. break;
  840. }
  841. msleep(1000);
  842. } while ((--max_wait_time));
  843. /*
  844. * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  845. * after the soft reset has taken place.
  846. */
  847. spin_lock_irqsave(&ha->hardware_lock, flags);
  848. ctrl_status = readw(&ha->reg->ctrl_status);
  849. if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  850. writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  851. readl(&ha->reg->ctrl_status);
  852. }
  853. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  854. /* If soft reset fails then most probably the bios on other
  855. * function is also enabled.
  856. * Since the initialization is sequential the other fn
  857. * wont be able to acknowledge the soft reset.
  858. * Issue a force soft reset to workaround this scenario.
  859. */
  860. if (max_wait_time == 0) {
  861. /* Issue Force Soft Reset */
  862. spin_lock_irqsave(&ha->hardware_lock, flags);
  863. writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  864. readl(&ha->reg->ctrl_status);
  865. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  866. /* Wait until the firmware tells us the Soft Reset is done */
  867. max_wait_time = SOFT_RESET_TOV;
  868. do {
  869. spin_lock_irqsave(&ha->hardware_lock, flags);
  870. ctrl_status = readw(&ha->reg->ctrl_status);
  871. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  872. if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  873. status = QLA_SUCCESS;
  874. break;
  875. }
  876. msleep(1000);
  877. } while ((--max_wait_time));
  878. }
  879. return status;
  880. }
  881. /**
  882. * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
  883. * @ha: Pointer to host adapter structure.
  884. * @res: returned scsi status
  885. *
  886. * This routine is called just prior to a HARD RESET to return all
  887. * outstanding commands back to the Operating System.
  888. * Caller should make sure that the following locks are released
  889. * before this calling routine: Hardware lock, and io_request_lock.
  890. **/
  891. static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
  892. {
  893. struct srb *srb;
  894. int i;
  895. unsigned long flags;
  896. spin_lock_irqsave(&ha->hardware_lock, flags);
  897. for (i = 0; i < ha->host->can_queue; i++) {
  898. srb = qla4xxx_del_from_active_array(ha, i);
  899. if (srb != NULL) {
  900. srb->cmd->result = res;
  901. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  902. }
  903. }
  904. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  905. }
  906. void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  907. {
  908. clear_bit(AF_ONLINE, &ha->flags);
  909. /* Disable the board */
  910. ql4_printk(KERN_INFO, ha, "Disabling the board\n");
  911. set_bit(AF_HBA_GOING_AWAY, &ha->flags);
  912. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  913. qla4xxx_mark_all_devices_missing(ha);
  914. clear_bit(AF_INIT_DONE, &ha->flags);
  915. }
  916. /**
  917. * qla4xxx_recover_adapter - recovers adapter after a fatal error
  918. * @ha: Pointer to host adapter structure.
  919. **/
  920. static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
  921. {
  922. int status = QLA_ERROR;
  923. uint8_t reset_chip = 0;
  924. /* Stall incoming I/O until we are done */
  925. scsi_block_requests(ha->host);
  926. clear_bit(AF_ONLINE, &ha->flags);
  927. DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
  928. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  929. if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  930. reset_chip = 1;
  931. /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  932. * do not reset adapter, jump to initialize_adapter */
  933. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  934. status = QLA_SUCCESS;
  935. goto recover_ha_init_adapter;
  936. }
  937. /* For the ISP-82xx adapter, issue a stop_firmware if invoked
  938. * from eh_host_reset or ioctl module */
  939. if (is_qla8022(ha) && !reset_chip &&
  940. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  941. DEBUG2(ql4_printk(KERN_INFO, ha,
  942. "scsi%ld: %s - Performing stop_firmware...\n",
  943. ha->host_no, __func__));
  944. status = ha->isp_ops->reset_firmware(ha);
  945. if (status == QLA_SUCCESS) {
  946. qla4xxx_cmd_wait(ha);
  947. ha->isp_ops->disable_intrs(ha);
  948. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  949. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  950. } else {
  951. /* If the stop_firmware fails then
  952. * reset the entire chip */
  953. reset_chip = 1;
  954. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  955. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  956. }
  957. }
  958. /* Issue full chip reset if recovering from a catastrophic error,
  959. * or if stop_firmware fails for ISP-82xx.
  960. * This is the default case for ISP-4xxx */
  961. if (!is_qla8022(ha) || reset_chip) {
  962. qla4xxx_cmd_wait(ha);
  963. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  964. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  965. DEBUG2(ql4_printk(KERN_INFO, ha,
  966. "scsi%ld: %s - Performing chip reset..\n",
  967. ha->host_no, __func__));
  968. status = ha->isp_ops->reset_chip(ha);
  969. }
  970. /* Flush any pending ddb changed AENs */
  971. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  972. recover_ha_init_adapter:
  973. /* Upon successful firmware/chip reset, re-initialize the adapter */
  974. if (status == QLA_SUCCESS) {
  975. /* For ISP-4xxx, force function 1 to always initialize
  976. * before function 3 to prevent both funcions from
  977. * stepping on top of the other */
  978. if (!is_qla8022(ha) && (ha->mac_index == 3))
  979. ssleep(6);
  980. /* NOTE: AF_ONLINE flag set upon successful completion of
  981. * qla4xxx_initialize_adapter */
  982. status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  983. }
  984. /* Retry failed adapter initialization, if necessary
  985. * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  986. * case to prevent ping-pong resets between functions */
  987. if (!test_bit(AF_ONLINE, &ha->flags) &&
  988. !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  989. /* Adapter initialization failed, see if we can retry
  990. * resetting the ha.
  991. * Since we don't want to block the DPC for too long
  992. * with multiple resets in the same thread,
  993. * utilize DPC to retry */
  994. if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  995. ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  996. DEBUG2(printk("scsi%ld: recover adapter - retrying "
  997. "(%d) more times\n", ha->host_no,
  998. ha->retry_reset_ha_cnt));
  999. set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1000. status = QLA_ERROR;
  1001. } else {
  1002. if (ha->retry_reset_ha_cnt > 0) {
  1003. /* Schedule another Reset HA--DPC will retry */
  1004. ha->retry_reset_ha_cnt--;
  1005. DEBUG2(printk("scsi%ld: recover adapter - "
  1006. "retry remaining %d\n",
  1007. ha->host_no,
  1008. ha->retry_reset_ha_cnt));
  1009. status = QLA_ERROR;
  1010. }
  1011. if (ha->retry_reset_ha_cnt == 0) {
  1012. /* Recover adapter retries have been exhausted.
  1013. * Adapter DEAD */
  1014. DEBUG2(printk("scsi%ld: recover adapter "
  1015. "failed - board disabled\n",
  1016. ha->host_no));
  1017. qla4xxx_dead_adapter_cleanup(ha);
  1018. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1019. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1020. clear_bit(DPC_RESET_HA_FW_CONTEXT,
  1021. &ha->dpc_flags);
  1022. status = QLA_ERROR;
  1023. }
  1024. }
  1025. } else {
  1026. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1027. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1028. clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  1029. }
  1030. ha->adapter_error_count++;
  1031. if (test_bit(AF_ONLINE, &ha->flags))
  1032. ha->isp_ops->enable_intrs(ha);
  1033. scsi_unblock_requests(ha->host);
  1034. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1035. DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
  1036. status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
  1037. return status;
  1038. }
  1039. void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  1040. {
  1041. if (ha->dpc_thread &&
  1042. !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
  1043. set_bit(AF_DPC_SCHEDULED, &ha->flags);
  1044. queue_work(ha->dpc_thread, &ha->dpc_work);
  1045. }
  1046. }
  1047. /**
  1048. * qla4xxx_do_dpc - dpc routine
  1049. * @data: in our case pointer to adapter structure
  1050. *
  1051. * This routine is a task that is schedule by the interrupt handler
  1052. * to perform the background processing for interrupts. We put it
  1053. * on a task queue that is consumed whenever the scheduler runs; that's
  1054. * so you can do anything (i.e. put the process to sleep etc). In fact,
  1055. * the mid-level tries to sleep when it reaches the driver threshold
  1056. * "host->can_queue". This can cause a panic if we were in our interrupt code.
  1057. **/
  1058. static void qla4xxx_do_dpc(struct work_struct *work)
  1059. {
  1060. struct scsi_qla_host *ha =
  1061. container_of(work, struct scsi_qla_host, dpc_work);
  1062. struct ddb_entry *ddb_entry, *dtemp;
  1063. int status = QLA_ERROR;
  1064. DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
  1065. "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
  1066. ha->host_no, __func__, ha->flags, ha->dpc_flags))
  1067. /* Initialization not yet finished. Don't do anything yet. */
  1068. if (!test_bit(AF_INIT_DONE, &ha->flags))
  1069. goto do_dpc_exit;
  1070. if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  1071. DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
  1072. ha->host_no, __func__, ha->flags));
  1073. goto do_dpc_exit;
  1074. }
  1075. /* HBA is in the process of being permanently disabled.
  1076. * Don't process anything */
  1077. if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
  1078. return;
  1079. if (is_qla8022(ha)) {
  1080. if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  1081. qla4_8xxx_idc_lock(ha);
  1082. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  1083. QLA82XX_DEV_FAILED);
  1084. qla4_8xxx_idc_unlock(ha);
  1085. ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
  1086. qla4_8xxx_device_state_handler(ha);
  1087. }
  1088. if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  1089. qla4_8xxx_need_qsnt_handler(ha);
  1090. }
  1091. }
  1092. if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  1093. (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  1094. test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  1095. test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  1096. if (ql4xdontresethba) {
  1097. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1098. ha->host_no, __func__));
  1099. clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  1100. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1101. clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1102. goto dpc_post_reset_ha;
  1103. }
  1104. if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  1105. test_bit(DPC_RESET_HA, &ha->dpc_flags))
  1106. qla4xxx_recover_adapter(ha);
  1107. if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  1108. uint8_t wait_time = RESET_INTR_TOV;
  1109. while ((readw(&ha->reg->ctrl_status) &
  1110. (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  1111. if (--wait_time == 0)
  1112. break;
  1113. msleep(1000);
  1114. }
  1115. if (wait_time == 0)
  1116. DEBUG2(printk("scsi%ld: %s: SR|FSR "
  1117. "bit not cleared-- resetting\n",
  1118. ha->host_no, __func__));
  1119. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1120. if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  1121. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1122. status = qla4xxx_recover_adapter(ha);
  1123. }
  1124. clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  1125. if (status == QLA_SUCCESS)
  1126. ha->isp_ops->enable_intrs(ha);
  1127. }
  1128. }
  1129. dpc_post_reset_ha:
  1130. /* ---- process AEN? --- */
  1131. if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  1132. qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  1133. /* ---- Get DHCP IP Address? --- */
  1134. if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  1135. qla4xxx_get_dhcp_ip_address(ha);
  1136. /* ---- link change? --- */
  1137. if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  1138. if (!test_bit(AF_LINK_UP, &ha->flags)) {
  1139. /* ---- link down? --- */
  1140. list_for_each_entry_safe(ddb_entry, dtemp,
  1141. &ha->ddb_list, list) {
  1142. if (atomic_read(&ddb_entry->state) ==
  1143. DDB_STATE_ONLINE)
  1144. qla4xxx_mark_device_missing(ha,
  1145. ddb_entry);
  1146. }
  1147. } else {
  1148. /* ---- link up? --- *
  1149. * F/W will auto login to all devices ONLY ONCE after
  1150. * link up during driver initialization and runtime
  1151. * fatal error recovery. Therefore, the driver must
  1152. * manually relogin to devices when recovering from
  1153. * connection failures, logouts, expired KATO, etc. */
  1154. list_for_each_entry_safe(ddb_entry, dtemp,
  1155. &ha->ddb_list, list) {
  1156. if ((atomic_read(&ddb_entry->state) ==
  1157. DDB_STATE_MISSING) ||
  1158. (atomic_read(&ddb_entry->state) ==
  1159. DDB_STATE_DEAD)) {
  1160. if (ddb_entry->fw_ddb_device_state ==
  1161. DDB_DS_SESSION_ACTIVE) {
  1162. atomic_set(&ddb_entry->state,
  1163. DDB_STATE_ONLINE);
  1164. ql4_printk(KERN_INFO, ha,
  1165. "scsi%ld: %s: ddb[%d]"
  1166. " marked ONLINE\n",
  1167. ha->host_no, __func__,
  1168. ddb_entry->fw_ddb_index);
  1169. iscsi_unblock_session(
  1170. ddb_entry->sess);
  1171. } else
  1172. qla4xxx_relogin_device(
  1173. ha, ddb_entry);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. /* ---- relogin device? --- */
  1179. if (adapter_up(ha) &&
  1180. test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  1181. list_for_each_entry_safe(ddb_entry, dtemp,
  1182. &ha->ddb_list, list) {
  1183. if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  1184. atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
  1185. qla4xxx_relogin_device(ha, ddb_entry);
  1186. /*
  1187. * If mbx cmd times out there is no point
  1188. * in continuing further.
  1189. * With large no of targets this can hang
  1190. * the system.
  1191. */
  1192. if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1193. printk(KERN_WARNING "scsi%ld: %s: "
  1194. "need to reset hba\n",
  1195. ha->host_no, __func__);
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. do_dpc_exit:
  1201. clear_bit(AF_DPC_SCHEDULED, &ha->flags);
  1202. }
  1203. /**
  1204. * qla4xxx_free_adapter - release the adapter
  1205. * @ha: pointer to adapter structure
  1206. **/
  1207. static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  1208. {
  1209. if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  1210. /* Turn-off interrupts on the card. */
  1211. ha->isp_ops->disable_intrs(ha);
  1212. }
  1213. /* Remove timer thread, if present */
  1214. if (ha->timer_active)
  1215. qla4xxx_stop_timer(ha);
  1216. /* Kill the kernel thread for this host */
  1217. if (ha->dpc_thread)
  1218. destroy_workqueue(ha->dpc_thread);
  1219. /* Put firmware in known state */
  1220. ha->isp_ops->reset_firmware(ha);
  1221. if (is_qla8022(ha)) {
  1222. qla4_8xxx_idc_lock(ha);
  1223. qla4_8xxx_clear_drv_active(ha);
  1224. qla4_8xxx_idc_unlock(ha);
  1225. }
  1226. /* Detach interrupts */
  1227. if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
  1228. qla4xxx_free_irqs(ha);
  1229. /* free extra memory */
  1230. qla4xxx_mem_free(ha);
  1231. }
  1232. int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  1233. {
  1234. int status = 0;
  1235. uint8_t revision_id;
  1236. unsigned long mem_base, mem_len, db_base, db_len;
  1237. struct pci_dev *pdev = ha->pdev;
  1238. status = pci_request_regions(pdev, DRIVER_NAME);
  1239. if (status) {
  1240. printk(KERN_WARNING
  1241. "scsi(%ld) Failed to reserve PIO regions (%s) "
  1242. "status=%d\n", ha->host_no, pci_name(pdev), status);
  1243. goto iospace_error_exit;
  1244. }
  1245. pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  1246. DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
  1247. __func__, revision_id));
  1248. ha->revision_id = revision_id;
  1249. /* remap phys address */
  1250. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  1251. mem_len = pci_resource_len(pdev, 0);
  1252. DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
  1253. __func__, mem_base, mem_len));
  1254. /* mapping of pcibase pointer */
  1255. ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  1256. if (!ha->nx_pcibase) {
  1257. printk(KERN_ERR
  1258. "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
  1259. pci_release_regions(ha->pdev);
  1260. goto iospace_error_exit;
  1261. }
  1262. /* Mapping of IO base pointer, door bell read and write pointer */
  1263. /* mapping of IO base pointer */
  1264. ha->qla4_8xxx_reg =
  1265. (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
  1266. 0xbc000 + (ha->pdev->devfn << 11));
  1267. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  1268. db_len = pci_resource_len(pdev, 4);
  1269. /* mapping of doorbell write pointer */
  1270. ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
  1271. (ha->pdev->devfn << 12), 4);
  1272. if (!ha->nx_db_wr_ptr) {
  1273. printk(KERN_ERR
  1274. "cannot remap MMIO doorbell-write (%s), aborting\n",
  1275. pci_name(pdev));
  1276. goto iospace_error_exit;
  1277. }
  1278. /* mapping of doorbell read pointer */
  1279. ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
  1280. (ha->pdev->devfn * 8);
  1281. if (!ha->nx_db_rd_ptr)
  1282. printk(KERN_ERR
  1283. "cannot remap MMIO doorbell-read (%s), aborting\n",
  1284. pci_name(pdev));
  1285. return 0;
  1286. iospace_error_exit:
  1287. return -ENOMEM;
  1288. }
  1289. /***
  1290. * qla4xxx_iospace_config - maps registers
  1291. * @ha: pointer to adapter structure
  1292. *
  1293. * This routines maps HBA's registers from the pci address space
  1294. * into the kernel virtual address space for memory mapped i/o.
  1295. **/
  1296. int qla4xxx_iospace_config(struct scsi_qla_host *ha)
  1297. {
  1298. unsigned long pio, pio_len, pio_flags;
  1299. unsigned long mmio, mmio_len, mmio_flags;
  1300. pio = pci_resource_start(ha->pdev, 0);
  1301. pio_len = pci_resource_len(ha->pdev, 0);
  1302. pio_flags = pci_resource_flags(ha->pdev, 0);
  1303. if (pio_flags & IORESOURCE_IO) {
  1304. if (pio_len < MIN_IOBASE_LEN) {
  1305. ql4_printk(KERN_WARNING, ha,
  1306. "Invalid PCI I/O region size\n");
  1307. pio = 0;
  1308. }
  1309. } else {
  1310. ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
  1311. pio = 0;
  1312. }
  1313. /* Use MMIO operations for all accesses. */
  1314. mmio = pci_resource_start(ha->pdev, 1);
  1315. mmio_len = pci_resource_len(ha->pdev, 1);
  1316. mmio_flags = pci_resource_flags(ha->pdev, 1);
  1317. if (!(mmio_flags & IORESOURCE_MEM)) {
  1318. ql4_printk(KERN_ERR, ha,
  1319. "region #0 not an MMIO resource, aborting\n");
  1320. goto iospace_error_exit;
  1321. }
  1322. if (mmio_len < MIN_IOBASE_LEN) {
  1323. ql4_printk(KERN_ERR, ha,
  1324. "Invalid PCI mem region size, aborting\n");
  1325. goto iospace_error_exit;
  1326. }
  1327. if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
  1328. ql4_printk(KERN_WARNING, ha,
  1329. "Failed to reserve PIO/MMIO regions\n");
  1330. goto iospace_error_exit;
  1331. }
  1332. ha->pio_address = pio;
  1333. ha->pio_length = pio_len;
  1334. ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  1335. if (!ha->reg) {
  1336. ql4_printk(KERN_ERR, ha,
  1337. "cannot remap MMIO, aborting\n");
  1338. goto iospace_error_exit;
  1339. }
  1340. return 0;
  1341. iospace_error_exit:
  1342. return -ENOMEM;
  1343. }
  1344. static struct isp_operations qla4xxx_isp_ops = {
  1345. .iospace_config = qla4xxx_iospace_config,
  1346. .pci_config = qla4xxx_pci_config,
  1347. .disable_intrs = qla4xxx_disable_intrs,
  1348. .enable_intrs = qla4xxx_enable_intrs,
  1349. .start_firmware = qla4xxx_start_firmware,
  1350. .intr_handler = qla4xxx_intr_handler,
  1351. .interrupt_service_routine = qla4xxx_interrupt_service_routine,
  1352. .reset_chip = qla4xxx_soft_reset,
  1353. .reset_firmware = qla4xxx_hw_reset,
  1354. .queue_iocb = qla4xxx_queue_iocb,
  1355. .complete_iocb = qla4xxx_complete_iocb,
  1356. .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
  1357. .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
  1358. .get_sys_info = qla4xxx_get_sys_info,
  1359. };
  1360. static struct isp_operations qla4_8xxx_isp_ops = {
  1361. .iospace_config = qla4_8xxx_iospace_config,
  1362. .pci_config = qla4_8xxx_pci_config,
  1363. .disable_intrs = qla4_8xxx_disable_intrs,
  1364. .enable_intrs = qla4_8xxx_enable_intrs,
  1365. .start_firmware = qla4_8xxx_load_risc,
  1366. .intr_handler = qla4_8xxx_intr_handler,
  1367. .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  1368. .reset_chip = qla4_8xxx_isp_reset,
  1369. .reset_firmware = qla4_8xxx_stop_firmware,
  1370. .queue_iocb = qla4_8xxx_queue_iocb,
  1371. .complete_iocb = qla4_8xxx_complete_iocb,
  1372. .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
  1373. .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
  1374. .get_sys_info = qla4_8xxx_get_sys_info,
  1375. };
  1376. uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1377. {
  1378. return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  1379. }
  1380. uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  1381. {
  1382. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  1383. }
  1384. uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1385. {
  1386. return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  1387. }
  1388. uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  1389. {
  1390. return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  1391. }
  1392. /**
  1393. * qla4xxx_probe_adapter - callback function to probe HBA
  1394. * @pdev: pointer to pci_dev structure
  1395. * @pci_device_id: pointer to pci_device entry
  1396. *
  1397. * This routine will probe for Qlogic 4xxx iSCSI host adapters.
  1398. * It returns zero if successful. It also initializes all data necessary for
  1399. * the driver.
  1400. **/
  1401. static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  1402. const struct pci_device_id *ent)
  1403. {
  1404. int ret = -ENODEV, status;
  1405. struct Scsi_Host *host;
  1406. struct scsi_qla_host *ha;
  1407. uint8_t init_retry_count = 0;
  1408. char buf[34];
  1409. struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
  1410. if (pci_enable_device(pdev))
  1411. return -1;
  1412. host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
  1413. if (host == NULL) {
  1414. printk(KERN_WARNING
  1415. "qla4xxx: Couldn't allocate host from scsi layer!\n");
  1416. goto probe_disable_device;
  1417. }
  1418. /* Clear our data area */
  1419. ha = (struct scsi_qla_host *) host->hostdata;
  1420. memset(ha, 0, sizeof(*ha));
  1421. /* Save the information from PCI BIOS. */
  1422. ha->pdev = pdev;
  1423. ha->host = host;
  1424. ha->host_no = host->host_no;
  1425. pci_enable_pcie_error_reporting(pdev);
  1426. /* Setup Runtime configurable options */
  1427. if (is_qla8022(ha)) {
  1428. ha->isp_ops = &qla4_8xxx_isp_ops;
  1429. rwlock_init(&ha->hw_lock);
  1430. ha->qdr_sn_window = -1;
  1431. ha->ddr_mn_window = -1;
  1432. ha->curr_window = 255;
  1433. ha->func_num = PCI_FUNC(ha->pdev->devfn);
  1434. nx_legacy_intr = &legacy_intr[ha->func_num];
  1435. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  1436. ha->nx_legacy_intr.tgt_status_reg =
  1437. nx_legacy_intr->tgt_status_reg;
  1438. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  1439. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  1440. } else {
  1441. ha->isp_ops = &qla4xxx_isp_ops;
  1442. }
  1443. /* Set EEH reset type to fundamental if required by hba */
  1444. if (is_qla8022(ha))
  1445. pdev->needs_freset = 1;
  1446. /* Configure PCI I/O space. */
  1447. ret = ha->isp_ops->iospace_config(ha);
  1448. if (ret)
  1449. goto probe_failed_ioconfig;
  1450. ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
  1451. pdev->device, pdev->irq, ha->reg);
  1452. qla4xxx_config_dma_addressing(ha);
  1453. /* Initialize lists and spinlocks. */
  1454. INIT_LIST_HEAD(&ha->ddb_list);
  1455. INIT_LIST_HEAD(&ha->free_srb_q);
  1456. mutex_init(&ha->mbox_sem);
  1457. init_completion(&ha->mbx_intr_comp);
  1458. spin_lock_init(&ha->hardware_lock);
  1459. /* Allocate dma buffers */
  1460. if (qla4xxx_mem_alloc(ha)) {
  1461. ql4_printk(KERN_WARNING, ha,
  1462. "[ERROR] Failed to allocate memory for adapter\n");
  1463. ret = -ENOMEM;
  1464. goto probe_failed;
  1465. }
  1466. if (is_qla8022(ha))
  1467. (void) qla4_8xxx_get_flash_info(ha);
  1468. /*
  1469. * Initialize the Host adapter request/response queues and
  1470. * firmware
  1471. * NOTE: interrupts enabled upon successful completion
  1472. */
  1473. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1474. while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  1475. init_retry_count++ < MAX_INIT_RETRIES) {
  1476. DEBUG2(printk("scsi: %s: retrying adapter initialization "
  1477. "(%d)\n", __func__, init_retry_count));
  1478. if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  1479. continue;
  1480. status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
  1481. }
  1482. if (!test_bit(AF_ONLINE, &ha->flags)) {
  1483. ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
  1484. ret = -ENODEV;
  1485. goto probe_failed;
  1486. }
  1487. host->cmd_per_lun = 3;
  1488. host->max_channel = 0;
  1489. host->max_lun = MAX_LUNS - 1;
  1490. host->max_id = MAX_TARGETS;
  1491. host->max_cmd_len = IOCB_MAX_CDB_LEN;
  1492. host->can_queue = MAX_SRBS ;
  1493. host->transportt = qla4xxx_scsi_transport;
  1494. ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  1495. if (ret) {
  1496. ql4_printk(KERN_WARNING, ha,
  1497. "scsi_init_shared_tag_map failed\n");
  1498. goto probe_failed;
  1499. }
  1500. /* Startup the kernel thread for this host adapter. */
  1501. DEBUG2(printk("scsi: %s: Starting kernel thread for "
  1502. "qla4xxx_dpc\n", __func__));
  1503. sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  1504. ha->dpc_thread = create_singlethread_workqueue(buf);
  1505. if (!ha->dpc_thread) {
  1506. ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
  1507. ret = -ENODEV;
  1508. goto probe_failed;
  1509. }
  1510. INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
  1511. /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  1512. * (which is called indirectly by qla4xxx_initialize_adapter),
  1513. * so that irqs will be registered after crbinit but before
  1514. * mbx_intr_enable.
  1515. */
  1516. if (!is_qla8022(ha)) {
  1517. ret = qla4xxx_request_irqs(ha);
  1518. if (ret) {
  1519. ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  1520. "interrupt %d already in use.\n", pdev->irq);
  1521. goto probe_failed;
  1522. }
  1523. }
  1524. pci_save_state(ha->pdev);
  1525. ha->isp_ops->enable_intrs(ha);
  1526. /* Start timer thread. */
  1527. qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  1528. set_bit(AF_INIT_DONE, &ha->flags);
  1529. pci_set_drvdata(pdev, ha);
  1530. ret = scsi_add_host(host, &pdev->dev);
  1531. if (ret)
  1532. goto probe_failed;
  1533. printk(KERN_INFO
  1534. " QLogic iSCSI HBA Driver version: %s\n"
  1535. " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
  1536. qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  1537. ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  1538. ha->patch_number, ha->build_number);
  1539. scsi_scan_host(host);
  1540. return 0;
  1541. probe_failed:
  1542. qla4xxx_free_adapter(ha);
  1543. probe_failed_ioconfig:
  1544. pci_disable_pcie_error_reporting(pdev);
  1545. scsi_host_put(ha->host);
  1546. probe_disable_device:
  1547. pci_disable_device(pdev);
  1548. return ret;
  1549. }
  1550. /**
  1551. * qla4xxx_remove_adapter - calback function to remove adapter.
  1552. * @pci_dev: PCI device pointer
  1553. **/
  1554. static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  1555. {
  1556. struct scsi_qla_host *ha;
  1557. ha = pci_get_drvdata(pdev);
  1558. set_bit(AF_HBA_GOING_AWAY, &ha->flags);
  1559. /* remove devs from iscsi_sessions to scsi_devices */
  1560. qla4xxx_free_ddb_list(ha);
  1561. scsi_remove_host(ha->host);
  1562. qla4xxx_free_adapter(ha);
  1563. scsi_host_put(ha->host);
  1564. pci_disable_pcie_error_reporting(pdev);
  1565. pci_disable_device(pdev);
  1566. pci_set_drvdata(pdev, NULL);
  1567. }
  1568. /**
  1569. * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
  1570. * @ha: HA context
  1571. *
  1572. * At exit, the @ha's flags.enable_64bit_addressing set to indicated
  1573. * supported addressing method.
  1574. */
  1575. static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
  1576. {
  1577. int retval;
  1578. /* Update our PCI device dma_mask for full 64 bit mask */
  1579. if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  1580. if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
  1581. dev_dbg(&ha->pdev->dev,
  1582. "Failed to set 64 bit PCI consistent mask; "
  1583. "using 32 bit.\n");
  1584. retval = pci_set_consistent_dma_mask(ha->pdev,
  1585. DMA_BIT_MASK(32));
  1586. }
  1587. } else
  1588. retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
  1589. }
  1590. static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  1591. {
  1592. struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
  1593. struct ddb_entry *ddb = sess->dd_data;
  1594. sdev->hostdata = ddb;
  1595. sdev->tagged_supported = 1;
  1596. scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
  1597. return 0;
  1598. }
  1599. static int qla4xxx_slave_configure(struct scsi_device *sdev)
  1600. {
  1601. sdev->tagged_supported = 1;
  1602. return 0;
  1603. }
  1604. static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  1605. {
  1606. scsi_deactivate_tcq(sdev, 1);
  1607. }
  1608. /**
  1609. * qla4xxx_del_from_active_array - returns an active srb
  1610. * @ha: Pointer to host adapter structure.
  1611. * @index: index into the active_array
  1612. *
  1613. * This routine removes and returns the srb at the specified index
  1614. **/
  1615. struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
  1616. uint32_t index)
  1617. {
  1618. struct srb *srb = NULL;
  1619. struct scsi_cmnd *cmd = NULL;
  1620. cmd = scsi_host_find_tag(ha->host, index);
  1621. if (!cmd)
  1622. return srb;
  1623. srb = (struct srb *)CMD_SP(cmd);
  1624. if (!srb)
  1625. return srb;
  1626. /* update counters */
  1627. if (srb->flags & SRB_DMA_VALID) {
  1628. ha->req_q_count += srb->iocb_cnt;
  1629. ha->iocb_cnt -= srb->iocb_cnt;
  1630. if (srb->cmd)
  1631. srb->cmd->host_scribble =
  1632. (unsigned char *)(unsigned long) MAX_SRBS;
  1633. }
  1634. return srb;
  1635. }
  1636. /**
  1637. * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
  1638. * @ha: Pointer to host adapter structure.
  1639. * @cmd: Scsi Command to wait on.
  1640. *
  1641. * This routine waits for the command to be returned by the Firmware
  1642. * for some max time.
  1643. **/
  1644. static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  1645. struct scsi_cmnd *cmd)
  1646. {
  1647. int done = 0;
  1648. struct srb *rp;
  1649. uint32_t max_wait_time = EH_WAIT_CMD_TOV;
  1650. int ret = SUCCESS;
  1651. /* Dont wait on command if PCI error is being handled
  1652. * by PCI AER driver
  1653. */
  1654. if (unlikely(pci_channel_offline(ha->pdev)) ||
  1655. (test_bit(AF_EEH_BUSY, &ha->flags))) {
  1656. ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
  1657. ha->host_no, __func__);
  1658. return ret;
  1659. }
  1660. do {
  1661. /* Checking to see if its returned to OS */
  1662. rp = (struct srb *) CMD_SP(cmd);
  1663. if (rp == NULL) {
  1664. done++;
  1665. break;
  1666. }
  1667. msleep(2000);
  1668. } while (max_wait_time--);
  1669. return done;
  1670. }
  1671. /**
  1672. * qla4xxx_wait_for_hba_online - waits for HBA to come online
  1673. * @ha: Pointer to host adapter structure
  1674. **/
  1675. static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  1676. {
  1677. unsigned long wait_online;
  1678. wait_online = jiffies + (30 * HZ);
  1679. while (time_before(jiffies, wait_online)) {
  1680. if (adapter_up(ha))
  1681. return QLA_SUCCESS;
  1682. else if (ha->retry_reset_ha_cnt == 0)
  1683. return QLA_ERROR;
  1684. msleep(2000);
  1685. }
  1686. return QLA_ERROR;
  1687. }
  1688. /**
  1689. * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
  1690. * @ha: pointer to HBA
  1691. * @t: target id
  1692. * @l: lun id
  1693. *
  1694. * This function waits for all outstanding commands to a lun to complete. It
  1695. * returns 0 if all pending commands are returned and 1 otherwise.
  1696. **/
  1697. static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  1698. struct scsi_target *stgt,
  1699. struct scsi_device *sdev)
  1700. {
  1701. int cnt;
  1702. int status = 0;
  1703. struct scsi_cmnd *cmd;
  1704. /*
  1705. * Waiting for all commands for the designated target or dev
  1706. * in the active array
  1707. */
  1708. for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  1709. cmd = scsi_host_find_tag(ha->host, cnt);
  1710. if (cmd && stgt == scsi_target(cmd->device) &&
  1711. (!sdev || sdev == cmd->device)) {
  1712. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1713. status++;
  1714. break;
  1715. }
  1716. }
  1717. }
  1718. return status;
  1719. }
  1720. /**
  1721. * qla4xxx_eh_abort - callback for abort task.
  1722. * @cmd: Pointer to Linux's SCSI command structure
  1723. *
  1724. * This routine is called by the Linux OS to abort the specified
  1725. * command.
  1726. **/
  1727. static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  1728. {
  1729. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1730. unsigned int id = cmd->device->id;
  1731. unsigned int lun = cmd->device->lun;
  1732. unsigned long serial = cmd->serial_number;
  1733. struct srb *srb = NULL;
  1734. int ret = SUCCESS;
  1735. int wait = 0;
  1736. ql4_printk(KERN_INFO, ha,
  1737. "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
  1738. ha->host_no, id, lun, cmd, serial);
  1739. srb = (struct srb *) CMD_SP(cmd);
  1740. if (!srb)
  1741. return SUCCESS;
  1742. kref_get(&srb->srb_ref);
  1743. if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  1744. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
  1745. ha->host_no, id, lun));
  1746. ret = FAILED;
  1747. } else {
  1748. DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
  1749. ha->host_no, id, lun));
  1750. wait = 1;
  1751. }
  1752. kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  1753. /* Wait for command to complete */
  1754. if (wait) {
  1755. if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  1756. DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
  1757. ha->host_no, id, lun));
  1758. ret = FAILED;
  1759. }
  1760. }
  1761. ql4_printk(KERN_INFO, ha,
  1762. "scsi%ld:%d:%d: Abort command - %s\n",
  1763. ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
  1764. return ret;
  1765. }
  1766. /**
  1767. * qla4xxx_eh_device_reset - callback for target reset.
  1768. * @cmd: Pointer to Linux's SCSI command structure
  1769. *
  1770. * This routine is called by the Linux OS to reset all luns on the
  1771. * specified target.
  1772. **/
  1773. static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  1774. {
  1775. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1776. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1777. int ret = FAILED, stat;
  1778. if (!ddb_entry)
  1779. return ret;
  1780. ret = iscsi_block_scsi_eh(cmd);
  1781. if (ret)
  1782. return ret;
  1783. ret = FAILED;
  1784. ql4_printk(KERN_INFO, ha,
  1785. "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
  1786. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1787. DEBUG2(printk(KERN_INFO
  1788. "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  1789. "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
  1790. cmd, jiffies, cmd->request->timeout / HZ,
  1791. ha->dpc_flags, cmd->result, cmd->allowed));
  1792. /* FIXME: wait for hba to go online */
  1793. stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  1794. if (stat != QLA_SUCCESS) {
  1795. ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
  1796. goto eh_dev_reset_done;
  1797. }
  1798. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1799. cmd->device)) {
  1800. ql4_printk(KERN_INFO, ha,
  1801. "DEVICE RESET FAILED - waiting for "
  1802. "commands.\n");
  1803. goto eh_dev_reset_done;
  1804. }
  1805. /* Send marker. */
  1806. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1807. MM_LUN_RESET) != QLA_SUCCESS)
  1808. goto eh_dev_reset_done;
  1809. ql4_printk(KERN_INFO, ha,
  1810. "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
  1811. ha->host_no, cmd->device->channel, cmd->device->id,
  1812. cmd->device->lun);
  1813. ret = SUCCESS;
  1814. eh_dev_reset_done:
  1815. return ret;
  1816. }
  1817. /**
  1818. * qla4xxx_eh_target_reset - callback for target reset.
  1819. * @cmd: Pointer to Linux's SCSI command structure
  1820. *
  1821. * This routine is called by the Linux OS to reset the target.
  1822. **/
  1823. static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  1824. {
  1825. struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  1826. struct ddb_entry *ddb_entry = cmd->device->hostdata;
  1827. int stat, ret;
  1828. if (!ddb_entry)
  1829. return FAILED;
  1830. ret = iscsi_block_scsi_eh(cmd);
  1831. if (ret)
  1832. return ret;
  1833. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1834. "WARM TARGET RESET ISSUED.\n");
  1835. DEBUG2(printk(KERN_INFO
  1836. "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  1837. "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
  1838. ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
  1839. ha->dpc_flags, cmd->result, cmd->allowed));
  1840. stat = qla4xxx_reset_target(ha, ddb_entry);
  1841. if (stat != QLA_SUCCESS) {
  1842. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1843. "WARM TARGET RESET FAILED.\n");
  1844. return FAILED;
  1845. }
  1846. if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  1847. NULL)) {
  1848. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1849. "WARM TARGET DEVICE RESET FAILED - "
  1850. "waiting for commands.\n");
  1851. return FAILED;
  1852. }
  1853. /* Send marker. */
  1854. if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  1855. MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  1856. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1857. "WARM TARGET DEVICE RESET FAILED - "
  1858. "marker iocb failed.\n");
  1859. return FAILED;
  1860. }
  1861. starget_printk(KERN_INFO, scsi_target(cmd->device),
  1862. "WARM TARGET RESET SUCCEEDED.\n");
  1863. return SUCCESS;
  1864. }
  1865. /**
  1866. * qla4xxx_eh_host_reset - kernel callback
  1867. * @cmd: Pointer to Linux's SCSI command structure
  1868. *
  1869. * This routine is invoked by the Linux kernel to perform fatal error
  1870. * recovery on the specified adapter.
  1871. **/
  1872. static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  1873. {
  1874. int return_status = FAILED;
  1875. struct scsi_qla_host *ha;
  1876. ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
  1877. if (ql4xdontresethba) {
  1878. DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
  1879. ha->host_no, __func__));
  1880. return FAILED;
  1881. }
  1882. ql4_printk(KERN_INFO, ha,
  1883. "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
  1884. cmd->device->channel, cmd->device->id, cmd->device->lun);
  1885. if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  1886. DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
  1887. "DEAD.\n", ha->host_no, cmd->device->channel,
  1888. __func__));
  1889. return FAILED;
  1890. }
  1891. if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  1892. if (is_qla8022(ha))
  1893. set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  1894. else
  1895. set_bit(DPC_RESET_HA, &ha->dpc_flags);
  1896. }
  1897. if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
  1898. return_status = SUCCESS;
  1899. ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
  1900. return_status == FAILED ? "FAILED" : "SUCCEDED");
  1901. return return_status;
  1902. }
  1903. /* PCI AER driver recovers from all correctable errors w/o
  1904. * driver intervention. For uncorrectable errors PCI AER
  1905. * driver calls the following device driver's callbacks
  1906. *
  1907. * - Fatal Errors - link_reset
  1908. * - Non-Fatal Errors - driver's pci_error_detected() which
  1909. * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
  1910. *
  1911. * PCI AER driver calls
  1912. * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
  1913. * returns RECOVERED or NEED_RESET if fw_hung
  1914. * NEED_RESET - driver's slot_reset()
  1915. * DISCONNECT - device is dead & cannot recover
  1916. * RECOVERED - driver's pci_resume()
  1917. */
  1918. static pci_ers_result_t
  1919. qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  1920. {
  1921. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1922. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
  1923. ha->host_no, __func__, state);
  1924. if (!is_aer_supported(ha))
  1925. return PCI_ERS_RESULT_NONE;
  1926. switch (state) {
  1927. case pci_channel_io_normal:
  1928. clear_bit(AF_EEH_BUSY, &ha->flags);
  1929. return PCI_ERS_RESULT_CAN_RECOVER;
  1930. case pci_channel_io_frozen:
  1931. set_bit(AF_EEH_BUSY, &ha->flags);
  1932. qla4xxx_mailbox_premature_completion(ha);
  1933. qla4xxx_free_irqs(ha);
  1934. pci_disable_device(pdev);
  1935. return PCI_ERS_RESULT_NEED_RESET;
  1936. case pci_channel_io_perm_failure:
  1937. set_bit(AF_EEH_BUSY, &ha->flags);
  1938. set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  1939. qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  1940. return PCI_ERS_RESULT_DISCONNECT;
  1941. }
  1942. return PCI_ERS_RESULT_NEED_RESET;
  1943. }
  1944. /**
  1945. * qla4xxx_pci_mmio_enabled() gets called if
  1946. * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
  1947. * and read/write to the device still works.
  1948. **/
  1949. static pci_ers_result_t
  1950. qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  1951. {
  1952. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  1953. if (!is_aer_supported(ha))
  1954. return PCI_ERS_RESULT_NONE;
  1955. if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
  1956. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
  1957. "mmio_enabled\n", ha->host_no, __func__);
  1958. return PCI_ERS_RESULT_NEED_RESET;
  1959. } else
  1960. return PCI_ERS_RESULT_RECOVERED;
  1961. }
  1962. uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
  1963. {
  1964. uint32_t rval = QLA_ERROR;
  1965. int fn;
  1966. struct pci_dev *other_pdev = NULL;
  1967. ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
  1968. set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  1969. if (test_bit(AF_ONLINE, &ha->flags)) {
  1970. clear_bit(AF_ONLINE, &ha->flags);
  1971. qla4xxx_mark_all_devices_missing(ha);
  1972. qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  1973. qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  1974. }
  1975. fn = PCI_FUNC(ha->pdev->devfn);
  1976. while (fn > 0) {
  1977. fn--;
  1978. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  1979. "func %x\n", ha->host_no, __func__, fn);
  1980. /* Get the pci device given the domain, bus,
  1981. * slot/function number */
  1982. other_pdev =
  1983. pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  1984. ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  1985. fn));
  1986. if (!other_pdev)
  1987. continue;
  1988. if (atomic_read(&other_pdev->enable_cnt)) {
  1989. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  1990. "func in enabled state%x\n", ha->host_no,
  1991. __func__, fn);
  1992. pci_dev_put(other_pdev);
  1993. break;
  1994. }
  1995. pci_dev_put(other_pdev);
  1996. }
  1997. /* The first function on the card, the reset owner will
  1998. * start & initialize the firmware. The other functions
  1999. * on the card will reset the firmware context
  2000. */
  2001. if (!fn) {
  2002. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  2003. "0x%x is the owner\n", ha->host_no, __func__,
  2004. ha->pdev->devfn);
  2005. qla4_8xxx_idc_lock(ha);
  2006. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2007. QLA82XX_DEV_COLD);
  2008. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  2009. QLA82XX_IDC_VERSION);
  2010. qla4_8xxx_idc_unlock(ha);
  2011. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2012. rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
  2013. qla4_8xxx_idc_lock(ha);
  2014. if (rval != QLA_SUCCESS) {
  2015. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2016. "FAILED\n", ha->host_no, __func__);
  2017. qla4_8xxx_clear_drv_active(ha);
  2018. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2019. QLA82XX_DEV_FAILED);
  2020. } else {
  2021. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  2022. "READY\n", ha->host_no, __func__);
  2023. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2024. QLA82XX_DEV_READY);
  2025. /* Clear driver state register */
  2026. qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  2027. qla4_8xxx_set_drv_active(ha);
  2028. ha->isp_ops->enable_intrs(ha);
  2029. }
  2030. qla4_8xxx_idc_unlock(ha);
  2031. } else {
  2032. ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  2033. "the reset owner\n", ha->host_no, __func__,
  2034. ha->pdev->devfn);
  2035. if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  2036. QLA82XX_DEV_READY)) {
  2037. clear_bit(AF_FW_RECOVERY, &ha->flags);
  2038. rval = qla4xxx_initialize_adapter(ha,
  2039. PRESERVE_DDB_LIST);
  2040. if (rval == QLA_SUCCESS)
  2041. ha->isp_ops->enable_intrs(ha);
  2042. qla4_8xxx_idc_lock(ha);
  2043. qla4_8xxx_set_drv_active(ha);
  2044. qla4_8xxx_idc_unlock(ha);
  2045. }
  2046. }
  2047. clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  2048. return rval;
  2049. }
  2050. static pci_ers_result_t
  2051. qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  2052. {
  2053. pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  2054. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2055. int rc;
  2056. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
  2057. ha->host_no, __func__);
  2058. if (!is_aer_supported(ha))
  2059. return PCI_ERS_RESULT_NONE;
  2060. /* Restore the saved state of PCIe device -
  2061. * BAR registers, PCI Config space, PCIX, MSI,
  2062. * IOV states
  2063. */
  2064. pci_restore_state(pdev);
  2065. /* pci_restore_state() clears the saved_state flag of the device
  2066. * save restored state which resets saved_state flag
  2067. */
  2068. pci_save_state(pdev);
  2069. /* Initialize device or resume if in suspended state */
  2070. rc = pci_enable_device(pdev);
  2071. if (rc) {
  2072. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
  2073. "device after reset\n", ha->host_no, __func__);
  2074. goto exit_slot_reset;
  2075. }
  2076. ret = qla4xxx_request_irqs(ha);
  2077. if (ret) {
  2078. ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
  2079. " already in use.\n", pdev->irq);
  2080. goto exit_slot_reset;
  2081. }
  2082. if (is_qla8022(ha)) {
  2083. if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  2084. ret = PCI_ERS_RESULT_RECOVERED;
  2085. goto exit_slot_reset;
  2086. } else
  2087. goto exit_slot_reset;
  2088. }
  2089. exit_slot_reset:
  2090. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
  2091. "device after reset\n", ha->host_no, __func__, ret);
  2092. return ret;
  2093. }
  2094. static void
  2095. qla4xxx_pci_resume(struct pci_dev *pdev)
  2096. {
  2097. struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  2098. int ret;
  2099. ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
  2100. ha->host_no, __func__);
  2101. ret = qla4xxx_wait_for_hba_online(ha);
  2102. if (ret != QLA_SUCCESS) {
  2103. ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  2104. "resume I/O from slot/link_reset\n", ha->host_no,
  2105. __func__);
  2106. }
  2107. pci_cleanup_aer_uncorrect_error_status(pdev);
  2108. clear_bit(AF_EEH_BUSY, &ha->flags);
  2109. }
  2110. static struct pci_error_handlers qla4xxx_err_handler = {
  2111. .error_detected = qla4xxx_pci_error_detected,
  2112. .mmio_enabled = qla4xxx_pci_mmio_enabled,
  2113. .slot_reset = qla4xxx_pci_slot_reset,
  2114. .resume = qla4xxx_pci_resume,
  2115. };
  2116. static struct pci_device_id qla4xxx_pci_tbl[] = {
  2117. {
  2118. .vendor = PCI_VENDOR_ID_QLOGIC,
  2119. .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
  2120. .subvendor = PCI_ANY_ID,
  2121. .subdevice = PCI_ANY_ID,
  2122. },
  2123. {
  2124. .vendor = PCI_VENDOR_ID_QLOGIC,
  2125. .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
  2126. .subvendor = PCI_ANY_ID,
  2127. .subdevice = PCI_ANY_ID,
  2128. },
  2129. {
  2130. .vendor = PCI_VENDOR_ID_QLOGIC,
  2131. .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
  2132. .subvendor = PCI_ANY_ID,
  2133. .subdevice = PCI_ANY_ID,
  2134. },
  2135. {
  2136. .vendor = PCI_VENDOR_ID_QLOGIC,
  2137. .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
  2138. .subvendor = PCI_ANY_ID,
  2139. .subdevice = PCI_ANY_ID,
  2140. },
  2141. {0, 0},
  2142. };
  2143. MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
  2144. static struct pci_driver qla4xxx_pci_driver = {
  2145. .name = DRIVER_NAME,
  2146. .id_table = qla4xxx_pci_tbl,
  2147. .probe = qla4xxx_probe_adapter,
  2148. .remove = qla4xxx_remove_adapter,
  2149. .err_handler = &qla4xxx_err_handler,
  2150. };
  2151. static int __init qla4xxx_module_init(void)
  2152. {
  2153. int ret;
  2154. /* Allocate cache for SRBs. */
  2155. srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
  2156. SLAB_HWCACHE_ALIGN, NULL);
  2157. if (srb_cachep == NULL) {
  2158. printk(KERN_ERR
  2159. "%s: Unable to allocate SRB cache..."
  2160. "Failing load!\n", DRIVER_NAME);
  2161. ret = -ENOMEM;
  2162. goto no_srp_cache;
  2163. }
  2164. /* Derive version string. */
  2165. strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
  2166. if (ql4xextended_error_logging)
  2167. strcat(qla4xxx_version_str, "-debug");
  2168. qla4xxx_scsi_transport =
  2169. iscsi_register_transport(&qla4xxx_iscsi_transport);
  2170. if (!qla4xxx_scsi_transport){
  2171. ret = -ENODEV;
  2172. goto release_srb_cache;
  2173. }
  2174. ret = pci_register_driver(&qla4xxx_pci_driver);
  2175. if (ret)
  2176. goto unregister_transport;
  2177. printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
  2178. return 0;
  2179. unregister_transport:
  2180. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2181. release_srb_cache:
  2182. kmem_cache_destroy(srb_cachep);
  2183. no_srp_cache:
  2184. return ret;
  2185. }
  2186. static void __exit qla4xxx_module_exit(void)
  2187. {
  2188. pci_unregister_driver(&qla4xxx_pci_driver);
  2189. iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  2190. kmem_cache_destroy(srb_cachep);
  2191. }
  2192. module_init(qla4xxx_module_init);
  2193. module_exit(qla4xxx_module_exit);
  2194. MODULE_AUTHOR("QLogic Corporation");
  2195. MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  2196. MODULE_LICENSE("GPL");
  2197. MODULE_VERSION(QLA4XXX_DRIVER_VERSION);