ibmvscsi.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robbin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/vio.h>
  73. #include <scsi/scsi.h>
  74. #include <scsi/scsi_cmnd.h>
  75. #include <scsi/scsi_host.h>
  76. #include <scsi/scsi_device.h>
  77. #include "ibmvscsi.h"
  78. /* The values below are somewhat arbitrary default values, but
  79. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  80. * Note that there are 3 bits of channel value, 6 bits of id, and
  81. * 5 bits of LUN.
  82. */
  83. static int max_id = 64;
  84. static int max_channel = 3;
  85. static int init_timeout = 5;
  86. static int max_requests = 50;
  87. #define IBMVSCSI_VERSION "1.5.6"
  88. MODULE_DESCRIPTION("IBM Virtual SCSI");
  89. MODULE_AUTHOR("Dave Boutcher");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(IBMVSCSI_VERSION);
  92. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  94. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(max_channel, "Largest channel value");
  96. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  98. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  100. /* ------------------------------------------------------------
  101. * Routines for the event pool and event structs
  102. */
  103. /**
  104. * initialize_event_pool: - Allocates and initializes the event pool for a host
  105. * @pool: event_pool to be initialized
  106. * @size: Number of events in pool
  107. * @hostdata: ibmvscsi_host_data who owns the event pool
  108. *
  109. * Returns zero on success.
  110. */
  111. static int initialize_event_pool(struct event_pool *pool,
  112. int size, struct ibmvscsi_host_data *hostdata)
  113. {
  114. int i;
  115. pool->size = size;
  116. pool->next = 0;
  117. pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
  118. if (!pool->events)
  119. return -ENOMEM;
  120. memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
  121. pool->iu_storage =
  122. dma_alloc_coherent(hostdata->dev,
  123. pool->size * sizeof(*pool->iu_storage),
  124. &pool->iu_token, 0);
  125. if (!pool->iu_storage) {
  126. kfree(pool->events);
  127. return -ENOMEM;
  128. }
  129. for (i = 0; i < pool->size; ++i) {
  130. struct srp_event_struct *evt = &pool->events[i];
  131. memset(&evt->crq, 0x00, sizeof(evt->crq));
  132. atomic_set(&evt->free, 1);
  133. evt->crq.valid = 0x80;
  134. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  135. evt->crq.IU_data_ptr = pool->iu_token +
  136. sizeof(*evt->xfer_iu) * i;
  137. evt->xfer_iu = pool->iu_storage + i;
  138. evt->hostdata = hostdata;
  139. }
  140. return 0;
  141. }
  142. /**
  143. * release_event_pool: - Frees memory of an event pool of a host
  144. * @pool: event_pool to be released
  145. * @hostdata: ibmvscsi_host_data who owns the even pool
  146. *
  147. * Returns zero on success.
  148. */
  149. static void release_event_pool(struct event_pool *pool,
  150. struct ibmvscsi_host_data *hostdata)
  151. {
  152. int i, in_use = 0;
  153. for (i = 0; i < pool->size; ++i)
  154. if (atomic_read(&pool->events[i].free) != 1)
  155. ++in_use;
  156. if (in_use)
  157. printk(KERN_WARNING
  158. "ibmvscsi: releasing event pool with %d "
  159. "events still in use?\n", in_use);
  160. kfree(pool->events);
  161. dma_free_coherent(hostdata->dev,
  162. pool->size * sizeof(*pool->iu_storage),
  163. pool->iu_storage, pool->iu_token);
  164. }
  165. /**
  166. * valid_event_struct: - Determines if event is valid.
  167. * @pool: event_pool that contains the event
  168. * @evt: srp_event_struct to be checked for validity
  169. *
  170. * Returns zero if event is invalid, one otherwise.
  171. */
  172. static int valid_event_struct(struct event_pool *pool,
  173. struct srp_event_struct *evt)
  174. {
  175. int index = evt - pool->events;
  176. if (index < 0 || index >= pool->size) /* outside of bounds */
  177. return 0;
  178. if (evt != pool->events + index) /* unaligned */
  179. return 0;
  180. return 1;
  181. }
  182. /**
  183. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  184. * @pool: event_pool that contains the event
  185. * @evt: srp_event_struct to be modified
  186. *
  187. */
  188. static void free_event_struct(struct event_pool *pool,
  189. struct srp_event_struct *evt)
  190. {
  191. if (!valid_event_struct(pool, evt)) {
  192. printk(KERN_ERR
  193. "ibmvscsi: Freeing invalid event_struct %p "
  194. "(not in pool %p)\n", evt, pool->events);
  195. return;
  196. }
  197. if (atomic_inc_return(&evt->free) != 1) {
  198. printk(KERN_ERR
  199. "ibmvscsi: Freeing event_struct %p "
  200. "which is not in use!\n", evt);
  201. return;
  202. }
  203. }
  204. /**
  205. * get_evt_struct: - Gets the next free event in pool
  206. * @pool: event_pool that contains the events to be searched
  207. *
  208. * Returns the next event in "free" state, and NULL if none are free.
  209. * Note that no synchronization is done here, we assume the host_lock
  210. * will syncrhonze things.
  211. */
  212. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  213. {
  214. int i;
  215. int poolsize = pool->size;
  216. int offset = pool->next;
  217. for (i = 0; i < poolsize; i++) {
  218. offset = (offset + 1) % poolsize;
  219. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  220. pool->next = offset;
  221. return &pool->events[offset];
  222. }
  223. }
  224. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  225. return NULL;
  226. }
  227. /**
  228. * init_event_struct: Initialize fields in an event struct that are always
  229. * required.
  230. * @evt: The event
  231. * @done: Routine to call when the event is responded to
  232. * @format: SRP or MAD format
  233. * @timeout: timeout value set in the CRQ
  234. */
  235. static void init_event_struct(struct srp_event_struct *evt_struct,
  236. void (*done) (struct srp_event_struct *),
  237. u8 format,
  238. int timeout)
  239. {
  240. evt_struct->cmnd = NULL;
  241. evt_struct->cmnd_done = NULL;
  242. evt_struct->sync_srp = NULL;
  243. evt_struct->crq.format = format;
  244. evt_struct->crq.timeout = timeout;
  245. evt_struct->done = done;
  246. }
  247. /* ------------------------------------------------------------
  248. * Routines for receiving SCSI responses from the hosting partition
  249. */
  250. /**
  251. * set_srp_direction: Set the fields in the srp related to data
  252. * direction and number of buffers based on the direction in
  253. * the scsi_cmnd and the number of buffers
  254. */
  255. static void set_srp_direction(struct scsi_cmnd *cmd,
  256. struct srp_cmd *srp_cmd,
  257. int numbuf)
  258. {
  259. if (numbuf == 0)
  260. return;
  261. if (numbuf == 1) {
  262. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  263. srp_cmd->data_out_format = SRP_DIRECT_BUFFER;
  264. else
  265. srp_cmd->data_in_format = SRP_DIRECT_BUFFER;
  266. } else {
  267. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  268. srp_cmd->data_out_format = SRP_INDIRECT_BUFFER;
  269. srp_cmd->data_out_count = numbuf;
  270. } else {
  271. srp_cmd->data_in_format = SRP_INDIRECT_BUFFER;
  272. srp_cmd->data_in_count = numbuf;
  273. }
  274. }
  275. }
  276. /**
  277. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  278. * @cmd: srp_cmd whose additional_data member will be unmapped
  279. * @dev: device for which the memory is mapped
  280. *
  281. */
  282. static void unmap_cmd_data(struct srp_cmd *cmd, struct device *dev)
  283. {
  284. int i;
  285. if ((cmd->data_out_format == SRP_NO_BUFFER) &&
  286. (cmd->data_in_format == SRP_NO_BUFFER))
  287. return;
  288. else if ((cmd->data_out_format == SRP_DIRECT_BUFFER) ||
  289. (cmd->data_in_format == SRP_DIRECT_BUFFER)) {
  290. struct memory_descriptor *data =
  291. (struct memory_descriptor *)cmd->additional_data;
  292. dma_unmap_single(dev, data->virtual_address, data->length,
  293. DMA_BIDIRECTIONAL);
  294. } else {
  295. struct indirect_descriptor *indirect =
  296. (struct indirect_descriptor *)cmd->additional_data;
  297. int num_mapped = indirect->head.length /
  298. sizeof(indirect->list[0]);
  299. for (i = 0; i < num_mapped; ++i) {
  300. struct memory_descriptor *data = &indirect->list[i];
  301. dma_unmap_single(dev,
  302. data->virtual_address,
  303. data->length, DMA_BIDIRECTIONAL);
  304. }
  305. }
  306. }
  307. /**
  308. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  309. * @cmd: Scsi_Cmnd with the scatterlist
  310. * @srp_cmd: srp_cmd that contains the memory descriptor
  311. * @dev: device for which to map dma memory
  312. *
  313. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  314. * Returns 1 on success.
  315. */
  316. static int map_sg_data(struct scsi_cmnd *cmd,
  317. struct srp_cmd *srp_cmd, struct device *dev)
  318. {
  319. int i, sg_mapped;
  320. u64 total_length = 0;
  321. struct scatterlist *sg = cmd->request_buffer;
  322. struct memory_descriptor *data =
  323. (struct memory_descriptor *)srp_cmd->additional_data;
  324. struct indirect_descriptor *indirect =
  325. (struct indirect_descriptor *)data;
  326. sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
  327. if (sg_mapped == 0)
  328. return 0;
  329. set_srp_direction(cmd, srp_cmd, sg_mapped);
  330. /* special case; we can use a single direct descriptor */
  331. if (sg_mapped == 1) {
  332. data->virtual_address = sg_dma_address(&sg[0]);
  333. data->length = sg_dma_len(&sg[0]);
  334. data->memory_handle = 0;
  335. return 1;
  336. }
  337. if (sg_mapped > MAX_INDIRECT_BUFS) {
  338. printk(KERN_ERR
  339. "ibmvscsi: More than %d mapped sg entries, got %d\n",
  340. MAX_INDIRECT_BUFS, sg_mapped);
  341. return 0;
  342. }
  343. indirect->head.virtual_address = 0;
  344. indirect->head.length = sg_mapped * sizeof(indirect->list[0]);
  345. indirect->head.memory_handle = 0;
  346. for (i = 0; i < sg_mapped; ++i) {
  347. struct memory_descriptor *descr = &indirect->list[i];
  348. struct scatterlist *sg_entry = &sg[i];
  349. descr->virtual_address = sg_dma_address(sg_entry);
  350. descr->length = sg_dma_len(sg_entry);
  351. descr->memory_handle = 0;
  352. total_length += sg_dma_len(sg_entry);
  353. }
  354. indirect->total_length = total_length;
  355. return 1;
  356. }
  357. /**
  358. * map_single_data: - Maps memory and initializes memory decriptor fields
  359. * @cmd: struct scsi_cmnd with the memory to be mapped
  360. * @srp_cmd: srp_cmd that contains the memory descriptor
  361. * @dev: device for which to map dma memory
  362. *
  363. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  364. * Returns 1 on success.
  365. */
  366. static int map_single_data(struct scsi_cmnd *cmd,
  367. struct srp_cmd *srp_cmd, struct device *dev)
  368. {
  369. struct memory_descriptor *data =
  370. (struct memory_descriptor *)srp_cmd->additional_data;
  371. data->virtual_address =
  372. dma_map_single(dev, cmd->request_buffer,
  373. cmd->request_bufflen,
  374. DMA_BIDIRECTIONAL);
  375. if (dma_mapping_error(data->virtual_address)) {
  376. printk(KERN_ERR
  377. "ibmvscsi: Unable to map request_buffer for command!\n");
  378. return 0;
  379. }
  380. data->length = cmd->request_bufflen;
  381. data->memory_handle = 0;
  382. set_srp_direction(cmd, srp_cmd, 1);
  383. return 1;
  384. }
  385. /**
  386. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  387. * @cmd: struct scsi_cmnd with the memory to be mapped
  388. * @srp_cmd: srp_cmd that contains the memory descriptor
  389. * @dev: dma device for which to map dma memory
  390. *
  391. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  392. * Returns 1 on success.
  393. */
  394. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  395. struct srp_cmd *srp_cmd, struct device *dev)
  396. {
  397. switch (cmd->sc_data_direction) {
  398. case DMA_FROM_DEVICE:
  399. case DMA_TO_DEVICE:
  400. break;
  401. case DMA_NONE:
  402. return 1;
  403. case DMA_BIDIRECTIONAL:
  404. printk(KERN_ERR
  405. "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
  406. return 0;
  407. default:
  408. printk(KERN_ERR
  409. "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
  410. cmd->sc_data_direction);
  411. return 0;
  412. }
  413. if (!cmd->request_buffer)
  414. return 1;
  415. if (cmd->use_sg)
  416. return map_sg_data(cmd, srp_cmd, dev);
  417. return map_single_data(cmd, srp_cmd, dev);
  418. }
  419. /* ------------------------------------------------------------
  420. * Routines for sending and receiving SRPs
  421. */
  422. /**
  423. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  424. * @evt_struct: evt_struct to be sent
  425. * @hostdata: ibmvscsi_host_data of host
  426. *
  427. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  428. * Note that this routine assumes that host_lock is held for synchronization
  429. */
  430. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  431. struct ibmvscsi_host_data *hostdata)
  432. {
  433. struct scsi_cmnd *cmnd;
  434. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  435. int rc;
  436. /* If we have exhausted our request limit, just fail this request.
  437. * Note that there are rare cases involving driver generated requests
  438. * (such as task management requests) that the mid layer may think we
  439. * can handle more requests (can_queue) when we actually can't
  440. */
  441. if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
  442. (atomic_dec_if_positive(&hostdata->request_limit) < 0)) {
  443. /* See if the adapter is disabled */
  444. if (atomic_read(&hostdata->request_limit) < 0)
  445. goto send_error;
  446. printk(KERN_WARNING
  447. "ibmvscsi: Warning, request_limit exceeded\n");
  448. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  449. hostdata->dev);
  450. free_event_struct(&hostdata->pool, evt_struct);
  451. return SCSI_MLQUEUE_HOST_BUSY;
  452. }
  453. /* Copy the IU into the transfer area */
  454. *evt_struct->xfer_iu = evt_struct->iu;
  455. evt_struct->xfer_iu->srp.generic.tag = (u64)evt_struct;
  456. /* Add this to the sent list. We need to do this
  457. * before we actually send
  458. * in case it comes back REALLY fast
  459. */
  460. list_add_tail(&evt_struct->list, &hostdata->sent);
  461. if ((rc =
  462. ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  463. list_del(&evt_struct->list);
  464. printk(KERN_ERR "ibmvscsi: failed to send event struct rc %d\n",
  465. rc);
  466. goto send_error;
  467. }
  468. return 0;
  469. send_error:
  470. unmap_cmd_data(&evt_struct->iu.srp.cmd, hostdata->dev);
  471. if ((cmnd = evt_struct->cmnd) != NULL) {
  472. cmnd->result = DID_ERROR << 16;
  473. evt_struct->cmnd_done(cmnd);
  474. } else if (evt_struct->done)
  475. evt_struct->done(evt_struct);
  476. free_event_struct(&hostdata->pool, evt_struct);
  477. return 0;
  478. }
  479. /**
  480. * handle_cmd_rsp: - Handle responses from commands
  481. * @evt_struct: srp_event_struct to be handled
  482. *
  483. * Used as a callback by when sending scsi cmds.
  484. * Gets called by ibmvscsi_handle_crq()
  485. */
  486. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  487. {
  488. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  489. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  490. if (unlikely(rsp->type != SRP_RSP_TYPE)) {
  491. if (printk_ratelimit())
  492. printk(KERN_WARNING
  493. "ibmvscsi: bad SRP RSP type %d\n",
  494. rsp->type);
  495. }
  496. if (cmnd) {
  497. cmnd->result = rsp->status;
  498. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  499. memcpy(cmnd->sense_buffer,
  500. rsp->sense_and_response_data,
  501. rsp->sense_data_list_length);
  502. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  503. evt_struct->hostdata->dev);
  504. if (rsp->doover)
  505. cmnd->resid = rsp->data_out_residual_count;
  506. else if (rsp->diover)
  507. cmnd->resid = rsp->data_in_residual_count;
  508. }
  509. if (evt_struct->cmnd_done)
  510. evt_struct->cmnd_done(cmnd);
  511. }
  512. /**
  513. * lun_from_dev: - Returns the lun of the scsi device
  514. * @dev: struct scsi_device
  515. *
  516. */
  517. static inline u16 lun_from_dev(struct scsi_device *dev)
  518. {
  519. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  520. }
  521. /**
  522. * ibmvscsi_queue: - The queuecommand function of the scsi template
  523. * @cmd: struct scsi_cmnd to be executed
  524. * @done: Callback function to be called when cmd is completed
  525. */
  526. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  527. void (*done) (struct scsi_cmnd *))
  528. {
  529. struct srp_cmd *srp_cmd;
  530. struct srp_event_struct *evt_struct;
  531. struct ibmvscsi_host_data *hostdata =
  532. (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
  533. u16 lun = lun_from_dev(cmnd->device);
  534. evt_struct = get_event_struct(&hostdata->pool);
  535. if (!evt_struct)
  536. return SCSI_MLQUEUE_HOST_BUSY;
  537. init_event_struct(evt_struct,
  538. handle_cmd_rsp,
  539. VIOSRP_SRP_FORMAT,
  540. cmnd->timeout);
  541. evt_struct->cmnd = cmnd;
  542. evt_struct->cmnd_done = done;
  543. /* Set up the actual SRP IU */
  544. srp_cmd = &evt_struct->iu.srp.cmd;
  545. memset(srp_cmd, 0x00, sizeof(*srp_cmd));
  546. srp_cmd->type = SRP_CMD_TYPE;
  547. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
  548. srp_cmd->lun = ((u64) lun) << 48;
  549. if (!map_data_for_srp_cmd(cmnd, srp_cmd, hostdata->dev)) {
  550. printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
  551. free_event_struct(&hostdata->pool, evt_struct);
  552. return SCSI_MLQUEUE_HOST_BUSY;
  553. }
  554. /* Fix up dma address of the buffer itself */
  555. if ((srp_cmd->data_out_format == SRP_INDIRECT_BUFFER) ||
  556. (srp_cmd->data_in_format == SRP_INDIRECT_BUFFER)) {
  557. struct indirect_descriptor *indirect =
  558. (struct indirect_descriptor *)srp_cmd->additional_data;
  559. indirect->head.virtual_address = evt_struct->crq.IU_data_ptr +
  560. offsetof(struct srp_cmd, additional_data) +
  561. offsetof(struct indirect_descriptor, list);
  562. }
  563. return ibmvscsi_send_srp_event(evt_struct, hostdata);
  564. }
  565. /* ------------------------------------------------------------
  566. * Routines for driver initialization
  567. */
  568. /**
  569. * adapter_info_rsp: - Handle response to MAD adapter info request
  570. * @evt_struct: srp_event_struct with the response
  571. *
  572. * Used as a "done" callback by when sending adapter_info. Gets called
  573. * by ibmvscsi_handle_crq()
  574. */
  575. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  576. {
  577. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  578. dma_unmap_single(hostdata->dev,
  579. evt_struct->iu.mad.adapter_info.buffer,
  580. evt_struct->iu.mad.adapter_info.common.length,
  581. DMA_BIDIRECTIONAL);
  582. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  583. printk("ibmvscsi: error %d getting adapter info\n",
  584. evt_struct->xfer_iu->mad.adapter_info.common.status);
  585. } else {
  586. printk("ibmvscsi: host srp version: %s, "
  587. "host partition %s (%d), OS %d, max io %u\n",
  588. hostdata->madapter_info.srp_version,
  589. hostdata->madapter_info.partition_name,
  590. hostdata->madapter_info.partition_number,
  591. hostdata->madapter_info.os_type,
  592. hostdata->madapter_info.port_max_txu[0]);
  593. if (hostdata->madapter_info.port_max_txu[0])
  594. hostdata->host->max_sectors =
  595. hostdata->madapter_info.port_max_txu[0] >> 9;
  596. }
  597. }
  598. /**
  599. * send_mad_adapter_info: - Sends the mad adapter info request
  600. * and stores the result so it can be retrieved with
  601. * sysfs. We COULD consider causing a failure if the
  602. * returned SRP version doesn't match ours.
  603. * @hostdata: ibmvscsi_host_data of host
  604. *
  605. * Returns zero if successful.
  606. */
  607. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  608. {
  609. struct viosrp_adapter_info *req;
  610. struct srp_event_struct *evt_struct;
  611. evt_struct = get_event_struct(&hostdata->pool);
  612. if (!evt_struct) {
  613. printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
  614. "for ADAPTER_INFO_REQ!\n");
  615. return;
  616. }
  617. init_event_struct(evt_struct,
  618. adapter_info_rsp,
  619. VIOSRP_MAD_FORMAT,
  620. init_timeout * HZ);
  621. req = &evt_struct->iu.mad.adapter_info;
  622. memset(req, 0x00, sizeof(*req));
  623. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  624. req->common.length = sizeof(hostdata->madapter_info);
  625. req->buffer = dma_map_single(hostdata->dev,
  626. &hostdata->madapter_info,
  627. sizeof(hostdata->madapter_info),
  628. DMA_BIDIRECTIONAL);
  629. if (dma_mapping_error(req->buffer)) {
  630. printk(KERN_ERR
  631. "ibmvscsi: Unable to map request_buffer "
  632. "for adapter_info!\n");
  633. free_event_struct(&hostdata->pool, evt_struct);
  634. return;
  635. }
  636. if (ibmvscsi_send_srp_event(evt_struct, hostdata))
  637. printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
  638. };
  639. /**
  640. * login_rsp: - Handle response to SRP login request
  641. * @evt_struct: srp_event_struct with the response
  642. *
  643. * Used as a "done" callback by when sending srp_login. Gets called
  644. * by ibmvscsi_handle_crq()
  645. */
  646. static void login_rsp(struct srp_event_struct *evt_struct)
  647. {
  648. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  649. switch (evt_struct->xfer_iu->srp.generic.type) {
  650. case SRP_LOGIN_RSP_TYPE: /* it worked! */
  651. break;
  652. case SRP_LOGIN_REJ_TYPE: /* refused! */
  653. printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REQ rejected\n");
  654. /* Login failed. */
  655. atomic_set(&hostdata->request_limit, -1);
  656. return;
  657. default:
  658. printk(KERN_ERR
  659. "ibmvscsi: Invalid login response typecode 0x%02x!\n",
  660. evt_struct->xfer_iu->srp.generic.type);
  661. /* Login failed. */
  662. atomic_set(&hostdata->request_limit, -1);
  663. return;
  664. }
  665. printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
  666. if (evt_struct->xfer_iu->srp.login_rsp.request_limit_delta >
  667. (max_requests - 2))
  668. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta =
  669. max_requests - 2;
  670. /* Now we know what the real request-limit is */
  671. atomic_set(&hostdata->request_limit,
  672. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta);
  673. hostdata->host->can_queue =
  674. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta - 2;
  675. if (hostdata->host->can_queue < 1) {
  676. printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
  677. return;
  678. }
  679. send_mad_adapter_info(hostdata);
  680. return;
  681. }
  682. /**
  683. * send_srp_login: - Sends the srp login
  684. * @hostdata: ibmvscsi_host_data of host
  685. *
  686. * Returns zero if successful.
  687. */
  688. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  689. {
  690. int rc;
  691. unsigned long flags;
  692. struct srp_login_req *login;
  693. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  694. if (!evt_struct) {
  695. printk(KERN_ERR
  696. "ibmvscsi: couldn't allocate an event for login req!\n");
  697. return FAILED;
  698. }
  699. init_event_struct(evt_struct,
  700. login_rsp,
  701. VIOSRP_SRP_FORMAT,
  702. init_timeout * HZ);
  703. login = &evt_struct->iu.srp.login_req;
  704. login->type = SRP_LOGIN_REQ_TYPE;
  705. login->max_requested_initiator_to_target_iulen = sizeof(union srp_iu);
  706. login->required_buffer_formats = 0x0006;
  707. /* Start out with a request limit of 1, since this is negotiated in
  708. * the login request we are just sending
  709. */
  710. atomic_set(&hostdata->request_limit, 1);
  711. spin_lock_irqsave(hostdata->host->host_lock, flags);
  712. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  713. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  714. return rc;
  715. };
  716. /**
  717. * sync_completion: Signal that a synchronous command has completed
  718. * Note that after returning from this call, the evt_struct is freed.
  719. * the caller waiting on this completion shouldn't touch the evt_struct
  720. * again.
  721. */
  722. static void sync_completion(struct srp_event_struct *evt_struct)
  723. {
  724. /* copy the response back */
  725. if (evt_struct->sync_srp)
  726. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  727. complete(&evt_struct->comp);
  728. }
  729. /**
  730. * ibmvscsi_abort: Abort a command...from scsi host template
  731. * send this over to the server and wait synchronously for the response
  732. */
  733. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  734. {
  735. struct ibmvscsi_host_data *hostdata =
  736. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  737. struct srp_tsk_mgmt *tsk_mgmt;
  738. struct srp_event_struct *evt;
  739. struct srp_event_struct *tmp_evt, *found_evt;
  740. union viosrp_iu srp_rsp;
  741. int rsp_rc;
  742. u16 lun = lun_from_dev(cmd->device);
  743. /* First, find this command in our sent list so we can figure
  744. * out the correct tag
  745. */
  746. found_evt = NULL;
  747. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  748. if (tmp_evt->cmnd == cmd) {
  749. found_evt = tmp_evt;
  750. break;
  751. }
  752. }
  753. if (!found_evt)
  754. return FAILED;
  755. evt = get_event_struct(&hostdata->pool);
  756. if (evt == NULL) {
  757. printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
  758. return FAILED;
  759. }
  760. init_event_struct(evt,
  761. sync_completion,
  762. VIOSRP_SRP_FORMAT,
  763. init_timeout * HZ);
  764. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  765. /* Set up an abort SRP command */
  766. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  767. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  768. tsk_mgmt->lun = ((u64) lun) << 48;
  769. tsk_mgmt->task_mgmt_flags = 0x01; /* ABORT TASK */
  770. tsk_mgmt->managed_task_tag = (u64) found_evt;
  771. printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
  772. tsk_mgmt->lun, tsk_mgmt->managed_task_tag);
  773. evt->sync_srp = &srp_rsp;
  774. init_completion(&evt->comp);
  775. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  776. printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
  777. return FAILED;
  778. }
  779. wait_for_completion(&evt->comp);
  780. /* make sure we got a good response */
  781. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  782. if (printk_ratelimit())
  783. printk(KERN_WARNING
  784. "ibmvscsi: abort bad SRP RSP type %d\n",
  785. srp_rsp.srp.generic.type);
  786. return FAILED;
  787. }
  788. if (srp_rsp.srp.rsp.rspvalid)
  789. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  790. else
  791. rsp_rc = srp_rsp.srp.rsp.status;
  792. if (rsp_rc) {
  793. if (printk_ratelimit())
  794. printk(KERN_WARNING
  795. "ibmvscsi: abort code %d for task tag 0x%lx\n",
  796. rsp_rc,
  797. tsk_mgmt->managed_task_tag);
  798. return FAILED;
  799. }
  800. /* Because we dropped the spinlock above, it's possible
  801. * The event is no longer in our list. Make sure it didn't
  802. * complete while we were aborting
  803. */
  804. found_evt = NULL;
  805. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  806. if (tmp_evt->cmnd == cmd) {
  807. found_evt = tmp_evt;
  808. break;
  809. }
  810. }
  811. if (found_evt == NULL) {
  812. printk(KERN_INFO
  813. "ibmvscsi: aborted task tag 0x%lx completed\n",
  814. tsk_mgmt->managed_task_tag);
  815. return SUCCESS;
  816. }
  817. printk(KERN_INFO
  818. "ibmvscsi: successfully aborted task tag 0x%lx\n",
  819. tsk_mgmt->managed_task_tag);
  820. cmd->result = (DID_ABORT << 16);
  821. list_del(&found_evt->list);
  822. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt->hostdata->dev);
  823. free_event_struct(&found_evt->hostdata->pool, found_evt);
  824. atomic_inc(&hostdata->request_limit);
  825. return SUCCESS;
  826. }
  827. /**
  828. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  829. * template send this over to the server and wait synchronously for the
  830. * response
  831. */
  832. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  833. {
  834. struct ibmvscsi_host_data *hostdata =
  835. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  836. struct srp_tsk_mgmt *tsk_mgmt;
  837. struct srp_event_struct *evt;
  838. struct srp_event_struct *tmp_evt, *pos;
  839. union viosrp_iu srp_rsp;
  840. int rsp_rc;
  841. u16 lun = lun_from_dev(cmd->device);
  842. evt = get_event_struct(&hostdata->pool);
  843. if (evt == NULL) {
  844. printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
  845. return FAILED;
  846. }
  847. init_event_struct(evt,
  848. sync_completion,
  849. VIOSRP_SRP_FORMAT,
  850. init_timeout * HZ);
  851. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  852. /* Set up a lun reset SRP command */
  853. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  854. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  855. tsk_mgmt->lun = ((u64) lun) << 48;
  856. tsk_mgmt->task_mgmt_flags = 0x08; /* LUN RESET */
  857. printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
  858. tsk_mgmt->lun);
  859. evt->sync_srp = &srp_rsp;
  860. init_completion(&evt->comp);
  861. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  862. printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
  863. return FAILED;
  864. }
  865. wait_for_completion(&evt->comp);
  866. /* make sure we got a good response */
  867. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  868. if (printk_ratelimit())
  869. printk(KERN_WARNING
  870. "ibmvscsi: reset bad SRP RSP type %d\n",
  871. srp_rsp.srp.generic.type);
  872. return FAILED;
  873. }
  874. if (srp_rsp.srp.rsp.rspvalid)
  875. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  876. else
  877. rsp_rc = srp_rsp.srp.rsp.status;
  878. if (rsp_rc) {
  879. if (printk_ratelimit())
  880. printk(KERN_WARNING
  881. "ibmvscsi: reset code %d for task tag 0x%lx\n",
  882. rsp_rc,
  883. tsk_mgmt->managed_task_tag);
  884. return FAILED;
  885. }
  886. /* We need to find all commands for this LUN that have not yet been
  887. * responded to, and fail them with DID_RESET
  888. */
  889. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  890. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  891. if (tmp_evt->cmnd)
  892. tmp_evt->cmnd->result = (DID_RESET << 16);
  893. list_del(&tmp_evt->list);
  894. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt->hostdata->dev);
  895. free_event_struct(&tmp_evt->hostdata->pool,
  896. tmp_evt);
  897. atomic_inc(&hostdata->request_limit);
  898. if (tmp_evt->cmnd_done)
  899. tmp_evt->cmnd_done(tmp_evt->cmnd);
  900. else if (tmp_evt->done)
  901. tmp_evt->done(tmp_evt);
  902. }
  903. }
  904. return SUCCESS;
  905. }
  906. /**
  907. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  908. * @hostdata: the adapter
  909. */
  910. static void purge_requests(struct ibmvscsi_host_data *hostdata)
  911. {
  912. struct srp_event_struct *tmp_evt, *pos;
  913. unsigned long flags;
  914. spin_lock_irqsave(hostdata->host->host_lock, flags);
  915. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  916. list_del(&tmp_evt->list);
  917. if (tmp_evt->cmnd) {
  918. tmp_evt->cmnd->result = (DID_ERROR << 16);
  919. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  920. tmp_evt->hostdata->dev);
  921. if (tmp_evt->cmnd_done)
  922. tmp_evt->cmnd_done(tmp_evt->cmnd);
  923. } else {
  924. if (tmp_evt->done) {
  925. tmp_evt->done(tmp_evt);
  926. }
  927. }
  928. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  929. }
  930. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  931. }
  932. /**
  933. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  934. * @crq: Command/Response queue
  935. * @hostdata: ibmvscsi_host_data of host
  936. *
  937. */
  938. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  939. struct ibmvscsi_host_data *hostdata)
  940. {
  941. unsigned long flags;
  942. struct srp_event_struct *evt_struct =
  943. (struct srp_event_struct *)crq->IU_data_ptr;
  944. switch (crq->valid) {
  945. case 0xC0: /* initialization */
  946. switch (crq->format) {
  947. case 0x01: /* Initialization message */
  948. printk(KERN_INFO "ibmvscsi: partner initialized\n");
  949. /* Send back a response */
  950. if (ibmvscsi_send_crq(hostdata,
  951. 0xC002000000000000LL, 0) == 0) {
  952. /* Now login */
  953. send_srp_login(hostdata);
  954. } else {
  955. printk(KERN_ERR
  956. "ibmvscsi: Unable to send init rsp\n");
  957. }
  958. break;
  959. case 0x02: /* Initialization response */
  960. printk(KERN_INFO
  961. "ibmvscsi: partner initialization complete\n");
  962. /* Now login */
  963. send_srp_login(hostdata);
  964. break;
  965. default:
  966. printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
  967. }
  968. return;
  969. case 0xFF: /* Hypervisor telling us the connection is closed */
  970. printk(KERN_INFO "ibmvscsi: Virtual adapter failed!\n");
  971. atomic_set(&hostdata->request_limit, -1);
  972. purge_requests(hostdata);
  973. ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  974. return;
  975. case 0x80: /* real payload */
  976. break;
  977. default:
  978. printk(KERN_ERR
  979. "ibmvscsi: got an invalid message type 0x%02x\n",
  980. crq->valid);
  981. return;
  982. }
  983. /* The only kind of payload CRQs we should get are responses to
  984. * things we send. Make sure this response is to something we
  985. * actually sent
  986. */
  987. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  988. printk(KERN_ERR
  989. "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
  990. (void *)crq->IU_data_ptr);
  991. return;
  992. }
  993. if (atomic_read(&evt_struct->free)) {
  994. printk(KERN_ERR
  995. "ibmvscsi: received duplicate correlation_token 0x%p!\n",
  996. (void *)crq->IU_data_ptr);
  997. return;
  998. }
  999. if (crq->format == VIOSRP_SRP_FORMAT)
  1000. atomic_add(evt_struct->xfer_iu->srp.rsp.request_limit_delta,
  1001. &hostdata->request_limit);
  1002. if (evt_struct->done)
  1003. evt_struct->done(evt_struct);
  1004. else
  1005. printk(KERN_ERR
  1006. "ibmvscsi: returned done() is NULL; not running it!\n");
  1007. /*
  1008. * Lock the host_lock before messing with these structures, since we
  1009. * are running in a task context
  1010. */
  1011. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1012. list_del(&evt_struct->list);
  1013. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1014. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1015. }
  1016. /**
  1017. * ibmvscsi_get_host_config: Send the command to the server to get host
  1018. * configuration data. The data is opaque to us.
  1019. */
  1020. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1021. unsigned char *buffer, int length)
  1022. {
  1023. struct viosrp_host_config *host_config;
  1024. struct srp_event_struct *evt_struct;
  1025. int rc;
  1026. evt_struct = get_event_struct(&hostdata->pool);
  1027. if (!evt_struct) {
  1028. printk(KERN_ERR
  1029. "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
  1030. return -1;
  1031. }
  1032. init_event_struct(evt_struct,
  1033. sync_completion,
  1034. VIOSRP_MAD_FORMAT,
  1035. init_timeout * HZ);
  1036. host_config = &evt_struct->iu.mad.host_config;
  1037. /* Set up a lun reset SRP command */
  1038. memset(host_config, 0x00, sizeof(*host_config));
  1039. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1040. host_config->common.length = length;
  1041. host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
  1042. DMA_BIDIRECTIONAL);
  1043. if (dma_mapping_error(host_config->buffer)) {
  1044. printk(KERN_ERR
  1045. "ibmvscsi: dma_mapping error " "getting host config\n");
  1046. free_event_struct(&hostdata->pool, evt_struct);
  1047. return -1;
  1048. }
  1049. init_completion(&evt_struct->comp);
  1050. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  1051. if (rc == 0) {
  1052. wait_for_completion(&evt_struct->comp);
  1053. dma_unmap_single(hostdata->dev, host_config->buffer,
  1054. length, DMA_BIDIRECTIONAL);
  1055. }
  1056. return rc;
  1057. }
  1058. /* ------------------------------------------------------------
  1059. * sysfs attributes
  1060. */
  1061. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1062. {
  1063. struct Scsi_Host *shost = class_to_shost(class_dev);
  1064. struct ibmvscsi_host_data *hostdata =
  1065. (struct ibmvscsi_host_data *)shost->hostdata;
  1066. int len;
  1067. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1068. hostdata->madapter_info.srp_version);
  1069. return len;
  1070. }
  1071. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1072. .attr = {
  1073. .name = "srp_version",
  1074. .mode = S_IRUGO,
  1075. },
  1076. .show = show_host_srp_version,
  1077. };
  1078. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1079. char *buf)
  1080. {
  1081. struct Scsi_Host *shost = class_to_shost(class_dev);
  1082. struct ibmvscsi_host_data *hostdata =
  1083. (struct ibmvscsi_host_data *)shost->hostdata;
  1084. int len;
  1085. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1086. hostdata->madapter_info.partition_name);
  1087. return len;
  1088. }
  1089. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1090. .attr = {
  1091. .name = "partition_name",
  1092. .mode = S_IRUGO,
  1093. },
  1094. .show = show_host_partition_name,
  1095. };
  1096. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1097. char *buf)
  1098. {
  1099. struct Scsi_Host *shost = class_to_shost(class_dev);
  1100. struct ibmvscsi_host_data *hostdata =
  1101. (struct ibmvscsi_host_data *)shost->hostdata;
  1102. int len;
  1103. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1104. hostdata->madapter_info.partition_number);
  1105. return len;
  1106. }
  1107. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1108. .attr = {
  1109. .name = "partition_number",
  1110. .mode = S_IRUGO,
  1111. },
  1112. .show = show_host_partition_number,
  1113. };
  1114. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1115. {
  1116. struct Scsi_Host *shost = class_to_shost(class_dev);
  1117. struct ibmvscsi_host_data *hostdata =
  1118. (struct ibmvscsi_host_data *)shost->hostdata;
  1119. int len;
  1120. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1121. hostdata->madapter_info.mad_version);
  1122. return len;
  1123. }
  1124. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1125. .attr = {
  1126. .name = "mad_version",
  1127. .mode = S_IRUGO,
  1128. },
  1129. .show = show_host_mad_version,
  1130. };
  1131. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1132. {
  1133. struct Scsi_Host *shost = class_to_shost(class_dev);
  1134. struct ibmvscsi_host_data *hostdata =
  1135. (struct ibmvscsi_host_data *)shost->hostdata;
  1136. int len;
  1137. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1138. return len;
  1139. }
  1140. static struct class_device_attribute ibmvscsi_host_os_type = {
  1141. .attr = {
  1142. .name = "os_type",
  1143. .mode = S_IRUGO,
  1144. },
  1145. .show = show_host_os_type,
  1146. };
  1147. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1148. {
  1149. struct Scsi_Host *shost = class_to_shost(class_dev);
  1150. struct ibmvscsi_host_data *hostdata =
  1151. (struct ibmvscsi_host_data *)shost->hostdata;
  1152. /* returns null-terminated host config data */
  1153. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1154. return strlen(buf);
  1155. else
  1156. return 0;
  1157. }
  1158. static struct class_device_attribute ibmvscsi_host_config = {
  1159. .attr = {
  1160. .name = "config",
  1161. .mode = S_IRUGO,
  1162. },
  1163. .show = show_host_config,
  1164. };
  1165. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1166. &ibmvscsi_host_srp_version,
  1167. &ibmvscsi_host_partition_name,
  1168. &ibmvscsi_host_partition_number,
  1169. &ibmvscsi_host_mad_version,
  1170. &ibmvscsi_host_os_type,
  1171. &ibmvscsi_host_config,
  1172. NULL
  1173. };
  1174. /* ------------------------------------------------------------
  1175. * SCSI driver registration
  1176. */
  1177. static struct scsi_host_template driver_template = {
  1178. .module = THIS_MODULE,
  1179. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1180. .proc_name = "ibmvscsi",
  1181. .queuecommand = ibmvscsi_queuecommand,
  1182. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1183. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1184. .cmd_per_lun = 16,
  1185. .can_queue = 1, /* Updated after SRP_LOGIN */
  1186. .this_id = -1,
  1187. .sg_tablesize = MAX_INDIRECT_BUFS,
  1188. .use_clustering = ENABLE_CLUSTERING,
  1189. .shost_attrs = ibmvscsi_attrs,
  1190. };
  1191. /**
  1192. * Called by bus code for each adapter
  1193. */
  1194. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1195. {
  1196. struct ibmvscsi_host_data *hostdata;
  1197. struct Scsi_Host *host;
  1198. struct device *dev = &vdev->dev;
  1199. unsigned long wait_switch = 0;
  1200. vdev->dev.driver_data = NULL;
  1201. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1202. if (!host) {
  1203. printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
  1204. goto scsi_host_alloc_failed;
  1205. }
  1206. hostdata = (struct ibmvscsi_host_data *)host->hostdata;
  1207. memset(hostdata, 0x00, sizeof(*hostdata));
  1208. INIT_LIST_HEAD(&hostdata->sent);
  1209. hostdata->host = host;
  1210. hostdata->dev = dev;
  1211. atomic_set(&hostdata->request_limit, -1);
  1212. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1213. if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
  1214. max_requests) != 0) {
  1215. printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
  1216. goto init_crq_failed;
  1217. }
  1218. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1219. printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
  1220. goto init_pool_failed;
  1221. }
  1222. host->max_lun = 8;
  1223. host->max_id = max_id;
  1224. host->max_channel = max_channel;
  1225. if (scsi_add_host(hostdata->host, hostdata->dev))
  1226. goto add_host_failed;
  1227. /* Try to send an initialization message. Note that this is allowed
  1228. * to fail if the other end is not acive. In that case we don't
  1229. * want to scan
  1230. */
  1231. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
  1232. /*
  1233. * Wait around max init_timeout secs for the adapter to finish
  1234. * initializing. When we are done initializing, we will have a
  1235. * valid request_limit. We don't want Linux scanning before
  1236. * we are ready.
  1237. */
  1238. for (wait_switch = jiffies + (init_timeout * HZ);
  1239. time_before(jiffies, wait_switch) &&
  1240. atomic_read(&hostdata->request_limit) < 2;) {
  1241. msleep(10);
  1242. }
  1243. /* if we now have a valid request_limit, initiate a scan */
  1244. if (atomic_read(&hostdata->request_limit) > 0)
  1245. scsi_scan_host(host);
  1246. }
  1247. vdev->dev.driver_data = hostdata;
  1248. return 0;
  1249. add_host_failed:
  1250. release_event_pool(&hostdata->pool, hostdata);
  1251. init_pool_failed:
  1252. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1253. init_crq_failed:
  1254. scsi_host_put(host);
  1255. scsi_host_alloc_failed:
  1256. return -1;
  1257. }
  1258. static int ibmvscsi_remove(struct vio_dev *vdev)
  1259. {
  1260. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1261. release_event_pool(&hostdata->pool, hostdata);
  1262. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  1263. max_requests);
  1264. scsi_remove_host(hostdata->host);
  1265. scsi_host_put(hostdata->host);
  1266. return 0;
  1267. }
  1268. /**
  1269. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1270. * support.
  1271. */
  1272. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1273. {"vscsi", "IBM,v-scsi"},
  1274. {0,}
  1275. };
  1276. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1277. static struct vio_driver ibmvscsi_driver = {
  1278. .name = "ibmvscsi",
  1279. .id_table = ibmvscsi_device_table,
  1280. .probe = ibmvscsi_probe,
  1281. .remove = ibmvscsi_remove
  1282. };
  1283. int __init ibmvscsi_module_init(void)
  1284. {
  1285. return vio_register_driver(&ibmvscsi_driver);
  1286. }
  1287. void __exit ibmvscsi_module_exit(void)
  1288. {
  1289. vio_unregister_driver(&ibmvscsi_driver);
  1290. }
  1291. module_init(ibmvscsi_module_init);
  1292. module_exit(ibmvscsi_module_exit);