dasd_alias.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * PAV alias management for the DASD ECKD discipline
  3. *
  4. * Copyright IBM Corporation, 2007
  5. * Author(s): Stefan Weinhuber <wein@de.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "dasd-eckd"
  8. #include <linux/list.h>
  9. #include <asm/ebcdic.h>
  10. #include "dasd_int.h"
  11. #include "dasd_eckd.h"
  12. #ifdef PRINTK_HEADER
  13. #undef PRINTK_HEADER
  14. #endif /* PRINTK_HEADER */
  15. #define PRINTK_HEADER "dasd(eckd):"
  16. /*
  17. * General concept of alias management:
  18. * - PAV and DASD alias management is specific to the eckd discipline.
  19. * - A device is connected to an lcu as long as the device exists.
  20. * dasd_alias_make_device_known_to_lcu will be called wenn the
  21. * device is checked by the eckd discipline and
  22. * dasd_alias_disconnect_device_from_lcu will be called
  23. * before the device is deleted.
  24. * - The dasd_alias_add_device / dasd_alias_remove_device
  25. * functions mark the point when a device is 'ready for service'.
  26. * - A summary unit check is a rare occasion, but it is mandatory to
  27. * support it. It requires some complex recovery actions before the
  28. * devices can be used again (see dasd_alias_handle_summary_unit_check).
  29. * - dasd_alias_get_start_dev will find an alias device that can be used
  30. * instead of the base device and does some (very simple) load balancing.
  31. * This is the function that gets called for each I/O, so when improving
  32. * something, this function should get faster or better, the rest has just
  33. * to be correct.
  34. */
  35. static void summary_unit_check_handling_work(struct work_struct *);
  36. static void lcu_update_work(struct work_struct *);
  37. static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
  38. static struct alias_root aliastree = {
  39. .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
  40. .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
  41. };
  42. static struct alias_server *_find_server(struct dasd_uid *uid)
  43. {
  44. struct alias_server *pos;
  45. list_for_each_entry(pos, &aliastree.serverlist, server) {
  46. if (!strncmp(pos->uid.vendor, uid->vendor,
  47. sizeof(uid->vendor))
  48. && !strncmp(pos->uid.serial, uid->serial,
  49. sizeof(uid->serial)))
  50. return pos;
  51. };
  52. return NULL;
  53. }
  54. static struct alias_lcu *_find_lcu(struct alias_server *server,
  55. struct dasd_uid *uid)
  56. {
  57. struct alias_lcu *pos;
  58. list_for_each_entry(pos, &server->lculist, lcu) {
  59. if (pos->uid.ssid == uid->ssid)
  60. return pos;
  61. };
  62. return NULL;
  63. }
  64. static struct alias_pav_group *_find_group(struct alias_lcu *lcu,
  65. struct dasd_uid *uid)
  66. {
  67. struct alias_pav_group *pos;
  68. __u8 search_unit_addr;
  69. /* for hyper pav there is only one group */
  70. if (lcu->pav == HYPER_PAV) {
  71. if (list_empty(&lcu->grouplist))
  72. return NULL;
  73. else
  74. return list_first_entry(&lcu->grouplist,
  75. struct alias_pav_group, group);
  76. }
  77. /* for base pav we have to find the group that matches the base */
  78. if (uid->type == UA_BASE_DEVICE)
  79. search_unit_addr = uid->real_unit_addr;
  80. else
  81. search_unit_addr = uid->base_unit_addr;
  82. list_for_each_entry(pos, &lcu->grouplist, group) {
  83. if (pos->uid.base_unit_addr == search_unit_addr &&
  84. !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
  85. return pos;
  86. };
  87. return NULL;
  88. }
  89. static struct alias_server *_allocate_server(struct dasd_uid *uid)
  90. {
  91. struct alias_server *server;
  92. server = kzalloc(sizeof(*server), GFP_KERNEL);
  93. if (!server)
  94. return ERR_PTR(-ENOMEM);
  95. memcpy(server->uid.vendor, uid->vendor, sizeof(uid->vendor));
  96. memcpy(server->uid.serial, uid->serial, sizeof(uid->serial));
  97. INIT_LIST_HEAD(&server->server);
  98. INIT_LIST_HEAD(&server->lculist);
  99. return server;
  100. }
  101. static void _free_server(struct alias_server *server)
  102. {
  103. kfree(server);
  104. }
  105. static struct alias_lcu *_allocate_lcu(struct dasd_uid *uid)
  106. {
  107. struct alias_lcu *lcu;
  108. lcu = kzalloc(sizeof(*lcu), GFP_KERNEL);
  109. if (!lcu)
  110. return ERR_PTR(-ENOMEM);
  111. lcu->uac = kzalloc(sizeof(*(lcu->uac)), GFP_KERNEL | GFP_DMA);
  112. if (!lcu->uac)
  113. goto out_err1;
  114. lcu->rsu_cqr = kzalloc(sizeof(*lcu->rsu_cqr), GFP_KERNEL | GFP_DMA);
  115. if (!lcu->rsu_cqr)
  116. goto out_err2;
  117. lcu->rsu_cqr->cpaddr = kzalloc(sizeof(struct ccw1),
  118. GFP_KERNEL | GFP_DMA);
  119. if (!lcu->rsu_cqr->cpaddr)
  120. goto out_err3;
  121. lcu->rsu_cqr->data = kzalloc(16, GFP_KERNEL | GFP_DMA);
  122. if (!lcu->rsu_cqr->data)
  123. goto out_err4;
  124. memcpy(lcu->uid.vendor, uid->vendor, sizeof(uid->vendor));
  125. memcpy(lcu->uid.serial, uid->serial, sizeof(uid->serial));
  126. lcu->uid.ssid = uid->ssid;
  127. lcu->pav = NO_PAV;
  128. lcu->flags = NEED_UAC_UPDATE | UPDATE_PENDING;
  129. INIT_LIST_HEAD(&lcu->lcu);
  130. INIT_LIST_HEAD(&lcu->inactive_devices);
  131. INIT_LIST_HEAD(&lcu->active_devices);
  132. INIT_LIST_HEAD(&lcu->grouplist);
  133. INIT_WORK(&lcu->suc_data.worker, summary_unit_check_handling_work);
  134. INIT_DELAYED_WORK(&lcu->ruac_data.dwork, lcu_update_work);
  135. spin_lock_init(&lcu->lock);
  136. init_completion(&lcu->lcu_setup);
  137. return lcu;
  138. out_err4:
  139. kfree(lcu->rsu_cqr->cpaddr);
  140. out_err3:
  141. kfree(lcu->rsu_cqr);
  142. out_err2:
  143. kfree(lcu->uac);
  144. out_err1:
  145. kfree(lcu);
  146. return ERR_PTR(-ENOMEM);
  147. }
  148. static void _free_lcu(struct alias_lcu *lcu)
  149. {
  150. kfree(lcu->rsu_cqr->data);
  151. kfree(lcu->rsu_cqr->cpaddr);
  152. kfree(lcu->rsu_cqr);
  153. kfree(lcu->uac);
  154. kfree(lcu);
  155. }
  156. /*
  157. * This is the function that will allocate all the server and lcu data,
  158. * so this function must be called first for a new device.
  159. * If the return value is 1, the lcu was already known before, if it
  160. * is 0, this is a new lcu.
  161. * Negative return code indicates that something went wrong (e.g. -ENOMEM)
  162. */
  163. int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
  164. {
  165. struct dasd_eckd_private *private;
  166. unsigned long flags;
  167. struct alias_server *server, *newserver;
  168. struct alias_lcu *lcu, *newlcu;
  169. int is_lcu_known;
  170. struct dasd_uid *uid;
  171. private = (struct dasd_eckd_private *) device->private;
  172. uid = &private->uid;
  173. spin_lock_irqsave(&aliastree.lock, flags);
  174. is_lcu_known = 1;
  175. server = _find_server(uid);
  176. if (!server) {
  177. spin_unlock_irqrestore(&aliastree.lock, flags);
  178. newserver = _allocate_server(uid);
  179. if (IS_ERR(newserver))
  180. return PTR_ERR(newserver);
  181. spin_lock_irqsave(&aliastree.lock, flags);
  182. server = _find_server(uid);
  183. if (!server) {
  184. list_add(&newserver->server, &aliastree.serverlist);
  185. server = newserver;
  186. is_lcu_known = 0;
  187. } else {
  188. /* someone was faster */
  189. _free_server(newserver);
  190. }
  191. }
  192. lcu = _find_lcu(server, uid);
  193. if (!lcu) {
  194. spin_unlock_irqrestore(&aliastree.lock, flags);
  195. newlcu = _allocate_lcu(uid);
  196. if (IS_ERR(newlcu))
  197. return PTR_ERR(newlcu);
  198. spin_lock_irqsave(&aliastree.lock, flags);
  199. lcu = _find_lcu(server, uid);
  200. if (!lcu) {
  201. list_add(&newlcu->lcu, &server->lculist);
  202. lcu = newlcu;
  203. is_lcu_known = 0;
  204. } else {
  205. /* someone was faster */
  206. _free_lcu(newlcu);
  207. }
  208. is_lcu_known = 0;
  209. }
  210. spin_lock(&lcu->lock);
  211. list_add(&device->alias_list, &lcu->inactive_devices);
  212. private->lcu = lcu;
  213. spin_unlock(&lcu->lock);
  214. spin_unlock_irqrestore(&aliastree.lock, flags);
  215. return is_lcu_known;
  216. }
  217. /*
  218. * The first device to be registered on an LCU will have to do
  219. * some additional setup steps to configure that LCU on the
  220. * storage server. All further devices should wait with their
  221. * initialization until the first device is done.
  222. * To synchronize this work, the first device will call
  223. * dasd_alias_lcu_setup_complete when it is done, and all
  224. * other devices will wait for it with dasd_alias_wait_for_lcu_setup.
  225. */
  226. void dasd_alias_lcu_setup_complete(struct dasd_device *device)
  227. {
  228. struct dasd_eckd_private *private;
  229. unsigned long flags;
  230. struct alias_server *server;
  231. struct alias_lcu *lcu;
  232. struct dasd_uid *uid;
  233. private = (struct dasd_eckd_private *) device->private;
  234. uid = &private->uid;
  235. lcu = NULL;
  236. spin_lock_irqsave(&aliastree.lock, flags);
  237. server = _find_server(uid);
  238. if (server)
  239. lcu = _find_lcu(server, uid);
  240. spin_unlock_irqrestore(&aliastree.lock, flags);
  241. if (!lcu) {
  242. DBF_EVENT_DEVID(DBF_ERR, device->cdev,
  243. "could not find lcu for %04x %02x",
  244. uid->ssid, uid->real_unit_addr);
  245. WARN_ON(1);
  246. return;
  247. }
  248. complete_all(&lcu->lcu_setup);
  249. }
  250. void dasd_alias_wait_for_lcu_setup(struct dasd_device *device)
  251. {
  252. struct dasd_eckd_private *private;
  253. unsigned long flags;
  254. struct alias_server *server;
  255. struct alias_lcu *lcu;
  256. struct dasd_uid *uid;
  257. private = (struct dasd_eckd_private *) device->private;
  258. uid = &private->uid;
  259. lcu = NULL;
  260. spin_lock_irqsave(&aliastree.lock, flags);
  261. server = _find_server(uid);
  262. if (server)
  263. lcu = _find_lcu(server, uid);
  264. spin_unlock_irqrestore(&aliastree.lock, flags);
  265. if (!lcu) {
  266. DBF_EVENT_DEVID(DBF_ERR, device->cdev,
  267. "could not find lcu for %04x %02x",
  268. uid->ssid, uid->real_unit_addr);
  269. WARN_ON(1);
  270. return;
  271. }
  272. wait_for_completion(&lcu->lcu_setup);
  273. }
  274. /*
  275. * This function removes a device from the scope of alias management.
  276. * The complicated part is to make sure that it is not in use by
  277. * any of the workers. If necessary cancel the work.
  278. */
  279. void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
  280. {
  281. struct dasd_eckd_private *private;
  282. unsigned long flags;
  283. struct alias_lcu *lcu;
  284. struct alias_server *server;
  285. int was_pending;
  286. private = (struct dasd_eckd_private *) device->private;
  287. lcu = private->lcu;
  288. spin_lock_irqsave(&lcu->lock, flags);
  289. list_del_init(&device->alias_list);
  290. /* make sure that the workers don't use this device */
  291. if (device == lcu->suc_data.device) {
  292. spin_unlock_irqrestore(&lcu->lock, flags);
  293. cancel_work_sync(&lcu->suc_data.worker);
  294. spin_lock_irqsave(&lcu->lock, flags);
  295. if (device == lcu->suc_data.device)
  296. lcu->suc_data.device = NULL;
  297. }
  298. was_pending = 0;
  299. if (device == lcu->ruac_data.device) {
  300. spin_unlock_irqrestore(&lcu->lock, flags);
  301. was_pending = 1;
  302. cancel_delayed_work_sync(&lcu->ruac_data.dwork);
  303. spin_lock_irqsave(&lcu->lock, flags);
  304. if (device == lcu->ruac_data.device)
  305. lcu->ruac_data.device = NULL;
  306. }
  307. private->lcu = NULL;
  308. spin_unlock_irqrestore(&lcu->lock, flags);
  309. spin_lock_irqsave(&aliastree.lock, flags);
  310. spin_lock(&lcu->lock);
  311. if (list_empty(&lcu->grouplist) &&
  312. list_empty(&lcu->active_devices) &&
  313. list_empty(&lcu->inactive_devices)) {
  314. list_del(&lcu->lcu);
  315. spin_unlock(&lcu->lock);
  316. _free_lcu(lcu);
  317. lcu = NULL;
  318. } else {
  319. if (was_pending)
  320. _schedule_lcu_update(lcu, NULL);
  321. spin_unlock(&lcu->lock);
  322. }
  323. server = _find_server(&private->uid);
  324. if (server && list_empty(&server->lculist)) {
  325. list_del(&server->server);
  326. _free_server(server);
  327. }
  328. spin_unlock_irqrestore(&aliastree.lock, flags);
  329. }
  330. /*
  331. * This function assumes that the unit address configuration stored
  332. * in the lcu is up to date and will update the device uid before
  333. * adding it to a pav group.
  334. */
  335. static int _add_device_to_lcu(struct alias_lcu *lcu,
  336. struct dasd_device *device)
  337. {
  338. struct dasd_eckd_private *private;
  339. struct alias_pav_group *group;
  340. struct dasd_uid *uid;
  341. private = (struct dasd_eckd_private *) device->private;
  342. uid = &private->uid;
  343. uid->type = lcu->uac->unit[uid->real_unit_addr].ua_type;
  344. uid->base_unit_addr = lcu->uac->unit[uid->real_unit_addr].base_ua;
  345. dasd_set_uid(device->cdev, &private->uid);
  346. /* if we have no PAV anyway, we don't need to bother with PAV groups */
  347. if (lcu->pav == NO_PAV) {
  348. list_move(&device->alias_list, &lcu->active_devices);
  349. return 0;
  350. }
  351. group = _find_group(lcu, uid);
  352. if (!group) {
  353. group = kzalloc(sizeof(*group), GFP_ATOMIC);
  354. if (!group)
  355. return -ENOMEM;
  356. memcpy(group->uid.vendor, uid->vendor, sizeof(uid->vendor));
  357. memcpy(group->uid.serial, uid->serial, sizeof(uid->serial));
  358. group->uid.ssid = uid->ssid;
  359. if (uid->type == UA_BASE_DEVICE)
  360. group->uid.base_unit_addr = uid->real_unit_addr;
  361. else
  362. group->uid.base_unit_addr = uid->base_unit_addr;
  363. memcpy(group->uid.vduit, uid->vduit, sizeof(uid->vduit));
  364. INIT_LIST_HEAD(&group->group);
  365. INIT_LIST_HEAD(&group->baselist);
  366. INIT_LIST_HEAD(&group->aliaslist);
  367. list_add(&group->group, &lcu->grouplist);
  368. }
  369. if (uid->type == UA_BASE_DEVICE)
  370. list_move(&device->alias_list, &group->baselist);
  371. else
  372. list_move(&device->alias_list, &group->aliaslist);
  373. private->pavgroup = group;
  374. return 0;
  375. };
  376. static void _remove_device_from_lcu(struct alias_lcu *lcu,
  377. struct dasd_device *device)
  378. {
  379. struct dasd_eckd_private *private;
  380. struct alias_pav_group *group;
  381. private = (struct dasd_eckd_private *) device->private;
  382. list_move(&device->alias_list, &lcu->inactive_devices);
  383. group = private->pavgroup;
  384. if (!group)
  385. return;
  386. private->pavgroup = NULL;
  387. if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
  388. list_del(&group->group);
  389. kfree(group);
  390. return;
  391. }
  392. if (group->next == device)
  393. group->next = NULL;
  394. };
  395. static int read_unit_address_configuration(struct dasd_device *device,
  396. struct alias_lcu *lcu)
  397. {
  398. struct dasd_psf_prssd_data *prssdp;
  399. struct dasd_ccw_req *cqr;
  400. struct ccw1 *ccw;
  401. int rc;
  402. unsigned long flags;
  403. cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
  404. (sizeof(struct dasd_psf_prssd_data)),
  405. device);
  406. if (IS_ERR(cqr))
  407. return PTR_ERR(cqr);
  408. cqr->startdev = device;
  409. cqr->memdev = device;
  410. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  411. cqr->retries = 10;
  412. cqr->expires = 20 * HZ;
  413. /* Prepare for Read Subsystem Data */
  414. prssdp = (struct dasd_psf_prssd_data *) cqr->data;
  415. memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
  416. prssdp->order = PSF_ORDER_PRSSD;
  417. prssdp->suborder = 0x0e; /* Read unit address configuration */
  418. /* all other bytes of prssdp must be zero */
  419. ccw = cqr->cpaddr;
  420. ccw->cmd_code = DASD_ECKD_CCW_PSF;
  421. ccw->count = sizeof(struct dasd_psf_prssd_data);
  422. ccw->flags |= CCW_FLAG_CC;
  423. ccw->cda = (__u32)(addr_t) prssdp;
  424. /* Read Subsystem Data - feature codes */
  425. memset(lcu->uac, 0, sizeof(*(lcu->uac)));
  426. ccw++;
  427. ccw->cmd_code = DASD_ECKD_CCW_RSSD;
  428. ccw->count = sizeof(*(lcu->uac));
  429. ccw->cda = (__u32)(addr_t) lcu->uac;
  430. cqr->buildclk = get_clock();
  431. cqr->status = DASD_CQR_FILLED;
  432. /* need to unset flag here to detect race with summary unit check */
  433. spin_lock_irqsave(&lcu->lock, flags);
  434. lcu->flags &= ~NEED_UAC_UPDATE;
  435. spin_unlock_irqrestore(&lcu->lock, flags);
  436. do {
  437. rc = dasd_sleep_on(cqr);
  438. } while (rc && (cqr->retries > 0));
  439. if (rc) {
  440. spin_lock_irqsave(&lcu->lock, flags);
  441. lcu->flags |= NEED_UAC_UPDATE;
  442. spin_unlock_irqrestore(&lcu->lock, flags);
  443. }
  444. dasd_kfree_request(cqr, cqr->memdev);
  445. return rc;
  446. }
  447. static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
  448. {
  449. unsigned long flags;
  450. struct alias_pav_group *pavgroup, *tempgroup;
  451. struct dasd_device *device, *tempdev;
  452. int i, rc;
  453. struct dasd_eckd_private *private;
  454. spin_lock_irqsave(&lcu->lock, flags);
  455. list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
  456. list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
  457. alias_list) {
  458. list_move(&device->alias_list, &lcu->active_devices);
  459. private = (struct dasd_eckd_private *) device->private;
  460. private->pavgroup = NULL;
  461. }
  462. list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
  463. alias_list) {
  464. list_move(&device->alias_list, &lcu->active_devices);
  465. private = (struct dasd_eckd_private *) device->private;
  466. private->pavgroup = NULL;
  467. }
  468. list_del(&pavgroup->group);
  469. kfree(pavgroup);
  470. }
  471. spin_unlock_irqrestore(&lcu->lock, flags);
  472. rc = read_unit_address_configuration(refdev, lcu);
  473. if (rc)
  474. return rc;
  475. spin_lock_irqsave(&lcu->lock, flags);
  476. lcu->pav = NO_PAV;
  477. for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
  478. switch (lcu->uac->unit[i].ua_type) {
  479. case UA_BASE_PAV_ALIAS:
  480. lcu->pav = BASE_PAV;
  481. break;
  482. case UA_HYPER_PAV_ALIAS:
  483. lcu->pav = HYPER_PAV;
  484. break;
  485. }
  486. if (lcu->pav != NO_PAV)
  487. break;
  488. }
  489. list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
  490. alias_list) {
  491. _add_device_to_lcu(lcu, device);
  492. }
  493. spin_unlock_irqrestore(&lcu->lock, flags);
  494. return 0;
  495. }
  496. static void lcu_update_work(struct work_struct *work)
  497. {
  498. struct alias_lcu *lcu;
  499. struct read_uac_work_data *ruac_data;
  500. struct dasd_device *device;
  501. unsigned long flags;
  502. int rc;
  503. ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
  504. lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
  505. device = ruac_data->device;
  506. rc = _lcu_update(device, lcu);
  507. /*
  508. * Need to check flags again, as there could have been another
  509. * prepare_update or a new device a new device while we were still
  510. * processing the data
  511. */
  512. spin_lock_irqsave(&lcu->lock, flags);
  513. if (rc || (lcu->flags & NEED_UAC_UPDATE)) {
  514. DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
  515. " alias data in lcu (rc = %d), retry later", rc);
  516. schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ);
  517. } else {
  518. lcu->ruac_data.device = NULL;
  519. lcu->flags &= ~UPDATE_PENDING;
  520. }
  521. spin_unlock_irqrestore(&lcu->lock, flags);
  522. }
  523. static int _schedule_lcu_update(struct alias_lcu *lcu,
  524. struct dasd_device *device)
  525. {
  526. struct dasd_device *usedev = NULL;
  527. struct alias_pav_group *group;
  528. lcu->flags |= NEED_UAC_UPDATE;
  529. if (lcu->ruac_data.device) {
  530. /* already scheduled or running */
  531. return 0;
  532. }
  533. if (device && !list_empty(&device->alias_list))
  534. usedev = device;
  535. if (!usedev && !list_empty(&lcu->grouplist)) {
  536. group = list_first_entry(&lcu->grouplist,
  537. struct alias_pav_group, group);
  538. if (!list_empty(&group->baselist))
  539. usedev = list_first_entry(&group->baselist,
  540. struct dasd_device,
  541. alias_list);
  542. else if (!list_empty(&group->aliaslist))
  543. usedev = list_first_entry(&group->aliaslist,
  544. struct dasd_device,
  545. alias_list);
  546. }
  547. if (!usedev && !list_empty(&lcu->active_devices)) {
  548. usedev = list_first_entry(&lcu->active_devices,
  549. struct dasd_device, alias_list);
  550. }
  551. /*
  552. * if we haven't found a proper device yet, give up for now, the next
  553. * device that will be set active will trigger an lcu update
  554. */
  555. if (!usedev)
  556. return -EINVAL;
  557. lcu->ruac_data.device = usedev;
  558. schedule_delayed_work(&lcu->ruac_data.dwork, 0);
  559. return 0;
  560. }
  561. int dasd_alias_add_device(struct dasd_device *device)
  562. {
  563. struct dasd_eckd_private *private;
  564. struct alias_lcu *lcu;
  565. unsigned long flags;
  566. int rc;
  567. private = (struct dasd_eckd_private *) device->private;
  568. lcu = private->lcu;
  569. rc = 0;
  570. spin_lock_irqsave(&lcu->lock, flags);
  571. if (!(lcu->flags & UPDATE_PENDING)) {
  572. rc = _add_device_to_lcu(lcu, device);
  573. if (rc)
  574. lcu->flags |= UPDATE_PENDING;
  575. }
  576. if (lcu->flags & UPDATE_PENDING) {
  577. list_move(&device->alias_list, &lcu->active_devices);
  578. _schedule_lcu_update(lcu, device);
  579. }
  580. spin_unlock_irqrestore(&lcu->lock, flags);
  581. return rc;
  582. }
  583. int dasd_alias_remove_device(struct dasd_device *device)
  584. {
  585. struct dasd_eckd_private *private;
  586. struct alias_lcu *lcu;
  587. unsigned long flags;
  588. private = (struct dasd_eckd_private *) device->private;
  589. lcu = private->lcu;
  590. spin_lock_irqsave(&lcu->lock, flags);
  591. _remove_device_from_lcu(lcu, device);
  592. spin_unlock_irqrestore(&lcu->lock, flags);
  593. return 0;
  594. }
  595. struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
  596. {
  597. struct dasd_device *alias_device;
  598. struct alias_pav_group *group;
  599. struct alias_lcu *lcu;
  600. struct dasd_eckd_private *private, *alias_priv;
  601. unsigned long flags;
  602. private = (struct dasd_eckd_private *) base_device->private;
  603. group = private->pavgroup;
  604. lcu = private->lcu;
  605. if (!group || !lcu)
  606. return NULL;
  607. if (lcu->pav == NO_PAV ||
  608. lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
  609. return NULL;
  610. spin_lock_irqsave(&lcu->lock, flags);
  611. alias_device = group->next;
  612. if (!alias_device) {
  613. if (list_empty(&group->aliaslist)) {
  614. spin_unlock_irqrestore(&lcu->lock, flags);
  615. return NULL;
  616. } else {
  617. alias_device = list_first_entry(&group->aliaslist,
  618. struct dasd_device,
  619. alias_list);
  620. }
  621. }
  622. if (list_is_last(&alias_device->alias_list, &group->aliaslist))
  623. group->next = list_first_entry(&group->aliaslist,
  624. struct dasd_device, alias_list);
  625. else
  626. group->next = list_first_entry(&alias_device->alias_list,
  627. struct dasd_device, alias_list);
  628. spin_unlock_irqrestore(&lcu->lock, flags);
  629. alias_priv = (struct dasd_eckd_private *) alias_device->private;
  630. if ((alias_priv->count < private->count) && !alias_device->stopped)
  631. return alias_device;
  632. else
  633. return NULL;
  634. }
  635. /*
  636. * Summary unit check handling depends on the way alias devices
  637. * are handled so it is done here rather then in dasd_eckd.c
  638. */
  639. static int reset_summary_unit_check(struct alias_lcu *lcu,
  640. struct dasd_device *device,
  641. char reason)
  642. {
  643. struct dasd_ccw_req *cqr;
  644. int rc = 0;
  645. struct ccw1 *ccw;
  646. cqr = lcu->rsu_cqr;
  647. strncpy((char *) &cqr->magic, "ECKD", 4);
  648. ASCEBC((char *) &cqr->magic, 4);
  649. ccw = cqr->cpaddr;
  650. ccw->cmd_code = DASD_ECKD_CCW_RSCK;
  651. ccw->flags = 0 ;
  652. ccw->count = 16;
  653. ccw->cda = (__u32)(addr_t) cqr->data;
  654. ((char *)cqr->data)[0] = reason;
  655. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  656. cqr->retries = 255; /* set retry counter to enable basic ERP */
  657. cqr->startdev = device;
  658. cqr->memdev = device;
  659. cqr->block = NULL;
  660. cqr->expires = 5 * HZ;
  661. cqr->buildclk = get_clock();
  662. cqr->status = DASD_CQR_FILLED;
  663. rc = dasd_sleep_on_immediatly(cqr);
  664. return rc;
  665. }
  666. static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
  667. {
  668. struct alias_pav_group *pavgroup;
  669. struct dasd_device *device;
  670. struct dasd_eckd_private *private;
  671. /* active and inactive list can contain alias as well as base devices */
  672. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  673. private = (struct dasd_eckd_private *) device->private;
  674. if (private->uid.type != UA_BASE_DEVICE)
  675. continue;
  676. dasd_schedule_block_bh(device->block);
  677. dasd_schedule_device_bh(device);
  678. }
  679. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  680. private = (struct dasd_eckd_private *) device->private;
  681. if (private->uid.type != UA_BASE_DEVICE)
  682. continue;
  683. dasd_schedule_block_bh(device->block);
  684. dasd_schedule_device_bh(device);
  685. }
  686. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  687. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  688. dasd_schedule_block_bh(device->block);
  689. dasd_schedule_device_bh(device);
  690. }
  691. }
  692. }
  693. static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
  694. {
  695. struct alias_pav_group *pavgroup;
  696. struct dasd_device *device, *temp;
  697. struct dasd_eckd_private *private;
  698. int rc;
  699. unsigned long flags;
  700. LIST_HEAD(active);
  701. /*
  702. * Problem here ist that dasd_flush_device_queue may wait
  703. * for termination of a request to complete. We can't keep
  704. * the lcu lock during that time, so we must assume that
  705. * the lists may have changed.
  706. * Idea: first gather all active alias devices in a separate list,
  707. * then flush the first element of this list unlocked, and afterwards
  708. * check if it is still on the list before moving it to the
  709. * active_devices list.
  710. */
  711. spin_lock_irqsave(&lcu->lock, flags);
  712. list_for_each_entry_safe(device, temp, &lcu->active_devices,
  713. alias_list) {
  714. private = (struct dasd_eckd_private *) device->private;
  715. if (private->uid.type == UA_BASE_DEVICE)
  716. continue;
  717. list_move(&device->alias_list, &active);
  718. }
  719. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  720. list_splice_init(&pavgroup->aliaslist, &active);
  721. }
  722. while (!list_empty(&active)) {
  723. device = list_first_entry(&active, struct dasd_device,
  724. alias_list);
  725. spin_unlock_irqrestore(&lcu->lock, flags);
  726. rc = dasd_flush_device_queue(device);
  727. spin_lock_irqsave(&lcu->lock, flags);
  728. /*
  729. * only move device around if it wasn't moved away while we
  730. * were waiting for the flush
  731. */
  732. if (device == list_first_entry(&active,
  733. struct dasd_device, alias_list))
  734. list_move(&device->alias_list, &lcu->active_devices);
  735. }
  736. spin_unlock_irqrestore(&lcu->lock, flags);
  737. }
  738. static void __stop_device_on_lcu(struct dasd_device *device,
  739. struct dasd_device *pos)
  740. {
  741. /* If pos == device then device is already locked! */
  742. if (pos == device) {
  743. dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
  744. return;
  745. }
  746. spin_lock(get_ccwdev_lock(pos->cdev));
  747. dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
  748. spin_unlock(get_ccwdev_lock(pos->cdev));
  749. }
  750. /*
  751. * This function is called in interrupt context, so the
  752. * cdev lock for device is already locked!
  753. */
  754. static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
  755. struct dasd_device *device)
  756. {
  757. struct alias_pav_group *pavgroup;
  758. struct dasd_device *pos;
  759. list_for_each_entry(pos, &lcu->active_devices, alias_list)
  760. __stop_device_on_lcu(device, pos);
  761. list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
  762. __stop_device_on_lcu(device, pos);
  763. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  764. list_for_each_entry(pos, &pavgroup->baselist, alias_list)
  765. __stop_device_on_lcu(device, pos);
  766. list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
  767. __stop_device_on_lcu(device, pos);
  768. }
  769. }
  770. static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
  771. {
  772. struct alias_pav_group *pavgroup;
  773. struct dasd_device *device;
  774. unsigned long flags;
  775. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  776. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  777. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  778. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  779. }
  780. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  781. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  782. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  783. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  784. }
  785. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  786. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  787. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  788. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  789. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  790. flags);
  791. }
  792. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  793. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  794. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  795. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  796. flags);
  797. }
  798. }
  799. }
  800. static void summary_unit_check_handling_work(struct work_struct *work)
  801. {
  802. struct alias_lcu *lcu;
  803. struct summary_unit_check_work_data *suc_data;
  804. unsigned long flags;
  805. struct dasd_device *device;
  806. suc_data = container_of(work, struct summary_unit_check_work_data,
  807. worker);
  808. lcu = container_of(suc_data, struct alias_lcu, suc_data);
  809. device = suc_data->device;
  810. /* 1. flush alias devices */
  811. flush_all_alias_devices_on_lcu(lcu);
  812. /* 2. reset summary unit check */
  813. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  814. dasd_device_remove_stop_bits(device,
  815. (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
  816. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  817. reset_summary_unit_check(lcu, device, suc_data->reason);
  818. spin_lock_irqsave(&lcu->lock, flags);
  819. _unstop_all_devices_on_lcu(lcu);
  820. _restart_all_base_devices_on_lcu(lcu);
  821. /* 3. read new alias configuration */
  822. _schedule_lcu_update(lcu, device);
  823. lcu->suc_data.device = NULL;
  824. spin_unlock_irqrestore(&lcu->lock, flags);
  825. }
  826. /*
  827. * note: this will be called from int handler context (cdev locked)
  828. */
  829. void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
  830. struct irb *irb)
  831. {
  832. struct alias_lcu *lcu;
  833. char reason;
  834. struct dasd_eckd_private *private;
  835. char *sense;
  836. private = (struct dasd_eckd_private *) device->private;
  837. sense = dasd_get_sense(irb);
  838. if (sense) {
  839. reason = sense[8];
  840. DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
  841. "eckd handle summary unit check: reason", reason);
  842. } else {
  843. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  844. "eckd handle summary unit check:"
  845. " no reason code available");
  846. return;
  847. }
  848. lcu = private->lcu;
  849. if (!lcu) {
  850. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  851. "device not ready to handle summary"
  852. " unit check (no lcu structure)");
  853. return;
  854. }
  855. spin_lock(&lcu->lock);
  856. _stop_all_devices_on_lcu(lcu, device);
  857. /* prepare for lcu_update */
  858. private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
  859. /* If this device is about to be removed just return and wait for
  860. * the next interrupt on a different device
  861. */
  862. if (list_empty(&device->alias_list)) {
  863. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  864. "device is in offline processing,"
  865. " don't do summary unit check handling");
  866. spin_unlock(&lcu->lock);
  867. return;
  868. }
  869. if (lcu->suc_data.device) {
  870. /* already scheduled or running */
  871. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  872. "previous instance of summary unit check worker"
  873. " still pending");
  874. spin_unlock(&lcu->lock);
  875. return ;
  876. }
  877. lcu->suc_data.reason = reason;
  878. lcu->suc_data.device = device;
  879. spin_unlock(&lcu->lock);
  880. schedule_work(&lcu->suc_data.worker);
  881. };