i2o_block.c 31 KB

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