dasd_alias.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. return lcu;
  137. out_err4:
  138. kfree(lcu->rsu_cqr->cpaddr);
  139. out_err3:
  140. kfree(lcu->rsu_cqr);
  141. out_err2:
  142. kfree(lcu->uac);
  143. out_err1:
  144. kfree(lcu);
  145. return ERR_PTR(-ENOMEM);
  146. }
  147. static void _free_lcu(struct alias_lcu *lcu)
  148. {
  149. kfree(lcu->rsu_cqr->data);
  150. kfree(lcu->rsu_cqr->cpaddr);
  151. kfree(lcu->rsu_cqr);
  152. kfree(lcu->uac);
  153. kfree(lcu);
  154. }
  155. /*
  156. * This is the function that will allocate all the server and lcu data,
  157. * so this function must be called first for a new device.
  158. * If the return value is 1, the lcu was already known before, if it
  159. * is 0, this is a new lcu.
  160. * Negative return code indicates that something went wrong (e.g. -ENOMEM)
  161. */
  162. int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
  163. {
  164. struct dasd_eckd_private *private;
  165. unsigned long flags;
  166. struct alias_server *server, *newserver;
  167. struct alias_lcu *lcu, *newlcu;
  168. int is_lcu_known;
  169. struct dasd_uid *uid;
  170. private = (struct dasd_eckd_private *) device->private;
  171. uid = &private->uid;
  172. spin_lock_irqsave(&aliastree.lock, flags);
  173. is_lcu_known = 1;
  174. server = _find_server(uid);
  175. if (!server) {
  176. spin_unlock_irqrestore(&aliastree.lock, flags);
  177. newserver = _allocate_server(uid);
  178. if (IS_ERR(newserver))
  179. return PTR_ERR(newserver);
  180. spin_lock_irqsave(&aliastree.lock, flags);
  181. server = _find_server(uid);
  182. if (!server) {
  183. list_add(&newserver->server, &aliastree.serverlist);
  184. server = newserver;
  185. is_lcu_known = 0;
  186. } else {
  187. /* someone was faster */
  188. _free_server(newserver);
  189. }
  190. }
  191. lcu = _find_lcu(server, uid);
  192. if (!lcu) {
  193. spin_unlock_irqrestore(&aliastree.lock, flags);
  194. newlcu = _allocate_lcu(uid);
  195. if (IS_ERR(newlcu))
  196. return PTR_ERR(lcu);
  197. spin_lock_irqsave(&aliastree.lock, flags);
  198. lcu = _find_lcu(server, uid);
  199. if (!lcu) {
  200. list_add(&newlcu->lcu, &server->lculist);
  201. lcu = newlcu;
  202. is_lcu_known = 0;
  203. } else {
  204. /* someone was faster */
  205. _free_lcu(newlcu);
  206. }
  207. is_lcu_known = 0;
  208. }
  209. spin_lock(&lcu->lock);
  210. list_add(&device->alias_list, &lcu->inactive_devices);
  211. private->lcu = lcu;
  212. spin_unlock(&lcu->lock);
  213. spin_unlock_irqrestore(&aliastree.lock, flags);
  214. return is_lcu_known;
  215. }
  216. /*
  217. * This function removes a device from the scope of alias management.
  218. * The complicated part is to make sure that it is not in use by
  219. * any of the workers. If necessary cancel the work.
  220. */
  221. void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
  222. {
  223. struct dasd_eckd_private *private;
  224. unsigned long flags;
  225. struct alias_lcu *lcu;
  226. struct alias_server *server;
  227. int was_pending;
  228. private = (struct dasd_eckd_private *) device->private;
  229. lcu = private->lcu;
  230. spin_lock_irqsave(&lcu->lock, flags);
  231. list_del_init(&device->alias_list);
  232. /* make sure that the workers don't use this device */
  233. if (device == lcu->suc_data.device) {
  234. spin_unlock_irqrestore(&lcu->lock, flags);
  235. cancel_work_sync(&lcu->suc_data.worker);
  236. spin_lock_irqsave(&lcu->lock, flags);
  237. if (device == lcu->suc_data.device)
  238. lcu->suc_data.device = NULL;
  239. }
  240. was_pending = 0;
  241. if (device == lcu->ruac_data.device) {
  242. spin_unlock_irqrestore(&lcu->lock, flags);
  243. was_pending = 1;
  244. cancel_delayed_work_sync(&lcu->ruac_data.dwork);
  245. spin_lock_irqsave(&lcu->lock, flags);
  246. if (device == lcu->ruac_data.device)
  247. lcu->ruac_data.device = NULL;
  248. }
  249. private->lcu = NULL;
  250. spin_unlock_irqrestore(&lcu->lock, flags);
  251. spin_lock_irqsave(&aliastree.lock, flags);
  252. spin_lock(&lcu->lock);
  253. if (list_empty(&lcu->grouplist) &&
  254. list_empty(&lcu->active_devices) &&
  255. list_empty(&lcu->inactive_devices)) {
  256. list_del(&lcu->lcu);
  257. spin_unlock(&lcu->lock);
  258. _free_lcu(lcu);
  259. lcu = NULL;
  260. } else {
  261. if (was_pending)
  262. _schedule_lcu_update(lcu, NULL);
  263. spin_unlock(&lcu->lock);
  264. }
  265. server = _find_server(&private->uid);
  266. if (server && list_empty(&server->lculist)) {
  267. list_del(&server->server);
  268. _free_server(server);
  269. }
  270. spin_unlock_irqrestore(&aliastree.lock, flags);
  271. }
  272. /*
  273. * This function assumes that the unit address configuration stored
  274. * in the lcu is up to date and will update the device uid before
  275. * adding it to a pav group.
  276. */
  277. static int _add_device_to_lcu(struct alias_lcu *lcu,
  278. struct dasd_device *device)
  279. {
  280. struct dasd_eckd_private *private;
  281. struct alias_pav_group *group;
  282. struct dasd_uid *uid;
  283. private = (struct dasd_eckd_private *) device->private;
  284. uid = &private->uid;
  285. uid->type = lcu->uac->unit[uid->real_unit_addr].ua_type;
  286. uid->base_unit_addr = lcu->uac->unit[uid->real_unit_addr].base_ua;
  287. dasd_set_uid(device->cdev, &private->uid);
  288. /* if we have no PAV anyway, we don't need to bother with PAV groups */
  289. if (lcu->pav == NO_PAV) {
  290. list_move(&device->alias_list, &lcu->active_devices);
  291. return 0;
  292. }
  293. group = _find_group(lcu, uid);
  294. if (!group) {
  295. group = kzalloc(sizeof(*group), GFP_ATOMIC);
  296. if (!group)
  297. return -ENOMEM;
  298. memcpy(group->uid.vendor, uid->vendor, sizeof(uid->vendor));
  299. memcpy(group->uid.serial, uid->serial, sizeof(uid->serial));
  300. group->uid.ssid = uid->ssid;
  301. if (uid->type == UA_BASE_DEVICE)
  302. group->uid.base_unit_addr = uid->real_unit_addr;
  303. else
  304. group->uid.base_unit_addr = uid->base_unit_addr;
  305. memcpy(group->uid.vduit, uid->vduit, sizeof(uid->vduit));
  306. INIT_LIST_HEAD(&group->group);
  307. INIT_LIST_HEAD(&group->baselist);
  308. INIT_LIST_HEAD(&group->aliaslist);
  309. list_add(&group->group, &lcu->grouplist);
  310. }
  311. if (uid->type == UA_BASE_DEVICE)
  312. list_move(&device->alias_list, &group->baselist);
  313. else
  314. list_move(&device->alias_list, &group->aliaslist);
  315. private->pavgroup = group;
  316. return 0;
  317. };
  318. static void _remove_device_from_lcu(struct alias_lcu *lcu,
  319. struct dasd_device *device)
  320. {
  321. struct dasd_eckd_private *private;
  322. struct alias_pav_group *group;
  323. private = (struct dasd_eckd_private *) device->private;
  324. list_move(&device->alias_list, &lcu->inactive_devices);
  325. group = private->pavgroup;
  326. if (!group)
  327. return;
  328. private->pavgroup = NULL;
  329. if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
  330. list_del(&group->group);
  331. kfree(group);
  332. return;
  333. }
  334. if (group->next == device)
  335. group->next = NULL;
  336. };
  337. static int read_unit_address_configuration(struct dasd_device *device,
  338. struct alias_lcu *lcu)
  339. {
  340. struct dasd_psf_prssd_data *prssdp;
  341. struct dasd_ccw_req *cqr;
  342. struct ccw1 *ccw;
  343. int rc;
  344. unsigned long flags;
  345. cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
  346. (sizeof(struct dasd_psf_prssd_data)),
  347. device);
  348. if (IS_ERR(cqr))
  349. return PTR_ERR(cqr);
  350. cqr->startdev = device;
  351. cqr->memdev = device;
  352. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  353. cqr->retries = 10;
  354. cqr->expires = 20 * HZ;
  355. /* Prepare for Read Subsystem Data */
  356. prssdp = (struct dasd_psf_prssd_data *) cqr->data;
  357. memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
  358. prssdp->order = PSF_ORDER_PRSSD;
  359. prssdp->suborder = 0x0e; /* Read unit address configuration */
  360. /* all other bytes of prssdp must be zero */
  361. ccw = cqr->cpaddr;
  362. ccw->cmd_code = DASD_ECKD_CCW_PSF;
  363. ccw->count = sizeof(struct dasd_psf_prssd_data);
  364. ccw->flags |= CCW_FLAG_CC;
  365. ccw->cda = (__u32)(addr_t) prssdp;
  366. /* Read Subsystem Data - feature codes */
  367. memset(lcu->uac, 0, sizeof(*(lcu->uac)));
  368. ccw++;
  369. ccw->cmd_code = DASD_ECKD_CCW_RSSD;
  370. ccw->count = sizeof(*(lcu->uac));
  371. ccw->cda = (__u32)(addr_t) lcu->uac;
  372. cqr->buildclk = get_clock();
  373. cqr->status = DASD_CQR_FILLED;
  374. /* need to unset flag here to detect race with summary unit check */
  375. spin_lock_irqsave(&lcu->lock, flags);
  376. lcu->flags &= ~NEED_UAC_UPDATE;
  377. spin_unlock_irqrestore(&lcu->lock, flags);
  378. do {
  379. rc = dasd_sleep_on(cqr);
  380. } while (rc && (cqr->retries > 0));
  381. if (rc) {
  382. spin_lock_irqsave(&lcu->lock, flags);
  383. lcu->flags |= NEED_UAC_UPDATE;
  384. spin_unlock_irqrestore(&lcu->lock, flags);
  385. }
  386. dasd_kfree_request(cqr, cqr->memdev);
  387. return rc;
  388. }
  389. static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
  390. {
  391. unsigned long flags;
  392. struct alias_pav_group *pavgroup, *tempgroup;
  393. struct dasd_device *device, *tempdev;
  394. int i, rc;
  395. struct dasd_eckd_private *private;
  396. spin_lock_irqsave(&lcu->lock, flags);
  397. list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
  398. list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
  399. alias_list) {
  400. list_move(&device->alias_list, &lcu->active_devices);
  401. private = (struct dasd_eckd_private *) device->private;
  402. private->pavgroup = NULL;
  403. }
  404. list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
  405. alias_list) {
  406. list_move(&device->alias_list, &lcu->active_devices);
  407. private = (struct dasd_eckd_private *) device->private;
  408. private->pavgroup = NULL;
  409. }
  410. list_del(&pavgroup->group);
  411. kfree(pavgroup);
  412. }
  413. spin_unlock_irqrestore(&lcu->lock, flags);
  414. rc = read_unit_address_configuration(refdev, lcu);
  415. if (rc)
  416. return rc;
  417. spin_lock_irqsave(&lcu->lock, flags);
  418. lcu->pav = NO_PAV;
  419. for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
  420. switch (lcu->uac->unit[i].ua_type) {
  421. case UA_BASE_PAV_ALIAS:
  422. lcu->pav = BASE_PAV;
  423. break;
  424. case UA_HYPER_PAV_ALIAS:
  425. lcu->pav = HYPER_PAV;
  426. break;
  427. }
  428. if (lcu->pav != NO_PAV)
  429. break;
  430. }
  431. list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
  432. alias_list) {
  433. _add_device_to_lcu(lcu, device);
  434. }
  435. spin_unlock_irqrestore(&lcu->lock, flags);
  436. return 0;
  437. }
  438. static void lcu_update_work(struct work_struct *work)
  439. {
  440. struct alias_lcu *lcu;
  441. struct read_uac_work_data *ruac_data;
  442. struct dasd_device *device;
  443. unsigned long flags;
  444. int rc;
  445. ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
  446. lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
  447. device = ruac_data->device;
  448. rc = _lcu_update(device, lcu);
  449. /*
  450. * Need to check flags again, as there could have been another
  451. * prepare_update or a new device a new device while we were still
  452. * processing the data
  453. */
  454. spin_lock_irqsave(&lcu->lock, flags);
  455. if (rc || (lcu->flags & NEED_UAC_UPDATE)) {
  456. DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
  457. " alias data in lcu (rc = %d), retry later", rc);
  458. schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ);
  459. } else {
  460. lcu->ruac_data.device = NULL;
  461. lcu->flags &= ~UPDATE_PENDING;
  462. }
  463. spin_unlock_irqrestore(&lcu->lock, flags);
  464. }
  465. static int _schedule_lcu_update(struct alias_lcu *lcu,
  466. struct dasd_device *device)
  467. {
  468. struct dasd_device *usedev = NULL;
  469. struct alias_pav_group *group;
  470. lcu->flags |= NEED_UAC_UPDATE;
  471. if (lcu->ruac_data.device) {
  472. /* already scheduled or running */
  473. return 0;
  474. }
  475. if (device && !list_empty(&device->alias_list))
  476. usedev = device;
  477. if (!usedev && !list_empty(&lcu->grouplist)) {
  478. group = list_first_entry(&lcu->grouplist,
  479. struct alias_pav_group, group);
  480. if (!list_empty(&group->baselist))
  481. usedev = list_first_entry(&group->baselist,
  482. struct dasd_device,
  483. alias_list);
  484. else if (!list_empty(&group->aliaslist))
  485. usedev = list_first_entry(&group->aliaslist,
  486. struct dasd_device,
  487. alias_list);
  488. }
  489. if (!usedev && !list_empty(&lcu->active_devices)) {
  490. usedev = list_first_entry(&lcu->active_devices,
  491. struct dasd_device, alias_list);
  492. }
  493. /*
  494. * if we haven't found a proper device yet, give up for now, the next
  495. * device that will be set active will trigger an lcu update
  496. */
  497. if (!usedev)
  498. return -EINVAL;
  499. lcu->ruac_data.device = usedev;
  500. schedule_delayed_work(&lcu->ruac_data.dwork, 0);
  501. return 0;
  502. }
  503. int dasd_alias_add_device(struct dasd_device *device)
  504. {
  505. struct dasd_eckd_private *private;
  506. struct alias_lcu *lcu;
  507. unsigned long flags;
  508. int rc;
  509. private = (struct dasd_eckd_private *) device->private;
  510. lcu = private->lcu;
  511. rc = 0;
  512. spin_lock_irqsave(&lcu->lock, flags);
  513. if (!(lcu->flags & UPDATE_PENDING)) {
  514. rc = _add_device_to_lcu(lcu, device);
  515. if (rc)
  516. lcu->flags |= UPDATE_PENDING;
  517. }
  518. if (lcu->flags & UPDATE_PENDING) {
  519. list_move(&device->alias_list, &lcu->active_devices);
  520. _schedule_lcu_update(lcu, device);
  521. }
  522. spin_unlock_irqrestore(&lcu->lock, flags);
  523. return rc;
  524. }
  525. int dasd_alias_remove_device(struct dasd_device *device)
  526. {
  527. struct dasd_eckd_private *private;
  528. struct alias_lcu *lcu;
  529. unsigned long flags;
  530. private = (struct dasd_eckd_private *) device->private;
  531. lcu = private->lcu;
  532. spin_lock_irqsave(&lcu->lock, flags);
  533. _remove_device_from_lcu(lcu, device);
  534. spin_unlock_irqrestore(&lcu->lock, flags);
  535. return 0;
  536. }
  537. struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
  538. {
  539. struct dasd_device *alias_device;
  540. struct alias_pav_group *group;
  541. struct alias_lcu *lcu;
  542. struct dasd_eckd_private *private, *alias_priv;
  543. unsigned long flags;
  544. private = (struct dasd_eckd_private *) base_device->private;
  545. group = private->pavgroup;
  546. lcu = private->lcu;
  547. if (!group || !lcu)
  548. return NULL;
  549. if (lcu->pav == NO_PAV ||
  550. lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
  551. return NULL;
  552. spin_lock_irqsave(&lcu->lock, flags);
  553. alias_device = group->next;
  554. if (!alias_device) {
  555. if (list_empty(&group->aliaslist)) {
  556. spin_unlock_irqrestore(&lcu->lock, flags);
  557. return NULL;
  558. } else {
  559. alias_device = list_first_entry(&group->aliaslist,
  560. struct dasd_device,
  561. alias_list);
  562. }
  563. }
  564. if (list_is_last(&alias_device->alias_list, &group->aliaslist))
  565. group->next = list_first_entry(&group->aliaslist,
  566. struct dasd_device, alias_list);
  567. else
  568. group->next = list_first_entry(&alias_device->alias_list,
  569. struct dasd_device, alias_list);
  570. spin_unlock_irqrestore(&lcu->lock, flags);
  571. alias_priv = (struct dasd_eckd_private *) alias_device->private;
  572. if ((alias_priv->count < private->count) && !alias_device->stopped)
  573. return alias_device;
  574. else
  575. return NULL;
  576. }
  577. /*
  578. * Summary unit check handling depends on the way alias devices
  579. * are handled so it is done here rather then in dasd_eckd.c
  580. */
  581. static int reset_summary_unit_check(struct alias_lcu *lcu,
  582. struct dasd_device *device,
  583. char reason)
  584. {
  585. struct dasd_ccw_req *cqr;
  586. int rc = 0;
  587. struct ccw1 *ccw;
  588. cqr = lcu->rsu_cqr;
  589. strncpy((char *) &cqr->magic, "ECKD", 4);
  590. ASCEBC((char *) &cqr->magic, 4);
  591. ccw = cqr->cpaddr;
  592. ccw->cmd_code = DASD_ECKD_CCW_RSCK;
  593. ccw->flags = 0 ;
  594. ccw->count = 16;
  595. ccw->cda = (__u32)(addr_t) cqr->data;
  596. ((char *)cqr->data)[0] = reason;
  597. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  598. cqr->retries = 255; /* set retry counter to enable basic ERP */
  599. cqr->startdev = device;
  600. cqr->memdev = device;
  601. cqr->block = NULL;
  602. cqr->expires = 5 * HZ;
  603. cqr->buildclk = get_clock();
  604. cqr->status = DASD_CQR_FILLED;
  605. rc = dasd_sleep_on_immediatly(cqr);
  606. return rc;
  607. }
  608. static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
  609. {
  610. struct alias_pav_group *pavgroup;
  611. struct dasd_device *device;
  612. struct dasd_eckd_private *private;
  613. /* active and inactive list can contain alias as well as base devices */
  614. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  615. private = (struct dasd_eckd_private *) device->private;
  616. if (private->uid.type != UA_BASE_DEVICE)
  617. continue;
  618. dasd_schedule_block_bh(device->block);
  619. dasd_schedule_device_bh(device);
  620. }
  621. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  622. private = (struct dasd_eckd_private *) device->private;
  623. if (private->uid.type != UA_BASE_DEVICE)
  624. continue;
  625. dasd_schedule_block_bh(device->block);
  626. dasd_schedule_device_bh(device);
  627. }
  628. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  629. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  630. dasd_schedule_block_bh(device->block);
  631. dasd_schedule_device_bh(device);
  632. }
  633. }
  634. }
  635. static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
  636. {
  637. struct alias_pav_group *pavgroup;
  638. struct dasd_device *device, *temp;
  639. struct dasd_eckd_private *private;
  640. int rc;
  641. unsigned long flags;
  642. LIST_HEAD(active);
  643. /*
  644. * Problem here ist that dasd_flush_device_queue may wait
  645. * for termination of a request to complete. We can't keep
  646. * the lcu lock during that time, so we must assume that
  647. * the lists may have changed.
  648. * Idea: first gather all active alias devices in a separate list,
  649. * then flush the first element of this list unlocked, and afterwards
  650. * check if it is still on the list before moving it to the
  651. * active_devices list.
  652. */
  653. spin_lock_irqsave(&lcu->lock, flags);
  654. list_for_each_entry_safe(device, temp, &lcu->active_devices,
  655. alias_list) {
  656. private = (struct dasd_eckd_private *) device->private;
  657. if (private->uid.type == UA_BASE_DEVICE)
  658. continue;
  659. list_move(&device->alias_list, &active);
  660. }
  661. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  662. list_splice_init(&pavgroup->aliaslist, &active);
  663. }
  664. while (!list_empty(&active)) {
  665. device = list_first_entry(&active, struct dasd_device,
  666. alias_list);
  667. spin_unlock_irqrestore(&lcu->lock, flags);
  668. rc = dasd_flush_device_queue(device);
  669. spin_lock_irqsave(&lcu->lock, flags);
  670. /*
  671. * only move device around if it wasn't moved away while we
  672. * were waiting for the flush
  673. */
  674. if (device == list_first_entry(&active,
  675. struct dasd_device, alias_list))
  676. list_move(&device->alias_list, &lcu->active_devices);
  677. }
  678. spin_unlock_irqrestore(&lcu->lock, flags);
  679. }
  680. static void __stop_device_on_lcu(struct dasd_device *device,
  681. struct dasd_device *pos)
  682. {
  683. /* If pos == device then device is already locked! */
  684. if (pos == device) {
  685. pos->stopped |= DASD_STOPPED_SU;
  686. return;
  687. }
  688. spin_lock(get_ccwdev_lock(pos->cdev));
  689. pos->stopped |= DASD_STOPPED_SU;
  690. spin_unlock(get_ccwdev_lock(pos->cdev));
  691. }
  692. /*
  693. * This function is called in interrupt context, so the
  694. * cdev lock for device is already locked!
  695. */
  696. static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
  697. struct dasd_device *device)
  698. {
  699. struct alias_pav_group *pavgroup;
  700. struct dasd_device *pos;
  701. list_for_each_entry(pos, &lcu->active_devices, alias_list)
  702. __stop_device_on_lcu(device, pos);
  703. list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
  704. __stop_device_on_lcu(device, pos);
  705. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  706. list_for_each_entry(pos, &pavgroup->baselist, alias_list)
  707. __stop_device_on_lcu(device, pos);
  708. list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
  709. __stop_device_on_lcu(device, pos);
  710. }
  711. }
  712. static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
  713. {
  714. struct alias_pav_group *pavgroup;
  715. struct dasd_device *device;
  716. unsigned long flags;
  717. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  718. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  719. device->stopped &= ~DASD_STOPPED_SU;
  720. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  721. }
  722. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  723. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  724. device->stopped &= ~DASD_STOPPED_SU;
  725. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  726. }
  727. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  728. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  729. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  730. device->stopped &= ~DASD_STOPPED_SU;
  731. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  732. flags);
  733. }
  734. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  735. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  736. device->stopped &= ~DASD_STOPPED_SU;
  737. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  738. flags);
  739. }
  740. }
  741. }
  742. static void summary_unit_check_handling_work(struct work_struct *work)
  743. {
  744. struct alias_lcu *lcu;
  745. struct summary_unit_check_work_data *suc_data;
  746. unsigned long flags;
  747. struct dasd_device *device;
  748. suc_data = container_of(work, struct summary_unit_check_work_data,
  749. worker);
  750. lcu = container_of(suc_data, struct alias_lcu, suc_data);
  751. device = suc_data->device;
  752. /* 1. flush alias devices */
  753. flush_all_alias_devices_on_lcu(lcu);
  754. /* 2. reset summary unit check */
  755. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  756. device->stopped &= ~(DASD_STOPPED_SU | DASD_STOPPED_PENDING);
  757. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  758. reset_summary_unit_check(lcu, device, suc_data->reason);
  759. spin_lock_irqsave(&lcu->lock, flags);
  760. _unstop_all_devices_on_lcu(lcu);
  761. _restart_all_base_devices_on_lcu(lcu);
  762. /* 3. read new alias configuration */
  763. _schedule_lcu_update(lcu, device);
  764. lcu->suc_data.device = NULL;
  765. spin_unlock_irqrestore(&lcu->lock, flags);
  766. }
  767. /*
  768. * note: this will be called from int handler context (cdev locked)
  769. */
  770. void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
  771. struct irb *irb)
  772. {
  773. struct alias_lcu *lcu;
  774. char reason;
  775. struct dasd_eckd_private *private;
  776. char *sense;
  777. private = (struct dasd_eckd_private *) device->private;
  778. sense = dasd_get_sense(irb);
  779. if (sense) {
  780. reason = sense[8];
  781. DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
  782. "eckd handle summary unit check: reason", reason);
  783. } else {
  784. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  785. "eckd handle summary unit check:"
  786. " no reason code available");
  787. return;
  788. }
  789. lcu = private->lcu;
  790. if (!lcu) {
  791. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  792. "device not ready to handle summary"
  793. " unit check (no lcu structure)");
  794. return;
  795. }
  796. spin_lock(&lcu->lock);
  797. _stop_all_devices_on_lcu(lcu, device);
  798. /* prepare for lcu_update */
  799. private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
  800. /* If this device is about to be removed just return and wait for
  801. * the next interrupt on a different device
  802. */
  803. if (list_empty(&device->alias_list)) {
  804. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  805. "device is in offline processing,"
  806. " don't do summary unit check handling");
  807. spin_unlock(&lcu->lock);
  808. return;
  809. }
  810. if (lcu->suc_data.device) {
  811. /* already scheduled or running */
  812. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  813. "previous instance of summary unit check worker"
  814. " still pending");
  815. spin_unlock(&lcu->lock);
  816. return ;
  817. }
  818. lcu->suc_data.reason = reason;
  819. lcu->suc_data.device = device;
  820. spin_unlock(&lcu->lock);
  821. schedule_work(&lcu->suc_data.worker);
  822. };