i2o_block.c 31 KB

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