ibmvscsi.c 45 KB

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