i2o_block.c 31 KB

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