zfcp_erp.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. /*
  2. * zfcp device driver
  3. *
  4. * Error Recovery Procedures (ERP).
  5. *
  6. * Copyright IBM Corp. 2002, 2016
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kthread.h>
  11. #include "zfcp_ext.h"
  12. #include "zfcp_reqlist.h"
  13. #define ZFCP_MAX_ERPS 3
  14. enum zfcp_erp_act_flags {
  15. ZFCP_STATUS_ERP_TIMEDOUT = 0x10000000,
  16. ZFCP_STATUS_ERP_CLOSE_ONLY = 0x01000000,
  17. ZFCP_STATUS_ERP_DISMISSING = 0x00100000,
  18. ZFCP_STATUS_ERP_DISMISSED = 0x00200000,
  19. ZFCP_STATUS_ERP_LOWMEM = 0x00400000,
  20. ZFCP_STATUS_ERP_NO_REF = 0x00800000,
  21. };
  22. enum zfcp_erp_steps {
  23. ZFCP_ERP_STEP_UNINITIALIZED = 0x0000,
  24. ZFCP_ERP_STEP_FSF_XCONFIG = 0x0001,
  25. ZFCP_ERP_STEP_PHYS_PORT_CLOSING = 0x0010,
  26. ZFCP_ERP_STEP_PORT_CLOSING = 0x0100,
  27. ZFCP_ERP_STEP_PORT_OPENING = 0x0800,
  28. ZFCP_ERP_STEP_LUN_CLOSING = 0x1000,
  29. ZFCP_ERP_STEP_LUN_OPENING = 0x2000,
  30. };
  31. /**
  32. * enum zfcp_erp_act_type - Type of ERP action object.
  33. * @ZFCP_ERP_ACTION_REOPEN_LUN: LUN recovery.
  34. * @ZFCP_ERP_ACTION_REOPEN_PORT: Port recovery.
  35. * @ZFCP_ERP_ACTION_REOPEN_PORT_FORCED: Forced port recovery.
  36. * @ZFCP_ERP_ACTION_REOPEN_ADAPTER: Adapter recovery.
  37. * @ZFCP_ERP_ACTION_NONE: Eyecatcher pseudo flag to bitwise or-combine with
  38. * either of the first four enum values.
  39. * Used to indicate that an ERP action could not be
  40. * set up despite a detected need for some recovery.
  41. * @ZFCP_ERP_ACTION_FAILED: Eyecatcher pseudo flag to bitwise or-combine with
  42. * either of the first four enum values.
  43. * Used to indicate that ERP not needed because
  44. * the object has ZFCP_STATUS_COMMON_ERP_FAILED.
  45. */
  46. enum zfcp_erp_act_type {
  47. ZFCP_ERP_ACTION_REOPEN_LUN = 1,
  48. ZFCP_ERP_ACTION_REOPEN_PORT = 2,
  49. ZFCP_ERP_ACTION_REOPEN_PORT_FORCED = 3,
  50. ZFCP_ERP_ACTION_REOPEN_ADAPTER = 4,
  51. ZFCP_ERP_ACTION_NONE = 0xc0,
  52. ZFCP_ERP_ACTION_FAILED = 0xe0,
  53. };
  54. enum zfcp_erp_act_state {
  55. ZFCP_ERP_ACTION_RUNNING = 1,
  56. ZFCP_ERP_ACTION_READY = 2,
  57. };
  58. enum zfcp_erp_act_result {
  59. ZFCP_ERP_SUCCEEDED = 0,
  60. ZFCP_ERP_FAILED = 1,
  61. ZFCP_ERP_CONTINUES = 2,
  62. ZFCP_ERP_EXIT = 3,
  63. ZFCP_ERP_DISMISSED = 4,
  64. ZFCP_ERP_NOMEM = 5,
  65. };
  66. static void zfcp_erp_adapter_block(struct zfcp_adapter *adapter, int mask)
  67. {
  68. zfcp_erp_clear_adapter_status(adapter,
  69. ZFCP_STATUS_COMMON_UNBLOCKED | mask);
  70. }
  71. static int zfcp_erp_action_exists(struct zfcp_erp_action *act)
  72. {
  73. struct zfcp_erp_action *curr_act;
  74. list_for_each_entry(curr_act, &act->adapter->erp_running_head, list)
  75. if (act == curr_act)
  76. return ZFCP_ERP_ACTION_RUNNING;
  77. return 0;
  78. }
  79. static void zfcp_erp_action_ready(struct zfcp_erp_action *act)
  80. {
  81. struct zfcp_adapter *adapter = act->adapter;
  82. list_move(&act->list, &act->adapter->erp_ready_head);
  83. zfcp_dbf_rec_run("erardy1", act);
  84. wake_up(&adapter->erp_ready_wq);
  85. zfcp_dbf_rec_run("erardy2", act);
  86. }
  87. static void zfcp_erp_action_dismiss(struct zfcp_erp_action *act)
  88. {
  89. act->status |= ZFCP_STATUS_ERP_DISMISSED;
  90. if (zfcp_erp_action_exists(act) == ZFCP_ERP_ACTION_RUNNING)
  91. zfcp_erp_action_ready(act);
  92. }
  93. static void zfcp_erp_action_dismiss_lun(struct scsi_device *sdev)
  94. {
  95. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  96. if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
  97. zfcp_erp_action_dismiss(&zfcp_sdev->erp_action);
  98. }
  99. static void zfcp_erp_action_dismiss_port(struct zfcp_port *port)
  100. {
  101. struct scsi_device *sdev;
  102. if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
  103. zfcp_erp_action_dismiss(&port->erp_action);
  104. else {
  105. spin_lock(port->adapter->scsi_host->host_lock);
  106. __shost_for_each_device(sdev, port->adapter->scsi_host)
  107. if (sdev_to_zfcp(sdev)->port == port)
  108. zfcp_erp_action_dismiss_lun(sdev);
  109. spin_unlock(port->adapter->scsi_host->host_lock);
  110. }
  111. }
  112. static void zfcp_erp_action_dismiss_adapter(struct zfcp_adapter *adapter)
  113. {
  114. struct zfcp_port *port;
  115. if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_INUSE)
  116. zfcp_erp_action_dismiss(&adapter->erp_action);
  117. else {
  118. read_lock(&adapter->port_list_lock);
  119. list_for_each_entry(port, &adapter->port_list, list)
  120. zfcp_erp_action_dismiss_port(port);
  121. read_unlock(&adapter->port_list_lock);
  122. }
  123. }
  124. static int zfcp_erp_handle_failed(int want, struct zfcp_adapter *adapter,
  125. struct zfcp_port *port,
  126. struct scsi_device *sdev)
  127. {
  128. int need = want;
  129. struct zfcp_scsi_dev *zsdev;
  130. switch (want) {
  131. case ZFCP_ERP_ACTION_REOPEN_LUN:
  132. zsdev = sdev_to_zfcp(sdev);
  133. if (atomic_read(&zsdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
  134. need = 0;
  135. break;
  136. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  137. if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
  138. need = 0;
  139. break;
  140. case ZFCP_ERP_ACTION_REOPEN_PORT:
  141. if (atomic_read(&port->status) &
  142. ZFCP_STATUS_COMMON_ERP_FAILED) {
  143. need = 0;
  144. /* ensure propagation of failed status to new devices */
  145. zfcp_erp_set_port_status(
  146. port, ZFCP_STATUS_COMMON_ERP_FAILED);
  147. }
  148. break;
  149. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  150. if (atomic_read(&adapter->status) &
  151. ZFCP_STATUS_COMMON_ERP_FAILED) {
  152. need = 0;
  153. /* ensure propagation of failed status to new devices */
  154. zfcp_erp_set_adapter_status(
  155. adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
  156. }
  157. break;
  158. default:
  159. need = 0;
  160. break;
  161. }
  162. return need;
  163. }
  164. static int zfcp_erp_required_act(int want, struct zfcp_adapter *adapter,
  165. struct zfcp_port *port,
  166. struct scsi_device *sdev)
  167. {
  168. int need = want;
  169. int l_status, p_status, a_status;
  170. struct zfcp_scsi_dev *zfcp_sdev;
  171. switch (want) {
  172. case ZFCP_ERP_ACTION_REOPEN_LUN:
  173. zfcp_sdev = sdev_to_zfcp(sdev);
  174. l_status = atomic_read(&zfcp_sdev->status);
  175. if (l_status & ZFCP_STATUS_COMMON_ERP_INUSE)
  176. return 0;
  177. p_status = atomic_read(&port->status);
  178. if (!(p_status & ZFCP_STATUS_COMMON_RUNNING) ||
  179. p_status & ZFCP_STATUS_COMMON_ERP_FAILED)
  180. return 0;
  181. if (!(p_status & ZFCP_STATUS_COMMON_UNBLOCKED))
  182. need = ZFCP_ERP_ACTION_REOPEN_PORT;
  183. /* fall through */
  184. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  185. p_status = atomic_read(&port->status);
  186. if (!(p_status & ZFCP_STATUS_COMMON_OPEN))
  187. need = ZFCP_ERP_ACTION_REOPEN_PORT;
  188. /* fall through */
  189. case ZFCP_ERP_ACTION_REOPEN_PORT:
  190. p_status = atomic_read(&port->status);
  191. if (p_status & ZFCP_STATUS_COMMON_ERP_INUSE)
  192. return 0;
  193. a_status = atomic_read(&adapter->status);
  194. if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) ||
  195. a_status & ZFCP_STATUS_COMMON_ERP_FAILED)
  196. return 0;
  197. if (p_status & ZFCP_STATUS_COMMON_NOESC)
  198. return need;
  199. if (!(a_status & ZFCP_STATUS_COMMON_UNBLOCKED))
  200. need = ZFCP_ERP_ACTION_REOPEN_ADAPTER;
  201. /* fall through */
  202. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  203. a_status = atomic_read(&adapter->status);
  204. if (a_status & ZFCP_STATUS_COMMON_ERP_INUSE)
  205. return 0;
  206. if (!(a_status & ZFCP_STATUS_COMMON_RUNNING) &&
  207. !(a_status & ZFCP_STATUS_COMMON_OPEN))
  208. return 0; /* shutdown requested for closed adapter */
  209. }
  210. return need;
  211. }
  212. static struct zfcp_erp_action *zfcp_erp_setup_act(int need, u32 act_status,
  213. struct zfcp_adapter *adapter,
  214. struct zfcp_port *port,
  215. struct scsi_device *sdev)
  216. {
  217. struct zfcp_erp_action *erp_action;
  218. struct zfcp_scsi_dev *zfcp_sdev;
  219. switch (need) {
  220. case ZFCP_ERP_ACTION_REOPEN_LUN:
  221. zfcp_sdev = sdev_to_zfcp(sdev);
  222. if (!(act_status & ZFCP_STATUS_ERP_NO_REF))
  223. if (scsi_device_get(sdev))
  224. return NULL;
  225. atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE,
  226. &zfcp_sdev->status);
  227. erp_action = &zfcp_sdev->erp_action;
  228. WARN_ON_ONCE(erp_action->port != port);
  229. WARN_ON_ONCE(erp_action->sdev != sdev);
  230. if (!(atomic_read(&zfcp_sdev->status) &
  231. ZFCP_STATUS_COMMON_RUNNING))
  232. act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
  233. break;
  234. case ZFCP_ERP_ACTION_REOPEN_PORT:
  235. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  236. if (!get_device(&port->dev))
  237. return NULL;
  238. zfcp_erp_action_dismiss_port(port);
  239. atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &port->status);
  240. erp_action = &port->erp_action;
  241. WARN_ON_ONCE(erp_action->port != port);
  242. WARN_ON_ONCE(erp_action->sdev != NULL);
  243. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_RUNNING))
  244. act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
  245. break;
  246. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  247. kref_get(&adapter->ref);
  248. zfcp_erp_action_dismiss_adapter(adapter);
  249. atomic_or(ZFCP_STATUS_COMMON_ERP_INUSE, &adapter->status);
  250. erp_action = &adapter->erp_action;
  251. WARN_ON_ONCE(erp_action->port != NULL);
  252. WARN_ON_ONCE(erp_action->sdev != NULL);
  253. if (!(atomic_read(&adapter->status) &
  254. ZFCP_STATUS_COMMON_RUNNING))
  255. act_status |= ZFCP_STATUS_ERP_CLOSE_ONLY;
  256. break;
  257. default:
  258. return NULL;
  259. }
  260. WARN_ON_ONCE(erp_action->adapter != adapter);
  261. memset(&erp_action->list, 0, sizeof(erp_action->list));
  262. memset(&erp_action->timer, 0, sizeof(erp_action->timer));
  263. erp_action->step = ZFCP_ERP_STEP_UNINITIALIZED;
  264. erp_action->fsf_req_id = 0;
  265. erp_action->action = need;
  266. erp_action->status = act_status;
  267. return erp_action;
  268. }
  269. static int zfcp_erp_action_enqueue(int want, struct zfcp_adapter *adapter,
  270. struct zfcp_port *port,
  271. struct scsi_device *sdev,
  272. char *id, u32 act_status)
  273. {
  274. int retval = 1, need;
  275. struct zfcp_erp_action *act;
  276. need = zfcp_erp_handle_failed(want, adapter, port, sdev);
  277. if (!need) {
  278. need = ZFCP_ERP_ACTION_FAILED; /* marker for trace */
  279. goto out;
  280. }
  281. if (!adapter->erp_thread) {
  282. need = ZFCP_ERP_ACTION_NONE; /* marker for trace */
  283. retval = -EIO;
  284. goto out;
  285. }
  286. need = zfcp_erp_required_act(want, adapter, port, sdev);
  287. if (!need)
  288. goto out;
  289. act = zfcp_erp_setup_act(need, act_status, adapter, port, sdev);
  290. if (!act) {
  291. need |= ZFCP_ERP_ACTION_NONE; /* marker for trace */
  292. goto out;
  293. }
  294. atomic_or(ZFCP_STATUS_ADAPTER_ERP_PENDING, &adapter->status);
  295. ++adapter->erp_total_count;
  296. list_add_tail(&act->list, &adapter->erp_ready_head);
  297. wake_up(&adapter->erp_ready_wq);
  298. retval = 0;
  299. out:
  300. zfcp_dbf_rec_trig(id, adapter, port, sdev, want, need);
  301. return retval;
  302. }
  303. void zfcp_erp_port_forced_no_port_dbf(char *id, struct zfcp_adapter *adapter,
  304. u64 port_name, u32 port_id)
  305. {
  306. unsigned long flags;
  307. static /* don't waste stack */ struct zfcp_port tmpport;
  308. write_lock_irqsave(&adapter->erp_lock, flags);
  309. /* Stand-in zfcp port with fields just good enough for
  310. * zfcp_dbf_rec_trig() and zfcp_dbf_set_common().
  311. * Under lock because tmpport is static.
  312. */
  313. atomic_set(&tmpport.status, -1); /* unknown */
  314. tmpport.wwpn = port_name;
  315. tmpport.d_id = port_id;
  316. zfcp_dbf_rec_trig(id, adapter, &tmpport, NULL,
  317. ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
  318. ZFCP_ERP_ACTION_NONE);
  319. write_unlock_irqrestore(&adapter->erp_lock, flags);
  320. }
  321. static int _zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter,
  322. int clear_mask, char *id)
  323. {
  324. zfcp_erp_adapter_block(adapter, clear_mask);
  325. zfcp_scsi_schedule_rports_block(adapter);
  326. return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER,
  327. adapter, NULL, NULL, id, 0);
  328. }
  329. /**
  330. * zfcp_erp_adapter_reopen - Reopen adapter.
  331. * @adapter: Adapter to reopen.
  332. * @clear: Status flags to clear.
  333. * @id: Id for debug trace event.
  334. */
  335. void zfcp_erp_adapter_reopen(struct zfcp_adapter *adapter, int clear, char *id)
  336. {
  337. unsigned long flags;
  338. zfcp_erp_adapter_block(adapter, clear);
  339. zfcp_scsi_schedule_rports_block(adapter);
  340. write_lock_irqsave(&adapter->erp_lock, flags);
  341. zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_ADAPTER, adapter,
  342. NULL, NULL, id, 0);
  343. write_unlock_irqrestore(&adapter->erp_lock, flags);
  344. }
  345. /**
  346. * zfcp_erp_adapter_shutdown - Shutdown adapter.
  347. * @adapter: Adapter to shut down.
  348. * @clear: Status flags to clear.
  349. * @id: Id for debug trace event.
  350. */
  351. void zfcp_erp_adapter_shutdown(struct zfcp_adapter *adapter, int clear,
  352. char *id)
  353. {
  354. int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
  355. zfcp_erp_adapter_reopen(adapter, clear | flags, id);
  356. }
  357. /**
  358. * zfcp_erp_port_shutdown - Shutdown port
  359. * @port: Port to shut down.
  360. * @clear: Status flags to clear.
  361. * @id: Id for debug trace event.
  362. */
  363. void zfcp_erp_port_shutdown(struct zfcp_port *port, int clear, char *id)
  364. {
  365. int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
  366. zfcp_erp_port_reopen(port, clear | flags, id);
  367. }
  368. static void zfcp_erp_port_block(struct zfcp_port *port, int clear)
  369. {
  370. zfcp_erp_clear_port_status(port,
  371. ZFCP_STATUS_COMMON_UNBLOCKED | clear);
  372. }
  373. static void _zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear,
  374. char *id)
  375. {
  376. zfcp_erp_port_block(port, clear);
  377. zfcp_scsi_schedule_rport_block(port);
  378. zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT_FORCED,
  379. port->adapter, port, NULL, id, 0);
  380. }
  381. /**
  382. * zfcp_erp_port_forced_reopen - Forced close of port and open again
  383. * @port: Port to force close and to reopen.
  384. * @clear: Status flags to clear.
  385. * @id: Id for debug trace event.
  386. */
  387. void zfcp_erp_port_forced_reopen(struct zfcp_port *port, int clear, char *id)
  388. {
  389. unsigned long flags;
  390. struct zfcp_adapter *adapter = port->adapter;
  391. write_lock_irqsave(&adapter->erp_lock, flags);
  392. _zfcp_erp_port_forced_reopen(port, clear, id);
  393. write_unlock_irqrestore(&adapter->erp_lock, flags);
  394. }
  395. static int _zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
  396. {
  397. zfcp_erp_port_block(port, clear);
  398. zfcp_scsi_schedule_rport_block(port);
  399. return zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_PORT,
  400. port->adapter, port, NULL, id, 0);
  401. }
  402. /**
  403. * zfcp_erp_port_reopen - trigger remote port recovery
  404. * @port: port to recover
  405. * @clear_mask: flags in port status to be cleared
  406. * @id: Id for debug trace event.
  407. *
  408. * Returns 0 if recovery has been triggered, < 0 if not.
  409. */
  410. int zfcp_erp_port_reopen(struct zfcp_port *port, int clear, char *id)
  411. {
  412. int retval;
  413. unsigned long flags;
  414. struct zfcp_adapter *adapter = port->adapter;
  415. write_lock_irqsave(&adapter->erp_lock, flags);
  416. retval = _zfcp_erp_port_reopen(port, clear, id);
  417. write_unlock_irqrestore(&adapter->erp_lock, flags);
  418. return retval;
  419. }
  420. static void zfcp_erp_lun_block(struct scsi_device *sdev, int clear_mask)
  421. {
  422. zfcp_erp_clear_lun_status(sdev,
  423. ZFCP_STATUS_COMMON_UNBLOCKED | clear_mask);
  424. }
  425. static void _zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id,
  426. u32 act_status)
  427. {
  428. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  429. struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
  430. zfcp_erp_lun_block(sdev, clear);
  431. zfcp_erp_action_enqueue(ZFCP_ERP_ACTION_REOPEN_LUN, adapter,
  432. zfcp_sdev->port, sdev, id, act_status);
  433. }
  434. /**
  435. * zfcp_erp_lun_reopen - initiate reopen of a LUN
  436. * @sdev: SCSI device / LUN to be reopened
  437. * @clear_mask: specifies flags in LUN status to be cleared
  438. * @id: Id for debug trace event.
  439. *
  440. * Return: 0 on success, < 0 on error
  441. */
  442. void zfcp_erp_lun_reopen(struct scsi_device *sdev, int clear, char *id)
  443. {
  444. unsigned long flags;
  445. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  446. struct zfcp_port *port = zfcp_sdev->port;
  447. struct zfcp_adapter *adapter = port->adapter;
  448. write_lock_irqsave(&adapter->erp_lock, flags);
  449. _zfcp_erp_lun_reopen(sdev, clear, id, 0);
  450. write_unlock_irqrestore(&adapter->erp_lock, flags);
  451. }
  452. /**
  453. * zfcp_erp_lun_shutdown - Shutdown LUN
  454. * @sdev: SCSI device / LUN to shut down.
  455. * @clear: Status flags to clear.
  456. * @id: Id for debug trace event.
  457. */
  458. void zfcp_erp_lun_shutdown(struct scsi_device *sdev, int clear, char *id)
  459. {
  460. int flags = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
  461. zfcp_erp_lun_reopen(sdev, clear | flags, id);
  462. }
  463. /**
  464. * zfcp_erp_lun_shutdown_wait - Shutdown LUN and wait for erp completion
  465. * @sdev: SCSI device / LUN to shut down.
  466. * @id: Id for debug trace event.
  467. *
  468. * Do not acquire a reference for the LUN when creating the ERP
  469. * action. It is safe, because this function waits for the ERP to
  470. * complete first. This allows to shutdown the LUN, even when the SCSI
  471. * device is in the state SDEV_DEL when scsi_device_get will fail.
  472. */
  473. void zfcp_erp_lun_shutdown_wait(struct scsi_device *sdev, char *id)
  474. {
  475. unsigned long flags;
  476. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  477. struct zfcp_port *port = zfcp_sdev->port;
  478. struct zfcp_adapter *adapter = port->adapter;
  479. int clear = ZFCP_STATUS_COMMON_RUNNING | ZFCP_STATUS_COMMON_ERP_FAILED;
  480. write_lock_irqsave(&adapter->erp_lock, flags);
  481. _zfcp_erp_lun_reopen(sdev, clear, id, ZFCP_STATUS_ERP_NO_REF);
  482. write_unlock_irqrestore(&adapter->erp_lock, flags);
  483. zfcp_erp_wait(adapter);
  484. }
  485. static int status_change_set(unsigned long mask, atomic_t *status)
  486. {
  487. return (atomic_read(status) ^ mask) & mask;
  488. }
  489. static void zfcp_erp_adapter_unblock(struct zfcp_adapter *adapter)
  490. {
  491. if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status))
  492. zfcp_dbf_rec_run("eraubl1", &adapter->erp_action);
  493. atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &adapter->status);
  494. }
  495. static void zfcp_erp_port_unblock(struct zfcp_port *port)
  496. {
  497. if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status))
  498. zfcp_dbf_rec_run("erpubl1", &port->erp_action);
  499. atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &port->status);
  500. }
  501. static void zfcp_erp_lun_unblock(struct scsi_device *sdev)
  502. {
  503. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  504. if (status_change_set(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status))
  505. zfcp_dbf_rec_run("erlubl1", &sdev_to_zfcp(sdev)->erp_action);
  506. atomic_or(ZFCP_STATUS_COMMON_UNBLOCKED, &zfcp_sdev->status);
  507. }
  508. static void zfcp_erp_action_to_running(struct zfcp_erp_action *erp_action)
  509. {
  510. list_move(&erp_action->list, &erp_action->adapter->erp_running_head);
  511. zfcp_dbf_rec_run("erator1", erp_action);
  512. }
  513. static void zfcp_erp_strategy_check_fsfreq(struct zfcp_erp_action *act)
  514. {
  515. struct zfcp_adapter *adapter = act->adapter;
  516. struct zfcp_fsf_req *req;
  517. if (!act->fsf_req_id)
  518. return;
  519. spin_lock(&adapter->req_list->lock);
  520. req = _zfcp_reqlist_find(adapter->req_list, act->fsf_req_id);
  521. if (req && req->erp_action == act) {
  522. if (act->status & (ZFCP_STATUS_ERP_DISMISSED |
  523. ZFCP_STATUS_ERP_TIMEDOUT)) {
  524. req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
  525. zfcp_dbf_rec_run("erscf_1", act);
  526. req->erp_action = NULL;
  527. }
  528. if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
  529. zfcp_dbf_rec_run("erscf_2", act);
  530. if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED)
  531. act->fsf_req_id = 0;
  532. } else
  533. act->fsf_req_id = 0;
  534. spin_unlock(&adapter->req_list->lock);
  535. }
  536. /**
  537. * zfcp_erp_notify - Trigger ERP action.
  538. * @erp_action: ERP action to continue.
  539. * @set_mask: ERP action status flags to set.
  540. */
  541. void zfcp_erp_notify(struct zfcp_erp_action *erp_action, unsigned long set_mask)
  542. {
  543. struct zfcp_adapter *adapter = erp_action->adapter;
  544. unsigned long flags;
  545. write_lock_irqsave(&adapter->erp_lock, flags);
  546. if (zfcp_erp_action_exists(erp_action) == ZFCP_ERP_ACTION_RUNNING) {
  547. erp_action->status |= set_mask;
  548. zfcp_erp_action_ready(erp_action);
  549. }
  550. write_unlock_irqrestore(&adapter->erp_lock, flags);
  551. }
  552. /**
  553. * zfcp_erp_timeout_handler - Trigger ERP action from timed out ERP request
  554. * @data: ERP action (from timer data)
  555. */
  556. void zfcp_erp_timeout_handler(unsigned long data)
  557. {
  558. struct zfcp_erp_action *act = (struct zfcp_erp_action *) data;
  559. zfcp_erp_notify(act, ZFCP_STATUS_ERP_TIMEDOUT);
  560. }
  561. static void zfcp_erp_memwait_handler(unsigned long data)
  562. {
  563. zfcp_erp_notify((struct zfcp_erp_action *)data, 0);
  564. }
  565. static void zfcp_erp_strategy_memwait(struct zfcp_erp_action *erp_action)
  566. {
  567. init_timer(&erp_action->timer);
  568. erp_action->timer.function = zfcp_erp_memwait_handler;
  569. erp_action->timer.data = (unsigned long) erp_action;
  570. erp_action->timer.expires = jiffies + HZ;
  571. add_timer(&erp_action->timer);
  572. }
  573. static void _zfcp_erp_port_reopen_all(struct zfcp_adapter *adapter,
  574. int clear, char *id)
  575. {
  576. struct zfcp_port *port;
  577. read_lock(&adapter->port_list_lock);
  578. list_for_each_entry(port, &adapter->port_list, list)
  579. _zfcp_erp_port_reopen(port, clear, id);
  580. read_unlock(&adapter->port_list_lock);
  581. }
  582. static void _zfcp_erp_lun_reopen_all(struct zfcp_port *port, int clear,
  583. char *id)
  584. {
  585. struct scsi_device *sdev;
  586. spin_lock(port->adapter->scsi_host->host_lock);
  587. __shost_for_each_device(sdev, port->adapter->scsi_host)
  588. if (sdev_to_zfcp(sdev)->port == port)
  589. _zfcp_erp_lun_reopen(sdev, clear, id, 0);
  590. spin_unlock(port->adapter->scsi_host->host_lock);
  591. }
  592. static void zfcp_erp_strategy_followup_failed(struct zfcp_erp_action *act)
  593. {
  594. switch (act->action) {
  595. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  596. _zfcp_erp_adapter_reopen(act->adapter, 0, "ersff_1");
  597. break;
  598. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  599. _zfcp_erp_port_forced_reopen(act->port, 0, "ersff_2");
  600. break;
  601. case ZFCP_ERP_ACTION_REOPEN_PORT:
  602. _zfcp_erp_port_reopen(act->port, 0, "ersff_3");
  603. break;
  604. case ZFCP_ERP_ACTION_REOPEN_LUN:
  605. _zfcp_erp_lun_reopen(act->sdev, 0, "ersff_4", 0);
  606. break;
  607. }
  608. }
  609. static void zfcp_erp_strategy_followup_success(struct zfcp_erp_action *act)
  610. {
  611. switch (act->action) {
  612. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  613. _zfcp_erp_port_reopen_all(act->adapter, 0, "ersfs_1");
  614. break;
  615. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  616. _zfcp_erp_port_reopen(act->port, 0, "ersfs_2");
  617. break;
  618. case ZFCP_ERP_ACTION_REOPEN_PORT:
  619. _zfcp_erp_lun_reopen_all(act->port, 0, "ersfs_3");
  620. break;
  621. }
  622. }
  623. static void zfcp_erp_wakeup(struct zfcp_adapter *adapter)
  624. {
  625. unsigned long flags;
  626. read_lock_irqsave(&adapter->erp_lock, flags);
  627. if (list_empty(&adapter->erp_ready_head) &&
  628. list_empty(&adapter->erp_running_head)) {
  629. atomic_andnot(ZFCP_STATUS_ADAPTER_ERP_PENDING,
  630. &adapter->status);
  631. wake_up(&adapter->erp_done_wqh);
  632. }
  633. read_unlock_irqrestore(&adapter->erp_lock, flags);
  634. }
  635. static void zfcp_erp_enqueue_ptp_port(struct zfcp_adapter *adapter)
  636. {
  637. struct zfcp_port *port;
  638. port = zfcp_port_enqueue(adapter, adapter->peer_wwpn, 0,
  639. adapter->peer_d_id);
  640. if (IS_ERR(port)) /* error or port already attached */
  641. return;
  642. _zfcp_erp_port_reopen(port, 0, "ereptp1");
  643. }
  644. static int zfcp_erp_adapter_strat_fsf_xconf(struct zfcp_erp_action *erp_action)
  645. {
  646. int retries;
  647. int sleep = 1;
  648. struct zfcp_adapter *adapter = erp_action->adapter;
  649. atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK, &adapter->status);
  650. for (retries = 7; retries; retries--) {
  651. atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  652. &adapter->status);
  653. write_lock_irq(&adapter->erp_lock);
  654. zfcp_erp_action_to_running(erp_action);
  655. write_unlock_irq(&adapter->erp_lock);
  656. if (zfcp_fsf_exchange_config_data(erp_action)) {
  657. atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  658. &adapter->status);
  659. return ZFCP_ERP_FAILED;
  660. }
  661. wait_event(adapter->erp_ready_wq,
  662. !list_empty(&adapter->erp_ready_head));
  663. if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT)
  664. break;
  665. if (!(atomic_read(&adapter->status) &
  666. ZFCP_STATUS_ADAPTER_HOST_CON_INIT))
  667. break;
  668. ssleep(sleep);
  669. sleep *= 2;
  670. }
  671. atomic_andnot(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
  672. &adapter->status);
  673. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_XCONFIG_OK))
  674. return ZFCP_ERP_FAILED;
  675. if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
  676. zfcp_erp_enqueue_ptp_port(adapter);
  677. return ZFCP_ERP_SUCCEEDED;
  678. }
  679. static int zfcp_erp_adapter_strategy_open_fsf_xport(struct zfcp_erp_action *act)
  680. {
  681. int ret;
  682. struct zfcp_adapter *adapter = act->adapter;
  683. write_lock_irq(&adapter->erp_lock);
  684. zfcp_erp_action_to_running(act);
  685. write_unlock_irq(&adapter->erp_lock);
  686. ret = zfcp_fsf_exchange_port_data(act);
  687. if (ret == -EOPNOTSUPP)
  688. return ZFCP_ERP_SUCCEEDED;
  689. if (ret)
  690. return ZFCP_ERP_FAILED;
  691. zfcp_dbf_rec_run("erasox1", act);
  692. wait_event(adapter->erp_ready_wq,
  693. !list_empty(&adapter->erp_ready_head));
  694. zfcp_dbf_rec_run("erasox2", act);
  695. if (act->status & ZFCP_STATUS_ERP_TIMEDOUT)
  696. return ZFCP_ERP_FAILED;
  697. return ZFCP_ERP_SUCCEEDED;
  698. }
  699. static int zfcp_erp_adapter_strategy_open_fsf(struct zfcp_erp_action *act)
  700. {
  701. if (zfcp_erp_adapter_strat_fsf_xconf(act) == ZFCP_ERP_FAILED)
  702. return ZFCP_ERP_FAILED;
  703. if (zfcp_erp_adapter_strategy_open_fsf_xport(act) == ZFCP_ERP_FAILED)
  704. return ZFCP_ERP_FAILED;
  705. if (mempool_resize(act->adapter->pool.sr_data,
  706. act->adapter->stat_read_buf_num))
  707. return ZFCP_ERP_FAILED;
  708. if (mempool_resize(act->adapter->pool.status_read_req,
  709. act->adapter->stat_read_buf_num))
  710. return ZFCP_ERP_FAILED;
  711. atomic_set(&act->adapter->stat_miss, act->adapter->stat_read_buf_num);
  712. if (zfcp_status_read_refill(act->adapter))
  713. return ZFCP_ERP_FAILED;
  714. return ZFCP_ERP_SUCCEEDED;
  715. }
  716. static void zfcp_erp_adapter_strategy_close(struct zfcp_erp_action *act)
  717. {
  718. struct zfcp_adapter *adapter = act->adapter;
  719. /* close queues to ensure that buffers are not accessed by adapter */
  720. zfcp_qdio_close(adapter->qdio);
  721. zfcp_fsf_req_dismiss_all(adapter);
  722. adapter->fsf_req_seq_no = 0;
  723. zfcp_fc_wka_ports_force_offline(adapter->gs);
  724. /* all ports and LUNs are closed */
  725. zfcp_erp_clear_adapter_status(adapter, ZFCP_STATUS_COMMON_OPEN);
  726. atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
  727. ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
  728. }
  729. static int zfcp_erp_adapter_strategy_open(struct zfcp_erp_action *act)
  730. {
  731. struct zfcp_adapter *adapter = act->adapter;
  732. if (zfcp_qdio_open(adapter->qdio)) {
  733. atomic_andnot(ZFCP_STATUS_ADAPTER_XCONFIG_OK |
  734. ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED,
  735. &adapter->status);
  736. return ZFCP_ERP_FAILED;
  737. }
  738. if (zfcp_erp_adapter_strategy_open_fsf(act)) {
  739. zfcp_erp_adapter_strategy_close(act);
  740. return ZFCP_ERP_FAILED;
  741. }
  742. atomic_or(ZFCP_STATUS_COMMON_OPEN, &adapter->status);
  743. return ZFCP_ERP_SUCCEEDED;
  744. }
  745. static int zfcp_erp_adapter_strategy(struct zfcp_erp_action *act)
  746. {
  747. struct zfcp_adapter *adapter = act->adapter;
  748. if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN) {
  749. zfcp_erp_adapter_strategy_close(act);
  750. if (act->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  751. return ZFCP_ERP_EXIT;
  752. }
  753. if (zfcp_erp_adapter_strategy_open(act)) {
  754. ssleep(8);
  755. return ZFCP_ERP_FAILED;
  756. }
  757. return ZFCP_ERP_SUCCEEDED;
  758. }
  759. static int zfcp_erp_port_forced_strategy_close(struct zfcp_erp_action *act)
  760. {
  761. int retval;
  762. retval = zfcp_fsf_close_physical_port(act);
  763. if (retval == -ENOMEM)
  764. return ZFCP_ERP_NOMEM;
  765. act->step = ZFCP_ERP_STEP_PHYS_PORT_CLOSING;
  766. if (retval)
  767. return ZFCP_ERP_FAILED;
  768. return ZFCP_ERP_CONTINUES;
  769. }
  770. static int zfcp_erp_port_forced_strategy(struct zfcp_erp_action *erp_action)
  771. {
  772. struct zfcp_port *port = erp_action->port;
  773. int status = atomic_read(&port->status);
  774. switch (erp_action->step) {
  775. case ZFCP_ERP_STEP_UNINITIALIZED:
  776. if ((status & ZFCP_STATUS_PORT_PHYS_OPEN) &&
  777. (status & ZFCP_STATUS_COMMON_OPEN))
  778. return zfcp_erp_port_forced_strategy_close(erp_action);
  779. else
  780. return ZFCP_ERP_FAILED;
  781. case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
  782. if (!(status & ZFCP_STATUS_PORT_PHYS_OPEN))
  783. return ZFCP_ERP_SUCCEEDED;
  784. }
  785. return ZFCP_ERP_FAILED;
  786. }
  787. static int zfcp_erp_port_strategy_close(struct zfcp_erp_action *erp_action)
  788. {
  789. int retval;
  790. retval = zfcp_fsf_close_port(erp_action);
  791. if (retval == -ENOMEM)
  792. return ZFCP_ERP_NOMEM;
  793. erp_action->step = ZFCP_ERP_STEP_PORT_CLOSING;
  794. if (retval)
  795. return ZFCP_ERP_FAILED;
  796. return ZFCP_ERP_CONTINUES;
  797. }
  798. static int zfcp_erp_port_strategy_open_port(struct zfcp_erp_action *erp_action)
  799. {
  800. int retval;
  801. retval = zfcp_fsf_open_port(erp_action);
  802. if (retval == -ENOMEM)
  803. return ZFCP_ERP_NOMEM;
  804. erp_action->step = ZFCP_ERP_STEP_PORT_OPENING;
  805. if (retval)
  806. return ZFCP_ERP_FAILED;
  807. return ZFCP_ERP_CONTINUES;
  808. }
  809. static int zfcp_erp_open_ptp_port(struct zfcp_erp_action *act)
  810. {
  811. struct zfcp_adapter *adapter = act->adapter;
  812. struct zfcp_port *port = act->port;
  813. if (port->wwpn != adapter->peer_wwpn) {
  814. zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
  815. return ZFCP_ERP_FAILED;
  816. }
  817. port->d_id = adapter->peer_d_id;
  818. return zfcp_erp_port_strategy_open_port(act);
  819. }
  820. static int zfcp_erp_port_strategy_open_common(struct zfcp_erp_action *act)
  821. {
  822. struct zfcp_adapter *adapter = act->adapter;
  823. struct zfcp_port *port = act->port;
  824. int p_status = atomic_read(&port->status);
  825. switch (act->step) {
  826. case ZFCP_ERP_STEP_UNINITIALIZED:
  827. case ZFCP_ERP_STEP_PHYS_PORT_CLOSING:
  828. case ZFCP_ERP_STEP_PORT_CLOSING:
  829. if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_PTP)
  830. return zfcp_erp_open_ptp_port(act);
  831. if (!port->d_id) {
  832. zfcp_fc_trigger_did_lookup(port);
  833. return ZFCP_ERP_EXIT;
  834. }
  835. return zfcp_erp_port_strategy_open_port(act);
  836. case ZFCP_ERP_STEP_PORT_OPENING:
  837. /* D_ID might have changed during open */
  838. if (p_status & ZFCP_STATUS_COMMON_OPEN) {
  839. if (!port->d_id) {
  840. zfcp_fc_trigger_did_lookup(port);
  841. return ZFCP_ERP_EXIT;
  842. }
  843. return ZFCP_ERP_SUCCEEDED;
  844. }
  845. if (port->d_id && !(p_status & ZFCP_STATUS_COMMON_NOESC)) {
  846. port->d_id = 0;
  847. return ZFCP_ERP_FAILED;
  848. }
  849. /* fall through otherwise */
  850. }
  851. return ZFCP_ERP_FAILED;
  852. }
  853. static int zfcp_erp_port_strategy(struct zfcp_erp_action *erp_action)
  854. {
  855. struct zfcp_port *port = erp_action->port;
  856. int p_status = atomic_read(&port->status);
  857. if ((p_status & ZFCP_STATUS_COMMON_NOESC) &&
  858. !(p_status & ZFCP_STATUS_COMMON_OPEN))
  859. goto close_init_done;
  860. switch (erp_action->step) {
  861. case ZFCP_ERP_STEP_UNINITIALIZED:
  862. if (p_status & ZFCP_STATUS_COMMON_OPEN)
  863. return zfcp_erp_port_strategy_close(erp_action);
  864. break;
  865. case ZFCP_ERP_STEP_PORT_CLOSING:
  866. if (p_status & ZFCP_STATUS_COMMON_OPEN)
  867. return ZFCP_ERP_FAILED;
  868. break;
  869. }
  870. close_init_done:
  871. if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  872. return ZFCP_ERP_EXIT;
  873. return zfcp_erp_port_strategy_open_common(erp_action);
  874. }
  875. static void zfcp_erp_lun_strategy_clearstati(struct scsi_device *sdev)
  876. {
  877. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  878. atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED,
  879. &zfcp_sdev->status);
  880. }
  881. static int zfcp_erp_lun_strategy_close(struct zfcp_erp_action *erp_action)
  882. {
  883. int retval = zfcp_fsf_close_lun(erp_action);
  884. if (retval == -ENOMEM)
  885. return ZFCP_ERP_NOMEM;
  886. erp_action->step = ZFCP_ERP_STEP_LUN_CLOSING;
  887. if (retval)
  888. return ZFCP_ERP_FAILED;
  889. return ZFCP_ERP_CONTINUES;
  890. }
  891. static int zfcp_erp_lun_strategy_open(struct zfcp_erp_action *erp_action)
  892. {
  893. int retval = zfcp_fsf_open_lun(erp_action);
  894. if (retval == -ENOMEM)
  895. return ZFCP_ERP_NOMEM;
  896. erp_action->step = ZFCP_ERP_STEP_LUN_OPENING;
  897. if (retval)
  898. return ZFCP_ERP_FAILED;
  899. return ZFCP_ERP_CONTINUES;
  900. }
  901. static int zfcp_erp_lun_strategy(struct zfcp_erp_action *erp_action)
  902. {
  903. struct scsi_device *sdev = erp_action->sdev;
  904. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  905. switch (erp_action->step) {
  906. case ZFCP_ERP_STEP_UNINITIALIZED:
  907. zfcp_erp_lun_strategy_clearstati(sdev);
  908. if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
  909. return zfcp_erp_lun_strategy_close(erp_action);
  910. /* already closed, fall through */
  911. case ZFCP_ERP_STEP_LUN_CLOSING:
  912. if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
  913. return ZFCP_ERP_FAILED;
  914. if (erp_action->status & ZFCP_STATUS_ERP_CLOSE_ONLY)
  915. return ZFCP_ERP_EXIT;
  916. return zfcp_erp_lun_strategy_open(erp_action);
  917. case ZFCP_ERP_STEP_LUN_OPENING:
  918. if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_OPEN)
  919. return ZFCP_ERP_SUCCEEDED;
  920. }
  921. return ZFCP_ERP_FAILED;
  922. }
  923. static int zfcp_erp_strategy_check_lun(struct scsi_device *sdev, int result)
  924. {
  925. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  926. switch (result) {
  927. case ZFCP_ERP_SUCCEEDED :
  928. atomic_set(&zfcp_sdev->erp_counter, 0);
  929. zfcp_erp_lun_unblock(sdev);
  930. break;
  931. case ZFCP_ERP_FAILED :
  932. atomic_inc(&zfcp_sdev->erp_counter);
  933. if (atomic_read(&zfcp_sdev->erp_counter) > ZFCP_MAX_ERPS) {
  934. dev_err(&zfcp_sdev->port->adapter->ccw_device->dev,
  935. "ERP failed for LUN 0x%016Lx on "
  936. "port 0x%016Lx\n",
  937. (unsigned long long)zfcp_scsi_dev_lun(sdev),
  938. (unsigned long long)zfcp_sdev->port->wwpn);
  939. zfcp_erp_set_lun_status(sdev,
  940. ZFCP_STATUS_COMMON_ERP_FAILED);
  941. }
  942. break;
  943. }
  944. if (atomic_read(&zfcp_sdev->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
  945. zfcp_erp_lun_block(sdev, 0);
  946. result = ZFCP_ERP_EXIT;
  947. }
  948. return result;
  949. }
  950. static int zfcp_erp_strategy_check_port(struct zfcp_port *port, int result)
  951. {
  952. switch (result) {
  953. case ZFCP_ERP_SUCCEEDED :
  954. atomic_set(&port->erp_counter, 0);
  955. zfcp_erp_port_unblock(port);
  956. break;
  957. case ZFCP_ERP_FAILED :
  958. if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC) {
  959. zfcp_erp_port_block(port, 0);
  960. result = ZFCP_ERP_EXIT;
  961. }
  962. atomic_inc(&port->erp_counter);
  963. if (atomic_read(&port->erp_counter) > ZFCP_MAX_ERPS) {
  964. dev_err(&port->adapter->ccw_device->dev,
  965. "ERP failed for remote port 0x%016Lx\n",
  966. (unsigned long long)port->wwpn);
  967. zfcp_erp_set_port_status(port,
  968. ZFCP_STATUS_COMMON_ERP_FAILED);
  969. }
  970. break;
  971. }
  972. if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
  973. zfcp_erp_port_block(port, 0);
  974. result = ZFCP_ERP_EXIT;
  975. }
  976. return result;
  977. }
  978. static int zfcp_erp_strategy_check_adapter(struct zfcp_adapter *adapter,
  979. int result)
  980. {
  981. switch (result) {
  982. case ZFCP_ERP_SUCCEEDED :
  983. atomic_set(&adapter->erp_counter, 0);
  984. zfcp_erp_adapter_unblock(adapter);
  985. break;
  986. case ZFCP_ERP_FAILED :
  987. atomic_inc(&adapter->erp_counter);
  988. if (atomic_read(&adapter->erp_counter) > ZFCP_MAX_ERPS) {
  989. dev_err(&adapter->ccw_device->dev,
  990. "ERP cannot recover an error "
  991. "on the FCP device\n");
  992. zfcp_erp_set_adapter_status(adapter,
  993. ZFCP_STATUS_COMMON_ERP_FAILED);
  994. }
  995. break;
  996. }
  997. if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED) {
  998. zfcp_erp_adapter_block(adapter, 0);
  999. result = ZFCP_ERP_EXIT;
  1000. }
  1001. return result;
  1002. }
  1003. static int zfcp_erp_strategy_check_target(struct zfcp_erp_action *erp_action,
  1004. int result)
  1005. {
  1006. struct zfcp_adapter *adapter = erp_action->adapter;
  1007. struct zfcp_port *port = erp_action->port;
  1008. struct scsi_device *sdev = erp_action->sdev;
  1009. switch (erp_action->action) {
  1010. case ZFCP_ERP_ACTION_REOPEN_LUN:
  1011. result = zfcp_erp_strategy_check_lun(sdev, result);
  1012. break;
  1013. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1014. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1015. result = zfcp_erp_strategy_check_port(port, result);
  1016. break;
  1017. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1018. result = zfcp_erp_strategy_check_adapter(adapter, result);
  1019. break;
  1020. }
  1021. return result;
  1022. }
  1023. static int zfcp_erp_strat_change_det(atomic_t *target_status, u32 erp_status)
  1024. {
  1025. int status = atomic_read(target_status);
  1026. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  1027. (erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
  1028. return 1; /* take it online */
  1029. if (!(status & ZFCP_STATUS_COMMON_RUNNING) &&
  1030. !(erp_status & ZFCP_STATUS_ERP_CLOSE_ONLY))
  1031. return 1; /* take it offline */
  1032. return 0;
  1033. }
  1034. static int zfcp_erp_strategy_statechange(struct zfcp_erp_action *act, int ret)
  1035. {
  1036. int action = act->action;
  1037. struct zfcp_adapter *adapter = act->adapter;
  1038. struct zfcp_port *port = act->port;
  1039. struct scsi_device *sdev = act->sdev;
  1040. struct zfcp_scsi_dev *zfcp_sdev;
  1041. u32 erp_status = act->status;
  1042. switch (action) {
  1043. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1044. if (zfcp_erp_strat_change_det(&adapter->status, erp_status)) {
  1045. _zfcp_erp_adapter_reopen(adapter,
  1046. ZFCP_STATUS_COMMON_ERP_FAILED,
  1047. "ersscg1");
  1048. return ZFCP_ERP_EXIT;
  1049. }
  1050. break;
  1051. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1052. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1053. if (zfcp_erp_strat_change_det(&port->status, erp_status)) {
  1054. _zfcp_erp_port_reopen(port,
  1055. ZFCP_STATUS_COMMON_ERP_FAILED,
  1056. "ersscg2");
  1057. return ZFCP_ERP_EXIT;
  1058. }
  1059. break;
  1060. case ZFCP_ERP_ACTION_REOPEN_LUN:
  1061. zfcp_sdev = sdev_to_zfcp(sdev);
  1062. if (zfcp_erp_strat_change_det(&zfcp_sdev->status, erp_status)) {
  1063. _zfcp_erp_lun_reopen(sdev,
  1064. ZFCP_STATUS_COMMON_ERP_FAILED,
  1065. "ersscg3", 0);
  1066. return ZFCP_ERP_EXIT;
  1067. }
  1068. break;
  1069. }
  1070. return ret;
  1071. }
  1072. static void zfcp_erp_action_dequeue(struct zfcp_erp_action *erp_action)
  1073. {
  1074. struct zfcp_adapter *adapter = erp_action->adapter;
  1075. struct zfcp_scsi_dev *zfcp_sdev;
  1076. adapter->erp_total_count--;
  1077. if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
  1078. adapter->erp_low_mem_count--;
  1079. erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
  1080. }
  1081. list_del(&erp_action->list);
  1082. zfcp_dbf_rec_run("eractd1", erp_action);
  1083. switch (erp_action->action) {
  1084. case ZFCP_ERP_ACTION_REOPEN_LUN:
  1085. zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
  1086. atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
  1087. &zfcp_sdev->status);
  1088. break;
  1089. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1090. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1091. atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
  1092. &erp_action->port->status);
  1093. break;
  1094. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1095. atomic_andnot(ZFCP_STATUS_COMMON_ERP_INUSE,
  1096. &erp_action->adapter->status);
  1097. break;
  1098. }
  1099. }
  1100. /**
  1101. * zfcp_erp_try_rport_unblock - unblock rport if no more/new recovery
  1102. * @port: zfcp_port whose fc_rport we should try to unblock
  1103. */
  1104. static void zfcp_erp_try_rport_unblock(struct zfcp_port *port)
  1105. {
  1106. unsigned long flags;
  1107. struct zfcp_adapter *adapter = port->adapter;
  1108. int port_status;
  1109. struct Scsi_Host *shost = adapter->scsi_host;
  1110. struct scsi_device *sdev;
  1111. write_lock_irqsave(&adapter->erp_lock, flags);
  1112. port_status = atomic_read(&port->status);
  1113. if ((port_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
  1114. (port_status & (ZFCP_STATUS_COMMON_ERP_INUSE |
  1115. ZFCP_STATUS_COMMON_ERP_FAILED)) != 0) {
  1116. /* new ERP of severity >= port triggered elsewhere meanwhile or
  1117. * local link down (adapter erp_failed but not clear unblock)
  1118. */
  1119. zfcp_dbf_rec_run_lvl(4, "ertru_p", &port->erp_action);
  1120. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1121. return;
  1122. }
  1123. spin_lock(shost->host_lock);
  1124. __shost_for_each_device(sdev, shost) {
  1125. struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
  1126. int lun_status;
  1127. if (zsdev->port != port)
  1128. continue;
  1129. /* LUN under port of interest */
  1130. lun_status = atomic_read(&zsdev->status);
  1131. if ((lun_status & ZFCP_STATUS_COMMON_ERP_FAILED) != 0)
  1132. continue; /* unblock rport despite failed LUNs */
  1133. /* LUN recovery not given up yet [maybe follow-up pending] */
  1134. if ((lun_status & ZFCP_STATUS_COMMON_UNBLOCKED) == 0 ||
  1135. (lun_status & ZFCP_STATUS_COMMON_ERP_INUSE) != 0) {
  1136. /* LUN blocked:
  1137. * not yet unblocked [LUN recovery pending]
  1138. * or meanwhile blocked [new LUN recovery triggered]
  1139. */
  1140. zfcp_dbf_rec_run_lvl(4, "ertru_l", &zsdev->erp_action);
  1141. spin_unlock(shost->host_lock);
  1142. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1143. return;
  1144. }
  1145. }
  1146. /* now port has no child or all children have completed recovery,
  1147. * and no ERP of severity >= port was meanwhile triggered elsewhere
  1148. */
  1149. zfcp_scsi_schedule_rport_register(port);
  1150. spin_unlock(shost->host_lock);
  1151. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1152. }
  1153. static void zfcp_erp_action_cleanup(struct zfcp_erp_action *act, int result)
  1154. {
  1155. struct zfcp_adapter *adapter = act->adapter;
  1156. struct zfcp_port *port = act->port;
  1157. struct scsi_device *sdev = act->sdev;
  1158. switch (act->action) {
  1159. case ZFCP_ERP_ACTION_REOPEN_LUN:
  1160. if (!(act->status & ZFCP_STATUS_ERP_NO_REF))
  1161. scsi_device_put(sdev);
  1162. zfcp_erp_try_rport_unblock(port);
  1163. break;
  1164. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1165. /* This switch case might also happen after a forced reopen
  1166. * was successfully done and thus overwritten with a new
  1167. * non-forced reopen at `ersfs_2'. In this case, we must not
  1168. * do the clean-up of the non-forced version.
  1169. */
  1170. if (act->step != ZFCP_ERP_STEP_UNINITIALIZED)
  1171. if (result == ZFCP_ERP_SUCCEEDED)
  1172. zfcp_erp_try_rport_unblock(port);
  1173. /* fall through */
  1174. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1175. put_device(&port->dev);
  1176. break;
  1177. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1178. if (result == ZFCP_ERP_SUCCEEDED) {
  1179. register_service_level(&adapter->service_level);
  1180. zfcp_fc_conditional_port_scan(adapter);
  1181. queue_work(adapter->work_queue, &adapter->ns_up_work);
  1182. } else
  1183. unregister_service_level(&adapter->service_level);
  1184. kref_put(&adapter->ref, zfcp_adapter_release);
  1185. break;
  1186. }
  1187. }
  1188. static int zfcp_erp_strategy_do_action(struct zfcp_erp_action *erp_action)
  1189. {
  1190. switch (erp_action->action) {
  1191. case ZFCP_ERP_ACTION_REOPEN_ADAPTER:
  1192. return zfcp_erp_adapter_strategy(erp_action);
  1193. case ZFCP_ERP_ACTION_REOPEN_PORT_FORCED:
  1194. return zfcp_erp_port_forced_strategy(erp_action);
  1195. case ZFCP_ERP_ACTION_REOPEN_PORT:
  1196. return zfcp_erp_port_strategy(erp_action);
  1197. case ZFCP_ERP_ACTION_REOPEN_LUN:
  1198. return zfcp_erp_lun_strategy(erp_action);
  1199. }
  1200. return ZFCP_ERP_FAILED;
  1201. }
  1202. static int zfcp_erp_strategy(struct zfcp_erp_action *erp_action)
  1203. {
  1204. int retval;
  1205. unsigned long flags;
  1206. struct zfcp_adapter *adapter = erp_action->adapter;
  1207. kref_get(&adapter->ref);
  1208. write_lock_irqsave(&adapter->erp_lock, flags);
  1209. zfcp_erp_strategy_check_fsfreq(erp_action);
  1210. if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED) {
  1211. zfcp_erp_action_dequeue(erp_action);
  1212. retval = ZFCP_ERP_DISMISSED;
  1213. goto unlock;
  1214. }
  1215. if (erp_action->status & ZFCP_STATUS_ERP_TIMEDOUT) {
  1216. retval = ZFCP_ERP_FAILED;
  1217. goto check_target;
  1218. }
  1219. zfcp_erp_action_to_running(erp_action);
  1220. /* no lock to allow for blocking operations */
  1221. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1222. retval = zfcp_erp_strategy_do_action(erp_action);
  1223. write_lock_irqsave(&adapter->erp_lock, flags);
  1224. if (erp_action->status & ZFCP_STATUS_ERP_DISMISSED)
  1225. retval = ZFCP_ERP_CONTINUES;
  1226. switch (retval) {
  1227. case ZFCP_ERP_NOMEM:
  1228. if (!(erp_action->status & ZFCP_STATUS_ERP_LOWMEM)) {
  1229. ++adapter->erp_low_mem_count;
  1230. erp_action->status |= ZFCP_STATUS_ERP_LOWMEM;
  1231. }
  1232. if (adapter->erp_total_count == adapter->erp_low_mem_count)
  1233. _zfcp_erp_adapter_reopen(adapter, 0, "erstgy1");
  1234. else {
  1235. zfcp_erp_strategy_memwait(erp_action);
  1236. retval = ZFCP_ERP_CONTINUES;
  1237. }
  1238. goto unlock;
  1239. case ZFCP_ERP_CONTINUES:
  1240. if (erp_action->status & ZFCP_STATUS_ERP_LOWMEM) {
  1241. --adapter->erp_low_mem_count;
  1242. erp_action->status &= ~ZFCP_STATUS_ERP_LOWMEM;
  1243. }
  1244. goto unlock;
  1245. }
  1246. check_target:
  1247. retval = zfcp_erp_strategy_check_target(erp_action, retval);
  1248. zfcp_erp_action_dequeue(erp_action);
  1249. retval = zfcp_erp_strategy_statechange(erp_action, retval);
  1250. if (retval == ZFCP_ERP_EXIT)
  1251. goto unlock;
  1252. if (retval == ZFCP_ERP_SUCCEEDED)
  1253. zfcp_erp_strategy_followup_success(erp_action);
  1254. if (retval == ZFCP_ERP_FAILED)
  1255. zfcp_erp_strategy_followup_failed(erp_action);
  1256. unlock:
  1257. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1258. if (retval != ZFCP_ERP_CONTINUES)
  1259. zfcp_erp_action_cleanup(erp_action, retval);
  1260. kref_put(&adapter->ref, zfcp_adapter_release);
  1261. return retval;
  1262. }
  1263. static int zfcp_erp_thread(void *data)
  1264. {
  1265. struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
  1266. struct list_head *next;
  1267. struct zfcp_erp_action *act;
  1268. unsigned long flags;
  1269. for (;;) {
  1270. wait_event_interruptible(adapter->erp_ready_wq,
  1271. !list_empty(&adapter->erp_ready_head) ||
  1272. kthread_should_stop());
  1273. if (kthread_should_stop())
  1274. break;
  1275. write_lock_irqsave(&adapter->erp_lock, flags);
  1276. next = adapter->erp_ready_head.next;
  1277. write_unlock_irqrestore(&adapter->erp_lock, flags);
  1278. if (next != &adapter->erp_ready_head) {
  1279. act = list_entry(next, struct zfcp_erp_action, list);
  1280. /* there is more to come after dismission, no notify */
  1281. if (zfcp_erp_strategy(act) != ZFCP_ERP_DISMISSED)
  1282. zfcp_erp_wakeup(adapter);
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * zfcp_erp_thread_setup - Start ERP thread for adapter
  1289. * @adapter: Adapter to start the ERP thread for
  1290. *
  1291. * Returns 0 on success or error code from kernel_thread()
  1292. */
  1293. int zfcp_erp_thread_setup(struct zfcp_adapter *adapter)
  1294. {
  1295. struct task_struct *thread;
  1296. thread = kthread_run(zfcp_erp_thread, adapter, "zfcperp%s",
  1297. dev_name(&adapter->ccw_device->dev));
  1298. if (IS_ERR(thread)) {
  1299. dev_err(&adapter->ccw_device->dev,
  1300. "Creating an ERP thread for the FCP device failed.\n");
  1301. return PTR_ERR(thread);
  1302. }
  1303. adapter->erp_thread = thread;
  1304. return 0;
  1305. }
  1306. /**
  1307. * zfcp_erp_thread_kill - Stop ERP thread.
  1308. * @adapter: Adapter where the ERP thread should be stopped.
  1309. *
  1310. * The caller of this routine ensures that the specified adapter has
  1311. * been shut down and that this operation has been completed. Thus,
  1312. * there are no pending erp_actions which would need to be handled
  1313. * here.
  1314. */
  1315. void zfcp_erp_thread_kill(struct zfcp_adapter *adapter)
  1316. {
  1317. kthread_stop(adapter->erp_thread);
  1318. adapter->erp_thread = NULL;
  1319. WARN_ON(!list_empty(&adapter->erp_ready_head));
  1320. WARN_ON(!list_empty(&adapter->erp_running_head));
  1321. }
  1322. /**
  1323. * zfcp_erp_wait - wait for completion of error recovery on an adapter
  1324. * @adapter: adapter for which to wait for completion of its error recovery
  1325. */
  1326. void zfcp_erp_wait(struct zfcp_adapter *adapter)
  1327. {
  1328. wait_event(adapter->erp_done_wqh,
  1329. !(atomic_read(&adapter->status) &
  1330. ZFCP_STATUS_ADAPTER_ERP_PENDING));
  1331. }
  1332. /**
  1333. * zfcp_erp_set_adapter_status - set adapter status bits
  1334. * @adapter: adapter to change the status
  1335. * @mask: status bits to change
  1336. *
  1337. * Changes in common status bits are propagated to attached ports and LUNs.
  1338. */
  1339. void zfcp_erp_set_adapter_status(struct zfcp_adapter *adapter, u32 mask)
  1340. {
  1341. struct zfcp_port *port;
  1342. struct scsi_device *sdev;
  1343. unsigned long flags;
  1344. u32 common_mask = mask & ZFCP_COMMON_FLAGS;
  1345. atomic_or(mask, &adapter->status);
  1346. if (!common_mask)
  1347. return;
  1348. read_lock_irqsave(&adapter->port_list_lock, flags);
  1349. list_for_each_entry(port, &adapter->port_list, list)
  1350. atomic_or(common_mask, &port->status);
  1351. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  1352. spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
  1353. __shost_for_each_device(sdev, adapter->scsi_host)
  1354. atomic_or(common_mask, &sdev_to_zfcp(sdev)->status);
  1355. spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
  1356. }
  1357. /**
  1358. * zfcp_erp_clear_adapter_status - clear adapter status bits
  1359. * @adapter: adapter to change the status
  1360. * @mask: status bits to change
  1361. *
  1362. * Changes in common status bits are propagated to attached ports and LUNs.
  1363. */
  1364. void zfcp_erp_clear_adapter_status(struct zfcp_adapter *adapter, u32 mask)
  1365. {
  1366. struct zfcp_port *port;
  1367. struct scsi_device *sdev;
  1368. unsigned long flags;
  1369. u32 common_mask = mask & ZFCP_COMMON_FLAGS;
  1370. u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
  1371. atomic_andnot(mask, &adapter->status);
  1372. if (!common_mask)
  1373. return;
  1374. if (clear_counter)
  1375. atomic_set(&adapter->erp_counter, 0);
  1376. read_lock_irqsave(&adapter->port_list_lock, flags);
  1377. list_for_each_entry(port, &adapter->port_list, list) {
  1378. atomic_andnot(common_mask, &port->status);
  1379. if (clear_counter)
  1380. atomic_set(&port->erp_counter, 0);
  1381. }
  1382. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  1383. spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
  1384. __shost_for_each_device(sdev, adapter->scsi_host) {
  1385. atomic_andnot(common_mask, &sdev_to_zfcp(sdev)->status);
  1386. if (clear_counter)
  1387. atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
  1388. }
  1389. spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
  1390. }
  1391. /**
  1392. * zfcp_erp_set_port_status - set port status bits
  1393. * @port: port to change the status
  1394. * @mask: status bits to change
  1395. *
  1396. * Changes in common status bits are propagated to attached LUNs.
  1397. */
  1398. void zfcp_erp_set_port_status(struct zfcp_port *port, u32 mask)
  1399. {
  1400. struct scsi_device *sdev;
  1401. u32 common_mask = mask & ZFCP_COMMON_FLAGS;
  1402. unsigned long flags;
  1403. atomic_or(mask, &port->status);
  1404. if (!common_mask)
  1405. return;
  1406. spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
  1407. __shost_for_each_device(sdev, port->adapter->scsi_host)
  1408. if (sdev_to_zfcp(sdev)->port == port)
  1409. atomic_or(common_mask,
  1410. &sdev_to_zfcp(sdev)->status);
  1411. spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
  1412. }
  1413. /**
  1414. * zfcp_erp_clear_port_status - clear port status bits
  1415. * @port: adapter to change the status
  1416. * @mask: status bits to change
  1417. *
  1418. * Changes in common status bits are propagated to attached LUNs.
  1419. */
  1420. void zfcp_erp_clear_port_status(struct zfcp_port *port, u32 mask)
  1421. {
  1422. struct scsi_device *sdev;
  1423. u32 common_mask = mask & ZFCP_COMMON_FLAGS;
  1424. u32 clear_counter = mask & ZFCP_STATUS_COMMON_ERP_FAILED;
  1425. unsigned long flags;
  1426. atomic_andnot(mask, &port->status);
  1427. if (!common_mask)
  1428. return;
  1429. if (clear_counter)
  1430. atomic_set(&port->erp_counter, 0);
  1431. spin_lock_irqsave(port->adapter->scsi_host->host_lock, flags);
  1432. __shost_for_each_device(sdev, port->adapter->scsi_host)
  1433. if (sdev_to_zfcp(sdev)->port == port) {
  1434. atomic_andnot(common_mask,
  1435. &sdev_to_zfcp(sdev)->status);
  1436. if (clear_counter)
  1437. atomic_set(&sdev_to_zfcp(sdev)->erp_counter, 0);
  1438. }
  1439. spin_unlock_irqrestore(port->adapter->scsi_host->host_lock, flags);
  1440. }
  1441. /**
  1442. * zfcp_erp_set_lun_status - set lun status bits
  1443. * @sdev: SCSI device / lun to set the status bits
  1444. * @mask: status bits to change
  1445. */
  1446. void zfcp_erp_set_lun_status(struct scsi_device *sdev, u32 mask)
  1447. {
  1448. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  1449. atomic_or(mask, &zfcp_sdev->status);
  1450. }
  1451. /**
  1452. * zfcp_erp_clear_lun_status - clear lun status bits
  1453. * @sdev: SCSi device / lun to clear the status bits
  1454. * @mask: status bits to change
  1455. */
  1456. void zfcp_erp_clear_lun_status(struct scsi_device *sdev, u32 mask)
  1457. {
  1458. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  1459. atomic_andnot(mask, &zfcp_sdev->status);
  1460. if (mask & ZFCP_STATUS_COMMON_ERP_FAILED)
  1461. atomic_set(&zfcp_sdev->erp_counter, 0);
  1462. }