i2o_block.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*
  2. * Block OSM
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * For the purpose of avoiding doubt the preferred form of the work
  19. * for making modifications shall be a standards compliant form such
  20. * gzipped tar and not one requiring a proprietary or patent encumbered
  21. * tool to unpack.
  22. *
  23. * Fixes/additions:
  24. * Steve Ralston:
  25. * Multiple device handling error fixes,
  26. * Added a queue depth.
  27. * Alan Cox:
  28. * FC920 has an rmw bug. Dont or in the end marker.
  29. * Removed queue walk, fixed for 64bitness.
  30. * Rewrote much of the code over time
  31. * Added indirect block lists
  32. * Handle 64K limits on many controllers
  33. * Don't use indirects on the Promise (breaks)
  34. * Heavily chop down the queue depths
  35. * Deepak Saxena:
  36. * Independent queues per IOP
  37. * Support for dynamic device creation/deletion
  38. * Code cleanup
  39. * Support for larger I/Os through merge* functions
  40. * (taken from DAC960 driver)
  41. * Boji T Kannanthanam:
  42. * Set the I2O Block devices to be detected in increasing
  43. * order of TIDs during boot.
  44. * Search and set the I2O block device that we boot off
  45. * from as the first device to be claimed (as /dev/i2o/hda)
  46. * Properly attach/detach I2O gendisk structure from the
  47. * system gendisk list. The I2O block devices now appear in
  48. * /proc/partitions.
  49. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  50. * Minor bugfixes for 2.6.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/i2o.h>
  54. #include <linux/mempool.h>
  55. #include <linux/genhd.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/hdreg.h>
  58. #include "i2o_block.h"
  59. #define OSM_NAME "block-osm"
  60. #define OSM_VERSION "$Rev$"
  61. #define OSM_DESCRIPTION "I2O Block Device OSM"
  62. static struct i2o_driver i2o_block_driver;
  63. /* global Block OSM request mempool */
  64. static struct i2o_block_mempool i2o_blk_req_pool;
  65. /* Block OSM class handling definition */
  66. static struct i2o_class_id i2o_block_class_id[] = {
  67. {I2O_CLASS_RANDOM_BLOCK_STORAGE},
  68. {I2O_CLASS_END}
  69. };
  70. /**
  71. * i2o_block_device_free - free the memory of the I2O Block device
  72. * @dev: I2O Block device, which should be cleaned up
  73. *
  74. * Frees the request queue, gendisk and the i2o_block_device structure.
  75. */
  76. static void i2o_block_device_free(struct i2o_block_device *dev)
  77. {
  78. blk_cleanup_queue(dev->gd->queue);
  79. put_disk(dev->gd);
  80. kfree(dev);
  81. };
  82. /**
  83. * i2o_block_remove - remove the I2O Block device from the system again
  84. * @dev: I2O Block device which should be removed
  85. *
  86. * Remove gendisk from system and free all allocated memory.
  87. *
  88. * Always returns 0.
  89. */
  90. static int i2o_block_remove(struct device *dev)
  91. {
  92. struct i2o_device *i2o_dev = to_i2o_device(dev);
  93. struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);
  94. osm_info("Device removed %s\n", i2o_blk_dev->gd->disk_name);
  95. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);
  96. del_gendisk(i2o_blk_dev->gd);
  97. dev_set_drvdata(dev, NULL);
  98. i2o_device_claim_release(i2o_dev);
  99. i2o_block_device_free(i2o_blk_dev);
  100. return 0;
  101. };
  102. /**
  103. * i2o_block_device flush - Flush all dirty data of I2O device dev
  104. * @dev: I2O device which should be flushed
  105. *
  106. * Flushes all dirty data on device dev.
  107. *
  108. * Returns 0 on success or negative error code on failure.
  109. */
  110. static int i2o_block_device_flush(struct i2o_device *dev)
  111. {
  112. struct i2o_message __iomem *msg;
  113. u32 m;
  114. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  115. if (m == I2O_QUEUE_EMPTY)
  116. return -ETIMEDOUT;
  117. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  118. writel(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->lct_data.tid,
  119. &msg->u.head[1]);
  120. writel(60 << 16, &msg->body[0]);
  121. osm_debug("Flushing...\n");
  122. return i2o_msg_post_wait(dev->iop, m, 60);
  123. };
  124. /**
  125. * i2o_block_device_mount - Mount (load) the media of device dev
  126. * @dev: I2O device which should receive the mount request
  127. * @media_id: Media Identifier
  128. *
  129. * Load a media into drive. Identifier should be set to -1, because the
  130. * spec does not support any other value.
  131. *
  132. * Returns 0 on success or negative error code on failure.
  133. */
  134. static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
  135. {
  136. struct i2o_message __iomem *msg;
  137. u32 m;
  138. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  139. if (m == I2O_QUEUE_EMPTY)
  140. return -ETIMEDOUT;
  141. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  142. writel(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->lct_data.tid,
  143. &msg->u.head[1]);
  144. writel(-1, &msg->body[0]);
  145. writel(0, &msg->body[1]);
  146. osm_debug("Mounting...\n");
  147. return i2o_msg_post_wait(dev->iop, m, 2);
  148. };
  149. /**
  150. * i2o_block_device_lock - Locks the media of device dev
  151. * @dev: I2O device which should receive the lock request
  152. * @media_id: Media Identifier
  153. *
  154. * Lock media of device dev to prevent removal. The media identifier
  155. * should be set to -1, because the spec does not support any other value.
  156. *
  157. * Returns 0 on success or negative error code on failure.
  158. */
  159. static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
  160. {
  161. struct i2o_message __iomem *msg;
  162. u32 m;
  163. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  164. if (m == I2O_QUEUE_EMPTY)
  165. return -ETIMEDOUT;
  166. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  167. writel(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  168. &msg->u.head[1]);
  169. writel(-1, &msg->body[0]);
  170. osm_debug("Locking...\n");
  171. return i2o_msg_post_wait(dev->iop, m, 2);
  172. };
  173. /**
  174. * i2o_block_device_unlock - Unlocks the media of device dev
  175. * @dev: I2O device which should receive the unlocked request
  176. * @media_id: Media Identifier
  177. *
  178. * Unlocks the media in device dev. The media identifier should be set to
  179. * -1, because the spec does not support any other value.
  180. *
  181. * Returns 0 on success or negative error code on failure.
  182. */
  183. static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
  184. {
  185. struct i2o_message __iomem *msg;
  186. u32 m;
  187. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  188. if (m == I2O_QUEUE_EMPTY)
  189. return -ETIMEDOUT;
  190. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  191. writel(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  192. &msg->u.head[1]);
  193. writel(media_id, &msg->body[0]);
  194. osm_debug("Unlocking...\n");
  195. return i2o_msg_post_wait(dev->iop, m, 2);
  196. };
  197. /**
  198. * i2o_block_device_power - Power management for device dev
  199. * @dev: I2O device which should receive the power management request
  200. * @operation: Operation which should be send
  201. *
  202. * Send a power management request to the device dev.
  203. *
  204. * Returns 0 on success or negative error code on failure.
  205. */
  206. static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
  207. {
  208. struct i2o_device *i2o_dev = dev->i2o_dev;
  209. struct i2o_controller *c = i2o_dev->iop;
  210. struct i2o_message __iomem *msg;
  211. u32 m;
  212. int rc;
  213. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  214. if (m == I2O_QUEUE_EMPTY)
  215. return -ETIMEDOUT;
  216. writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  217. writel(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->lct_data.
  218. tid, &msg->u.head[1]);
  219. writel(op << 24, &msg->body[0]);
  220. osm_debug("Power...\n");
  221. rc = i2o_msg_post_wait(c, m, 60);
  222. if (!rc)
  223. dev->power = op;
  224. return rc;
  225. };
  226. /**
  227. * i2o_block_request_alloc - Allocate an I2O block request struct
  228. *
  229. * Allocates an I2O block request struct and initialize the list.
  230. *
  231. * Returns a i2o_block_request pointer on success or negative error code
  232. * on failure.
  233. */
  234. static inline struct i2o_block_request *i2o_block_request_alloc(void)
  235. {
  236. struct i2o_block_request *ireq;
  237. ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
  238. if (!ireq)
  239. return ERR_PTR(-ENOMEM);
  240. INIT_LIST_HEAD(&ireq->queue);
  241. return ireq;
  242. };
  243. /**
  244. * i2o_block_request_free - Frees a I2O block request
  245. * @ireq: I2O block request which should be freed
  246. *
  247. * Fres the allocated memory (give it back to the request mempool).
  248. */
  249. static inline void i2o_block_request_free(struct i2o_block_request *ireq)
  250. {
  251. mempool_free(ireq, i2o_blk_req_pool.pool);
  252. };
  253. /**
  254. * i2o_block_sglist_alloc - Allocate the SG list and map it
  255. * @ireq: I2O block request
  256. *
  257. * Builds the SG list and map it into to be accessable by the controller.
  258. *
  259. * Returns the number of elements in the SG list or 0 on failure.
  260. */
  261. static inline int i2o_block_sglist_alloc(struct i2o_block_request *ireq)
  262. {
  263. struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
  264. int nents;
  265. nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
  266. if (rq_data_dir(ireq->req) == READ)
  267. ireq->sg_dma_direction = PCI_DMA_FROMDEVICE;
  268. else
  269. ireq->sg_dma_direction = PCI_DMA_TODEVICE;
  270. ireq->sg_nents = dma_map_sg(dev, ireq->sg_table, nents,
  271. ireq->sg_dma_direction);
  272. return ireq->sg_nents;
  273. };
  274. /**
  275. * i2o_block_sglist_free - Frees the SG list
  276. * @ireq: I2O block request from which the SG should be freed
  277. *
  278. * Frees the SG list from the I2O block request.
  279. */
  280. static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
  281. {
  282. struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
  283. dma_unmap_sg(dev, ireq->sg_table, ireq->sg_nents,
  284. ireq->sg_dma_direction);
  285. };
  286. /**
  287. * i2o_block_prep_req_fn - Allocates I2O block device specific struct
  288. * @q: request queue for the request
  289. * @req: the request to prepare
  290. *
  291. * Allocate the necessary i2o_block_request struct and connect it to
  292. * the request. This is needed that we not loose the SG list later on.
  293. *
  294. * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
  295. */
  296. static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
  297. {
  298. struct i2o_block_device *i2o_blk_dev = q->queuedata;
  299. struct i2o_block_request *ireq;
  300. /* request is already processed by us, so return */
  301. if (req->flags & REQ_SPECIAL) {
  302. osm_debug("REQ_SPECIAL already set!\n");
  303. req->flags |= REQ_DONTPREP;
  304. return BLKPREP_OK;
  305. }
  306. /* connect the i2o_block_request to the request */
  307. if (!req->special) {
  308. ireq = i2o_block_request_alloc();
  309. if (unlikely(IS_ERR(ireq))) {
  310. osm_debug("unable to allocate i2o_block_request!\n");
  311. return BLKPREP_DEFER;
  312. }
  313. ireq->i2o_blk_dev = i2o_blk_dev;
  314. req->special = ireq;
  315. ireq->req = req;
  316. } else
  317. ireq = req->special;
  318. /* do not come back here */
  319. req->flags |= REQ_DONTPREP | REQ_SPECIAL;
  320. return BLKPREP_OK;
  321. };
  322. /**
  323. * i2o_block_delayed_request_fn - delayed request queue function
  324. * delayed_request: the delayed request with the queue to start
  325. *
  326. * If the request queue is stopped for a disk, and there is no open
  327. * request, a new event is created, which calls this function to start
  328. * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
  329. * be started again.
  330. */
  331. static void i2o_block_delayed_request_fn(void *delayed_request)
  332. {
  333. struct i2o_block_delayed_request *dreq = delayed_request;
  334. struct request_queue *q = dreq->queue;
  335. unsigned long flags;
  336. spin_lock_irqsave(q->queue_lock, flags);
  337. blk_start_queue(q);
  338. spin_unlock_irqrestore(q->queue_lock, flags);
  339. kfree(dreq);
  340. };
  341. /**
  342. * i2o_block_reply - Block OSM reply handler.
  343. * @c: I2O controller from which the message arrives
  344. * @m: message id of reply
  345. * qmsg: the actuall I2O message reply
  346. *
  347. * This function gets all the message replies.
  348. *
  349. */
  350. static int i2o_block_reply(struct i2o_controller *c, u32 m,
  351. struct i2o_message *msg)
  352. {
  353. struct i2o_block_request *ireq;
  354. struct request *req;
  355. struct i2o_block_device *dev;
  356. struct request_queue *q;
  357. u8 st;
  358. unsigned long flags;
  359. /* FAILed message */
  360. if (unlikely(le32_to_cpu(msg->u.head[0]) & (1 << 13))) {
  361. struct i2o_message *pmsg;
  362. u32 pm;
  363. /*
  364. * FAILed message from controller
  365. * We increment the error count and abort it
  366. *
  367. * In theory this will never happen. The I2O block class
  368. * specification states that block devices never return
  369. * FAILs but instead use the REQ status field...but
  370. * better be on the safe side since no one really follows
  371. * the spec to the book :)
  372. */
  373. pm = le32_to_cpu(msg->body[3]);
  374. pmsg = i2o_msg_in_to_virt(c, pm);
  375. req = i2o_cntxt_list_get(c, le32_to_cpu(pmsg->u.s.tcntxt));
  376. if (unlikely(!req)) {
  377. osm_err("NULL reply received!\n");
  378. return -1;
  379. }
  380. ireq = req->special;
  381. dev = ireq->i2o_blk_dev;
  382. q = dev->gd->queue;
  383. req->errors++;
  384. spin_lock_irqsave(q->queue_lock, flags);
  385. while (end_that_request_chunk(req, !req->errors,
  386. le32_to_cpu(pmsg->body[1]))) ;
  387. end_that_request_last(req);
  388. dev->open_queue_depth--;
  389. list_del(&ireq->queue);
  390. blk_start_queue(q);
  391. spin_unlock_irqrestore(q->queue_lock, flags);
  392. /* Now flush the message by making it a NOP */
  393. i2o_msg_nop(c, pm);
  394. return -1;
  395. }
  396. req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  397. if (unlikely(!req)) {
  398. osm_err("NULL reply received!\n");
  399. return -1;
  400. }
  401. ireq = req->special;
  402. dev = ireq->i2o_blk_dev;
  403. q = dev->gd->queue;
  404. if (unlikely(!dev->i2o_dev)) {
  405. /*
  406. * This is HACK, but Intel Integrated RAID allows user
  407. * to delete a volume that is claimed, locked, and in use
  408. * by the OS. We have to check for a reply from a
  409. * non-existent device and flag it as an error or the system
  410. * goes kaput...
  411. */
  412. req->errors++;
  413. osm_warn("Data transfer to deleted device!\n");
  414. spin_lock_irqsave(q->queue_lock, flags);
  415. while (end_that_request_chunk
  416. (req, !req->errors, le32_to_cpu(msg->body[1]))) ;
  417. end_that_request_last(req);
  418. dev->open_queue_depth--;
  419. list_del(&ireq->queue);
  420. blk_start_queue(q);
  421. spin_unlock_irqrestore(q->queue_lock, flags);
  422. return -1;
  423. }
  424. /*
  425. * Lets see what is cooking. We stuffed the
  426. * request in the context.
  427. */
  428. st = le32_to_cpu(msg->body[0]) >> 24;
  429. if (st != 0) {
  430. int err;
  431. char *bsa_errors[] = {
  432. "Success",
  433. "Media Error",
  434. "Failure communicating to device",
  435. "Device Failure",
  436. "Device is not ready",
  437. "Media not present",
  438. "Media is locked by another user",
  439. "Media has failed",
  440. "Failure communicating to device",
  441. "Device bus failure",
  442. "Device is locked by another user",
  443. "Device is write protected",
  444. "Device has reset",
  445. "Volume has changed, waiting for acknowledgement"
  446. };
  447. err = le32_to_cpu(msg->body[0]) & 0xffff;
  448. /*
  449. * Device not ready means two things. One is that the
  450. * the thing went offline (but not a removal media)
  451. *
  452. * The second is that you have a SuperTrak 100 and the
  453. * firmware got constipated. Unlike standard i2o card
  454. * setups the supertrak returns an error rather than
  455. * blocking for the timeout in these cases.
  456. *
  457. * Don't stick a supertrak100 into cache aggressive modes
  458. */
  459. osm_err("block-osm: /dev/%s error: %s", dev->gd->disk_name,
  460. bsa_errors[le32_to_cpu(msg->body[0]) & 0xffff]);
  461. if (le32_to_cpu(msg->body[0]) & 0x00ff0000)
  462. printk(KERN_ERR " - DDM attempted %d retries",
  463. (le32_to_cpu(msg->body[0]) >> 16) & 0x00ff);
  464. printk(KERN_ERR ".\n");
  465. req->errors++;
  466. } else
  467. req->errors = 0;
  468. if (!end_that_request_chunk
  469. (req, !req->errors, le32_to_cpu(msg->body[1]))) {
  470. add_disk_randomness(req->rq_disk);
  471. spin_lock_irqsave(q->queue_lock, flags);
  472. end_that_request_last(req);
  473. dev->open_queue_depth--;
  474. list_del(&ireq->queue);
  475. blk_start_queue(q);
  476. spin_unlock_irqrestore(q->queue_lock, flags);
  477. i2o_block_sglist_free(ireq);
  478. i2o_block_request_free(ireq);
  479. } else
  480. osm_err("still remaining chunks\n");
  481. return 1;
  482. };
  483. static void i2o_block_event(struct i2o_event *evt)
  484. {
  485. osm_info("block-osm: event received\n");
  486. kfree(evt);
  487. };
  488. /*
  489. * SCSI-CAM for ioctl geometry mapping
  490. * Duplicated with SCSI - this should be moved into somewhere common
  491. * perhaps genhd ?
  492. *
  493. * LBA -> CHS mapping table taken from:
  494. *
  495. * "Incorporating the I2O Architecture into BIOS for Intel Architecture
  496. * Platforms"
  497. *
  498. * This is an I2O document that is only available to I2O members,
  499. * not developers.
  500. *
  501. * From my understanding, this is how all the I2O cards do this
  502. *
  503. * Disk Size | Sectors | Heads | Cylinders
  504. * ---------------+---------+-------+-------------------
  505. * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
  506. * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
  507. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  508. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  509. *
  510. */
  511. #define BLOCK_SIZE_528M 1081344
  512. #define BLOCK_SIZE_1G 2097152
  513. #define BLOCK_SIZE_21G 4403200
  514. #define BLOCK_SIZE_42G 8806400
  515. #define BLOCK_SIZE_84G 17612800
  516. static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
  517. unsigned char *hds, unsigned char *secs)
  518. {
  519. unsigned long heads, sectors, cylinders;
  520. sectors = 63L; /* Maximize sectors per track */
  521. if (capacity <= BLOCK_SIZE_528M)
  522. heads = 16;
  523. else if (capacity <= BLOCK_SIZE_1G)
  524. heads = 32;
  525. else if (capacity <= BLOCK_SIZE_21G)
  526. heads = 64;
  527. else if (capacity <= BLOCK_SIZE_42G)
  528. heads = 128;
  529. else
  530. heads = 255;
  531. cylinders = (unsigned long)capacity / (heads * sectors);
  532. *cyls = (unsigned short)cylinders; /* Stuff return values */
  533. *secs = (unsigned char)sectors;
  534. *hds = (unsigned char)heads;
  535. }
  536. /**
  537. * i2o_block_open - Open the block device
  538. *
  539. * Power up the device, mount and lock the media. This function is called,
  540. * if the block device is opened for access.
  541. *
  542. * Returns 0 on success or negative error code on failure.
  543. */
  544. static int i2o_block_open(struct inode *inode, struct file *file)
  545. {
  546. struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
  547. if (!dev->i2o_dev)
  548. return -ENODEV;
  549. if (dev->power > 0x1f)
  550. i2o_block_device_power(dev, 0x02);
  551. i2o_block_device_mount(dev->i2o_dev, -1);
  552. i2o_block_device_lock(dev->i2o_dev, -1);
  553. osm_debug("Ready.\n");
  554. return 0;
  555. };
  556. /**
  557. * i2o_block_release - Release the I2O block device
  558. *
  559. * Unlock and unmount the media, and power down the device. Gets called if
  560. * the block device is closed.
  561. *
  562. * Returns 0 on success or negative error code on failure.
  563. */
  564. static int i2o_block_release(struct inode *inode, struct file *file)
  565. {
  566. struct gendisk *disk = inode->i_bdev->bd_disk;
  567. struct i2o_block_device *dev = disk->private_data;
  568. u8 operation;
  569. /*
  570. * This is to deail with the case of an application
  571. * opening a device and then the device dissapears while
  572. * it's in use, and then the application tries to release
  573. * it. ex: Unmounting a deleted RAID volume at reboot.
  574. * If we send messages, it will just cause FAILs since
  575. * the TID no longer exists.
  576. */
  577. if (!dev->i2o_dev)
  578. return 0;
  579. i2o_block_device_flush(dev->i2o_dev);
  580. i2o_block_device_unlock(dev->i2o_dev, -1);
  581. if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
  582. operation = 0x21;
  583. else
  584. operation = 0x24;
  585. i2o_block_device_power(dev, operation);
  586. return 0;
  587. }
  588. /**
  589. * i2o_block_ioctl - Issue device specific ioctl calls.
  590. * @cmd: ioctl command
  591. * @arg: arg
  592. *
  593. * Handles ioctl request for the block device.
  594. *
  595. * Return 0 on success or negative error on failure.
  596. */
  597. static int i2o_block_ioctl(struct inode *inode, struct file *file,
  598. unsigned int cmd, unsigned long arg)
  599. {
  600. struct gendisk *disk = inode->i_bdev->bd_disk;
  601. struct i2o_block_device *dev = disk->private_data;
  602. void __user *argp = (void __user *)arg;
  603. /* Anyone capable of this syscall can do *real bad* things */
  604. if (!capable(CAP_SYS_ADMIN))
  605. return -EPERM;
  606. switch (cmd) {
  607. case HDIO_GETGEO:
  608. {
  609. struct hd_geometry g;
  610. i2o_block_biosparam(get_capacity(disk),
  611. &g.cylinders, &g.heads, &g.sectors);
  612. g.start = get_start_sect(inode->i_bdev);
  613. return copy_to_user(argp, &g, sizeof(g)) ? -EFAULT : 0;
  614. }
  615. case BLKI2OGRSTRAT:
  616. return put_user(dev->rcache, (int __user *)arg);
  617. case BLKI2OGWSTRAT:
  618. return put_user(dev->wcache, (int __user *)arg);
  619. case BLKI2OSRSTRAT:
  620. if (arg < 0 || arg > CACHE_SMARTFETCH)
  621. return -EINVAL;
  622. dev->rcache = arg;
  623. break;
  624. case BLKI2OSWSTRAT:
  625. if (arg != 0
  626. && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
  627. return -EINVAL;
  628. dev->wcache = arg;
  629. break;
  630. }
  631. return -ENOTTY;
  632. };
  633. /**
  634. * i2o_block_media_changed - Have we seen a media change?
  635. * @disk: gendisk which should be verified
  636. *
  637. * Verifies if the media has changed.
  638. *
  639. * Returns 1 if the media was changed or 0 otherwise.
  640. */
  641. static int i2o_block_media_changed(struct gendisk *disk)
  642. {
  643. struct i2o_block_device *p = disk->private_data;
  644. if (p->media_change_flag) {
  645. p->media_change_flag = 0;
  646. return 1;
  647. }
  648. return 0;
  649. }
  650. /**
  651. * i2o_block_transfer - Transfer a request to/from the I2O controller
  652. * @req: the request which should be transfered
  653. *
  654. * This function converts the request into a I2O message. The necessary
  655. * DMA buffers are allocated and after everything is setup post the message
  656. * to the I2O controller. No cleanup is done by this function. It is done
  657. * on the interrupt side when the reply arrives.
  658. *
  659. * Return 0 on success or negative error code on failure.
  660. */
  661. static int i2o_block_transfer(struct request *req)
  662. {
  663. struct i2o_block_device *dev = req->rq_disk->private_data;
  664. struct i2o_controller *c = dev->i2o_dev->iop;
  665. int tid = dev->i2o_dev->lct_data.tid;
  666. struct i2o_message __iomem *msg;
  667. void __iomem *mptr;
  668. struct i2o_block_request *ireq = req->special;
  669. struct scatterlist *sg;
  670. int sgnum;
  671. int i;
  672. u32 m;
  673. u32 tcntxt;
  674. u32 sg_flags;
  675. int rc;
  676. m = i2o_msg_get(c, &msg);
  677. if (m == I2O_QUEUE_EMPTY) {
  678. rc = -EBUSY;
  679. goto exit;
  680. }
  681. tcntxt = i2o_cntxt_list_add(c, req);
  682. if (!tcntxt) {
  683. rc = -ENOMEM;
  684. goto nop_msg;
  685. }
  686. if ((sgnum = i2o_block_sglist_alloc(ireq)) <= 0) {
  687. rc = -ENOMEM;
  688. goto context_remove;
  689. }
  690. /* Build the message based on the request. */
  691. writel(i2o_block_driver.context, &msg->u.s.icntxt);
  692. writel(tcntxt, &msg->u.s.tcntxt);
  693. writel(req->nr_sectors << 9, &msg->body[1]);
  694. writel((((u64) req->sector) << 9) & 0xffffffff, &msg->body[2]);
  695. writel(req->sector >> 23, &msg->body[3]);
  696. mptr = &msg->body[4];
  697. sg = ireq->sg_table;
  698. if (rq_data_dir(req) == READ) {
  699. writel(I2O_CMD_BLOCK_READ << 24 | HOST_TID << 12 | tid,
  700. &msg->u.head[1]);
  701. sg_flags = 0x10000000;
  702. switch (dev->rcache) {
  703. case CACHE_NULL:
  704. writel(0, &msg->body[0]);
  705. break;
  706. case CACHE_PREFETCH:
  707. writel(0x201F0008, &msg->body[0]);
  708. break;
  709. case CACHE_SMARTFETCH:
  710. if (req->nr_sectors > 16)
  711. writel(0x201F0008, &msg->body[0]);
  712. else
  713. writel(0x001F0000, &msg->body[0]);
  714. break;
  715. }
  716. } else {
  717. writel(I2O_CMD_BLOCK_WRITE << 24 | HOST_TID << 12 | tid,
  718. &msg->u.head[1]);
  719. sg_flags = 0x14000000;
  720. switch (dev->wcache) {
  721. case CACHE_NULL:
  722. writel(0, &msg->body[0]);
  723. break;
  724. case CACHE_WRITETHROUGH:
  725. writel(0x001F0008, &msg->body[0]);
  726. break;
  727. case CACHE_WRITEBACK:
  728. writel(0x001F0010, &msg->body[0]);
  729. break;
  730. case CACHE_SMARTBACK:
  731. if (req->nr_sectors > 16)
  732. writel(0x001F0004, &msg->body[0]);
  733. else
  734. writel(0x001F0010, &msg->body[0]);
  735. break;
  736. case CACHE_SMARTTHROUGH:
  737. if (req->nr_sectors > 16)
  738. writel(0x001F0004, &msg->body[0]);
  739. else
  740. writel(0x001F0010, &msg->body[0]);
  741. }
  742. }
  743. for (i = sgnum; i > 0; i--) {
  744. if (i == 1)
  745. sg_flags |= 0x80000000;
  746. writel(sg_flags | sg_dma_len(sg), mptr);
  747. writel(sg_dma_address(sg), mptr + 4);
  748. mptr += 8;
  749. sg++;
  750. }
  751. writel(I2O_MESSAGE_SIZE
  752. (((unsigned long)mptr -
  753. (unsigned long)&msg->u.head[0]) >> 2) | SGL_OFFSET_8,
  754. &msg->u.head[0]);
  755. list_add_tail(&ireq->queue, &dev->open_queue);
  756. dev->open_queue_depth++;
  757. i2o_msg_post(c, m);
  758. return 0;
  759. context_remove:
  760. i2o_cntxt_list_remove(c, req);
  761. nop_msg:
  762. i2o_msg_nop(c, m);
  763. exit:
  764. return rc;
  765. };
  766. /**
  767. * i2o_block_request_fn - request queue handling function
  768. * q: request queue from which the request could be fetched
  769. *
  770. * Takes the next request from the queue, transfers it and if no error
  771. * occurs dequeue it from the queue. On arrival of the reply the message
  772. * will be processed further. If an error occurs requeue the request.
  773. */
  774. static void i2o_block_request_fn(struct request_queue *q)
  775. {
  776. struct request *req;
  777. while (!blk_queue_plugged(q)) {
  778. req = elv_next_request(q);
  779. if (!req)
  780. break;
  781. if (blk_fs_request(req)) {
  782. struct i2o_block_delayed_request *dreq;
  783. struct i2o_block_request *ireq = req->special;
  784. unsigned int queue_depth;
  785. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  786. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS)
  787. if (!i2o_block_transfer(req)) {
  788. blkdev_dequeue_request(req);
  789. continue;
  790. }
  791. if (queue_depth)
  792. break;
  793. /* stop the queue and retry later */
  794. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  795. if (!dreq)
  796. continue;
  797. dreq->queue = q;
  798. INIT_WORK(&dreq->work, i2o_block_delayed_request_fn,
  799. dreq);
  800. osm_info("transfer error\n");
  801. if (!queue_delayed_work(i2o_block_driver.event_queue,
  802. &dreq->work,
  803. I2O_BLOCK_RETRY_TIME))
  804. kfree(dreq);
  805. else {
  806. blk_stop_queue(q);
  807. break;
  808. }
  809. } else
  810. end_request(req, 0);
  811. }
  812. };
  813. /* I2O Block device operations definition */
  814. static struct block_device_operations i2o_block_fops = {
  815. .owner = THIS_MODULE,
  816. .open = i2o_block_open,
  817. .release = i2o_block_release,
  818. .ioctl = i2o_block_ioctl,
  819. .media_changed = i2o_block_media_changed
  820. };
  821. /**
  822. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  823. *
  824. * Allocate memory for the i2o_block_device struct, gendisk and request
  825. * queue and initialize them as far as no additional information is needed.
  826. *
  827. * Returns a pointer to the allocated I2O Block device on succes or a
  828. * negative error code on failure.
  829. */
  830. static struct i2o_block_device *i2o_block_device_alloc(void)
  831. {
  832. struct i2o_block_device *dev;
  833. struct gendisk *gd;
  834. struct request_queue *queue;
  835. int rc;
  836. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  837. if (!dev) {
  838. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  839. rc = -ENOMEM;
  840. goto exit;
  841. }
  842. memset(dev, 0, sizeof(*dev));
  843. INIT_LIST_HEAD(&dev->open_queue);
  844. spin_lock_init(&dev->lock);
  845. dev->rcache = CACHE_PREFETCH;
  846. dev->wcache = CACHE_WRITEBACK;
  847. /* allocate a gendisk with 16 partitions */
  848. gd = alloc_disk(16);
  849. if (!gd) {
  850. osm_err("Insufficient memory to allocate gendisk.\n");
  851. rc = -ENOMEM;
  852. goto cleanup_dev;
  853. }
  854. /* initialize the request queue */
  855. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  856. if (!queue) {
  857. osm_err("Insufficient memory to allocate request queue.\n");
  858. rc = -ENOMEM;
  859. goto cleanup_queue;
  860. }
  861. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  862. gd->major = I2O_MAJOR;
  863. gd->queue = queue;
  864. gd->fops = &i2o_block_fops;
  865. gd->private_data = dev;
  866. dev->gd = gd;
  867. return dev;
  868. cleanup_queue:
  869. put_disk(gd);
  870. cleanup_dev:
  871. kfree(dev);
  872. exit:
  873. return ERR_PTR(rc);
  874. };
  875. /**
  876. * i2o_block_probe - verify if dev is a I2O Block device and install it
  877. * @dev: device to verify if it is a I2O Block device
  878. *
  879. * We only verify if the user_tid of the device is 0xfff and then install
  880. * the device. Otherwise it is used by some other device (e. g. RAID).
  881. *
  882. * Returns 0 on success or negative error code on failure.
  883. */
  884. static int i2o_block_probe(struct device *dev)
  885. {
  886. struct i2o_device *i2o_dev = to_i2o_device(dev);
  887. struct i2o_block_device *i2o_blk_dev;
  888. struct i2o_controller *c = i2o_dev->iop;
  889. struct gendisk *gd;
  890. struct request_queue *queue;
  891. static int unit = 0;
  892. int rc;
  893. u64 size;
  894. u32 blocksize;
  895. u16 power;
  896. u32 flags, status;
  897. int segments;
  898. /* skip devices which are used by IOP */
  899. if (i2o_dev->lct_data.user_tid != 0xfff) {
  900. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  901. return -ENODEV;
  902. }
  903. osm_info("New device detected (TID: %03x)\n", i2o_dev->lct_data.tid);
  904. if (i2o_device_claim(i2o_dev)) {
  905. osm_warn("Unable to claim device. Installation aborted\n");
  906. rc = -EFAULT;
  907. goto exit;
  908. }
  909. i2o_blk_dev = i2o_block_device_alloc();
  910. if (IS_ERR(i2o_blk_dev)) {
  911. osm_err("could not alloc a new I2O block device");
  912. rc = PTR_ERR(i2o_blk_dev);
  913. goto claim_release;
  914. }
  915. i2o_blk_dev->i2o_dev = i2o_dev;
  916. dev_set_drvdata(dev, i2o_blk_dev);
  917. /* setup gendisk */
  918. gd = i2o_blk_dev->gd;
  919. gd->first_minor = unit << 4;
  920. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  921. sprintf(gd->devfs_name, "i2o/hd%c", 'a' + unit);
  922. gd->driverfs_dev = &i2o_dev->device;
  923. /* setup request queue */
  924. queue = gd->queue;
  925. queue->queuedata = i2o_blk_dev;
  926. blk_queue_max_phys_segments(queue, I2O_MAX_SEGMENTS);
  927. blk_queue_max_sectors(queue, I2O_MAX_SECTORS);
  928. if (c->short_req)
  929. segments = 8;
  930. else {
  931. i2o_status_block *sb;
  932. sb = c->status_block.virt;
  933. segments = (sb->inbound_frame_size -
  934. sizeof(struct i2o_message) / 4 - 4) / 2;
  935. }
  936. blk_queue_max_hw_segments(queue, segments);
  937. osm_debug("max sectors = %d\n", I2O_MAX_SECTORS);
  938. osm_debug("phys segments = %d\n", I2O_MAX_SEGMENTS);
  939. osm_debug("hw segments = %d\n", segments);
  940. /*
  941. * Ask for the current media data. If that isn't supported
  942. * then we ask for the device capacity data
  943. */
  944. if (i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) != 0
  945. || i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) != 0) {
  946. i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4);
  947. i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8);
  948. }
  949. osm_debug("blocksize = %d\n", blocksize);
  950. if (i2o_parm_field_get(i2o_dev, 0x0000, 2, &power, 2))
  951. power = 0;
  952. i2o_parm_field_get(i2o_dev, 0x0000, 5, &flags, 4);
  953. i2o_parm_field_get(i2o_dev, 0x0000, 6, &status, 4);
  954. set_capacity(gd, size >> 9);
  955. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  956. add_disk(gd);
  957. unit++;
  958. return 0;
  959. claim_release:
  960. i2o_device_claim_release(i2o_dev);
  961. exit:
  962. return rc;
  963. };
  964. /* Block OSM driver struct */
  965. static struct i2o_driver i2o_block_driver = {
  966. .name = OSM_NAME,
  967. .event = i2o_block_event,
  968. .reply = i2o_block_reply,
  969. .classes = i2o_block_class_id,
  970. .driver = {
  971. .probe = i2o_block_probe,
  972. .remove = i2o_block_remove,
  973. },
  974. };
  975. /**
  976. * i2o_block_init - Block OSM initialization function
  977. *
  978. * Allocate the slab and mempool for request structs, registers i2o_block
  979. * block device and finally register the Block OSM in the I2O core.
  980. *
  981. * Returns 0 on success or negative error code on failure.
  982. */
  983. static int __init i2o_block_init(void)
  984. {
  985. int rc;
  986. int size;
  987. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  988. /* Allocate request mempool and slab */
  989. size = sizeof(struct i2o_block_request);
  990. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  991. SLAB_HWCACHE_ALIGN, NULL,
  992. NULL);
  993. if (!i2o_blk_req_pool.slab) {
  994. osm_err("can't init request slab\n");
  995. rc = -ENOMEM;
  996. goto exit;
  997. }
  998. i2o_blk_req_pool.pool = mempool_create(I2O_REQ_MEMPOOL_SIZE,
  999. mempool_alloc_slab,
  1000. mempool_free_slab,
  1001. i2o_blk_req_pool.slab);
  1002. if (!i2o_blk_req_pool.pool) {
  1003. osm_err("can't init request mempool\n");
  1004. rc = -ENOMEM;
  1005. goto free_slab;
  1006. }
  1007. /* Register the block device interfaces */
  1008. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  1009. if (rc) {
  1010. osm_err("unable to register block device\n");
  1011. goto free_mempool;
  1012. }
  1013. #ifdef MODULE
  1014. osm_info("registered device at major %d\n", I2O_MAJOR);
  1015. #endif
  1016. /* Register Block OSM into I2O core */
  1017. rc = i2o_driver_register(&i2o_block_driver);
  1018. if (rc) {
  1019. osm_err("Could not register Block driver\n");
  1020. goto unregister_blkdev;
  1021. }
  1022. return 0;
  1023. unregister_blkdev:
  1024. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1025. free_mempool:
  1026. mempool_destroy(i2o_blk_req_pool.pool);
  1027. free_slab:
  1028. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1029. exit:
  1030. return rc;
  1031. };
  1032. /**
  1033. * i2o_block_exit - Block OSM exit function
  1034. *
  1035. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  1036. * and frees the mempool and slab.
  1037. */
  1038. static void __exit i2o_block_exit(void)
  1039. {
  1040. /* Unregister I2O Block OSM from I2O core */
  1041. i2o_driver_unregister(&i2o_block_driver);
  1042. /* Unregister block device */
  1043. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1044. /* Free request mempool and slab */
  1045. mempool_destroy(i2o_blk_req_pool.pool);
  1046. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1047. };
  1048. MODULE_AUTHOR("Red Hat");
  1049. MODULE_LICENSE("GPL");
  1050. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1051. MODULE_VERSION(OSM_VERSION);
  1052. module_init(i2o_block_init);
  1053. module_exit(i2o_block_exit);