ibmvscsi.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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-robin 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. * TODO: This is currently pretty tied to the IBM pSeries hypervisor
  59. * interfaces. It would be really nice to abstract this above an RDMA
  60. * layer.
  61. */
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/delay.h>
  66. #include <linux/slab.h>
  67. #include <linux/of.h>
  68. #include <linux/pm.h>
  69. #include <linux/kthread.h>
  70. #include <asm/firmware.h>
  71. #include <asm/vio.h>
  72. #include <scsi/scsi.h>
  73. #include <scsi/scsi_cmnd.h>
  74. #include <scsi/scsi_host.h>
  75. #include <scsi/scsi_device.h>
  76. #include <scsi/scsi_transport_srp.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 = 300;
  86. static int login_timeout = 60;
  87. static int info_timeout = 30;
  88. static int abort_timeout = 60;
  89. static int reset_timeout = 60;
  90. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  91. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  92. static int fast_fail = 1;
  93. static int client_reserve = 1;
  94. static char partition_name[96] = "UNKNOWN";
  95. static unsigned int partition_number = -1;
  96. static LIST_HEAD(ibmvscsi_head);
  97. static DEFINE_SPINLOCK(ibmvscsi_driver_lock);
  98. static struct scsi_transport_template *ibmvscsi_transport_template;
  99. #define IBMVSCSI_VERSION "1.5.9"
  100. MODULE_DESCRIPTION("IBM Virtual SCSI");
  101. MODULE_AUTHOR("Dave Boutcher");
  102. MODULE_LICENSE("GPL");
  103. MODULE_VERSION(IBMVSCSI_VERSION);
  104. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(max_id, "Largest ID value for each channel [Default=64]");
  106. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(max_channel, "Largest channel value [Default=3]");
  108. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  109. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  110. module_param_named(max_requests, max_requests, int, S_IRUGO);
  111. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  112. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  113. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  114. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  115. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  116. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  117. struct ibmvscsi_host_data *hostdata);
  118. /* ------------------------------------------------------------
  119. * Routines for managing the command/response queue
  120. */
  121. /**
  122. * ibmvscsi_handle_event: - Interrupt handler for crq events
  123. * @irq: number of irq to handle, not used
  124. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  125. *
  126. * Disables interrupts and schedules srp_task
  127. * Always returns IRQ_HANDLED
  128. */
  129. static irqreturn_t ibmvscsi_handle_event(int irq, void *dev_instance)
  130. {
  131. struct ibmvscsi_host_data *hostdata =
  132. (struct ibmvscsi_host_data *)dev_instance;
  133. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  134. tasklet_schedule(&hostdata->srp_task);
  135. return IRQ_HANDLED;
  136. }
  137. /**
  138. * release_crq_queue: - Deallocates data and unregisters CRQ
  139. * @queue: crq_queue to initialize and register
  140. * @host_data: ibmvscsi_host_data of host
  141. *
  142. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  143. * the crq with the hypervisor.
  144. */
  145. static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
  146. struct ibmvscsi_host_data *hostdata,
  147. int max_requests)
  148. {
  149. long rc = 0;
  150. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  151. free_irq(vdev->irq, (void *)hostdata);
  152. tasklet_kill(&hostdata->srp_task);
  153. do {
  154. if (rc)
  155. msleep(100);
  156. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  157. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  158. dma_unmap_single(hostdata->dev,
  159. queue->msg_token,
  160. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  161. free_page((unsigned long)queue->msgs);
  162. }
  163. /**
  164. * crq_queue_next_crq: - Returns the next entry in message queue
  165. * @queue: crq_queue to use
  166. *
  167. * Returns pointer to next entry in queue, or NULL if there are no new
  168. * entried in the CRQ.
  169. */
  170. static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
  171. {
  172. struct viosrp_crq *crq;
  173. unsigned long flags;
  174. spin_lock_irqsave(&queue->lock, flags);
  175. crq = &queue->msgs[queue->cur];
  176. if (crq->valid != VIOSRP_CRQ_FREE) {
  177. if (++queue->cur == queue->size)
  178. queue->cur = 0;
  179. /* Ensure the read of the valid bit occurs before reading any
  180. * other bits of the CRQ entry
  181. */
  182. rmb();
  183. } else
  184. crq = NULL;
  185. spin_unlock_irqrestore(&queue->lock, flags);
  186. return crq;
  187. }
  188. /**
  189. * ibmvscsi_send_crq: - Send a CRQ
  190. * @hostdata: the adapter
  191. * @word1: the first 64 bits of the data
  192. * @word2: the second 64 bits of the data
  193. */
  194. static int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata,
  195. u64 word1, u64 word2)
  196. {
  197. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  198. /*
  199. * Ensure the command buffer is flushed to memory before handing it
  200. * over to the VIOS to prevent it from fetching any stale data.
  201. */
  202. mb();
  203. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  204. }
  205. /**
  206. * ibmvscsi_task: - Process srps asynchronously
  207. * @data: ibmvscsi_host_data of host
  208. */
  209. static void ibmvscsi_task(void *data)
  210. {
  211. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  212. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  213. struct viosrp_crq *crq;
  214. int done = 0;
  215. while (!done) {
  216. /* Pull all the valid messages off the CRQ */
  217. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  218. ibmvscsi_handle_crq(crq, hostdata);
  219. crq->valid = VIOSRP_CRQ_FREE;
  220. wmb();
  221. }
  222. vio_enable_interrupts(vdev);
  223. crq = crq_queue_next_crq(&hostdata->queue);
  224. if (crq != NULL) {
  225. vio_disable_interrupts(vdev);
  226. ibmvscsi_handle_crq(crq, hostdata);
  227. crq->valid = VIOSRP_CRQ_FREE;
  228. wmb();
  229. } else {
  230. done = 1;
  231. }
  232. }
  233. }
  234. static void gather_partition_info(void)
  235. {
  236. const char *ppartition_name;
  237. const __be32 *p_number_ptr;
  238. /* Retrieve information about this partition */
  239. if (!of_root)
  240. return;
  241. of_node_get(of_root);
  242. ppartition_name = of_get_property(of_root, "ibm,partition-name", NULL);
  243. if (ppartition_name)
  244. strlcpy(partition_name, ppartition_name,
  245. sizeof(partition_name));
  246. p_number_ptr = of_get_property(of_root, "ibm,partition-no", NULL);
  247. if (p_number_ptr)
  248. partition_number = of_read_number(p_number_ptr, 1);
  249. of_node_put(of_root);
  250. }
  251. static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
  252. {
  253. memset(&hostdata->madapter_info, 0x00,
  254. sizeof(hostdata->madapter_info));
  255. dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
  256. strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
  257. strncpy(hostdata->madapter_info.partition_name, partition_name,
  258. sizeof(hostdata->madapter_info.partition_name));
  259. hostdata->madapter_info.partition_number =
  260. cpu_to_be32(partition_number);
  261. hostdata->madapter_info.mad_version = cpu_to_be32(SRP_MAD_VERSION_1);
  262. hostdata->madapter_info.os_type = cpu_to_be32(SRP_MAD_OS_LINUX);
  263. }
  264. /**
  265. * reset_crq_queue: - resets a crq after a failure
  266. * @queue: crq_queue to initialize and register
  267. * @hostdata: ibmvscsi_host_data of host
  268. *
  269. */
  270. static int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
  271. struct ibmvscsi_host_data *hostdata)
  272. {
  273. int rc = 0;
  274. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  275. /* Close the CRQ */
  276. do {
  277. if (rc)
  278. msleep(100);
  279. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  280. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  281. /* Clean out the queue */
  282. memset(queue->msgs, 0x00, PAGE_SIZE);
  283. queue->cur = 0;
  284. set_adapter_info(hostdata);
  285. /* And re-open it again */
  286. rc = plpar_hcall_norets(H_REG_CRQ,
  287. vdev->unit_address,
  288. queue->msg_token, PAGE_SIZE);
  289. if (rc == H_CLOSED) {
  290. /* Adapter is good, but other end is not ready */
  291. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  292. } else if (rc != 0) {
  293. dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
  294. }
  295. return rc;
  296. }
  297. /**
  298. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  299. * @queue: crq_queue to initialize and register
  300. * @hostdata: ibmvscsi_host_data of host
  301. *
  302. * Allocates a page for messages, maps it for dma, and registers
  303. * the crq with the hypervisor.
  304. * Returns zero on success.
  305. */
  306. static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
  307. struct ibmvscsi_host_data *hostdata,
  308. int max_requests)
  309. {
  310. int rc;
  311. int retrc;
  312. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  313. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  314. if (!queue->msgs)
  315. goto malloc_failed;
  316. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  317. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  318. queue->size * sizeof(*queue->msgs),
  319. DMA_BIDIRECTIONAL);
  320. if (dma_mapping_error(hostdata->dev, queue->msg_token))
  321. goto map_failed;
  322. gather_partition_info();
  323. set_adapter_info(hostdata);
  324. retrc = rc = plpar_hcall_norets(H_REG_CRQ,
  325. vdev->unit_address,
  326. queue->msg_token, PAGE_SIZE);
  327. if (rc == H_RESOURCE)
  328. /* maybe kexecing and resource is busy. try a reset */
  329. rc = ibmvscsi_reset_crq_queue(queue,
  330. hostdata);
  331. if (rc == H_CLOSED) {
  332. /* Adapter is good, but other end is not ready */
  333. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  334. retrc = 0;
  335. } else if (rc != 0) {
  336. dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
  337. goto reg_crq_failed;
  338. }
  339. queue->cur = 0;
  340. spin_lock_init(&queue->lock);
  341. tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
  342. (unsigned long)hostdata);
  343. if (request_irq(vdev->irq,
  344. ibmvscsi_handle_event,
  345. 0, "ibmvscsi", (void *)hostdata) != 0) {
  346. dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
  347. vdev->irq);
  348. goto req_irq_failed;
  349. }
  350. rc = vio_enable_interrupts(vdev);
  351. if (rc != 0) {
  352. dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
  353. goto req_irq_failed;
  354. }
  355. return retrc;
  356. req_irq_failed:
  357. tasklet_kill(&hostdata->srp_task);
  358. rc = 0;
  359. do {
  360. if (rc)
  361. msleep(100);
  362. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  363. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  364. reg_crq_failed:
  365. dma_unmap_single(hostdata->dev,
  366. queue->msg_token,
  367. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  368. map_failed:
  369. free_page((unsigned long)queue->msgs);
  370. malloc_failed:
  371. return -1;
  372. }
  373. /**
  374. * reenable_crq_queue: - reenables a crq after
  375. * @queue: crq_queue to initialize and register
  376. * @hostdata: ibmvscsi_host_data of host
  377. *
  378. */
  379. static int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
  380. struct ibmvscsi_host_data *hostdata)
  381. {
  382. int rc = 0;
  383. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  384. set_adapter_info(hostdata);
  385. /* Re-enable the CRQ */
  386. do {
  387. if (rc)
  388. msleep(100);
  389. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  390. } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  391. if (rc)
  392. dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
  393. return rc;
  394. }
  395. /* ------------------------------------------------------------
  396. * Routines for the event pool and event structs
  397. */
  398. /**
  399. * initialize_event_pool: - Allocates and initializes the event pool for a host
  400. * @pool: event_pool to be initialized
  401. * @size: Number of events in pool
  402. * @hostdata: ibmvscsi_host_data who owns the event pool
  403. *
  404. * Returns zero on success.
  405. */
  406. static int initialize_event_pool(struct event_pool *pool,
  407. int size, struct ibmvscsi_host_data *hostdata)
  408. {
  409. int i;
  410. pool->size = size;
  411. pool->next = 0;
  412. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  413. if (!pool->events)
  414. return -ENOMEM;
  415. pool->iu_storage =
  416. dma_alloc_coherent(hostdata->dev,
  417. pool->size * sizeof(*pool->iu_storage),
  418. &pool->iu_token, 0);
  419. if (!pool->iu_storage) {
  420. kfree(pool->events);
  421. return -ENOMEM;
  422. }
  423. for (i = 0; i < pool->size; ++i) {
  424. struct srp_event_struct *evt = &pool->events[i];
  425. memset(&evt->crq, 0x00, sizeof(evt->crq));
  426. atomic_set(&evt->free, 1);
  427. evt->crq.valid = VIOSRP_CRQ_CMD_RSP;
  428. evt->crq.IU_length = cpu_to_be16(sizeof(*evt->xfer_iu));
  429. evt->crq.IU_data_ptr = cpu_to_be64(pool->iu_token +
  430. sizeof(*evt->xfer_iu) * i);
  431. evt->xfer_iu = pool->iu_storage + i;
  432. evt->hostdata = hostdata;
  433. evt->ext_list = NULL;
  434. evt->ext_list_token = 0;
  435. }
  436. return 0;
  437. }
  438. /**
  439. * release_event_pool: - Frees memory of an event pool of a host
  440. * @pool: event_pool to be released
  441. * @hostdata: ibmvscsi_host_data who owns the even pool
  442. *
  443. * Returns zero on success.
  444. */
  445. static void release_event_pool(struct event_pool *pool,
  446. struct ibmvscsi_host_data *hostdata)
  447. {
  448. int i, in_use = 0;
  449. for (i = 0; i < pool->size; ++i) {
  450. if (atomic_read(&pool->events[i].free) != 1)
  451. ++in_use;
  452. if (pool->events[i].ext_list) {
  453. dma_free_coherent(hostdata->dev,
  454. SG_ALL * sizeof(struct srp_direct_buf),
  455. pool->events[i].ext_list,
  456. pool->events[i].ext_list_token);
  457. }
  458. }
  459. if (in_use)
  460. dev_warn(hostdata->dev, "releasing event pool with %d "
  461. "events still in use?\n", in_use);
  462. kfree(pool->events);
  463. dma_free_coherent(hostdata->dev,
  464. pool->size * sizeof(*pool->iu_storage),
  465. pool->iu_storage, pool->iu_token);
  466. }
  467. /**
  468. * valid_event_struct: - Determines if event is valid.
  469. * @pool: event_pool that contains the event
  470. * @evt: srp_event_struct to be checked for validity
  471. *
  472. * Returns zero if event is invalid, one otherwise.
  473. */
  474. static int valid_event_struct(struct event_pool *pool,
  475. struct srp_event_struct *evt)
  476. {
  477. int index = evt - pool->events;
  478. if (index < 0 || index >= pool->size) /* outside of bounds */
  479. return 0;
  480. if (evt != pool->events + index) /* unaligned */
  481. return 0;
  482. return 1;
  483. }
  484. /**
  485. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  486. * @pool: event_pool that contains the event
  487. * @evt: srp_event_struct to be modified
  488. *
  489. */
  490. static void free_event_struct(struct event_pool *pool,
  491. struct srp_event_struct *evt)
  492. {
  493. if (!valid_event_struct(pool, evt)) {
  494. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  495. "(not in pool %p)\n", evt, pool->events);
  496. return;
  497. }
  498. if (atomic_inc_return(&evt->free) != 1) {
  499. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  500. "which is not in use!\n", evt);
  501. return;
  502. }
  503. }
  504. /**
  505. * get_evt_struct: - Gets the next free event in pool
  506. * @pool: event_pool that contains the events to be searched
  507. *
  508. * Returns the next event in "free" state, and NULL if none are free.
  509. * Note that no synchronization is done here, we assume the host_lock
  510. * will syncrhonze things.
  511. */
  512. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  513. {
  514. int i;
  515. int poolsize = pool->size;
  516. int offset = pool->next;
  517. for (i = 0; i < poolsize; i++) {
  518. offset = (offset + 1) % poolsize;
  519. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  520. pool->next = offset;
  521. return &pool->events[offset];
  522. }
  523. }
  524. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  525. return NULL;
  526. }
  527. /**
  528. * init_event_struct: Initialize fields in an event struct that are always
  529. * required.
  530. * @evt: The event
  531. * @done: Routine to call when the event is responded to
  532. * @format: SRP or MAD format
  533. * @timeout: timeout value set in the CRQ
  534. */
  535. static void init_event_struct(struct srp_event_struct *evt_struct,
  536. void (*done) (struct srp_event_struct *),
  537. u8 format,
  538. int timeout)
  539. {
  540. evt_struct->cmnd = NULL;
  541. evt_struct->cmnd_done = NULL;
  542. evt_struct->sync_srp = NULL;
  543. evt_struct->crq.format = format;
  544. evt_struct->crq.timeout = cpu_to_be16(timeout);
  545. evt_struct->done = done;
  546. }
  547. /* ------------------------------------------------------------
  548. * Routines for receiving SCSI responses from the hosting partition
  549. */
  550. /**
  551. * set_srp_direction: Set the fields in the srp related to data
  552. * direction and number of buffers based on the direction in
  553. * the scsi_cmnd and the number of buffers
  554. */
  555. static void set_srp_direction(struct scsi_cmnd *cmd,
  556. struct srp_cmd *srp_cmd,
  557. int numbuf)
  558. {
  559. u8 fmt;
  560. if (numbuf == 0)
  561. return;
  562. if (numbuf == 1)
  563. fmt = SRP_DATA_DESC_DIRECT;
  564. else {
  565. fmt = SRP_DATA_DESC_INDIRECT;
  566. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  567. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  568. srp_cmd->data_out_desc_cnt = numbuf;
  569. else
  570. srp_cmd->data_in_desc_cnt = numbuf;
  571. }
  572. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  573. srp_cmd->buf_fmt = fmt << 4;
  574. else
  575. srp_cmd->buf_fmt = fmt;
  576. }
  577. /**
  578. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  579. * @cmd: srp_cmd whose additional_data member will be unmapped
  580. * @dev: device for which the memory is mapped
  581. *
  582. */
  583. static void unmap_cmd_data(struct srp_cmd *cmd,
  584. struct srp_event_struct *evt_struct,
  585. struct device *dev)
  586. {
  587. u8 out_fmt, in_fmt;
  588. out_fmt = cmd->buf_fmt >> 4;
  589. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  590. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  591. return;
  592. if (evt_struct->cmnd)
  593. scsi_dma_unmap(evt_struct->cmnd);
  594. }
  595. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  596. struct srp_direct_buf *md)
  597. {
  598. int i;
  599. struct scatterlist *sg;
  600. u64 total_length = 0;
  601. scsi_for_each_sg(cmd, sg, nseg, i) {
  602. struct srp_direct_buf *descr = md + i;
  603. descr->va = cpu_to_be64(sg_dma_address(sg));
  604. descr->len = cpu_to_be32(sg_dma_len(sg));
  605. descr->key = 0;
  606. total_length += sg_dma_len(sg);
  607. }
  608. return total_length;
  609. }
  610. /**
  611. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  612. * @cmd: struct scsi_cmnd with the scatterlist
  613. * @srp_cmd: srp_cmd that contains the memory descriptor
  614. * @dev: device for which to map dma memory
  615. *
  616. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  617. * Returns 1 on success.
  618. */
  619. static int map_sg_data(struct scsi_cmnd *cmd,
  620. struct srp_event_struct *evt_struct,
  621. struct srp_cmd *srp_cmd, struct device *dev)
  622. {
  623. int sg_mapped;
  624. u64 total_length = 0;
  625. struct srp_direct_buf *data =
  626. (struct srp_direct_buf *) srp_cmd->add_data;
  627. struct srp_indirect_buf *indirect =
  628. (struct srp_indirect_buf *) data;
  629. sg_mapped = scsi_dma_map(cmd);
  630. if (!sg_mapped)
  631. return 1;
  632. else if (sg_mapped < 0)
  633. return 0;
  634. set_srp_direction(cmd, srp_cmd, sg_mapped);
  635. /* special case; we can use a single direct descriptor */
  636. if (sg_mapped == 1) {
  637. map_sg_list(cmd, sg_mapped, data);
  638. return 1;
  639. }
  640. indirect->table_desc.va = 0;
  641. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  642. sizeof(struct srp_direct_buf));
  643. indirect->table_desc.key = 0;
  644. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  645. total_length = map_sg_list(cmd, sg_mapped,
  646. &indirect->desc_list[0]);
  647. indirect->len = cpu_to_be32(total_length);
  648. return 1;
  649. }
  650. /* get indirect table */
  651. if (!evt_struct->ext_list) {
  652. evt_struct->ext_list = (struct srp_direct_buf *)
  653. dma_alloc_coherent(dev,
  654. SG_ALL * sizeof(struct srp_direct_buf),
  655. &evt_struct->ext_list_token, 0);
  656. if (!evt_struct->ext_list) {
  657. if (!firmware_has_feature(FW_FEATURE_CMO))
  658. sdev_printk(KERN_ERR, cmd->device,
  659. "Can't allocate memory "
  660. "for indirect table\n");
  661. scsi_dma_unmap(cmd);
  662. return 0;
  663. }
  664. }
  665. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  666. indirect->len = cpu_to_be32(total_length);
  667. indirect->table_desc.va = cpu_to_be64(evt_struct->ext_list_token);
  668. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  669. sizeof(indirect->desc_list[0]));
  670. memcpy(indirect->desc_list, evt_struct->ext_list,
  671. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  672. return 1;
  673. }
  674. /**
  675. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  676. * @cmd: struct scsi_cmnd with the memory to be mapped
  677. * @srp_cmd: srp_cmd that contains the memory descriptor
  678. * @dev: dma device for which to map dma memory
  679. *
  680. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  681. * Returns 1 on success.
  682. */
  683. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  684. struct srp_event_struct *evt_struct,
  685. struct srp_cmd *srp_cmd, struct device *dev)
  686. {
  687. switch (cmd->sc_data_direction) {
  688. case DMA_FROM_DEVICE:
  689. case DMA_TO_DEVICE:
  690. break;
  691. case DMA_NONE:
  692. return 1;
  693. case DMA_BIDIRECTIONAL:
  694. sdev_printk(KERN_ERR, cmd->device,
  695. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  696. return 0;
  697. default:
  698. sdev_printk(KERN_ERR, cmd->device,
  699. "Unknown data direction 0x%02x; can't map!\n",
  700. cmd->sc_data_direction);
  701. return 0;
  702. }
  703. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  704. }
  705. /**
  706. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  707. * @hostdata: the adapter
  708. */
  709. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  710. {
  711. struct srp_event_struct *evt;
  712. unsigned long flags;
  713. spin_lock_irqsave(hostdata->host->host_lock, flags);
  714. while (!list_empty(&hostdata->sent)) {
  715. evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
  716. list_del(&evt->list);
  717. del_timer(&evt->timer);
  718. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  719. if (evt->cmnd) {
  720. evt->cmnd->result = (error_code << 16);
  721. unmap_cmd_data(&evt->iu.srp.cmd, evt,
  722. evt->hostdata->dev);
  723. if (evt->cmnd_done)
  724. evt->cmnd_done(evt->cmnd);
  725. } else if (evt->done && evt->crq.format != VIOSRP_MAD_FORMAT &&
  726. evt->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  727. evt->done(evt);
  728. free_event_struct(&evt->hostdata->pool, evt);
  729. spin_lock_irqsave(hostdata->host->host_lock, flags);
  730. }
  731. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  732. }
  733. /**
  734. * ibmvscsi_reset_host - Reset the connection to the server
  735. * @hostdata: struct ibmvscsi_host_data to reset
  736. */
  737. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  738. {
  739. scsi_block_requests(hostdata->host);
  740. atomic_set(&hostdata->request_limit, 0);
  741. purge_requests(hostdata, DID_ERROR);
  742. hostdata->reset_crq = 1;
  743. wake_up(&hostdata->work_wait_q);
  744. }
  745. /**
  746. * ibmvscsi_timeout - Internal command timeout handler
  747. * @evt_struct: struct srp_event_struct that timed out
  748. *
  749. * Called when an internally generated command times out
  750. */
  751. static void ibmvscsi_timeout(struct timer_list *t)
  752. {
  753. struct srp_event_struct *evt_struct = from_timer(evt_struct, t, timer);
  754. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  755. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  756. evt_struct->iu.srp.cmd.opcode);
  757. ibmvscsi_reset_host(hostdata);
  758. }
  759. /* ------------------------------------------------------------
  760. * Routines for sending and receiving SRPs
  761. */
  762. /**
  763. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  764. * @evt_struct: evt_struct to be sent
  765. * @hostdata: ibmvscsi_host_data of host
  766. * @timeout: timeout in seconds - 0 means do not time command
  767. *
  768. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  769. * Note that this routine assumes that host_lock is held for synchronization
  770. */
  771. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  772. struct ibmvscsi_host_data *hostdata,
  773. unsigned long timeout)
  774. {
  775. __be64 *crq_as_u64 = (__be64 *)&evt_struct->crq;
  776. int request_status = 0;
  777. int rc;
  778. int srp_req = 0;
  779. /* If we have exhausted our request limit, just fail this request,
  780. * unless it is for a reset or abort.
  781. * Note that there are rare cases involving driver generated requests
  782. * (such as task management requests) that the mid layer may think we
  783. * can handle more requests (can_queue) when we actually can't
  784. */
  785. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  786. srp_req = 1;
  787. request_status =
  788. atomic_dec_if_positive(&hostdata->request_limit);
  789. /* If request limit was -1 when we started, it is now even
  790. * less than that
  791. */
  792. if (request_status < -1)
  793. goto send_error;
  794. /* Otherwise, we may have run out of requests. */
  795. /* If request limit was 0 when we started the adapter is in the
  796. * process of performing a login with the server adapter, or
  797. * we may have run out of requests.
  798. */
  799. else if (request_status == -1 &&
  800. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  801. goto send_busy;
  802. /* Abort and reset calls should make it through.
  803. * Nothing except abort and reset should use the last two
  804. * slots unless we had two or less to begin with.
  805. */
  806. else if (request_status < 2 &&
  807. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  808. /* In the case that we have less than two requests
  809. * available, check the server limit as a combination
  810. * of the request limit and the number of requests
  811. * in-flight (the size of the send list). If the
  812. * server limit is greater than 2, return busy so
  813. * that the last two are reserved for reset and abort.
  814. */
  815. int server_limit = request_status;
  816. struct srp_event_struct *tmp_evt;
  817. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  818. server_limit++;
  819. }
  820. if (server_limit > 2)
  821. goto send_busy;
  822. }
  823. }
  824. /* Copy the IU into the transfer area */
  825. *evt_struct->xfer_iu = evt_struct->iu;
  826. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  827. /* Add this to the sent list. We need to do this
  828. * before we actually send
  829. * in case it comes back REALLY fast
  830. */
  831. list_add_tail(&evt_struct->list, &hostdata->sent);
  832. timer_setup(&evt_struct->timer, ibmvscsi_timeout, 0);
  833. if (timeout) {
  834. evt_struct->timer.expires = jiffies + (timeout * HZ);
  835. add_timer(&evt_struct->timer);
  836. }
  837. rc = ibmvscsi_send_crq(hostdata, be64_to_cpu(crq_as_u64[0]),
  838. be64_to_cpu(crq_as_u64[1]));
  839. if (rc != 0) {
  840. list_del(&evt_struct->list);
  841. del_timer(&evt_struct->timer);
  842. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  843. * Firmware will send a CRQ with a transport event (0xFF) to
  844. * tell this client what has happened to the transport. This
  845. * will be handled in ibmvscsi_handle_crq()
  846. */
  847. if (rc == H_CLOSED) {
  848. dev_warn(hostdata->dev, "send warning. "
  849. "Receive queue closed, will retry.\n");
  850. goto send_busy;
  851. }
  852. dev_err(hostdata->dev, "send error %d\n", rc);
  853. if (srp_req)
  854. atomic_inc(&hostdata->request_limit);
  855. goto send_error;
  856. }
  857. return 0;
  858. send_busy:
  859. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  860. free_event_struct(&hostdata->pool, evt_struct);
  861. if (srp_req && request_status != -1)
  862. atomic_inc(&hostdata->request_limit);
  863. return SCSI_MLQUEUE_HOST_BUSY;
  864. send_error:
  865. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  866. if (evt_struct->cmnd != NULL) {
  867. evt_struct->cmnd->result = DID_ERROR << 16;
  868. evt_struct->cmnd_done(evt_struct->cmnd);
  869. } else if (evt_struct->done)
  870. evt_struct->done(evt_struct);
  871. free_event_struct(&hostdata->pool, evt_struct);
  872. return 0;
  873. }
  874. /**
  875. * handle_cmd_rsp: - Handle responses from commands
  876. * @evt_struct: srp_event_struct to be handled
  877. *
  878. * Used as a callback by when sending scsi cmds.
  879. * Gets called by ibmvscsi_handle_crq()
  880. */
  881. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  882. {
  883. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  884. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  885. if (unlikely(rsp->opcode != SRP_RSP)) {
  886. if (printk_ratelimit())
  887. dev_warn(evt_struct->hostdata->dev,
  888. "bad SRP RSP type %#02x\n", rsp->opcode);
  889. }
  890. if (cmnd) {
  891. cmnd->result |= rsp->status;
  892. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  893. memcpy(cmnd->sense_buffer,
  894. rsp->data,
  895. be32_to_cpu(rsp->sense_data_len));
  896. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  897. evt_struct,
  898. evt_struct->hostdata->dev);
  899. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  900. scsi_set_resid(cmnd,
  901. be32_to_cpu(rsp->data_out_res_cnt));
  902. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  903. scsi_set_resid(cmnd, be32_to_cpu(rsp->data_in_res_cnt));
  904. }
  905. if (evt_struct->cmnd_done)
  906. evt_struct->cmnd_done(cmnd);
  907. }
  908. /**
  909. * lun_from_dev: - Returns the lun of the scsi device
  910. * @dev: struct scsi_device
  911. *
  912. */
  913. static inline u16 lun_from_dev(struct scsi_device *dev)
  914. {
  915. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  916. }
  917. /**
  918. * ibmvscsi_queue: - The queuecommand function of the scsi template
  919. * @cmd: struct scsi_cmnd to be executed
  920. * @done: Callback function to be called when cmd is completed
  921. */
  922. static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
  923. void (*done) (struct scsi_cmnd *))
  924. {
  925. struct srp_cmd *srp_cmd;
  926. struct srp_event_struct *evt_struct;
  927. struct srp_indirect_buf *indirect;
  928. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  929. u16 lun = lun_from_dev(cmnd->device);
  930. u8 out_fmt, in_fmt;
  931. cmnd->result = (DID_OK << 16);
  932. evt_struct = get_event_struct(&hostdata->pool);
  933. if (!evt_struct)
  934. return SCSI_MLQUEUE_HOST_BUSY;
  935. /* Set up the actual SRP IU */
  936. srp_cmd = &evt_struct->iu.srp.cmd;
  937. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  938. srp_cmd->opcode = SRP_CMD;
  939. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  940. int_to_scsilun(lun, &srp_cmd->lun);
  941. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  942. if (!firmware_has_feature(FW_FEATURE_CMO))
  943. sdev_printk(KERN_ERR, cmnd->device,
  944. "couldn't convert cmd to srp_cmd\n");
  945. free_event_struct(&hostdata->pool, evt_struct);
  946. return SCSI_MLQUEUE_HOST_BUSY;
  947. }
  948. init_event_struct(evt_struct,
  949. handle_cmd_rsp,
  950. VIOSRP_SRP_FORMAT,
  951. cmnd->request->timeout/HZ);
  952. evt_struct->cmnd = cmnd;
  953. evt_struct->cmnd_done = done;
  954. /* Fix up dma address of the buffer itself */
  955. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  956. out_fmt = srp_cmd->buf_fmt >> 4;
  957. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  958. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  959. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  960. indirect->table_desc.va == 0) {
  961. indirect->table_desc.va =
  962. cpu_to_be64(be64_to_cpu(evt_struct->crq.IU_data_ptr) +
  963. offsetof(struct srp_cmd, add_data) +
  964. offsetof(struct srp_indirect_buf, desc_list));
  965. }
  966. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  967. }
  968. static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
  969. /* ------------------------------------------------------------
  970. * Routines for driver initialization
  971. */
  972. /**
  973. * map_persist_bufs: - Pre-map persistent data for adapter logins
  974. * @hostdata: ibmvscsi_host_data of host
  975. *
  976. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  977. * Return 1 on error, 0 on success.
  978. */
  979. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  980. {
  981. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  982. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  983. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  984. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  985. return 1;
  986. }
  987. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  988. &hostdata->madapter_info,
  989. sizeof(hostdata->madapter_info),
  990. DMA_BIDIRECTIONAL);
  991. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  992. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  993. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  994. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  995. return 1;
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  1001. * @hostdata: ibmvscsi_host_data of host
  1002. *
  1003. * Unmap the capabilities and adapter info DMA buffers
  1004. */
  1005. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  1006. {
  1007. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  1008. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  1009. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  1010. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  1011. }
  1012. /**
  1013. * login_rsp: - Handle response to SRP login request
  1014. * @evt_struct: srp_event_struct with the response
  1015. *
  1016. * Used as a "done" callback by when sending srp_login. Gets called
  1017. * by ibmvscsi_handle_crq()
  1018. */
  1019. static void login_rsp(struct srp_event_struct *evt_struct)
  1020. {
  1021. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1022. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  1023. case SRP_LOGIN_RSP: /* it worked! */
  1024. break;
  1025. case SRP_LOGIN_REJ: /* refused! */
  1026. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  1027. evt_struct->xfer_iu->srp.login_rej.reason);
  1028. /* Login failed. */
  1029. atomic_set(&hostdata->request_limit, -1);
  1030. return;
  1031. default:
  1032. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  1033. evt_struct->xfer_iu->srp.login_rsp.opcode);
  1034. /* Login failed. */
  1035. atomic_set(&hostdata->request_limit, -1);
  1036. return;
  1037. }
  1038. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  1039. hostdata->client_migrated = 0;
  1040. /* Now we know what the real request-limit is.
  1041. * This value is set rather than added to request_limit because
  1042. * request_limit could have been set to -1 by this client.
  1043. */
  1044. atomic_set(&hostdata->request_limit,
  1045. be32_to_cpu(evt_struct->xfer_iu->srp.login_rsp.req_lim_delta));
  1046. /* If we had any pending I/Os, kick them */
  1047. scsi_unblock_requests(hostdata->host);
  1048. }
  1049. /**
  1050. * send_srp_login: - Sends the srp login
  1051. * @hostdata: ibmvscsi_host_data of host
  1052. *
  1053. * Returns zero if successful.
  1054. */
  1055. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  1056. {
  1057. int rc;
  1058. unsigned long flags;
  1059. struct srp_login_req *login;
  1060. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  1061. BUG_ON(!evt_struct);
  1062. init_event_struct(evt_struct, login_rsp,
  1063. VIOSRP_SRP_FORMAT, login_timeout);
  1064. login = &evt_struct->iu.srp.login_req;
  1065. memset(login, 0, sizeof(*login));
  1066. login->opcode = SRP_LOGIN_REQ;
  1067. login->req_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
  1068. login->req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  1069. SRP_BUF_FORMAT_INDIRECT);
  1070. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1071. /* Start out with a request limit of 0, since this is negotiated in
  1072. * the login request we are just sending and login requests always
  1073. * get sent by the driver regardless of request_limit.
  1074. */
  1075. atomic_set(&hostdata->request_limit, 0);
  1076. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  1077. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1078. dev_info(hostdata->dev, "sent SRP login\n");
  1079. return rc;
  1080. };
  1081. /**
  1082. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  1083. * @evt_struct: srp_event_struct with the response
  1084. *
  1085. * Used as a "done" callback by when sending adapter_info.
  1086. */
  1087. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  1088. {
  1089. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1090. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  1091. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  1092. evt_struct->xfer_iu->mad.capabilities.common.status);
  1093. } else {
  1094. if (hostdata->caps.migration.common.server_support !=
  1095. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1096. dev_info(hostdata->dev, "Partition migration not supported\n");
  1097. if (client_reserve) {
  1098. if (hostdata->caps.reserve.common.server_support ==
  1099. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1100. dev_info(hostdata->dev, "Client reserve enabled\n");
  1101. else
  1102. dev_info(hostdata->dev, "Client reserve not supported\n");
  1103. }
  1104. }
  1105. send_srp_login(hostdata);
  1106. }
  1107. /**
  1108. * send_mad_capabilities: - Sends the mad capabilities request
  1109. * and stores the result so it can be retrieved with
  1110. * @hostdata: ibmvscsi_host_data of host
  1111. */
  1112. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  1113. {
  1114. struct viosrp_capabilities *req;
  1115. struct srp_event_struct *evt_struct;
  1116. unsigned long flags;
  1117. struct device_node *of_node = hostdata->dev->of_node;
  1118. const char *location;
  1119. evt_struct = get_event_struct(&hostdata->pool);
  1120. BUG_ON(!evt_struct);
  1121. init_event_struct(evt_struct, capabilities_rsp,
  1122. VIOSRP_MAD_FORMAT, info_timeout);
  1123. req = &evt_struct->iu.mad.capabilities;
  1124. memset(req, 0, sizeof(*req));
  1125. hostdata->caps.flags = cpu_to_be32(CAP_LIST_SUPPORTED);
  1126. if (hostdata->client_migrated)
  1127. hostdata->caps.flags |= cpu_to_be32(CLIENT_MIGRATED);
  1128. strlcpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  1129. sizeof(hostdata->caps.name));
  1130. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1131. location = location ? location : dev_name(hostdata->dev);
  1132. strlcpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  1133. req->common.type = cpu_to_be32(VIOSRP_CAPABILITIES_TYPE);
  1134. req->buffer = cpu_to_be64(hostdata->caps_addr);
  1135. hostdata->caps.migration.common.cap_type =
  1136. cpu_to_be32(MIGRATION_CAPABILITIES);
  1137. hostdata->caps.migration.common.length =
  1138. cpu_to_be16(sizeof(hostdata->caps.migration));
  1139. hostdata->caps.migration.common.server_support =
  1140. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1141. hostdata->caps.migration.ecl = cpu_to_be32(1);
  1142. if (client_reserve) {
  1143. hostdata->caps.reserve.common.cap_type =
  1144. cpu_to_be32(RESERVATION_CAPABILITIES);
  1145. hostdata->caps.reserve.common.length =
  1146. cpu_to_be16(sizeof(hostdata->caps.reserve));
  1147. hostdata->caps.reserve.common.server_support =
  1148. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1149. hostdata->caps.reserve.type =
  1150. cpu_to_be32(CLIENT_RESERVE_SCSI_2);
  1151. req->common.length =
  1152. cpu_to_be16(sizeof(hostdata->caps));
  1153. } else
  1154. req->common.length = cpu_to_be16(sizeof(hostdata->caps) -
  1155. sizeof(hostdata->caps.reserve));
  1156. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1157. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1158. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  1159. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1160. };
  1161. /**
  1162. * fast_fail_rsp: - Handle response to MAD enable fast fail
  1163. * @evt_struct: srp_event_struct with the response
  1164. *
  1165. * Used as a "done" callback by when sending enable fast fail. Gets called
  1166. * by ibmvscsi_handle_crq()
  1167. */
  1168. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  1169. {
  1170. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1171. u16 status = be16_to_cpu(evt_struct->xfer_iu->mad.fast_fail.common.status);
  1172. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  1173. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  1174. else if (status == VIOSRP_MAD_FAILED)
  1175. dev_err(hostdata->dev, "fast_fail request failed\n");
  1176. else if (status != VIOSRP_MAD_SUCCESS)
  1177. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  1178. send_mad_capabilities(hostdata);
  1179. }
  1180. /**
  1181. * init_host - Start host initialization
  1182. * @hostdata: ibmvscsi_host_data of host
  1183. *
  1184. * Returns zero if successful.
  1185. */
  1186. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  1187. {
  1188. int rc;
  1189. unsigned long flags;
  1190. struct viosrp_fast_fail *fast_fail_mad;
  1191. struct srp_event_struct *evt_struct;
  1192. if (!fast_fail) {
  1193. send_mad_capabilities(hostdata);
  1194. return 0;
  1195. }
  1196. evt_struct = get_event_struct(&hostdata->pool);
  1197. BUG_ON(!evt_struct);
  1198. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  1199. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  1200. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  1201. fast_fail_mad->common.type = cpu_to_be32(VIOSRP_ENABLE_FAST_FAIL);
  1202. fast_fail_mad->common.length = cpu_to_be16(sizeof(*fast_fail_mad));
  1203. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1204. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1205. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1206. return rc;
  1207. }
  1208. /**
  1209. * adapter_info_rsp: - Handle response to MAD adapter info request
  1210. * @evt_struct: srp_event_struct with the response
  1211. *
  1212. * Used as a "done" callback by when sending adapter_info. Gets called
  1213. * by ibmvscsi_handle_crq()
  1214. */
  1215. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  1216. {
  1217. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1218. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  1219. dev_err(hostdata->dev, "error %d getting adapter info\n",
  1220. evt_struct->xfer_iu->mad.adapter_info.common.status);
  1221. } else {
  1222. dev_info(hostdata->dev, "host srp version: %s, "
  1223. "host partition %s (%d), OS %d, max io %u\n",
  1224. hostdata->madapter_info.srp_version,
  1225. hostdata->madapter_info.partition_name,
  1226. be32_to_cpu(hostdata->madapter_info.partition_number),
  1227. be32_to_cpu(hostdata->madapter_info.os_type),
  1228. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]));
  1229. if (hostdata->madapter_info.port_max_txu[0])
  1230. hostdata->host->max_sectors =
  1231. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]) >> 9;
  1232. if (be32_to_cpu(hostdata->madapter_info.os_type) == SRP_MAD_OS_AIX &&
  1233. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  1234. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  1235. hostdata->madapter_info.srp_version);
  1236. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  1237. MAX_INDIRECT_BUFS);
  1238. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  1239. }
  1240. if (be32_to_cpu(hostdata->madapter_info.os_type) == SRP_MAD_OS_AIX) {
  1241. enable_fast_fail(hostdata);
  1242. return;
  1243. }
  1244. }
  1245. send_srp_login(hostdata);
  1246. }
  1247. /**
  1248. * send_mad_adapter_info: - Sends the mad adapter info request
  1249. * and stores the result so it can be retrieved with
  1250. * sysfs. We COULD consider causing a failure if the
  1251. * returned SRP version doesn't match ours.
  1252. * @hostdata: ibmvscsi_host_data of host
  1253. *
  1254. * Returns zero if successful.
  1255. */
  1256. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  1257. {
  1258. struct viosrp_adapter_info *req;
  1259. struct srp_event_struct *evt_struct;
  1260. unsigned long flags;
  1261. evt_struct = get_event_struct(&hostdata->pool);
  1262. BUG_ON(!evt_struct);
  1263. init_event_struct(evt_struct,
  1264. adapter_info_rsp,
  1265. VIOSRP_MAD_FORMAT,
  1266. info_timeout);
  1267. req = &evt_struct->iu.mad.adapter_info;
  1268. memset(req, 0x00, sizeof(*req));
  1269. req->common.type = cpu_to_be32(VIOSRP_ADAPTER_INFO_TYPE);
  1270. req->common.length = cpu_to_be16(sizeof(hostdata->madapter_info));
  1271. req->buffer = cpu_to_be64(hostdata->adapter_info_addr);
  1272. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1273. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1274. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  1275. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1276. };
  1277. /**
  1278. * init_adapter: Start virtual adapter initialization sequence
  1279. *
  1280. */
  1281. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  1282. {
  1283. send_mad_adapter_info(hostdata);
  1284. }
  1285. /**
  1286. * sync_completion: Signal that a synchronous command has completed
  1287. * Note that after returning from this call, the evt_struct is freed.
  1288. * the caller waiting on this completion shouldn't touch the evt_struct
  1289. * again.
  1290. */
  1291. static void sync_completion(struct srp_event_struct *evt_struct)
  1292. {
  1293. /* copy the response back */
  1294. if (evt_struct->sync_srp)
  1295. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1296. complete(&evt_struct->comp);
  1297. }
  1298. /**
  1299. * ibmvscsi_abort: Abort a command...from scsi host template
  1300. * send this over to the server and wait synchronously for the response
  1301. */
  1302. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1303. {
  1304. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1305. struct srp_tsk_mgmt *tsk_mgmt;
  1306. struct srp_event_struct *evt;
  1307. struct srp_event_struct *tmp_evt, *found_evt;
  1308. union viosrp_iu srp_rsp;
  1309. int rsp_rc;
  1310. unsigned long flags;
  1311. u16 lun = lun_from_dev(cmd->device);
  1312. unsigned long wait_switch = 0;
  1313. /* First, find this command in our sent list so we can figure
  1314. * out the correct tag
  1315. */
  1316. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1317. wait_switch = jiffies + (init_timeout * HZ);
  1318. do {
  1319. found_evt = NULL;
  1320. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1321. if (tmp_evt->cmnd == cmd) {
  1322. found_evt = tmp_evt;
  1323. break;
  1324. }
  1325. }
  1326. if (!found_evt) {
  1327. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1328. return SUCCESS;
  1329. }
  1330. evt = get_event_struct(&hostdata->pool);
  1331. if (evt == NULL) {
  1332. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1333. sdev_printk(KERN_ERR, cmd->device,
  1334. "failed to allocate abort event\n");
  1335. return FAILED;
  1336. }
  1337. init_event_struct(evt,
  1338. sync_completion,
  1339. VIOSRP_SRP_FORMAT,
  1340. abort_timeout);
  1341. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1342. /* Set up an abort SRP command */
  1343. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1344. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1345. int_to_scsilun(lun, &tsk_mgmt->lun);
  1346. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1347. tsk_mgmt->task_tag = (u64) found_evt;
  1348. evt->sync_srp = &srp_rsp;
  1349. init_completion(&evt->comp);
  1350. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1351. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1352. break;
  1353. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1354. msleep(10);
  1355. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1356. } while (time_before(jiffies, wait_switch));
  1357. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1358. if (rsp_rc != 0) {
  1359. sdev_printk(KERN_ERR, cmd->device,
  1360. "failed to send abort() event. rc=%d\n", rsp_rc);
  1361. return FAILED;
  1362. }
  1363. sdev_printk(KERN_INFO, cmd->device,
  1364. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1365. (((u64) lun) << 48), (u64) found_evt);
  1366. wait_for_completion(&evt->comp);
  1367. /* make sure we got a good response */
  1368. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1369. if (printk_ratelimit())
  1370. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1371. srp_rsp.srp.rsp.opcode);
  1372. return FAILED;
  1373. }
  1374. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1375. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1376. else
  1377. rsp_rc = srp_rsp.srp.rsp.status;
  1378. if (rsp_rc) {
  1379. if (printk_ratelimit())
  1380. sdev_printk(KERN_WARNING, cmd->device,
  1381. "abort code %d for task tag 0x%llx\n",
  1382. rsp_rc, tsk_mgmt->task_tag);
  1383. return FAILED;
  1384. }
  1385. /* Because we dropped the spinlock above, it's possible
  1386. * The event is no longer in our list. Make sure it didn't
  1387. * complete while we were aborting
  1388. */
  1389. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1390. found_evt = NULL;
  1391. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1392. if (tmp_evt->cmnd == cmd) {
  1393. found_evt = tmp_evt;
  1394. break;
  1395. }
  1396. }
  1397. if (found_evt == NULL) {
  1398. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1399. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1400. tsk_mgmt->task_tag);
  1401. return SUCCESS;
  1402. }
  1403. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1404. tsk_mgmt->task_tag);
  1405. cmd->result = (DID_ABORT << 16);
  1406. list_del(&found_evt->list);
  1407. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1408. found_evt->hostdata->dev);
  1409. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1410. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1411. atomic_inc(&hostdata->request_limit);
  1412. return SUCCESS;
  1413. }
  1414. /**
  1415. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1416. * template send this over to the server and wait synchronously for the
  1417. * response
  1418. */
  1419. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1420. {
  1421. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1422. struct srp_tsk_mgmt *tsk_mgmt;
  1423. struct srp_event_struct *evt;
  1424. struct srp_event_struct *tmp_evt, *pos;
  1425. union viosrp_iu srp_rsp;
  1426. int rsp_rc;
  1427. unsigned long flags;
  1428. u16 lun = lun_from_dev(cmd->device);
  1429. unsigned long wait_switch = 0;
  1430. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1431. wait_switch = jiffies + (init_timeout * HZ);
  1432. do {
  1433. evt = get_event_struct(&hostdata->pool);
  1434. if (evt == NULL) {
  1435. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1436. sdev_printk(KERN_ERR, cmd->device,
  1437. "failed to allocate reset event\n");
  1438. return FAILED;
  1439. }
  1440. init_event_struct(evt,
  1441. sync_completion,
  1442. VIOSRP_SRP_FORMAT,
  1443. reset_timeout);
  1444. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1445. /* Set up a lun reset SRP command */
  1446. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1447. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1448. int_to_scsilun(lun, &tsk_mgmt->lun);
  1449. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1450. evt->sync_srp = &srp_rsp;
  1451. init_completion(&evt->comp);
  1452. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1453. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1454. break;
  1455. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1456. msleep(10);
  1457. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1458. } while (time_before(jiffies, wait_switch));
  1459. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1460. if (rsp_rc != 0) {
  1461. sdev_printk(KERN_ERR, cmd->device,
  1462. "failed to send reset event. rc=%d\n", rsp_rc);
  1463. return FAILED;
  1464. }
  1465. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1466. (((u64) lun) << 48));
  1467. wait_for_completion(&evt->comp);
  1468. /* make sure we got a good response */
  1469. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1470. if (printk_ratelimit())
  1471. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1472. srp_rsp.srp.rsp.opcode);
  1473. return FAILED;
  1474. }
  1475. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1476. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1477. else
  1478. rsp_rc = srp_rsp.srp.rsp.status;
  1479. if (rsp_rc) {
  1480. if (printk_ratelimit())
  1481. sdev_printk(KERN_WARNING, cmd->device,
  1482. "reset code %d for task tag 0x%llx\n",
  1483. rsp_rc, tsk_mgmt->task_tag);
  1484. return FAILED;
  1485. }
  1486. /* We need to find all commands for this LUN that have not yet been
  1487. * responded to, and fail them with DID_RESET
  1488. */
  1489. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1490. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1491. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1492. if (tmp_evt->cmnd)
  1493. tmp_evt->cmnd->result = (DID_RESET << 16);
  1494. list_del(&tmp_evt->list);
  1495. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1496. tmp_evt->hostdata->dev);
  1497. free_event_struct(&tmp_evt->hostdata->pool,
  1498. tmp_evt);
  1499. atomic_inc(&hostdata->request_limit);
  1500. if (tmp_evt->cmnd_done)
  1501. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1502. else if (tmp_evt->done)
  1503. tmp_evt->done(tmp_evt);
  1504. }
  1505. }
  1506. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1507. return SUCCESS;
  1508. }
  1509. /**
  1510. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1511. * @cmd: struct scsi_cmnd having problems
  1512. */
  1513. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1514. {
  1515. unsigned long wait_switch = 0;
  1516. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1517. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1518. ibmvscsi_reset_host(hostdata);
  1519. for (wait_switch = jiffies + (init_timeout * HZ);
  1520. time_before(jiffies, wait_switch) &&
  1521. atomic_read(&hostdata->request_limit) < 2;) {
  1522. msleep(10);
  1523. }
  1524. if (atomic_read(&hostdata->request_limit) <= 0)
  1525. return FAILED;
  1526. return SUCCESS;
  1527. }
  1528. /**
  1529. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1530. * @crq: Command/Response queue
  1531. * @hostdata: ibmvscsi_host_data of host
  1532. *
  1533. */
  1534. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1535. struct ibmvscsi_host_data *hostdata)
  1536. {
  1537. long rc;
  1538. unsigned long flags;
  1539. /* The hypervisor copies our tag value here so no byteswapping */
  1540. struct srp_event_struct *evt_struct =
  1541. (__force struct srp_event_struct *)crq->IU_data_ptr;
  1542. switch (crq->valid) {
  1543. case VIOSRP_CRQ_INIT_RSP: /* initialization */
  1544. switch (crq->format) {
  1545. case VIOSRP_CRQ_INIT: /* Initialization message */
  1546. dev_info(hostdata->dev, "partner initialized\n");
  1547. /* Send back a response */
  1548. rc = ibmvscsi_send_crq(hostdata, 0xC002000000000000LL, 0);
  1549. if (rc == 0) {
  1550. /* Now login */
  1551. init_adapter(hostdata);
  1552. } else {
  1553. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1554. }
  1555. break;
  1556. case VIOSRP_CRQ_INIT_COMPLETE: /* Initialization response */
  1557. dev_info(hostdata->dev, "partner initialization complete\n");
  1558. /* Now login */
  1559. init_adapter(hostdata);
  1560. break;
  1561. default:
  1562. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1563. }
  1564. return;
  1565. case VIOSRP_CRQ_XPORT_EVENT: /* Hypervisor telling us the connection is closed */
  1566. scsi_block_requests(hostdata->host);
  1567. atomic_set(&hostdata->request_limit, 0);
  1568. if (crq->format == 0x06) {
  1569. /* We need to re-setup the interpartition connection */
  1570. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1571. hostdata->client_migrated = 1;
  1572. hostdata->reenable_crq = 1;
  1573. purge_requests(hostdata, DID_REQUEUE);
  1574. wake_up(&hostdata->work_wait_q);
  1575. } else {
  1576. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1577. crq->format);
  1578. ibmvscsi_reset_host(hostdata);
  1579. }
  1580. return;
  1581. case VIOSRP_CRQ_CMD_RSP: /* real payload */
  1582. break;
  1583. default:
  1584. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1585. crq->valid);
  1586. return;
  1587. }
  1588. /* The only kind of payload CRQs we should get are responses to
  1589. * things we send. Make sure this response is to something we
  1590. * actually sent
  1591. */
  1592. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1593. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1594. evt_struct);
  1595. return;
  1596. }
  1597. if (atomic_read(&evt_struct->free)) {
  1598. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1599. evt_struct);
  1600. return;
  1601. }
  1602. if (crq->format == VIOSRP_SRP_FORMAT)
  1603. atomic_add(be32_to_cpu(evt_struct->xfer_iu->srp.rsp.req_lim_delta),
  1604. &hostdata->request_limit);
  1605. del_timer(&evt_struct->timer);
  1606. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1607. evt_struct->cmnd->result = DID_ERROR << 16;
  1608. if (evt_struct->done)
  1609. evt_struct->done(evt_struct);
  1610. else
  1611. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1612. /*
  1613. * Lock the host_lock before messing with these structures, since we
  1614. * are running in a task context
  1615. */
  1616. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1617. list_del(&evt_struct->list);
  1618. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1619. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1620. }
  1621. /**
  1622. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1623. * @sdev: struct scsi_device device to configure
  1624. *
  1625. * Enable allow_restart for a device if it is a disk. Adjust the
  1626. * queue_depth here also as is required by the documentation for
  1627. * struct scsi_host_template.
  1628. */
  1629. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1630. {
  1631. struct Scsi_Host *shost = sdev->host;
  1632. unsigned long lock_flags = 0;
  1633. spin_lock_irqsave(shost->host_lock, lock_flags);
  1634. if (sdev->type == TYPE_DISK) {
  1635. sdev->allow_restart = 1;
  1636. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1637. }
  1638. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1639. return 0;
  1640. }
  1641. /**
  1642. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1643. * @sdev: scsi device struct
  1644. * @qdepth: depth to set
  1645. * @reason: calling context
  1646. *
  1647. * Return value:
  1648. * actual depth set
  1649. **/
  1650. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1651. {
  1652. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1653. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1654. return scsi_change_queue_depth(sdev, qdepth);
  1655. }
  1656. /* ------------------------------------------------------------
  1657. * sysfs attributes
  1658. */
  1659. static ssize_t show_host_vhost_loc(struct device *dev,
  1660. struct device_attribute *attr, char *buf)
  1661. {
  1662. struct Scsi_Host *shost = class_to_shost(dev);
  1663. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1664. int len;
  1665. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1666. hostdata->caps.loc);
  1667. return len;
  1668. }
  1669. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1670. .attr = {
  1671. .name = "vhost_loc",
  1672. .mode = S_IRUGO,
  1673. },
  1674. .show = show_host_vhost_loc,
  1675. };
  1676. static ssize_t show_host_vhost_name(struct device *dev,
  1677. struct device_attribute *attr, char *buf)
  1678. {
  1679. struct Scsi_Host *shost = class_to_shost(dev);
  1680. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1681. int len;
  1682. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1683. hostdata->caps.name);
  1684. return len;
  1685. }
  1686. static struct device_attribute ibmvscsi_host_vhost_name = {
  1687. .attr = {
  1688. .name = "vhost_name",
  1689. .mode = S_IRUGO,
  1690. },
  1691. .show = show_host_vhost_name,
  1692. };
  1693. static ssize_t show_host_srp_version(struct device *dev,
  1694. struct device_attribute *attr, char *buf)
  1695. {
  1696. struct Scsi_Host *shost = class_to_shost(dev);
  1697. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1698. int len;
  1699. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1700. hostdata->madapter_info.srp_version);
  1701. return len;
  1702. }
  1703. static struct device_attribute ibmvscsi_host_srp_version = {
  1704. .attr = {
  1705. .name = "srp_version",
  1706. .mode = S_IRUGO,
  1707. },
  1708. .show = show_host_srp_version,
  1709. };
  1710. static ssize_t show_host_partition_name(struct device *dev,
  1711. struct device_attribute *attr,
  1712. char *buf)
  1713. {
  1714. struct Scsi_Host *shost = class_to_shost(dev);
  1715. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1716. int len;
  1717. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1718. hostdata->madapter_info.partition_name);
  1719. return len;
  1720. }
  1721. static struct device_attribute ibmvscsi_host_partition_name = {
  1722. .attr = {
  1723. .name = "partition_name",
  1724. .mode = S_IRUGO,
  1725. },
  1726. .show = show_host_partition_name,
  1727. };
  1728. static ssize_t show_host_partition_number(struct device *dev,
  1729. struct device_attribute *attr,
  1730. char *buf)
  1731. {
  1732. struct Scsi_Host *shost = class_to_shost(dev);
  1733. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1734. int len;
  1735. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1736. be32_to_cpu(hostdata->madapter_info.partition_number));
  1737. return len;
  1738. }
  1739. static struct device_attribute ibmvscsi_host_partition_number = {
  1740. .attr = {
  1741. .name = "partition_number",
  1742. .mode = S_IRUGO,
  1743. },
  1744. .show = show_host_partition_number,
  1745. };
  1746. static ssize_t show_host_mad_version(struct device *dev,
  1747. struct device_attribute *attr, char *buf)
  1748. {
  1749. struct Scsi_Host *shost = class_to_shost(dev);
  1750. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1751. int len;
  1752. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1753. be32_to_cpu(hostdata->madapter_info.mad_version));
  1754. return len;
  1755. }
  1756. static struct device_attribute ibmvscsi_host_mad_version = {
  1757. .attr = {
  1758. .name = "mad_version",
  1759. .mode = S_IRUGO,
  1760. },
  1761. .show = show_host_mad_version,
  1762. };
  1763. static ssize_t show_host_os_type(struct device *dev,
  1764. struct device_attribute *attr, char *buf)
  1765. {
  1766. struct Scsi_Host *shost = class_to_shost(dev);
  1767. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1768. int len;
  1769. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1770. be32_to_cpu(hostdata->madapter_info.os_type));
  1771. return len;
  1772. }
  1773. static struct device_attribute ibmvscsi_host_os_type = {
  1774. .attr = {
  1775. .name = "os_type",
  1776. .mode = S_IRUGO,
  1777. },
  1778. .show = show_host_os_type,
  1779. };
  1780. static ssize_t show_host_config(struct device *dev,
  1781. struct device_attribute *attr, char *buf)
  1782. {
  1783. return 0;
  1784. }
  1785. static struct device_attribute ibmvscsi_host_config = {
  1786. .attr = {
  1787. .name = "config",
  1788. .mode = S_IRUGO,
  1789. },
  1790. .show = show_host_config,
  1791. };
  1792. static struct device_attribute *ibmvscsi_attrs[] = {
  1793. &ibmvscsi_host_vhost_loc,
  1794. &ibmvscsi_host_vhost_name,
  1795. &ibmvscsi_host_srp_version,
  1796. &ibmvscsi_host_partition_name,
  1797. &ibmvscsi_host_partition_number,
  1798. &ibmvscsi_host_mad_version,
  1799. &ibmvscsi_host_os_type,
  1800. &ibmvscsi_host_config,
  1801. NULL
  1802. };
  1803. /* ------------------------------------------------------------
  1804. * SCSI driver registration
  1805. */
  1806. static struct scsi_host_template driver_template = {
  1807. .module = THIS_MODULE,
  1808. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1809. .proc_name = "ibmvscsi",
  1810. .queuecommand = ibmvscsi_queuecommand,
  1811. .eh_timed_out = srp_timed_out,
  1812. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1813. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1814. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1815. .slave_configure = ibmvscsi_slave_configure,
  1816. .change_queue_depth = ibmvscsi_change_queue_depth,
  1817. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1818. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1819. .this_id = -1,
  1820. .sg_tablesize = SG_ALL,
  1821. .use_clustering = ENABLE_CLUSTERING,
  1822. .shost_attrs = ibmvscsi_attrs,
  1823. };
  1824. /**
  1825. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1826. *
  1827. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1828. *
  1829. * Return value:
  1830. * Number of bytes of IO data the driver will need to perform well.
  1831. */
  1832. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1833. {
  1834. /* iu_storage data allocated in initialize_event_pool */
  1835. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1836. /* add io space for sg data */
  1837. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1838. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1839. return desired_io;
  1840. }
  1841. static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  1842. {
  1843. int rc;
  1844. char *action = "reset";
  1845. if (hostdata->reset_crq) {
  1846. smp_rmb();
  1847. hostdata->reset_crq = 0;
  1848. rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  1849. if (!rc)
  1850. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1851. vio_enable_interrupts(to_vio_dev(hostdata->dev));
  1852. } else if (hostdata->reenable_crq) {
  1853. smp_rmb();
  1854. action = "enable";
  1855. rc = ibmvscsi_reenable_crq_queue(&hostdata->queue, hostdata);
  1856. hostdata->reenable_crq = 0;
  1857. if (!rc)
  1858. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1859. } else
  1860. return;
  1861. if (rc) {
  1862. atomic_set(&hostdata->request_limit, -1);
  1863. dev_err(hostdata->dev, "error after %s\n", action);
  1864. }
  1865. scsi_unblock_requests(hostdata->host);
  1866. }
  1867. static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
  1868. {
  1869. if (kthread_should_stop())
  1870. return 1;
  1871. else if (hostdata->reset_crq) {
  1872. smp_rmb();
  1873. return 1;
  1874. } else if (hostdata->reenable_crq) {
  1875. smp_rmb();
  1876. return 1;
  1877. }
  1878. return 0;
  1879. }
  1880. static int ibmvscsi_work(void *data)
  1881. {
  1882. struct ibmvscsi_host_data *hostdata = data;
  1883. int rc;
  1884. set_user_nice(current, MIN_NICE);
  1885. while (1) {
  1886. rc = wait_event_interruptible(hostdata->work_wait_q,
  1887. ibmvscsi_work_to_do(hostdata));
  1888. BUG_ON(rc);
  1889. if (kthread_should_stop())
  1890. break;
  1891. ibmvscsi_do_work(hostdata);
  1892. }
  1893. return 0;
  1894. }
  1895. /**
  1896. * Called by bus code for each adapter
  1897. */
  1898. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1899. {
  1900. struct ibmvscsi_host_data *hostdata;
  1901. struct Scsi_Host *host;
  1902. struct device *dev = &vdev->dev;
  1903. struct srp_rport_identifiers ids;
  1904. struct srp_rport *rport;
  1905. unsigned long wait_switch = 0;
  1906. int rc;
  1907. dev_set_drvdata(&vdev->dev, NULL);
  1908. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1909. if (!host) {
  1910. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1911. goto scsi_host_alloc_failed;
  1912. }
  1913. host->transportt = ibmvscsi_transport_template;
  1914. hostdata = shost_priv(host);
  1915. memset(hostdata, 0x00, sizeof(*hostdata));
  1916. INIT_LIST_HEAD(&hostdata->sent);
  1917. init_waitqueue_head(&hostdata->work_wait_q);
  1918. hostdata->host = host;
  1919. hostdata->dev = dev;
  1920. atomic_set(&hostdata->request_limit, -1);
  1921. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1922. if (map_persist_bufs(hostdata)) {
  1923. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1924. goto persist_bufs_failed;
  1925. }
  1926. hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
  1927. "ibmvscsi", host->host_no);
  1928. if (IS_ERR(hostdata->work_thread)) {
  1929. dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
  1930. PTR_ERR(hostdata->work_thread));
  1931. goto init_crq_failed;
  1932. }
  1933. rc = ibmvscsi_init_crq_queue(&hostdata->queue, hostdata, max_events);
  1934. if (rc != 0 && rc != H_RESOURCE) {
  1935. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1936. goto kill_kthread;
  1937. }
  1938. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1939. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1940. goto init_pool_failed;
  1941. }
  1942. host->max_lun = IBMVSCSI_MAX_LUN;
  1943. host->max_id = max_id;
  1944. host->max_channel = max_channel;
  1945. host->max_cmd_len = 16;
  1946. dev_info(dev,
  1947. "Maximum ID: %d Maximum LUN: %llu Maximum Channel: %d\n",
  1948. host->max_id, host->max_lun, host->max_channel);
  1949. if (scsi_add_host(hostdata->host, hostdata->dev))
  1950. goto add_host_failed;
  1951. /* we don't have a proper target_port_id so let's use the fake one */
  1952. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1953. sizeof(ids.port_id));
  1954. ids.roles = SRP_RPORT_ROLE_TARGET;
  1955. rport = srp_rport_add(host, &ids);
  1956. if (IS_ERR(rport))
  1957. goto add_srp_port_failed;
  1958. /* Try to send an initialization message. Note that this is allowed
  1959. * to fail if the other end is not acive. In that case we don't
  1960. * want to scan
  1961. */
  1962. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1963. || rc == H_RESOURCE) {
  1964. /*
  1965. * Wait around max init_timeout secs for the adapter to finish
  1966. * initializing. When we are done initializing, we will have a
  1967. * valid request_limit. We don't want Linux scanning before
  1968. * we are ready.
  1969. */
  1970. for (wait_switch = jiffies + (init_timeout * HZ);
  1971. time_before(jiffies, wait_switch) &&
  1972. atomic_read(&hostdata->request_limit) < 2;) {
  1973. msleep(10);
  1974. }
  1975. /* if we now have a valid request_limit, initiate a scan */
  1976. if (atomic_read(&hostdata->request_limit) > 0)
  1977. scsi_scan_host(host);
  1978. }
  1979. dev_set_drvdata(&vdev->dev, hostdata);
  1980. spin_lock(&ibmvscsi_driver_lock);
  1981. list_add_tail(&hostdata->host_list, &ibmvscsi_head);
  1982. spin_unlock(&ibmvscsi_driver_lock);
  1983. return 0;
  1984. add_srp_port_failed:
  1985. scsi_remove_host(hostdata->host);
  1986. add_host_failed:
  1987. release_event_pool(&hostdata->pool, hostdata);
  1988. init_pool_failed:
  1989. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_events);
  1990. kill_kthread:
  1991. kthread_stop(hostdata->work_thread);
  1992. init_crq_failed:
  1993. unmap_persist_bufs(hostdata);
  1994. persist_bufs_failed:
  1995. scsi_host_put(host);
  1996. scsi_host_alloc_failed:
  1997. return -1;
  1998. }
  1999. static int ibmvscsi_remove(struct vio_dev *vdev)
  2000. {
  2001. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  2002. srp_remove_host(hostdata->host);
  2003. scsi_remove_host(hostdata->host);
  2004. purge_requests(hostdata, DID_ERROR);
  2005. release_event_pool(&hostdata->pool, hostdata);
  2006. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  2007. max_events);
  2008. kthread_stop(hostdata->work_thread);
  2009. unmap_persist_bufs(hostdata);
  2010. spin_lock(&ibmvscsi_driver_lock);
  2011. list_del(&hostdata->host_list);
  2012. spin_unlock(&ibmvscsi_driver_lock);
  2013. scsi_host_put(hostdata->host);
  2014. return 0;
  2015. }
  2016. /**
  2017. * ibmvscsi_resume: Resume from suspend
  2018. * @dev: device struct
  2019. *
  2020. * We may have lost an interrupt across suspend/resume, so kick the
  2021. * interrupt handler
  2022. */
  2023. static int ibmvscsi_resume(struct device *dev)
  2024. {
  2025. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  2026. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  2027. tasklet_schedule(&hostdata->srp_task);
  2028. return 0;
  2029. }
  2030. /**
  2031. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  2032. * support.
  2033. */
  2034. static const struct vio_device_id ibmvscsi_device_table[] = {
  2035. {"vscsi", "IBM,v-scsi"},
  2036. { "", "" }
  2037. };
  2038. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  2039. static const struct dev_pm_ops ibmvscsi_pm_ops = {
  2040. .resume = ibmvscsi_resume
  2041. };
  2042. static struct vio_driver ibmvscsi_driver = {
  2043. .id_table = ibmvscsi_device_table,
  2044. .probe = ibmvscsi_probe,
  2045. .remove = ibmvscsi_remove,
  2046. .get_desired_dma = ibmvscsi_get_desired_dma,
  2047. .name = "ibmvscsi",
  2048. .pm = &ibmvscsi_pm_ops,
  2049. };
  2050. static struct srp_function_template ibmvscsi_transport_functions = {
  2051. };
  2052. int __init ibmvscsi_module_init(void)
  2053. {
  2054. int ret;
  2055. /* Ensure we have two requests to do error recovery */
  2056. driver_template.can_queue = max_requests;
  2057. max_events = max_requests + 2;
  2058. if (!firmware_has_feature(FW_FEATURE_VIO))
  2059. return -ENODEV;
  2060. ibmvscsi_transport_template =
  2061. srp_attach_transport(&ibmvscsi_transport_functions);
  2062. if (!ibmvscsi_transport_template)
  2063. return -ENOMEM;
  2064. ret = vio_register_driver(&ibmvscsi_driver);
  2065. if (ret)
  2066. srp_release_transport(ibmvscsi_transport_template);
  2067. return ret;
  2068. }
  2069. void __exit ibmvscsi_module_exit(void)
  2070. {
  2071. vio_unregister_driver(&ibmvscsi_driver);
  2072. srp_release_transport(ibmvscsi_transport_template);
  2073. }
  2074. module_init(ibmvscsi_module_init);
  2075. module_exit(ibmvscsi_module_exit);