dasd_alias.c 29 KB

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